examples/kerneltest/TestMasterSlave.c
changeset 391 7802a7d5584f
child 467 40efa79d27dd
equal deleted inserted replaced
390:31dc4ec8710c 391:7802a7d5584f
       
     1 #include "canfestival.h"
       
     2 
       
     3 #include "Master.h"
       
     4 #include "Slave.h"
       
     5 #include "TestMasterSlave.h"
       
     6 
       
     7 
       
     8 static UNS32 OnMasterMap1Update(CO_Data* d, const indextable * unsused_indextable, UNS8 unsused_bSubindex)
       
     9 {
       
    10 	eprintf("OnSlaveMap1Update:%d\n", SlaveMap1);
       
    11 	return 0;
       
    12 }
       
    13 
       
    14 s_BOARD SlaveBoard = {"0", "125K"};
       
    15 s_BOARD MasterBoard = {"1", "125K"};
       
    16 
       
    17 /***************************  INIT  *****************************************/
       
    18 static void InitNodes(CO_Data* d, UNS32 id)
       
    19 {
       
    20 	/****************************** INITIALISATION SLAVE *******************************/
       
    21 	if(strcmp(SlaveBoard.baudrate, "none")) {
       
    22 		setNodeId(&TestSlave_Data, 0x02);
       
    23 
       
    24 		setState(&TestSlave_Data, Initialisation);
       
    25 	}
       
    26 
       
    27 	/****************************** INITIALISATION MASTER *******************************/
       
    28 	if(strcmp(MasterBoard.baudrate, "none")){
       
    29  		RegisterSetODentryCallBack(&TestMaster_Data, 0x2000, 0, &OnMasterMap1Update);
       
    30 		
       
    31 		// Defining the node Id
       
    32 		setNodeId(&TestMaster_Data, 0x01);
       
    33 
       
    34 		setState(&TestMaster_Data, Initialisation);
       
    35 	}
       
    36 }
       
    37 
       
    38 int TestMasterSlave_start (void)
       
    39 {
       
    40 	if(strcmp(SlaveBoard.baudrate, "none")){
       
    41 		
       
    42 		TestSlave_Data.heartbeatError = TestSlave_heartbeatError;
       
    43 		TestSlave_Data.initialisation = TestSlave_initialisation;
       
    44 		TestSlave_Data.preOperational = TestSlave_preOperational;
       
    45 		TestSlave_Data.operational = TestSlave_operational;
       
    46 		TestSlave_Data.stopped = TestSlave_stopped;
       
    47 		TestSlave_Data.post_sync = TestSlave_post_sync;
       
    48 		TestSlave_Data.post_TPDO = TestSlave_post_TPDO;
       
    49 		TestSlave_Data.storeODSubIndex = TestSlave_storeODSubIndex;
       
    50 		TestSlave_Data.post_emcy = TestSlave_post_emcy;
       
    51 
       
    52 		if(!canOpen(&SlaveBoard,&TestSlave_Data)){
       
    53 			eprintf("Cannot open Slave Board (%s,%s)\n",SlaveBoard.busname, SlaveBoard.baudrate);
       
    54 			return 1;
       
    55 		}
       
    56 	}
       
    57 	if(strcmp(MasterBoard.baudrate, "none")){
       
    58 		
       
    59 		TestMaster_Data.heartbeatError = TestMaster_heartbeatError;
       
    60 		TestMaster_Data.initialisation = TestMaster_initialisation;
       
    61 		TestMaster_Data.preOperational = TestMaster_preOperational;
       
    62 		TestMaster_Data.operational = TestMaster_operational;
       
    63 		TestMaster_Data.stopped = TestMaster_stopped;
       
    64 		TestMaster_Data.post_sync = TestMaster_post_sync;
       
    65 		TestMaster_Data.post_TPDO = TestMaster_post_TPDO;
       
    66 		TestMaster_Data.post_emcy = TestMaster_post_emcy;
       
    67 		TestMaster_Data.post_SlaveBootup=TestMaster_post_SlaveBootup;
       
    68 		
       
    69 		if(!canOpen(&MasterBoard,&TestMaster_Data)){
       
    70 			eprintf("Cannot open Master Board (%s,%s)\n",MasterBoard.busname, MasterBoard.baudrate);
       
    71 			if(strcmp(MasterBoard.baudrate, "none")) canClose(&TestMaster_Data);
       
    72 			return 1;
       
    73 		}
       
    74 	}
       
    75 
       
    76 	// Start timer thread
       
    77 	StartTimerLoop(&InitNodes);
       
    78 
       
    79 	return 0;
       
    80 }
       
    81 
       
    82 void TestMasterSlave_stop (void)
       
    83 {
       
    84 	eprintf("Finishing.\n");
       
    85 	
       
    86 	EnterMutex();
       
    87 	masterSendNMTstateChange (&TestMaster_Data, 0x02, NMT_Reset_Node);
       
    88 	LeaveMutex();
       
    89 	
       
    90 	// Stop master
       
    91 	EnterMutex();
       
    92 	setState(&TestMaster_Data, Stopped);
       
    93 	LeaveMutex();
       
    94 	
       
    95 	// Stop timer thread
       
    96 	StopTimerLoop();
       
    97 	
       
    98 	// Close CAN devices (and can threads)
       
    99 	if(strcmp(SlaveBoard.baudrate, "none")) canClose(&TestSlave_Data);
       
   100 	if(strcmp(MasterBoard.baudrate, "none")) canClose(&TestMaster_Data);
       
   101 
       
   102 	eprintf("End.\n");
       
   103 }