0
|
1 |
/*
|
|
2 |
This file is part of CanFestival, a library implementing CanOpen Stack.
|
|
3 |
|
|
4 |
Copyright (C): Edouard TISSERANT and Francis DUPIN
|
|
5 |
|
|
6 |
See COPYING file for copyrights details.
|
|
7 |
|
|
8 |
This library is free software; you can redistribute it and/or
|
|
9 |
modify it under the terms of the GNU Lesser General Public
|
|
10 |
License as published by the Free Software Foundation; either
|
|
11 |
version 2.1 of the License, or (at your option) any later version.
|
|
12 |
|
|
13 |
This library is distributed in the hope that it will be useful,
|
|
14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
16 |
Lesser General Public License for more details.
|
|
17 |
|
|
18 |
You should have received a copy of the GNU Lesser General Public
|
|
19 |
License along with this library; if not, write to the Free Software
|
|
20 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
21 |
*/
|
|
22 |
|
|
23 |
#include <stdio.h>
|
|
24 |
#include <string.h>
|
|
25 |
#include <sys/time.h>
|
|
26 |
#include <unistd.h>
|
|
27 |
#include <stdlib.h>
|
|
28 |
#include <signal.h>
|
|
29 |
|
|
30 |
#include <applicfg.h>
|
|
31 |
#include <can_driver.h>
|
|
32 |
#include <timers_driver.h>
|
|
33 |
|
|
34 |
#include "Master.h"
|
|
35 |
#include "Slave.h"
|
|
36 |
#include "TestMasterSlave.h"
|
|
37 |
|
|
38 |
#define MyCase(fc) case fc: eprintf(#fc);break;
|
|
39 |
void print_message(Message *m)
|
|
40 |
{
|
|
41 |
int i;
|
|
42 |
switch(m->cob_id.w >> 7)
|
|
43 |
{
|
|
44 |
MyCase(SYNC)
|
|
45 |
MyCase(TIME_STAMP)
|
|
46 |
MyCase(PDO1tx)
|
|
47 |
MyCase(PDO1rx)
|
|
48 |
MyCase(PDO2tx)
|
|
49 |
MyCase(PDO2rx)
|
|
50 |
MyCase(PDO3tx)
|
|
51 |
MyCase(PDO3rx)
|
|
52 |
MyCase(PDO4tx)
|
|
53 |
MyCase(PDO4rx)
|
|
54 |
MyCase(SDOtx)
|
|
55 |
MyCase(SDOrx)
|
|
56 |
MyCase(NODE_GUARD)
|
|
57 |
MyCase(NMT)
|
|
58 |
}
|
|
59 |
eprintf(" rtr:%d", m->rtr);
|
|
60 |
eprintf(" len:%d", m->len);
|
|
61 |
for (i = 0 ; i < m->len ; i++)
|
|
62 |
eprintf(" %02x", m->data[i]);
|
|
63 |
eprintf("\n");
|
|
64 |
}
|
|
65 |
|
|
66 |
UNS32 OnMasterMap1Update(CO_Data* d, const indextable * unsused_indextable, UNS8 unsused_bSubindex)
|
|
67 |
{
|
|
68 |
eprintf("OnSlaveMap1Update:%d\n", SlaveMap1);
|
31
|
69 |
return 0;
|
0
|
70 |
}
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
void InitNodes(CO_Data* d, UNS32 id)
|
|
75 |
{
|
|
76 |
// TestSlave_Index2000_callbacks[0] = &OnMasterMap1Update;
|
|
77 |
RegisterSetODentryCallBack(&TestMaster_Data, 0x2000, 0, &OnMasterMap1Update);
|
|
78 |
|
|
79 |
/****************************** INITIALISATION MASTER *******************************/
|
|
80 |
/* Defining the node Id */
|
|
81 |
setNodeId(&TestMaster_Data, 0x01);
|
|
82 |
|
|
83 |
/* init */
|
|
84 |
setState(&TestMaster_Data, Initialisation);
|
|
85 |
|
|
86 |
/****************************** INITIALISATION SLAVE *******************************/
|
|
87 |
/* Defining the node Id */
|
|
88 |
setNodeId(&TestSlave_Data, 0x02);
|
|
89 |
|
|
90 |
/* init */
|
|
91 |
setState(&TestSlave_Data, Initialisation);
|
|
92 |
|
|
93 |
/****************************** START *******************************/
|
|
94 |
/* Put the master in operational mode */
|
|
95 |
setState(&TestMaster_Data, Operational);
|
|
96 |
|
|
97 |
masterSendNMTstateChange (&TestMaster_Data, 0x02, NMT_Start_Node);
|
|
98 |
|
|
99 |
}
|
|
100 |
|
|
101 |
CAN_HANDLE SlaveCanHandle;
|
|
102 |
CAN_HANDLE MasterCanHandle;
|
|
103 |
|
|
104 |
// Baudrate values for Peak board :
|
|
105 |
// CAN_BAUD_1M CAN_BAUD_500K CAN_BAUD_250K CAN_BAUD_125K CAN_BAUD_100K CAN_BAUD_50K
|
|
106 |
// CAN_BAUD_20K CAN_BAUD_10K CAN_BAUD_5K
|
|
107 |
|
|
108 |
#ifdef CAN_BAUD_500K
|
|
109 |
// Appli have been compiled for Peak. Baudrate is defined
|
|
110 |
# define BAUDRATE CAN_BAUD_500K
|
|
111 |
#else
|
|
112 |
// Appli have been compiled for Generic. Baudrate not used
|
|
113 |
# define BAUDRATE 0
|
|
114 |
#endif
|
|
115 |
|
|
116 |
void catch_signal(int sig)
|
|
117 |
{
|
|
118 |
signal(SIGTERM, catch_signal);
|
|
119 |
signal(SIGINT, catch_signal);
|
32
|
120 |
eprintf("Got Signal %d\n",sig);
|
|
121 |
}
|
0
|
122 |
|
|
123 |
void help()
|
|
124 |
{
|
|
125 |
printf("**************************************************************\n");
|
|
126 |
printf("* TestMasterSlave *\n");
|
|
127 |
printf("* *\n");
|
|
128 |
printf("* A simple example for PC. It does implement 2 CanOpen *\n");
|
|
129 |
printf("* nodes in the same process. A master and a slave. Both *\n");
|
|
130 |
printf("* communicate together, exchanging periodically NMT, SYNC, *\n");
|
|
131 |
printf("* SDO and PDO. *\n");
|
|
132 |
printf("* *\n");
|
|
133 |
printf("* If you have chosen virtual CAN driver, just type *\n");
|
|
134 |
printf("* ./TestMasterSlave *\n");
|
|
135 |
printf("* *\n");
|
31
|
136 |
printf("* Else you need to specify bus: *\n");
|
0
|
137 |
printf("* *\n");
|
|
138 |
printf("* -s : slave CAN bus [default 0, peak first PCI] *\n");
|
|
139 |
printf("* -m : master CAN bus [default 1, peak second PCI] *\n");
|
|
140 |
printf("* *\n");
|
|
141 |
printf("**************************************************************\n");
|
|
142 |
}
|
|
143 |
|
|
144 |
/****************************************************************************/
|
|
145 |
/*************************** MAIN *****************************************/
|
|
146 |
/****************************************************************************/
|
|
147 |
int main(int argc,char **argv)
|
|
148 |
{
|
|
149 |
s_BOARD SlaveBoard = {"0", BAUDRATE, &TestSlave_Data};
|
|
150 |
s_BOARD MasterBoard = {"1", BAUDRATE, &TestMaster_Data};
|
|
151 |
|
|
152 |
|
|
153 |
char c;
|
|
154 |
extern char *optarg;
|
|
155 |
|
|
156 |
while ((c = getopt(argc, argv, "-m:s:")) != EOF)
|
|
157 |
{
|
|
158 |
switch(c)
|
|
159 |
{
|
|
160 |
case 's' :
|
|
161 |
if (optarg[0] == 0)
|
|
162 |
{
|
|
163 |
help();
|
|
164 |
exit(1);
|
|
165 |
}
|
|
166 |
SlaveBoard.busname = optarg;
|
|
167 |
break;
|
|
168 |
case 'm' :
|
|
169 |
if (optarg[0] == 0)
|
|
170 |
{
|
|
171 |
help();
|
|
172 |
exit(1);
|
|
173 |
}
|
|
174 |
MasterBoard.busname = optarg;
|
|
175 |
break;
|
|
176 |
default:
|
|
177 |
help();
|
|
178 |
exit(1);
|
|
179 |
}
|
|
180 |
}
|
|
181 |
|
|
182 |
/* install signal handler for manual break */
|
|
183 |
signal(SIGTERM, catch_signal);
|
|
184 |
signal(SIGINT, catch_signal);
|
|
185 |
|
|
186 |
// Open CAN devices
|
|
187 |
SlaveCanHandle = canOpen(&SlaveBoard);
|
|
188 |
MasterCanHandle = canOpen(&MasterBoard);
|
|
189 |
|
32
|
190 |
// Start timer thread
|
|
191 |
StartTimerLoop(&InitNodes);
|
|
192 |
|
|
193 |
// wait Ctrl-C
|
|
194 |
pause();
|
|
195 |
eprintf("Finishing.\n");
|
|
196 |
|
|
197 |
// Stop timer thread
|
|
198 |
StopTimerLoop();
|
|
199 |
|
|
200 |
// Close CAN devices (and can threads)
|
0
|
201 |
canClose(SlaveCanHandle);
|
|
202 |
canClose(MasterCanHandle);
|
|
203 |
|
|
204 |
|
|
205 |
return 0;
|
|
206 |
}
|