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
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
/******************************************************************************
*
* $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 finite state machines.
*/
/*****************************************************************************/
#include "globals.h"
#include "master.h"
#include "mailbox.h"
#include "fsm_master.h"
/*****************************************************************************/
void ec_fsm_master_state_start(ec_fsm_master_t *);
void ec_fsm_master_state_broadcast(ec_fsm_master_t *);
void ec_fsm_master_state_read_states(ec_fsm_master_t *);
void ec_fsm_master_state_acknowledge(ec_fsm_master_t *);
void ec_fsm_master_state_validate_vendor(ec_fsm_master_t *);
void ec_fsm_master_state_validate_product(ec_fsm_master_t *);
void ec_fsm_master_state_rewrite_addresses(ec_fsm_master_t *);
void ec_fsm_master_state_configure_slave(ec_fsm_master_t *);
void ec_fsm_master_state_scan_slaves(ec_fsm_master_t *);
void ec_fsm_master_state_write_eeprom(ec_fsm_master_t *);
void ec_fsm_master_state_sdodict(ec_fsm_master_t *);
void ec_fsm_master_state_sdo_request(ec_fsm_master_t *);
void ec_fsm_master_state_end(ec_fsm_master_t *);
void ec_fsm_master_state_error(ec_fsm_master_t *);
/*****************************************************************************/
/**
Constructor.
*/
void ec_fsm_master_init(ec_fsm_master_t *fsm, /**< master state machine */
ec_master_t *master, /**< EtherCAT master */
ec_datagram_t *datagram /**< datagram object to use */
)
{
fsm->master = master;
fsm->datagram = datagram;
fsm->state = ec_fsm_master_state_start;
fsm->slaves_responding = 0;
fsm->topology_change_pending = 0;
Florian Pose
committed
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
fsm->slave_states = EC_SLAVE_STATE_UNKNOWN;
fsm->validate = 0;
// init sub-state-machines
ec_fsm_slave_init(&fsm->fsm_slave, fsm->datagram);
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);
}
/*****************************************************************************/
/**
Destructor.
*/
void ec_fsm_master_clear(ec_fsm_master_t *fsm /**< master state machine */)
{
// clear sub-state machines
ec_fsm_slave_clear(&fsm->fsm_slave);
ec_fsm_sii_clear(&fsm->fsm_sii);
ec_fsm_change_clear(&fsm->fsm_change);
ec_fsm_coe_clear(&fsm->fsm_coe);
}
/*****************************************************************************/
/**
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_master_exec(ec_fsm_master_t *fsm /**< master 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_master_running(fsm);
}
fsm->state(fsm);
return ec_fsm_master_running(fsm);
}
/*****************************************************************************/
/**
\return false, if state machine has terminated
*/
int ec_fsm_master_running(ec_fsm_master_t *fsm /**< master state machine */)
{
return fsm->state != ec_fsm_master_state_end
&& fsm->state != ec_fsm_master_state_error;
}
/*****************************************************************************/
/**
\return true, if the master state machine terminated gracefully
*/
int ec_fsm_master_success(ec_fsm_master_t *fsm /**< master state machine */)
{
return fsm->state == ec_fsm_master_state_end;
}
/******************************************************************************
* operation/idle state machine
*****************************************************************************/
/**
Master state: START.
Starts with getting slave count and slave states.
*/
void ec_fsm_master_state_start(ec_fsm_master_t *fsm)
{
ec_datagram_brd(fsm->datagram, 0x0130, 2);
ec_master_queue_datagram(fsm->master, fsm->datagram);
fsm->state = ec_fsm_master_state_broadcast;
}
/*****************************************************************************/
/**
Master state: BROADCAST.
Processes the broadcast read slave count and slaves states.
*/
void ec_fsm_master_state_broadcast(ec_fsm_master_t *fsm /**< master state machine */)
{
ec_datagram_t *datagram = fsm->datagram;
unsigned int i;
Florian Pose
committed
ec_slave_t *slave;
ec_master_t *master = fsm->master;
if (datagram->state == EC_DATAGRAM_TIMED_OUT) {
// always retry
ec_master_queue_datagram(fsm->master, fsm->datagram);
return;
}
if (datagram->state != EC_DATAGRAM_RECEIVED) { // EC_DATAGRAM_ERROR
// link is down
fsm->slaves_responding = 0;
list_for_each_entry(slave, &master->slaves, list) {
slave->online = 0;
}
fsm->state = ec_fsm_master_state_error;
return;
}
// bus topology change?
if (datagram->working_counter != fsm->slaves_responding) {
fsm->topology_change_pending = 1;
fsm->slaves_responding = datagram->working_counter;
Florian Pose
committed
EC_INFO("%i slave%s responding.\n",
fsm->slaves_responding,
fsm->slaves_responding == 1 ? "" : "s");
if (master->mode == EC_MASTER_MODE_OPERATION) {
if (fsm->slaves_responding == master->slave_count) {
fsm->validate = 1; // start validation later
}
else {
EC_WARN("Invalid slave count. Bus in tainted state.\n");
}
}
}
// slave states changed?
if (EC_READ_U8(datagram->data) != fsm->slave_states) {
Florian Pose
committed
char states[EC_STATE_STRING_SIZE];
fsm->slave_states = EC_READ_U8(datagram->data);
Florian Pose
committed
ec_state_string(fsm->slave_states, states);
EC_INFO("Slave states: %s.\n", states);
}
// topology change in idle mode: clear all slaves and scan the bus
if (fsm->topology_change_pending &&
master->mode == EC_MASTER_MODE_IDLE) {
fsm->topology_change_pending = 0;
Florian Pose
committed
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
ec_master_eoe_stop(master);
ec_master_destroy_slaves(master);
master->slave_count = datagram->working_counter;
if (!master->slave_count) {
// no slaves present -> finish state machine.
fsm->state = ec_fsm_master_state_end;
return;
}
// init slaves
for (i = 0; i < master->slave_count; i++) {
if (!(slave = (ec_slave_t *) kmalloc(sizeof(ec_slave_t),
GFP_ATOMIC))) {
EC_ERR("Failed to allocate slave %i!\n", i);
ec_master_destroy_slaves(master);
fsm->state = ec_fsm_master_state_error;
return;
}
if (ec_slave_init(slave, master, i, i + 1)) {
// freeing of "slave" already done
ec_master_destroy_slaves(master);
fsm->state = ec_fsm_master_state_error;
return;
}
list_add_tail(&slave->list, &master->slaves);
}
EC_INFO("Scanning bus.\n");
// begin scanning of slaves
fsm->slave = list_entry(master->slaves.next, ec_slave_t, list);
ec_fsm_slave_start_scan(&fsm->fsm_slave, fsm->slave);
Florian Pose
committed
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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
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
ec_fsm_slave_exec(&fsm->fsm_slave); // execute immediately
fsm->state = ec_fsm_master_state_scan_slaves;
return;
}
// fetch state from each slave
fsm->slave = list_entry(master->slaves.next, ec_slave_t, list);
ec_datagram_nprd(fsm->datagram, fsm->slave->station_address, 0x0130, 2);
ec_master_queue_datagram(master, fsm->datagram);
fsm->retries = EC_FSM_RETRIES;
fsm->state = ec_fsm_master_state_read_states;
}
/*****************************************************************************/
/**
Master action: PROC_STATES.
Processes the slave states.
*/
void ec_fsm_master_action_process_states(ec_fsm_master_t *fsm
/**< master state machine */
)
{
ec_master_t *master = fsm->master;
ec_slave_t *slave;
char old_state[EC_STATE_STRING_SIZE], new_state[EC_STATE_STRING_SIZE];
// check if any slaves are not in the state, they're supposed to be
list_for_each_entry(slave, &master->slaves, list) {
if (slave->error_flag
|| !slave->online
|| slave->requested_state == EC_SLAVE_STATE_UNKNOWN
|| (slave->current_state == slave->requested_state
&& slave->self_configured)) continue;
if (master->debug_level) {
ec_state_string(slave->current_state, old_state);
if (slave->current_state != slave->requested_state) {
ec_state_string(slave->requested_state, new_state);
EC_DBG("Changing state of slave %i (%s -> %s).\n",
slave->ring_position, old_state, new_state);
}
else if (!slave->self_configured) {
EC_DBG("Reconfiguring slave %i (%s).\n",
slave->ring_position, old_state);
}
}
fsm->slave = slave;
ec_fsm_slave_start_conf(&fsm->fsm_slave, slave);
ec_fsm_slave_exec(&fsm->fsm_slave); // execute immediately
fsm->state = ec_fsm_master_state_configure_slave;
return;
}
// Check, if EoE processing has to be started
ec_master_eoe_start(master);
if (master->mode == EC_MASTER_MODE_IDLE) {
// Check for a pending SDO request
if (master->sdo_seq_master != master->sdo_seq_user) {
if (master->debug_level)
EC_DBG("Processing SDO request...\n");
slave = master->sdo_request->sdo->slave;
if (slave->current_state == EC_SLAVE_STATE_INIT
|| !slave->online) {
EC_ERR("Failed to process SDO request, slave %i not ready.\n",
slave->ring_position);
master->sdo_request->return_code = -1;
master->sdo_seq_master++;
}
else {
// start uploading SDO
fsm->slave = slave;
fsm->state = ec_fsm_master_state_sdo_request;
fsm->sdo_request = master->sdo_request;
ec_fsm_coe_upload(&fsm->fsm_coe, slave, fsm->sdo_request);
ec_fsm_coe_exec(&fsm->fsm_coe); // execute immediately
return;
}
}
// check, if slaves have an SDO dictionary to read out.
list_for_each_entry(slave, &master->slaves, list) {
if (!(slave->sii_mailbox_protocols & EC_MBOX_COE)
|| slave->sdo_dictionary_fetched
|| slave->current_state == EC_SLAVE_STATE_INIT
|| jiffies - slave->jiffies_preop < EC_WAIT_SDO_DICT * HZ
|| !slave->online
|| slave->error_flag) continue;
if (master->debug_level) {
EC_DBG("Fetching SDO dictionary from slave %i.\n",
slave->ring_position);
}
slave->sdo_dictionary_fetched = 1;
// start fetching SDO dictionary
fsm->slave = slave;
fsm->state = ec_fsm_master_state_sdodict;
ec_fsm_coe_dictionary(&fsm->fsm_coe, slave);
ec_fsm_coe_exec(&fsm->fsm_coe); // execute immediately
return;
}
// check for pending EEPROM write operations.
list_for_each_entry(slave, &master->slaves, list) {
if (!slave->new_eeprom_data) continue;
if (!slave->online || slave->error_flag) {
kfree(slave->new_eeprom_data);
slave->new_eeprom_data = NULL;
EC_ERR("Discarding EEPROM data, slave %i not ready.\n",
slave->ring_position);
continue;
}
// found pending EEPROM write operation. execute it!
EC_INFO("Writing EEPROM of slave %i...\n", slave->ring_position);
fsm->slave = slave;
fsm->sii_offset = 0x0000;
ec_fsm_sii_write(&fsm->fsm_sii, slave, fsm->sii_offset,
slave->new_eeprom_data, EC_FSM_SII_NODE);
fsm->state = ec_fsm_master_state_write_eeprom;
fsm->state(fsm); // execute immediately
return;
}
}
fsm->state = ec_fsm_master_state_end;
}
/*****************************************************************************/
/**
Master action: Get state of next slave.
*/
void ec_fsm_master_action_next_slave_state(ec_fsm_master_t *fsm
/**< master state machine */)
{
ec_master_t *master = fsm->master;
ec_slave_t *slave = fsm->slave;
// is there another slave to query?
if (slave->list.next != &master->slaves) {
// process next slave
fsm->slave = list_entry(fsm->slave->list.next, ec_slave_t, list);
ec_datagram_nprd(fsm->datagram, fsm->slave->station_address,
0x0130, 2);
ec_master_queue_datagram(master, fsm->datagram);
fsm->retries = EC_FSM_RETRIES;
fsm->state = ec_fsm_master_state_read_states;
return;
}
// all slave states read
// check, if a bus validation has to be done
if (fsm->validate) {
fsm->validate = 0;
list_for_each_entry(slave, &master->slaves, list) {
if (slave->online) continue;
// At least one slave is offline. validate!
EC_INFO("Validating bus.\n");
fsm->slave = list_entry(master->slaves.next, ec_slave_t, list);
fsm->state = ec_fsm_master_state_validate_vendor;
ec_fsm_sii_read(&fsm->fsm_sii, slave, 0x0008, EC_FSM_SII_POSITION);
ec_fsm_sii_exec(&fsm->fsm_sii); // execute immediately
return;
}
}
ec_fsm_master_action_process_states(fsm);
}
/*****************************************************************************/
/**
Master state: READ STATES.
Fetches the AL- and online state of a slave.
*/
void ec_fsm_master_state_read_states(ec_fsm_master_t *fsm /**< master state machine */)
{
ec_slave_t *slave = fsm->slave;
ec_datagram_t *datagram = fsm->datagram;
uint8_t new_state;
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
ec_master_queue_datagram(fsm->master, fsm->datagram);
return;
}
if (datagram->state != EC_DATAGRAM_RECEIVED) {
EC_ERR("Failed to receive AL state datagram for slave %i"
" (datagram state %i)\n", slave->ring_position, datagram->state);
Florian Pose
committed
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
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
614
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
fsm->state = ec_fsm_master_state_error;
return;
}
// did the slave not respond to its station address?
if (datagram->working_counter != 1) {
if (slave->online) {
slave->online = 0;
if (slave->master->debug_level)
EC_DBG("Slave %i: offline.\n", slave->ring_position);
}
ec_fsm_master_action_next_slave_state(fsm);
return;
}
// slave responded
new_state = EC_READ_U8(datagram->data);
if (!slave->online) { // slave was offline before
slave->online = 1;
slave->error_flag = 0; // clear error flag
slave->current_state = new_state;
if (slave->master->debug_level) {
char cur_state[EC_STATE_STRING_SIZE];
ec_state_string(slave->current_state, cur_state);
EC_DBG("Slave %i: online (%s).\n",
slave->ring_position, cur_state);
}
}
else if (new_state != slave->current_state) {
if (slave->master->debug_level) {
char old_state[EC_STATE_STRING_SIZE],
cur_state[EC_STATE_STRING_SIZE];
ec_state_string(slave->current_state, old_state);
ec_state_string(new_state, cur_state);
EC_DBG("Slave %i: %s -> %s.\n",
slave->ring_position, old_state, cur_state);
}
slave->current_state = new_state;
}
// check, if new slave state has to be acknowledged
if (slave->current_state & EC_SLAVE_STATE_ACK_ERR && !slave->error_flag) {
ec_fsm_change_ack(&fsm->fsm_change, slave);
ec_fsm_change_exec(&fsm->fsm_change);
fsm->state = ec_fsm_master_state_acknowledge;
return;
}
ec_fsm_master_action_next_slave_state(fsm);
}
/*****************************************************************************/
/**
Master state: ACKNOWLEDGE
*/
void ec_fsm_master_state_acknowledge(ec_fsm_master_t *fsm /**< master state machine */)
{
ec_slave_t *slave = fsm->slave;
if (ec_fsm_change_exec(&fsm->fsm_change)) return;
if (!ec_fsm_change_success(&fsm->fsm_change)) {
fsm->slave->error_flag = 1;
EC_ERR("Failed to acknowledge state change on slave %i.\n",
slave->ring_position);
fsm->state = ec_fsm_master_state_error;
return;
}
ec_fsm_master_action_next_slave_state(fsm);
}
/*****************************************************************************/
/**
Master state: VALIDATE_VENDOR.
Validates the vendor ID of a slave.
*/
void ec_fsm_master_state_validate_vendor(ec_fsm_master_t *fsm /**< master state machine */)
{
ec_slave_t *slave = fsm->slave;
if (ec_fsm_sii_exec(&fsm->fsm_sii)) return;
if (!ec_fsm_sii_success(&fsm->fsm_sii)) {
fsm->slave->error_flag = 1;
EC_ERR("Failed to validate vendor ID of slave %i.\n",
slave->ring_position);
fsm->state = ec_fsm_master_state_error;
return;
}
if (EC_READ_U32(fsm->fsm_sii.value) != slave->sii_vendor_id) {
EC_ERR("Slave %i has an invalid vendor ID!\n", slave->ring_position);
fsm->state = ec_fsm_master_state_error;
return;
}
// vendor ID is ok. check product code.
fsm->state = ec_fsm_master_state_validate_product;
ec_fsm_sii_read(&fsm->fsm_sii, slave, 0x000A, EC_FSM_SII_POSITION);
ec_fsm_sii_exec(&fsm->fsm_sii); // execute immediately
}
/*****************************************************************************/
/**
Master action: ADDRESS.
Looks for slave, that have lost their configuration and writes
their station address, so that they can be reconfigured later.
*/
void ec_fsm_master_action_addresses(ec_fsm_master_t *fsm /**< master state machine */)
{
ec_datagram_t *datagram = fsm->datagram;
while (fsm->slave->online) {
if (fsm->slave->list.next == &fsm->master->slaves) { // last slave?
fsm->state = ec_fsm_master_state_start;
fsm->state(fsm); // execute immediately
return;
}
// check next slave
fsm->slave = list_entry(fsm->slave->list.next, ec_slave_t, list);
}
if (fsm->master->debug_level)
EC_DBG("Reinitializing slave %i.\n", fsm->slave->ring_position);
// write station address
ec_datagram_apwr(datagram, fsm->slave->ring_position, 0x0010, 2);
EC_WRITE_U16(datagram->data, fsm->slave->station_address);
ec_master_queue_datagram(fsm->master, datagram);
fsm->retries = EC_FSM_RETRIES;
fsm->state = ec_fsm_master_state_rewrite_addresses;
}
/*****************************************************************************/
/**
Master state: VALIDATE_PRODUCT.
Validates the product ID of a slave.
*/
void ec_fsm_master_state_validate_product(ec_fsm_master_t *fsm /**< master state machine */)
{
ec_slave_t *slave = fsm->slave;
if (ec_fsm_sii_exec(&fsm->fsm_sii)) return;
if (!ec_fsm_sii_success(&fsm->fsm_sii)) {
fsm->slave->error_flag = 1;
EC_ERR("Failed to validate product code of slave %i.\n",
slave->ring_position);
fsm->state = ec_fsm_master_state_error;
return;
}
if (EC_READ_U32(fsm->fsm_sii.value) != slave->sii_product_code) {
EC_ERR("Slave %i: invalid product code!\n", slave->ring_position);
EC_ERR("expected 0x%08X, got 0x%08X.\n", slave->sii_product_code,
EC_READ_U32(fsm->fsm_sii.value));
fsm->state = ec_fsm_master_state_error;
return;
}
// have all states been validated?
if (slave->list.next == &fsm->master->slaves) {
fsm->slave = list_entry(fsm->master->slaves.next, ec_slave_t, list);
// start writing addresses to offline slaves
ec_fsm_master_action_addresses(fsm);
return;
}
// validate next slave
fsm->slave = list_entry(fsm->slave->list.next, ec_slave_t, list);
fsm->state = ec_fsm_master_state_validate_vendor;
ec_fsm_sii_read(&fsm->fsm_sii, slave, 0x0008, EC_FSM_SII_POSITION);
ec_fsm_sii_exec(&fsm->fsm_sii); // execute immediately
}
/*****************************************************************************/
/**
Master state: REWRITE ADDRESS.
Checks, if the new station address has been written to the slave.
*/
void ec_fsm_master_state_rewrite_addresses(ec_fsm_master_t *fsm
/**< master state machine */
)
{
ec_slave_t *slave = fsm->slave;
ec_datagram_t *datagram = fsm->datagram;
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
ec_master_queue_datagram(fsm->master, fsm->datagram);
return;
}
if (datagram->state != EC_DATAGRAM_RECEIVED) {
EC_ERR("Failed to receive address datagram for slave %i"
" (datagram state %i).\n",
slave->ring_position, datagram->state);
Florian Pose
committed
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
fsm->state = ec_fsm_master_state_error;
return;
}
if (datagram->working_counter != 1) {
EC_ERR("Failed to write station address - slave %i did not respond.\n",
slave->ring_position);
fsm->state = ec_fsm_master_state_error;
return;
}
if (fsm->slave->list.next == &fsm->master->slaves) { // last slave?
fsm->state = ec_fsm_master_state_start;
fsm->state(fsm); // execute immediately
return;
}
// check next slave
fsm->slave = list_entry(fsm->slave->list.next, ec_slave_t, list);
// Write new station address to slave
ec_fsm_master_action_addresses(fsm);
}
/*****************************************************************************/
/**
Master state: SCAN SLAVES.
Executes the sub-statemachine for the scanning of a slave.
*/
void ec_fsm_master_state_scan_slaves(ec_fsm_master_t *fsm /**< master state machine */)
{
ec_master_t *master = fsm->master;
ec_slave_t *slave;
Florian Pose
committed
if (ec_fsm_slave_exec(&fsm->fsm_slave)) // execute slave state machine
return;
// another slave to fetch?
if (fsm->slave->list.next != &master->slaves) {
Florian Pose
committed
fsm->slave = list_entry(fsm->slave->list.next, ec_slave_t, list);
ec_fsm_slave_start_scan(&fsm->fsm_slave, fsm->slave);
Florian Pose
committed
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
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
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
ec_fsm_slave_exec(&fsm->fsm_slave); // execute immediately
return;
}
EC_INFO("Bus scanning completed.\n");
ec_master_calc_addressing(master);
// set initial states of all slaves to PREOP to make mailbox
// communication possible
list_for_each_entry(slave, &master->slaves, list) {
ec_slave_request_state(slave, EC_SLAVE_STATE_PREOP);
}
fsm->state = ec_fsm_master_state_end;
}
/*****************************************************************************/
/**
Master state: CONFIGURE SLAVES.
Starts configuring a slave.
*/
void ec_fsm_master_state_configure_slave(ec_fsm_master_t *fsm
/**< master state machine */
)
{
if (ec_fsm_slave_exec(&fsm->fsm_slave)) // execute slave's state machine
return;
ec_fsm_master_action_process_states(fsm);
}
/*****************************************************************************/
/**
Master state: WRITE EEPROM.
*/
void ec_fsm_master_state_write_eeprom(ec_fsm_master_t *fsm /**< master state machine */)
{
ec_slave_t *slave = fsm->slave;
if (ec_fsm_sii_exec(&fsm->fsm_sii)) return;
if (!ec_fsm_sii_success(&fsm->fsm_sii)) {
fsm->slave->error_flag = 1;
EC_ERR("Failed to write EEPROM contents to slave %i.\n",
slave->ring_position);
kfree(slave->new_eeprom_data);
slave->new_eeprom_data = NULL;
fsm->state = ec_fsm_master_state_error;
return;
}
fsm->sii_offset++;
if (fsm->sii_offset < slave->new_eeprom_size) {
ec_fsm_sii_write(&fsm->fsm_sii, slave, fsm->sii_offset,
slave->new_eeprom_data + fsm->sii_offset,
EC_FSM_SII_NODE);
ec_fsm_sii_exec(&fsm->fsm_sii); // execute immediately
return;
}
// finished writing EEPROM
EC_INFO("Finished writing EEPROM of slave %i.\n", slave->ring_position);
kfree(slave->new_eeprom_data);
slave->new_eeprom_data = NULL;
// TODO: Evaluate new EEPROM contents!
// restart master state machine.
fsm->state = ec_fsm_master_state_start;
fsm->state(fsm); // execute immediately
}
/*****************************************************************************/
/**
Master state: SDODICT.
*/
void ec_fsm_master_state_sdodict(ec_fsm_master_t *fsm /**< master state machine */)
{
ec_slave_t *slave = fsm->slave;
ec_master_t *master = fsm->master;
if (ec_fsm_coe_exec(&fsm->fsm_coe)) return;
if (!ec_fsm_coe_success(&fsm->fsm_coe)) {
fsm->state = ec_fsm_master_state_error;
return;
}
// SDO dictionary fetching finished
if (master->debug_level) {
unsigned int sdo_count, entry_count;
ec_slave_sdo_dict_info(slave, &sdo_count, &entry_count);
EC_DBG("Fetched %i SDOs and %i entries from slave %i.\n",
sdo_count, entry_count, slave->ring_position);
}
// restart master state machine.
fsm->state = ec_fsm_master_state_start;
fsm->state(fsm); // execute immediately
}
/*****************************************************************************/
/**
Master state: SDO REQUEST.
*/
void ec_fsm_master_state_sdo_request(ec_fsm_master_t *fsm /**< master state machine */)
{
ec_master_t *master = fsm->master;
ec_sdo_request_t *request = fsm->sdo_request;
if (ec_fsm_coe_exec(&fsm->fsm_coe)) return;
if (!ec_fsm_coe_success(&fsm->fsm_coe)) {
request->return_code = -1;
master->sdo_seq_master++;
fsm->state = ec_fsm_master_state_error;
return;
}
// SDO dictionary fetching finished
request->return_code = 1;
master->sdo_seq_master++;
// restart master state machine.
fsm->state = ec_fsm_master_state_start;
fsm->state(fsm); // execute immediately
}
/*****************************************************************************/
/**
State: ERROR.
*/
void ec_fsm_master_state_error(
ec_fsm_master_t *fsm /**< master state machine */
)
{
fsm->state = ec_fsm_master_state_start;
}
/*****************************************************************************/
/**
State: END.
*/
void ec_fsm_master_state_end(ec_fsm_master_t *fsm /**< master state machine */)
{
fsm->state = ec_fsm_master_state_start;
}
/*****************************************************************************/