22
22
#define CAN_CLOCK MCP_8MHZ
23
23
MCP_CAN CAN0 (CAN_CS); // TODO make configurable
24
24
// since long messages can be sliced and arrive mixed with other messages assemble buffer is required
25
- #define bufSize 8 // TODO make configurable
25
+ #define CAN_BUF_SIZE 8 // TODO make configurable
26
26
bool canInitialized=false ;
27
27
28
28
// input buffer for raw data (from library).
@@ -36,7 +36,7 @@ unsigned char _nodeId;
36
36
// buffer element
37
37
typedef struct {
38
38
uint8_t len;
39
- uint8_t data[32 ];
39
+ uint8_t data[MAX_MESSAGE_SIZE ];
40
40
uint8_t address;
41
41
uint8_t lastReceivedPart;
42
42
bool locked;
@@ -45,7 +45,7 @@ typedef struct {
45
45
bool ready;
46
46
} CAN_Packet;
47
47
48
- CAN_Packet packets[bufSize ];
48
+ CAN_Packet packets[CAN_BUF_SIZE ];
49
49
50
50
// filter incoming messages (MCP2515 feature)
51
51
void _initFilters () {
@@ -73,7 +73,7 @@ bool transportInit(void)
73
73
return false ;
74
74
}
75
75
canInitialized=true ;
76
- for (uint8_t i = 0 ; i < bufSize ; i++) {
76
+ for (uint8_t i = 0 ; i < CAN_BUF_SIZE ; i++) {
77
77
_cleanSlot (i);
78
78
}
79
79
_initFilters ();
@@ -94,20 +94,20 @@ void _cleanSlot(uint8_t slot) {
94
94
95
95
// find empty slot in buffer
96
96
uint8_t _findCanPacketSlot () {
97
- uint8_t slot=bufSize ;
97
+ uint8_t slot=CAN_BUF_SIZE ;
98
98
uint8_t i;
99
- for (i = 0 ; i < bufSize ; i++) {
99
+ for (i = 0 ; i < CAN_BUF_SIZE ; i++) {
100
100
if (packets[i].locked ) {
101
101
packets[i].age ++;
102
102
} else {
103
103
slot=i;
104
104
}
105
105
}
106
- if (slot<bufSize )
106
+ if (slot < CAN_BUF_SIZE )
107
107
return slot;
108
108
// if empty slot not found. Clear oldest message.
109
109
slot=0 ;
110
- for (i = 1 ; i < bufSize ; i++) {
110
+ for (i = 1 ; i < CAN_BUF_SIZE ; i++) {
111
111
if (packets[i].age >packets[slot].age ) {
112
112
slot=i;
113
113
}
@@ -119,14 +119,14 @@ uint8_t _findCanPacketSlot() {
119
119
120
120
// find slot with previous data parts.
121
121
uint8_t _findCanPacketSlot (long unsigned int from,long unsigned int currentPart,long unsigned int messageId){
122
- uint8_t slot=bufSize ;
122
+ uint8_t slot=CAN_BUF_SIZE ;
123
123
uint8_t i;
124
- for (i = 0 ; i < bufSize ; i++) {
124
+ for (i = 0 ; i < CAN_BUF_SIZE ; i++) {
125
125
if (packets[i].locked && packets[i].address ==from && packets[i].packetId ==messageId && packets[i].lastReceivedPart ==currentPart+1 ) {
126
126
slot=i;
127
127
}
128
128
}
129
- if (slot==bufSize ) {
129
+ if (slot == CAN_BUF_SIZE ) {
130
130
// log error. Received message id not found in buffer.
131
131
}
132
132
return slot;
@@ -217,7 +217,7 @@ bool transportDataAvailable(void)
217
217
} else {
218
218
slot=_findCanPacketSlot (from,currentPart,messageId);
219
219
}
220
- if (slot!=bufSize ) {
220
+ if (slot != CAN_BUF_SIZE ) {
221
221
memcpy (packets[slot].data + packets[slot].len , rxBuf, len);
222
222
packets[slot].lastReceivedPart ++;
223
223
packets[slot].len += len;
@@ -234,14 +234,14 @@ bool transportDataAvailable(void)
234
234
}
235
235
uint8_t transportReceive (void * data)
236
236
{
237
- uint8_t slot=bufSize ;
237
+ uint8_t slot=CAN_BUF_SIZE ;
238
238
uint8_t i;
239
- for (i = 0 ; i < bufSize ; i++) {
239
+ for (i = 0 ; i < CAN_BUF_SIZE ; i++) {
240
240
if (packets[i].ready ) {
241
241
slot=i;
242
242
}
243
243
}
244
- if (slot<bufSize ) {
244
+ if (slot < CAN_BUF_SIZE ) {
245
245
memcpy (data,packets[slot].data ,packets[slot].len );
246
246
i=packets[slot].len ;
247
247
_cleanSlot (slot);
0 commit comments