NNP STM Generic Remote Module git-main
Loading...
Searching...
No Matches
states.c
Go to the documentation of this file.
1
13#include "data.h"
14#include "sysdep.h"
15
18 s_state_communication *newCommunicationState);
19
20/**********************************************************************************************************
21* LOCAL GLOBAL VARIABLES
22**********************************************************************************************************/
23
24
25
32e_nodeState getState(CO_Data* d)
33{
34 return d->nodeState;
35}
36
37
44{
45 UNS16 cob_id = UNS16_LE(m->cob_id);
46
47 switch(cob_id >> 7)
48 {
49 case SYNC: /* can be a SYNC or a EMCY message */
50 if(cob_id == 0x080) /* SYNC */
51 {
52 if(d->CurrentCommunicationState.csSYNC)
53 processSYNC(d,m);
54 } else /* EMCY */
55 if(d->CurrentCommunicationState.csEmergency)
56 proceedEMCY(d,m);
57 break;
58 case BOOT:
59 if(cob_id == 0x140)
60 {
61 //Bootloader Query Node command
62 processBOOT(d,m);
63 }
64 break;
65 case PDO1tx:
66 case PDO1rx:
67 case PDO2tx:
68 case PDO2rx:
69 case PDO3tx:
70 case PDO3rx:
71 case PDO4tx:
72 case PDO4rx:
73 if (d->CurrentCommunicationState.csPDO)
74 processPDO(d,m);
75 break;
76 case SDOtx:
77 case SDOrx:
78 if (d->CurrentCommunicationState.csSDO)
79 processSDO(d,m);
80 break;
81 case NODE_GUARD:
82 if (d->CurrentCommunicationState.csHeartbeat || !(*(d->iam_a_slave)))
84 break;
85 case NMT:
86 // cause a reset to enter bootloader
87 if ( ((m->data[1]== 0x00) || (m->data[1]== getNodeId(d))) &&
88 (m->data[0]== NMT_Enter_Bootloader) )
89 {
90 NVIC_SystemReset();
91 }
92
93 if (*(d->iam_a_slave))
94 {
96 }
97#ifdef CO_ENABLE_LSS
98 case LSS:
99 if (!d->CurrentCommunicationState.csLSS)break;
100 if ((*(d->iam_a_slave)) && cob_id==MLSS_ADRESS)
101 {
102 proceedLSS_Slave(d,m);
103 }
104 else if(!(*(d->iam_a_slave)) && cob_id==SLSS_ADRESS)
105 {
106 proceedLSS_Master(d,m);
107 }
108 break;
109#endif
110 } // end CASE statement
111
112
113}
114
115#define StartOrStop(CommType, FuncStart, FuncStop) \
116 if(newCommunicationState->CommType && d->CurrentCommunicationState.CommType == 0){\
117 MSG_WAR(0x9999,#FuncStart, 9999);\
118 d->CurrentCommunicationState.CommType = 1;\
119 FuncStart;\
120 }else if(!newCommunicationState->CommType && d->CurrentCommunicationState.CommType == 1){\
121 MSG_WAR(0x9999,#FuncStop, 9999);\
122 d->CurrentCommunicationState.CommType = 0;\
123 FuncStop;\
124 }
125#define None
126
134{
135#ifdef CO_ENABLE_LSS
136 StartOrStop(csLSS, startLSS(d), stopLSS(d))
137#endif
138 StartOrStop(csSDO, None, resetSDO(d))
139 StartOrStop(csSYNC, startSYNC(d), stopSYNC(d))
140 StartOrStop(csHeartbeat, heartbeatInit(d), heartbeatStop(d))
141 StartOrStop(csEmergency, emergencyInit(d), emergencyStop(d))
142 StartOrStop(csPDO, PDOInit(d), PDOStop(d))
143 StartOrStop(csBoot_Up, None, slaveSendBootUp(d))
144}
145
161UNS8 setState(CO_Data* d, e_nodeState newState)
162{
163 if(newState != d->nodeState){
164 switch( newState ){
165 case Mode_Patient_Control:
166 {
167 s_state_communication newCommunicationState = {0, 1, 1, 1, 1, 1, 0};
168 d->nodeState = Mode_Patient_Control;
169 switchCommunicationState(d, &newCommunicationState);
170 }
171 break;
172
173 case Mode_X_Manual:
174 {
175 s_state_communication newCommunicationState = {0, 1, 1, 1, 1, 1, 0};
176 d->nodeState = Mode_X_Manual;
177 switchCommunicationState(d, &newCommunicationState);
178 }
179 break;
180
181 case Mode_Y_Manual:
182 {
183 s_state_communication newCommunicationState = {0, 1, 1, 1, 1, 1, 0};
184 d->nodeState = Mode_Y_Manual;
185 switchCommunicationState(d, &newCommunicationState);
186 }
187 break;
188
189 case Waiting:
190 {
191 s_state_communication newCommunicationState = {0, 1, 1, 0, 1, 0, 0};
192 d->nodeState = Waiting;
193 switchCommunicationState(d, &newCommunicationState);
194 (*d->waiting)(d);
195 }
196 break;
197
198 case Stopped:
199 {
200 s_state_communication newCommunicationState = {0, 1, 1, 0, 1, 0, 0};
201 d->nodeState = Stopped;
202 switchCommunicationState(d, &newCommunicationState);
203 (*d->stopped)(d);
204 }
205 break;
206
207 case Mode_Patient_Manual:
208 {
209 s_state_communication newCommunicationState = {0, 1, 1, 1, 1, 1, 0};
210 d->nodeState = Mode_Patient_Manual;
211 switchCommunicationState(d, &newCommunicationState);
212 }
213 break;
214
215 case Mode_Produce_X_Manual:
216 {
217 s_state_communication newCommunicationState = {0, 1, 1, 1, 1, 1, 0};
218 d->nodeState = Mode_Produce_X_Manual;
219 switchCommunicationState(d, &newCommunicationState);
220 /* call user app init callback now. */
221 }
222 break;
223
224 case Mode_Record_X:
225 {
226 s_state_communication newCommunicationState = {0, 1, 1, 1, 1, 1, 0};
227 d->nodeState = Mode_Record_X;
228 switchCommunicationState(d, &newCommunicationState);
229 /* call user app init callback now. */
230 }
231 break;
232
233 default:
234 return 0xFF;
235
236 }/* end switch case */
237
238 }
239 /* d->nodeState contains the final state */
240 /* may not be the requested state */
241 return d->nodeState;
242}
243
251{
252 return *d->bDeviceNodeId;
253}
254
261void setNodeId(CO_Data* d, UNS8 nodeId)
262{
263 UNS16 offset = d->firstIndex->SDO_SVR;
264
265#ifdef CO_ENABLE_LSS
266 d->lss_transfer.nodeID = nodeId;
267 *d->bDeviceNodeId = nodeId;
268
269 if(nodeId==0xFF)
270 {
271 *d->bDeviceNodeId = nodeId;
272 return;
273 }
274 else
275 #endif
276 if(!(nodeId > 0 && nodeId <= 127))
277 {
278 MSG_WAR(0x2D01, "Invalid NodeID",nodeId);
279 return;
280 }
281
282 if(offset)
283 {
284 /* Adjust COB-ID Client->Server (rx) only id already set to default value or id not valid (id==0xFF)*/
285 //if((*(UNS32*)d->objdict[offset].pSubindex[1].pObject == 0x600 + *d->bDeviceNodeId)||(*d->bDeviceNodeId==0xFF))
286 {
287 /* cob_id_client = 0x600 + nodeId; */
288 *(UNS32*)d->objdict[offset].pSubindex[1].pObject = 0x600 + nodeId;
289 }
290 /* Adjust COB-ID Server -> Client (tx) only id already set to default value or id not valid (id==0xFF)*/
291 //if((*(UNS32*)d->objdict[offset].pSubindex[2].pObject == 0x580 + *d->bDeviceNodeId)||(*d->bDeviceNodeId==0xFF))
292 {
293 /* cob_id_server = 0x580 + nodeId; */
294 *(UNS32*)d->objdict[offset].pSubindex[2].pObject = 0x580 + nodeId;
295 }
296 }
297
298 /*
299 Initialize the server(s) SDO parameters
300 Remember that only one SDO server is allowed, defined at index 0x1200
301
302
303 */
304
305 /* ** Initialize the transmit PDO communication parameters. Only for 0x1800 to 0x1803 */
306 {
307 UNS8 i = 0;
308 UNS16 offset = d->firstIndex->PDO_TRS;
309 UNS16 lastIndex = d->lastIndex->PDO_TRS;
310 UNS32 cobID[] = {0x180, 0x280, 0x380, 0x480};
311 i = 0;
312 if( offset ) while ((offset <= lastIndex) && (i < 4))
313 {
314 //if((*(UNS32*)d->objdict[offset].pSubindex[1].pObject == cobID[i] + *d->bDeviceNodeId)||(*d->bDeviceNodeId==0xFF))
315 *(UNS32*)d->objdict[offset].pSubindex[1].pObject = cobID[i] + nodeId;
316 i ++;
317 offset ++;
318 }
319 }
320
321 /* Update EMCY COB-ID if already set to default*/
322 if((*d->error_cobid == *d->bDeviceNodeId + 0x80)||(*d->bDeviceNodeId==0xFF))
323 *d->error_cobid = nodeId + 0x80;
324
325 /* bDeviceNodeId is defined in the object dictionary. */
326 *d->bDeviceNodeId = nodeId;
327}
328
335
342
349
356
363
370
371
#define UNS8
Unsigned int8 representation in CANFest.
Definition applicfg.h:25
#define UNS16
Unsigned int16 representation in CANFest.
Definition applicfg.h:26
#define MSG_WAR(num, str, val)
Definition of MSG_WAR.
Definition applicfg.h:64
#define UNS32
Unsigned int32 representation in CANFest.
Definition applicfg.h:27
UNS8 processBOOT(CO_Data *d, Message *m)
Initializes node ID and serial number data in the object dictionary.
Definition boot.c:15
void proceedEMCY(CO_Data *d, Message *m)
This function is responsible to process an EMCY canopen-message.
Definition emcy.c:202
void emergencyStop(CO_Data *d)
Stop EMCY producer and consumer.
Definition emcy.c:62
void emergencyInit(CO_Data *d)
Start EMCY consumer and producer.
Definition emcy.c:50
void heartbeatStop(CO_Data *d)
Stop heartbeat consumer and producer.
Definition lifegrd.c:219
void processNODE_GUARD(CO_Data *d, Message *m)
This function is responsible to process a canopen-message which seams to be an NMT Error Control Mess...
Definition lifegrd.c:68
void heartbeatInit(CO_Data *d)
Start heartbeat consumer and producer with respect to 0x1016 and 0x1017 object dictionary entries.
Definition lifegrd.c:189
void processNMTstateChange(CO_Data *d, Message *m)
Manage the reception of a NMT message from the master.
Definition nmtSlave.c:34
UNS8 slaveSendBootUp(CO_Data *d)
Transmit the boot-Up frame when the slave is moving from initialization state to pre_operational stat...
Definition nmtSlave.c:227
UNS8 processPDO(CO_Data *d, Message *m)
Compute a PDO frame reception bus_id is hardware dependant.
Definition pdo.c:144
void PDOInit(CO_Data *d)
Initialize PDO feature.
Definition pdo.c:752
void PDOStop(CO_Data *d)
Stop PDO feature.
Definition pdo.c:790
void resetSDO(CO_Data *d)
Reset all SDO buffers.
Definition sdo.c:120
UNS8 processSDO(CO_Data *d, Message *m)
Treat a SDO frame reception call the function sendSDO.
Definition sdo.c:649
UNS8 setState(CO_Data *d, e_nodeState newState)
Change the state of the node.
Definition states.c:161
UNS8 getNodeId(CO_Data *d)
Returns the nodId.
Definition states.c:250
void _mode_X_Manual(CO_Data *d)
Function that user app can overload.
Definition states.c:334
void _waiting(CO_Data *d)
Function that user app can overload.
Definition states.c:348
void _stopped(CO_Data *d)
Function that user app can overload.
Definition states.c:355
void _mode_Patient_Control(CO_Data *d)
Function that user app can overload.
Definition states.c:369
void switchCommunicationState(CO_Data *d, s_state_communication *newCommunicationState)
<BRIEF> switchCommunicationState
Definition states.c:133
e_nodeState getState(CO_Data *d)
Returns the state of the node.
Definition states.c:32
void _mode_Y_Manual(CO_Data *d)
Function that user app can overload.
Definition states.c:341
void setNodeId(CO_Data *d, UNS8 nodeId)
Define the node ID. Initialize the object dictionary.
Definition states.c:261
void _mode_Patient_Manual(CO_Data *d)
Function that user app can overload.
Definition states.c:362
void startSYNC(CO_Data *d)
<BRIEF> startSYNC
Definition sync.c:58
void stopSYNC(CO_Data *d)
<BRIEF> stopSYNC
Definition sync.c:83
UNS8 processSYNC(CO_Data *d, Message *m)
This function is called when the node is receiving a SYNC message (cob-id = 0x80).
Definition sync.c:135
void canDispatch(CO_Data *d, Message *m)
Called by driver/app when receiving messages.
Definition states.c:43
Definition can.h:13
UNS16 cob_id
Definition can.h:14
UNS8 data[8]
Definition can.h:17
This structure contains all necessary informations to define a CANOpen node.
Definition data.h:44