examples/TestMasterSlave/TestMasterSlave.c
changeset 0 4472ee7c6c3e
child 31 a82b70738e5c
equal deleted inserted replaced
-1:000000000000 0:4472ee7c6c3e
       
     1 /*
       
     2 This file is part of CanFestival, a library implementing CanOpen Stack. 
       
     3 
       
     4 Copyright (C): Edouard TISSERANT and Francis DUPIN
       
     5 
       
     6 See COPYING file for copyrights details.
       
     7 
       
     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.
       
    12 
       
    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.
       
    17 
       
    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
       
    21 */
       
    22 
       
    23 #include <stdio.h>
       
    24 #include <string.h>
       
    25 #include <sys/time.h>
       
    26 #include <unistd.h>
       
    27 #include <stdlib.h>
       
    28 #include <signal.h>
       
    29 
       
    30 #include <applicfg.h>
       
    31 #include <can_driver.h>
       
    32 #include <timers_driver.h>
       
    33 
       
    34 #include "Master.h"
       
    35 #include "Slave.h"
       
    36 #include "TestMasterSlave.h"
       
    37 
       
    38 #define MyCase(fc) case fc: eprintf(#fc);break;
       
    39 void print_message(Message *m)
       
    40 {
       
    41 	int i;
       
    42 	switch(m->cob_id.w >> 7)
       
    43 	{
       
    44 		MyCase(SYNC)
       
    45 		MyCase(TIME_STAMP)
       
    46 		MyCase(PDO1tx)
       
    47 		MyCase(PDO1rx)
       
    48 		MyCase(PDO2tx)
       
    49 		MyCase(PDO2rx)
       
    50 		MyCase(PDO3tx)
       
    51 		MyCase(PDO3rx)
       
    52 		MyCase(PDO4tx)
       
    53 		MyCase(PDO4rx)
       
    54 		MyCase(SDOtx)
       
    55 		MyCase(SDOrx)
       
    56 		MyCase(NODE_GUARD)
       
    57 		MyCase(NMT)
       
    58 	}
       
    59 	eprintf(" rtr:%d", m->rtr);
       
    60 	eprintf(" len:%d", m->len);
       
    61 	for (i = 0 ; i < m->len ; i++)
       
    62 		eprintf(" %02x", m->data[i]);
       
    63 	eprintf("\n");
       
    64 }
       
    65 
       
    66 UNS32 OnMasterMap1Update(CO_Data* d, const indextable * unsused_indextable, UNS8 unsused_bSubindex)
       
    67 {
       
    68 	eprintf("OnSlaveMap1Update:%d\n", SlaveMap1);
       
    69 }
       
    70 
       
    71 
       
    72 
       
    73 void InitNodes(CO_Data* d, UNS32 id)
       
    74 {
       
    75 //  TestSlave_Index2000_callbacks[0] = &OnMasterMap1Update;
       
    76   RegisterSetODentryCallBack(&TestMaster_Data, 0x2000, 0, &OnMasterMap1Update);
       
    77 
       
    78   /****************************** INITIALISATION MASTER *******************************/
       
    79   /* Defining the node Id */
       
    80   setNodeId(&TestMaster_Data, 0x01);
       
    81 
       
    82   /* init */
       
    83   setState(&TestMaster_Data, Initialisation);
       
    84 
       
    85   /****************************** INITIALISATION SLAVE *******************************/
       
    86   /* Defining the node Id */
       
    87   setNodeId(&TestSlave_Data, 0x02);
       
    88 
       
    89   /* init */
       
    90   setState(&TestSlave_Data, Initialisation);
       
    91 
       
    92   /****************************** START *******************************/
       
    93   /* Put the master in operational mode */
       
    94   setState(&TestMaster_Data, Operational);
       
    95   
       
    96   masterSendNMTstateChange (&TestMaster_Data, 0x02, NMT_Start_Node);
       
    97 	
       
    98 }
       
    99 
       
   100 CAN_HANDLE SlaveCanHandle;
       
   101 CAN_HANDLE MasterCanHandle;
       
   102 
       
   103 // Baudrate values for Peak board :
       
   104 // CAN_BAUD_1M CAN_BAUD_500K CAN_BAUD_250K CAN_BAUD_125K CAN_BAUD_100K CAN_BAUD_50K
       
   105 // CAN_BAUD_20K CAN_BAUD_10K CAN_BAUD_5K
       
   106 
       
   107 #ifdef CAN_BAUD_500K
       
   108 // Appli have been compiled for Peak. Baudrate is defined
       
   109 # define BAUDRATE CAN_BAUD_500K
       
   110 #else
       
   111 // Appli have been compiled for Generic. Baudrate not used
       
   112 # define BAUDRATE 0
       
   113 #endif
       
   114 
       
   115 void catch_signal(int sig)
       
   116 {
       
   117   signal(SIGTERM, catch_signal);
       
   118   signal(SIGINT, catch_signal);
       
   119   stopTimer();
       
   120   eprintf("Got Sigterm - Finishing.\n");
       
   121 }
       
   122 
       
   123 
       
   124 void help()
       
   125 {
       
   126   printf("**************************************************************\n");
       
   127   printf("*  TestMasterSlave                                           *\n");
       
   128   printf("*                                                            *\n");
       
   129   printf("*  A simple example for PC. It does implement 2 CanOpen      *\n");
       
   130   printf("*  nodes in the same process. A master and a slave. Both     *\n");
       
   131   printf("*   communicate together, exchanging periodically NMT, SYNC, *\n");
       
   132   printf("*   SDO and PDO.                                             *\n");
       
   133   printf("*                                                            *\n");
       
   134   printf("*   If you have chosen virtual CAN driver, just type         *\n");
       
   135   printf("*   ./TestMasterSlave                                        *\n");
       
   136   printf("*                                                            *\n");
       
   137   printf("*   Else you need to sepcify bus:                            *\n");
       
   138   printf("*                                                            *\n");
       
   139   printf("*     -s : slave CAN bus [default 0, peak first PCI]         *\n");
       
   140   printf("*     -m : master CAN bus [default 1, peak second PCI]       *\n");
       
   141   printf("*                                                            *\n");
       
   142   printf("**************************************************************\n");
       
   143 }
       
   144 
       
   145 /****************************************************************************/
       
   146 /***************************  MAIN  *****************************************/
       
   147 /****************************************************************************/
       
   148 int main(int argc,char **argv)
       
   149 {
       
   150 	s_BOARD SlaveBoard = {"0", BAUDRATE, &TestSlave_Data};
       
   151 	s_BOARD MasterBoard = {"1", BAUDRATE, &TestMaster_Data};
       
   152 
       
   153 
       
   154   char c;
       
   155   extern char *optarg;
       
   156 
       
   157   while ((c = getopt(argc, argv, "-m:s:")) != EOF)
       
   158   {
       
   159     switch(c)
       
   160     {
       
   161       case 's' :
       
   162         if (optarg[0] == 0)
       
   163         {
       
   164           help();
       
   165           exit(1);
       
   166         }
       
   167         SlaveBoard.busname = optarg;
       
   168         break;
       
   169       case 'm' :
       
   170         if (optarg[0] == 0)
       
   171         {
       
   172           help();
       
   173           exit(1);
       
   174         }
       
   175         MasterBoard.busname = optarg;
       
   176         break;
       
   177       default:
       
   178         help();
       
   179         exit(1);
       
   180     }
       
   181   }
       
   182 
       
   183 	/* install signal handler for manual break */
       
   184 	signal(SIGTERM, catch_signal);
       
   185 	signal(SIGINT, catch_signal);
       
   186 	
       
   187 	// Open CAN devices
       
   188 	SlaveCanHandle = canOpen(&SlaveBoard);
       
   189 	MasterCanHandle = canOpen(&MasterBoard);	
       
   190 	
       
   191 	// Will call InitNodes, and wait and handle next timer events.
       
   192 	TimerLoop(&InitNodes);
       
   193 	
       
   194 	// Close CAN devices
       
   195 	canClose(SlaveCanHandle);
       
   196 	canClose(MasterCanHandle);	
       
   197 	
       
   198 
       
   199   return 0;
       
   200 }