The following code is meant to log all CAN traffic on two separate vehicle CAN networks on the same vehicle. CAN1=125000k, CAN2=500000k. The logging part seems to work well. However, during testing this logger in my vehicle I noticed some odd behaviors of the vehicle when the mBed was connected. Every now and again the blinkers would skip a few flashes and the radio display would reset. I hooked up an independent CAN monitoring tool (Vector CANAlyzer) and noticed the following:
1) Maximum bus load was ~30%. This is good.
2) Error frames on the CAN1=125000k bus. This is not good. I think the error frames are preventing some of the network messages from getting through.
3) No error frames on CAN2=500000k
To isolate where the error frames are coming from I did the following:
1) Unplugged my logging tool from the OBDII connector. Error Frames went away.
2) Reconnected the logging tool to the OBDII connector and then unplugged the mBed from the MCP2551 CAN Transceivers. This means that the MCP2551 circuits were still connected the CAN bus. The problem went away. This seems to indicate that the issue is not with the MCP2551 CAN Hardware but with the mBed itself.
My assumption is that the mBed is passively reading the CAN buses. If this is the case I can’t see how the mBed could be generating error frames. Perhaps my assumptions are incorrect and the mBed is not passively reading the CAN Bus. Perhaps the mBed is ACK/NACK the messages incorrectly?
Either I’m missing something or there is a bug in the mBed CAN library? Anyone have any ideas? Is there any more data I could collect to try and isolate this issue?
Thanks.
<<<< CODE >>>>>
// Full Bus CAN Monitor and Logger Tool
//
// Two CAN Buses are monitored and logged to PC via USB port.
// CAN messages are time-stamped and sorted before sending them out the serial port.
// Freddie Leaf
// June 20, 2010
// Version 1.0
#include "mbed.h"
#include "CAN.h"
#define TRUE 1
#define FALSE 0
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
// Setup CAN1 Controller, MSCAN 125K
CAN can1(p9, p10);
// Setup CAN2 Controller, HSCAN 500K
CAN can2(p30, p29);
// Setup USB Serial Port
Serial pc(USBTX, USBRX); // tx, rx
int main()
{
char MessageFoundCAN1;
char MessageFoundCAN2;
int TimeStampCAN1;
int TimeStampCAN2;
CANMessage CAN1_MsgRx; // Message reception on CAN1
CANMessage CAN2_MsgRx; // Message reception on CAN2
Timer FreeRunningTimer; //Free running timer for CAN message stamp
// USB Serail Port baud rate
pc.baud(230400);
// CAN1, MSCAN, 125kbit/s
can1.frequency(125000);
// CAN2, HSCAN, 500kbit/s
can2.frequency(500000);
/****** Program Starts Here *******/
pc.printf("Connected to mBed...\r\n");
// This is a free running timer that is used for the CAN message time stamp
FreeRunningTimer.start();
// For completeness, initialize these Time Stamps to 0
TimeStampCAN1 = 0;
TimeStampCAN2 = 0;
while (1)
{
MessageFoundCAN1 = FALSE; // Do this every loop so that we dont log old data
MessageFoundCAN2 = FALSE; // Do this every loop so that we dont log old data
if ( can1.read(CAN1_MsgRx) ) // See if any CAN messages are in CAN1 Buffer- MSCAN
{
TimeStampCAN1 = FreeRunningTimer.read_us();
MessageFoundCAN1 = TRUE;
led1 = !led1; // Toggle LED1
}
if ( can2.read(CAN2_MsgRx) ) // See if any CAN messages are in CAN2 Buffer- HSCAN
{
TimeStampCAN2 = FreeRunningTimer.read_us();
MessageFoundCAN2 = TRUE;
led2 = !led2; // Toggle LED2
}
if( MessageFoundCAN1 || MessageFoundCAN2 ) // Message were found on CAN1 or CAN2 so print them.
{
if( TimeStampCAN1 < TimeStampCAN2 ) // CAN1 Message is earlier message
{
if ( MessageFoundCAN1 == TRUE ) // only print if message was found
{
// Print CAN1 Message First
pc.printf("%d %04X 1 ", TimeStampCAN1, CAN1_MsgRx.id);
for (char i=0; i<CAN1_MsgRx.len; i++)
{
pc.printf("%02X ", CAN1_MsgRx.data[i]);
}
pc.printf ("%d", FreeRunningTimer.read_us() - TimeStampCAN1 ); // debug code
printf("\r\n");
led4 = !led4; // Toggle LED4 to show that somehting was sent to the PC via USB
}
if ( MessageFoundCAN2 == TRUE ) // only print if message was found
{
// Print CAN2 Message Second
pc.printf("%d %04X 2 ", TimeStampCAN2, CAN2_MsgRx.id);
for (char i=0; i<CAN2_MsgRx.len; i++)
{
pc.printf("%02X ", CAN2_MsgRx.data[i]);
}
pc.printf ("%d", FreeRunningTimer.read_us() - TimeStampCAN2 ); // debug code
printf("\r\n");
led4 = !led4; // Toggle LED4 to show that somehting was sent to the PC via USB
}
}
else // CAN2 Message is earlier message or both messages have the same time stamp
{
if ( MessageFoundCAN2 == TRUE ) // only print if message was found
{
// Print CAN2 Message First
pc.printf("%d %04X 2 ", TimeStampCAN2, CAN2_MsgRx.id);
for (char i=0; i<CAN2_MsgRx.len; i++)
{
pc.printf("%02X ", CAN2_MsgRx.data[i]);
}
pc.printf ("%d", FreeRunningTimer.read_us() - TimeStampCAN2 ); // debug code
printf("\r\n");
led4 = !led4; // Toggle LED4 to show that somehting was sent to the PC via USB
}
if ( MessageFoundCAN1 == TRUE ) // only print if message was found
{
// Print CAN1 Message Second
pc.printf("%d %04X 1 ", TimeStampCAN1, CAN1_MsgRx.id);
for (char i=0; i<CAN1_MsgRx.len; i++)
{
pc.printf("%02X ", CAN1_MsgRx.data[i]);
}
pc.printf ("%d", FreeRunningTimer.read_us() - TimeStampCAN1 ); // debug code
printf("\r\n");
led4 = !led4; // Toggle LED4 to show that somehting was sent to the PC via USB
}
}//else
}//if
}//while
}//main
The following code is meant to log all CAN traffic on two separate vehicle CAN networks on the same vehicle. CAN1=125000k, CAN2=500000k. The logging part seems to work well. However, during testing this logger in my vehicle I noticed some odd behaviors of the vehicle when the mBed was connected. Every now and again the blinkers would skip a few flashes and the radio display would reset. I hooked up an independent CAN monitoring tool (Vector CANAlyzer) and noticed the following:
1) Maximum bus load was ~30%. This is good.
2) Error frames on the CAN1=125000k bus. This is not good. I think the error frames are preventing some of the network messages from getting through.
3) No error frames on CAN2=500000k
To isolate where the error frames are coming from I did the following:
1) Unplugged my logging tool from the OBDII connector. Error Frames went away.
2) Reconnected the logging tool to the OBDII connector and then unplugged the mBed from the MCP2551 CAN Transceivers. This means that the MCP2551 circuits were still connected the CAN bus. The problem went away. This seems to indicate that the issue is not with the MCP2551 CAN Hardware but with the mBed itself.
My assumption is that the mBed is passively reading the CAN buses. If this is the case I can’t see how the mBed could be generating error frames. Perhaps my assumptions are incorrect and the mBed is not passively reading the CAN Bus. Perhaps the mBed is ACK/NACK the messages incorrectly?
Either I’m missing something or there is a bug in the mBed CAN library? Anyone have any ideas? Is there any more data I could collect to try and isolate this issue?
Thanks.
<<<< CODE >>>>>