Skip to content

Commit 613097e

Browse files
authored
Force extended (#19)
* Allow forcing extended Ids even if ID < 0x7ff * On the outside interface always use bit 29 as "force extended id" bit On the inside store it in bit 11 or 29 depending on whether the module is configured to use extended ids or not Made signed reception optional and turned off by default for backward compatibility
1 parent decbdfb commit 613097e

File tree

5 files changed

+46
-8
lines changed

5 files changed

+46
-8
lines changed

include/canmap.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define CAN_ERR_INVALID_LEN -3
2727
#define CAN_ERR_MAXMESSAGES -4
2828
#define CAN_ERR_MAXITEMS -5
29+
#define CAN_FORCE_EXTENDED 0x20000000
2930

3031
#ifndef MAX_ITEMS
3132
#define MAX_ITEMS 50
@@ -35,6 +36,10 @@
3536
#define MAX_MESSAGES 10
3637
#endif
3738

39+
#ifndef CAN_SIGNED
40+
#define CAN_SIGNED 0
41+
#endif // CAN_SIGNED
42+
3843
#ifdef CAN_EXT
3944
#define MAX_COB_ID 0x1fffffff
4045
#else

src/canhardware.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ bool CanHardware::AddCallback(CanCallback* recv)
5353
}
5454

5555
/** \brief Add CAN Id to user message list
56+
* canId can be 0x20000000 + std id to force registering a filter for an extended ID
57+
* even if the Id is < 0x7ff
5658
* \post Receive callback will be called when a message with this Id id received
5759
* \param canId CAN identifier of message to be user handled
58-
* \return true: success, false: already 10 messages registered
60+
* \return true: success, false: already maximum messages registered
5961
*
6062
*/
6163
bool CanHardware::RegisterUserMessage(uint32_t canId, uint32_t mask)

src/canmap.cpp

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,20 @@
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
*/
250262
int 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

255268
int 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
*/
275290
int 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

503534
int 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;

src/cansdo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ void CanSdo::AddCanMap(CAN_SDO* sdo, bool rx)
358358

359359
if (sdo->subIndex == 0)
360360
{
361-
if (sdo->data <= MAX_COB_ID)
361+
if (sdo->data < 0x20000000 || (sdo->data & ~CAN_FORCE_EXTENDED) < 0x800)
362362
{
363363
mapId = sdo->data;
364364
result = 0;

src/stm32_can.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ void Stm32Can::ConfigureFilters()
305305
{
306306
if (userIds[i] > 0x7ff)
307307
{
308-
extIdList[extIdIndex] = userIds[i];
308+
extIdList[extIdIndex] = userIds[i] & 0x1FFFFFFF;
309309
extIdIndex++;
310310
}
311311
else if (userMasks[i] != 0)

0 commit comments

Comments
 (0)