forked from OpenEtherCATsociety/SOEM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethercatcoe.c
1505 lines (1445 loc) · 55.1 KB
/
ethercatcoe.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
* CAN over EtherCAT (CoE) module.
*
* SDO read / write and SDO service functions
*/
#include <stdio.h>
#include <string.h>
#include "osal.h"
#include "oshw.h"
#include "ethercattype.h"
#include "ethercatbase.h"
#include "ethercatmain.h"
#include "ethercatcoe.h"
/** SDO structure, not to be confused with EcSDOserviceT */
PACKED_BEGIN
typedef struct PACKED
{
ec_mbxheadert MbxHeader;
uint16 CANOpen;
uint8 Command;
uint16 Index;
uint8 SubIndex;
union
{
uint8 bdata[0x200]; /* variants for easy data access */
uint16 wdata[0x100];
uint32 ldata[0x80];
};
} ec_SDOt;
PACKED_END
/** SDO service structure */
PACKED_BEGIN
typedef struct PACKED
{
ec_mbxheadert MbxHeader;
uint16 CANOpen;
uint8 Opcode;
uint8 Reserved;
uint16 Fragments;
union
{
uint8 bdata[0x200]; /* variants for easy data access */
uint16 wdata[0x100];
uint32 ldata[0x80];
};
} ec_SDOservicet;
PACKED_END
/** Report SDO 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] AbortCode = Abortcode, see EtherCAT documentation for list
*/
void ecx_SDOerror(ecx_contextt *context, uint16 Slave, uint16 Index, uint8 SubIdx, int32 AbortCode)
{
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_SDO_ERROR;
Ec.AbortCode = AbortCode;
ecx_pusherror(context, &Ec);
}
/** Report SDO info 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] AbortCode = Abortcode, see EtherCAT documentation for list
*/
static void ecx_SDOinfoerror(ecx_contextt *context, uint16 Slave, uint16 Index, uint8 SubIdx, int32 AbortCode)
{
ec_errort Ec;
memset(&Ec, 0, sizeof(Ec));
Ec.Slave = Slave;
Ec.Index = Index;
Ec.SubIdx = SubIdx;
*(context->ecaterror) = TRUE;
Ec.Etype = EC_ERR_TYPE_SDOINFO_ERROR;
Ec.AbortCode = AbortCode;
ecx_pusherror(context, &Ec);
}
/** CoE SDO read, blocking. Single subindex or Complete Access.
*
* Only a "normal" upload request is issued. If the requested parameter is <= 4bytes
* then a "expedited" response is returned, otherwise a "normal" response. If a "normal"
* response is larger than the mailbox size then the response is segmented. The function
* will combine all segments and copy them to the parameter buffer.
*
* @param[in] context = context struct
* @param[in] slave = Slave number
* @param[in] index = Index to read
* @param[in] subindex = Subindex to read, must be 0 or 1 if CA is used.
* @param[in] CA = FALSE = single subindex. TRUE = Complete Access, all subindexes read.
* @param[in,out] psize = Size in bytes of parameter buffer, returns bytes read from SDO.
* @param[out] p = Pointer to parameter buffer
* @param[in] timeout = Timeout in us, standard is EC_TIMEOUTRXM
* @return Workcounter from last slave response
*/
int ecx_SDOread(ecx_contextt *context, uint16 slave, uint16 index, uint8 subindex,
boolean CA, int *psize, void *p, int timeout)
{
ec_SDOt *SDOp, *aSDOp;
uint16 bytesize, Framedatasize;
int wkc;
int32 SDOlen;
uint8 *bp;
uint8 *hp;
ec_mbxbuft MbxIn, MbxOut;
uint8 cnt, toggle;
boolean NotLast;
ec_clearmbx(&MbxIn);
/* Empty slave out mailbox if something is in. Timeout set to 0 */
wkc = ecx_mbxreceive(context, slave, (ec_mbxbuft *)&MbxIn, 0);
ec_clearmbx(&MbxOut);
aSDOp = (ec_SDOt *)&MbxIn;
SDOp = (ec_SDOt *)&MbxOut;
SDOp->MbxHeader.length = htoes(0x000a);
SDOp->MbxHeader.address = htoes(0x0000);
SDOp->MbxHeader.priority = 0x00;
/* get new mailbox count value, used as session handle */
cnt = ec_nextmbxcnt(context->slavelist[slave].mbx_cnt);
context->slavelist[slave].mbx_cnt = cnt;
SDOp->MbxHeader.mbxtype = ECT_MBXT_COE + MBX_HDR_SET_CNT(cnt); /* CoE */
SDOp->CANOpen = htoes(0x000 + (ECT_COES_SDOREQ << 12)); /* number 9bits service upper 4 bits (SDO request) */
if (CA)
{
SDOp->Command = ECT_SDO_UP_REQ_CA; /* upload request complete access */
}
else
{
SDOp->Command = ECT_SDO_UP_REQ; /* upload request normal */
}
SDOp->Index = htoes(index);
if (CA && (subindex > 1))
{
subindex = 1;
}
SDOp->SubIndex = subindex;
SDOp->ldata[0] = 0;
/* send CoE request to slave */
wkc = ecx_mbxsend(context, slave, (ec_mbxbuft *)&MbxOut, EC_TIMEOUTTXM);
if (wkc > 0) /* succeeded to place mailbox in slave ? */
{
/* clean mailboxbuffer */
ec_clearmbx(&MbxIn);
/* read slave response */
wkc = ecx_mbxreceive(context, slave, (ec_mbxbuft *)&MbxIn, timeout);
if (wkc > 0) /* succeeded to read slave response ? */
{
/* slave response should be CoE, SDO response and the correct index */
if (((aSDOp->MbxHeader.mbxtype & 0x0f) == ECT_MBXT_COE) &&
((etohs(aSDOp->CANOpen) >> 12) == ECT_COES_SDORES) &&
(aSDOp->Index == SDOp->Index))
{
if ((aSDOp->Command & 0x02) > 0)
{
/* expedited frame response */
bytesize = 4 - ((aSDOp->Command >> 2) & 0x03);
if (*psize >= bytesize) /* parameter buffer big enough ? */
{
/* copy parameter in parameter buffer */
memcpy(p, &aSDOp->ldata[0], bytesize);
/* return the real parameter size */
*psize = bytesize;
}
else
{
wkc = 0;
ecx_packeterror(context, slave, index, subindex, 3); /* data container too small for type */
}
}
else
{ /* normal frame response */
SDOlen = etohl(aSDOp->ldata[0]);
/* Does parameter fit in parameter buffer ? */
if (SDOlen <= *psize)
{
bp = p;
hp = p;
/* calculate mailbox transfer size */
Framedatasize = (etohs(aSDOp->MbxHeader.length) - 10);
if (Framedatasize < SDOlen) /* transfer in segments? */
{
/* copy parameter data in parameter buffer */
memcpy(hp, &aSDOp->ldata[1], Framedatasize);
/* increment buffer pointer */
hp += Framedatasize;
*psize = Framedatasize;
NotLast = TRUE;
toggle= 0x00;
while (NotLast) /* segmented transfer */
{
SDOp = (ec_SDOt *)&MbxOut;
SDOp->MbxHeader.length = htoes(0x000a);
SDOp->MbxHeader.address = htoes(0x0000);
SDOp->MbxHeader.priority = 0x00;
cnt = ec_nextmbxcnt(context->slavelist[slave].mbx_cnt);
context->slavelist[slave].mbx_cnt = cnt;
SDOp->MbxHeader.mbxtype = ECT_MBXT_COE + MBX_HDR_SET_CNT(cnt); /* CoE */
SDOp->CANOpen = htoes(0x000 + (ECT_COES_SDOREQ << 12)); /* number 9bits service upper 4 bits (SDO request) */
SDOp->Command = ECT_SDO_SEG_UP_REQ + toggle; /* segment upload request */
SDOp->Index = htoes(index);
SDOp->SubIndex = subindex;
SDOp->ldata[0] = 0;
/* send segmented upload request to slave */
wkc = ecx_mbxsend(context, slave, (ec_mbxbuft *)&MbxOut, EC_TIMEOUTTXM);
/* is mailbox transferred to slave ? */
if (wkc > 0)
{
ec_clearmbx(&MbxIn);
/* read slave response */
wkc = ecx_mbxreceive(context, slave, (ec_mbxbuft *)&MbxIn, timeout);
/* has slave responded ? */
if (wkc > 0)
{
/* slave response should be CoE, SDO response */
if ((((aSDOp->MbxHeader.mbxtype & 0x0f) == ECT_MBXT_COE) &&
((etohs(aSDOp->CANOpen) >> 12) == ECT_COES_SDORES) &&
((aSDOp->Command & 0xe0) == 0x00)))
{
/* calculate mailbox transfer size */
Framedatasize = etohs(aSDOp->MbxHeader.length) - 3;
if ((aSDOp->Command & 0x01) > 0)
{ /* last segment */
NotLast = FALSE;
if (Framedatasize == 7)
/* subtract unused bytes from frame */
Framedatasize = Framedatasize - ((aSDOp->Command & 0x0e) >> 1);
/* copy to parameter buffer */
memcpy(hp, &(aSDOp->Index), Framedatasize);
}
else /* segments follow */
{
/* copy to parameter buffer */
memcpy(hp, &(aSDOp->Index), Framedatasize);
/* increment buffer pointer */
hp += Framedatasize;
}
/* update parameter size */
*psize += Framedatasize;
}
/* unexpected frame returned from slave */
else
{
NotLast = FALSE;
if ((aSDOp->Command) == ECT_SDO_ABORT) /* SDO abort frame received */
ecx_SDOerror(context, slave, index, subindex, etohl(aSDOp->ldata[0]));
else
ecx_packeterror(context, slave, index, subindex, 1); /* Unexpected frame returned */
wkc = 0;
}
}
}
toggle = toggle ^ 0x10; /* toggle bit for segment request */
}
}
/* non segmented transfer */
else
{
/* copy to parameter buffer */
memcpy(bp, &aSDOp->ldata[1], SDOlen);
*psize = SDOlen;
}
}
/* parameter buffer too small */
else
{
wkc = 0;
ecx_packeterror(context, slave, index, subindex, 3); /* data container too small for type */
}
}
}
/* other slave response */
else
{
if ((aSDOp->Command) == ECT_SDO_ABORT) /* SDO abort frame received */
{
ecx_SDOerror(context, slave, index, subindex, etohl(aSDOp->ldata[0]));
}
else
{
ecx_packeterror(context, slave, index, subindex, 1); /* Unexpected frame returned */
}
wkc = 0;
}
}
}
return wkc;
}
/** CoE SDO write, blocking. Single subindex or Complete Access.
*
* A "normal" download request is issued, unless we have
* small data, then a "expedited" transfer is used. If the parameter is larger than
* the mailbox size then the download is segmented. The function will split the
* parameter data in segments and send them to the slave one by one.
*
* @param[in] context = context struct
* @param[in] Slave = Slave number
* @param[in] Index = Index to write
* @param[in] SubIndex = Subindex to write, must be 0 or 1 if CA is used.
* @param[in] CA = FALSE = single subindex. TRUE = Complete Access, all subindexes written.
* @param[in] psize = Size in bytes of parameter buffer.
* @param[out] p = Pointer to parameter buffer
* @param[in] Timeout = Timeout in us, standard is EC_TIMEOUTRXM
* @return Workcounter from last slave response
*/
int ecx_SDOwrite(ecx_contextt *context, uint16 Slave, uint16 Index, uint8 SubIndex,
boolean CA, int psize, void *p, int Timeout)
{
ec_SDOt *SDOp, *aSDOp;
int wkc, maxdata, framedatasize;
ec_mbxbuft MbxIn, MbxOut;
uint8 cnt, toggle;
boolean NotLast;
uint8 *hp;
ec_clearmbx(&MbxIn);
/* Empty slave out mailbox if something is in. Timeout set to 0 */
wkc = ecx_mbxreceive(context, Slave, (ec_mbxbuft *)&MbxIn, 0);
ec_clearmbx(&MbxOut);
aSDOp = (ec_SDOt *)&MbxIn;
SDOp = (ec_SDOt *)&MbxOut;
maxdata = context->slavelist[Slave].mbx_l - 0x10; /* data section=mailbox size - 6 mbx - 2 CoE - 8 sdo req */
/* if small data use expedited transfer */
if ((psize <= 4) && !CA)
{
SDOp->MbxHeader.length = htoes(0x000a);
SDOp->MbxHeader.address = htoes(0x0000);
SDOp->MbxHeader.priority = 0x00;
/* get new mailbox counter, used for session handle */
cnt = ec_nextmbxcnt(context->slavelist[Slave].mbx_cnt);
context->slavelist[Slave].mbx_cnt = cnt;
SDOp->MbxHeader.mbxtype = ECT_MBXT_COE + MBX_HDR_SET_CNT(cnt); /* CoE */
SDOp->CANOpen = htoes(0x000 + (ECT_COES_SDOREQ << 12)); /* number 9bits service upper 4 bits */
SDOp->Command = ECT_SDO_DOWN_EXP | (((4 - psize) << 2) & 0x0c); /* expedited SDO download transfer */
SDOp->Index = htoes(Index);
SDOp->SubIndex = SubIndex;
hp = p;
/* copy parameter data to mailbox */
memcpy(&SDOp->ldata[0], hp, psize);
/* send mailbox SDO download request to slave */
wkc = ecx_mbxsend(context, Slave, (ec_mbxbuft *)&MbxOut, EC_TIMEOUTTXM);
if (wkc > 0)
{
ec_clearmbx(&MbxIn);
/* read slave response */
wkc = ecx_mbxreceive(context, Slave, (ec_mbxbuft *)&MbxIn, Timeout);
if (wkc > 0)
{
/* response should be CoE, SDO response, correct index and subindex */
if (((aSDOp->MbxHeader.mbxtype & 0x0f) == ECT_MBXT_COE) &&
((etohs(aSDOp->CANOpen) >> 12) == ECT_COES_SDORES) &&
(aSDOp->Index == SDOp->Index) &&
(aSDOp->SubIndex == SDOp->SubIndex))
{
/* all OK */
}
/* unexpected response from slave */
else
{
if (aSDOp->Command == ECT_SDO_ABORT) /* SDO abort frame received */
{
ecx_SDOerror(context, Slave, Index, SubIndex, etohl(aSDOp->ldata[0]));
}
else
{
ecx_packeterror(context, Slave, Index, SubIndex, 1); /* Unexpected frame returned */
}
wkc = 0;
}
}
}
}
else
{
framedatasize = psize;
NotLast = FALSE;
if (framedatasize > maxdata)
{
framedatasize = maxdata; /* segmented transfer needed */
NotLast = TRUE;
}
SDOp->MbxHeader.length = htoes((uint16)(0x0a + framedatasize));
SDOp->MbxHeader.address = htoes(0x0000);
SDOp->MbxHeader.priority = 0x00;
/* get new mailbox counter, used for session handle */
cnt = ec_nextmbxcnt(context->slavelist[Slave].mbx_cnt);
context->slavelist[Slave].mbx_cnt = cnt;
SDOp->MbxHeader.mbxtype = ECT_MBXT_COE + MBX_HDR_SET_CNT(cnt); /* CoE */
SDOp->CANOpen = htoes(0x000 + (ECT_COES_SDOREQ << 12)); /* number 9bits service upper 4 bits */
if (CA)
{
SDOp->Command = ECT_SDO_DOWN_INIT_CA; /* Complete Access, normal SDO init download transfer */
}
else
{
SDOp->Command = ECT_SDO_DOWN_INIT; /* normal SDO init download transfer */
}
SDOp->Index = htoes(Index);
SDOp->SubIndex = SubIndex;
if (CA && (SubIndex > 1))
{
SDOp->SubIndex = 1;
}
SDOp->ldata[0] = htoel(psize);
hp = p;
/* copy parameter data to mailbox */
memcpy(&SDOp->ldata[1], hp, framedatasize);
hp += framedatasize;
psize -= framedatasize;
/* send mailbox SDO download request to slave */
wkc = ecx_mbxsend(context, Slave, (ec_mbxbuft *)&MbxOut, EC_TIMEOUTTXM);
if (wkc > 0)
{
ec_clearmbx(&MbxIn);
/* read slave response */
wkc = ecx_mbxreceive(context, Slave, (ec_mbxbuft *)&MbxIn, Timeout);
if (wkc > 0)
{
/* response should be CoE, SDO response, correct index and subindex */
if (((aSDOp->MbxHeader.mbxtype & 0x0f) == ECT_MBXT_COE) &&
((etohs(aSDOp->CANOpen) >> 12) == ECT_COES_SDORES) &&
(aSDOp->Index == SDOp->Index) &&
(aSDOp->SubIndex == SDOp->SubIndex))
{
/* all ok */
maxdata += 7;
toggle = 0;
/* repeat while segments left */
while (NotLast)
{
SDOp = (ec_SDOt *)&MbxOut;
framedatasize = psize;
NotLast = FALSE;
SDOp->Command = 0x01; /* last segment */
if (framedatasize > maxdata)
{
framedatasize = maxdata; /* more segments needed */
NotLast = TRUE;
SDOp->Command = 0x00; /* segments follow */
}
if (!NotLast && (framedatasize < 7))
{
SDOp->MbxHeader.length = htoes(0x0a); /* minimum size */
SDOp->Command = (uint8)(0x01 + ((7 - framedatasize) << 1)); /* last segment reduced octets */
}
else
{
SDOp->MbxHeader.length = htoes((uint16)(framedatasize + 3)); /* data + 2 CoE + 1 SDO */
}
SDOp->MbxHeader.address = htoes(0x0000);
SDOp->MbxHeader.priority = 0x00;
/* get new mailbox counter value */
cnt = ec_nextmbxcnt(context->slavelist[Slave].mbx_cnt);
context->slavelist[Slave].mbx_cnt = cnt;
SDOp->MbxHeader.mbxtype = ECT_MBXT_COE + MBX_HDR_SET_CNT(cnt); /* CoE */
SDOp->CANOpen = htoes(0x000 + (ECT_COES_SDOREQ << 12)); /* number 9bits service upper 4 bits (SDO request) */
SDOp->Command = SDOp->Command + toggle; /* add toggle bit to command byte */
/* copy parameter data to mailbox */
memcpy(&SDOp->Index, hp, framedatasize);
/* update parameter buffer pointer */
hp += framedatasize;
psize -= framedatasize;
/* send SDO download request */
wkc = ecx_mbxsend(context, Slave, (ec_mbxbuft *)&MbxOut, EC_TIMEOUTTXM);
if (wkc > 0)
{
ec_clearmbx(&MbxIn);
/* read slave response */
wkc = ecx_mbxreceive(context, Slave, (ec_mbxbuft *)&MbxIn, Timeout);
if (wkc > 0)
{
if (((aSDOp->MbxHeader.mbxtype & 0x0f) == ECT_MBXT_COE) &&
((etohs(aSDOp->CANOpen) >> 12) == ECT_COES_SDORES) &&
((aSDOp->Command & 0xe0) == 0x20))
{
/* all OK, nothing to do */
}
else
{
if (aSDOp->Command == ECT_SDO_ABORT) /* SDO abort frame received */
{
ecx_SDOerror(context, Slave, Index, SubIndex, etohl(aSDOp->ldata[0]));
}
else
{
ecx_packeterror(context, Slave, Index, SubIndex, 1); /* Unexpected frame returned */
}
wkc = 0;
NotLast = FALSE;
}
}
}
toggle = toggle ^ 0x10; /* toggle bit for segment request */
}
}
/* unexpected response from slave */
else
{
if (aSDOp->Command == ECT_SDO_ABORT) /* SDO abort frame received */
{
ecx_SDOerror(context, Slave, Index, SubIndex, etohl(aSDOp->ldata[0]));
}
else
{
ecx_packeterror(context, Slave, Index, SubIndex, 1); /* Unexpected frame returned */
}
wkc = 0;
}
}
}
}
return wkc;
}
/** CoE RxPDO write, blocking.
*
* A RxPDO download request is issued.
*
* @param[in] context = context struct
* @param[in] Slave = Slave number
* @param[in] RxPDOnumber = Related RxPDO number
* @param[in] psize = Size in bytes of PDO buffer.
* @param[out] p = Pointer to PDO buffer
* @return Workcounter from last slave response
*/
int ecx_RxPDO(ecx_contextt *context, uint16 Slave, uint16 RxPDOnumber, int psize, void *p)
{
ec_SDOt *SDOp;
int wkc, maxdata, framedatasize;
ec_mbxbuft MbxIn, MbxOut;
uint8 cnt;
ec_clearmbx(&MbxIn);
/* Empty slave out mailbox if something is in. Timeout set to 0 */
wkc = ecx_mbxreceive(context, Slave, (ec_mbxbuft *)&MbxIn, 0);
ec_clearmbx(&MbxOut);
SDOp = (ec_SDOt *)&MbxOut;
maxdata = context->slavelist[Slave].mbx_l - 0x08; /* data section=mailbox size - 6 mbx - 2 CoE */
framedatasize = psize;
if (framedatasize > maxdata)
{
framedatasize = maxdata; /* limit transfer */
}
SDOp->MbxHeader.length = htoes((uint16)(0x02 + framedatasize));
SDOp->MbxHeader.address = htoes(0x0000);
SDOp->MbxHeader.priority = 0x00;
/* get new mailbox counter, used for session handle */
cnt = ec_nextmbxcnt(context->slavelist[Slave].mbx_cnt);
context->slavelist[Slave].mbx_cnt = cnt;
SDOp->MbxHeader.mbxtype = ECT_MBXT_COE + MBX_HDR_SET_CNT(cnt); /* CoE */
SDOp->CANOpen = htoes((RxPDOnumber & 0x01ff) + (ECT_COES_RXPDO << 12)); /* number 9bits service upper 4 bits */
/* copy PDO data to mailbox */
memcpy(&SDOp->Command, p, framedatasize);
/* send mailbox RxPDO request to slave */
wkc = ecx_mbxsend(context, Slave, (ec_mbxbuft *)&MbxOut, EC_TIMEOUTTXM);
return wkc;
}
/** CoE TxPDO read remote request, blocking.
*
* A RxPDO download request is issued.
*
* @param[in] context = context struct
* @param[in] slave = Slave number
* @param[in] TxPDOnumber = Related TxPDO number
* @param[in,out] psize = Size in bytes of PDO buffer, returns bytes read from PDO.
* @param[out] p = Pointer to PDO buffer
* @param[in] timeout = Timeout in us, standard is EC_TIMEOUTRXM
* @return Workcounter from last slave response
*/
int ecx_TxPDO(ecx_contextt *context, uint16 slave, uint16 TxPDOnumber , int *psize, void *p, int timeout)
{
ec_SDOt *SDOp, *aSDOp;
int wkc;
ec_mbxbuft MbxIn, MbxOut;
uint8 cnt;
uint16 framedatasize;
ec_clearmbx(&MbxIn);
/* Empty slave out mailbox if something is in. Timeout set to 0 */
wkc = ecx_mbxreceive(context, slave, (ec_mbxbuft *)&MbxIn, 0);
ec_clearmbx(&MbxOut);
aSDOp = (ec_SDOt *)&MbxIn;
SDOp = (ec_SDOt *)&MbxOut;
SDOp->MbxHeader.length = htoes(0x02);
SDOp->MbxHeader.address = htoes(0x0000);
SDOp->MbxHeader.priority = 0x00;
/* get new mailbox counter, used for session handle */
cnt = ec_nextmbxcnt(context->slavelist[slave].mbx_cnt);
context->slavelist[slave].mbx_cnt = cnt;
SDOp->MbxHeader.mbxtype = ECT_MBXT_COE + MBX_HDR_SET_CNT(cnt); /* CoE */
SDOp->CANOpen = htoes((TxPDOnumber & 0x01ff) + (ECT_COES_TXPDO_RR << 12)); /* number 9bits service upper 4 bits */
wkc = ecx_mbxsend(context, slave, (ec_mbxbuft *)&MbxOut, EC_TIMEOUTTXM);
if (wkc > 0)
{
/* clean mailboxbuffer */
ec_clearmbx(&MbxIn);
/* read slave response */
wkc = ecx_mbxreceive(context, slave, (ec_mbxbuft *)&MbxIn, timeout);
if (wkc > 0) /* succeeded to read slave response ? */
{
/* slave response should be CoE, TxPDO */
if (((aSDOp->MbxHeader.mbxtype & 0x0f) == ECT_MBXT_COE) &&
((etohs(aSDOp->CANOpen) >> 12) == ECT_COES_TXPDO))
{
/* TxPDO response */
framedatasize = (aSDOp->MbxHeader.length - 2);
if (*psize >= framedatasize) /* parameter buffer big enough ? */
{
/* copy parameter in parameter buffer */
memcpy(p, &aSDOp->Command, framedatasize);
/* return the real parameter size */
*psize = framedatasize;
}
/* parameter buffer too small */
else
{
wkc = 0;
ecx_packeterror(context, slave, 0, 0, 3); /* data container too small for type */
}
}
/* other slave response */
else
{
if ((aSDOp->Command) == ECT_SDO_ABORT) /* SDO abort frame received */
{
ecx_SDOerror(context, slave, 0, 0, etohl(aSDOp->ldata[0]));
}
else
{
ecx_packeterror(context, slave, 0, 0, 1); /* Unexpected frame returned */
}
wkc = 0;
}
}
}
return wkc;
}
/** Read PDO assign structure
* @param[in] context = context struct
* @param[in] Slave = Slave number
* @param[in] PDOassign = PDO assign object
* @return total bitlength of PDO assign
*/
uint32 ecx_readPDOassign(ecx_contextt *context, uint16 Slave, uint16 PDOassign)
{
uint16 idxloop, nidx, subidxloop, rdat, idx, subidx;
uint8 subcnt;
int wkc, rdl;
int32 rdat2;
uint32 bsize = 0;
rdl = sizeof(rdat); rdat = 0;
/* read PDO assign subindex 0 ( = number of PDO's) */
wkc = ecx_SDOread(context, Slave, PDOassign, 0x00, FALSE, &rdl, &rdat, EC_TIMEOUTRXM);
rdat = etohs(rdat);
/* positive result from slave ? */
if ((wkc > 0) && (rdat > 0))
{
/* number of available sub indexes */
nidx = rdat;
bsize = 0;
/* read all PDO's */
for (idxloop = 1; idxloop <= nidx; idxloop++)
{
rdl = sizeof(rdat); rdat = 0;
/* read PDO assign */
wkc = ecx_SDOread(context, Slave, PDOassign, (uint8)idxloop, FALSE, &rdl, &rdat, EC_TIMEOUTRXM);
/* result is index of PDO */
idx = etohs(rdat);
if (idx > 0)
{
rdl = sizeof(subcnt); subcnt = 0;
/* read number of subindexes of PDO */
wkc = ecx_SDOread(context, Slave,idx, 0x00, FALSE, &rdl, &subcnt, EC_TIMEOUTRXM);
subidx = subcnt;
/* for each subindex */
for (subidxloop = 1; subidxloop <= subidx; subidxloop++)
{
rdl = sizeof(rdat2); rdat2 = 0;
/* read SDO that is mapped in PDO */
wkc = ecx_SDOread(context, Slave, idx, (uint8)subidxloop, FALSE, &rdl, &rdat2, EC_TIMEOUTRXM);
rdat2 = etohl(rdat2);
/* extract bitlength of SDO */
if (LO_BYTE(rdat2) < 0xff)
{
bsize += LO_BYTE(rdat2);
}
else
{
rdl = sizeof(rdat); rdat = htoes(0xff);
/* read Object Entry in Object database */
// wkc = ec_readOEsingle(idx, (uint8)SubCount, pODlist, pOElist);
bsize += etohs(rdat);
}
}
}
}
}
/* return total found bitlength (PDO) */
return bsize;
}
/** Read PDO assign structure in Complete Access mode
* @param[in] context = context struct
* @param[in] Slave = Slave number
* @param[in] Thread_n = Calling thread index
* @param[in] PDOassign = PDO assign object
* @return total bitlength of PDO assign
*/
uint32 ecx_readPDOassignCA(ecx_contextt *context, uint16 Slave, int Thread_n,
uint16 PDOassign)
{
uint16 idxloop, nidx, subidxloop, idx, subidx;
int wkc, rdl;
uint32 bsize = 0;
/* find maximum size of PDOassign buffer */
rdl = sizeof(ec_PDOassignt);
context->PDOassign[Thread_n].n=0;
/* read rxPDOassign in CA mode, all subindexes are read in one struct */
wkc = ecx_SDOread(context, Slave, PDOassign, 0x00, TRUE, &rdl,
&(context->PDOassign[Thread_n]), EC_TIMEOUTRXM);
/* positive result from slave ? */
if ((wkc > 0) && (context->PDOassign[Thread_n].n > 0))
{
nidx = context->PDOassign[Thread_n].n;
bsize = 0;
/* for each PDO do */
for (idxloop = 1; idxloop <= nidx; idxloop++)
{
/* get index from PDOassign struct */
idx = etohs(context->PDOassign[Thread_n].index[idxloop - 1]);
if (idx > 0)
{
rdl = sizeof(ec_PDOdesct); context->PDOdesc[Thread_n].n = 0;
/* read SDO's that are mapped in PDO, CA mode */
wkc = ecx_SDOread(context, Slave,idx, 0x00, TRUE, &rdl,
&(context->PDOdesc[Thread_n]), EC_TIMEOUTRXM);
subidx = context->PDOdesc[Thread_n].n;
/* extract all bitlengths of SDO's */
for (subidxloop = 1; subidxloop <= subidx; subidxloop++)
{
bsize += LO_BYTE(etohl(context->PDOdesc[Thread_n].PDO[subidxloop -1]));
}
}
}
}
/* return total found bitlength (PDO) */
return bsize;
}
/** CoE read PDO mapping.
*
* CANopen has standard indexes defined for PDO mapping. This function
* tries to read them and collect a full input and output mapping size
* of designated slave.
*
* Principal structure in slave:\n
* 1C00:00 is number of SM defined\n
* 1C00:01 SM0 type -> 1C10\n
* 1C00:02 SM1 type -> 1C11\n
* 1C00:03 SM2 type -> 1C12\n
* 1C00:04 SM3 type -> 1C13\n
* Type 0 = unused, 1 = mailbox in, 2 = mailbox out,
* 3 = outputs (RxPDO), 4 = inputs (TxPDO).
*
* 1C12:00 is number of PDO's defined for SM2\n
* 1C12:01 PDO assign SDO #1 -> f.e. 1A00\n
* 1C12:02 PDO assign SDO #2 -> f.e. 1A04\
*
* 1A00:00 is number of object defined for this PDO\n
* 1A00:01 object mapping #1, f.e. 60100710 (SDO 6010 SI 07 bitlength 0x10)
*
* @param[in] context = context struct
* @param[in] Slave = Slave number
* @param[out] Osize = Size in bits of output mapping (rxPDO) found
* @param[out] Isize = Size in bits of input mapping (txPDO) found
* @return >0 if mapping successful.
*/
int ecx_readPDOmap(ecx_contextt *context, uint16 Slave, uint32 *Osize, uint32 *Isize)
{
int wkc, rdl;
int retVal = 0;
uint8 nSM, iSM, tSM;
uint32 Tsize;
uint8 SMt_bug_add;
*Isize = 0;
*Osize = 0;
SMt_bug_add = 0;
rdl = sizeof(nSM); nSM = 0;
/* read SyncManager Communication Type object count */
wkc = ecx_SDOread(context, Slave, ECT_SDO_SMCOMMTYPE, 0x00, FALSE, &rdl, &nSM, EC_TIMEOUTRXM);
/* positive result from slave ? */
if ((wkc > 0) && (nSM > 2))
{
/* limit to maximum number of SM defined, if true the slave can't be configured */
if (nSM > EC_MAXSM)
nSM = EC_MAXSM;
/* iterate for every SM type defined */
for (iSM = 2 ; iSM < nSM ; iSM++)
{
rdl = sizeof(tSM); tSM = 0;
/* read SyncManager Communication Type */
wkc = ecx_SDOread(context, Slave, ECT_SDO_SMCOMMTYPE, iSM + 1, FALSE, &rdl, &tSM, EC_TIMEOUTRXM);
if (wkc > 0)
{
// start slave bug prevention code, remove if possible
if((iSM == 2) && (tSM == 2)) // SM2 has type 2 == mailbox out, this is a bug in the slave!
{
SMt_bug_add = 1; // try to correct, this works if the types are 0 1 2 3 and should be 1 2 3 4
}
if(tSM)
{
tSM += SMt_bug_add; // only add if SMt > 0
}
if((iSM == 2) && (tSM == 0)) // SM2 has type 0, this is a bug in the slave!
{
tSM = 3;
}
if((iSM == 3) && (tSM == 0)) // SM3 has type 0, this is a bug in the slave!
{
tSM = 4;
}
// end slave bug prevention code
context->slavelist[Slave].SMtype[iSM] = tSM;
/* check if SM is unused -> clear enable flag */
if (tSM == 0)
{
context->slavelist[Slave].SM[iSM].SMflags =
htoel( etohl(context->slavelist[Slave].SM[iSM].SMflags) & EC_SMENABLEMASK);
}
if ((tSM == 3) || (tSM == 4))
{
/* read the assign PDO */
Tsize = ecx_readPDOassign(context, Slave, ECT_SDO_PDOASSIGN + iSM );
/* if a mapping is found */
if (Tsize)
{
context->slavelist[Slave].SM[iSM].SMlength = htoes((uint16)((Tsize + 7) / 8));
if (tSM == 3)
{
/* we are doing outputs */
*Osize += Tsize;
}
else
{
/* we are doing inputs */
*Isize += Tsize;
}
}
}
}
}
}
/* found some I/O bits ? */
if ((*Isize > 0) || (*Osize > 0))
{
retVal = 1;
}
return retVal;
}
/** CoE read PDO mapping in Complete Access mode (CA).
*
* CANopen has standard indexes defined for PDO mapping. This function
* tries to read them and collect a full input and output mapping size
* of designated slave. Slave has to support CA, otherwise use ec_readPDOmap().
*
* @param[in] context = context struct
* @param[in] Slave = Slave number
* @param[in] Thread_n = Calling thread index
* @param[out] Osize = Size in bits of output mapping (rxPDO) found
* @param[out] Isize = Size in bits of input mapping (txPDO) found
* @return >0 if mapping successful.
*/
int ecx_readPDOmapCA(ecx_contextt *context, uint16 Slave, int Thread_n, uint32 *Osize, uint32 *Isize)
{
int wkc, rdl;
int retVal = 0;
uint8 nSM, iSM, tSM;
uint32 Tsize;
uint8 SMt_bug_add;
*Isize = 0;
*Osize = 0;
SMt_bug_add = 0;
rdl = sizeof(ec_SMcommtypet);
context->SMcommtype[Thread_n].n = 0;
/* read SyncManager Communication Type object count Complete Access*/
wkc = ecx_SDOread(context, Slave, ECT_SDO_SMCOMMTYPE, 0x00, TRUE, &rdl,
&(context->SMcommtype[Thread_n]), EC_TIMEOUTRXM);
/* positive result from slave ? */
if ((wkc > 0) && (context->SMcommtype[Thread_n].n > 2))
{
nSM = context->SMcommtype[Thread_n].n;
/* limit to maximum number of SM defined, if true the slave can't be configured */
if (nSM > EC_MAXSM)
{
nSM = EC_MAXSM;
ecx_packeterror(context, Slave, 0, 0, 10); /* #SM larger than EC_MAXSM */
}
/* iterate for every SM type defined */
for (iSM = 2 ; iSM < nSM ; iSM++)
{
tSM = context->SMcommtype[Thread_n].SMtype[iSM];
// start slave bug prevention code, remove if possible
if((iSM == 2) && (tSM == 2)) // SM2 has type 2 == mailbox out, this is a bug in the slave!
{
SMt_bug_add = 1; // try to correct, this works if the types are 0 1 2 3 and should be 1 2 3 4
}
if(tSM)
{
tSM += SMt_bug_add; // only add if SMt > 0
}
// end slave bug prevention code
context->slavelist[Slave].SMtype[iSM] = tSM;
/* check if SM is unused -> clear enable flag */
if (tSM == 0)
{
context->slavelist[Slave].SM[iSM].SMflags =
htoel( etohl(context->slavelist[Slave].SM[iSM].SMflags) & EC_SMENABLEMASK);
}
if ((tSM == 3) || (tSM == 4))
{
/* read the assign PDO */
Tsize = ecx_readPDOassignCA(context, Slave, Thread_n,
ECT_SDO_PDOASSIGN + iSM );
/* if a mapping is found */
if (Tsize)
{
context->slavelist[Slave].SM[iSM].SMlength = htoes((uint16)((Tsize + 7) / 8));
if (tSM == 3)
{
/* we are doing outputs */
*Osize += Tsize;
}
else
{
/* we are doing inputs */
*Isize += Tsize;
}
}
}
}
}
/* found some I/O bits ? */
if ((*Isize > 0) || (*Osize > 0))
{
retVal = 1;
}
return retVal;
}
/** CoE read Object Description List.
*
* @param[in] context = context struct
* @param[in] Slave = Slave number.
* @param[out] pODlist = resulting Object Description list.
* @return Workcounter of slave response.
*/
int ecx_readODlist(ecx_contextt *context, uint16 Slave, ec_ODlistt *pODlist)
{
ec_SDOservicet *SDOp, *aSDOp;