]> rtime.felk.cvut.cz Git - CanFestival-3.git/blobdiff - examples/TestMasterSlave/TestMasterSlave.c
- add RTAI support
[CanFestival-3.git] / examples / TestMasterSlave / TestMasterSlave.c
index 8ba09de790abb08648f9fcba66b033d110a15ced..021e39a328c8dae7d5a0955ae2aa964c434603ce 100644 (file)
@@ -30,59 +30,27 @@ void pause(void)
 #else
 #include <stdio.h>
 #include <string.h>
-//#include <sys/time.h>
-//#include <unistd.h>
+#include <unistd.h>
 #include <stdlib.h>
 #include <signal.h>
 #endif
 
-#include <applicfg.h>
-#include <can_driver.h>
-#include <timers_driver.h>
+#include "canfestival.h"
+//#include <can_driver.h>
+//#include <timers_driver.h>
 
 #include "Master.h"
 #include "Slave.h"
 #include "TestMasterSlave.h"
 
-#define MyCase(fc) case fc: eprintf(#fc);break;
-void print_message(Message *m)
-{
-       int i;
-       switch(m->cob_id.w >> 7)
-       {
-               MyCase(SYNC)
-               MyCase(TIME_STAMP)
-               MyCase(PDO1tx)
-               MyCase(PDO1rx)
-               MyCase(PDO2tx)
-               MyCase(PDO2rx)
-               MyCase(PDO3tx)
-               MyCase(PDO3rx)
-               MyCase(PDO4tx)
-               MyCase(PDO4rx)
-               MyCase(SDOtx)
-               MyCase(SDOrx)
-               MyCase(NODE_GUARD)
-               MyCase(NMT)
-       }
-       eprintf(" rtr:%d", m->rtr);
-       eprintf(" len:%d", m->len);
-       for (i = 0 ; i < m->len ; i++)
-               eprintf(" %02x", m->data[i]);
-       eprintf("\n");
-}
-
 UNS32 OnMasterMap1Update(CO_Data* d, const indextable * unsused_indextable, UNS8 unsused_bSubindex)
 {
        eprintf("OnSlaveMap1Update:%d\n", SlaveMap1);
        return 0;
 }
 
-CAN_PORT SlaveCanHandle;
-CAN_PORT MasterCanHandle;
-
-s_BOARD SlaveBoard = {"0", "500K"};
-s_BOARD MasterBoard = {"1", "500K"};
+s_BOARD SlaveBoard = {"0", "125K"};
+s_BOARD MasterBoard = {"1", "125K"};
 
 #if !defined(WIN32) || defined(__CYGWIN__)
 void catch_signal(int sig)
@@ -93,7 +61,7 @@ void catch_signal(int sig)
 }
 #endif
 
