examples/TestMasterSlave/TestMasterSlave.c
changeset 36 ff3ba4a90548
parent 32 8afa33692372
child 40 ddeeb217ed71
equal deleted inserted replaced
35:88812de1d7cc 36:ff3ba4a90548
    67 {
    67 {
    68 	eprintf("OnSlaveMap1Update:%d\n", SlaveMap1);
    68 	eprintf("OnSlaveMap1Update:%d\n", SlaveMap1);
    69 	return 0;
    69 	return 0;
    70 }
    70 }
    71 
    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;
    72 CAN_HANDLE SlaveCanHandle;
   102 CAN_HANDLE MasterCanHandle;
    73 CAN_HANDLE MasterCanHandle;
   103 
    74 
   104 // Baudrate values for Peak board :
    75 // Baudrate values for Peak board :
   105 // CAN_BAUD_1M CAN_BAUD_500K CAN_BAUD_250K CAN_BAUD_125K CAN_BAUD_100K CAN_BAUD_50K
    76 // 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
    77 // CAN_BAUD_20K CAN_BAUD_10K CAN_BAUD_5K
   107 
    78 
   108 #ifdef CAN_BAUD_500K
    79 #ifdef CAN_BAUD_500K
   109 // Appli have been compiled for Peak. Baudrate is defined
    80 int TranslateBaudeRate(char* optarg){
   110 # define BAUDRATE CAN_BAUD_500K
    81 	if(!strcmp( optarg, "1M")) return CAN_BAUD_1M;
       
    82 	if(!strcmp( optarg, "500K")) return CAN_BAUD_500K;
       
    83 	if(!strcmp( optarg, "250K")) return CAN_BAUD_250K;
       
    84 	if(!strcmp( optarg, "125K")) return CAN_BAUD_125K;
       
    85 	if(!strcmp( optarg, "100K")) return CAN_BAUD_100K;
       
    86 	if(!strcmp( optarg, "50K")) return CAN_BAUD_50K;
       
    87 	if(!strcmp( optarg, "20K")) return CAN_BAUD_20K;
       
    88 	if(!strcmp( optarg, "10K")) return CAN_BAUD_10K;
       
    89 	if(!strcmp( optarg, "5K")) return CAN_BAUD_5K;
       
    90 	if(!strcmp( optarg, "none")) return 0;
       
    91 	return 0x0000;
       
    92 }
       
    93 s_BOARD SlaveBoard = {"0", CAN_BAUD_500K, &TestSlave_Data};
       
    94 s_BOARD MasterBoard = {"1", CAN_BAUD_500K, &TestMaster_Data};
   111 #else
    95 #else
   112 // Appli have been compiled for Generic. Baudrate not used
    96 int TranslateBaudeRate(char* optarg){
   113 # define BAUDRATE 0
    97 	if(!strcmp( optarg, "1M")) return 1000;
       
    98 	if(!strcmp( optarg, "500K")) return 500;
       
    99 	if(!strcmp( optarg, "250K")) return 250;
       
   100 	if(!strcmp( optarg, "125K")) return 125;
       
   101 	if(!strcmp( optarg, "100K")) return 100;
       
   102 	if(!strcmp( optarg, "50K")) return 50;
       
   103 	if(!strcmp( optarg, "20K")) return 20;
       
   104 	if(!strcmp( optarg, "10K")) return 10;
       
   105 	if(!strcmp( optarg, "5K")) return 5;
       
   106 	if(!strcmp( optarg, "none")) return 0;
       
   107 	return 0;
       
   108 }
       
   109 s_BOARD SlaveBoard = {"0", 500, &TestSlave_Data};
       
   110 s_BOARD MasterBoard = {"1", 500, &TestMaster_Data};
   114 #endif
   111 #endif
       
   112 
   115 
   113 
   116 void catch_signal(int sig)
   114 void catch_signal(int sig)
   117 {
   115 {
   118   signal(SIGTERM, catch_signal);
   116   signal(SIGTERM, catch_signal);
   119   signal(SIGINT, catch_signal);
   117   signal(SIGINT, catch_signal);
   125   printf("**************************************************************\n");
   123   printf("**************************************************************\n");
   126   printf("*  TestMasterSlave                                           *\n");
   124   printf("*  TestMasterSlave                                           *\n");
   127   printf("*                                                            *\n");
   125   printf("*                                                            *\n");
   128   printf("*  A simple example for PC. It does implement 2 CanOpen      *\n");
   126   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");
   127   printf("*  nodes in the same process. A master and a slave. Both     *\n");
   130   printf("*   communicate together, exchanging periodically NMT, SYNC, *\n");
   128   printf("*  communicate together, exchanging periodically NMT, SYNC,  *\n");
   131   printf("*   SDO and PDO.                                             *\n");
   129   printf("*  SDO and PDO.                                              *\n");
   132   printf("*                                                            *\n");
   130   printf("*                                                            *\n");
   133   printf("*   If you have chosen virtual CAN driver, just type         *\n");
   131   printf("*   Usage:                                                   *\n");
   134   printf("*   ./TestMasterSlave                                        *\n");
   132   printf("*   ./TestMasterSlave  [OPTIONS]                             *\n");
   135   printf("*                                                            *\n");
   133   printf("*                                                            *\n");
   136   printf("*   Else you need to specify bus:                            *\n");
   134   printf("*   OPTIONS:                                                 *\n");
   137   printf("*                                                            *\n");
   135   printf("*    Slave:                                                  *\n");
   138   printf("*     -s : slave CAN bus [default 0, peak first PCI]         *\n");
   136   printf("*     -s : bus name [\"0\"]                                    *\n");
   139   printf("*     -m : master CAN bus [default 1, peak second PCI]       *\n");
   137   printf("*     -S : 1M,500K,250K,125K,100K,50K,20K,10K,none(disable)  *\n");
       
   138   printf("*                                                            *\n");
       
   139   printf("*    Master:                                                 *\n");
       
   140   printf("*     -m : bus name [\"1\"]                                    *\n");
       
   141   printf("*     -M : 1M,500K,250K,125K,100K,50K,20K,10K,none(disable)  *\n");
   140   printf("*                                                            *\n");
   142   printf("*                                                            *\n");
   141   printf("**************************************************************\n");
   143   printf("**************************************************************\n");
       
   144 }
       
   145 
       
   146 /***************************  INIT  *****************************************/
       
   147 void InitNodes(CO_Data* d, UNS32 id)
       
   148 {
       
   149 	/****************************** INITIALISATION SLAVE *******************************/
       
   150 	if(SlaveBoard.baudrate) {
       
   151 		/* Defining the node Id */
       
   152 		setNodeId(&TestSlave_Data, 0x02);
       
   153 		/* init */
       
   154 		setState(&TestSlave_Data, Initialisation);
       
   155 	}
       
   156 
       
   157 	/****************************** INITIALISATION MASTER *******************************/
       
   158 	if(MasterBoard.baudrate){
       
   159  		RegisterSetODentryCallBack(&TestMaster_Data, 0x2000, 0, &OnMasterMap1Update);
       
   160 
       
   161 		/* Defining the node Id */
       
   162 		setNodeId(&TestMaster_Data, 0x01);
       
   163 
       
   164 		/* init */
       
   165 		setState(&TestMaster_Data, Initialisation);
       
   166 
       
   167 		/****************************** START *******************************/
       
   168 		/* Put the master in operational mode */
       
   169 		setState(&TestMaster_Data, Operational);
       
   170   
       
   171 		/* Ask slave node to go in operational mode */
       
   172 		masterSendNMTstateChange (&TestMaster_Data, 0x02, NMT_Start_Node);
       
   173 	}
   142 }
   174 }
   143 
   175 
   144 /****************************************************************************/
   176 /****************************************************************************/
   145 /***************************  MAIN  *****************************************/
   177 /***************************  MAIN  *****************************************/
   146 /****************************************************************************/
   178 /****************************************************************************/
   147 int main(int argc,char **argv)
   179 int main(int argc,char **argv)
   148 {
   180 {
   149 	s_BOARD SlaveBoard = {"0", BAUDRATE, &TestSlave_Data};
       
   150 	s_BOARD MasterBoard = {"1", BAUDRATE, &TestMaster_Data};
       
   151 
       
   152 
   181 
   153   char c;
   182   char c;
   154   extern char *optarg;
   183   extern char *optarg;
   155 
   184 
   156   while ((c = getopt(argc, argv, "-m:s:")) != EOF)
   185   while ((c = getopt(argc, argv, "-m:s:M:S:")) != EOF)
   157   {
   186   {
   158     switch(c)
   187     switch(c)
   159     {
   188     {
   160       case 's' :
   189       case 's' :
   161         if (optarg[0] == 0)
   190         if (optarg[0] == 0)
   170         {
   199         {
   171           help();
   200           help();
   172           exit(1);
   201           exit(1);
   173         }
   202         }
   174         MasterBoard.busname = optarg;
   203         MasterBoard.busname = optarg;
       
   204         break;
       
   205       case 'S' :
       
   206         if (optarg[0] == 0)
       
   207         {
       
   208           help();
       
   209           exit(1);
       
   210         }
       
   211         SlaveBoard.baudrate = TranslateBaudeRate(optarg);
       
   212         break;
       
   213       case 'M' :
       
   214         if (optarg[0] == 0)
       
   215         {
       
   216           help();
       
   217           exit(1);
       
   218         }
       
   219         MasterBoard.baudrate = TranslateBaudeRate(optarg);
   175         break;
   220         break;
   176       default:
   221       default:
   177         help();
   222         help();
   178         exit(1);
   223         exit(1);
   179     }
   224     }
   182 	/* install signal handler for manual break */
   227 	/* install signal handler for manual break */
   183 	signal(SIGTERM, catch_signal);
   228 	signal(SIGTERM, catch_signal);
   184 	signal(SIGINT, catch_signal);
   229 	signal(SIGINT, catch_signal);
   185 	
   230 	
   186 	// Open CAN devices
   231 	// Open CAN devices
   187 	SlaveCanHandle = canOpen(&SlaveBoard);
   232 	if(SlaveBoard.baudrate)
   188 	MasterCanHandle = canOpen(&MasterBoard);	
   233 		if((SlaveCanHandle = canOpen(&SlaveBoard))==NULL) goto fail_slave;
       
   234 		
       
   235 	if(MasterBoard.baudrate)
       
   236 		if((MasterCanHandle = canOpen(&MasterBoard))==NULL) goto fail_master;
   189 	
   237 	
   190 	// Start timer thread
   238 	// Start timer thread
   191 	StartTimerLoop(&InitNodes);
   239 	StartTimerLoop(&InitNodes);
   192 
   240 
   193 	// wait Ctrl-C
   241 	// wait Ctrl-C
   196 	
   244 	
   197 	// Stop timer thread
   245 	// Stop timer thread
   198 	StopTimerLoop();
   246 	StopTimerLoop();
   199 	
   247 	
   200 	// Close CAN devices (and can threads)
   248 	// Close CAN devices (and can threads)
   201 	canClose(SlaveCanHandle);
   249 	if(SlaveBoard.baudrate) canClose(SlaveCanHandle);
   202 	canClose(MasterCanHandle);	
   250 fail_master:
       
   251 	if(MasterBoard.baudrate) canClose(MasterCanHandle);	
       
   252 fail_slave:
   203 	
   253 	
   204 
   254 
   205   return 0;
   255   return 0;
   206 }
   256 }