canfestival/cf_runtime.c
author Laurent Bessard
Mon, 10 Jun 2013 01:15:39 +0200
changeset 1237 0c8b8ef9559b
parent 778 4f9e6d9a3d98
child 1397 21a2ea65cb30
permissions -rw-r--r--
Fixed support for default function block name in FBDBlockDialog
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"
776
c81397b665b6 Fixed generated CF code to match latest changes
Edouard Tisserant
parents: 721
diff changeset
     3
#include "dcf.h"
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
     4
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
     5
/* CanFestival nodes generated OD headers*/
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
     6
%(nodes_includes)s
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
     7
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
     8
#define BOARD_DECL(nodename, busname, baudrate)\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
     9
    s_BOARD nodename##Board = {busname, baudrate};
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    10
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    11
/* CAN channels declaration */
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    12
%(board_decls)s
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    13
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    14
/* 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
    15
static int init_level=0;
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    16
/* Retrieve PLC cycle time */
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
    17
extern int common_ticktime__;
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    18
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    19
/* Per master node slavebootup callbacks. Checks that
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    20
 * 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
    21
%(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
    22
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    23
/* One slave node post_sync callback.
336
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 235
diff changeset
    24
 * Used to align PLC tick-time on CANopen SYNC
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    25
 */
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    26
%(post_sync)s
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    27
778
4f9e6d9a3d98 Updated CF bootup strategy.
Edouard Tisserant
parents: 776
diff changeset
    28
/* Triggers DCF transission
4f9e6d9a3d98 Updated CF bootup strategy.
Edouard Tisserant
parents: 776
diff changeset
    29
 */
4f9e6d9a3d98 Updated CF bootup strategy.
Edouard Tisserant
parents: 776
diff changeset
    30
