author | Florian Pose <fp@igh-essen.com> |
Fri, 29 Feb 2008 08:27:17 +0000 | |
changeset 846 | 19248bbf9308 |
parent 835 | 23fd8b510803 |
child 854 | f4f53be425ac |
permissions | -rw-r--r-- |
799 | 1 |
/****************************************************************************** |
2 |
* |
|
3 |
* $Id$ |
|
4 |
* |
|
5 |
* Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH |
|
6 |
* |
|
7 |
* This file is part of the IgH EtherCAT Master. |
|
8 |
* |
|
9 |
* The IgH EtherCAT Master is free software; you can redistribute it |
|
10 |
* and/or modify it under the terms of the GNU General Public License |
|
11 |
* as published by the Free Software Foundation; either version 2 of the |
|
12 |
* License, or (at your option) any later version. |
|
13 |
* |
|
14 |
* The IgH EtherCAT Master is distributed in the hope that it will be |
|
15 |
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 |
* GNU General Public License for more details. |
|
18 |
* |
|
19 |
* You should have received a copy of the GNU General Public License |
|
20 |
* along with the IgH EtherCAT Master; if not, write to the Free Software |
|
21 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
22 |
* |
|
23 |
* The right to use EtherCAT Technology is granted and comes free of |
|
24 |
* charge under condition of compatibility of product made by |
|
25 |
* Licensee. People intending to distribute/sell products based on the |
|
26 |
* code, have to sign an agreement to guarantee that products using |
|
27 |
* software based on IgH EtherCAT master stay compatible with the actual |
|
28 |
* EtherCAT specification (which are released themselves as an open |
|
29 |
* standard) as the (only) precondition to have the right to use EtherCAT |
|
30 |
* Technology, IP and trade marks. |
|
31 |
* |
|
32 |
*****************************************************************************/ |
|
33 |
||
34 |
/** \file |
|
35 |
* EtherCAT Pdo configuration state machine. |
|
36 |
*/ |
|
37 |
||
38 |
/*****************************************************************************/ |
|
39 |
||
40 |
#include "globals.h" |
|
41 |
#include "master.h" |
|
42 |
#include "mailbox.h" |
|
43 |
#include "slave_config.h" |
|
44 |
||
45 |
#include "fsm_pdo_config.h" |
|
46 |
||
47 |
/*****************************************************************************/ |
|
48 |
||
49 |
void ec_fsm_pdo_config_state_start(ec_fsm_pdo_config_t *); |
|
50 |
void ec_fsm_pdo_config_state_zero_count(ec_fsm_pdo_config_t *); |
|
51 |
void ec_fsm_pdo_config_state_add_entry(ec_fsm_pdo_config_t *); |
|
52 |
void ec_fsm_pdo_config_state_entry_count(ec_fsm_pdo_config_t *); |
|
53 |
void ec_fsm_pdo_config_state_end(ec_fsm_pdo_config_t *); |
|
54 |
void ec_fsm_pdo_config_state_error(ec_fsm_pdo_config_t *); |
|
55 |
||
56 |
void ec_fsm_pdo_config_next_pdo(ec_fsm_pdo_config_t *); |
|
57 |
||
58 |
/*****************************************************************************/ |
|
59 |
||
60 |
/** Constructor. |
|
61 |
*/ |
|
62 |
void ec_fsm_pdo_config_init( |
|
63 |
ec_fsm_pdo_config_t *fsm, /**< pdo_config state machine */ |
|
64 |
ec_fsm_coe_t *fsm_coe /**< CoE state machine to use */ |
|
65 |
) |
|
66 |
{ |
|
67 |
fsm->fsm_coe = fsm_coe; |
|
68 |
fsm->sdodata.data = (uint8_t *) &fsm->sdo_value; |
|
69 |
} |
|
70 |
||
71 |
/*****************************************************************************/ |
|
72 |
||
73 |
/** Destructor. |
|
74 |
*/ |
|
75 |
void ec_fsm_pdo_config_clear( |
|
76 |
ec_fsm_pdo_config_t *fsm /**< pdo_config state machine */ |
|
77 |
) |
|
78 |
{ |
|
79 |
} |
|
80 |
||
81 |
/*****************************************************************************/ |
|
82 |
||
83 |
/** Start Pdo configuration state machine. |
|
84 |
*/ |
|
85 |
void ec_fsm_pdo_config_start( |
|
86 |
ec_fsm_pdo_config_t *fsm, /**< Pdo configuration state machine */ |
|
87 |
ec_slave_t *slave /**< slave to configure */ |
|
88 |
) |
|
89 |
{ |
|
90 |
fsm->slave = slave; |
|
91 |
fsm->state = ec_fsm_pdo_config_state_start; |
|
92 |
} |
|
93 |
||
94 |
/*****************************************************************************/ |
|
95 |
||
96 |
/** Get running state. |
|
97 |
* |
|
98 |
* \return false, if state machine has terminated |
|
99 |
*/ |
|
100 |
int ec_fsm_pdo_config_running( |
|
101 |
const ec_fsm_pdo_config_t *fsm /**< Pdo configuration state machine */ |
|
102 |
) |
|
103 |
{ |
|
104 |
return fsm->state != ec_fsm_pdo_config_state_end |
|
105 |
&& fsm->state != ec_fsm_pdo_config_state_error; |
|
106 |
} |
|
107 |
||
108 |
/*****************************************************************************/ |
|
109 |
||
110 |
/** Executes the current state. |
|
111 |
* |
|
112 |
* \return false, if state machine has terminated |
|
113 |
*/ |
|
114 |
int ec_fsm_pdo_config_exec( |
|
115 |
ec_fsm_pdo_config_t *fsm /**< Pdo configuration state machine */ |
|
116 |
) |
|
117 |
{ |
|
118 |
fsm->state(fsm); |
|
119 |
return ec_fsm_pdo_config_running(fsm); |
|
120 |
} |
|
121 |
||
122 |
/*****************************************************************************/ |
|
123 |
||
124 |
/** Get execution result. |
|
125 |
* |
|
126 |
* \return true, if the state machine terminated gracefully |
|
127 |
*/ |
|
128 |
int ec_fsm_pdo_config_success( |
|
129 |
const ec_fsm_pdo_config_t *fsm /**< Pdo configuration state machine */ |
|
130 |
) |
|
131 |
{ |
|
132 |
return fsm->state == ec_fsm_pdo_config_state_end; |
|
133 |
} |
|
134 |
||
135 |
/****************************************************************************** |
|
136 |
* State functions. |
|
137 |
*****************************************************************************/ |
|
138 |
||
139 |
/** Start Pdo configuration. |
|
140 |
*/ |
|
141 |
void ec_fsm_pdo_config_state_start( |
|
142 |
ec_fsm_pdo_config_t *fsm /**< Pdo configuration state machine */ |
|
143 |
) |
|
144 |
{ |
|
145 |
if (!fsm->slave->config) { |
|
146 |
fsm->state = ec_fsm_pdo_config_state_end; |
|
147 |
return; |
|
148 |
} |
|
149 |
||
150 |
fsm->pdo = NULL; |
|
151 |
ec_fsm_pdo_config_next_pdo(fsm); |
|
152 |
} |
|
153 |
||
154 |
/*****************************************************************************/ |
|
155 |
||
156 |
/** Process configuration of next Pdo. |
|
157 |
*/ |
|
158 |
void ec_fsm_pdo_config_next_pdo( |
|
159 |
ec_fsm_pdo_config_t *fsm /**< Pdo configuration state machine */ |
|
160 |
) |
|
161 |
{ |
|
162 |
ec_direction_t dir; |
|
163 |
const ec_pdo_mapping_t *map; |
|
164 |
const ec_pdo_t *pdo, *mapped_pdo; |
|
165 |
||
166 |
for (dir = EC_DIR_OUTPUT; dir <= EC_DIR_INPUT; dir++) { |
|
167 |
map = &fsm->slave->config->mapping[dir]; |
|
168 |
||
169 |
list_for_each_entry(pdo, &map->pdos, list) { |
|
801
6b1e2b0ed8bc
Cleaned up Pdo mapping and configuration state machines.
Florian Pose <fp@igh-essen.com>
parents:
800
diff
changeset
|
170 |
if (fsm->pdo) { // there was a Pdo configured in the last run |
6b1e2b0ed8bc
Cleaned up Pdo mapping and configuration state machines.
Florian Pose <fp@igh-essen.com>
parents:
800
diff
changeset
|
171 |
if (pdo == fsm->pdo) // this is the last Pdo |
6b1e2b0ed8bc
Cleaned up Pdo mapping and configuration state machines.
Florian Pose <fp@igh-essen.com>
parents:
800
diff
changeset
|
172 |
fsm->pdo = NULL; // take the next one |
799 | 173 |
} else { |
174 |
if ((mapped_pdo = ec_slave_find_pdo(fsm->slave, pdo->index))) |
|
175 |
if (ec_pdo_equal_entries(pdo, mapped_pdo)) |
|
176 |
continue; // Pdo configured correctly |
|
177 |
||
178 |
fsm->pdo = pdo; |
|
801
6b1e2b0ed8bc
Cleaned up Pdo mapping and configuration state machines.
Florian Pose <fp@igh-essen.com>
parents:
800
diff
changeset
|
179 |
break; |
799 | 180 |
} |
181 |
} |
|
182 |
} |
|
183 |
||
801
6b1e2b0ed8bc
Cleaned up Pdo mapping and configuration state machines.
Florian Pose <fp@igh-essen.com>
parents:
800
diff
changeset
|
184 |
if (!fsm->pdo) { |
6b1e2b0ed8bc
Cleaned up Pdo mapping and configuration state machines.
Florian Pose <fp@igh-essen.com>
parents:
800
diff
changeset
|
185 |
if (fsm->slave->master->debug_level) |
6b1e2b0ed8bc
Cleaned up Pdo mapping and configuration state machines.
Florian Pose <fp@igh-essen.com>
parents:
800
diff
changeset
|
186 |
EC_DBG("Pdo configuration finished for slave %u.\n", |
6b1e2b0ed8bc
Cleaned up Pdo mapping and configuration state machines.
Florian Pose <fp@igh-essen.com>
parents:
800
diff
changeset
|
187 |
fsm->slave->ring_position); |
6b1e2b0ed8bc
Cleaned up Pdo mapping and configuration state machines.
Florian Pose <fp@igh-essen.com>
parents:
800
diff
changeset
|
188 |
fsm->state = ec_fsm_pdo_config_state_end; |
6b1e2b0ed8bc
Cleaned up Pdo mapping and configuration state machines.
Florian Pose <fp@igh-essen.com>
parents:
800
diff
changeset
|
189 |
return; |
6b1e2b0ed8bc
Cleaned up Pdo mapping and configuration state machines.
Florian Pose <fp@igh-essen.com>
parents:
800
diff
changeset
|
190 |
} |
6b1e2b0ed8bc
Cleaned up Pdo mapping and configuration state machines.
Florian Pose <fp@igh-essen.com>
parents:
800
diff
changeset
|
191 |
|
835
23fd8b510803
Evaluate CoE details from general category; moved current consumption
Florian Pose <fp@igh-essen.com>
parents:
814
diff
changeset
|
192 |
// Pdo configuration has to be changed. Does the slave support this? |
23fd8b510803
Evaluate CoE details from general category; moved current consumption
Florian Pose <fp@igh-essen.com>
parents:
814
diff
changeset
|
193 |
if (fsm->slave->sii.mailbox_protocols & EC_MBOX_COE |
23fd8b510803
Evaluate CoE details from general category; moved current consumption
Florian Pose <fp@igh-essen.com>
parents:
814
diff
changeset
|
194 |
|| (fsm->slave->sii.has_general |
23fd8b510803
Evaluate CoE details from general category; moved current consumption
Florian Pose <fp@igh-essen.com>
parents:
814
diff
changeset
|
195 |
&& !fsm->slave->sii.coe_details.enable_pdo_configuration)) { |
23fd8b510803
Evaluate CoE details from general category; moved current consumption
Florian Pose <fp@igh-essen.com>
parents:
814
diff
changeset
|
196 |
EC_ERR("Slave %u does not support changing the Pdo configuration!\n", |
23fd8b510803
Evaluate CoE details from general category; moved current consumption
Florian Pose <fp@igh-essen.com>
parents:
814
diff
changeset
|
197 |
fsm->slave->ring_position); |
23fd8b510803
Evaluate CoE details from general category; moved current consumption
Florian Pose <fp@igh-essen.com>
parents:
814
diff
changeset
|
198 |
fsm->state = ec_fsm_pdo_config_state_error; |
23fd8b510803
Evaluate CoE details from general category; moved current consumption
Florian Pose <fp@igh-essen.com>
parents:
814
diff
changeset
|
199 |
return; |
23fd8b510803
Evaluate CoE details from general category; moved current consumption
Florian Pose <fp@igh-essen.com>
parents:
814
diff
changeset
|
200 |
} |
23fd8b510803
Evaluate CoE details from general category; moved current consumption
Florian Pose <fp@igh-essen.com>
parents:
814
diff
changeset
|
201 |
|
799 | 202 |
if (fsm->slave->master->debug_level) { |
203 |
EC_DBG("Changing configuration of Pdo 0x%04X of slave %u.\n", |
|
204 |
fsm->pdo->index, fsm->slave->ring_position); |
|
205 |
} |
|
206 |
||
814 | 207 |
// set mapped Pdo count to zero |
799 | 208 |
fsm->sdodata.index = fsm->pdo->index; |
209 |
fsm->sdodata.subindex = 0; // number of configured entries |
|
210 |
EC_WRITE_U8(&fsm->sdo_value, 0); |
|
211 |
fsm->sdodata.size = 1; |
|
212 |
if (fsm->slave->master->debug_level) |
|
213 |
EC_DBG("Setting entry count to zero for Pdo 0x%04X.\n", |
|
214 |
fsm->pdo->index); |
|
215 |
||
216 |
fsm->state = ec_fsm_pdo_config_state_zero_count; |
|
217 |
ec_fsm_coe_download(fsm->fsm_coe, fsm->slave, &fsm->sdodata); |
|
218 |
ec_fsm_coe_exec(fsm->fsm_coe); // execute immediately |
|
219 |
} |
|
220 |
||
221 |
/*****************************************************************************/ |
|
222 |
||
223 |
/** Process next Pdo entry. |
|
224 |
*/ |
|
225 |
ec_pdo_entry_t *ec_fsm_pdo_config_next_entry( |
|
226 |
const ec_fsm_pdo_config_t *fsm, /**< Pdo configuration state machine */ |
|
227 |
const struct list_head *list /**< current entry list item */ |
|
228 |
) |
|
229 |
{ |
|
230 |
list = list->next; |
|
231 |
if (list == &fsm->pdo->entries) |
|
232 |
return NULL; // no next entry |
|
233 |
return list_entry(list, ec_pdo_entry_t, list); |
|
234 |
} |
|
235 |
||
236 |
/*****************************************************************************/ |
|
237 |
||
238 |
/** Starts to add a Pdo entry. |
|
239 |
*/ |
|
240 |
void ec_fsm_pdo_config_add_entry( |
|
241 |
ec_fsm_pdo_config_t *fsm /**< Pdo configuration state machine. */ |
|
242 |
) |
|
243 |
{ |
|
244 |
uint32_t value; |
|
245 |
||
246 |
fsm->sdodata.subindex = fsm->entry_count; |
|
247 |
value = fsm->entry->index << 16 |
|
248 |
| fsm->entry->subindex << 8 | fsm->entry->bit_length; |
|
249 |
EC_WRITE_U32(&fsm->sdo_value, value); |
|
250 |
fsm->sdodata.size = 4; |
|
251 |
||
252 |
if (fsm->slave->master->debug_level) |
|
253 |
EC_DBG("Configuring Pdo entry %08X at position %u.\n", |
|
254 |
value, fsm->entry_count); |
|
255 |
||
256 |
fsm->state = ec_fsm_pdo_config_state_add_entry; |
|
257 |
ec_fsm_coe_download(fsm->fsm_coe, fsm->slave, &fsm->sdodata); |
|
258 |
ec_fsm_coe_exec(fsm->fsm_coe); // execute immediately |
|
259 |
} |
|
260 |
||
261 |
/*****************************************************************************/ |
|
262 |
||
263 |
/** Set the number of configured entries to zero. |
|
264 |
*/ |
|
265 |
void ec_fsm_pdo_config_state_zero_count( |
|
266 |
ec_fsm_pdo_config_t *fsm /**< Pdo configuration state machine */ |
|
267 |
) |
|
268 |
{ |
|
269 |
if (ec_fsm_coe_exec(fsm->fsm_coe)) return; |
|
270 |
||
271 |
if (!ec_fsm_coe_success(fsm->fsm_coe)) { |
|
272 |
EC_ERR("Failed to clear Pdo configuration for slave %u.\n", |
|
273 |
fsm->slave->ring_position); |
|
274 |
fsm->state = ec_fsm_pdo_config_state_error; |
|
275 |
return; |
|
276 |
} |
|
277 |
||
278 |
// find first entry |
|
279 |
if (!(fsm->entry = |
|
280 |
ec_fsm_pdo_config_next_entry(fsm, &fsm->pdo->entries))) { |
|
281 |
if (fsm->slave->master->debug_level) |
|
282 |
EC_DBG("No entries to configure for Pdo 0x%04X of slave %u.\n", |
|
283 |
fsm->pdo->index, fsm->slave->ring_position); |
|
284 |
ec_fsm_pdo_config_next_pdo(fsm); |
|
285 |
return; |
|
286 |
} |
|
287 |
||
288 |
// add first entry |
|
289 |
fsm->entry_count = 1; |
|
290 |
ec_fsm_pdo_config_add_entry(fsm); |
|
291 |
} |
|
292 |
||
293 |
/*****************************************************************************/ |
|
294 |
||
295 |
/** Add a Pdo entry. |
|
296 |
*/ |
|
297 |
void ec_fsm_pdo_config_state_add_entry( |
|
298 |
ec_fsm_pdo_config_t *fsm /**< Pdo configuration state machine */ |
|
299 |
) |
|
300 |
{ |
|
301 |
if (ec_fsm_coe_exec(fsm->fsm_coe)) return; |
|
302 |
||
303 |
if (!ec_fsm_coe_success(fsm->fsm_coe)) { |
|
304 |
EC_ERR("Failed to add entry 0x%04X:%u for slave %u.\n", |
|
305 |
fsm->entry->index, fsm->entry->subindex, |
|
306 |
fsm->slave->ring_position); |
|
307 |
fsm->state = ec_fsm_pdo_config_state_error; |
|
308 |
return; |
|
309 |
} |
|
310 |
||
311 |
// find next entry |
|
312 |
if (!(fsm->entry = ec_fsm_pdo_config_next_entry(fsm, &fsm->entry->list))) { |
|
313 |
// No more entries to add. Write entry count. |
|
314 |
fsm->sdodata.subindex = 0; |
|
315 |
EC_WRITE_U8(&fsm->sdo_value, fsm->entry_count); |
|
316 |
fsm->sdodata.size = 1; |
|
317 |
||
318 |
if (fsm->slave->master->debug_level) |
|
319 |
EC_DBG("Setting number of Pdo entries to %u.\n", |
|
320 |
fsm->entry_count); |
|
321 |
||
322 |
fsm->state = ec_fsm_pdo_config_state_entry_count; |
|
323 |
ec_fsm_coe_download(fsm->fsm_coe, fsm->slave, &fsm->sdodata); |
|
324 |
ec_fsm_coe_exec(fsm->fsm_coe); // execute immediately |
|
325 |
return; |
|
326 |
} |
|
327 |
||
328 |
// add next entry |
|
329 |
fsm->entry_count++; |
|
330 |
ec_fsm_pdo_config_add_entry(fsm); |
|
331 |
} |
|
332 |
||
333 |
/*****************************************************************************/ |
|
334 |
||
335 |
/** Set the number of entries. |
|
336 |
*/ |
|
337 |
void ec_fsm_pdo_config_state_entry_count( |
|
338 |
ec_fsm_pdo_config_t *fsm /**< Pdo configuration state machine */ |
|
339 |
) |
|
340 |
{ |
|
341 |
if (ec_fsm_coe_exec(fsm->fsm_coe)) return; |
|
342 |
||
343 |
if (!ec_fsm_coe_success(fsm->fsm_coe)) { |
|
344 |
EC_ERR("Failed to set number of entries for slave %u.\n", |
|
345 |
fsm->slave->ring_position); |
|
346 |
fsm->state = ec_fsm_pdo_config_state_error; |
|
347 |
return; |
|
348 |
} |
|
349 |
||
350 |
if (fsm->slave->master->debug_level) |
|
351 |
EC_DBG("Successfully configured Pdo 0x%04X on slave %u.\n", |
|
352 |
fsm->pdo->index, fsm->slave->ring_position); |
|
353 |
||
354 |
ec_fsm_pdo_config_next_pdo(fsm); |
|
355 |
} |
|
356 |
||
357 |
/****************************************************************************** |
|
358 |
* Common state functions |
|
359 |
*****************************************************************************/ |
|
360 |
||
361 |
/** State: ERROR. |
|
362 |
*/ |
|
363 |
void ec_fsm_pdo_config_state_error( |
|
364 |
ec_fsm_pdo_config_t *fsm /**< Pdo configuration state machine */ |
|
365 |
) |
|
366 |
{ |
|
367 |
} |
|
368 |
||
369 |
/*****************************************************************************/ |
|
370 |
||
371 |
/** State: END. |
|
372 |
*/ |
|
373 |
void ec_fsm_pdo_config_state_end( |
|
374 |
ec_fsm_pdo_config_t *fsm /**< Pdo configuration state machine */ |
|
375 |
) |
|
376 |
{ |
|
377 |
} |
|
378 |
||
379 |
/*****************************************************************************/ |