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 |
*/
|
|
22 |
|
|
23 |
#include "states.h"
|
|
24 |
#include "def.h"
|
|
25 |
|
|
26 |
#include "nmtSlave.h"
|
|
27 |
|
|
28 |
#ifdef LED_ENABLE
|
|
29 |
#include "led.h"
|
|
30 |
#else
|
|
31 |
#define led_set_state(a,b)
|
|
32 |
#endif
|
|
33 |
|
53
|
34 |
// Prototypes for internals functions
|
|
35 |
void switchCommunicationState(CO_Data* d,
|
|
36 |
s_state_communication *newCommunicationState);
|
|
37 |
|
|
38 |
/*****************************************************************************/
|
0
|
39 |
e_nodeState getState(CO_Data* d)
|
|
40 |
{
|
|
41 |
return d->nodeState;
|
|
42 |
}
|
|
43 |
|
53
|
44 |
/*****************************************************************************/
|
0
|
45 |
void canDispatch(CO_Data* d, Message *m)
|
|
46 |
{
|
|
47 |
switch(m->cob_id.w >> 7)
|
|
48 |
{
|
|
49 |
case SYNC:
|
|
50 |
if(d->CurrentCommunicationState.csSYNC)
|
|
51 |
proceedSYNC(d,m);
|
|
52 |
break;
|
|
53 |
//case TIME_STAMP:
|
|
54 |
case PDO1tx:
|
|
55 |
case PDO1rx:
|
|
56 |
case PDO2tx:
|
|
57 |
case PDO2rx:
|
|
58 |
case PDO3tx:
|
|
59 |
case PDO3rx:
|
|
60 |
case PDO4tx:
|
|
61 |
case PDO4rx:
|
|
62 |
if (d->CurrentCommunicationState.csPDO)
|
|
63 |
proceedPDO(d,m);
|
|
64 |
break;
|
|
65 |
case SDOtx:
|
|
66 |
case SDOrx:
|
|
67 |
if (d->CurrentCommunicationState.csSDO)
|
|
68 |
proceedSDO(d,m);
|
|
69 |
break;
|
|
70 |
case NODE_GUARD:
|
|
71 |
if (d->CurrentCommunicationState.csHeartbeat)
|
|
72 |
proceedNODE_GUARD(d,m);
|
|
73 |
break;
|
|
74 |
case NMT:
|
|
75 |
if (d->iam_a_slave)
|
|
76 |
{
|
|
77 |
proceedNMTstateChange(d,m);
|
|
78 |
}
|
|
79 |
#ifdef CANOPEN_LSS_ENABLE
|
|
80 |
default:
|
|
81 |
if (m->cob_id.w == 0x7E4 || m->cob_id.w == 0x705)
|
|
82 |
{
|
|
83 |
proceedLSS(d,m);
|
|
84 |
}
|
|
85 |
#endif
|
|
86 |
}
|
|
87 |
}
|
|
88 |
|
|
89 |
#define StartOrStop(CommType, FuncStart, FuncStop) \
|
|
90 |
if(newCommunicationState->CommType && !d->CurrentCommunicationState.CommType){\
|
|
91 |
MSG_ERR(0x9999,#FuncStart, 9999);\
|
|
92 |
d->CurrentCommunicationState.CommType = 1;\
|
|
93 |
FuncStart;\
|
|
94 |
}else if(!newCommunicationState->CommType && d->CurrentCommunicationState.CommType){\
|
|
95 |
MSG_ERR(0x9999,#FuncStop, 9999);\
|
|
96 |
d->CurrentCommunicationState.CommType = 0;\
|
|
97 |
FuncStop;\
|
|
98 |
}
|
|
99 |
#define None
|
53
|
100 |
|
|
101 |
/*****************************************************************************/
|
0
|
102 |
void switchCommunicationState(CO_Data* d, s_state_communication *newCommunicationState)
|
|
103 |
{
|
|
104 |
StartOrStop(csSDO, None, resetSDO(d))
|
|
105 |
StartOrStop(csSYNC, startSYNC(d), stopSYNC(d))
|
|
106 |
StartOrStop(csHeartbeat, heartbeatInit(d), heartbeatStop(d))
|
|
107 |
// StartOrStop(Emergency,,)
|
|
108 |
StartOrStop(csPDO, None, None)
|
14
|
109 |
StartOrStop(csBoot_Up, None, slaveSendBootUp(d))
|
0
|
110 |
}
|
|
111 |
|
53
|
112 |
/*****************************************************************************/
|
0
|
113 |
UNS8 setState(CO_Data* d, e_nodeState newState)
|
|
114 |
{
|
|
115 |
while(newState != d->nodeState){
|
|
116 |
switch( newState ){
|
|
117 |
case Initialisation:
|
|
118 |
{
|
|
119 |
s_state_communication newCommunicationState = {
|
|
120 |
csBoot_Up: 1,
|
|
121 |
csSDO: 0,
|
|
122 |
csEmergency: 0,
|
|
123 |
csSYNC: 0,
|
|
124 |
csHeartbeat: 0,
|
|
125 |
csPDO: 0};
|
|
126 |
// This will force a second loop for the state switch
|
|
127 |
d->nodeState = Initialisation;
|
|
128 |
newState = Pre_operational;
|
|
129 |
switchCommunicationState(d, &newCommunicationState);
|
|
130 |
// call user app related state func.
|
|
131 |
(*d->initialisation)();
|
|
132 |
}
|
|
133 |
break;
|
|
134 |
|
|
135 |
case Pre_operational:
|
|
136 |
{
|
|
137 |
s_state_communication newCommunicationState = {
|
|
138 |
csBoot_Up: 0,
|
|
139 |
csSDO: 1,
|
|
140 |
csEmergency: 1,
|
|
141 |
csSYNC: 1,
|
|
142 |
csHeartbeat: 1,
|
|
143 |
csPDO: 0};
|
|
144 |
d->nodeState = Pre_operational;
|
|
145 |
newState = Pre_operational;
|
|
146 |
switchCommunicationState(d, &newCommunicationState);
|
|
147 |
(*d->preOperational)();
|
|
148 |
}
|
|
149 |
break;
|
|
150 |
|
|
151 |
case Operational:
|
|
152 |
if(d->nodeState == Initialisation) return 0xFF;
|
|
153 |
{
|
|
154 |
s_state_communication newCommunicationState = {
|
|
155 |
csBoot_Up: 0,
|
|
156 |
csSDO: 1,
|
|
157 |
csEmergency: 1,
|
|
158 |
csSYNC: 1,
|
|
159 |
csHeartbeat: 1,
|
|
160 |
csPDO: 1};
|
|
161 |
d->nodeState = Operational;
|
|
162 |
newState = Operational;
|
|
163 |
switchCommunicationState(d, &newCommunicationState);
|
|
164 |
(*d->operational)();
|
|
165 |
}
|
|
166 |
break;
|
|
167 |
|
|
168 |
case Stopped:
|
|
169 |
if(d->nodeState == Initialisation) return 0xFF;
|
|
170 |
{
|
|
171 |
s_state_communication newCommunicationState = {
|
|
172 |
csBoot_Up: 0,
|
|
173 |
csSDO: 0,
|
|
174 |
csEmergency: 0,
|
|
175 |
csSYNC: 0,
|
|
176 |
csHeartbeat: 1,
|
|
177 |
csPDO: 0};
|
|
178 |
d->nodeState = Stopped;
|
|
179 |
newState = Stopped;
|
|
180 |
switchCommunicationState(d, &newCommunicationState);
|
|
181 |
(*d->stopped)();
|
|
182 |
}
|
|
183 |
break;
|
|
184 |
|
|
185 |
default:
|
|
186 |
return 0xFF;
|
|
187 |
}//end switch case
|
|
188 |
|
|
189 |
led_set_state(d, newState);
|
|
190 |
}
|
|
191 |
return 0;
|
|
192 |
}
|
|
193 |
|
53
|
194 |
/*****************************************************************************/
|
0
|
195 |
UNS8 getNodeId(CO_Data* d)
|
|
196 |
{
|
|
197 |
return *d->bDeviceNodeId;
|
|
198 |
}
|
|
199 |
|
53
|
200 |
/*****************************************************************************/
|
0
|
201 |
void setNodeId(CO_Data* d, UNS8 nodeId)
|
|
202 |
{
|
|
203 |
UNS16 offset = d->firstIndex->SDO_SVR;
|
|
204 |
if(offset){
|
|
205 |
//cob_id_client = 0x600 + nodeId;
|
|
206 |
*(UNS32*)d->objdict[offset].pSubindex[1].pObject = 0x600 + nodeId;
|
|
207 |
//cob_id_server = 0x580 + nodeId;
|
|
208 |
*(UNS32*)d->objdict[offset].pSubindex[2].pObject = 0x580 + nodeId;
|
|
209 |
// node Id client. As we do not know the value, we put the node Id Server
|
|
210 |
//*(UNS8*)d->objdict[offset].pSubindex[3].pObject = nodeId;
|
|
211 |
}
|
|
212 |
|
|
213 |
// ** Initialize the server(s) SDO parameters
|
|
214 |
// Remember that only one SDO server is allowed, defined at index 0x1200
|
|
215 |
|
|
216 |
// ** Initialize the client(s) SDO parameters
|
|
217 |
// Nothing to initialize (no default values required by the DS 401)
|
|
218 |
// ** Initialize the receive PDO communication parameters. Only for 0x1400 to 0x1403
|
|
219 |
{
|
|
220 |
UNS8 i = 0;
|
|
221 |
UNS16 offset = d->firstIndex->PDO_RCV;
|
|
222 |
UNS16 lastIndex = d->lastIndex->PDO_RCV;
|
|
223 |
UNS32 cobID[] = {0x200, 0x300, 0x400, 0x500};
|
|
224 |
if( offset ) while( (offset <= lastIndex) && (i < 4)) {
|
|
225 |
if(*(UNS32*)d->objdict[offset].pSubindex[1].pObject == cobID[i] + *d->bDeviceNodeId)
|
|
226 |
*(UNS32*)d->objdict[offset].pSubindex[1].pObject = cobID[i] + nodeId;
|
|
227 |
i ++;
|
|
228 |
offset ++;
|
|
229 |
}
|
|
230 |
}
|
|
231 |
// ** Initialize the transmit PDO communication parameters. Only for 0x1800 to 0x1803
|
|
232 |
{
|
|
233 |
UNS8 i = 0;
|
|
234 |
UNS16 offset = d->firstIndex->PDO_TRS;
|
|
235 |
UNS16 lastIndex = d->lastIndex->PDO_TRS;
|
|
236 |
UNS32 cobID[] = {0x180, 0x280, 0x380, 0x480};
|
|
237 |
i = 0;
|
|
238 |
if( offset ) while ((offset <= lastIndex) && (i < 4)) {
|
|
239 |
if(*(UNS32*)d->objdict[offset].pSubindex[1].pObject == cobID[i] + *d->bDeviceNodeId)
|
|
240 |
*(UNS32*)d->objdict[offset].pSubindex[1].pObject = cobID[i] + nodeId;
|
|
241 |
i ++;
|
|
242 |
offset ++;
|
|
243 |
}
|
|
244 |
}
|
|
245 |
// bDeviceNodeId is defined in the object dictionary.
|
|
246 |
*d->bDeviceNodeId = nodeId;
|
|
247 |
}
|