author | lbessard |
Mon, 10 Dec 2007 16:25:18 +0100 | |
changeset 336 | 42ab55532da8 |
parent 320 | f82e758840bd |
child 343 | 118c1cabd0b0 |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
This file is part of CanFestival, a library implementing CanOpen Stack. |
|
3 |
||
4 |
Copyright (C): Edouard TISSERANT and Francis DUPIN |
|
5 |
||
6 |
See COPYING file for copyrights details. |
|
7 |
||
8 |
This library is free software; you can redistribute it and/or |
|
9 |
modify it under the terms of the GNU Lesser General Public |
|
10 |
License as published by the Free Software Foundation; either |
|
11 |
version 2.1 of the License, or (at your option) any later version. |
|
12 |
||
13 |
This library is distributed in the hope that it will be useful, |
|
14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 |
Lesser General Public License for more details. |
|
17 |
||
18 |
You should have received a copy of the GNU Lesser General Public |
|
19 |
License along with this library; if not, write to the Free Software |
|
20 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
21 |
*/ |
|
208 | 22 |
/*! |
23 |
** @file states.c |
|
24 |
** @author Edouard TISSERANT and Francis DUPIN |
|
25 |
** @date Tue Jun 5 09:32:32 2007 |
|
26 |
** |
|
27 |
** @brief |
|
28 |
** |
|
29 |
** |
|
30 |
*/ |
|
0 | 31 |
|
32 |
#include "states.h" |
|
33 |
#include "def.h" |
|
204
44ce74232ccb
Some fixes for visual studio C compiler compatiblity.
etisserant
parents:
195
diff
changeset
|
34 |
#include "dcf.h" |
0 | 35 |
#include "nmtSlave.h" |
284 | 36 |
#include "emcy.h" |
0 | 37 |
|
208 | 38 |
/** Prototypes for internals functions */ |
39 |
/*! |
|
40 |
** |
|
41 |
** |
|
42 |
** @param d |
|
43 |
** @param newCommunicationState |
|
44 |
**/ |
|
53 | 45 |
void switchCommunicationState(CO_Data* d, |
46 |
s_state_communication *newCommunicationState); |
|
47 |
||
208 | 48 |
/*! |
49 |
** |
|
50 |
** |
|
51 |
** @param d |
|
52 |
** |
|
53 |
** @return |
|
54 |
**/ |
|
0 | 55 |
e_nodeState getState(CO_Data* d) |
56 |
{ |
|
57 |
return d->nodeState; |
|
58 |
} |
|
59 |
||
208 | 60 |
/*! |
61 |
** |
|
62 |
** |
|
63 |
** @param d |
|
64 |
** @param m |
|
65 |
**/ |
|
0 | 66 |
void canDispatch(CO_Data* d, Message *m) |
67 |
{ |
|
68 |
switch(m->cob_id.w >> 7) |
|
69 |
{ |
|
284 | 70 |
case SYNC: /* can be a SYNC or a EMCY message */ |
71 |
if(m->cob_id.w == 0x080) /* SYNC */ |
|
72 |
{ |
|
73 |
if(d->CurrentCommunicationState.csSYNC) |
|
74 |
proceedSYNC(d); |
|
75 |
} else /* EMCY */ |
|
76 |
if(d->CurrentCommunicationState.csEmergency) |
|
77 |
proceedEMCY(d,m); |
|
0 | 78 |
break; |
215 | 79 |
/* case TIME_STAMP: */ |
0 | 80 |
case PDO1tx: |
81 |
case PDO1rx: |
|
82 |
case PDO2tx: |
|
83 |
case PDO2rx: |
|
84 |
case PDO3tx: |
|
85 |
case PDO3rx: |
|
86 |
case PDO4tx: |
|
87 |
case PDO4rx: |
|
88 |
if (d->CurrentCommunicationState.csPDO) |
|
89 |
proceedPDO(d,m); |
|
90 |
break; |
|
91 |
case SDOtx: |
|
92 |
case SDOrx: |
|
93 |
if (d->CurrentCommunicationState.csSDO) |
|
94 |
proceedSDO(d,m); |
|
95 |
break; |
|
96 |
case NODE_GUARD: |
|
97 |
if (d->CurrentCommunicationState.csHeartbeat) |
|
98 |
proceedNODE_GUARD(d,m); |
|
99 |
break; |
|
100 |
case NMT: |
|
88 | 101 |
if (*(d->iam_a_slave)) |
0 | 102 |
{ |
103 |
proceedNMTstateChange(d,m); |
|
104 |
} |
|
105 |
} |
|
106 |
} |
|
107 |
||
108 |
#define StartOrStop(CommType, FuncStart, FuncStop) \ |
|
291
6165554cbfe9
Some tricks in communication services status struct (states.c) and in sync.c to fit with Beremiz generated node code.
etisserant
parents:
284
diff
changeset
|
109 |
if(newCommunicationState->CommType && d->CurrentCommunicationState.CommType == 0){\ |
195
1510dd61ead0
Added debug level opt in configure and re-enabled debug macros.
etisserant
parents:
183
diff
changeset
|
110 |
MSG_WAR(0x9999,#FuncStart, 9999);\ |
0 | 111 |
d->CurrentCommunicationState.CommType = 1;\ |
112 |
FuncStart;\ |
|
291
6165554cbfe9
Some tricks in communication services status struct (states.c) and in sync.c to fit with Beremiz generated node code.
etisserant
parents:
284
diff
changeset
|
113 |
}else if(!newCommunicationState->CommType && d->CurrentCommunicationState.CommType == 1){\ |
195
1510dd61ead0
Added debug level opt in configure and re-enabled debug macros.
etisserant
parents:
183
diff
changeset
|
114 |
MSG_WAR(0x9999,#FuncStop, 9999);\ |
0 | 115 |
d->CurrentCommunicationState.CommType = 0;\ |
116 |
FuncStop;\ |
|
117 |
} |
|
118 |
#define None |
|
208 | 119 |
|
120 |
/*! |
|
121 |
** |
|
122 |
** |
|
123 |
** @param d |
|
124 |
** @param newCommunicationState |
|
125 |
**/ |
|
0 | 126 |
void switchCommunicationState(CO_Data* d, s_state_communication *newCommunicationState) |
127 |
{ |
|
128 |
StartOrStop(csSDO, None, resetSDO(d)) |
|
129 |
StartOrStop(csSYNC, startSYNC(d), stopSYNC(d)) |
|
130 |
StartOrStop(csHeartbeat, heartbeatInit(d), heartbeatStop(d)) |
|
284 | 131 |
StartOrStop(csEmergency, emergencyInit(d), emergencyStop(d)) |
235
f812bf6b7237
Preliminary implementation of Event Timer and Inhibit Timer driven TPDO
etisserant
parents:
215
diff
changeset
|
132 |
StartOrStop(csPDO, PDOInit(d), PDOStop(d)) |
14 | 133 |
StartOrStop(csBoot_Up, None, slaveSendBootUp(d)) |
0 | 134 |
} |
135 |
||
208 | 136 |
/*! |
137 |
** |
|
138 |
** |
|
139 |
** @param d |
|
140 |
** @param newState |
|
141 |
** |
|
142 |
** @return |
|
143 |
**/ |
|
0 | 144 |
UNS8 setState(CO_Data* d, e_nodeState newState) |
145 |
{ |
|
146 |
while(newState != d->nodeState){ |
|
147 |
switch( newState ){ |
|
148 |
case Initialisation: |
|
149 |
{ |
|
71 | 150 |
s_state_communication newCommunicationState = {1, 0, 0, 0, 0, 0}; |
215 | 151 |
/* This will force a second loop for the state switch */ |
0 | 152 |
d->nodeState = Initialisation; |
153 |
newState = Pre_operational; |
|
154 |
switchCommunicationState(d, &newCommunicationState); |
|
215 | 155 |
/* call user app related state func. */ |
0 | 156 |
(*d->initialisation)(); |
178 | 157 |
|
0 | 158 |
} |
159 |
break; |
|
160 |
||
161 |
case Pre_operational: |
|
162 |
{ |
|
178 | 163 |
|
71 | 164 |
s_state_communication newCommunicationState = {0, 1, 1, 1, 1, 0}; |
0 | 165 |
d->nodeState = Pre_operational; |
166 |
newState = Pre_operational; |
|
167 |
switchCommunicationState(d, &newCommunicationState); |
|
178 | 168 |
if (!(*(d->iam_a_slave))) |
169 |
{ |
|
320
f82e758840bd
Some fixes suggested by Luis Jim?nez, plus some minor enhancements in DCF.
etisserant
parents:
314
diff
changeset
|
170 |
send_consise_dcf(d,0x01); |
f82e758840bd
Some fixes suggested by Luis Jim?nez, plus some minor enhancements in DCF.
etisserant
parents:
314
diff
changeset
|
171 |
/*(*d->preOperational)() will be called once dcf sent */ |
178 | 172 |
} |
173 |
else |
|
174 |
{ |
|
175 |
(*d->preOperational)(); |
|
176 |
} |
|
0 | 177 |
} |
178 |
break; |
|
179 |
||
180 |
case Operational: |
|
181 |
if(d->nodeState == Initialisation) return 0xFF; |
|
182 |
{ |
|
71 | 183 |
s_state_communication newCommunicationState = {0, 1, 1, 1, 1, 1}; |
0 | 184 |
d->nodeState = Operational; |
185 |
newState = Operational; |
|
186 |
switchCommunicationState(d, &newCommunicationState); |
|
187 |
(*d->operational)(); |
|
188 |
} |
|
189 |
break; |
|
190 |
||
191 |
case Stopped: |
|
192 |
if(d->nodeState == Initialisation) return 0xFF; |
|
193 |
{ |
|
71 | 194 |
s_state_communication newCommunicationState = {0, 0, 0, 0, 1, 0}; |
0 | 195 |
d->nodeState = Stopped; |
196 |
newState = Stopped; |
|
197 |
switchCommunicationState(d, &newCommunicationState); |
|
198 |
(*d->stopped)(); |
|
199 |
} |
|
200 |
break; |
|
201 |
||
202 |
default: |
|
203 |
return 0xFF; |
|
215 | 204 |
}/* end switch case */ |
94
bdf4c86be6b2
Removed all non-supported and uncontrolled source code. Please refer to CVS version "Before_..." to see old code.
etisserant
parents:
88
diff
changeset
|
205 |
|
0 | 206 |
} |
207 |
return 0; |
|
208 |
} |
|
209 |
||
208 | 210 |
/*! |
211 |
** |
|
212 |
** |
|
213 |
** @param d |
|
214 |
** |
|
215 |
** @return |
|
216 |
**/ |
|
0 | 217 |
UNS8 getNodeId(CO_Data* d) |
218 |
{ |
|
219 |
return *d->bDeviceNodeId; |
|
220 |
} |
|
221 |
||
208 | 222 |
/*! |
223 |
** |
|
224 |
** |
|
225 |
** @param d |
|
226 |
** @param nodeId |
|
227 |
**/ |
|
0 | 228 |
void setNodeId(CO_Data* d, UNS8 nodeId) |
229 |
{ |
|
230 |
UNS16 offset = d->firstIndex->SDO_SVR; |
|
231 |
if(offset){ |
|
303
340348f0193f
Now, SetNodeId will change 1200h SDO server params to new defaul values only if already set to appropriate default values.
etisserant
parents:
291
diff
changeset
|
232 |
/* Adjust COB-ID Client->Server (rx) only id already set to default value*/ |
340348f0193f
Now, SetNodeId will change 1200h SDO server params to new defaul values only if already set to appropriate default values.
etisserant
parents:
291
diff
changeset
|
233 |
if(*(UNS32*)d->objdict[offset].pSubindex[1].pObject = 0x600 + *d->bDeviceNodeId){ |
215 | 234 |
/* cob_id_client = 0x600 + nodeId; */ |
0 | 235 |
*(UNS32*)d->objdict[offset].pSubindex[1].pObject = 0x600 + nodeId; |
303
340348f0193f
Now, SetNodeId will change 1200h SDO server params to new defaul values only if already set to appropriate default values.
etisserant
parents:
291
diff
changeset
|
236 |
} |
340348f0193f
Now, SetNodeId will change 1200h SDO server params to new defaul values only if already set to appropriate default values.
etisserant
parents:
291
diff
changeset
|
237 |
/* Adjust COB-ID Server -> Client (tx) only id already set to default value*/ |
340348f0193f
Now, SetNodeId will change 1200h SDO server params to new defaul values only if already set to appropriate default values.
etisserant
parents:
291
diff
changeset
|
238 |
if(*(UNS32*)d->objdict[offset].pSubindex[2].pObject = 0x580 + *d->bDeviceNodeId){ |
215 | 239 |
/* cob_id_server = 0x580 + nodeId; */ |
0 | 240 |
*(UNS32*)d->objdict[offset].pSubindex[2].pObject = 0x580 + nodeId; |
303
340348f0193f
Now, SetNodeId will change 1200h SDO server params to new defaul values only if already set to appropriate default values.
etisserant
parents:
291
diff
changeset
|
241 |
} |
0 | 242 |
} |
243 |
||
215 | 244 |
/* |
208 | 245 |
Initialize the server(s) SDO parameters |
246 |
Remember that only one SDO server is allowed, defined at index 0x1200 |
|
247 |
||
248 |
Initialize the client(s) SDO parameters |
|
249 |
Nothing to initialize (no default values required by the DS 401) |
|
250 |
Initialize the receive PDO communication parameters. Only for 0x1400 to 0x1403 |
|
251 |
*/ |
|
0 | 252 |
{ |
253 |
UNS8 i = 0; |
|
254 |
UNS16 offset = d->firstIndex->PDO_RCV; |
|
255 |
UNS16 lastIndex = d->lastIndex->PDO_RCV; |
|
256 |
UNS32 cobID[] = {0x200, 0x300, 0x400, 0x500}; |
|
257 |
if( offset ) while( (offset <= lastIndex) && (i < 4)) { |
|
236
905677ed00f3
Full preliminary implementation of TPDO transmit type:
etisserant
parents:
235
diff
changeset
|
258 |
if(*(UNS32*)d->objdict[offset].pSubindex[1].pObject == cobID[i] + *d->bDeviceNodeId) |
0 | 259 |
*(UNS32*)d->objdict[offset].pSubindex[1].pObject = cobID[i] + nodeId; |
260 |
i ++; |
|
261 |
offset ++; |
|
262 |
} |
|
263 |
} |
|
71 | 264 |
/* ** Initialize the transmit PDO communication parameters. Only for 0x1800 to 0x1803 */ |
0 | 265 |
{ |
266 |
UNS8 i = 0; |
|
267 |
UNS16 offset = d->firstIndex->PDO_TRS; |
|
268 |
UNS16 lastIndex = d->lastIndex->PDO_TRS; |
|
269 |
UNS32 cobID[] = {0x180, 0x280, 0x380, 0x480}; |
|
270 |
i = 0; |
|
271 |
if( offset ) while ((offset <= lastIndex) && (i < 4)) { |
|
236
905677ed00f3
Full preliminary implementation of TPDO transmit type:
etisserant
parents:
235
diff
changeset
|
272 |
if(*(UNS32*)d->objdict[offset].pSubindex[1].pObject == cobID[i] + *d->bDeviceNodeId) |
0 | 273 |
*(UNS32*)d->objdict[offset].pSubindex[1].pObject = cobID[i] + nodeId; |
274 |
i ++; |
|
275 |
offset ++; |
|
276 |
} |
|
277 |
} |
|
314
68e83c3ffbb5
Better EMCY support. Now EMCY COB-ID depend on OD 1014h entry, as told in DS-301.
etisserant
parents:
303
diff
changeset
|
278 |
|
68e83c3ffbb5
Better EMCY support. Now EMCY COB-ID depend on OD 1014h entry, as told in DS-301.
etisserant
parents:
303
diff
changeset
|
279 |
/* Update EMCY COB-ID if already set to default*/ |
68e83c3ffbb5
Better EMCY support. Now EMCY COB-ID depend on OD 1014h entry, as told in DS-301.
etisserant
parents:
303
diff
changeset
|
280 |
if(*d->error_cobid == *d->bDeviceNodeId + 0x80) |
68e83c3ffbb5
Better EMCY support. Now EMCY COB-ID depend on OD 1014h entry, as told in DS-301.
etisserant
parents:
303
diff
changeset
|
281 |
*d->error_cobid = nodeId + 0x80; |
68e83c3ffbb5
Better EMCY support. Now EMCY COB-ID depend on OD 1014h entry, as told in DS-301.
etisserant
parents:
303
diff
changeset
|
282 |
|
215 | 283 |
/* bDeviceNodeId is defined in the object dictionary. */ |
0 | 284 |
*d->bDeviceNodeId = nodeId; |
285 |
} |
|
149 | 286 |
|
287 |
void _initialisation(){} |
|
288 |
void _preOperational(){} |
|
289 |
void _operational(){} |
|
290 |
void _stopped(){} |