2 This file is part of CanFestival, a library implementing CanOpen Stack.
4 Copyright (C): Edouard TISSERANT and Francis DUPIN
6 See COPYING file for copyrights details.
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.
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.
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
31 #include <can_driver.h>
32 #include <timers_driver.h>
36 #include "TestMasterSlave.h"
38 #define MyCase(fc) case fc: eprintf(#fc);break;
39 void print_message(Message *m)
42 switch(m->cob_id.w >> 7)
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]);
66 UNS32 OnMasterMap1Update(CO_Data* d, const indextable * unsused_indextable, UNS8 unsused_bSubindex)
68 eprintf("OnSlaveMap1Update:%d\n", SlaveMap1);
72 CAN_HANDLE SlaveCanHandle;
73 CAN_HANDLE MasterCanHandle;
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
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;
93 s_BOARD SlaveBoard = {"0", CAN_BAUD_500K, &TestSlave_Data};
94 s_BOARD MasterBoard = {"1", CAN_BAUD_500K, &TestMaster_Data};
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;
109 s_BOARD SlaveBoard = {"0", 500, &TestSlave_Data};
110 s_BOARD MasterBoard = {"1", 500, &TestMaster_Data};
114 void catch_signal(int sig)
116 signal(SIGTERM, catch_signal);
117 signal(SIGINT, catch_signal);
118 eprintf("Got Signal %d\n",sig);
123 printf("**************************************************************\n");
124 printf("* TestMasterSlave *\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");
131 printf("* Usage: *\n");
132 printf("* ./TestMasterSlave [OPTIONS] *\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");
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");
143 printf("**************************************************************\n");
146 /*************************** INIT *****************************************/
147 void InitNodes(CO_Data* d, UNS32 id)
149 /****************************** INITIALISATION SLAVE *******************************/
150 if(SlaveBoard.baudrate) {
151 /* Defining the node Id */
152 setNodeId(&TestSlave_Data, 0x02);
154 setState(&TestSlave_Data, Initialisation);
157 /****************************** INITIALISATION MASTER *******************************/
158 if(MasterBoard.baudrate){
159 RegisterSetODentryCallBack(&TestMaster_Data, 0x2000, 0, &OnMasterMap1Update);
161 /* Defining the node Id */
162 setNodeId(&TestMaster_Data, 0x01);
165 setState(&TestMaster_Data, Initialisation);
167 /****************************** START *******************************/
168 /* Put the master in operational mode */
169 setState(&TestMaster_Data, Operational);
171 /* Ask slave node to go in operational mode */
172 masterSendNMTstateChange (&TestMaster_Data, 0x02, NMT_Start_Node);
176 /****************************************************************************/
177 /*************************** MAIN *****************************************/
178 /****************************************************************************/
179 int main(int argc,char **argv)
185 while ((c = getopt(argc, argv, "-m:s:M:S:")) != EOF)
195 SlaveBoard.busname = optarg;
203 MasterBoard.busname = optarg;
211 SlaveBoard.baudrate = TranslateBaudeRate(optarg);
219 MasterBoard.baudrate = TranslateBaudeRate(optarg);
227 /* install signal handler for manual break */
228 signal(SIGTERM, catch_signal);
229 signal(SIGINT, catch_signal);
232 if(SlaveBoard.baudrate)
233 if((SlaveCanHandle = canOpen(&SlaveBoard))==NULL) goto fail_slave;
235 if(MasterBoard.baudrate)
236 if((MasterCanHandle = canOpen(&MasterBoard))==NULL) goto fail_master;
238 // Start timer thread
239 StartTimerLoop(&InitNodes);
243 eprintf("Finishing.\n");
248 // Close CAN devices (and can threads)
249 if(SlaveBoard.baudrate) canClose(SlaveCanHandle);
251 if(MasterBoard.baudrate) canClose(MasterCanHandle);