-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCANopen.cpp
More file actions
413 lines (341 loc) · 12.1 KB
/
Copy pathCANopen.cpp
File metadata and controls
413 lines (341 loc) · 12.1 KB
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
#include <core/io/CANopen.hpp>
#include <stdint.h>
#include <co_csdo.h>
#include <core/dev/RTC.hpp>
#include <core/io/CANDevice.hpp>
#include <core/io/types/CANMessage.hpp>
#include <core/utils/log.hpp>
#include <core/utils/types/FixedQueue.hpp>
#define MAX_SIZE 64
#define SDO_WAIT 10
#define SDO_REQUEST_TIMEOUT 1000
namespace log = core::log;
/*
* Empty namespace to contain "global" variables. These will be used within
* the driver implementations.
*/
namespace {
core::io::CAN* __evt_core_can__;
// Temporary values for testing CANopen without actual timer
core::dev::Timer* __evt_core_can_timer__;
/** Counts the number of interrupts that have taken place */
uint32_t timerCounter = 0;
/** The target value for the counter */
uint32_t counterTarget = 0;
/* only return timer updates while timer is running - fixes hard fault in COTmrService*/
bool timerRunning = false;
// Temporary "storage" to allow the NVM to work, do not use as actual NVM
uint8_t testerStorage[MAX_SIZE];
// Queue that stores the CAN messages to send to the CANopen parser
core::types::FixedQueue<CANOPEN_QUEUE_SIZE, core::io::CANMessage>* canQueue;
// SDO variables
typedef struct SDOState {
bool inProgress = false;
void* context;
core::io::csdo_callback_t callback;
CO_NODE* node;
uint32_t lastErr;
} sdo_state_t;
sdo_state_t state;
void internalCallback(CO_CSDO* csdo, uint16_t index, uint8_t sub, uint32_t code) {
if (state.callback != nullptr) {
state.callback(csdo, CO_DEV(index, sub), code, state.context);
}
state.callback = nullptr;
state.node = nullptr;
state.context = nullptr;
state.lastErr = code;
state.inProgress = false;
}
} // namespace
///////////////////////////////////////////////////////////////////////////////
// Forward declarations of CANopen stack CAN functions
///////////////////////////////////////////////////////////////////////////////
static void canInit(void);
static void canEnable(uint32_t baudrate);
static int16_t canSend(CO_IF_FRM* frm);
static int16_t canRead(CO_IF_FRM* frm);
static void canReset(void);
static void canClose(void);
///////////////////////////////////////////////////////////////////////////////
// Forward declarations of CANopen stack timer functions
///////////////////////////////////////////////////////////////////////////////
static void timerInit(uint32_t freq);
static void timerReload(uint32_t reload);
static void timerStart(void);
static uint8_t timerUpdate(void);
static uint32_t timerDelay(void);
static void timerStop(void);
///////////////////////////////////////////////////////////////////////////////
// Forward declarations of CANopen stack NVM functions
///////////////////////////////////////////////////////////////////////////////
static void nvmInit(void);
static uint32_t nvmRead(uint32_t start, uint8_t* buffer, uint32_t size);
static uint32_t nvmWrite(uint32_t start, uint8_t* buffer, uint32_t size);
namespace core::io {
void getCANopenCANDriver(io::CAN* canInf, core::types::FixedQueue<CANOPEN_QUEUE_SIZE, io::CANMessage>* messageQueue,
CO_IF_CAN_DRV* canDriver) {
__evt_core_can__ = canInf;
canQueue = messageQueue;
canDriver->Init = canInit;
canDriver->Enable = canEnable;
canDriver->Read = canRead;
canDriver->Send = canSend;
canDriver->Reset = canReset;
canDriver->Close = canClose;
}
void getCANopenTimerDriver(dev::Timer* timerIntf, CO_IF_TIMER_DRV* timerDriver) {
__evt_core_can_timer__ = timerIntf;
timerDriver->Init = timerInit;
timerDriver->Reload = timerReload;
timerDriver->Delay = timerDelay;
timerDriver->Stop = timerStop;
timerDriver->Start = timerStart;
timerDriver->Update = timerUpdate;
}
void getCANopenNVMDriver(CO_IF_NVM_DRV* nvmDriver) {
nvmDriver->Init = nvmInit;
nvmDriver->Read = nvmRead;
nvmDriver->Write = nvmWrite;
}
void initializeCANopenDriver(types::FixedQueue<CANOPEN_QUEUE_SIZE, io::CANMessage>* canOpenQueue, io::CAN* can,
core::dev::Timer* timer, CO_IF_DRV* canStackDriver, CO_IF_NVM_DRV* nvmDriver,
CO_IF_TIMER_DRV* timerDriver, CO_IF_CAN_DRV* canDriver) {
io::getCANopenCANDriver(can, canOpenQueue, canDriver);
io::getCANopenTimerDriver(timer, timerDriver);
io::getCANopenNVMDriver(nvmDriver);
canStackDriver->Can = canDriver;
canStackDriver->Timer = timerDriver;
canStackDriver->Nvm = nvmDriver;
}
void initializeCANopenNode(CO_NODE* canNode, CANDevice* canDevice, CO_IF_DRV* canStackDriver,
uint8_t sdoBuffer[CO_SSDO_N * CO_SDO_BUF_BYTE], CO_TMR_MEM appTmrMem[16]) {
// setup CANopen Node
CO_NODE_SPEC canSpec = {
.NodeId = canDevice->getNodeID(),
.Baudrate = io::CAN::DEFAULT_BAUD,
.Dict = canDevice->getObjectDictionary(),
.DictLen = canDevice->getNumElements(),
.EmcyCode = NULL,
.TmrMem = appTmrMem,
.TmrNum = 16,
.TmrFreq = 100,
.Drv = canStackDriver,
.SdoBuf = reinterpret_cast<uint8_t*>(&sdoBuffer[0]),
};
CONodeInit(canNode, &canSpec);
CONodeStart(canNode);
}
void processCANopenNode(CO_NODE* canNode) {
// Process incoming CAN messages
CONodeProcess(canNode);
// Update the state of timer based events
COTmrService(&canNode->Tmr);
// Handle executing timer events that have elapsed
COTmrProcess(&canNode->Tmr);
}
void alertTPDO(CO_NODE* canNode, uint16_t tpdoNum) {
COTPdoTrigPdo(canNode->TPdo, tpdoNum);
}
CO_ERR SDOTransfer(CO_NODE& node, uint8_t* data, uint8_t size, uint32_t entry, csdo_callback_t transferCallback,
void* transferContext) {
while (state.inProgress == true) {
processCANopenNode(state.node);
time::wait(SDO_WAIT);
}
// Find the Client-SDO (CO_CSDO) object for the specified node.
CO_CSDO* csdo = COCSdoFind(&(node), 0);
CO_ERR err = CO_ERR_BAD_ARG;
if (csdo != nullptr) {
state.callback = transferCallback;
state.context = transferContext;
state.node = &node;
// Initiate an SDO download request.
err = COCSdoRequestDownload(csdo, entry, data, size, internalCallback, SDO_REQUEST_TIMEOUT);
}
if (err == CO_ERR_NONE) {
state.inProgress = true;
}
return err;
}
CO_ERR SDOReceive(CO_NODE& node, uint8_t* data, uint8_t size, uint32_t entry, csdo_callback_t receiveCallback,
void* receiveContext) {
while (state.inProgress == true) {
processCANopenNode(state.node);
time::wait(SDO_WAIT);
}
// Find the Client-SDO (CO_CSDO) object for the specified node.
CO_CSDO* csdo = COCSdoFind(&(node), 0);
CO_ERR err = CO_ERR_BAD_ARG;
if (csdo != nullptr) {
state.callback = receiveCallback;
state.context = receiveContext;
state.node = &node;
// Initiate an SDO upload request.
err = COCSdoRequestUpload(csdo, entry, data, size, internalCallback, SDO_REQUEST_TIMEOUT);
}
if (err == CO_ERR_NONE) {
state.inProgress = true;
}
return err;
}
uint32_t SDOTransferBlocking(CO_NODE& node, uint8_t* data, uint8_t size, uint32_t entry) {
CO_ERR err = SDOTransfer(node, data, size, entry, nullptr, nullptr);
while (state.inProgress == true) {
processCANopenNode(state.node);
time::wait(SDO_WAIT);
}
if (state.lastErr != 0) {
return state.lastErr;
}
return err;
}
uint32_t SDOReceiveBlocking(CO_NODE& node, uint8_t* data, uint8_t size, uint32_t entry) {
CO_ERR err = SDOReceive(node, data, size, entry, nullptr, nullptr);
while (state.inProgress == true) {
processCANopenNode(state.node);
time::wait(SDO_WAIT);
}
if (state.lastErr != 0) {
return state.lastErr;
}
return err;
}
} // namespace core::io
///////////////////////////////////////////////////////////////////////////////
// Implementation of CANopen stack CAN drivers
///////////////////////////////////////////////////////////////////////////////
/**
* Initialize the CAN driver. This doesn't do anything since the CAN interface
* should be passed into `getCANopenDriver` pre-initialized.
*/
static void canInit(void) {}
/**
* Enable the CAN driver at the specific baudrate. Again this doesn't
* do anything since the CAN interface will already be setup.
*/
static void canEnable(uint32_t baudrate) {
// TODO: Should have the ability to reset the CAN driver
}
/**
* Send a CAN message. This will convert the CANopen stack CAN message
* format into the core::io::CANMessage
*
* @param frm[in] The message to send over cat
* @return sizeof(CO_IF_FRM) on success, ((int16_t)-1u) on failure
*/
static int16_t canSend(CO_IF_FRM* frm) {
core::io::CANMessage message(frm->Identifier, frm->DLC, frm->Data, false);
__evt_core_can__->transmit(message);
return sizeof(CO_IF_FRM);
}
/**
* Read in a CAN message. This will convert from the core::io::CANMessage
* into the CANopen stack format.
*
* @param frm[out] The message to populate with CAN data
* @return sizeof(CO_IF_FMR) on success, ((int16_t) 0) on failure
*/
static int16_t canRead(CO_IF_FRM* frm) {
core::io::CANMessage message;
// No message
if (!canQueue->pop(&message))
return ((int16_t) 0); // This should be 0 according to CANopen's COIfCanRead
frm->Identifier = message.getId();
frm->DLC = message.getDataLength();
// Copy contents into payload buffer
for (int i = 0; i < message.getDataLength(); i++) {
frm->Data[i] = message.getPayload()[i];
}
return sizeof(CO_IF_FRM);
}
/**
* Reset the CAN interface. This does nothing at the moment.
*/
static void canReset(void) {}
/**
* Close the CAN connection. This does nothing at the moment.
*/
static void canClose(void) {}
///////////////////////////////////////////////////////////////////////////////
// Implementations of CANopen stack timer functions
///////////////////////////////////////////////////////////////////////////////
/**
* Interrupt handler for the timer, updates that the timer has gone off
*/
void timerHandler(void* context, void* halTim) {
timerCounter++;
}
/**
* Initiailize the timer driver.
*/
static void timerInit(uint32_t freq) {
timerCounter = 0;
__evt_core_can_timer__->setPeriod(10);
}
static void timerReload(uint32_t reload) {
__evt_core_can_timer__->stopTimer();
__evt_core_can_timer__->setPeriod(10);
__evt_core_can_timer__->startTimer(timerHandler, nullptr);
timerCounter = 0;
timerRunning = true;
counterTarget = reload;
}
/**
* Start the "timer" running
*/
static void timerStart(void) {
__evt_core_can_timer__->startTimer(timerHandler, nullptr);
timerRunning = true;
timerCounter = 0;
}
/**
* Return true if the timer has gone off
*/
static uint8_t timerUpdate(void) {
int result = timerCounter >= counterTarget && timerRunning ? 1 : 0;
return result;
}
/**
* Get the difference between the current value and the target value
*/
static uint32_t timerDelay(void) {
return timerCounter > counterTarget ? 0 : counterTarget - timerCounter;
}
/**
* Stop the timer, currently does nothing.
*/
static void timerStop(void) {
__evt_core_can_timer__->stopTimer();
timerRunning = false;
timerCounter = 0;
}
///////////////////////////////////////////////////////////////////////////////
// Implementation of CANopen stack NVM functions
///////////////////////////////////////////////////////////////////////////////
/**
* Initialize the NVM driver, does nothing
*/
static void nvmInit(void) {}
/**
* Read from the temporary buffer
*/
static uint32_t nvmRead(uint32_t start, uint8_t* buffer, uint32_t size) {
uint32_t bytesRead = 0;
for (unsigned int i = 0; i < size && i + start < MAX_SIZE; i++) {
buffer[i] = testerStorage[i + start];
bytesRead++;
}
return bytesRead;
}
/**
* Write to the temporary buffer
*/
static uint32_t nvmWrite(uint32_t start, uint8_t* buffer, uint32_t size) {
uint32_t bytesWrote = 0;
for (unsigned int i = 0; i < size && i + start < MAX_SIZE; i++) {
testerStorage[i + start] = buffer[i];
bytesWrote++;
}
return bytesWrote;
}