11
11
#include " hal/transport/CAN/driver/mcp_can.h"
12
12
#include " hal/transport/CAN/driver/mcp_can.cpp"
13
13
#include " MyTransportCAN.h"
14
- #define CAN0_INT 2 // TODO make configurable
15
- MCP_CAN CAN0 (10 ); // TODO make configurable
14
+ #if defined(MY_DEBUG_VERBOSE_CAN)
15
+ #define CAN_DEBUG (x,...) DEBUG_OUTPUT(x, ##__VA_ARGS__) // !< Debug print
16
+ #else
17
+ #define CAN_DEBUG (x,...) // !< DEBUG null
18
+ #endif
19
+ #define CAN_INT 2 // TODO make configurable
20
+ #define CAN_CS 10
21
+ #define CAN_SPEED CAN_250KBPS
22
+ #define CAN_CLOCK MCP_8MHZ
23
+ MCP_CAN CAN0 (CAN_CS); // TODO make configurable
16
24
// since long messages can be sliced and arrive mixed with other messages assemble buffer is required
17
25
#define bufSize 8 // TODO make configurable
18
26
bool canInitialized=false ;
@@ -54,11 +62,13 @@ void _initFilters() {
54
62
CAN0.init_Filt (4 ,1 ,0xFFFFFFFF ); // Init fifth filter.
55
63
CAN0.init_Filt (5 ,1 ,0xFFFFFFFF ); // Init sixth filter.
56
64
CAN0.setMode (MCP_NORMAL);
57
- hwPinMode (CAN0_INT , INPUT);
65
+ hwPinMode (CAN_INT , INPUT);
58
66
}
59
67
bool transportInit (void )
60
68
{
61
- if (CAN0.begin (MCP_STDEXT, CAN_250KBPS, MCP_8MHZ) != CAN_OK) {
69
+ CAN_DEBUG (PSTR (" CAN:INIT:CS=%" PRIu8 " ,INT=%" PRIu8 " ,SPE=%" PRIu8 " ,CLO=%" PRIu8 " \n " ), CAN_CS, CAN_INT, CAN_SPEED, CAN_CLOCK);
70
+
71
+ if (CAN0.begin (MCP_STDEXT, CAN_SPEED, CAN_CLOCK) != CAN_OK) {
62
72
canInitialized=false ;
63
73
return false ;
64
74
}
@@ -147,6 +157,7 @@ bool transportSend(const uint8_t to, const void* data, const uint8_t len, const
147
157
// shift left to create space for current frame number
148
158
h2=h2 << 4 ;
149
159
uint8_t i = 0 ;
160
+ CAN_DEBUG (PSTR (" CAN:SND:LN=%" PRIu8 " ,NOF=%" PRIu8 " \n " ), len, noOfFrames);
150
161
151
162
for (i = 0 ; i < noOfFrames; i++) {
152
163
uint32_t canId = h1;
@@ -170,56 +181,35 @@ bool transportSend(const uint8_t to, const void* data, const uint8_t len, const
170
181
uint8_t buff[8 ];
171
182
uint8_t j=0 ;
172
183
// memcpy(buff,datap[i*8+j],partlen);
173
- Serial.print (" send partLen: " );
174
- Serial.println (partLen);
175
- Serial.print (" send data: " );
176
184
for (j = 0 ; j < partLen; j++) {
177
185
buff[j]=datap[i*8 +j];
178
- Serial.print (buff[j]);
179
- Serial.print (" ;" );
180
186
}
187
+ CAN_DEBUG (PSTR (" CAN:SND:LN=%" PRIu8 " ,DTA0=%" PRIu8 " ,DTA1=%" PRIu8 " ,DTA2=%" PRIu8 " ,DTA3=%" PRIu8 " ,DTA4=%" PRIu8 " ,DTA5=%" PRIu8 " ,DTA6=%" PRIu8 " ,DTA7=%" PRIu8 " \n " ), partLen, buff[0 ], buff[1 ], buff[2 ],buff[3 ],buff[4 ],buff[5 ],buff[6 ],buff[7 ]);
181
188
189
+ CAN_DEBUG (PSTR (" CAN:SND:LN=%" PRIu8 " ,CANH=%" PRIu32 " \n " ), partLen, canId);
182
190
183
- Serial.println (" ." );
184
-
185
- Serial.print (" send CAN len: " );
186
- Serial.println (len);
187
- Serial.print (" send CAN noOfFrames: " );
188
- Serial.println (noOfFrames);
189
- Serial.print (" send CAN id: " );
190
- Serial.println (canId);
191
191
byte sndStat = CAN0.sendMsgBuf (canId, partLen, buff);
192
192
if (sndStat == CAN_OK) {
193
- Serial. print ( " send packet sent \n " );
193
+ CAN_DEBUG ( PSTR ( " CAN:SND:OK \n " ) );
194
194
return true ;
195
195
} else {
196
- Serial. print ( " send packet send error \n " );
196
+ CAN_DEBUG ( PSTR ( " !CAN:SND:FAIL \n " ) );
197
197
return false ;
198
198
}
199
199
}
200
200
}
201
201
bool transportDataAvailable (void )
202
202
{
203
- if (!hwDigitalRead (CAN0_INT )) // If CAN0_INT pin is low, read receive buffer
203
+ if (!hwDigitalRead (CAN_INT )) // If CAN_INT pin is low, read receive buffer
204
204
{
205
205
CAN0.readMsgBuf (&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
206
- Serial.print (" READ CAN id: " );
207
- Serial.println (rxId);
208
206
long unsigned int from=(rxId & 0x000000FF );
209
- Serial.print (" READ CAN from: " );
210
- Serial.println (from);
211
207
long unsigned int to=(rxId & 0x0000FF00 )>>8 ;
212
- Serial.print (" READ CAN to: " );
213
- Serial.println (to);
214
208
long unsigned int currentPart=(rxId & 0x000F0000 )>>16 ;
215
- Serial.print (" READ CAN currentPart: " );
216
- Serial.println (currentPart);
217
209
long unsigned int totalPartCount=(rxId & 0x00F00000 )>>20 ;
218
- Serial.print (" READ CAN totalPartCount: " );
219
- Serial.println (totalPartCount);
220
210
long unsigned int messageId=(rxId & 0x07000000 )>>24 ;
221
- Serial. print ( " READ CAN messageId: " );
222
- Serial. println (messageId);
211
+ CAN_DEBUG ( PSTR ( " CAN:RCV:CANH=% " PRIu32 " ,FROM=% " PRIu8 " ,TO=% " PRIu8 " ,CURR=% " PRIu8 " ,TOTAL=% " PRIu8 " ,ID=% " PRIu8 " \n " ), rxId, from, to, currentPart,totalPartCount,messageId );
212
+
223
213
uint8_t slot;
224
214
if (currentPart==0 ){
225
215
slot=_findCanPacketSlot ();
@@ -231,28 +221,19 @@ bool transportDataAvailable(void)
231
221
memcpy (packets[slot].data + packets[slot].len , rxBuf, len);
232
222
packets[slot].lastReceivedPart ++;
233
223
packets[slot].len += len;
234
- Serial.println (" READ CAN data: " );
235
- for (uint8_t k=0 ;k<packets[slot].len ;k++){
236
- Serial.print (packets[slot].data [k]);
237
- Serial.print (" , " );
238
- }
239
- Serial.println (" " );
240
- Serial.print (" READ CAN SLOT: " );
241
- Serial.println (slot);
242
- Serial.print (" READ CAN lastReceivedPart: " );
243
- Serial.println (packets[slot].lastReceivedPart );
224
+ CAN_DEBUG (PSTR (" CAN:RCV:SLOT=%" PRIu8 " ,PART=%" PRIu8 " \n " ), slot, packets[slot].lastReceivedPart );
244
225
if (packets[slot].lastReceivedPart == totalPartCount) {
245
226
packets[slot].ready = true ;
246
- Serial. print ( " READ CAN packet received \n " );
227
+ CAN_DEBUG ( PSTR ( " CAN:RCV:SLOT=% " PRIu8 " complete \n " ), slot );
247
228
return true ;
248
229
}
230
+
249
231
}
250
232
}
251
233
return false ;
252
234
}
253
235
uint8_t transportReceive (void * data)
254
236
{
255
- Serial.print (" transport receive called\n " );
256
237
uint8_t slot=bufSize;
257
238
uint8_t i;
258
239
for (i = 0 ; i < bufSize; i++) {
@@ -262,12 +243,6 @@ uint8_t transportReceive(void* data)
262
243
}
263
244
if (slot<bufSize) {
264
245
memcpy (data,packets[slot].data ,packets[slot].len );
265
- Serial.print (" transport receive data: " );
266
- for (uint8_t k=0 ;k<packets[slot].len ;k++){
267
- Serial.print (packets[slot].data [k]);
268
- Serial.print (" , " );
269
- }
270
- Serial.println (" " );
271
246
i=packets[slot].len ;
272
247
_cleanSlot (slot);
273
248
return i;
0 commit comments