plugins/canfestival/cf_runtime.c
author etisserant
Sat, 24 May 2008 15:51:11 +0200
changeset 153 b352a2012691
parent 152 9abf2ac9cb8e
child 156 f2b35f4285b8
permissions -rw-r--r--
changed WidgetInspector shortcut to ctrl+home (ctrl+F12 was grabbed by KDE).
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
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
     4
%(nodes_includes)s
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
     5
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
     6
#define BOARD_DECL(nodename, busname, baudrate)\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
     7
    s_BOARD nodename##Board = {busname, baudrate};
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
     8
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
     9
%(board_decls)s
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    10
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    11
static int init_level=0;
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
    12
extern int common_ticktime__;
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    13
59
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 57
diff changeset
    14
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 57
diff changeset
    15
static void ConfigureSlaveNode(CO_Data* d, UNS8 nodeId)
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 57
diff changeset
    16
{
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 57
diff changeset
    17
    /* 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
    18
    setState(d, Operational);
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 57
diff changeset
    19
      
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 57
diff changeset
    20
    /* 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
    21
    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
    22
}
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
#define NODE_DECLARE(nodename, nodeid)\
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 57
diff changeset
    25
void nodename##_preOperational()\
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
    ConfigureSlaveNode(&nodename##_Data, nodeid);\
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 57
diff changeset
    28
}\
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 57
diff changeset
    29
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 57
diff changeset
    30
%(nodes_declare)s
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 57
diff changeset
    31
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 57
diff changeset
    32
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    33
#define NODE_INIT(nodename, nodeid) \
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
    34
    /* 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
    35
    nodename##_Data.CurrentCommunicationState.csSYNC = -1;\
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
    36
    /* 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
    37
    *nodename##_Data.COB_ID_Sync = 0x40000080;\
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
    38
    *nodename##_Data.Sync_Cycle_Period = common_ticktime__ * 1000;\
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    39
    /* Defining the node Id */\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    40
    setNodeId(&nodename##_Data, nodeid);\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    41
    /* init */\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    42
    setState(&nodename##_Data, Initialisation);
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    43
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    44
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
    45
{
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    46
    %(nodes_init)s
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    47
}
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    48
144
7818ec7b5c53 add Exit function and StopTimerLoop arg to match with recent changes in CanFestival
greg
parents: 137
diff changeset
    49
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
    50
{
7818ec7b5c53 add Exit function and StopTimerLoop arg to match with recent changes in CanFestival
greg
parents: 137
diff changeset
    51
}
7818ec7b5c53 add Exit function and StopTimerLoop arg to match with recent changes in CanFestival
greg
parents: 137
diff changeset
    52
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    53
#define NODE_CLOSE(nodename) \
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
    54
    if(init_level-- > 0)\
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    55
    {\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    56
        EnterMutex();\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    57
        setState(&nodename##_Data, Stopped);\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    58
        LeaveMutex();\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    59
        canClose(&nodename##_Data);\
152
9abf2ac9cb8e add TimerInit() and TimerCleanup() in cf_runtime for matching with Timer_unix
greg
parents: 144
diff changeset
    60
        #if !defined(WIN32) || defined(__CYGWIN__)\
9abf2ac9cb8e add TimerInit() and TimerCleanup() in cf_runtime for matching with Timer_unix
greg
parents: 144
diff changeset
    61
        		TimerCleanup();\
9abf2ac9cb8e add TimerInit() and TimerCleanup() in cf_runtime for matching with Timer_unix
greg
parents: 144
diff changeset
    62
        #endif\
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    63
    }
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    64
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    65
void __cleanup_%(locstr)s()
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    66
{
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    67
    %(nodes_close)s
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    68
    
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    69
    // Stop timer thread
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
    70
    if(init_level-- > 0)
144
7818ec7b5c53 add Exit function and StopTimerLoop arg to match with recent changes in CanFestival
greg
parents: 137
diff changeset
    71
        StopTimerLoop(&Exit);
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    72
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    73
}
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    74
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    75
#define NODE_OPEN(nodename)\
152
9abf2ac9cb8e add TimerInit() and TimerCleanup() in cf_runtime for matching with Timer_unix
greg
parents: 144
diff changeset
    76
	#if !defined(WIN32) || defined(__CYGWIN__)\
9abf2ac9cb8e add TimerInit() and TimerCleanup() in cf_runtime for matching with Timer_unix
greg
parents: 144
diff changeset
    77
		TimerInit();\
9abf2ac9cb8e add TimerInit() and TimerCleanup() in cf_runtime for matching with Timer_unix
greg
parents: 144
diff changeset
    78
	#endif\
9abf2ac9cb8e add TimerInit() and TimerCleanup() in cf_runtime for matching with Timer_unix
greg
parents: 144
diff changeset
    79
	nodename##_Data.preOperational = nodename##_preOperational;\
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    80
    if(!canOpen(&nodename##Board,&nodename##_Data)){\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    81
        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
    82
        return -1;\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    83
    }\
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    84
    init_level++;
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    85
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    86
/***************************  INIT  *****************************************/
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    87
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
    88
{
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
    89
#ifndef NOT_USE_DYNAMIC_LOADING
75
9ad18a387a96 Windows related enhancements
etisserant
parents: 59
diff changeset
    90
    if( !LoadCanDriver("%(candriver)s") ){
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
    91
        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
    92
        return -1;
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
    93
    }
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
    94
#endif      
52
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
    %(nodes_open)s
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
    // Start timer thread
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
    99
    StartTimerLoop(&InitNodes);
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   100
    init_level++;
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   101
    return 0;
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
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   104
#define NODE_SEND_SYNC(nodename)\
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   105
    sendSYNCMessage(&nodename##_Data);
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   106
137
187a4e2412e5 Modifying svgui plugin for following new SVGUIEditor version
lbessard
parents: 75
diff changeset
   107
void __retrieve_%(locstr)s()
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   108
{
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   109
    /* 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
   110
     * TODO : implement buffers to avoid such a big lock  
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   111
     *  */
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   112
    EnterMutex();
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   113
    /*Send Sync */
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   114
    %(nodes_send_sync)s
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   115
}
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   116
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   117
#define NODE_PROCEED_SYNC(nodename)\
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   118
    proceedSYNC(&nodename##_Data);
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   119
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   120
void __publish_%(locstr)s()
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   121
{
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   122
    /*Call SendPDOEvent */
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 52
diff changeset
   123
    %(nodes_proceed_sync)s
52
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   124
    LeaveMutex();
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   125
}
eaffcd0a2f03 Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff changeset
   126