forked from OpenEtherCATsociety/SOEM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethercatmain.c
2402 lines (2212 loc) · 73.4 KB
/
ethercatmain.c
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
/*
* Licensed under the GNU General Public License version 2 with exceptions. See
* LICENSE file in the project root for full license information
*/
/**
* \file
* \brief
* Main EtherCAT functions.
*
* Initialisation, state set and read, mailbox primitives, EEPROM primitives,
* SII reading and processdata exchange.
*
* Defines ec_slave[]. All slave information is put in this structure.
* Needed for most user interaction with slaves.
*/
#include <stdio.h>
#include <string.h>
#include "osal.h"
#include "oshw.h"
#include "ethercat.h"
/** delay in us for eeprom ready loop */
#define EC_LOCALDELAY 200
/** record for ethercat eeprom communications */
PACKED_BEGIN
typedef struct PACKED
{
uint16 comm;
uint16 addr;
uint16 d2;
} ec_eepromt;
PACKED_END
/** mailbox error structure */
PACKED_BEGIN
typedef struct PACKED
{
ec_mbxheadert MbxHeader;
uint16 Type;
uint16 Detail;
} ec_mbxerrort;
PACKED_END
/** emergency request structure */
PACKED_BEGIN
typedef struct PACKED
{
ec_mbxheadert MbxHeader;
uint16 CANOpen;
uint16 ErrorCode;
uint8 ErrorReg;
uint8 bData;
uint16 w1,w2;
} ec_emcyt;
PACKED_END
#ifdef EC_VER1
/** Main slave data array.
* Each slave found on the network gets its own record.
* ec_slave[0] is reserved for the master. Structure gets filled
* in by the configuration function ec_config().
*/
ec_slavet ec_slave[EC_MAXSLAVE];
/** number of slaves found on the network */
int ec_slavecount;
/** slave group structure */
ec_groupt ec_group[EC_MAXGROUP];
/** cache for EEPROM read functions */
static uint8 ec_esibuf[EC_MAXEEPBUF];
/** bitmap for filled cache buffer bytes */
static uint32 ec_esimap[EC_MAXEEPBITMAP];
/** current slave for EEPROM cache buffer */
static ec_eringt ec_elist;
static ec_idxstackT ec_idxstack;
/** SyncManager Communication Type struct to store data of one slave */
static ec_SMcommtypet ec_SMcommtype[EC_MAX_MAPT];
/** PDO assign struct to store data of one slave */
static ec_PDOassignt ec_PDOassign[EC_MAX_MAPT];
/** PDO description struct to store data of one slave */
static ec_PDOdesct ec_PDOdesc[EC_MAX_MAPT];
/** buffer for EEPROM SM data */
static ec_eepromSMt ec_SM;
/** buffer for EEPROM FMMU data */
static ec_eepromFMMUt ec_FMMU;
/** Global variable TRUE if error available in error stack */
boolean EcatError = FALSE;
int64 ec_DCtime;
ecx_portt ecx_port;
ecx_redportt ecx_redport;
ecx_contextt ecx_context = {
&ecx_port, // .port =
&ec_slave[0], // .slavelist =
&ec_slavecount, // .slavecount =
EC_MAXSLAVE, // .maxslave =
&ec_group[0], // .grouplist =
EC_MAXGROUP, // .maxgroup =
&ec_esibuf[0], // .esibuf =
&ec_esimap[0], // .esimap =
0, // .esislave =
&ec_elist, // .elist =
&ec_idxstack, // .idxstack =
&EcatError, // .ecaterror =
&ec_DCtime, // .DCtime =
&ec_SMcommtype[0], // .SMcommtype =
&ec_PDOassign[0], // .PDOassign =
&ec_PDOdesc[0], // .PDOdesc =
&ec_SM, // .eepSM =
&ec_FMMU, // .eepFMMU =
NULL, // .FOEhook()
NULL, // .EOEhook()
0, // .manualstatechange
NULL, // .userdata
};
#endif
/** Create list over available network adapters.
*
* @return First element in list over available network adapters.
*/
ec_adaptert * ec_find_adapters (void)
{
ec_adaptert * ret_adapter;
ret_adapter = oshw_find_adapters ();
return ret_adapter;
}
/** Free dynamically allocated list over available network adapters.
*
* @param[in] adapter = Struct holding adapter name, description and pointer to next.
*/
void ec_free_adapters (ec_adaptert * adapter)
{
oshw_free_adapters (adapter);
}
/** Pushes an error on the error list.
*
* @param[in] context = context struct
* @param[in] Ec pointer describing the error.
*/
void ecx_pusherror(ecx_contextt *context, const ec_errort *Ec)
{
context->elist->Error[context->elist->head] = *Ec;
context->elist->Error[context->elist->head].Signal = TRUE;
context->elist->head++;
if (context->elist->head > EC_MAXELIST)
{
context->elist->head = 0;
}
if (context->elist->head == context->elist->tail)
{
context->elist->tail++;
}
if (context->elist->tail > EC_MAXELIST)
{
context->elist->tail = 0;
}
*(context->ecaterror) = TRUE;
}
/** Pops an error from the list.
*
* @param[in] context = context struct
* @param[out] Ec = Struct describing the error.
* @return TRUE if an error was popped.
*/
boolean ecx_poperror(ecx_contextt *context, ec_errort *Ec)
{
boolean notEmpty = (context->elist->head != context->elist->tail);
*Ec = context->elist->Error[context->elist->tail];
context->elist->Error[context->elist->tail].Signal = FALSE;
if (notEmpty)
{
context->elist->tail++;
if (context->elist->tail > EC_MAXELIST)
{
context->elist->tail = 0;
}
}
else
{
*(context->ecaterror) = FALSE;
}
return notEmpty;
}
/** Check if error list has entries.
*
* @param[in] context = context struct
* @return TRUE if error list contains entries.
*/
boolean ecx_iserror(ecx_contextt *context)
{
return (context->elist->head != context->elist->tail);
}
/** Report packet error
*
* @param[in] context = context struct
* @param[in] Slave = Slave number
* @param[in] Index = Index that generated error
* @param[in] SubIdx = Subindex that generated error
* @param[in] ErrorCode = Error code
*/
void ecx_packeterror(ecx_contextt *context, uint16 Slave, uint16 Index, uint8 SubIdx, uint16 ErrorCode)
{
ec_errort Ec;
memset(&Ec, 0, sizeof(Ec));
Ec.Time = osal_current_time();
Ec.Slave = Slave;
Ec.Index = Index;
Ec.SubIdx = SubIdx;
*(context->ecaterror) = TRUE;
Ec.Etype = EC_ERR_TYPE_PACKET_ERROR;
Ec.ErrorCode = ErrorCode;
ecx_pusherror(context, &Ec);
}
/** Report Mailbox Error
*
* @param[in] context = context struct
* @param[in] Slave = Slave number
* @param[in] Detail = Following EtherCAT specification
*/
static void ecx_mbxerror(ecx_contextt *context, uint16 Slave,uint16 Detail)
{
ec_errort Ec;
memset(&Ec, 0, sizeof(Ec));
Ec.Time = osal_current_time();
Ec.Slave = Slave;
Ec.Index = 0;
Ec.SubIdx = 0;
Ec.Etype = EC_ERR_TYPE_MBX_ERROR;
Ec.ErrorCode = Detail;
ecx_pusherror(context, &Ec);
}
/** Report Mailbox Emergency Error
*
* @param[in] context = context struct
* @param[in] Slave = Slave number
* @param[in] ErrorCode = Following EtherCAT specification
* @param[in] ErrorReg
* @param[in] b1
* @param[in] w1
* @param[in] w2
*/
static void ecx_mbxemergencyerror(ecx_contextt *context, uint16 Slave,uint16 ErrorCode,uint16 ErrorReg,
uint8 b1, uint16 w1, uint16 w2)
{
ec_errort Ec;
memset(&Ec, 0, sizeof(Ec));
Ec.Time = osal_current_time();
Ec.Slave = Slave;
Ec.Index = 0;
Ec.SubIdx = 0;
Ec.Etype = EC_ERR_TYPE_EMERGENCY;
Ec.ErrorCode = ErrorCode;
Ec.ErrorReg = (uint8)ErrorReg;
Ec.b1 = b1;
Ec.w1 = w1;
Ec.w2 = w2;
ecx_pusherror(context, &Ec);
}
/** Initialise lib in single NIC mode
* @param[in] context = context struct
* @param[in] ifname = Dev name, f.e. "eth0"
* @return >0 if OK
*/
int ecx_init(ecx_contextt *context, const char * ifname)
{
return ecx_setupnic(context->port, ifname, FALSE);
}
/** Initialise lib in redundant NIC mode
* @param[in] context = context struct
* @param[in] redport = pointer to redport, redundant port data
* @param[in] ifname = Primary Dev name, f.e. "eth0"
* @param[in] if2name = Secondary Dev name, f.e. "eth1"
* @return >0 if OK
*/
int ecx_init_redundant(ecx_contextt *context, ecx_redportt *redport, const char *ifname, char *if2name)
{
int rval, zbuf;
ec_etherheadert *ehp;
context->port->redport = redport;
ecx_setupnic(context->port, ifname, FALSE);
rval = ecx_setupnic(context->port, if2name, TRUE);
/* prepare "dummy" BRD tx frame for redundant operation */
ehp = (ec_etherheadert *)&(context->port->txbuf2);
ehp->sa1 = oshw_htons(secMAC[0]);
zbuf = 0;
ecx_setupdatagram(context->port, &(context->port->txbuf2), EC_CMD_BRD, 0, 0x0000, 0x0000, 2, &zbuf);
context->port->txbuflength2 = ETH_HEADERSIZE + EC_HEADERSIZE + EC_WKCSIZE + 2;
return rval;
}
/** Close lib.
* @param[in] context = context struct
*/
void ecx_close(ecx_contextt *context)
{
ecx_closenic(context->port);
};
/** Read one byte from slave EEPROM via cache.
* If the cache location is empty then a read request is made to the slave.
* Depending on the slave capabilities the request is 4 or 8 bytes.
* @param[in] context = context struct
* @param[in] slave = slave number
* @param[in] address = eeprom address in bytes (slave uses words)
* @return requested byte, if not available then 0xff
*/
uint8 ecx_siigetbyte(ecx_contextt *context, uint16 slave, uint16 address)
{
uint16 configadr, eadr;
uint64 edat64;
uint32 edat32;
uint16 mapw, mapb;
int lp,cnt;
uint8 retval;
retval = 0xff;
if (slave != context->esislave) /* not the same slave? */
{
memset(context->esimap, 0x00, EC_MAXEEPBITMAP * sizeof(uint32)); /* clear esibuf cache map */
context->esislave = slave;
}
if (address < EC_MAXEEPBUF)
{
mapw = address >> 5;
mapb = (uint16)(address - (mapw << 5));
if (context->esimap[mapw] & (1U << mapb))
{
/* byte is already in buffer */
retval = context->esibuf[address];
}
else
{
/* byte is not in buffer, put it there */
configadr = context->slavelist[slave].configadr;
ecx_eeprom2master(context, slave); /* set eeprom control to master */
eadr = address >> 1;
edat64 = ecx_readeepromFP (context, configadr, eadr, EC_TIMEOUTEEP);
/* 8 byte response */
if (context->slavelist[slave].eep_8byte)
{
put_unaligned64(edat64, &(context->esibuf[eadr << 1]));
cnt = 8;
}
/* 4 byte response */
else
{
edat32 = (uint32)edat64;
put_unaligned32(edat32, &(context->esibuf[eadr << 1]));
cnt = 4;
}
/* find bitmap location */
mapw = eadr >> 4;
mapb = (uint16)((eadr << 1) - (mapw << 5));
for(lp = 0 ; lp < cnt ; lp++)
{
/* set bitmap for each byte that is read */
context->esimap[mapw] |= (1U << mapb);
mapb++;
if (mapb > 31)
{
mapb = 0;
mapw++;
}
}
retval = context->esibuf[address];
}
}
return retval;
}
/** Find SII section header in slave EEPROM.
* @param[in] context = context struct
* @param[in] slave = slave number
* @param[in] cat = section category
* @return byte address of section at section length entry, if not available then 0
*/
int16 ecx_siifind(ecx_contextt *context, uint16 slave, uint16 cat)
{
int16 a;
uint16 p;
uint8 eectl = context->slavelist[slave].eep_pdi;
a = ECT_SII_START << 1;
/* read first SII section category */
p = ecx_siigetbyte(context, slave, a++);
p += (ecx_siigetbyte(context, slave, a++) << 8);
/* traverse SII while category is not found and not EOF */
while ((p != cat) && (p != 0xffff))
{
/* read section length */
p = ecx_siigetbyte(context, slave, a++);
p += (ecx_siigetbyte(context, slave, a++) << 8);
/* locate next section category */
a += p << 1;
/* read section category */
p = ecx_siigetbyte(context, slave, a++);
p += (ecx_siigetbyte(context, slave, a++) << 8);
}
if (p != cat)
{
a = 0;
}
if (eectl)
{
ecx_eeprom2pdi(context, slave); /* if eeprom control was previously pdi then restore */
}
return a;
}
/** Get string from SII string section in slave EEPROM.
* @param[in] context = context struct
* @param[out] str = requested string, 0x00 if not found
* @param[in] slave = slave number
* @param[in] Sn = string number
*/
void ecx_siistring(ecx_contextt *context, char *str, uint16 slave, uint16 Sn)
{
uint16 a,i,j,l,n,ba;
char *ptr;
uint8 eectl = context->slavelist[slave].eep_pdi;
ptr = str;
a = ecx_siifind (context, slave, ECT_SII_STRING); /* find string section */
if (a > 0)
{
ba = a + 2; /* skip SII section header */
n = ecx_siigetbyte(context, slave, ba++); /* read number of strings in section */
if (Sn <= n) /* is req string available? */
{
for (i = 1; i <= Sn; i++) /* walk through strings */
{
l = ecx_siigetbyte(context, slave, ba++); /* length of this string */
if (i < Sn)
{
ba += l;
}
else
{
ptr = str;
for (j = 1; j <= l; j++) /* copy one string */
{
if(j <= EC_MAXNAME)
{
*ptr = (char)ecx_siigetbyte(context, slave, ba++);
ptr++;
}
else
{
ba++;
}
}
}
}
*ptr = 0; /* add zero terminator */
}
else
{
ptr = str;
*ptr = 0; /* empty string */
}
}
if (eectl)
{
ecx_eeprom2pdi(context, slave); /* if eeprom control was previously pdi then restore */
}
}
/** Get FMMU data from SII FMMU section in slave EEPROM.
* @param[in] context = context struct
* @param[in] slave = slave number
* @param[out] FMMU = FMMU struct from SII, max. 4 FMMU's
* @return number of FMMU's defined in section
*/
uint16 ecx_siiFMMU(ecx_contextt *context, uint16 slave, ec_eepromFMMUt* FMMU)
{
uint16 a;
uint8 eectl = context->slavelist[slave].eep_pdi;
FMMU->nFMMU = 0;
FMMU->FMMU0 = 0;
FMMU->FMMU1 = 0;
FMMU->FMMU2 = 0;
FMMU->FMMU3 = 0;
FMMU->Startpos = ecx_siifind(context, slave, ECT_SII_FMMU);
if (FMMU->Startpos > 0)
{
a = FMMU->Startpos;
FMMU->nFMMU = ecx_siigetbyte(context, slave, a++);
FMMU->nFMMU += (ecx_siigetbyte(context, slave, a++) << 8);
FMMU->nFMMU *= 2;
FMMU->FMMU0 = ecx_siigetbyte(context, slave, a++);
FMMU->FMMU1 = ecx_siigetbyte(context, slave, a++);
if (FMMU->nFMMU > 2)
{
FMMU->FMMU2 = ecx_siigetbyte(context, slave, a++);
FMMU->FMMU3 = ecx_siigetbyte(context, slave, a++);
}
}
if (eectl)
{
ecx_eeprom2pdi(context, slave); /* if eeprom control was previously pdi then restore */
}
return FMMU->nFMMU;
}
/** Get SM data from SII SM section in slave EEPROM.
* @param[in] context = context struct
* @param[in] slave = slave number
* @param[out] SM = first SM struct from SII
* @return number of SM's defined in section
*/
uint16 ecx_siiSM(ecx_contextt *context, uint16 slave, ec_eepromSMt* SM)
{
uint16 a,w;
uint8 eectl = context->slavelist[slave].eep_pdi;
SM->nSM = 0;
SM->Startpos = ecx_siifind(context, slave, ECT_SII_SM);
if (SM->Startpos > 0)
{
a = SM->Startpos;
w = ecx_siigetbyte(context, slave, a++);
w += (ecx_siigetbyte(context, slave, a++) << 8);
SM->nSM = (uint8)(w / 4);
SM->PhStart = ecx_siigetbyte(context, slave, a++);
SM->PhStart += (ecx_siigetbyte(context, slave, a++) << 8);
SM->Plength = ecx_siigetbyte(context, slave, a++);
SM->Plength += (ecx_siigetbyte(context, slave, a++) << 8);
SM->Creg = ecx_siigetbyte(context, slave, a++);
SM->Sreg = ecx_siigetbyte(context, slave, a++);
SM->Activate = ecx_siigetbyte(context, slave, a++);
SM->PDIctrl = ecx_siigetbyte(context, slave, a++);
}
if (eectl)
{
ecx_eeprom2pdi(context, slave); /* if eeprom control was previously pdi then restore */
}
return SM->nSM;
}
/** Get next SM data from SII SM section in slave EEPROM.
* @param[in] context = context struct
* @param[in] slave = slave number
* @param[out] SM = first SM struct from SII
* @param[in] n = SM number
* @return >0 if OK
*/
uint16 ecx_siiSMnext(ecx_contextt *context, uint16 slave, ec_eepromSMt* SM, uint16 n)
{
uint16 a;
uint16 retVal = 0;
uint8 eectl = context->slavelist[slave].eep_pdi;
if (n < SM->nSM)
{
a = SM->Startpos + 2 + (n * 8);
SM->PhStart = ecx_siigetbyte(context, slave, a++);
SM->PhStart += (ecx_siigetbyte(context, slave, a++) << 8);
SM->Plength = ecx_siigetbyte(context, slave, a++);
SM->Plength += (ecx_siigetbyte(context, slave, a++) << 8);
SM->Creg = ecx_siigetbyte(context, slave, a++);
SM->Sreg = ecx_siigetbyte(context, slave, a++);
SM->Activate = ecx_siigetbyte(context, slave, a++);
SM->PDIctrl = ecx_siigetbyte(context, slave, a++);
retVal = 1;
}
if (eectl)
{
ecx_eeprom2pdi(context, slave); /* if eeprom control was previously pdi then restore */
}
return retVal;
}
/** Get PDO data from SII PDO section in slave EEPROM.
* @param[in] context = context struct
* @param[in] slave = slave number
* @param[out] PDO = PDO struct from SII
* @param[in] t = 0=RXPDO 1=TXPDO
* @return mapping size in bits of PDO
*/
uint32 ecx_siiPDO(ecx_contextt *context, uint16 slave, ec_eepromPDOt* PDO, uint8 t)
{
uint16 a , w, c, e, er, Size;
uint8 eectl = context->slavelist[slave].eep_pdi;
Size = 0;
PDO->nPDO = 0;
PDO->Length = 0;
PDO->Index[1] = 0;
for (c = 0 ; c < EC_MAXSM ; c++) PDO->SMbitsize[c] = 0;
if (t > 1)
t = 1;
PDO->Startpos = ecx_siifind(context, slave, ECT_SII_PDO + t);
if (PDO->Startpos > 0)
{
a = PDO->Startpos;
w = ecx_siigetbyte(context, slave, a++);
w += (ecx_siigetbyte(context, slave, a++) << 8);
PDO->Length = w;
c = 1;
/* traverse through all PDOs */
do
{
PDO->nPDO++;
PDO->Index[PDO->nPDO] = ecx_siigetbyte(context, slave, a++);
PDO->Index[PDO->nPDO] += (ecx_siigetbyte(context, slave, a++) << 8);
PDO->BitSize[PDO->nPDO] = 0;
c++;
e = ecx_siigetbyte(context, slave, a++);
PDO->SyncM[PDO->nPDO] = ecx_siigetbyte(context, slave, a++);
a += 4;
c += 2;
if (PDO->SyncM[PDO->nPDO] < EC_MAXSM) /* active and in range SM? */
{
/* read all entries defined in PDO */
for (er = 1; er <= e; er++)
{
c += 4;
a += 5;
PDO->BitSize[PDO->nPDO] += ecx_siigetbyte(context, slave, a++);
a += 2;
}
PDO->SMbitsize[ PDO->SyncM[PDO->nPDO] ] += PDO->BitSize[PDO->nPDO];
Size += PDO->BitSize[PDO->nPDO];
c++;
}
else /* PDO deactivated because SM is 0xff or > EC_MAXSM */
{
c += 4 * e;
a += 8 * e;
c++;
}
if (PDO->nPDO >= (EC_MAXEEPDO - 1))
{
c = PDO->Length; /* limit number of PDO entries in buffer */
}
}
while (c < PDO->Length);
}
if (eectl)
{
ecx_eeprom2pdi(context, slave); /* if eeprom control was previously pdi then restore */
}
return (Size);
}
#define MAX_FPRD_MULTI 64
int ecx_FPRD_multi(ecx_contextt *context, int n, uint16 *configlst, ec_alstatust *slstatlst, int timeout)
{
int wkc;
uint8 idx;
ecx_portt *port;
uint16 sldatapos[MAX_FPRD_MULTI];
int slcnt;
port = context->port;
idx = ecx_getindex(port);
slcnt = 0;
ecx_setupdatagram(port, &(port->txbuf[idx]), EC_CMD_FPRD, idx,
*(configlst + slcnt), ECT_REG_ALSTAT, sizeof(ec_alstatust), slstatlst + slcnt);
sldatapos[slcnt] = EC_HEADERSIZE;
while(++slcnt < (n - 1))
{
sldatapos[slcnt] = ecx_adddatagram(port, &(port->txbuf[idx]), EC_CMD_FPRD, idx, TRUE,
*(configlst + slcnt), ECT_REG_ALSTAT, sizeof(ec_alstatust), slstatlst + slcnt);
}
if(slcnt < n)
{
sldatapos[slcnt] = ecx_adddatagram(port, &(port->txbuf[idx]), EC_CMD_FPRD, idx, FALSE,
*(configlst + slcnt), ECT_REG_ALSTAT, sizeof(ec_alstatust), slstatlst + slcnt);
}
wkc = ecx_srconfirm(port, idx, timeout);
if (wkc >= 0)
{
for(slcnt = 0 ; slcnt < n ; slcnt++)
{
memcpy(slstatlst + slcnt, &(port->rxbuf[idx][sldatapos[slcnt]]), sizeof(ec_alstatust));
}
}
ecx_setbufstat(port, idx, EC_BUF_EMPTY);
return wkc;
}
/** Read all slave states in ec_slave.
* @param[in] context = context struct
* @return lowest state found
*/
int ecx_readstate(ecx_contextt *context)
{
uint16 slave, fslave, lslave, configadr, lowest, rval, bitwisestate;
ec_alstatust sl[MAX_FPRD_MULTI];
uint16 slca[MAX_FPRD_MULTI];
boolean noerrorflag, allslavessamestate;
boolean allslavespresent = FALSE;
int wkc;
/* Try to establish the state of all slaves sending only one broadcast datagram.
* This way a number of datagrams equal to the number of slaves will be sent only if needed.*/
rval = 0;
wkc = ecx_BRD(context->port, 0, ECT_REG_ALSTAT, sizeof(rval), &rval, EC_TIMEOUTRET);
if(wkc >= *(context->slavecount))
{
allslavespresent = TRUE;
}
rval = etohs(rval);
bitwisestate = (rval & 0x0f);
if ((rval & EC_STATE_ERROR) == 0)
{
noerrorflag = TRUE;
context->slavelist[0].ALstatuscode = 0;
}
else
{
noerrorflag = FALSE;
}
switch (bitwisestate)
{
case EC_STATE_INIT:
case EC_STATE_PRE_OP:
case EC_STATE_BOOT:
case EC_STATE_SAFE_OP:
case EC_STATE_OPERATIONAL:
allslavessamestate = TRUE;
context->slavelist[0].state = bitwisestate;
break;
default:
allslavessamestate = FALSE;
break;
}
if (noerrorflag && allslavessamestate && allslavespresent)
{
/* No slave has toggled the error flag so the alstatuscode
* (even if different from 0) should be ignored and
* the slaves have reached the same state so the internal state
* can be updated without sending any datagram. */
for (slave = 1; slave <= *(context->slavecount); slave++)
{
context->slavelist[slave].ALstatuscode = 0x0000;
context->slavelist[slave].state = bitwisestate;
}
lowest = bitwisestate;
}
else
{
/* Not all slaves have the same state or at least one is in error so one datagram per slave
* is needed. */
context->slavelist[0].ALstatuscode = 0;
lowest = 0xff;
fslave = 1;
do
{
lslave = (uint16)*(context->slavecount);
if ((lslave - fslave) >= MAX_FPRD_MULTI)
{
lslave = fslave + MAX_FPRD_MULTI - 1;
}
for (slave = fslave; slave <= lslave; slave++)
{
const ec_alstatust zero = { 0, 0, 0 };
configadr = context->slavelist[slave].configadr;
slca[slave - fslave] = configadr;
sl[slave - fslave] = zero;
}
ecx_FPRD_multi(context, (lslave - fslave) + 1, &(slca[0]), &(sl[0]), EC_TIMEOUTRET3);
for (slave = fslave; slave <= lslave; slave++)
{
configadr = context->slavelist[slave].configadr;
rval = etohs(sl[slave - fslave].alstatus);
context->slavelist[slave].ALstatuscode = etohs(sl[slave - fslave].alstatuscode);
if ((rval & 0xf) < lowest)
{
lowest = (rval & 0xf);
}
context->slavelist[slave].state = rval;
context->slavelist[0].ALstatuscode |= context->slavelist[slave].ALstatuscode;
}
fslave = lslave + 1;
} while (lslave < *(context->slavecount));
context->slavelist[0].state = lowest;
}
return lowest;
}
/** Write slave state, if slave = 0 then write to all slaves.
* The function does not check if the actual state is changed.
* @param[in] context = context struct
* @param[in] slave = Slave number, 0 = master
* @return Workcounter or EC_NOFRAME
*/
int ecx_writestate(ecx_contextt *context, uint16 slave)
{
int ret;
uint16 configadr, slstate;
if (slave == 0)
{
slstate = htoes(context->slavelist[slave].state);
ret = ecx_BWR(context->port, 0, ECT_REG_ALCTL, sizeof(slstate),
&slstate, EC_TIMEOUTRET3);
}
else
{
configadr = context->slavelist[slave].configadr;
ret = ecx_FPWRw(context->port, configadr, ECT_REG_ALCTL,
htoes(context->slavelist[slave].state), EC_TIMEOUTRET3);
}
return ret;
}
/** Check actual slave state.
* This is a blocking function.
* To refresh the state of all slaves ecx_readstate()should be called
* @param[in] context = context struct
* @param[in] slave = Slave number, 0 = all slaves (only the "slavelist[0].state" is refreshed)
* @param[in] reqstate = Requested state
* @param[in] timeout = Timeout value in us
* @return Requested state, or found state after timeout.
*/
uint16 ecx_statecheck(ecx_contextt *context, uint16 slave, uint16 reqstate, int timeout)
{
uint16 configadr, state, rval;
ec_alstatust slstat;
osal_timert timer;
if ( slave > *(context->slavecount) )
{
return 0;
}
osal_timer_start(&timer, timeout);
configadr = context->slavelist[slave].configadr;
do
{
if (slave < 1)
{
rval = 0;
ecx_BRD(context->port, 0, ECT_REG_ALSTAT, sizeof(rval), &rval , EC_TIMEOUTRET);
rval = etohs(rval);
}
else
{
slstat.alstatus = 0;
slstat.alstatuscode = 0;
ecx_FPRD(context->port, configadr, ECT_REG_ALSTAT, sizeof(slstat), &slstat, EC_TIMEOUTRET);
rval = etohs(slstat.alstatus);
context->slavelist[slave].ALstatuscode = etohs(slstat.alstatuscode);
}
state = rval & 0x000f; /* read slave status */
if (state != reqstate)
{
osal_usleep(1000);
}
}
while ((state != reqstate) && (osal_timer_is_expired(&timer) == FALSE));
context->slavelist[slave].state = rval;
return state;
}
/** Get index of next mailbox counter value.
* Used for Mailbox Link Layer.
* @param[in] cnt = Mailbox counter value [0..7]
* @return next mailbox counter value
*/
uint8 ec_nextmbxcnt(uint8 cnt)
{
cnt++;
if (cnt > 7)
{
cnt = 1; /* wrap around to 1, not 0 */
}
return cnt;
}
/** Clear mailbox buffer.
* @param[out] Mbx = Mailbox buffer to clear
*/
void ec_clearmbx(ec_mbxbuft *Mbx)
{
memset(Mbx, 0x00, EC_MAXMBX);
}
/** Check if IN mailbox of slave is empty.
* @param[in] context = context struct
* @param[in] slave = Slave number
* @param[in] timeout = Timeout in us
* @return >0 is success
*/
int ecx_mbxempty(ecx_contextt *context, uint16 slave, int timeout)
{
uint16 configadr;
uint8 SMstat;
int wkc;
osal_timert timer;
osal_timer_start(&timer, timeout);
configadr = context->slavelist[slave].configadr;
do
{
SMstat = 0;
wkc = ecx_FPRD(context->port, configadr, ECT_REG_SM0STAT, sizeof(SMstat), &SMstat, EC_TIMEOUTRET);
SMstat = etohs(SMstat);
if (((SMstat & 0x08) != 0) && (timeout > EC_LOCALDELAY))
{
osal_usleep(EC_LOCALDELAY);
}
}
while (((wkc <= 0) || ((SMstat & 0x08) != 0)) && (osal_timer_is_expired(&timer) == FALSE));
if ((wkc > 0) && ((SMstat & 0x08) == 0))
{
return 1;
}
return 0;
}
/** Write IN mailbox to slave.
* @param[in] context = context struct
* @param[in] slave = Slave number
* @param[out] mbx = Mailbox data
* @param[in] timeout = Timeout in us
* @return Work counter (>0 is success)
*/
int ecx_mbxsend(ecx_contextt *context, uint16 slave,ec_mbxbuft *mbx, int timeout)
{
uint16 mbxwo,mbxl,configadr;
int wkc;
wkc = 0;
configadr = context->slavelist[slave].configadr;
mbxl = context->slavelist[slave].mbx_l;
if ((mbxl > 0) && (mbxl <= EC_MAXMBX))
{
if (ecx_mbxempty(context, slave, timeout))
{
mbxwo = context->slavelist[slave].mbx_wo;
/* write slave in mailbox */
wkc = ecx_FPWR(context->port, configadr, mbxwo, mbxl, mbx, EC_TIMEOUTRET3);
}
else
{
wkc = 0;
}
}
return wkc;
}
/** Read OUT mailbox from slave.
* Supports Mailbox Link Layer with repeat requests.
* @param[in] context = context struct
* @param[in] slave = Slave number
* @param[out] mbx = Mailbox data
* @param[in] timeout = Timeout in us
* @return Work counter (>0 is success)
*/
int ecx_mbxreceive(ecx_contextt *context, uint16 slave, ec_mbxbuft *mbx, int timeout)