-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathstates.cpp
319 lines (288 loc) · 10.1 KB
/
states.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
This file is part of CanFestival, a library implementing CanOpen Stack.
Copyright (C): Edouard TISSERANT and Francis DUPIN
See COPYING file for copyrights details.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*!
** @file states.c
** @author Edouard TISSERANT and Francis DUPIN
** @date Tue Jun 5 09:32:32 2007
**
** @brief
**
**
*/
#include "data.h"
#include "sysdep.h"
/** Prototypes for internals functions */
/*!
**
**
** @param d
** @param newCommunicationState
**/
void switchCommunicationState(s_state_communication *newCommunicationState);
/*!
**
**
** @param d
**
** @return
**/
e_nodeState getState() {
return ObjDict_Data.nodeState;
}
/*!
**
**
** @param d
** @param m
**/
void canDispatch(Message *m) {
UNS16 cob_id = UNS16_LE(m->cob_id);
switch (cob_id >> 7) {
case SYNC: /* can be a SYNC or a EMCY message */
if (cob_id == 0x080) /* SYNC */
{
if (ObjDict_Data.CurrentCommunicationState.csSYNC)
proceedSYNC();
} else /* EMCY */
if (ObjDict_Data.CurrentCommunicationState.csEmergency)
proceedEMCY(m);
break;
/* case TIME_STAMP: */
case PDO1tx:
case PDO1rx:
case PDO2tx:
case PDO2rx:
case PDO3tx:
case PDO3rx:
case PDO4tx:
case PDO4rx:
if (ObjDict_Data.CurrentCommunicationState.csPDO)
proceedPDO(m);
break;
case SDOtx:
case SDOrx:
if (ObjDict_Data.CurrentCommunicationState.csSDO)
proceedSDO(m);
break;
case NODE_GUARD:
if (ObjDict_Data.CurrentCommunicationState.csLifeGuard)
proceedNODE_GUARD(m);
break;
case NMT:
// if (ObjDict_iam_a_slave) {
proceedNMTstateChange(m);
// }
break;
#ifdef CO_ENABLE_LSS
case LSS:
if (!ObjDict_Data.CurrentCommunicationState.csLSS)break;
if ((*(ObjDict_iam_a_slave)) && cob_id==MLSS_ADRESS)
{
proceedLSS_Slave(m);
}
else if(!(*(ObjDict_iam_a_slave)) && cob_id==SLSS_ADRESS)
{
proceedLSS_Master(m);
}
break;
#endif
}
}
#define StartOrStop(CommType, FuncStart, FuncStop) \
if(newCommunicationState->CommType && ObjDict_Data.CurrentCommunicationState.CommType == 0){\
MSG_WAR(0x9999,#FuncStart, 9999);\
ObjDict_Data.CurrentCommunicationState.CommType = 1;\
FuncStart;\
}else if(!newCommunicationState->CommType && ObjDict_Data.CurrentCommunicationState.CommType == 1){\
MSG_WAR(0x9999,#FuncStop, 9999);\
ObjDict_Data.CurrentCommunicationState.CommType = 0;\
FuncStop;\
}
#define None
/*!
**
**
** @param d
** @param newCommunicationState
**/
void switchCommunicationState(s_state_communication *newCommunicationState) {
#ifdef CO_ENABLE_LSS
StartOrStop(csLSS, startLSS(), stopLSS())
#endif
StartOrStop(csSDO, None, resetSDO())
StartOrStop(csSYNC, startSYNC(), stopSYNC())
StartOrStop(csLifeGuard, lifeGuardInit(), lifeGuardStop())
StartOrStop(csEmergency, emergencyInit(), emergencyStop())
StartOrStop(csPDO, PDOInit(), PDOStop())
StartOrStop(csBoot_Up, None, slaveSendBootUp())
}
/*!
**
**
** @param d
** @param newState
**
** @return
**/
UNS8 setState(e_nodeState newState) {
if (newState != ObjDict_Data.nodeState) {
switch (newState) {
case Initialisation: {
s_state_communication newCommunicationState = { 1, 0, 0, 0, 0, 0, 0 };
ObjDict_Data.nodeState = Initialisation;
switchCommunicationState(&newCommunicationState);
/* call user app init callback now. */
/* ObjDict_Data.initialisation MUST NOT CALL SetState */
// (*ObjDict_Data.initialisation)();
}
/* Automatic transition - No break statement ! */
/* Transition from Initialisation to Pre_operational */
/* is automatic as defined in DS301. */
/* App don't have to call SetState(Pre_operational) */
case Pre_operational: {
s_state_communication newCommunicationState = { 0, 1, 1, 1, 1, 0, 1 };
ObjDict_Data.nodeState = Pre_operational;
switchCommunicationState(&newCommunicationState);
// (*ObjDict_Data.preOperational)();
}
break;
case Operational:
if (ObjDict_Data.nodeState == Initialisation)
return 0xFF;
{
s_state_communication newCommunicationState = { 0, 1, 1, 1, 1, 1, 0 };
ObjDict_Data.nodeState = Operational;
newState = Operational;
switchCommunicationState(&newCommunicationState);
// (*ObjDict_Data.operational)();
}
break;
case Stopped:
if (ObjDict_Data.nodeState == Initialisation)
return 0xFF;
{
s_state_communication newCommunicationState = { 0, 0, 0, 0, 1, 0, 1 };
ObjDict_Data.nodeState = Stopped;
newState = Stopped;
switchCommunicationState(&newCommunicationState);
// (*ObjDict_Data.stopped)();
}
break;
default:
return 0xFF;
}/* end switch case */
}
/* ObjDict_Data.nodeState contains the final state */
/* may not be the requested state */
return ObjDict_Data.nodeState;
}
/*!
**
**
** @param d
**
** @return
**/
UNS8 getNodeId() {
return ObjDict_bDeviceNodeId;
}
#ifdef CO_ENABLE_CHANGE_NODE_ID
/*!
**
**
** @param d
** @param nodeId
**/
void setNodeId(UNS8 nodeId) {
const subindex *si;
UNS8 si_size;
ODCallback_t *callbacks;
#ifdef CO_ENABLE_LSS
ObjDict_Data.lss_transfer.nodeID=nodeId;
if(nodeId==0xFF) {
*ObjDict_Data.bDeviceNodeId = nodeId;
return;
}
else
#endif
if (!(nodeId > 0 && nodeId <= 127)) {
MSG_WAR(0x2D01, "Invalid NodeID",nodeId);
return;
}
if (!(si= ObjDict_scanIndexOD(0x1200, &si_size, &callbacks))) {
/* Adjust COB-ID Client->Server (rx) only id already set to default value or id not valid (id==0xFF)*/
if ((*(UNS16*) si[1].pObject == 0x600 + *ObjDict_Data.bDeviceNodeId) || (*ObjDict_Data.bDeviceNodeId == 0xFF)) {
/* cob_id_client = 0x600 + nodeId; */
*(UNS16*) si[1].pObject = 0x600 + nodeId;
}
/* Adjust COB-ID Server -> Client (tx) only id already set to default value or id not valid (id==0xFF)*/
if ((*(UNS16*) si[2].pObject == 0x580 + *ObjDict_Data.bDeviceNodeId) || (*ObjDict_Data.bDeviceNodeId == 0xFF)) {
/* cob_id_server = 0x580 + nodeId; */
*(UNS16*) si[2].pObject = 0x580 + nodeId;
}
}
/*
Initialize the server(s) SDO parameters
Remember that only one SDO server is allowed, defined at index 0x1200
Initialize the client(s) SDO parameters
Nothing to initialize (no default values required by the DS 401)
Initialize the receive PDO communication parameters. Only for 0x1400 to 0x1403
*/
{
UNS8 i = 0;
UNS32 cobID[] = {0x200, 0x300, 0x400, 0x500};
while ((si= ObjDict_scanIndexOD(0x1400+i, &si_size, &callbacks)) && (i < 4)) {
if ((*(UNS16*) si[1].pObject
== cobID[i] + *ObjDict_Data.bDeviceNodeId)
|| (*ObjDict_Data.bDeviceNodeId == 0xFF))
*(UNS16*) si[1].pObject = cobID[i]
+ nodeId;
i++;
}
}
/* ** Initialize the transmit PDO communication parameters. Only for 0x1800 to 0x1803 */
{
UNS8 i = 0;
UNS32 cobID[] = {0x180, 0x280, 0x380, 0x480};
i = 0;
while ((si= ObjDict_scanIndexOD(0x1800+i, &si_size, &callbacks)) && (i < 4)) {
if ((*(UNS16*) si[1].pObject
== cobID[i] + *ObjDict_Data.bDeviceNodeId)
|| (*ObjDict_Data.bDeviceNodeId == 0xFF))
*(UNS16*) si[1].pObject = cobID[i]
+ nodeId;
i++;
}
}
/* Update EMCY COB-ID if already set to default*/
if ((error_cobid == *ObjDict_Data.bDeviceNodeId + 0x80)
|| (*ObjDict_Data.bDeviceNodeId == 0xFF))
error_cobid = nodeId + 0x80;
/* bDeviceNodeId is defined in the object dictionary. */
*ObjDict_Data.bDeviceNodeId = nodeId;
}
#endif
// void _initialisation() {}
// void _preOperational() {
// #ifdef CO_ENABLE_MASTER
// if (!(*(ObjDict_iam_a_slave))) {
// masterSendNMTstateChange(0, NMT_Reset_Node);
// }
// #endif
// }
// void _operational() {}
// void _stopped() {}