34
34
#define ITEM_UNSET 0xff
35
35
#define forEachCanMap (c,m ) for (CANIDMAP *c = m; (c - m) < MAX_MESSAGES && c->first != MAX_ITEMS; c++)
36
36
#define forEachPosMap (c,m ) for (CANPOS *c = &canPosMap[m->first]; c->next != ITEM_UNSET; c = &canPosMap[c->next])
37
+ #define IS_EXT_FORCE (id ) ((SHIFT_FORCE_FLAG(1 ) & id) != 0 )
38
+ #define MASK_EXT_FORCE (id ) (id & ~SHIFT_FORCE_FLAG (1 ))
39
+
40
+ // If we configure the module to only support 11-bit IDs, we only have 16 bits of memory
41
+ // allocated for the ID. In this case we put the "force extended ID filter" flag
42
+ // into the 11th bit.
43
+ // When allowing extended ids we put it into the 29th bit
37
44
38
45
#ifdef CAN_EXT
39
46
#define IDMAPSIZE 8
47
+ #define SHIFT_FORCE_FLAG (f ) (f << 29 )
40
48
#else
41
49
#define IDMAPSIZE 4
50
+ #define SHIFT_FORCE_FLAG (f ) (f << 11 )
42
51
#endif // CAN_EXT
43
52
#if (MAX_ITEMS * 12 + 2 * MAX_MESSAGES * IDMAPSIZE + 4) > FLASH_PAGE_SIZE
44
53
#error CANMAP will not fit in one flash page
@@ -62,7 +71,8 @@ void CanMap::HandleClear()
62
71
{
63
72
forEachCanMap (curMap, canRecvMap)
64
73
{
65
- canHardware->RegisterUserMessage (curMap->canId );
74
+ bool forceExtended = IS_EXT_FORCE (curMap->canId );
75
+ canHardware->RegisterUserMessage ((curMap->canId & ~SHIFT_FORCE_FLAG (1 )) + (forceExtended * CAN_FORCE_EXTENDED));
66
76
}
67
77
}
68
78
@@ -131,12 +141,14 @@ bool CanMap::HandleRx(uint32_t canId, uint32_t data[2], uint8_t)
131
141
// sign-extend our arbitrary sized integer out to 32-bits but only if
132
142
// it is bigger than a single bit
133
143
int32_t ival;
144
+ #if CAN_SIGNED
134
145
if (numBits > 1 )
135
146
{
136
147
uint32_t sign_bit = 1L << (numBits - 1 );
137
148
ival = static_cast <int32_t >(((word + sign_bit) & mask)) - sign_bit;
138
149
}
139
150
else
151
+ #endif
140
152
{
141
153
ival = word;
142
154
}
@@ -249,15 +261,18 @@ void CanMap::SendAll()
249
261
*/
250
262
int CanMap::AddSend (Param::PARAM_NUM param, uint32_t canId, uint8_t offsetBits, int8_t length, float gain, int8_t offset)
251
263
{
264
+ if (canId > MAX_COB_ID) return CAN_ERR_INVALID_ID;
252
265
return Add (canSendMap, param, canId, offsetBits, length, gain, offset);
253
266
}
254
267
255
268
int CanMap::AddSend (Param::PARAM_NUM param, uint32_t canId, uint8_t offsetBits, int8_t length, float gain)
256
269
{
270
+ if (canId > MAX_COB_ID) return CAN_ERR_INVALID_ID;
257
271
return Add (canSendMap, param, canId, offsetBits, length, gain, 0 );
258
272
}
259
273
260
274
/* * \brief Map data from CAN bus to parameter
275
+ * To force registering an extended filter for a standard ID, add 0x20000000 to canId
261
276
*
262
277
* \param param Parameter index of parameter to be received
263
278
* \param canId CAN identifier of consumed message
@@ -274,7 +289,13 @@ int CanMap::AddSend(Param::PARAM_NUM param, uint32_t canId, uint8_t offsetBits,
274
289
*/
275
290
int CanMap::AddRecv (Param::PARAM_NUM param, uint32_t canId, uint8_t offsetBits, int8_t length, float gain, int8_t offset)
276
291
{
277
- int res = Add (canRecvMap, param, canId, offsetBits, length, gain, offset);
292
+ bool forceExtended = (canId & CAN_FORCE_EXTENDED) != 0 ;
293
+ uint32_t moddedId = canId & ~CAN_FORCE_EXTENDED; // mask out force flag
294
+ if (moddedId > MAX_COB_ID) return CAN_ERR_INVALID_ID;
295
+ // Put force flag either in bit 11 (when mapping restricted to std IDs or bit 29 when allowing ext ids
296
+ moddedId |= SHIFT_FORCE_FLAG (forceExtended);
297
+
298
+ int res = Add (canRecvMap, param, moddedId, offsetBits, length, gain, offset);
278
299
canHardware->RegisterUserMessage (canId);
279
300
return res;
280
301
}
@@ -432,7 +453,10 @@ bool CanMap::FindMap(Param::PARAM_NUM param, uint32_t& canId, uint8_t& start, in
432
453
{
433
454
if (curPos->mapParam == param)
434
455
{
456
+ bool forceExt = IS_EXT_FORCE (curMap->canId );
435
457
canId = curMap->canId ;
458
+ canId = MASK_EXT_FORCE (canId);
459
+ canId |= forceExt * CAN_FORCE_EXTENDED;
436
460
start = curPos->offsetBits ;
437
461
length = curPos->numBits ;
438
462
gain = curPos->gain ;
@@ -457,7 +481,10 @@ const CanMap::CANPOS* CanMap::GetMap(bool rx, uint8_t ididx, uint8_t itemidx, ui
457
481
{
458
482
if (itemidx == 0 )
459
483
{
484
+ bool forceExt = IS_EXT_FORCE (map->canId );
460
485
canId = map->canId ;
486
+ canId = MASK_EXT_FORCE (canId);
487
+ canId |= forceExt * CAN_FORCE_EXTENDED;
461
488
return curPos;
462
489
}
463
490
itemidx--;
@@ -475,7 +502,11 @@ void CanMap::IterateCanMap(void (*callback)(Param::PARAM_NUM, uint32_t, uint8_t,
475
502
{
476
503
forEachPosMap (curPos, curMap)
477
504
{
478
- callback ((Param::PARAM_NUM)curPos->mapParam , curMap->canId , curPos->offsetBits , curPos->numBits , curPos->gain , curPos->offset , rx);
505
+ bool forceExt = IS_EXT_FORCE (curMap->canId );
506
+ uint32_t canId = curMap->canId ;
507
+ canId = MASK_EXT_FORCE (canId);
508
+ canId |= forceExt * CAN_FORCE_EXTENDED;
509
+ callback ((Param::PARAM_NUM)curPos->mapParam , canId, curPos->offsetBits , curPos->numBits , curPos->gain , curPos->offset , rx);
479
510
}
480
511
}
481
512
done = rx;
@@ -502,7 +533,7 @@ void CanMap::ClearMap(CANIDMAP *canMap)
502
533
503
534
int CanMap::Add (CANIDMAP *canMap, Param::PARAM_NUM param, uint32_t canId, uint8_t offsetBits, int8_t length, float gain, int8_t offset)
504
535
{
505
- if (canId > MAX_COB_ID) return CAN_ERR_INVALID_ID;
536
+ // if (canId > MAX_COB_ID) return CAN_ERR_INVALID_ID;
506
537
if (length == 0 || ABS (length) > 32 ) return CAN_ERR_INVALID_LEN;
507
538
if (length > 0 )
508
539
{
@@ -672,7 +703,7 @@ CanMap::CANIDMAP* CanMap::FindById(CANIDMAP *canMap, uint32_t canId)
672
703
{
673
704
forEachCanMap (curMap, canMap)
674
705
{
675
- if (curMap->canId == canId)
706
+ if (( curMap->canId & ~SHIFT_FORCE_FLAG ( 1 )) == canId)
676
707
return curMap;
677
708
}
678
709
return 0 ;
0 commit comments