2 This file is part of CanFestival, a library implementing CanOpen Stack.
4 Copyright (C): Edouard TISSERANT and Francis DUPIN
6 See COPYING file for copyrights details.
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.
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.
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
23 #if defined(WIN32) && !defined(__CYGWIN__)
38 #include "canfestival.h"
39 //#include <can_driver.h>
40 //#include <timers_driver.h>
45 #include "TestMasterSlaveLSS.h"
47 s_BOARD SlaveBoardA = {"0", "125K"};
48 s_BOARD SlaveBoardB = {"1", "125K"};
49 s_BOARD MasterBoard = {"2", "125K"};
51 #if !defined(WIN32) || defined(__CYGWIN__)
52 void catch_signal(int sig)
54 signal(SIGTERM, catch_signal);
55 signal(SIGINT, catch_signal);
56 eprintf("Got Signal %d\n",sig);
62 printf("**************************************************************\n");
63 printf("* TestMasterSlaveLSS *\n");
65 printf("* A LSS example for PC. It does implement 3 CanOpen *\n");
66 printf("* nodes in the same process. A master and 2 slaves. All *\n");
67 printf("* communicate together, exchanging periodically NMT, SYNC, *\n");
68 printf("* SDO and PDO. Master configure heartbeat producer time *\n");
69 printf("* at 1000 ms for the slaves by concise DCF. *\n");
71 printf("* Usage: *\n");
72 printf("* ./TestMasterSlaveLSS [OPTIONS] *\n");
74 printf("* OPTIONS: *\n");
75 printf("* -l : Can library [\"libcanfestival_can_virtual.so\"] *\n");
77 printf("* SlaveA: *\n");
78 printf("* -a : bus name [\"0\"] *\n");
79 printf("* -A : 1M,500K,250K,125K,100K,50K,20K,10K,none(disable) *\n");
81 printf("* SlaveB: *\n");
82 printf("* -b : bus name [\"1\"] *\n");
83 printf("* -B : 1M,500K,250K,125K,100K,50K,20K,10K,none(disable) *\n");
85 printf("* Master: *\n");
86 printf("* -m : bus name [\"2\"] *\n");
87 printf("* -M : 1M,500K,250K,125K,100K,50K,20K,10K,none(disable) *\n");
89 printf("**************************************************************\n");
92 /*************************** INIT *****************************************/
93 void InitNodes(CO_Data* d, UNS32 id)
95 /****************************** INITIALISATION SLAVE_A *******************************/
96 if(strcmp(SlaveBoardA.baudrate, "none")) {
97 /* Set an invalid nodeID */
98 setNodeId(&TestSlaveA_Data, 0xFF);
101 setState(&TestSlaveA_Data, Initialisation);
104 /****************************** INITIALISATION SLAVE_B *******************************/
105 if(strcmp(SlaveBoardB.baudrate, "none")) {
107 /* Set an invalid nodeID */
108 setNodeId(&TestSlaveB_Data, 0xFF);
111 setState(&TestSlaveB_Data, Initialisation);
114 /****************************** INITIALISATION MASTER *******************************/
115 if(strcmp(MasterBoard.baudrate, "none")){
117 /* Defining the node Id */
118 setNodeId(&TestMaster_Data, 0x01);
121 setState(&TestMaster_Data, Initialisation);
125 /*************************** EXIT *****************************************/
126 void Exit(CO_Data* d, UNS32 id)
128 if(strcmp(MasterBoard.baudrate, "none")){
129 eprintf("Finishing.\n");
130 masterSendNMTstateChange (&TestMaster_Data, 0x00, NMT_Stop_Node);
135 setState(&TestMaster_Data, Stopped);
139 /****************************************************************************/
140 /*************************** MAIN *****************************************/
141 /****************************************************************************/
142 int main(int argc,char **argv)
147 char* LibraryPath="../../drivers/can_virtual/libcanfestival_can_virtual.so";
149 while ((c = getopt(argc, argv, "-m:a:b:M:A:B:l:")) != EOF)
159 SlaveBoardA.busname = optarg;
167 SlaveBoardB.busname = optarg;
175 MasterBoard.busname = optarg;
183 SlaveBoardA.baudrate = optarg;
191 SlaveBoardB.baudrate = optarg;
199 MasterBoard.baudrate = optarg;
207 LibraryPath = optarg;
215 #if !defined(WIN32) || defined(__CYGWIN__)
216 /* install signal handler for manual break */
217 signal(SIGTERM, catch_signal);
218 signal(SIGINT, catch_signal);
222 #ifndef NOT_USE_DYNAMIC_LOADING
223 if (LoadCanDriver(LibraryPath) == NULL)
224 printf("Unable to load library: %s\n",LibraryPath);
228 if(strcmp(SlaveBoardA.baudrate, "none")){
230 TestSlaveA_Data.heartbeatError = TestSlaveA_heartbeatError;
231 TestSlaveA_Data.initialisation = TestSlaveA_initialisation;
232 TestSlaveA_Data.preOperational = TestSlaveA_preOperational;
233 TestSlaveA_Data.operational = TestSlaveA_operational;
234 TestSlaveA_Data.stopped = TestSlaveA_stopped;
235 TestSlaveA_Data.post_sync = TestSlaveA_post_sync;
236 TestSlaveA_Data.post_TPDO = TestSlaveA_post_TPDO;
237 TestSlaveA_Data.storeODSubIndex = TestSlaveA_storeODSubIndex;
238 TestSlaveA_Data.post_emcy = TestSlaveA_post_emcy;
239 /* in this example the slave doesn't implement NMT_Slave_Communications_Reset_Callback */
240 //TestSlaveA_Data.NMT_Slave_Communications_Reset_Callback = TestSlaveA_NMT_Slave_Communications_Reset_Callback;
241 TestSlaveA_Data.lss_StoreConfiguration = TestSlaveA_StoreConfiguration;
243 if(!canOpen(&SlaveBoardA,&TestSlaveA_Data)){
244 eprintf("Cannot open SlaveA Board (%s,%s)\n",SlaveBoardA.busname, SlaveBoardA.baudrate);
249 if(strcmp(SlaveBoardB.baudrate, "none")){
251 TestSlaveB_Data.heartbeatError = TestSlaveB_heartbeatError;
252 TestSlaveB_Data.initialisation = TestSlaveB_initialisation;
253 TestSlaveB_Data.preOperational = TestSlaveB_preOperational;
254 TestSlaveB_Data.operational = TestSlaveB_operational;
255 TestSlaveB_Data.stopped = TestSlaveB_stopped;
256 TestSlaveB_Data.post_sync = TestSlaveB_post_sync;
257 TestSlaveB_Data.post_TPDO = TestSlaveB_post_TPDO;
258 TestSlaveB_Data.storeODSubIndex = TestSlaveB_storeODSubIndex;
259 TestSlaveB_Data.post_emcy = TestSlaveB_post_emcy;
260 TestSlaveB_Data.NMT_Slave_Communications_Reset_Callback = TestSlaveB_NMT_Slave_Communications_Reset_Callback;
261 TestSlaveB_Data.lss_StoreConfiguration = TestSlaveB_StoreConfiguration;
263 if(!canOpen(&SlaveBoardB,&TestSlaveB_Data)){
264 eprintf("Cannot open SlaveB Board (%s,%s)\n",SlaveBoardB.busname, SlaveBoardB.baudrate);
269 if(strcmp(MasterBoard.baudrate, "none")){
271 TestMaster_Data.heartbeatError = TestMaster_heartbeatError;
272 TestMaster_Data.initialisation = TestMaster_initialisation;
273 TestMaster_Data.preOperational = TestMaster_preOperational;
274 TestMaster_Data.operational = TestMaster_operational;
275 TestMaster_Data.stopped = TestMaster_stopped;
276 TestMaster_Data.post_sync = TestMaster_post_sync;
277 TestMaster_Data.post_TPDO = TestMaster_post_TPDO;
278 TestMaster_Data.post_emcy = TestMaster_post_emcy;
279 TestMaster_Data.post_SlaveBootup=TestMaster_post_SlaveBootup;
281 if(!canOpen(&MasterBoard,&TestMaster_Data)){
282 eprintf("Cannot open Master Board (%s,%s)\n",MasterBoard.busname, MasterBoard.baudrate);
287 // Start timer thread
288 StartTimerLoop(&InitNodes);
295 StopTimerLoop(&Exit);
297 // Close CAN devices (and can threads)
298 if(strcmp(MasterBoard.baudrate, "none")) canClose(&TestMaster_Data);
300 if(strcmp(SlaveBoardB.baudrate, "none")) canClose(&TestSlaveB_Data);
302 if(strcmp(SlaveBoardA.baudrate, "none")) canClose(&TestSlaveA_Data);