-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcan_common_local.cpp
475 lines (413 loc) · 10.7 KB
/
can_common_local.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
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
#include "projectsettings.h"
#include "can_common_local.h"
#ifdef USE_CANFD
//CAN FD only allows discrete frame lengths after the normal CAN 8 byte limit. They are encoded below
//as a FLASH based look up table for ease of use
const uint8_t fdLengthEncoding[65] = {0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,9 ,9 ,9 ,10,10,10,
10,11,11,11,11,12,12,12,12,13,13,13,13,13,13,13,
13,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
14,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15};
#endif
CAN_FRAME::CAN_FRAME()
{
id = 0;
fid = 0;
rtr = 0;
priority = 15;
extended = false;
timestamp = 0;
length = 0;
data.value = 0;
}
#ifdef USE_CANFD
CAN_FRAME_FD::CAN_FRAME_FD()
{
id = 0;
fid = 0;
rrs = 0;
fdMode = 0;
priority = 15;
extended = false;
timestamp = 0;
length = 0;
for (int i = 0; i < 8; i++) data.uint64[i] = 0;
}
#endif
CANListener::CANListener()
{
callbacksActive = 0;
generalCBActive = false;
numFilters = 32;
}
//an empty version so that the linker doesn't complain that no implementation exists.
void CANListener::gotFrame(CAN_FRAME */*frame*/, int /*mailbox*/)
{
}
#ifdef USE_CANFD
void CANListener::gotFrameFD(CAN_FRAME_FD *frame, int mailbox)
{
}
#endif
void CANListener::setCallback(uint8_t mailBox)
{
if ( mailBox < numFilters )
{
callbacksActive |= (1<<mailBox);
}
}
void CANListener::removeCallback(uint8_t mailBox)
{
if ( mailBox < numFilters )
{
callbacksActive &= ~(1ull<<mailBox);
}
}
void CANListener::setGeneralHandler()
{
generalCBActive = true;
}
void CANListener::removeGeneralHandler()
{
generalCBActive = false;
}
void CANListener::initialize()
{
callbacksActive = 0;
}
bool CANListener::isCallbackActive(int callback)
{
if (callback == -1) return generalCBActive;
if (callback < numFilters)
return (callbacksActive & (1ull << callback))?true:false;
return false;
}
void CANListener::setNumFilters(int numFilt)
{
numFilters = numFilt;
}
/*
Some functions in CAN_COMMON are declared = 0 which means you HAVE to reimplement them or the compiler will puke. Some functions are optional
and they've got blank or very simple implementations below.
*/
CAN_COMMON::CAN_COMMON(int numFilt)
{
numFilters = numFilt;
memset(cbCANFrame, 0, 4 * numFilters);
#ifdef USE_CANFD
memset(cbCANFrameFD, 0, 4 * numFilters);
#endif
cbGeneral = NULL;
#ifdef USE_CANFD
cbGeneralFD = NULL;
#endif
enablePin = 255;
busSpeed = 0;
#ifdef USE_CANFD
fd_DataSpeed = 0;
#endif
faulted = false;
rxFault = false;
txFault = false;
debuggingMode = false;
fdSupported = false;
for (int i = 0; i < SIZE_LISTENERS; i++) listener[i] = 0;
}
void CAN_COMMON::setDebuggingMode(bool mode)
{
debuggingMode = mode;
}
/**
* \brief Returns whether a serious fault has occurred.
*
* \ret Bool indicating whether interface has any sort of fault or not
*/
bool CAN_COMMON::isFaulted()
{
return faulted;
}
/**
* \brief Returns whether a serious reception fault has occurred.
*
* \ret Bool indicating whether interface has a fault in receiving frames
*/
bool CAN_COMMON::hasRXFault()
{
return rxFault;
}
/**
* \brief Returns whether a serious frame transmission fault has occurred.
*
* \ret Bool indicating whether interface has a fault in transmitting frames
*/
bool CAN_COMMON::hasTXFault()
{
return txFault;
}
/**
* \brief Returns whether this interface supports CANFD or not.
*
* \ret Bool indicating whether interface can support CANFD or not.
*
* \note This is used for outside the class, internally fdSupported is directly used
*/
bool CAN_COMMON::supportsFDMode()
{
return fdSupported;
}
uint32_t CAN_COMMON::begin()
{
return init(CAN_DEFAULT_BAUD);
}
#ifdef USE_CANFD
uint32_t CAN_COMMON::beginFD()
{
if (fdSupported) return initFD(CAN_DEFAULT_BAUD, CAN_DEFAULT_FD_RATE);
return 0;
}
#endif
uint32_t CAN_COMMON::begin(uint32_t baudrate)
{
return init(baudrate);
}
#ifdef USE_CANFD
uint32_t CAN_COMMON::beginFD(uint32_t baudrate, uint32_t fastRate)
{
if (fdSupported) return initFD(baudrate, fastRate);
return 0;
}
#endif
uint32_t CAN_COMMON::begin(uint32_t baudrate, uint8_t enPin)
{
enablePin = enPin;
return init(baudrate);
}
#ifdef USE_CANFD
uint32_t CAN_COMMON::beginFD(uint32_t baudrate, uint32_t fastRate, uint8_t enPin)
{
enablePin = enPin;
if (fdSupported) return initFD(baudrate, fastRate);
return 0;
}
#endif
uint32_t CAN_COMMON::getBusSpeed()
{
return busSpeed;
}
#ifdef USE_CANFD
uint32_t CAN_COMMON::getDataSpeedFD()
{
if (fdSupported) return fd_DataSpeed;
return 0;
}
#endif
boolean CAN_COMMON::attachObj(CANListener *listener)
{
for (int i = 0; i < SIZE_LISTENERS; i++)
{
if (this->listener[i] == NULL)
{
this->listener[i] = listener;
listener->initialize();
return true;
}
}
return false;
}
boolean CAN_COMMON::detachObj(CANListener *listener)
{
for (int i = 0; i < SIZE_LISTENERS; i++)
{
if (this->listener[i] == listener)
{
this->listener[i] = NULL;
return true;
}
}
return false;
}
/**
* \brief Set up a general callback that will be used if no callback was registered for receiving mailbox
*
* \param cb A function pointer to a function with prototype "void functionname(CAN_FRAME *frame);"
*
* \note If this function is used to set up a callback then no buffering of frames will ever take place.
*/
void CAN_COMMON::setGeneralCallback(void (*cb)(CAN_FRAME *))
{
cbGeneral = cb;
}
#ifdef USE_CANFD
void CAN_COMMON::setGeneralCallbackFD(void (*cb)(CAN_FRAME_FD *))
{
if (fdSupported) cbGeneralFD = cb;
else cbGeneralFD = NULL;
}
#endif
/**
* \brief Set up a callback function for given mailbox
*
* \param mailbox Which mailbox (0-31) to assign callback to.
* \param cb A function pointer to a function with prototype "void functionname(CAN_FRAME *frame);"
*
*/
void CAN_COMMON::setCallback(uint8_t mailbox, void (*cb)(CAN_FRAME *))
{
if ( mailbox >= numFilters ) return;
cbCANFrame[mailbox] = cb;
}
#ifdef USE_CANFD
void CAN_COMMON::setCallbackFD(uint8_t mailbox, void (*cb)(CAN_FRAME_FD *))
{
if ( mailbox >= numFilters ) return;
if (fdSupported) cbCANFrameFD[mailbox] = cb;
else cbCANFrameFD[mailbox] = NULL;
}
#endif
void CAN_COMMON::attachCANInterrupt(uint8_t mailBox, void (*cb)(CAN_FRAME *))
{
setCallback(mailBox, cb);
}
void CAN_COMMON::detachCANInterrupt(uint8_t mailBox)
{
if ( mailBox >= numFilters ) return;
cbCANFrame[mailBox] = NULL;
}
void CAN_COMMON::removeCallback()
{
for (int i = 0; i < numFilters; i++)
{
cbCANFrame[i] = NULL;
#ifdef USE_CANFD
cbCANFrameFD[i] = NULL;
#endif
}
}
void CAN_COMMON::removeCallback(uint8_t mailbox)
{
if (mailbox >= numFilters) return;
cbCANFrame[mailbox] = NULL;
}
void CAN_COMMON::removeGeneralCallback()
{
cbGeneral = NULL;
}
#ifdef USE_CANFD
void CAN_COMMON::removeGeneralCallbackFD()
{
cbGeneralFD = NULL;
}
void CAN_COMMON::removeCallbackFD(uint8_t mailbox)
{
if (mailbox >= numFilters) return;
cbCANFrameFD[mailbox] = NULL;
}
#endif
int CAN_COMMON::setRXFilter(uint8_t mailbox, uint32_t id, uint32_t mask, bool extended)
{
return _setFilterSpecific(mailbox, id, mask, extended);
}
int CAN_COMMON::setRXFilter(uint32_t id, uint32_t mask, bool extended)
{
return _setFilter(id, mask, extended);
}
int CAN_COMMON::watchFor()
{
setRXFilter(0, 0, false);
return setRXFilter(0, 0, true);
}
//Let a single frame ID through. Automatic determination of extended. Also automatically sets mask
int CAN_COMMON::watchFor(uint32_t id)
{
if (id > 0x7FF) return setRXFilter(id, 0x1FFFFFFF, true);
else return setRXFilter(id, 0x7FF, false);
}
//Allow a range of IDs through based on mask. Still auto determines extended.
int CAN_COMMON::watchFor(uint32_t id, uint32_t mask)
{
if (id > 0x7FF) return setRXFilter(id, mask, true);
else return setRXFilter(id, mask, false);
}
//A bit more complicated. Makes sure that the range from id1 to id2 is let through. This might open
//the floodgates if you aren't careful.
//There are undoubtedly better ways to calculate the proper values for the filter but this way seems to work.
//It'll be kind of slow if you try to let a huge span through though.
int CAN_COMMON::watchForRange(uint32_t id1, uint32_t id2)
{
uint32_t id = 0;
uint32_t mask = 0;
uint32_t temp;
if (id1 > id2)
{ //looks funny I know. In place swap with no temporary storage. Neato!
id1 = id1 ^ id2;
id2 = id1 ^ id2; //note difference here.
id1 = id1 ^ id2;
}
id = id1;
if (id2 <= 0x7FF) mask = 0x7FF;
else mask = 0x1FFFFFFF;
/* Here is a quick overview of the theory behind these calculations.
We start with mask set to 11 or 29 set bits (all 1's)
and id set to the lowest ID in the range.
From there we go through every single ID possible in the range. For each ID
we AND with the current ID. At the end only bits that never changed and were 1's
will still be 1's. This yields the ID we can match against to let these frames through
The mask is calculated by finding the bitfield difference between the lowest ID and
the current ID. This calculation will be 1 anywhere the bits were different. We invert
this so that it is 1 anywhere the bits where the same. Then we AND with the current Mask.
At the end the mask will be 1 anywhere the bits never changed. This is the perfect mask.
*/
for (uint32_t c = id1; c <= id2; c++)
{
id &= c;
temp = (~(id1 ^ c)) & 0x1FFFFFFF;
mask &= temp;
}
//output of the above crazy loop is actually the end result.
if (id > 0x7FF) return setRXFilter(id, mask, true);
else return setRXFilter(id, mask, false);
}
//these next few functions would normally be pure abstract but they're implemented here
//so that not every class needs to implement FD functionality to work.
#ifdef USE_CANFD
uint32_t CAN_COMMON::set_baudrateFD(uint32_t nominalSpeed, uint32_t dataSpeed)
{
return 0;
}
bool CAN_COMMON::sendFrameFD(CAN_FRAME_FD& txFrame)
{
return false;
}
uint32_t CAN_COMMON::initFD(uint32_t nominalRate, uint32_t dataRate)
{
return 0;
}
//try to put a standard CAN frame into a CAN_FRAME_FD structure
bool CAN_COMMON::canToFD(CAN_FRAME &source, CAN_FRAME_FD &dest)
{
dest.id = source.id;
dest.fid = source.fid;
dest.rrs = source.rtr;
dest.priority = source.priority;
dest.extended = source.extended;
dest.fdMode = false;
dest.timestamp = source.timestamp;
dest.length = source.length;
dest.data.uint64[0] = source.data.uint64;
return true;
}
//Try to do inverse - turn a CANFD frame into a standard CAN_FRAME struct
bool CAN_COMMON::fdToCan(CAN_FRAME_FD &source, CAN_FRAME &dest)
{
if (source.length > 8) return false;
if (source.fdMode > 0) return false;
dest.id = source.id;
dest.fid = source.fid;
dest.rtr = source.rrs;
dest.priority = source.priority;
dest.extended = source.extended;
dest.timestamp = source.timestamp;
dest.length = source.length;
dest.data.uint64 = source.data.uint64[0];
return true;
}
#endif