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. Both *\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 slave node-id 0x02 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("* -s : bus name [\"0\"] *\n");
79 printf("* -S : 1M,500K,250K,125K,100K,50K,20K,10K,none(disable) *\n");
81 printf("* Master: *\n");
82 printf("* -m : bus name [\"1\"] *\n");
83 printf("* -M : 1M,500K,250K,125K,100K,50K,20K,10K,none(disable) *\n");
85 printf("**************************************************************\n");
88 /*************************** INIT *****************************************/
89 void InitNodes(CO_Data* d, UNS32 id)
91 /****************************** INITIALISATION SLAVE_A *******************************/
92 if(strcmp(SlaveBoardA.baudrate, "none")) {
93 /* Set an invalid nodeID */
94 setNodeId(&TestSlaveA_Data, 0xFF);
97 setState(&TestSlaveA_Data, Initialisation);
100 /****************************** INITIALISATION SLAVE_B *******************************/
101 if(strcmp(SlaveBoardB.baudrate, "none")) {
103 /* Set an invalid nodeID */
104 setNodeId(&TestSlaveB_Data, 0xFF);
107 setState(&TestSlaveB_Data, Initialisation);
110 /****************************** INITIALISATION MASTER *******************************/
111 if(strcmp(MasterBoard.baudrate, "none")){
113 /* Defining the node Id */
114 setNodeId(&TestMaster_Data, 0x01);
117 setState(&TestMaster_Data, Initialisation);
121 /****************************************************************************/
122 /*************************** MAIN *****************************************/
123 /****************************************************************************/
124 int main(int argc,char **argv)
129 char* LibraryPath="../../drivers/can_virtual/libcanfestival_can_virtual.so";
131 while ((c = getopt(argc, argv, "-m:s:M:S:l:")) != EOF)
141 SlaveBoardA.busname = optarg;
149 MasterBoard.busname = optarg;
157 SlaveBoardA.baudrate = optarg;
165 MasterBoard.baudrate = optarg;
173 LibraryPath = optarg;
181 #if !defined(WIN32) || defined(__CYGWIN__)
182 /* install signal handler for manual break */
183 signal(SIGTERM, catch_signal);
184 signal(SIGINT, catch_signal);
187 #ifndef NOT_USE_DYNAMIC_LOADING
188 if (LoadCanDriver(LibraryPath) == NULL)
189 printf("Unable to load library: %s\n",LibraryPath);
193 if(strcmp(SlaveBoardA.baudrate, "none")){
195 TestSlaveA_Data.heartbeatError = TestSlaveA_heartbeatError;
196 TestSlaveA_Data.initialisation = TestSlaveA_initialisation;
197 TestSlaveA_Data.preOperational = TestSlaveA_preOperational;
198 TestSlaveA_Data.operational = TestSlaveA_operational;
199 TestSlaveA_Data.stopped = TestSlaveA_stopped;
200 TestSlaveA_Data.post_sync = TestSlaveA_post_sync;
201 TestSlaveA_Data.post_TPDO = TestSlaveA_post_TPDO;
202 TestSlaveA_Data.storeODSubIndex = TestSlaveA_storeODSubIndex;
203 TestSlaveA_Data.post_emcy = TestSlaveA_post_emcy;
204 /* in this example the slave doesn't support Store configuration*/
205 TestSlaveA_Data.lss_StoreConfiguration = TestSlaveA_StoreConfiguration;
206 TestSlaveA_Data.lss_ChangeBaudRate=TestSlaveA_ChangeBaudRate;
208 if(!canOpen(&SlaveBoardA,&TestSlaveA_Data)){
209 eprintf("Cannot open SlaveA Board (%s,%s)\n",SlaveBoardA.busname, SlaveBoardA.baudrate);
214 if(strcmp(SlaveBoardB.baudrate, "none")){
216 TestSlaveB_Data.heartbeatError = TestSlaveB_heartbeatError;
217 TestSlaveB_Data.initialisation = TestSlaveB_initialisation;
218 TestSlaveB_Data.preOperational = TestSlaveB_preOperational;
219 TestSlaveB_Data.operational = TestSlaveB_operational;
220 TestSlaveB_Data.stopped = TestSlaveB_stopped;
221 TestSlaveB_Data.post_sync = TestSlaveB_post_sync;
222 TestSlaveB_Data.post_TPDO = TestSlaveB_post_TPDO;
223 TestSlaveB_Data.storeODSubIndex = TestSlaveB_storeODSubIndex;
224 TestSlaveB_Data.post_emcy = TestSlaveB_post_emcy;
225 TestSlaveB_Data.lss_StoreConfiguration = TestSlaveB_StoreConfiguration;
226 TestSlaveB_Data.lss_ChangeBaudRate=TestSlaveB_ChangeBaudRate;
229 if(!canOpen(&SlaveBoardB,&TestSlaveB_Data)){
230 eprintf("Cannot open SlaveB Board (%s,%s)\n",SlaveBoardB.busname, SlaveBoardB.baudrate);
235 if(strcmp(MasterBoard.baudrate, "none")){
237 TestMaster_Data.heartbeatError = TestMaster_heartbeatError;
238 TestMaster_Data.initialisation = TestMaster_initialisation;
239 TestMaster_Data.preOperational = TestMaster_preOperational;
240 TestMaster_Data.operational = TestMaster_operational;
241 TestMaster_Data.stopped = TestMaster_stopped;
242 TestMaster_Data.post_sync = TestMaster_post_sync;
243 TestMaster_Data.post_TPDO = TestMaster_post_TPDO;
244 TestMaster_Data.post_emcy = TestMaster_post_emcy;
245 TestMaster_Data.post_SlaveBootup=TestMaster_post_SlaveBootup;
246 TestMaster_Data.lss_ChangeBaudRate=TestMaster_ChangeBaudRate;
248 if(!canOpen(&MasterBoard,&TestMaster_Data)){
249 eprintf("Cannot open Master Board (%s,%s)\n",MasterBoard.busname, MasterBoard.baudrate);
254 // Start timer thread
255 StartTimerLoop(&InitNodes);
261 eprintf("Finishing.\n");
263 masterSendNMTstateChange (&TestMaster_Data, 0x00, NMT_Stop_Node);
269 setState(&TestMaster_Data, Stopped);
275 // Close CAN devices (and can threads)
276 if(strcmp(MasterBoard.baudrate, "none")) canClose(&TestMaster_Data);
278 if(strcmp(SlaveBoardB.baudrate, "none")) canClose(&TestSlaveB_Data);
280 if(strcmp(SlaveBoardA.baudrate, "none")) canClose(&TestSlaveA_Data);