-void help()
+void help(void)
 {
   printf("**************************************************************\n");
   printf("*  TestMasterSlave                                           *\n");
@@ -101,7 +69,8 @@ void help()
   printf("*  A simple example for PC. It does implement 2 CanOpen      *\n");
   printf("*  nodes in the same process. A master and a slave. Both     *\n");
   printf("*  communicate together, exchanging periodically NMT, SYNC,  *\n");
-  printf("*  SDO and PDO.                                              *\n");
+  printf("*  SDO and PDO. Master configure heartbeat producer time     *\n");
+  printf("*  at 1000 ms for slave node-id 0x02 by concise DCF.         *\n");                                  
   printf("*                                                            *\n");
   printf("*   Usage:                                                   *\n");
   printf("*   ./TestMasterSlave  [OPTIONS]                             *\n");
@@ -124,41 +93,44 @@ void help()
 void InitNodes(CO_Data* d, UNS32 id)
 {
        /****************************** INITIALISATION SLAVE *******************************/
-       if(SlaveBoard.baudrate) {
-               /* Defining the node Id */
+       if(strcmp(SlaveBoard.baudrate, "none")) {
                setNodeId(&TestSlave_Data, 0x02);
+
                /* init */
                setState(&TestSlave_Data, Initialisation);
        }
 
        /****************************** INITIALISATION MASTER *******************************/
-       if(MasterBoard.baudrate){
+       if(strcmp(MasterBoard.baudrate, "none")){
                RegisterSetODentryCallBack(&TestMaster_Data, 0x2000, 0, &OnMasterMap1Update);
-
+               
                /* Defining the node Id */
                setNodeId(&TestMaster_Data, 0x01);
 
                /* init */
                setState(&TestMaster_Data, Initialisation);
-
-               /****************************** START *******************************/
-               /* Put the master in operational mode */
-               setState(&TestMaster_Data, Operational);
-  
-               /* Ask slave node to go in operational mode */
-               masterSendNMTstateChange (&TestMaster_Data, 0x02, NMT_Start_Node);
+                       
        }
 }
 
+/***************************  EXIT  *****************************************/
+void Exit(CO_Data* d, UNS32 id)
+{
+       masterSendNMTstateChange(&TestMaster_Data, 0x02, NMT_Reset_Node);    
+    
+    //Stop master
+       setState(&TestMaster_Data, Stopped);
+}
+
 /****************************************************************************/
 /***************************  MAIN  *****************************************/
 /****************************************************************************/
 int main(int argc,char **argv)
 {
 
-  char c;
+  int c;
   extern char *optarg;
-  char* LibraryPath="libcanfestival_can_virtual.so";
+  char* LibraryPath="../../drivers/can_virtual/libcanfestival_can_virtual.so";
 
   while ((c = getopt(argc, argv, "-m:s:M:S:l:")) != EOF)
   {
@@ -214,44 +186,64 @@ int main(int argc,char **argv)
   /* install signal handler for manual break */
        signal(SIGTERM, catch_signal);
        signal(SIGINT, catch_signal);
+       TimerInit();
 #endif
 
 #ifndef NOT_USE_DYNAMIC_LOADING
-       LoadCanDriver(LibraryPath);
+       if (LoadCanDriver(LibraryPath) == NULL)
+           printf("Unable to load library: %s\n",LibraryPath);
 #endif         
        // Open CAN devices
-       if(SlaveBoard.baudrate){
-               SlaveCanHandle = canOpen(&SlaveBoard,&TestSlave_Data);
-               if(SlaveCanHandle == NULL){
+
+       if(strcmp(SlaveBoard.baudrate, "none")){
+               
+               TestSlave_Data.heartbeatError = TestSlave_heartbeatError;
+               TestSlave_Data.initialisation = TestSlave_initialisation;
+               TestSlave_Data.preOperational = TestSlave_preOperational;
+               TestSlave_Data.operational = TestSlave_operational;
+               TestSlave_Data.stopped = TestSlave_stopped;
+               TestSlave_Data.post_sync = TestSlave_post_sync;
+               TestSlave_Data.post_TPDO = TestSlave_post_TPDO;
+               TestSlave_Data.storeODSubIndex = TestSlave_storeODSubIndex;
+               TestSlave_Data.post_emcy = TestSlave_post_emcy;
+
+               if(!canOpen(&SlaveBoard,&TestSlave_Data)){
                        eprintf("Cannot open Slave Board (%s,%s)\n",SlaveBoard.busname, SlaveBoard.baudrate);
                        goto fail_slave;
                }
        }
-
-       if(MasterBoard.baudrate){
-               MasterCanHandle = canOpen(&MasterBoard,&TestMaster_Data);
-               if(MasterCanHandle == NULL){
-                       eprintf("Cannot open Master Board (%s,%s)\n",SlaveBoard.busname, SlaveBoard.baudrate);
+       if(strcmp(MasterBoard.baudrate, "none")){
+               
+               TestMaster_Data.heartbeatError = TestMaster_heartbeatError;
+               TestMaster_Data.initialisation = TestMaster_initialisation;
+               TestMaster_Data.preOperational = TestMaster_preOperational;
+               TestMaster_Data.operational = TestMaster_operational;
+               TestMaster_Data.stopped = TestMaster_stopped;
+               TestMaster_Data.post_sync = TestMaster_post_sync;
+               TestMaster_Data.post_TPDO = TestMaster_post_TPDO;
+               TestMaster_Data.post_emcy = TestMaster_post_emcy;
+               TestMaster_Data.post_SlaveBootup=TestMaster_post_SlaveBootup;
+               
+               if(!canOpen(&MasterBoard,&TestMaster_Data)){
+                       eprintf("Cannot open Master Board (%s,%s)\n",MasterBoard.busname, MasterBoard.baudrate);
                        goto fail_master;
                }
        }
-       
+
        // Start timer thread
        StartTimerLoop(&InitNodes);
 
        // wait Ctrl-C
        pause();
-       eprintf("Finishing.\n");
-       
+
        // Stop timer thread
-       StopTimerLoop();
+       StopTimerLoop(&Exit);
        
        // Close CAN devices (and can threads)
-       if(SlaveBoard.baudrate) canClose(SlaveCanHandle);
+       if(strcmp(MasterBoard.baudrate, "none")) canClose(&TestMaster_Data);    
 fail_master:
-       if(MasterBoard.baudrate) canClose(MasterCanHandle);     
+       if(strcmp(SlaveBoard.baudrate, "none")) canClose(&TestSlave_Data);
 fail_slave:
-       
-
-  return 0;
+       TimerCleanup();
+       return 0;
 }