%(pre_op)s
4f9e6d9a3d98 Updated CF bootup strategy.
Edouard Tisserant
parents: 776
diff changeset
    31
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    32
#define NODE_FORCE_SYNC(nodename) \
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
    33
    /* 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
    34
    nodename##_Data.CurrentCommunicationState.csSYNC = -1;\
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
    35
    /* 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
    36
    *nodename##_Data.COB_ID_Sync = 0x40000080;\
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    37
    *nodename##_Data.Sync_Cycle_Period = common_ticktime__ * 1000;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    38
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    39
#define NODE_INIT(nodename, nodeid) \
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    40
    /* Defining the node Id */\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    41
    setNodeId(&nodename##_Data, nodeid);\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    42
    /* init */\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    43
    setState(&nodename##_Data, Initialisation);
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    44
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    45
#define NODE_MASTER_INIT(nodename, nodeid) \
336
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 235
diff changeset
    46
    NODE_FORCE_SYNC(nodename) \
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 235
diff changeset
    47
    NODE_INIT(nodename, nodeid)
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    48
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    49
#define NODE_SLAVE_INIT(nodename, nodeid) \
336
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 235
diff changeset
    50
    NODE_INIT(nodename, nodeid)
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
    51
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    52
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
    53
{
336
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 235
diff changeset
    54
    %(slavebootup_register)s
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 235
diff changeset
    55
    %(post_sync_register)s
778
4f9e6d9a3d98 Updated CF bootup strategy.
Edouard Tisserant
parents: 776
diff changeset
    56
    %(pre_op_register)s
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    57
    %(nodes_init)s
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    58
}
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    59
336
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 235
diff changeset
    60
#define NODE_STOP(nodename) \
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 235
diff changeset
    61
    if(init_level-- > 0)\
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 235
diff changeset
    62
    {\
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 235
diff changeset
    63
        masterSendNMTstateChange(&nodename##_Data, 0, NMT_Reset_Node);\
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 235
diff changeset
    64
        setState(&nodename##_Data, Stopped);\
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 235
diff changeset
    65
    }
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 235
diff changeset
    66
144
7818ec7b5c53 add Exit function and StopTimerLoop arg to match with recent changes in CanFestival
greg
parents: 137
diff changeset
    67
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
    68
{
336
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 235
diff changeset
    69
    %(nodes_stop)s
144
7818ec7b5c53 add Exit function and StopTimerLoop arg to match with recent changes in CanFestival
greg
parents: 137
diff changeset
    70
}
7818ec7b5c53 add Exit function and StopTimerLoop arg to match with recent changes in CanFestival
greg
parents: 137
diff changeset
    71
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    72
#define NODE_CLOSE(nodename) \
336
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 235
diff changeset
    73
    if(init_level_c-- > 0)\
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    74
    {\
336
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 235
diff changeset
    75
      canClose(&nodename##_Data);\
52
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
512
36aeab46f27d Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents: 360
diff changeset
    78
void __cleanup_%(locstr)s(void)
52
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
    // Stop timer thread
203
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    81
    if(init_level-- > 0){
360
32339ad7d9ae update cf_runtime.c (now call TimerInit and TimerCleanup on win32 platform)
greg
parents: 336
diff changeset
    82
    int init_level_c = init_level;
144
7818ec7b5c53 add Exit function and StopTimerLoop arg to match with recent changes in CanFestival
greg
parents: 137
diff changeset
    83
        StopTimerLoop(&Exit);
203
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    84
        %(nodes_close)s
336
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 235
diff changeset
    85
    }
360
32339ad7d9ae update cf_runtime.c (now call TimerInit and TimerCleanup on win32 platform)
greg
parents: 336
diff changeset
    86
32339ad7d9ae update cf_runtime.c (now call TimerInit and TimerCleanup on win32 platform)
greg
parents: 336
diff changeset
    87
    TimerCleanup();
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    88
}
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    89
512
36aeab46f27d Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents: 360
diff changeset
    90
#ifndef stderr
36aeab46f27d Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents: 360
diff changeset
    91
#define fprintf(...)
36aeab46f27d Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents: 360
diff changeset
    92
#define fflush(...)
36aeab46f27d Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents: 360
diff changeset
    93
#endif
36aeab46f27d Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents: 360
diff changeset
    94
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    95
#define NODE_OPEN(nodename)\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    96
    if(!canOpen(&nodename##Board,&nodename##_Data)){\
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 203
diff changeset
    97
        fprintf(stderr,"Cannot open CAN intefrace %%s at speed %%s\n for CANopen node \"" #nodename "\"",nodename##Board.busname, nodename##Board.baudrate);\
a66e150f2888 Improved debug data feedback.
etisserant
parents: 203
diff changeset
    98
        fflush(stderr);\
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    99
        return -1;\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   100
    }\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   101
    init_level++;
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   102
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   103
/***************************  INIT  *****************************************/
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   104
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
   105
{
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   106
#ifndef NOT_USE_DYNAMIC_LOADING
75
9ad18a387a96 Windows related enhancements
etisserant
parents: 59
diff changeset
   107
    if( !LoadCanDriver("%(candriver)s") ){
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   108
        fprintf(stderr, "Cannot load CAN interface library for CanFestival (%(candriver)s)\n");\
512
36aeab46f27d Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents: 360
diff changeset
   109
        fflush(stderr);\
36aeab46f27d Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents: 360
diff changeset
   110
        return -1;\
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   111
    }
336
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 235
diff changeset
   112
#endif
360
32339ad7d9ae update cf_runtime.c (now call TimerInit and TimerCleanup on win32 platform)
greg
parents: 336
diff changeset
   113
32339ad7d9ae update cf_runtime.c (now call TimerInit and TimerCleanup on win32 platform)
greg
parents: 336
diff changeset
   114
    TimerInit();
336
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 235
diff changeset
   115
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   116
    %(nodes_open)s
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
    // Start timer thread
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   119
    StartTimerLoop(&InitNodes);
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   120
    init_level++;
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   121
    return 0;
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   122
}
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
#define NODE_SEND_SYNC(nodename)\
776
c81397b665b6 Fixed generated CF code to match latest changes
Edouard Tisserant
parents: 721
diff changeset
   125
    if(getState(&nodename##_Data)==Operational){\
c81397b665b6 Fixed generated CF code to match latest changes
Edouard Tisserant
parents: 721
diff changeset
   126
        sendSYNCMessage(&nodename##_Data);\
c81397b665b6 Fixed generated CF code to match latest changes
Edouard Tisserant
parents: 721
diff changeset
   127
    }
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   128
512
36aeab46f27d Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents: 360
diff changeset
   129
void __retrieve_%(locstr)s(void)
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   130
{
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   131
    /* Locks the stack, so that no changes occurs while PLC access variables
336
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 235
diff changeset
   132
     * TODO : implement buffers to avoid such a big lock
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   133
     *  */
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   134
    EnterMutex();
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
   135
    /* Send Sync */
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   136
    %(nodes_send_sync)s
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   137
}
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   138
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   139
#define NODE_PROCEED_SYNC(nodename)\
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   140
    proceedSYNC(&nodename##_Data);
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   141
512
36aeab46f27d Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents: 360
diff changeset
   142
void __publish_%(locstr)s(void)
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   143
{
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 174
diff changeset
   144
    /* Process sync event */
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   145
    %(nodes_proceed_sync)s
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   146
    LeaveMutex();
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   147
}
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   148