examples/TestMasterSlave/TestMasterSlave.c
changeset 179 763b6d32c1a6
parent 172 0248c2b148bb
child 191 1e6e3d261b8f
equal deleted inserted replaced
178:01d81faa3350 179:763b6d32c1a6
    47 {
    47 {
    48 	eprintf("OnSlaveMap1Update:%d\n", SlaveMap1);
    48 	eprintf("OnSlaveMap1Update:%d\n", SlaveMap1);
    49 	return 0;
    49 	return 0;
    50 }
    50 }
    51 
    51 
    52 s_BOARD SlaveBoard = {"0", "500K"};
    52 s_BOARD SlaveBoard = {"0", "125K"};
    53 s_BOARD MasterBoard = {"1", "500K"};
    53 s_BOARD MasterBoard = {"1", "125K"};
    54 
    54 
    55 #if !defined(WIN32) || defined(__CYGWIN__)
    55 #if !defined(WIN32) || defined(__CYGWIN__)
    56 void catch_signal(int sig)
    56 void catch_signal(int sig)
    57 {
    57 {
    58   signal(SIGTERM, catch_signal);
    58   signal(SIGTERM, catch_signal);
    67   printf("*  TestMasterSlave                                           *\n");
    67   printf("*  TestMasterSlave                                           *\n");
    68   printf("*                                                            *\n");
    68   printf("*                                                            *\n");
    69   printf("*  A simple example for PC. It does implement 2 CanOpen      *\n");
    69   printf("*  A simple example for PC. It does implement 2 CanOpen      *\n");
    70   printf("*  nodes in the same process. A master and a slave. Both     *\n");
    70   printf("*  nodes in the same process. A master and a slave. Both     *\n");
    71   printf("*  communicate together, exchanging periodically NMT, SYNC,  *\n");
    71   printf("*  communicate together, exchanging periodically NMT, SYNC,  *\n");
    72   printf("*  SDO and PDO.                                              *\n");
    72   printf("*  SDO and PDO. Master configure heartbeat producer time     *\n");
       
    73   printf("*  at 1000 ms for slave node-id 0x02 by concise DCF.         *\n");                                  
    73   printf("*                                                            *\n");
    74   printf("*                                                            *\n");
    74   printf("*   Usage:                                                   *\n");
    75   printf("*   Usage:                                                   *\n");
    75   printf("*   ./TestMasterSlave  [OPTIONS]                             *\n");
    76   printf("*   ./TestMasterSlave  [OPTIONS]                             *\n");
    76   printf("*                                                            *\n");
    77   printf("*                                                            *\n");
    77   printf("*   OPTIONS:                                                 *\n");
    78   printf("*   OPTIONS:                                                 *\n");
    90 
    91 
    91 /***************************  INIT  *****************************************/
    92 /***************************  INIT  *****************************************/
    92 void InitNodes(CO_Data* d, UNS32 id)
    93 void InitNodes(CO_Data* d, UNS32 id)
    93 {
    94 {
    94 	/****************************** INITIALISATION SLAVE *******************************/
    95 	/****************************** INITIALISATION SLAVE *******************************/
    95 	if(SlaveBoard.baudrate) {
    96 	if(strcmp(SlaveBoard.baudrate, "none")) {
    96 		/* Defining the node Id */
    97 		/* Defining the node Id */
    97 		setNodeId(&TestSlave_Data, 0x02);
    98 		setNodeId(&TestSlave_Data, 0x02);
    98 		/* init */
    99 		/* init */
    99 		setState(&TestSlave_Data, Initialisation);
   100 		setState(&TestSlave_Data, Initialisation);
   100 	}
   101 	}
   101 
   102 
   102 	/****************************** INITIALISATION MASTER *******************************/
   103 	/****************************** INITIALISATION MASTER *******************************/
   103 	if(MasterBoard.baudrate){
   104 	if(strcmp(MasterBoard.baudrate, "none")){
   104  		RegisterSetODentryCallBack(&TestMaster_Data, 0x2000, 0, &OnMasterMap1Update);
   105  		RegisterSetODentryCallBack(&TestMaster_Data, 0x2000, 0, &OnMasterMap1Update);
   105 
   106 		
   106 		/* Defining the node Id */
   107 		/* Defining the node Id */
   107 		setNodeId(&TestMaster_Data, 0x01);
   108 		setNodeId(&TestMaster_Data, 0x01);
   108 
   109 
   109 		/* init */
   110 		/* init */
   110 		setState(&TestMaster_Data, Initialisation);
   111 		setState(&TestMaster_Data, Initialisation);
   111 
   112 			
   112 		/****************************** START *******************************/
       
   113 		/* Put the master in operational mode */
       
   114 		setState(&TestMaster_Data, Operational);
       
   115   
       
   116 		/* Ask slave node to go in operational mode */
       
   117 		masterSendNMTstateChange (&TestMaster_Data, 0x02, NMT_Start_Node);
       
   118 	}
   113 	}
   119 }
   114 }
   120 
   115 
   121 /****************************************************************************/
   116 /****************************************************************************/
   122 /***************************  MAIN  *****************************************/
   117 /***************************  MAIN  *****************************************/
   186 
   181 
   187 #ifndef NOT_USE_DYNAMIC_LOADING
   182 #ifndef NOT_USE_DYNAMIC_LOADING
   188 	LoadCanDriver(LibraryPath);
   183 	LoadCanDriver(LibraryPath);
   189 #endif		
   184 #endif		
   190 	// Open CAN devices
   185 	// Open CAN devices
   191 	if(strcmp( SlaveBoard.baudrate, "none")){
   186 
       
   187 	if(strcmp(SlaveBoard.baudrate, "none")){
   192 		
   188 		
   193 		TestSlave_Data.heartbeatError = TestSlave_heartbeatError;
   189 		TestSlave_Data.heartbeatError = TestSlave_heartbeatError;
   194 		TestSlave_Data.initialisation = TestSlave_initialisation;
   190 		TestSlave_Data.initialisation = TestSlave_initialisation;
   195 		TestSlave_Data.preOperational = TestSlave_preOperational;
   191 		TestSlave_Data.preOperational = TestSlave_preOperational;
   196 		TestSlave_Data.operational = TestSlave_operational;
   192 		TestSlave_Data.operational = TestSlave_operational;
   202 		if(!canOpen(&SlaveBoard,&TestSlave_Data)){
   198 		if(!canOpen(&SlaveBoard,&TestSlave_Data)){
   203 			eprintf("Cannot open Slave Board (%s,%s)\n",SlaveBoard.busname, SlaveBoard.baudrate);
   199 			eprintf("Cannot open Slave Board (%s,%s)\n",SlaveBoard.busname, SlaveBoard.baudrate);
   204 			goto fail_slave;
   200 			goto fail_slave;
   205 		}
   201 		}
   206 	}
   202 	}
   207 
   203 	if(strcmp(MasterBoard.baudrate, "none")){
   208 	if(strcmp( MasterBoard.baudrate, "none")){
       
   209 		
   204 		
   210 		TestMaster_Data.heartbeatError = TestMaster_heartbeatError;
   205 		TestMaster_Data.heartbeatError = TestMaster_heartbeatError;
   211 		TestMaster_Data.initialisation = TestMaster_initialisation;
   206 		TestMaster_Data.initialisation = TestMaster_initialisation;
   212 		TestMaster_Data.preOperational = TestMaster_preOperational;
   207 		TestMaster_Data.preOperational = TestMaster_preOperational;
   213 		TestMaster_Data.operational = TestMaster_operational;
   208 		TestMaster_Data.operational = TestMaster_operational;
   214 		TestMaster_Data.stopped = TestMaster_stopped;
   209 		TestMaster_Data.stopped = TestMaster_stopped;
   215 		TestMaster_Data.post_sync = TestMaster_post_sync;
   210 		TestMaster_Data.post_sync = TestMaster_post_sync;
   216 		TestMaster_Data.post_TPDO = TestMaster_post_TPDO;
   211 		TestMaster_Data.post_TPDO = TestMaster_post_TPDO;
   217 		
   212 		
   218 		if(!canOpen(&MasterBoard,&TestMaster_Data)){
   213 		if(!canOpen(&MasterBoard,&TestMaster_Data)){
   219 			eprintf("Cannot open Master Board (%s,%s)\n",SlaveBoard.busname, SlaveBoard.baudrate);
   214 			eprintf("Cannot open Master Board (%s,%s)\n",MasterBoard.busname, MasterBoard.baudrate);
   220 			goto fail_master;
   215 			goto fail_master;
   221 		}
   216 		}
   222 	}
   217 	}
   223 	
   218 	sleep(2);
   224 	// Start timer thread
   219 	// Start timer thread
   225 	StartTimerLoop(&InitNodes);
   220 	StartTimerLoop(&InitNodes);
   226 
   221 
   227 	// wait Ctrl-C
   222 	// wait Ctrl-C
       
   223 	
   228 	pause();
   224 	pause();
   229 	eprintf("Finishing.\n");
   225 	eprintf("Finishing.\n");
   230 	
   226 	
       
   227 	masterSendNMTstateChange (&TestMaster_Data, 0x02, NMT_Reset_Node);
       
   228 	eprintf("reset\n");
       
   229 	// Stop master
       
   230 	setState(&TestMaster_Data, Stopped);
       
   231 	
   231 	// Stop timer thread
   232 	// Stop timer thread
   232 	StopTimerLoop();
   233 	StopTimerLoop();
   233 	
   234 	
   234 	// Close CAN devices (and can threads)
   235 	// Close CAN devices (and can threads)
   235 	if(strcmp( SlaveBoard.baudrate, "none")) canClose(&TestSlave_Data);
   236 	if(strcmp(SlaveBoard.baudrate, "none")) canClose(&TestSlave_Data);
   236 fail_master:
   237 fail_master:
   237 	if(strcmp( MasterBoard.baudrate, "none")) canClose(&TestMaster_Data);	
   238 	if(strcmp(MasterBoard.baudrate, "none")) canClose(&TestMaster_Data);	
   238 fail_slave:
   239 fail_slave:
   239 	
   240 	return 0;
   240 
   241 }
   241   return 0;
       
   242 }