Skip to content

Commit 7587e99

Browse files
committed
Improve logging
1 parent 12e2790 commit 7587e99

File tree

1 file changed

+25
-50
lines changed

1 file changed

+25
-50
lines changed

hal/transport/CAN/MyTransportCAN.cpp

Lines changed: 25 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,16 @@
1111
#include "hal/transport/CAN/driver/mcp_can.h"
1212
#include "hal/transport/CAN/driver/mcp_can.cpp"
1313
#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
1624
//since long messages can be sliced and arrive mixed with other messages assemble buffer is required
1725
#define bufSize 8 //TODO make configurable
1826
bool canInitialized=false;
@@ -54,11 +62,13 @@ void _initFilters() {
5462
CAN0.init_Filt(4,1,0xFFFFFFFF); // Init fifth filter.
5563
CAN0.init_Filt(5,1,0xFFFFFFFF); // Init sixth filter.
5664
CAN0.setMode(MCP_NORMAL);
57-
hwPinMode(CAN0_INT, INPUT);
65+
hwPinMode(CAN_INT, INPUT);
5866
}
5967
bool transportInit(void)
6068
{
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) {
6272
canInitialized=false;
6373
return false;
6474
}
@@ -147,6 +157,7 @@ bool transportSend(const uint8_t to, const void* data, const uint8_t len, const
147157
//shift left to create space for current frame number
148158
h2=h2 << 4;
149159
uint8_t i = 0;
160+
CAN_DEBUG(PSTR("CAN:SND:LN=%" PRIu8 ",NOF=%" PRIu8 "\n"), len, noOfFrames);
150161

151162
for (i = 0; i < noOfFrames; i++) {
152163
uint32_t canId = h1;
@@ -170,56 +181,35 @@ bool transportSend(const uint8_t to, const void* data, const uint8_t len, const
170181
uint8_t buff[8];
171182
uint8_t j=0;
172183
// memcpy(buff,datap[i*8+j],partlen);
173-
Serial.print("send partLen: ");
174-
Serial.println(partLen);
175-
Serial.print("send data: ");
176184
for (j = 0; j < partLen; j++) {
177185
buff[j]=datap[i*8+j];
178-
Serial.print(buff[j]);
179-
Serial.print(";");
180186
}
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]);
181188

189+
CAN_DEBUG(PSTR("CAN:SND:LN=%" PRIu8 ",CANH=%" PRIu32 "\n"), partLen, canId);
182190

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);
191191
byte sndStat = CAN0.sendMsgBuf(canId, partLen, buff);
192192
if (sndStat == CAN_OK) {
193-
Serial.print("send packet sent\n");
193+
CAN_DEBUG(PSTR("CAN:SND:OK\n"));
194194
return true;
195195
} else {
196-
Serial.print("send packet send error\n");
196+
CAN_DEBUG(PSTR("!CAN:SND:FAIL\n"));
197197
return false;
198198
}
199199
}
200200
}
201201
bool transportDataAvailable(void)
202202
{
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
204204
{
205205
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
206-
Serial.print("READ CAN id: ");
207-
Serial.println(rxId);
208206
long unsigned int from=(rxId & 0x000000FF);
209-
Serial.print("READ CAN from: ");
210-
Serial.println(from);
211207
long unsigned int to=(rxId & 0x0000FF00)>>8;
212-
Serial.print("READ CAN to: ");
213-
Serial.println(to);
214208
long unsigned int currentPart=(rxId & 0x000F0000)>>16;
215-
Serial.print("READ CAN currentPart: ");
216-
Serial.println(currentPart);
217209
long unsigned int totalPartCount=(rxId & 0x00F00000)>>20;
218-
Serial.print("READ CAN totalPartCount: ");
219-
Serial.println(totalPartCount);
220210
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+
223213
uint8_t slot;
224214
if(currentPart==0){
225215
slot=_findCanPacketSlot();
@@ -231,28 +221,19 @@ bool transportDataAvailable(void)
231221
memcpy(packets[slot].data + packets[slot].len, rxBuf, len);
232222
packets[slot].lastReceivedPart++;
233223
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);
244225
if (packets[slot].lastReceivedPart == totalPartCount) {
245226
packets[slot].ready = true;
246-
Serial.print("READ CAN packet received\n");
227+
CAN_DEBUG(PSTR("CAN:RCV:SLOT=%" PRIu8 " complete\n"), slot);
247228
return true;
248229
}
230+
249231
}
250232
}
251233
return false;
252234
}
253235
uint8_t transportReceive(void* data)
254236
{
255-
Serial.print("transport receive called\n");
256237
uint8_t slot=bufSize;
257238
uint8_t i;
258239
for (i = 0; i < bufSize; i++) {
@@ -262,12 +243,6 @@ uint8_t transportReceive(void* data)
262243
}
263244
if (slot<bufSize) {
264245
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("");
271246
i=packets[slot].len;
272247
_cleanSlot(slot);
273248
return i;

0 commit comments

Comments
 (0)