Newer
Older
Florian Pose
committed
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
/******************************************************************************
*
* $Id$
*
* Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH
*
* This file is part of the IgH EtherCAT Master.
*
* The IgH EtherCAT Master is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* The IgH EtherCAT Master 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the IgH EtherCAT Master; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* The right to use EtherCAT Technology is granted and comes free of
* charge under condition of compatibility of product made by
* Licensee. People intending to distribute/sell products based on the
* code, have to sign an agreement to guarantee that products using
* software based on IgH EtherCAT master stay compatible with the actual
* EtherCAT specification (which are released themselves as an open
* standard) as the (only) precondition to have the right to use EtherCAT
* Technology, IP and trade marks.
*
*****************************************************************************/
/**
\file
EtherCAT slave state machines.
*/
/*****************************************************************************/
#include "globals.h"
#include "master.h"
#include "mailbox.h"
#include "fsm_slave.h"
#include "fsm_mapping.h"
Florian Pose
committed
/*****************************************************************************/
void ec_fsm_slave_scan_state_start(ec_fsm_slave_t *);
void ec_fsm_slave_scan_state_address(ec_fsm_slave_t *);
void ec_fsm_slave_scan_state_state(ec_fsm_slave_t *);
void ec_fsm_slave_scan_state_base(ec_fsm_slave_t *);
void ec_fsm_slave_scan_state_datalink(ec_fsm_slave_t *);
void ec_fsm_slave_scan_state_eeprom_size(ec_fsm_slave_t *);
void ec_fsm_slave_scan_state_eeprom_data(ec_fsm_slave_t *);
void ec_fsm_slave_conf_state_start(ec_fsm_slave_t *);
void ec_fsm_slave_conf_state_init(ec_fsm_slave_t *);
void ec_fsm_slave_conf_state_clear_fmmus(ec_fsm_slave_t *);
void ec_fsm_slave_conf_state_mbox_sync(ec_fsm_slave_t *);
Florian Pose
committed
void ec_fsm_slave_conf_state_preop(ec_fsm_slave_t *);
void ec_fsm_slave_conf_state_pdo_sync(ec_fsm_slave_t *);
Florian Pose
committed
void ec_fsm_slave_conf_state_fmmu(ec_fsm_slave_t *);
void ec_fsm_slave_conf_state_sdoconf(ec_fsm_slave_t *);
void ec_fsm_slave_conf_state_mapconf(ec_fsm_slave_t *);
Florian Pose
committed
void ec_fsm_slave_conf_state_saveop(ec_fsm_slave_t *);
void ec_fsm_slave_conf_state_op(ec_fsm_slave_t *);
void ec_fsm_slave_conf_enter_mbox_sync(ec_fsm_slave_t *);
Florian Pose
committed
void ec_fsm_slave_conf_enter_preop(ec_fsm_slave_t *);
void ec_fsm_slave_conf_enter_pdo_sync(ec_fsm_slave_t *);
Florian Pose
committed
void ec_fsm_slave_conf_enter_fmmu(ec_fsm_slave_t *);
void ec_fsm_slave_conf_enter_sdoconf(ec_fsm_slave_t *);
void ec_fsm_slave_conf_enter_mapconf(ec_fsm_slave_t *);
Florian Pose
committed
void ec_fsm_slave_conf_enter_saveop(ec_fsm_slave_t *);
void ec_fsm_slave_state_end(ec_fsm_slave_t *);
void ec_fsm_slave_state_error(ec_fsm_slave_t *);
/*****************************************************************************/
/**
Constructor.
*/
void ec_fsm_slave_init(ec_fsm_slave_t *fsm, /**< slave state machine */
ec_datagram_t *datagram /**< datagram structure to use */
)
{
fsm->datagram = datagram;
// init sub state machines
ec_fsm_sii_init(&fsm->fsm_sii, fsm->datagram);
ec_fsm_change_init(&fsm->fsm_change, fsm->datagram);
ec_fsm_coe_init(&fsm->fsm_coe, fsm->datagram);
ec_fsm_mapping_init(&fsm->fsm_map, &fsm->fsm_coe);
Florian Pose
committed
}
/*****************************************************************************/
/**
Destructor.
*/
void ec_fsm_slave_clear(ec_fsm_slave_t *fsm /**< slave state machine */)
{
// clear sub state machines
ec_fsm_sii_clear(&fsm->fsm_sii);
ec_fsm_change_clear(&fsm->fsm_change);
ec_fsm_coe_clear(&fsm->fsm_coe);
ec_fsm_mapping_clear(&fsm->fsm_map);
Florian Pose
committed
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
}
/*****************************************************************************/
/**
* Start slave scan state machine.
*/
void ec_fsm_slave_start_scan(ec_fsm_slave_t *fsm, /**< slave state machine */
ec_slave_t *slave /**< slave to configure */
)
{
fsm->slave = slave;
fsm->state = ec_fsm_slave_scan_state_start;
}
/*****************************************************************************/
/**
* Start slave configuration state machine.
*/
void ec_fsm_slave_start_conf(ec_fsm_slave_t *fsm, /**< slave state machine */
ec_slave_t *slave /**< slave to configure */
)
{
fsm->slave = slave;
fsm->state = ec_fsm_slave_conf_state_start;
}
/*****************************************************************************/
/**
\return false, if state machine has terminated
*/
int ec_fsm_slave_running(const ec_fsm_slave_t *fsm /**< slave state machine */)
{
return fsm->state != ec_fsm_slave_state_end
&& fsm->state != ec_fsm_slave_state_error;
}
/*****************************************************************************/
/**
Executes the current state of the state machine.
If the state machine's datagram is not sent or received yet, the execution
of the state machine is delayed to the next cycle.
\return false, if state machine has terminated
*/
int ec_fsm_slave_exec(ec_fsm_slave_t *fsm /**< slave state machine */)
{
if (fsm->datagram->state == EC_DATAGRAM_SENT
|| fsm->datagram->state == EC_DATAGRAM_QUEUED) {
// datagram was not sent or received yet.
return ec_fsm_slave_running(fsm);
}
fsm->state(fsm);
return ec_fsm_slave_running(fsm);
}
/*****************************************************************************/
/**
\return true, if the state machine terminated gracefully
*/
int ec_fsm_slave_success(const ec_fsm_slave_t *fsm /**< slave state machine */)
{
return fsm->state == ec_fsm_slave_state_end;
}
/******************************************************************************
* slave scan state machine
*****************************************************************************/
/**
Slave scan state: START.
First state of the slave state machine. Writes the station address to the
slave, according to its ring position.
*/
void ec_fsm_slave_scan_state_start(ec_fsm_slave_t *fsm /**< slave state machine */)
{
// write station address
ec_datagram_apwr(fsm->datagram, fsm->slave->ring_position, 0x0010, 2);
EC_WRITE_U16(fsm->datagram->data, fsm->slave->station_address);
fsm->retries = EC_FSM_RETRIES;
fsm->state = ec_fsm_slave_scan_state_address;
}
/*****************************************************************************/
/**
Slave scan state: ADDRESS.
*/
void ec_fsm_slave_scan_state_address(ec_fsm_slave_t *fsm /**< slave state machine */)
{
ec_datagram_t *datagram = fsm->datagram;
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
Florian Pose
committed
return;
if (datagram->state != EC_DATAGRAM_RECEIVED) {
fsm->state = ec_fsm_slave_state_error;
EC_ERR("Failed to receive station address datagram for slave %i"
" (datagram state %i)\n",
fsm->slave->ring_position, datagram->state);
Florian Pose
committed
return;
}
if (datagram->working_counter != 1) {
fsm->slave->error_flag = 1;
fsm->state = ec_fsm_slave_state_error;
EC_ERR("Failed to write station address on slave %i: ",
Florian Pose
committed
fsm->slave->ring_position);
ec_datagram_print_wc_error(datagram);
Florian Pose
committed
return;
}
// Read AL state
ec_datagram_nprd(datagram, fsm->slave->station_address, 0x0130, 2);
fsm->retries = EC_FSM_RETRIES;
fsm->state = ec_fsm_slave_scan_state_state;
}
/*****************************************************************************/
/**
Slave scan state: STATE.
*/
void ec_fsm_slave_scan_state_state(
ec_fsm_slave_t *fsm /**< slave state machine */
)
Florian Pose
committed
{
ec_datagram_t *datagram = fsm->datagram;
ec_slave_t *slave = fsm->slave;
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
Florian Pose
committed
return;
if (datagram->state != EC_DATAGRAM_RECEIVED) {
fsm->state = ec_fsm_slave_state_error;
EC_ERR("Failed to receive AL state datagram from slave %i"
" (datagram state %i).\n",
fsm->slave->ring_position, datagram->state);
Florian Pose
committed
return;
}
if (datagram->working_counter != 1) {
fsm->slave->error_flag = 1;
fsm->state = ec_fsm_slave_state_error;
EC_ERR("Failed to read AL state of slave %i: ",
Florian Pose
committed
fsm->slave->ring_position);
ec_datagram_print_wc_error(datagram);
Florian Pose
committed
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
return;
}
slave->current_state = EC_READ_U8(datagram->data);
if (slave->current_state & EC_SLAVE_STATE_ACK_ERR) {
char state_str[EC_STATE_STRING_SIZE];
ec_state_string(slave->current_state, state_str);
EC_WARN("Slave %i has state error bit set (%s)!\n",
slave->ring_position, state_str);
}
// read base data
ec_datagram_nprd(datagram, fsm->slave->station_address, 0x0000, 6);
fsm->retries = EC_FSM_RETRIES;
fsm->state = ec_fsm_slave_scan_state_base;
}
/*****************************************************************************/
/**
Slave scan state: BASE.
*/
void ec_fsm_slave_scan_state_base(ec_fsm_slave_t *fsm /**< slave state machine */)
{
ec_datagram_t *datagram = fsm->datagram;
ec_slave_t *slave = fsm->slave;
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
Florian Pose
committed
return;
if (datagram->state != EC_DATAGRAM_RECEIVED) {
fsm->state = ec_fsm_slave_state_error;
EC_ERR("Failed to receive base data datagram for slave %i"
" (datagram state %i).\n",
slave->ring_position, datagram->state);
Florian Pose
committed
return;
}
if (datagram->working_counter != 1) {
fsm->slave->error_flag = 1;
fsm->state = ec_fsm_slave_state_error;
EC_ERR("Failed to read base data from slave %i: ",
Florian Pose
committed
slave->ring_position);
ec_datagram_print_wc_error(datagram);
Florian Pose
committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
return;
}
slave->base_type = EC_READ_U8 (datagram->data);
slave->base_revision = EC_READ_U8 (datagram->data + 1);
slave->base_build = EC_READ_U16(datagram->data + 2);
slave->base_fmmu_count = EC_READ_U8 (datagram->data + 4);
if (slave->base_fmmu_count > EC_MAX_FMMUS)
slave->base_fmmu_count = EC_MAX_FMMUS;
// read data link status
ec_datagram_nprd(datagram, slave->station_address, 0x0110, 2);
fsm->retries = EC_FSM_RETRIES;
fsm->state = ec_fsm_slave_scan_state_datalink;
}
/*****************************************************************************/
/**
Slave scan state: DATALINK.
*/
void ec_fsm_slave_scan_state_datalink(ec_fsm_slave_t *fsm /**< slave state machine */)
{
ec_datagram_t *datagram = fsm->datagram;
ec_slave_t *slave = fsm->slave;
uint16_t dl_status;
unsigned int i;
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
Florian Pose
committed
return;
if (datagram->state != EC_DATAGRAM_RECEIVED) {
fsm->state = ec_fsm_slave_state_error;
EC_ERR("Failed to receive DL status datagram from slave %i"
" (datagram state %i).\n",
slave->ring_position, datagram->state);
Florian Pose
committed
return;
}
if (datagram->working_counter != 1) {
fsm->slave->error_flag = 1;
fsm->state = ec_fsm_slave_state_error;
EC_ERR("Failed to read DL status from slave %i: ",
Florian Pose
committed
slave->ring_position);
ec_datagram_print_wc_error(datagram);
Florian Pose
committed
return;
}
dl_status = EC_READ_U16(datagram->data);
for (i = 0; i < 4; i++) {
slave->dl_link[i] = dl_status & (1 << (4 + i)) ? 1 : 0;
slave->dl_loop[i] = dl_status & (1 << (8 + i * 2)) ? 1 : 0;
slave->dl_signal[i] = dl_status & (1 << (9 + i * 2)) ? 1 : 0;
}
// Start fetching EEPROM size
fsm->sii_offset = EC_FIRST_EEPROM_CATEGORY_OFFSET; // first category header
Florian Pose
committed
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
ec_fsm_sii_read(&fsm->fsm_sii, slave, fsm->sii_offset, EC_FSM_SII_NODE);
fsm->state = ec_fsm_slave_scan_state_eeprom_size;
fsm->state(fsm); // execute state immediately
}
/*****************************************************************************/
/**
Slave scan state: EEPROM SIZE.
*/
void ec_fsm_slave_scan_state_eeprom_size(ec_fsm_slave_t *fsm /**< slave state machine */)
{
ec_slave_t *slave = fsm->slave;
uint16_t cat_type, cat_size;
if (ec_fsm_sii_exec(&fsm->fsm_sii)) return;
if (!ec_fsm_sii_success(&fsm->fsm_sii)) {
fsm->slave->error_flag = 1;
fsm->state = ec_fsm_slave_state_error;
EC_ERR("Failed to read EEPROM size of slave %i.\n",
slave->ring_position);
return;
}
cat_type = EC_READ_U16(fsm->fsm_sii.value);
cat_size = EC_READ_U16(fsm->fsm_sii.value + 2);
if (cat_type != 0xFFFF) { // not the last category
fsm->sii_offset += cat_size + 2;
if (fsm->sii_offset >= EC_MAX_EEPROM_SIZE) {
EC_WARN("EEPROM size of slave %i exceeds"
" %i words (0xffff limiter missing?).\n",
slave->ring_position, EC_MAX_EEPROM_SIZE);
slave->eeprom_size = EC_FIRST_EEPROM_CATEGORY_OFFSET * 2;
goto alloc_eeprom;
}
Florian Pose
committed
ec_fsm_sii_read(&fsm->fsm_sii, slave, fsm->sii_offset,
EC_FSM_SII_NODE);
ec_fsm_sii_exec(&fsm->fsm_sii); // execute state immediately
return;
}
slave->eeprom_size = (fsm->sii_offset + 1) * 2;
alloc_eeprom:
Florian Pose
committed
if (slave->eeprom_data) {
EC_INFO("Freeing old EEPROM data on slave %i...\n",
slave->ring_position);
kfree(slave->eeprom_data);
}
if (!(slave->eeprom_data =
(uint8_t *) kmalloc(slave->eeprom_size, GFP_ATOMIC))) {
slave->eeprom_size = 0;
slave->error_flag = 1;
Florian Pose
committed
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
fsm->state = ec_fsm_slave_state_error;
EC_ERR("Failed to allocate EEPROM data on slave %i.\n",
slave->ring_position);
return;
}
// Start fetching EEPROM contents
fsm->state = ec_fsm_slave_scan_state_eeprom_data;
fsm->sii_offset = 0x0000;
ec_fsm_sii_read(&fsm->fsm_sii, slave, fsm->sii_offset, EC_FSM_SII_NODE);
ec_fsm_sii_exec(&fsm->fsm_sii); // execute state immediately
}
/*****************************************************************************/
/**
Slave scan state: EEPROM DATA.
*/
void ec_fsm_slave_scan_state_eeprom_data(ec_fsm_slave_t *fsm /**< slave state machine */)
{
ec_slave_t *slave = fsm->slave;
uint16_t *cat_word, cat_type, cat_size;
if (ec_fsm_sii_exec(&fsm->fsm_sii)) return;
if (!ec_fsm_sii_success(&fsm->fsm_sii)) {
fsm->slave->error_flag = 1;
fsm->state = ec_fsm_slave_state_error;
EC_ERR("Failed to fetch EEPROM contents of slave %i.\n",
slave->ring_position);
return;
}
// 2 words fetched
if (fsm->sii_offset + 2 <= slave->eeprom_size / 2) { // 2 words fit
memcpy(slave->eeprom_data + fsm->sii_offset * 2,
fsm->fsm_sii.value, 4);
}
else { // copy the last word
memcpy(slave->eeprom_data + fsm->sii_offset * 2,
fsm->fsm_sii.value, 2);
}
if (fsm->sii_offset + 2 < slave->eeprom_size / 2) {
// fetch the next 2 words
fsm->sii_offset += 2;
ec_fsm_sii_read(&fsm->fsm_sii, slave, fsm->sii_offset,
EC_FSM_SII_NODE);
ec_fsm_sii_exec(&fsm->fsm_sii); // execute state immediately
return;
}
// Evaluate EEPROM contents
slave->sii_alias =
EC_READ_U16(slave->eeprom_data + 2 * 0x0004);
slave->sii_vendor_id =
EC_READ_U32(slave->eeprom_data + 2 * 0x0008);
slave->sii_product_code =
EC_READ_U32(slave->eeprom_data + 2 * 0x000A);
slave->sii_revision_number =
EC_READ_U32(slave->eeprom_data + 2 * 0x000C);
slave->sii_serial_number =
EC_READ_U32(slave->eeprom_data + 2 * 0x000E);
slave->sii_rx_mailbox_offset =
EC_READ_U16(slave->eeprom_data + 2 * 0x0018);
slave->sii_rx_mailbox_size =
EC_READ_U16(slave->eeprom_data + 2 * 0x0019);
slave->sii_tx_mailbox_offset =
EC_READ_U16(slave->eeprom_data + 2 * 0x001A);
slave->sii_tx_mailbox_size =
EC_READ_U16(slave->eeprom_data + 2 * 0x001B);
slave->sii_mailbox_protocols =
EC_READ_U16(slave->eeprom_data + 2 * 0x001C);
// evaluate category data
cat_word = (uint16_t *) slave->eeprom_data + EC_FIRST_EEPROM_CATEGORY_OFFSET;
Florian Pose
committed
while (EC_READ_U16(cat_word) != 0xFFFF) {
cat_type = EC_READ_U16(cat_word) & 0x7FFF;
cat_size = EC_READ_U16(cat_word + 1);
switch (cat_type) {
case 0x000A:
if (ec_slave_fetch_sii_strings(
slave, (uint8_t *) (cat_word + 2)))
Florian Pose
committed
goto end;
break;
case 0x001E:
ec_slave_fetch_sii_general(
slave, (uint8_t *) (cat_word + 2));
Florian Pose
committed
break;
case 0x0028:
break;
case 0x0029:
if (ec_slave_fetch_sii_syncs(
slave, (uint8_t *) (cat_word + 2), cat_size))
Florian Pose
committed
goto end;
break;
case 0x0032:
if (ec_slave_fetch_sii_pdos(
slave, (uint8_t *) (cat_word + 2),
cat_size, EC_TX_PDO))
Florian Pose
committed
goto end;
break;
case 0x0033:
if (ec_slave_fetch_sii_pdos(
slave, (uint8_t *) (cat_word + 2),
cat_size, EC_RX_PDO))
Florian Pose
committed
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
goto end;
break;
default:
if (fsm->slave->master->debug_level)
EC_WARN("Unknown category type 0x%04X in slave %i.\n",
cat_type, slave->ring_position);
}
cat_word += cat_size + 2;
}
fsm->state = ec_fsm_slave_state_end;
return;
end:
EC_ERR("Failed to analyze category data.\n");
fsm->slave->error_flag = 1;
fsm->state = ec_fsm_slave_state_error;
}
/******************************************************************************
* slave configuration state machine
*****************************************************************************/
/**
Slave configuration state: START.
*/
void ec_fsm_slave_conf_state_start(ec_fsm_slave_t *fsm /**< slave state machine */)
{
if (fsm->slave->master->debug_level) {
EC_DBG("Configuring slave %i...\n", fsm->slave->ring_position);
}
ec_fsm_change_start(&fsm->fsm_change, fsm->slave, EC_SLAVE_STATE_INIT);
ec_fsm_change_exec(&fsm->fsm_change);
fsm->state = ec_fsm_slave_conf_state_init;
}
/*****************************************************************************/
/**
Slave configuration state: INIT.
*/
void ec_fsm_slave_conf_state_init(ec_fsm_slave_t *fsm /**< slave state machine */)
{
ec_master_t *master = fsm->slave->master;
ec_slave_t *slave = fsm->slave;
ec_datagram_t *datagram = fsm->datagram;
if (ec_fsm_change_exec(&fsm->fsm_change)) return;
if (!ec_fsm_change_success(&fsm->fsm_change)) {
slave->error_flag = 1;
fsm->state = ec_fsm_slave_state_error;
return;
}
slave->self_configured = 1;
if (master->debug_level) {
EC_DBG("Slave %i is now in INIT.\n", slave->ring_position);
}
// check and reset CRC fault counters
//ec_slave_check_crc(slave);
// TODO: Implement state machine for CRC checking.
if (!slave->base_fmmu_count) { // skip FMMU configuration
ec_fsm_slave_conf_enter_mbox_sync(fsm);
Florian Pose
committed
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
return;
}
if (master->debug_level)
EC_DBG("Clearing FMMU configurations of slave %i...\n",
slave->ring_position);
// clear FMMU configurations
ec_datagram_npwr(datagram, slave->station_address,
0x0600, EC_FMMU_SIZE * slave->base_fmmu_count);
memset(datagram->data, 0x00, EC_FMMU_SIZE * slave->base_fmmu_count);
fsm->retries = EC_FSM_RETRIES;
fsm->state = ec_fsm_slave_conf_state_clear_fmmus;
}
/*****************************************************************************/
/**
Slave configuration state: CLEAR FMMU.
*/
void ec_fsm_slave_conf_state_clear_fmmus(ec_fsm_slave_t *fsm
/**< slave state machine */)
{
ec_datagram_t *datagram = fsm->datagram;
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
Florian Pose
committed
return;
if (datagram->state != EC_DATAGRAM_RECEIVED) {
fsm->state = ec_fsm_slave_state_error;
EC_ERR("Failed receive FMMU clearing datagram for slave %i.\n",
fsm->slave->ring_position);
return;
}
if (datagram->working_counter != 1) {
fsm->slave->error_flag = 1;
fsm->state = ec_fsm_slave_state_error;
EC_ERR("Failed to clear FMMUs of slave %i: ",
Florian Pose
committed
fsm->slave->ring_position);
ec_datagram_print_wc_error(datagram);
Florian Pose
committed
return;
}
ec_fsm_slave_conf_enter_mbox_sync(fsm);
Florian Pose
committed
}
/*****************************************************************************/
/**
Florian Pose
committed
void ec_fsm_slave_conf_enter_mbox_sync(
ec_fsm_slave_t *fsm /**< slave state machine */
)
Florian Pose
committed
{
ec_master_t *master = fsm->slave->master;
ec_slave_t *slave = fsm->slave;
ec_datagram_t *datagram = fsm->datagram;
unsigned int i;
Florian Pose
committed
// slave is now in INIT
if (slave->current_state == slave->requested_state) {
fsm->state = ec_fsm_slave_state_end; // successful
if (master->debug_level) {
EC_DBG("Finished configuration of slave %i.\n",
slave->ring_position);
}
return;
}
if (!slave->sii_mailbox_protocols) {
// no mailbox protocols supported
if (master->debug_level)
EC_DBG("Slave %i does not support mailbox communication.\n",
slave->ring_position);
Florian Pose
committed
ec_fsm_slave_conf_enter_preop(fsm);
return;
}
if (master->debug_level) {
EC_DBG("Configuring mailbox sync managers of slave %i.\n",
Florian Pose
committed
slave->ring_position);
}
if (slave->sii_sync_count >= 2) {
// configure sync managers
ec_datagram_npwr(datagram, slave->station_address, 0x0800,
EC_SYNC_SIZE * slave->sii_sync_count);
memset(datagram->data, 0x00, EC_SYNC_SIZE * slave->sii_sync_count);
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
for (i = 0; i < 2; i++) {
ec_sync_config(&slave->sii_syncs[i],
datagram->data + EC_SYNC_SIZE * i);
}
} else { // no mailbox sync manager configurations provided
ec_sync_t sync;
if (master->debug_level)
EC_DBG("Slave %i does not provide"
" mailbox sync manager configurations.\n",
slave->ring_position);
ec_datagram_npwr(datagram, slave->station_address, 0x0800,
EC_SYNC_SIZE * 2);
memset(datagram->data, 0x00, EC_SYNC_SIZE * 2);
ec_sync_init(&sync, slave, 0);
sync.physical_start_address = slave->sii_rx_mailbox_offset;
sync.length = slave->sii_rx_mailbox_size;
sync.control_register = 0x26;
sync.enable = 1;
ec_sync_config(&sync, datagram->data + EC_SYNC_SIZE * sync.index);
ec_sync_init(&sync, slave, 1);
sync.physical_start_address = slave->sii_tx_mailbox_offset;
sync.length = slave->sii_tx_mailbox_size;
sync.control_register = 0x22;
sync.enable = 1;
ec_sync_config(&sync, datagram->data + EC_SYNC_SIZE * sync.index);
Florian Pose
committed
}
fsm->retries = EC_FSM_RETRIES;
fsm->state = ec_fsm_slave_conf_state_mbox_sync;
Florian Pose
committed
}
/*****************************************************************************/
/**
Slave configuration state: SYNC.
*/
void ec_fsm_slave_conf_state_mbox_sync(ec_fsm_slave_t *fsm /**< slave state machine */)
Florian Pose
committed
{
ec_datagram_t *datagram = fsm->datagram;
ec_slave_t *slave = fsm->slave;
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
Florian Pose
committed
return;
if (datagram->state != EC_DATAGRAM_RECEIVED) {
fsm->state = ec_fsm_slave_state_error;
EC_ERR("Failed to receive sync manager configuration datagram for"
" slave %i (datagram state %i).\n",
slave->ring_position, datagram->state);
Florian Pose
committed
return;
}
if (datagram->working_counter != 1) {
slave->error_flag = 1;
fsm->state = ec_fsm_slave_state_error;
EC_ERR("Failed to set sync managers of slave %i: ",
Florian Pose
committed
slave->ring_position);
ec_datagram_print_wc_error(datagram);
Florian Pose
committed
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
return;
}
ec_fsm_slave_conf_enter_preop(fsm);
}
/*****************************************************************************/
/**
*/
void ec_fsm_slave_conf_enter_preop(ec_fsm_slave_t *fsm /**< slave state machine */)
{
fsm->state = ec_fsm_slave_conf_state_preop;
ec_fsm_change_start(&fsm->fsm_change, fsm->slave, EC_SLAVE_STATE_PREOP);
ec_fsm_change_exec(&fsm->fsm_change); // execute immediately
}
/*****************************************************************************/
/**
Slave configuration state: PREOP.
*/
void ec_fsm_slave_conf_state_preop(ec_fsm_slave_t *fsm /**< slave state machine */)
{
ec_slave_t *slave = fsm->slave;
ec_master_t *master = fsm->slave->master;
if (ec_fsm_change_exec(&fsm->fsm_change)) return;
if (!ec_fsm_change_success(&fsm->fsm_change)) {
slave->error_flag = 1;
fsm->state = ec_fsm_slave_state_error;
return;
}
// slave is now in PREOP
slave->jiffies_preop = fsm->datagram->jiffies_received;
if (master->debug_level) {
EC_DBG("Slave %i is now in PREOP.\n", slave->ring_position);
}
if (slave->current_state == slave->requested_state) {
fsm->state = ec_fsm_slave_state_end; // successful
if (master->debug_level) {
EC_DBG("Finished configuration of slave %i.\n",
slave->ring_position);
}
return;
}
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
ec_fsm_slave_conf_enter_sdoconf(fsm);
}
/*****************************************************************************/
/**
*/
void ec_fsm_slave_conf_enter_sdoconf(ec_fsm_slave_t *fsm /**< slave state machine */)
{
ec_slave_t *slave = fsm->slave;
// No CoE configuration to be applied?
if (list_empty(&slave->sdo_confs)) { // skip SDO configuration
ec_fsm_slave_conf_enter_mapconf(fsm);
return;
}
// start SDO configuration
fsm->state = ec_fsm_slave_conf_state_sdoconf;
fsm->sdodata = list_entry(fsm->slave->sdo_confs.next, ec_sdo_data_t, list);
ec_fsm_coe_download(&fsm->fsm_coe, fsm->slave, fsm->sdodata);
ec_fsm_coe_exec(&fsm->fsm_coe); // execute immediately
}
/*****************************************************************************/
/**
Slave configuration state: SDOCONF.
*/
void ec_fsm_slave_conf_state_sdoconf(
ec_fsm_slave_t *fsm /**< slave state machine */
)
{
if (ec_fsm_coe_exec(&fsm->fsm_coe)) return;
if (!ec_fsm_coe_success(&fsm->fsm_coe)) {
EC_ERR("SDO configuration failed for slave %u.\n",
fsm->slave->ring_position);
fsm->slave->error_flag = 1;
fsm->state = ec_fsm_slave_state_error;
return;
}
// Another SDO to configure?
if (fsm->sdodata->list.next != &fsm->slave->sdo_confs) {
fsm->sdodata = list_entry(fsm->sdodata->list.next,
ec_sdo_data_t, list);
ec_fsm_coe_download(&fsm->fsm_coe, fsm->slave, fsm->sdodata);
ec_fsm_coe_exec(&fsm->fsm_coe); // execute immediately
return;
}
// All SDOs are now configured.
ec_fsm_slave_conf_enter_mapconf(fsm);
}
/*****************************************************************************/
/**
*/
void ec_fsm_slave_conf_enter_mapconf(
ec_fsm_slave_t *fsm /**< slave state machine */
)
{
ec_slave_t *slave = fsm->slave;
if (!(slave->sii_mailbox_protocols & EC_MBOX_COE)) {
// Slave does not support CoE: no configuration of PDO mapping.
ec_fsm_slave_conf_enter_pdo_sync(fsm);
return;
}
// start configuring PDO mapping
fsm->state = ec_fsm_slave_conf_state_mapconf;
ec_fsm_mapping_start(&fsm->fsm_map, fsm->slave);
ec_fsm_mapping_exec(&fsm->fsm_map); // execute immediately
}
/*****************************************************************************/
/**
Slave configuration state: MAPCONF.
*/
void ec_fsm_slave_conf_state_mapconf(
ec_fsm_slave_t *fsm /**< slave state machine */
)
{
if (ec_fsm_mapping_exec(&fsm->fsm_map)) return;
if (!ec_fsm_mapping_success(&fsm->fsm_map)) {
EC_ERR("PDO mapping configuration failed for slave %u.\n",
fsm->slave->ring_position);
fsm->slave->error_flag = 1;
fsm->state = ec_fsm_slave_state_error;
return;
}
ec_fsm_slave_conf_enter_pdo_sync(fsm);
Florian Pose
committed
}
/*****************************************************************************/
/**
Florian Pose
committed
void ec_fsm_slave_conf_enter_pdo_sync(
ec_fsm_slave_t *fsm /**< slave state machine */
)
Florian Pose
committed
{
ec_slave_t *slave = fsm->slave;
ec_datagram_t *datagram = fsm->datagram;
unsigned int i;
Florian Pose
committed
if (!slave->sii_sync_count) {
Florian Pose
committed
ec_fsm_slave_conf_enter_fmmu(fsm);
return;
}
// configure sync managers for process data
ec_datagram_npwr(datagram, slave->station_address, 0x0800,
EC_SYNC_SIZE * slave->sii_sync_count);
memset(datagram->data, 0x00, EC_SYNC_SIZE * slave->sii_sync_count);
Florian Pose
committed
for (i = 0; i < slave->sii_sync_count; i++) {
ec_sync_config(&slave->sii_syncs[i],
datagram->data + EC_SYNC_SIZE * i);
Florian Pose
committed
}
fsm->retries = EC_FSM_RETRIES;
fsm->state = ec_fsm_slave_conf_state_pdo_sync;
Florian Pose
committed
}
/*****************************************************************************/
/**
Florian Pose
committed
void ec_fsm_slave_conf_state_pdo_sync(ec_fsm_slave_t *fsm /**< slave state machine */)
Florian Pose
committed
{
ec_datagram_t *datagram = fsm->datagram;
ec_slave_t *slave = fsm->slave;
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
Florian Pose
committed
return;
if (datagram->state != EC_DATAGRAM_RECEIVED) {
fsm->state = ec_fsm_slave_state_error;
EC_ERR("Failed to receive process data sync manager configuration"
" datagram for slave %i (datagram state %i).\n",
slave->ring_position, datagram->state);
Florian Pose
committed
return;
}
if (datagram->working_counter != 1) {
slave->error_flag = 1;
fsm->state = ec_fsm_slave_state_error;
EC_ERR("Failed to set process data sync managers of slave %i: ",
slave->ring_position);
ec_datagram_print_wc_error(datagram);
Florian Pose
committed
return;
}
ec_fsm_slave_conf_enter_fmmu(fsm);
}
/*****************************************************************************/
/**
*/
void ec_fsm_slave_conf_enter_fmmu(ec_fsm_slave_t *fsm /**< slave state machine */)
{
ec_slave_t *slave = fsm->slave;
ec_datagram_t *datagram = fsm->datagram;