-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCO_helpers.cpp
90 lines (66 loc) · 2.41 KB
/
CO_helpers.cpp
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
#include "CO_helpers.h"
uint32_t swapBytes(uint32_t from) {
uint32_t to;
uint8_t b0, b1, b2, b3;
b0 = (from & 0xff);
b1 = (from & 0xff00) >> 8u;
b2 = (from & 0xff0000) >> 16u;
b3 = (from & 0xff000000) >> 24u;
to = (b3) | (b2 << 8u) | (b1 << 16u) | (b0 << 24u);
return to;
}
int CO_SDO_write(
uint8_t remoteNodeId,
uint16_t index, uint8_t subIndex,
uint8_t *dataTx, uint32_t dataSize,
uint32_t *SDOabortCode,
uint16_t SDOtimeoutTime) {
uint8_t err = 0;
if (CO_SDOclient_setup(CO->SDOclient, 0, 0, remoteNodeId) != CO_SDOcli_ok_communicationEnd) err = 1;
if (err == 0) {
if (CO_SDOclientDownloadInitiate(CO->SDOclient, index, subIndex, dataTx, dataSize, 0)) err = 2;
}
if (err == 0) {
CO_SDOclient_return_t ret;
uint16_t timer1msPrev;
timer1msPrev = CO_timer1ms;
do {
uint16_t timer1ms, timer1msDiff;
timer1ms = CO_timer1ms;
timer1msDiff = timer1ms - timer1msPrev;
timer1msPrev = timer1ms;
ret = CO_SDOclientDownload(CO->SDOclient, timer1msDiff, SDOtimeoutTime, SDOabortCode);
Thread::wait(5);
} while (ret > 0);
CO_SDOclientClose(CO->SDOclient);
}
return err;
}
int CO_SDO_read(
uint8_t remoteNodeId,
uint16_t index, uint8_t subIndex,
uint8_t *dataRx, uint32_t dataSize,
uint32_t *SDOabortCode, uint32_t *dataReadSize,
uint16_t SDOtimeoutTime) {
uint8_t err = 0;
uint32_t dataRxSize = sizeof(dataRx);
if (CO_SDOclient_setup(CO->SDOclient, 0, 0, remoteNodeId) != CO_SDOcli_ok_communicationEnd) err = 1;
if (!err) {
if (CO_SDOclientUploadInitiate(CO->SDOclient, index, subIndex, dataRx, dataRxSize, 0)) err = 2;
}
if (!err) {
CO_SDOclient_return_t ret;
uint16_t timer1msPrev;
timer1msPrev = CO_timer1ms;
do {
uint16_t timer1ms, timer1msDiff;
timer1ms = CO_timer1ms;
timer1msDiff = timer1ms - timer1msPrev;
timer1msPrev = timer1ms;
ret = CO_SDOclientUpload(CO->SDOclient, timer1msDiff, SDOtimeoutTime, dataReadSize, SDOabortCode);
Thread::wait(5);
} while (ret > CO_SDOcli_ok_communicationEnd);
CO_SDOclientClose(CO->SDOclient);
}
return err;
}