examples/SillySlave/slave.c
changeset 400 7d845f5d730c
child 454 bc000083297a
equal deleted inserted replaced
399:8d22c323fe0f 400:7d845f5d730c
       
     1 /*
       
     2 Copyright (C): Giuseppe Massimo BERTANI
       
     3 gmbertani@users.sourceforge.net
       
     4 
       
     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 /**
       
    24  * @file slave.c
       
    25  * @author GMB
       
    26  * @date 17/1/08
       
    27  *
       
    28  * This file is part of SillySlave demo for CANfestival
       
    29  * open source CANopen stack.
       
    30  */ 
       
    31 
       
    32 #include "SillySlave.h"
       
    33 #include "slave.h"
       
    34 #include "main.h"
       
    35 
       
    36 
       
    37 static UNS8 slaveNode = 0;  
       
    38 
       
    39 void InitNode(CO_Data* d, UNS32 id)
       
    40 {
       
    41     /* Defining the node Id */
       
    42     setNodeId(&SillySlave_Data, slaveNode);
       
    43     /* CAN init */
       
    44     setState(&SillySlave_Data, Initialisation);
       
    45 }
       
    46 
       
    47 INTEGER8 InitCANdevice( UNS8 bus, UNS32 baudrate, UNS8 node )
       
    48 { 
       
    49 char busName[2];
       
    50 char baudRate[7];
       
    51 s_BOARD board;
       
    52 
       
    53     sprintf(busName, "%u", bus);
       
    54     sprintf(baudRate, "%u", baudrate);
       
    55     board.busname = busName;
       
    56     board.baudrate = baudRate;
       
    57 
       
    58     slaveNode = node;
       
    59 
       
    60     SillySlave_Data.heartbeatError = SillySlave_heartbeatError;
       
    61     SillySlave_Data.initialisation = SillySlave_initialisation;
       
    62     SillySlave_Data.preOperational = SillySlave_preOperational;
       
    63     SillySlave_Data.operational = SillySlave_operational;
       
    64     SillySlave_Data.stopped = SillySlave_stopped;
       
    65     SillySlave_Data.post_sync = SillySlave_post_sync;
       
    66     SillySlave_Data.post_TPDO = SillySlave_post_TPDO;
       
    67     SillySlave_Data.storeODSubIndex = SillySlave_storeODSubIndex;
       
    68     SillySlave_Data.post_emcy = SillySlave_post_emcy;
       
    69     
       
    70     if(!canOpen(&board, &SillySlave_Data))
       
    71     {
       
    72         printf("\n\aInitCANdevice() CAN bus %s opening error, baudrate=%s\n",board.busname, board.baudrate);
       
    73         return -1;
       
    74     }
       
    75 
       
    76 
       
    77     printf("\nInitCANdevice(), canOpen() OK, starting timer loop...\n");
       
    78 
       
    79     /* Start timer thread */
       
    80     StartTimerLoop(&InitNode); 
       
    81     
       
    82 	/* wait Ctrl-C */
       
    83 	pause();
       
    84 	printf("\nFinishing.\n");
       
    85 	
       
    86 	/* Stop slave */
       
    87     setState(&SillySlave_Data, Stopped);
       
    88 	
       
    89 	/* Stop timer thread */
       
    90 	StopTimerLoop();
       
    91     return 0;
       
    92 }
       
    93 
       
    94 void SillySlave_heartbeatError(CO_Data* d, UNS8 heartbeatID)
       
    95 {
       
    96     printf("SillySlave_heartbeatError %d\n", heartbeatID);
       
    97 }
       
    98 
       
    99 void SillySlave_initialisation(CO_Data* d )
       
   100 {
       
   101     UNS32 PDO1_COBID = 0x0180 + NODE_MASTER; 
       
   102     UNS8 size = sizeof(PDO1_COBID); 
       
   103     
       
   104     printf("SillySlave_initialisation\n");
       
   105 
       
   106     /* sets TXPDO1 COB-ID to match master node ID */
       
   107     writeLocalDict( 
       
   108             &SillySlave_Data,       /*CO_Data* d*/
       
   109             0x1800,                 /*UNS16 index*/
       
   110             0x01,                   /*UNS8 subind*/ 
       
   111             &PDO1_COBID,            /*void * pSourceData,*/ 
       
   112             &size,                  /* UNS8 * pExpectedSize*/
       
   113             RW);                    /* UNS8 checkAccess */
       
   114             
       
   115     /* value sent to master at each SYNC received */
       
   116     LifeSignal = 0;
       
   117 }
       
   118 
       
   119 void SillySlave_preOperational(CO_Data* d)
       
   120 {
       
   121     printf("SillySlave_preOperational\n");
       
   122 }
       
   123 
       
   124 void SillySlave_operational(CO_Data* d)
       
   125 {
       
   126     printf("SillySlave_operational\n");
       
   127 }
       
   128 
       
   129 void SillySlave_stopped(CO_Data* d)
       
   130 {
       
   131     printf("SillySlave_stopped\n");
       
   132 }
       
   133 
       
   134 void SillySlave_post_sync(CO_Data* d)
       
   135 {
       
   136     printf("SillySlave_post_sync: \n");
       
   137     LifeSignal++;
       
   138 }
       
   139 
       
   140 void SillySlave_post_TPDO(CO_Data* d)
       
   141 {
       
   142     printf("SillySlave_post_TPDO: \n");
       
   143     printf("LifeSignal = %u\n", LifeSignal);    
       
   144 }
       
   145 
       
   146 void SillySlave_storeODSubIndex(CO_Data* d, UNS16 wIndex, UNS8 bSubindex)
       
   147 {
       
   148     /*TODO : 
       
   149      * - call getODEntry for index and subindex, 
       
   150      * - save content to file, database, flash, nvram, ...
       
   151      * 
       
   152      * To ease flash organisation, index of variable to store
       
   153      * can be established by scanning d->objdict[d->ObjdictSize]
       
   154      * for variables to store.
       
   155      * 
       
   156      * */
       
   157     printf("SillySlave_storeODSubIndex : %4.4x %2.2xh\n", wIndex,  bSubindex);
       
   158 }
       
   159 
       
   160 void SillySlave_post_emcy(CO_Data* d, UNS8 nodeID, UNS16 errCode, UNS8 errReg)
       
   161 {
       
   162     printf("Slave received EMCY message. Node: %2.2xh  ErrorCode: %4.4x  ErrorRegister: %2.2xh\n", nodeID, errCode, errReg);
       
   163 }
       
   164