plugins/canfestival/cf_runtime.c
author greg
Mon, 07 Jul 2008 13:37:23 +0200
changeset 196 93d06827e31b
parent 178 2390b409eb93
child 203 cb9901076a21
permissions -rw-r--r--
changes in plc_Win32_main to match with new api (alignment feature)
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
     1
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
     2
#include "canfestival.h"
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
     3
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
     4
/* CanFestival nodes generated OD headers*/
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
     5
%(nodes_includes)s
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
     6
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
     7
#define BOARD_DECL(nodename, busname, baudrate)\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
     8
    s_BOARD nodename##Board = {busname, baudrate};
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
     9
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    10
/* CAN channels declaration */
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    11
%(board_decls)s
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    12
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    13
/* Keep track of init level to cleanup correctly */
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    14
static int init_level=0;
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    15
/* Retrieve PLC cycle time */
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
    16
extern int common_ticktime__;
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    17
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    18
/* Called once all NetworkEdit declares slaves have booted*/
160
4b63934885e1 NMY start node on slavebootup
greg
parents: 156
diff changeset
    19
static void Master_post_SlaveBootup(CO_Data* d, UNS8 nodeId)
59
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 57
diff changeset
    20
{
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 57
diff changeset
    21
    /* Put the master in operational mode */
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 57
diff changeset
    22
    setState(d, Operational);
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 57
diff changeset
    23
      
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 57
diff changeset
    24
    /* Ask slave node to go in operational mode */
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 57
diff changeset
    25
    masterSendNMTstateChange (d, 0, NMT_Start_Node);
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 57
diff changeset
    26
}
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 57
diff changeset
    27
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    28
/* Per master node slavebootup callbacks. Checks that
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    29
 * every node have booted before calling Master_post_SlaveBootup */
169
8e87b69286c0 SlaveBootup now set operational state for both local node and network nodes only when all nodes declared in network edit have been initialized.
etisserant
parents: 160
diff changeset
    30
