author | laurent |
Sun, 08 Jan 2012 19:10:12 +0100 | |
changeset 661 | 5d3da49e8029 |
parent 512 | 36aeab46f27d |
permissions | -rw-r--r-- |
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 | 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 | 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); |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
235
diff
changeset
|
23 |
|
59
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. |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
235
diff
changeset
|
33 |
* 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
|
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 | 38 |
/* Artificially force sync state to 1 so that it is not started */\ |
39 |
nodename##_Data.CurrentCommunicationState.csSYNC = -1;\ |
|
40 |
/* Force sync period to common_ticktime__ so that other node can read it*/\ |
|
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) \ |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
235
diff
changeset
|
51 |
NODE_FORCE_SYNC(nodename) \ |
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
235
diff
changeset
|
52 |
NODE_INIT(nodename, nodeid) |
178
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) \ |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
235
diff
changeset
|
55 |
NODE_INIT(nodename, nodeid) |
178
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 |
{ |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
235
diff
changeset
|
59 |
%(slavebootup_register)s |
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
235
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 |
|
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
235
diff
changeset
|
64 |
#define NODE_STOP(nodename) \ |
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
235
diff
changeset
|
65 |
if(init_level-- > 0)\ |
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
235
diff
changeset
|
66 |
{\ |
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
235
diff
changeset
|
67 |
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
|
68 |
setState(&nodename##_Data, Stopped);\ |
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
235
diff
changeset
|
69 |
} |
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
235
diff
changeset
|
70 |
|
144
7818ec7b5c53
add Exit function and StopTimerLoop arg to match with recent changes in CanFestival
greg
parents:
137
diff
changeset
|
71 |
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
|
72 |
{ |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
235
diff
changeset
|
73 |
%(nodes_stop)s |
144
7818ec7b5c53
add Exit function and StopTimerLoop arg to match with recent changes in CanFestival
greg
parents:
137
diff
changeset
|
74 |
} |
7818ec7b5c53
add Exit function and StopTimerLoop arg to match with recent changes in CanFestival
greg
parents:
137
diff
changeset
|
75 |
|
52
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
76 |
#define NODE_CLOSE(nodename) \ |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
235
diff
changeset
|
77 |
if(init_level_c-- > 0)\ |
52
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
78 |
{\ |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
235
diff
changeset
|
79 |
canClose(&nodename##_Data);\ |
52
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
80 |
} |
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
81 |
|
512
36aeab46f27d
Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents:
360
diff
changeset
|
82 |
void __cleanup_%(locstr)s(void) |
52
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 |
// Stop timer thread |
203 | 85 |
if(init_level-- > 0){ |
360
32339ad7d9ae
update cf_runtime.c (now call TimerInit and TimerCleanup on win32 platform)
greg
parents:
336
diff
changeset
|
86 |
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
|
87 |
StopTimerLoop(&Exit); |
203 | 88 |
%(nodes_close)s |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
235
diff
changeset
|
89 |
} |
360
32339ad7d9ae
update cf_runtime.c (now call TimerInit and TimerCleanup on win32 platform)
greg
parents:
336
diff
changeset
|
90 |
|
32339ad7d9ae
update cf_runtime.c (now call TimerInit and TimerCleanup on win32 platform)
greg
parents:
336
diff
changeset
|
91 |
TimerCleanup(); |
52
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
92 |
} |
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
93 |
|
512
36aeab46f27d
Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents:
360
diff
changeset
|
94 |
#ifndef stderr |
36aeab46f27d
Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents:
360
diff
changeset
|
95 |
#define fprintf(...) |
36aeab46f27d
Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents:
360
diff
changeset
|
96 |
#define fflush(...) |
36aeab46f27d
Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents:
360
diff
changeset
|
97 |
#endif |
36aeab46f27d
Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents:
360
diff
changeset
|
98 |
|
52
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
99 |
#define NODE_OPEN(nodename)\ |
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
100 |
if(!canOpen(&nodename##Board,&nodename##_Data)){\ |
235 | 101 |
fprintf(stderr,"Cannot open CAN intefrace %%s at speed %%s\n for CANopen node \"" #nodename "\"",nodename##Board.busname, nodename##Board.baudrate);\ |
102 |
fflush(stderr);\ |
|
52
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
103 |
return -1;\ |
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
104 |
}\ |
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
105 |
init_level++; |
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
106 |
|
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
107 |
/*************************** INIT *****************************************/ |
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
108 |
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
|
109 |
{ |
57 | 110 |
#ifndef NOT_USE_DYNAMIC_LOADING |
75 | 111 |
if( !LoadCanDriver("%(candriver)s") ){ |
57 | 112 |
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
|
113 |
fflush(stderr);\ |
36aeab46f27d
Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents:
360
diff
changeset
|
114 |
return -1;\ |
57 | 115 |
} |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
235
diff
changeset
|
116 |
#endif |
360
32339ad7d9ae
update cf_runtime.c (now call TimerInit and TimerCleanup on win32 platform)
greg
parents:
336
diff
changeset
|
117 |
|
32339ad7d9ae
update cf_runtime.c (now call TimerInit and TimerCleanup on win32 platform)
greg
parents:
336
diff
changeset
|
118 |
TimerInit(); |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
235
diff
changeset
|
119 |
|
52
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
120 |
%(nodes_open)s |
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
121 |
|
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
122 |
// Start timer thread |
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
123 |
StartTimerLoop(&InitNodes); |
57 | 124 |
init_level++; |
52
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
125 |
return 0; |
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
126 |
} |
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
127 |
|
57 | 128 |
#define NODE_SEND_SYNC(nodename)\ |
129 |
sendSYNCMessage(&nodename##_Data); |
|
130 |
||
512
36aeab46f27d
Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents:
360
diff
changeset
|
131 |
void __retrieve_%(locstr)s(void) |
52
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
132 |
{ |
57 | 133 |
/* 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
|
134 |
* TODO : implement buffers to avoid such a big lock |
57 | 135 |
* */ |
52
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
136 |
EnterMutex(); |
178
2390b409eb93
Added PLC tick alignement on external synchronization source feature.
etisserant
parents:
174
diff
changeset
|
137 |
/* Send Sync */ |
57 | 138 |
%(nodes_send_sync)s |
52
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
139 |
} |
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
140 |
|
57 | 141 |
#define NODE_PROCEED_SYNC(nodename)\ |
142 |
proceedSYNC(&nodename##_Data); |
|
143 |
||
512
36aeab46f27d
Improved CanFEstival support for LPC - now build and link plugin generated files
edouard
parents:
360
diff
changeset
|
144 |
void __publish_%(locstr)s(void) |
52
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
145 |
{ |
178
2390b409eb93
Added PLC tick alignement on external synchronization source feature.
etisserant
parents:
174
diff
changeset
|
146 |
/* Process sync event */ |
57 | 147 |
%(nodes_proceed_sync)s |
52
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
148 |
LeaveMutex(); |
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
149 |
} |
eaffcd0a2f03
Added CanFestival "main" runtime part, initialize, all nodes, and start CF timer loop
etisserant
parents:
diff
changeset
|
150 |