]> rtime.felk.cvut.cz Git - CanFestival-3.git/blob - examples/TestMasterSlave/TestMasterSlave.c
Updated configure and fixes in Makefiles.in for cygwin compiling.
[CanFestival-3.git] / examples / TestMasterSlave / TestMasterSlave.c
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         return 0;
70 }
71
72 CAN_HANDLE SlaveCanHandle;
73 CAN_HANDLE MasterCanHandle;
74
75 // Baudrate values for Peak board :
76 // CAN_BAUD_1M CAN_BAUD_500K CAN_BAUD_250K CAN_BAUD_125K CAN_BAUD_100K CAN_BAUD_50K
77 // CAN_BAUD_20K CAN_BAUD_10K CAN_BAUD_5K
78
79 #ifdef CAN_BAUD_500K
80 int TranslateBaudeRate(char* optarg){
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};
95 #else
96 int TranslateBaudeRate(char* optarg){
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};
111 #endif
112
113
114 void catch_signal(int sig)
115 {
116   signal(SIGTERM, catch_signal);
117   signal(SIGINT, catch_signal);
118   eprintf("Got Signal %d\n",sig);
119 }
120
121 void help()
122 {
123   printf("**************************************************************\n");
124   printf("*  TestMasterSlave                                           *\n");
125   printf("*                                                            *\n");
126   printf("*  A simple example for PC. It does implement 2 CanOpen      *\n");
127   printf("*  nodes in the same process. A master and a slave. Both     *\n");
128   printf("*  communicate together, exchanging periodically NMT, SYNC,  *\n");
129   printf("*  SDO and PDO.                                              *\n");
130   printf("*                                                            *\n");
131   printf("*   Usage:                                                   *\n");
132   printf("*   ./TestMasterSlave  [OPTIONS]                             *\n");
133   printf("*                                                            *\n");
134   printf("*   OPTIONS:                                                 *\n");
135   printf("*    Slave:                                                  *\n");
136   printf("*     -s : bus name [\"0\"]                                    *\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");
142   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         }
174 }
175
176 /****************************************************************************/
177 /***************************  MAIN  *****************************************/
178 /****************************************************************************/
179 int main(int argc,char **argv)
180 {
181
182   char c;
183   extern char *optarg;
184
185   while ((c = getopt(argc, argv, "-m:s:M:S:")) != EOF)
186   {
187     switch(c)
188     {
189       case 's' :
190         if (optarg[0] == 0)
191         {
192           help();
193           exit(1);
194         }
195         SlaveBoard.busname = optarg;
196         break;
197       case 'm' :
198         if (optarg[0] == 0)
199         {
200           help();
201           exit(1);
202         }
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);
220         break;
221       default:
222         help();
223         exit(1);
224     }
225   }
226
227         /* install signal handler for manual break */
228         signal(SIGTERM, catch_signal);
229         signal(SIGINT, catch_signal);
230         
231         // Open CAN devices
232         if(SlaveBoard.baudrate)
233                 if((SlaveCanHandle = canOpen(&SlaveBoard))==NULL) goto fail_slave;
234
235         if(MasterBoard.baudrate)
236                 if((MasterCanHandle = canOpen(&MasterBoard))==NULL) goto fail_master;
237         
238         // Start timer thread
239         StartTimerLoop(&InitNodes);
240
241         // wait Ctrl-C
242         pause();
243         eprintf("Finishing.\n");
244         
245         // Stop timer thread
246         StopTimerLoop();
247         
248         // Close CAN devices (and can threads)
249         if(SlaveBoard.baudrate) canClose(SlaveCanHandle);
250 fail_master:
251         if(MasterBoard.baudrate) canClose(MasterCanHandle);     
252 fail_slave:
253         
254
255   return 0;
256 }