%(slavebootups)s
8e87b69286c0 SlaveBootup now set operational state for both local node and network nodes only when all nodes declared in network edit have been initialized.
etisserant
parents: 160
diff changeset
    31
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    32
/* One slave node post_sync callback.
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    33
 * Used to align PLC tick-time on CANopen SYNC 
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    34
 */
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    35
%(post_sync)s
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    36
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    37
#define NODE_FORCE_SYNC(nodename) \
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
    38
    /* Artificially force sync state to 1 so that it is not started */\
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
    39
    nodename##_Data.CurrentCommunicationState.csSYNC = -1;\
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
    40
    /* Force sync period to common_ticktime__ so that other node can read it*/\
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
    41
    *nodename##_Data.COB_ID_Sync = 0x40000080;\
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    42
    *nodename##_Data.Sync_Cycle_Period = common_ticktime__ * 1000;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    43
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    44
#define NODE_INIT(nodename, nodeid) \
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    45
    /* Defining the node Id */\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    46
    setNodeId(&nodename##_Data, nodeid);\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    47
    /* init */\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    48
    setState(&nodename##_Data, Initialisation);
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    49
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    50
#define NODE_MASTER_INIT(nodename, nodeid) \
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    51
	NODE_FORCE_SYNC(nodename) \
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    52
	NODE_INIT(nodename, nodeid)
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    53
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    54
#define NODE_SLAVE_INIT(nodename, nodeid) \
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    55
	NODE_INIT(nodename, nodeid)
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    56
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    57
void InitNodes(CO_Data* d, UNS32 id)
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    58
{
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    59
	%(slavebootup_register)s
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    60
	%(post_sync_register)s
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    61
    %(nodes_init)s
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    62
}
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    63
144
7818ec7b5c53 add Exit function and StopTimerLoop arg to match with recent changes in CanFestival
greg
parents: 137
diff changeset
    64
void Exit(CO_Data* d, UNS32 id)
7818ec7b5c53 add Exit function and StopTimerLoop arg to match with recent changes in CanFestival
greg
parents: 137
diff changeset
    65
{
7818ec7b5c53 add Exit function and StopTimerLoop arg to match with recent changes in CanFestival
greg
parents: 137
diff changeset
    66
}
7818ec7b5c53 add Exit function and StopTimerLoop arg to match with recent changes in CanFestival
greg
parents: 137
diff changeset
    67
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    68
#define NODE_CLOSE(nodename) \
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
    69
    if(init_level-- > 0)\
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    70
    {\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    71
        EnterMutex();\
160
4b63934885e1 NMY start node on slavebootup
greg
parents: 156
diff changeset
    72
        masterSendNMTstateChange(&nodename##_Data, 0, NMT_Reset_Node);\
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    73
        setState(&nodename##_Data, Stopped);\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    74
        LeaveMutex();\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    75
        canClose(&nodename##_Data);\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    76
    }
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    77
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    78
void __cleanup_%(locstr)s()
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    79
{
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    80
    %(nodes_close)s
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    81
    
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    82
    // Stop timer thread
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
    83
    if(init_level-- > 0)
144
7818ec7b5c53 add Exit function and StopTimerLoop arg to match with recent changes in CanFestival
greg
parents: 137
diff changeset
    84
        StopTimerLoop(&Exit);
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    85
156
f2b35f4285b8 Changed canfestival plugin runtime template to reflect recent changes on unix.
etisserant
parents: 152
diff changeset
    86
    #if !defined(WIN32) || defined(__CYGWIN__)
f2b35f4285b8 Changed canfestival plugin runtime template to reflect recent changes on unix.
etisserant
parents: 152
diff changeset
    87
   		TimerCleanup();
f2b35f4285b8 Changed canfestival plugin runtime template to reflect recent changes on unix.
etisserant
parents: 152
diff changeset
    88
    #endif
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    89
}
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    90
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    91
#define NODE_OPEN(nodename)\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    92
    if(!canOpen(&nodename##Board,&nodename##_Data)){\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    93
        printf("Cannot open " #nodename " Board (%%s,%%s)\n",nodename##Board.busname, nodename##Board.baudrate);\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    94
        return -1;\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    95
    }\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    96
    init_level++;
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    97
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    98
/***************************  INIT  *****************************************/
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    99
int __init_%(locstr)s(int argc,char **argv)
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   100
{
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   101
#ifndef NOT_USE_DYNAMIC_LOADING
75
9ad18a387a96 Windows related enhancements
etisserant
parents: 59
diff changeset
   102
    if( !LoadCanDriver("%(candriver)s") ){
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   103
        fprintf(stderr, "Cannot load CAN interface library for CanFestival (%(candriver)s)\n");\
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   104
        return -1;
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   105
    }
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   106
#endif      
156
f2b35f4285b8 Changed canfestival plugin runtime template to reflect recent changes on unix.
etisserant
parents: 152
diff changeset
   107
	#if !defined(WIN32) || defined(__CYGWIN__)
f2b35f4285b8 Changed canfestival plugin runtime template to reflect recent changes on unix.
etisserant
parents: 152
diff changeset
   108
		TimerInit();
f2b35f4285b8 Changed canfestival plugin runtime template to reflect recent changes on unix.
etisserant
parents: 152
diff changeset
   109
	#endif
f2b35f4285b8 Changed canfestival plugin runtime template to reflect recent changes on unix.
etisserant
parents: 152
diff changeset
   110
	
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   111
    %(nodes_open)s
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   112
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   113
    // Start timer thread
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   114
    StartTimerLoop(&InitNodes);
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   115
    init_level++;
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   116
    return 0;
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   117
}
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   118
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   119
#define NODE_SEND_SYNC(nodename)\
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   120
    sendSYNCMessage(&nodename##_Data);
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   121
137
187a4e2412e5 Modifying svgui plugin for following new SVGUIEditor version
lbessard
parents: 75
diff changeset
   122
void __retrieve_%(locstr)s()
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   123
{
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   124
    /* Locks the stack, so that no changes occurs while PLC access variables
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   125
     * TODO : implement buffers to avoid such a big lock  
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   126
     *  */
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   127
    EnterMutex();
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
   128
    /* Send Sync */
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   129
    %(nodes_send_sync)s
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   130
}
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   131
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   132
#define NODE_PROCEED_SYNC(nodename)\
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   133
    proceedSYNC(&nodename##_Data);
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   134
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   135
void __publish_%(locstr)s()
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   136
{
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
   137
    /* Process sync event */
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   138
    %(nodes_proceed_sync)s
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   139
    LeaveMutex();
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   140
}
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   141