3434#define ITEM_UNSET 0xff
3535#define forEachCanMap (c,m ) for (CANIDMAP *c = m; (c - m) < MAX_MESSAGES && c->first != MAX_ITEMS; c++)
3636#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
3744
3845#ifdef CAN_EXT
3946#define IDMAPSIZE 8
47+ #define SHIFT_FORCE_FLAG (f ) (f << 29 )
4048#else
4149#define IDMAPSIZE 4
50+ #define SHIFT_FORCE_FLAG (f ) (f << 11 )
4251#endif // CAN_EXT
4352#if (MAX_ITEMS * 12 + 2 * MAX_MESSAGES * IDMAPSIZE + 4) > FLASH_PAGE_SIZE
4453#error CANMAP will not fit in one flash page
@@ -62,7 +71,8 @@ void CanMap::HandleClear()
6271{
6372 forEachCanMap (curMap, canRecvMap)
6473 {
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));
6676 }
6777}
6878
@@ -131,12 +141,14 @@ bool CanMap::HandleRx(uint32_t canId, uint32_t data[2], uint8_t)
131141 // sign-extend our arbitrary sized integer out to 32-bits but only if
132142 // it is bigger than a single bit
133143 int32_t ival;
144+ #if CAN_SIGNED
134145 if (numBits > 1 )
135146 {
136147 uint32_t sign_bit = 1L << (numBits - 1 );
137148 ival = static_cast <int32_t >(((word + sign_bit) & mask)) - sign_bit;
138149 }
139150 else
151+ #endif
140152 {
141153 ival = word;
142154 }
@@ -249,15 +261,18 @@ void CanMap::SendAll()
249261 */
250262int CanMap::AddSend (Param::PARAM_NUM param, uint32_t canId, uint8_t offsetBits, int8_t length, float gain, int8_t offset)
251263{
264+ if (canId > MAX_COB_ID) return CAN_ERR_INVALID_ID;
252265 return Add (canSendMap, param, canId, offsetBits, length, gain, offset);
253266}
254267
255268int CanMap::AddSend (Param::PARAM_NUM param, uint32_t canId, uint8_t offsetBits, int8_t length, float gain)
256269{
270+ if (canId > MAX_COB_ID) return CAN_ERR_INVALID_ID;
257271 return Add (canSendMap, param, canId, offsetBits, length, gain, 0 );
258272}
259273
260274/* * \brief Map data from CAN bus to parameter
275+ * To force registering an extended filter for a standard ID, add 0x20000000 to canId
261276 *
262277 * \param param Parameter index of parameter to be received
263278 * \param canId CAN identifier of consumed message
@@ -274,7 +289,13 @@ int CanMap::AddSend(Param::PARAM_NUM param, uint32_t canId, uint8_t offsetBits,
274289 */
275290int CanMap::AddRecv (Param::PARAM_NUM param, uint32_t canId, uint8_t offsetBits, int8_t length, float gain, int8_t offset)
276291{
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);
278299 canHardware->RegisterUserMessage (canId);
279300 return res;
280301}
@@ -432,7 +453,10 @@ bool CanMap::FindMap(Param::PARAM_NUM param, uint32_t& canId, uint8_t& start, in
432453 {
433454 if (curPos->mapParam == param)
434455 {
456+ bool forceExt = IS_EXT_FORCE (curMap->canId );
435457 canId = curMap->canId ;
458+ canId = MASK_EXT_FORCE (canId);
459+ canId |= forceExt * CAN_FORCE_EXTENDED;
436460 start = curPos->offsetBits ;
437461 length = curPos->numBits ;
438462 gain = curPos->gain ;
@@ -457,7 +481,10 @@ const CanMap::CANPOS* CanMap::GetMap(bool rx, uint8_t ididx, uint8_t itemidx, ui
457481 {
458482 if (itemidx == 0 )
459483 {
484+ bool forceExt = IS_EXT_FORCE (map->canId );
460485 canId = map->canId ;
486+ canId = MASK_EXT_FORCE (canId);
487+ canId |= forceExt * CAN_FORCE_EXTENDED;
461488 return curPos;
462489 }
463490 itemidx--;
@@ -475,7 +502,11 @@ void CanMap::IterateCanMap(void (*callback)(Param::PARAM_NUM, uint32_t, uint8_t,
475502 {
476503 forEachPosMap (curPos, curMap)
477504 {
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);
479510 }
480511 }
481512 done = rx;
@@ -502,7 +533,7 @@ void CanMap::ClearMap(CANIDMAP *canMap)
502533
503534int CanMap::Add (CANIDMAP *canMap, Param::PARAM_NUM param, uint32_t canId, uint8_t offsetBits, int8_t length, float gain, int8_t offset)
504535{
505- if (canId > MAX_COB_ID) return CAN_ERR_INVALID_ID;
536+ // if (canId > MAX_COB_ID) return CAN_ERR_INVALID_ID;
506537 if (length == 0 || ABS (length) > 32 ) return CAN_ERR_INVALID_LEN;
507538 if (length > 0 )
508539 {
@@ -672,7 +703,7 @@ CanMap::CANIDMAP* CanMap::FindById(CANIDMAP *canMap, uint32_t canId)
672703{
673704 forEachCanMap (curMap, canMap)
674705 {
675- if (curMap->canId == canId)
706+ if (( curMap->canId & ~SHIFT_FORCE_FLAG ( 1 )) == canId)
676707 return curMap;
677708 }
678709 return 0 ;
0 commit comments