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
78
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
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
215
216
217
218
219
220
221
222
223
224
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
261
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
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
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
667
668
669
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
703
704
705
706
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
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
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
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
|
/******************************************************************************
*
* $Id$
*
* Copyright (C) 2006-2008 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 version 2, as
* published by the Free Software Foundation.
*
* 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 license mentioned above concerns the source code only. Using the
* EtherCAT technology and brand is only permitted in compliance with the
* industrial property and similar rights of Beckhoff Automation GmbH.
*
*****************************************************************************/
/**
\file
EtherCAT slave state machines.
*/
/*****************************************************************************/
#include "globals.h"
#include "master.h"
#include "mailbox.h"
#include "slave_config.h"
#include "fsm_slave_scan.h"
/*****************************************************************************/
void ec_fsm_slave_scan_state_start(ec_fsm_slave_scan_t *);
void ec_fsm_slave_scan_state_address(ec_fsm_slave_scan_t *);
void ec_fsm_slave_scan_state_state(ec_fsm_slave_scan_t *);
void ec_fsm_slave_scan_state_base(ec_fsm_slave_scan_t *);
void ec_fsm_slave_scan_state_dc_cap(ec_fsm_slave_scan_t *);
void ec_fsm_slave_scan_state_dc_times(ec_fsm_slave_scan_t *);
void ec_fsm_slave_scan_state_datalink(ec_fsm_slave_scan_t *);
#ifdef EC_SII_ASSIGN
void ec_fsm_slave_scan_state_assign_sii(ec_fsm_slave_scan_t *);
#endif
void ec_fsm_slave_scan_state_sii_size(ec_fsm_slave_scan_t *);
void ec_fsm_slave_scan_state_sii_data(ec_fsm_slave_scan_t *);
#ifdef EC_REGALIAS
void ec_fsm_slave_scan_state_regalias(ec_fsm_slave_scan_t *);
#endif
void ec_fsm_slave_scan_state_preop(ec_fsm_slave_scan_t *);
void ec_fsm_slave_scan_state_sync(ec_fsm_slave_scan_t *);
void ec_fsm_slave_scan_state_pdos(ec_fsm_slave_scan_t *);
void ec_fsm_slave_scan_state_end(ec_fsm_slave_scan_t *);
void ec_fsm_slave_scan_state_error(ec_fsm_slave_scan_t *);
void ec_fsm_slave_scan_enter_datalink(ec_fsm_slave_scan_t *);
#ifdef EC_REGALIAS
void ec_fsm_slave_scan_enter_regalias(ec_fsm_slave_scan_t *);
#endif
void ec_fsm_slave_scan_enter_preop(ec_fsm_slave_scan_t *);
void ec_fsm_slave_scan_enter_pdos(ec_fsm_slave_scan_t *);
/*****************************************************************************/
/** Constructor.
*/
void ec_fsm_slave_scan_init(
ec_fsm_slave_scan_t *fsm, /**< Slave scanning state machine. */
ec_datagram_t *datagram, /**< Datagram to use. */
ec_fsm_slave_config_t *fsm_slave_config, /**< Slave configuration
state machine to use. */
ec_fsm_pdo_t *fsm_pdo /**< PDO configuration machine to use. */
)
{
fsm->datagram = datagram;
fsm->fsm_slave_config = fsm_slave_config;
fsm->fsm_pdo = fsm_pdo;
// init sub state machines
ec_fsm_sii_init(&fsm->fsm_sii, fsm->datagram);
}
/*****************************************************************************/
/** Destructor.
*/
void ec_fsm_slave_scan_clear(ec_fsm_slave_scan_t *fsm /**< slave state machine */)
{
// clear sub state machines
ec_fsm_sii_clear(&fsm->fsm_sii);
}
/*****************************************************************************/
/**
* Start slave scan state machine.
*/
void ec_fsm_slave_scan_start(
ec_fsm_slave_scan_t *fsm, /**< slave state machine */
ec_slave_t *slave /**< slave to configure */
)
{
fsm->slave = slave;
fsm->state = ec_fsm_slave_scan_state_start;
}
/*****************************************************************************/
/**
\return false, if state machine has terminated
*/
int ec_fsm_slave_scan_running(const ec_fsm_slave_scan_t *fsm /**< slave state machine */)
{
return fsm->state != ec_fsm_slave_scan_state_end
&& fsm->state != ec_fsm_slave_scan_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_scan_exec(ec_fsm_slave_scan_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_scan_running(fsm);
}
fsm->state(fsm);
return ec_fsm_slave_scan_running(fsm);
}
/*****************************************************************************/
/**
\return true, if the state machine terminated gracefully
*/
int ec_fsm_slave_scan_success(const ec_fsm_slave_scan_t *fsm /**< slave state machine */)
{
return fsm->state == ec_fsm_slave_scan_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_scan_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_scan_t *fsm /**< slave state machine */
)
{
ec_datagram_t *datagram = fsm->datagram;
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
return;
if (datagram->state != EC_DATAGRAM_RECEIVED) {
fsm->state = ec_fsm_slave_scan_state_error;
EC_SLAVE_ERR(fsm->slave,
"Failed to receive station address datagram: ");
ec_datagram_print_state(datagram);
return;
}
if (datagram->working_counter != 1) {
fsm->slave->error_flag = 1;
fsm->state = ec_fsm_slave_scan_state_error;
EC_SLAVE_ERR(fsm->slave, "Failed to write station address: ");
ec_datagram_print_wc_error(datagram);
return;
}
// Read AL state
ec_datagram_fprd(datagram, fsm->slave->station_address, 0x0130, 2);
ec_datagram_zero(datagram);
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_scan_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--)
return;
if (datagram->state != EC_DATAGRAM_RECEIVED) {
fsm->state = ec_fsm_slave_scan_state_error;
EC_SLAVE_ERR(slave, "Failed to receive AL state datagram: ");
ec_datagram_print_state(datagram);
return;
}
if (datagram->working_counter != 1) {
fsm->slave->error_flag = 1;
fsm->state = ec_fsm_slave_scan_state_error;
EC_SLAVE_ERR(slave, "Failed to read AL state: ");
ec_datagram_print_wc_error(datagram);
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, 0);
EC_SLAVE_WARN(slave, "Slave has state error bit set (%s)!\n",
state_str);
}
// read base data
ec_datagram_fprd(datagram, fsm->slave->station_address, 0x0000, 12);
ec_datagram_zero(datagram);
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_scan_t *fsm /**< slave state machine */
)
{
ec_datagram_t *datagram = fsm->datagram;
ec_slave_t *slave = fsm->slave;
u8 octet;
int i;
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
return;
if (datagram->state != EC_DATAGRAM_RECEIVED) {
fsm->state = ec_fsm_slave_scan_state_error;
EC_SLAVE_ERR(slave, "Failed to receive base data datagram: ");
ec_datagram_print_state(datagram);
return;
}
if (datagram->working_counter != 1) {
fsm->slave->error_flag = 1;
fsm->state = ec_fsm_slave_scan_state_error;
EC_SLAVE_ERR(slave, "Failed to read base data: ");
ec_datagram_print_wc_error(datagram);
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) {
EC_SLAVE_WARN(slave, "Slave has more FMMUs (%u) than the master can"
" handle (%u).\n", slave->base_fmmu_count, EC_MAX_FMMUS);
slave->base_fmmu_count = EC_MAX_FMMUS;
}
slave->base_sync_count = EC_READ_U8(datagram->data + 5);
if (slave->base_sync_count > EC_MAX_SYNC_MANAGERS) {
EC_SLAVE_WARN(slave, "Slave provides more sync managers (%u)"
" than the master can handle (%u).\n",
slave->base_sync_count, EC_MAX_SYNC_MANAGERS);
slave->base_sync_count = EC_MAX_SYNC_MANAGERS;
}
octet = EC_READ_U8(datagram->data + 7);
for (i = 0; i < EC_MAX_PORTS; i++) {
slave->ports[i].desc = (octet >> (2 * i)) & 0x03;
}
octet = EC_READ_U8(datagram->data + 8);
slave->base_fmmu_bit_operation = octet & 0x01;
slave->base_dc_supported = (octet >> 2) & 0x01;
slave->base_dc_range = ((octet >> 3) & 0x01) ? EC_DC_64 : EC_DC_32;
if (slave->base_dc_supported) {
// read DC capabilities
ec_datagram_fprd(datagram, slave->station_address, 0x0910,
slave->base_dc_range == EC_DC_64 ? 8 : 4);
ec_datagram_zero(datagram);
fsm->retries = EC_FSM_RETRIES;
fsm->state = ec_fsm_slave_scan_state_dc_cap;
} else {
ec_fsm_slave_scan_enter_datalink(fsm);
}
}
/*****************************************************************************/
/**
Slave scan state: DC CAPABILITIES.
*/
void ec_fsm_slave_scan_state_dc_cap(
ec_fsm_slave_scan_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--)
return;
if (datagram->state != EC_DATAGRAM_RECEIVED) {
fsm->state = ec_fsm_slave_scan_state_error;
EC_SLAVE_ERR(slave, "Failed to receive system time datagram: ");
ec_datagram_print_state(datagram);
return;
}
if (datagram->working_counter == 1) {
slave->has_dc_system_time = 1;
EC_SLAVE_DBG(slave, 1, "Slave has the System Time register.\n");
} else if (datagram->working_counter == 0) {
EC_SLAVE_DBG(slave, 1, "Slave has no System Time register; delay "
"measurement only.\n");
} else {
fsm->slave->error_flag = 1;
fsm->state = ec_fsm_slave_scan_state_error;
EC_SLAVE_ERR(slave, "Failed to determine, if system time register is "
"supported: ");
ec_datagram_print_wc_error(datagram);
return;
}
// read DC port receive times
ec_datagram_fprd(datagram, slave->station_address, 0x0900, 16);
ec_datagram_zero(datagram);
fsm->retries = EC_FSM_RETRIES;
fsm->state = ec_fsm_slave_scan_state_dc_times;
}
/*****************************************************************************/
/**
Slave scan state: DC TIMES.
*/
void ec_fsm_slave_scan_state_dc_times(
ec_fsm_slave_scan_t *fsm /**< slave state machine */
)
{
ec_datagram_t *datagram = fsm->datagram;
ec_slave_t *slave = fsm->slave;
int i;
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
return;
if (datagram->state != EC_DATAGRAM_RECEIVED) {
fsm->state = ec_fsm_slave_scan_state_error;
EC_SLAVE_ERR(slave, "Failed to receive system time datagram: ");
ec_datagram_print_state(datagram);
return;
}
if (datagram->working_counter != 1) {
fsm->slave->error_flag = 1;
fsm->state = ec_fsm_slave_scan_state_error;
EC_SLAVE_ERR(slave, "Failed to get DC receive times: ");
ec_datagram_print_wc_error(datagram);
return;
}
for (i = 0; i < EC_MAX_PORTS; i++) {
slave->ports[i].receive_time = EC_READ_U32(datagram->data + 4 * i);
}
ec_fsm_slave_scan_enter_datalink(fsm);
}
/*****************************************************************************/
/**
Slave scan entry function: DATALINK.
*/
void ec_fsm_slave_scan_enter_datalink(
ec_fsm_slave_scan_t *fsm /**< slave state machine */
)
{
ec_datagram_t *datagram = fsm->datagram;
ec_slave_t *slave = fsm->slave;
// read data link status
ec_datagram_fprd(datagram, slave->station_address, 0x0110, 2);
ec_datagram_zero(datagram);
fsm->retries = EC_FSM_RETRIES;
fsm->state = ec_fsm_slave_scan_state_datalink;
}
/*****************************************************************************/
/** Enter slave scan state SII_SIZE.
*/
void ec_fsm_slave_scan_enter_sii_size(
ec_fsm_slave_scan_t *fsm /**< slave state machine */
)
{
// Start fetching SII size
EC_SLAVE_DBG(fsm->slave, 1, "Determining SII size.\n");
fsm->sii_offset = EC_FIRST_SII_CATEGORY_OFFSET; // first category header
ec_fsm_sii_read(&fsm->fsm_sii, fsm->slave, fsm->sii_offset,
EC_FSM_SII_USE_CONFIGURED_ADDRESS);
fsm->state = ec_fsm_slave_scan_state_sii_size;
fsm->state(fsm); // execute state immediately
}
/*****************************************************************************/
#ifdef EC_SII_ASSIGN
/** Enter slave scan state ASSIGN_SII.
*/
void ec_fsm_slave_scan_enter_assign_sii(
ec_fsm_slave_scan_t *fsm /**< slave state machine */
)
{
ec_datagram_t *datagram = fsm->datagram;
ec_slave_t *slave = fsm->slave;
EC_SLAVE_DBG(slave, 1, "Assigning SII access to EtherCAT.\n");
// assign SII to ECAT
ec_datagram_fpwr(datagram, slave->station_address, 0x0500, 1);
EC_WRITE_U8(datagram->data, 0x00); // EtherCAT
fsm->retries = EC_FSM_RETRIES;
fsm->state = ec_fsm_slave_scan_state_assign_sii;
}
#endif
/*****************************************************************************/
/**
Slave scan state: DATALINK.
*/
void ec_fsm_slave_scan_state_datalink(
ec_fsm_slave_scan_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--)
return;
if (datagram->state != EC_DATAGRAM_RECEIVED) {
fsm->state = ec_fsm_slave_scan_state_error;
EC_SLAVE_ERR(slave, "Failed to receive DL status datagram: ");
ec_datagram_print_state(datagram);
return;
}
if (datagram->working_counter != 1) {
fsm->slave->error_flag = 1;
fsm->state = ec_fsm_slave_scan_state_error;
EC_SLAVE_ERR(slave, "Failed to read DL status: ");
ec_datagram_print_wc_error(datagram);
return;
}
dl_status = EC_READ_U16(datagram->data);
for (i = 0; i < EC_MAX_PORTS; i++) {
slave->ports[i].link.link_up =
dl_status & (1 << (4 + i)) ? 1 : 0;
slave->ports[i].link.loop_closed =
dl_status & (1 << (8 + i * 2)) ? 1 : 0;
slave->ports[i].link.signal_detected =
dl_status & (1 << (9 + i * 2)) ? 1 : 0;
}
#ifdef EC_SII_ASSIGN
ec_fsm_slave_scan_enter_assign_sii(fsm);
#else
ec_fsm_slave_scan_enter_sii_size(fsm);
#endif
}
/*****************************************************************************/
#ifdef EC_SII_ASSIGN
/**
Slave scan state: ASSIGN_SII.
*/
void ec_fsm_slave_scan_state_assign_sii(
ec_fsm_slave_scan_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--) {
return;
}
if (datagram->state != EC_DATAGRAM_RECEIVED) {
EC_SLAVE_WARN(slave, "Failed to receive SII assignment datagram: ");
ec_datagram_print_state(datagram);
// Try to go on, probably assignment is correct
goto continue_with_sii_size;
}
if (datagram->working_counter != 1) {
EC_SLAVE_WARN(slave, "Failed to assign SII to EtherCAT: ");
ec_datagram_print_wc_error(datagram);
// Try to go on, probably assignment is correct
}
continue_with_sii_size:
ec_fsm_slave_scan_enter_sii_size(fsm);
}
#endif
/*****************************************************************************/
/**
Slave scan state: SII SIZE.
*/
void ec_fsm_slave_scan_state_sii_size(
ec_fsm_slave_scan_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_scan_state_error;
EC_SLAVE_ERR(slave, "Failed to determine SII content size:"
" Reading word offset 0x%04x failed. Assuming %u words.\n",
fsm->sii_offset, EC_FIRST_SII_CATEGORY_OFFSET);
slave->sii_nwords = EC_FIRST_SII_CATEGORY_OFFSET;
goto alloc_sii;
}
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
off_t next_offset = 2UL + fsm->sii_offset + cat_size;
EC_SLAVE_DBG(slave, 1, "Found category type %u with size %u."
" Proceeding to offset %zu.\n",
cat_type, cat_size, next_offset);
if (next_offset >= EC_MAX_SII_SIZE) {
EC_SLAVE_WARN(slave, "SII size exceeds %u words"
" (0xffff limiter missing?).\n", EC_MAX_SII_SIZE);
// cut off category data...
slave->sii_nwords = EC_FIRST_SII_CATEGORY_OFFSET;
goto alloc_sii;
}
fsm->sii_offset = next_offset;
ec_fsm_sii_read(&fsm->fsm_sii, slave, fsm->sii_offset,
EC_FSM_SII_USE_CONFIGURED_ADDRESS);
ec_fsm_sii_exec(&fsm->fsm_sii); // execute state immediately
return;
}
slave->sii_nwords = fsm->sii_offset + 1;
alloc_sii:
if (slave->sii_words) {
EC_SLAVE_WARN(slave, "Freeing old SII data...\n");
kfree(slave->sii_words);
}
if (!(slave->sii_words =
(uint16_t *) kmalloc(slave->sii_nwords * 2, GFP_KERNEL))) {
EC_SLAVE_ERR(slave, "Failed to allocate %zu words of SII data.\n",
slave->sii_nwords);
slave->sii_nwords = 0;
slave->error_flag = 1;
fsm->state = ec_fsm_slave_scan_state_error;
return;
}
// Start fetching SII contents
fsm->state = ec_fsm_slave_scan_state_sii_data;
fsm->sii_offset = 0x0000;
ec_fsm_sii_read(&fsm->fsm_sii, slave, fsm->sii_offset,
EC_FSM_SII_USE_CONFIGURED_ADDRESS);
ec_fsm_sii_exec(&fsm->fsm_sii); // execute state immediately
}
/*****************************************************************************/
/**
Slave scan state: SII DATA.
*/
void ec_fsm_slave_scan_state_sii_data(ec_fsm_slave_scan_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_scan_state_error;
EC_SLAVE_ERR(slave, "Failed to fetch SII contents.\n");
return;
}
// 2 words fetched
if (fsm->sii_offset + 2 <= slave->sii_nwords) { // 2 words fit
memcpy(slave->sii_words + fsm->sii_offset, fsm->fsm_sii.value, 4);
} else { // copy the last word
memcpy(slave->sii_words + fsm->sii_offset, fsm->fsm_sii.value, 2);
}
if (fsm->sii_offset + 2 < slave->sii_nwords) {
// fetch the next 2 words
fsm->sii_offset += 2;
ec_fsm_sii_read(&fsm->fsm_sii, slave, fsm->sii_offset,
EC_FSM_SII_USE_CONFIGURED_ADDRESS);
ec_fsm_sii_exec(&fsm->fsm_sii); // execute state immediately
return;
}
// Evaluate SII contents
ec_slave_clear_sync_managers(slave);
slave->sii.alias =
EC_READ_U16(slave->sii_words + 0x0004);
slave->effective_alias = slave->sii.alias;
slave->sii.vendor_id =
EC_READ_U32(slave->sii_words + 0x0008);
slave->sii.product_code =
EC_READ_U32(slave->sii_words + 0x000A);
slave->sii.revision_number =
EC_READ_U32(slave->sii_words + 0x000C);
slave->sii.serial_number =
EC_READ_U32(slave->sii_words + 0x000E);
slave->sii.boot_rx_mailbox_offset =
EC_READ_U16(slave->sii_words + 0x0014);
slave->sii.boot_rx_mailbox_size =
EC_READ_U16(slave->sii_words + 0x0015);
slave->sii.boot_tx_mailbox_offset =
EC_READ_U16(slave->sii_words + 0x0016);
slave->sii.boot_tx_mailbox_size =
EC_READ_U16(slave->sii_words + 0x0017);
slave->sii.std_rx_mailbox_offset =
EC_READ_U16(slave->sii_words + 0x0018);
slave->sii.std_rx_mailbox_size =
EC_READ_U16(slave->sii_words + 0x0019);
slave->sii.std_tx_mailbox_offset =
EC_READ_U16(slave->sii_words + 0x001A);
slave->sii.std_tx_mailbox_size =
EC_READ_U16(slave->sii_words + 0x001B);
slave->sii.mailbox_protocols =
EC_READ_U16(slave->sii_words + 0x001C);
if (slave->sii.mailbox_protocols) {
int need_delim = 0;
uint16_t all = EC_MBOX_AOE | EC_MBOX_COE | EC_MBOX_FOE |
EC_MBOX_SOE | EC_MBOX_VOE;
if ((slave->sii.mailbox_protocols & all) &&
slave->master->debug_level >= 1) {
EC_SLAVE_DBG(slave, 1, "Slave announces to support ");
if (slave->sii.mailbox_protocols & EC_MBOX_AOE) {
printk(KERN_CONT "AoE");
need_delim = 1;
}
if (slave->sii.mailbox_protocols & EC_MBOX_COE) {
if (need_delim) {
printk(KERN_CONT ", ");
}
printk(KERN_CONT "CoE");
need_delim = 1;
}
if (slave->sii.mailbox_protocols & EC_MBOX_FOE) {
if (need_delim) {
printk(KERN_CONT ", ");
}
printk(KERN_CONT "FoE");
need_delim = 1;
}
if (slave->sii.mailbox_protocols & EC_MBOX_SOE) {
if (need_delim) {
printk(KERN_CONT ", ");
}
printk(KERN_CONT "SoE");
need_delim = 1;
}
if (slave->sii.mailbox_protocols & EC_MBOX_VOE) {
if (need_delim) {
printk(KERN_CONT ", ");
}
printk(KERN_CONT "VoE");
need_delim = 1;
}
printk(KERN_CONT ".\n");
}
if (slave->sii.mailbox_protocols & ~all) {
EC_SLAVE_DBG(slave, 1, "Slave announces to support unknown"
" mailbox protocols 0x%04X.",
slave->sii.mailbox_protocols & ~all);
}
}
else {
EC_SLAVE_DBG(slave, 1, "Slave announces to support no mailbox"
" protocols.");
}
if (slave->sii.boot_rx_mailbox_offset == 0xffff ||
slave->sii.boot_rx_mailbox_size == 0xffff ||
slave->sii.boot_tx_mailbox_offset == 0xffff ||
slave->sii.boot_tx_mailbox_size == 0xffff ||
slave->sii.std_rx_mailbox_offset == 0xffff ||
slave->sii.std_rx_mailbox_size == 0xffff ||
slave->sii.std_tx_mailbox_offset == 0xffff ||
slave->sii.std_tx_mailbox_size == 0xffff) {
slave->sii.mailbox_protocols = 0x0000;
EC_SLAVE_ERR(slave, "Invalid mailbox settings in SII."
" Disabling mailbox communication.");
}
if (slave->sii_nwords == EC_FIRST_SII_CATEGORY_OFFSET) {
// sii does not contain category data
fsm->state = ec_fsm_slave_scan_state_end;
return;
}
if (slave->sii_nwords < EC_FIRST_SII_CATEGORY_OFFSET + 1) {
EC_SLAVE_ERR(slave, "Unexpected end of SII data:"
" First category header missing.\n");
goto end;
}
// evaluate category data
cat_word = slave->sii_words + EC_FIRST_SII_CATEGORY_OFFSET;
while (EC_READ_U16(cat_word) != 0xFFFF) {
// type and size words must fit
if (cat_word + 2 - slave->sii_words > slave->sii_nwords) {
EC_SLAVE_ERR(slave, "Unexpected end of SII data:"
" Category header incomplete.\n");
goto end;
}
cat_type = EC_READ_U16(cat_word) & 0x7FFF;
cat_size = EC_READ_U16(cat_word + 1);
cat_word += 2;
if (cat_word + cat_size - slave->sii_words > slave->sii_nwords) {
EC_SLAVE_WARN(slave, "Unexpected end of SII data:"
" Category data incomplete.\n");
goto end;
}
switch (cat_type) {
case 0x000A:
if (ec_slave_fetch_sii_strings(slave, (uint8_t *) cat_word,
cat_size * 2))
goto end;
break;
case 0x001E:
if (ec_slave_fetch_sii_general(slave, (uint8_t *) cat_word,
cat_size * 2))
goto end;
break;
case 0x0028:
break;
case 0x0029:
if (ec_slave_fetch_sii_syncs(slave, (uint8_t *) cat_word,
cat_size * 2))
goto end;
break;
case 0x0032:
if (ec_slave_fetch_sii_pdos( slave, (uint8_t *) cat_word,
cat_size * 2, EC_DIR_INPUT)) // TxPDO
goto end;
break;
case 0x0033:
if (ec_slave_fetch_sii_pdos( slave, (uint8_t *) cat_word,
cat_size * 2, EC_DIR_OUTPUT)) // RxPDO
goto end;
break;
default:
EC_SLAVE_DBG(slave, 1, "Unknown category type 0x%04X.\n",
cat_type);
}
cat_word += cat_size;
if (cat_word - slave->sii_words >= slave->sii_nwords) {
EC_SLAVE_WARN(slave, "Unexpected end of SII data:"
" Next category header missing.\n");
goto end;
}
}
#ifdef EC_REGALIAS
ec_fsm_slave_scan_enter_regalias(fsm);
#else
if (slave->sii.mailbox_protocols & EC_MBOX_COE) {
ec_fsm_slave_scan_enter_preop(fsm);
} else {
fsm->state = ec_fsm_slave_scan_state_end;
}
#endif
return;
end:
EC_SLAVE_ERR(slave, "Failed to analyze category data.\n");
fsm->slave->error_flag = 1;
fsm->state = ec_fsm_slave_scan_state_error;
}
/*****************************************************************************/
#ifdef EC_REGALIAS
/** Slave scan entry function: REGALIAS.
*/
void ec_fsm_slave_scan_enter_regalias(
ec_fsm_slave_scan_t *fsm /**< slave state machine */
)
{
ec_datagram_t *datagram = fsm->datagram;
ec_slave_t *slave = fsm->slave;
// read alias from register
EC_SLAVE_DBG(slave, 1, "Reading alias from register.\n");
ec_datagram_fprd(datagram, slave->station_address, 0x0012, 2);
ec_datagram_zero(datagram);
fsm->retries = EC_FSM_RETRIES;
fsm->state = ec_fsm_slave_scan_state_regalias;
}
/*****************************************************************************/
/** Slave scan state: REGALIAS.
*/
void ec_fsm_slave_scan_state_regalias(
ec_fsm_slave_scan_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--)
return;
if (datagram->state != EC_DATAGRAM_RECEIVED) {
fsm->state = ec_fsm_slave_scan_state_error;
EC_SLAVE_ERR(slave, "Failed to receive register alias datagram: ");
ec_datagram_print_state(datagram);
return;
}
if (datagram->working_counter != 1) {
EC_SLAVE_DBG(slave, 1, "Failed to read register alias.\n");
} else {
slave->effective_alias = EC_READ_U16(datagram->data);
EC_SLAVE_DBG(slave, 1, "Read alias %u from register.\n",
slave->effective_alias);
}
if (slave->sii.mailbox_protocols & EC_MBOX_COE) {
ec_fsm_slave_scan_enter_preop(fsm);
} else {
fsm->state = ec_fsm_slave_scan_state_end;
}
}
#endif // defined EC_REGALIAS
/*****************************************************************************/
/** Enter slave scan state PREOP.
*/
void ec_fsm_slave_scan_enter_preop(
ec_fsm_slave_scan_t *fsm /**< slave state machine */
)
{
ec_slave_t *slave = fsm->slave;
uint8_t current_state = slave->current_state & EC_SLAVE_STATE_MASK;
if (current_state != EC_SLAVE_STATE_PREOP
&& current_state != EC_SLAVE_STATE_SAFEOP
&& current_state != EC_SLAVE_STATE_OP) {
if (slave->master->debug_level) {
char str[EC_STATE_STRING_SIZE];
ec_state_string(current_state, str, 0);
EC_SLAVE_DBG(slave, 0, "Slave is not in the state"
" to do mailbox com (%s), setting to PREOP.\n", str);
}
fsm->state = ec_fsm_slave_scan_state_preop;
ec_slave_request_state(slave, EC_SLAVE_STATE_PREOP);
ec_fsm_slave_config_start(fsm->fsm_slave_config, slave);
ec_fsm_slave_config_exec(fsm->fsm_slave_config);
} else {
EC_SLAVE_DBG(slave, 1, "Reading mailbox"
" sync manager configuration.\n");
/* Scan current sync manager configuration to get configured mailbox
* sizes. */
ec_datagram_fprd(fsm->datagram, slave->station_address, 0x0800,
EC_SYNC_PAGE_SIZE * 2);
fsm->retries = EC_FSM_RETRIES;
fsm->state = ec_fsm_slave_scan_state_sync;
}
}
/*****************************************************************************/
/** Slave scan state: PREOP.
*/
void ec_fsm_slave_scan_state_preop(
ec_fsm_slave_scan_t *fsm /**< slave state machine */
)
{
if (ec_fsm_slave_config_exec(fsm->fsm_slave_config))
return;
if (!ec_fsm_slave_config_success(fsm->fsm_slave_config)) {
fsm->state = ec_fsm_slave_scan_state_error;
return;
}
ec_fsm_slave_scan_enter_pdos(fsm);
}
/*****************************************************************************/
/** Slave scan state: SYNC.
*/
void ec_fsm_slave_scan_state_sync(
ec_fsm_slave_scan_t *fsm /**< slave state machine */
)
{
ec_datagram_t *datagram = fsm->datagram;
ec_slave_t *slave = fsm->slave;
uint16_t tx_offset, tx_size, rx_offset, rx_size;
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
return;
if (datagram->state != EC_DATAGRAM_RECEIVED) {
fsm->state = ec_fsm_slave_scan_state_error;
EC_SLAVE_ERR(slave, "Failed to receive sync manager"
" configuration datagram: ");
ec_datagram_print_state(datagram);
return;
}
if (datagram->working_counter != 1) {
fsm->slave->error_flag = 1;
fsm->state = ec_fsm_slave_scan_state_error;
EC_SLAVE_ERR(slave, "Failed to read DL status: ");
ec_datagram_print_wc_error(datagram);
return;
}
rx_offset = EC_READ_U16(datagram->data);
rx_size = EC_READ_U16(datagram->data + 2);
tx_offset = EC_READ_U16(datagram->data + 8);
tx_size = EC_READ_U16(datagram->data + 10);
if (rx_size == 0xffff) {
fsm->state = ec_fsm_slave_scan_state_error;
slave->sii.mailbox_protocols = 0x0000;
EC_SLAVE_ERR(slave, "Invalid RX mailbox size (%u) configured."
" Disabling mailbox communication.", rx_size);
return;
}
if (tx_size == 0xffff) {
fsm->state = ec_fsm_slave_scan_state_error;
slave->sii.mailbox_protocols = 0x0000;
EC_SLAVE_ERR(slave, "Invalid TX mailbox size (%u) configured."
" Disabling mailbox communication.", tx_size);
return;
}
slave->configured_rx_mailbox_offset = rx_offset;
slave->configured_rx_mailbox_size = rx_size;
slave->configured_tx_mailbox_offset = tx_offset;
slave->configured_tx_mailbox_size = tx_size;
EC_SLAVE_DBG(slave, 1, "Mailbox configuration:\n");
EC_SLAVE_DBG(slave, 1, " RX offset=0x%04x size=%u\n",
slave->configured_rx_mailbox_offset,
slave->configured_rx_mailbox_size);
EC_SLAVE_DBG(slave, 1, " TX offset=0x%04x size=%u\n",
slave->configured_tx_mailbox_offset,
slave->configured_tx_mailbox_size);
ec_fsm_slave_scan_enter_pdos(fsm);
}
/*****************************************************************************/
/** Enter slave scan state PDOS.
*/
void ec_fsm_slave_scan_enter_pdos(
ec_fsm_slave_scan_t *fsm /**< slave state machine */
)
{
ec_slave_t *slave = fsm->slave;
EC_SLAVE_DBG(slave, 1, "Scanning PDO assignment and mapping.\n");
fsm->state = ec_fsm_slave_scan_state_pdos;
ec_fsm_pdo_start_reading(fsm->fsm_pdo, slave);
ec_fsm_pdo_exec(fsm->fsm_pdo, fsm->datagram); // execute immediately
}
/*****************************************************************************/
/** Slave scan state: PDOS.
*/
void ec_fsm_slave_scan_state_pdos(
ec_fsm_slave_scan_t *fsm /**< slave state machine */
)
{
if (ec_fsm_pdo_exec(fsm->fsm_pdo, fsm->datagram)) {
return;
}
if (!ec_fsm_pdo_success(fsm->fsm_pdo)) {
fsm->state = ec_fsm_slave_scan_state_error;
return;
}
// reading PDO configuration finished
fsm->state = ec_fsm_slave_scan_state_end;
}
/******************************************************************************
* Common state functions
*****************************************************************************/
/** State: ERROR.
*/
void ec_fsm_slave_scan_state_error(
ec_fsm_slave_scan_t *fsm /**< slave state machine */
)
{
}
/*****************************************************************************/
/** State: END.
*/
void ec_fsm_slave_scan_state_end(
ec_fsm_slave_scan_t *fsm /**< slave state machine */
)
{
}
/*****************************************************************************/
|