Skip to content

Commit 0aa8c7d

Browse files
committed
Fix bugs in bitfield code. This saves significant RAM/Heap
1 parent a6be940 commit 0aa8c7d

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/can_common.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
#include <can_common.h>
22

3+
//CAN FD only allows discrete frame lengths after the normal CAN 8 byte limit. They are encoded below
4+
//as a FLASH based look up table for ease of use
5+
const uint8_t fdLengthEncoding[65] = {0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,9 ,9 ,9 ,10,10,10,
6+
10,11,11,11,11,12,12,12,12,13,13,13,13,13,13,13,
7+
13,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
8+
14,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15};
9+
310
CAN_FRAME::CAN_FRAME()
411
{
512
id = 0;

src/can_common.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#define attachGeneralHandler setGeneralHandler
3636
#define detachGeneralHandler removeGeneralHandler
3737

38+
extern const uint8_t fdLengthEncoding[65];
39+
3840
class BitRef
3941
{
4042
public:
@@ -114,16 +116,16 @@ typedef union {
114116
int8_t int8[64];
115117

116118
struct {
117-
uint64_t bitField[64];
119+
uint8_t bitField[64];
118120
const bool operator[]( int pos ) const
119121
{
120-
if (pos < 0 || pos > 319) return 0;
122+
if (pos < 0 || pos > 511) return 0; //64 8 bit bytes is 512 bits, we start counting bits at 0
121123
int bitfieldIdx = pos / 8;
122124
return (bitField[bitfieldIdx] >> pos) & 1;
123125
}
124126
BitRef operator[]( int pos )
125127
{
126-
if (pos < 0 || pos > 319) return BitRef((uint8_t *)&bitField[0], 0);
128+
if (pos < 0 || pos > 511) return BitRef((uint8_t *)&bitField[0], 0);
127129
uint8_t *ptr = (uint8_t *)&bitField[0];
128130
return BitRef(ptr + (pos / 8), pos & 7);
129131
}
@@ -154,7 +156,7 @@ class CAN_FRAME_FD
154156
BytesUnion_FD data; // 64 bytes - lots of ways to access it.
155157
uint32_t id; // EID if ide set, SID otherwise
156158
uint32_t fid; // family ID
157-
uint32_t timestamp; // CAN timer value when mailbox message was received.
159+
uint32_t timestamp; // CAN timer value when mailbox message was received.
158160
uint8_t rrs; // RRS for CAN-FD (optional 12th standard ID bit)
159161
uint8_t priority; // Priority but only important for TX frames and then only for special uses. (0-31)
160162
uint8_t extended; // Extended ID flag

0 commit comments

Comments
 (0)