Files
Seeed_Arduino_CAN/examples/receive_interrupt/receive_interrupt.ino
T
Ralf Edmund Stranzenbach 0a791f6d4b Receive may freeze if CAN saturated
If either the CANBUS is saturated or the MCU is busy for some time, both RX buffers of the MCP2515 may be in use. If the MCU does not catch up in processing the incoming messages, the second message received just adds another reason for the IRQ.
Thus reading a single message does not clear the IRQ conditon of the MCP2515 leading to a permanent lock up of the receiver program.

Signed-off-by: Ralf Edmund Stranzenbach <ralf@reswi.de>
2014-10-14 23:53:17 +02:00

68 lines
1.7 KiB
Arduino

// demo: CAN-BUS Shield, receive data with interrupt mode
// when in interrupt mode, the data coming can't be too fast, must >20ms, or else you can use check mode
// loovee, 2014-6-13
#include <SPI.h>
#include "mcp_can.h"
MCP_CAN CAN(10); // Set CS to pin 10
unsigned char Flag_Recv = 0;
unsigned char len = 0;
unsigned char buf[8];
char str[20];
void setup()
{
Serial.begin(115200);
START_INIT:
if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init ok!");
}
else
{
Serial.println("CAN BUS Shield init fail");
Serial.println("Init CAN BUS Shield again");
delay(100);
goto START_INIT;
}
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
}
void MCP2515_ISR()
{
Flag_Recv = 1;
}
void loop()
{
if(Flag_Recv) { // check if get data
Flag_Recv = 0; // clear flag
// iterate over all pending messages
// If either the bus is saturated or the MCU is busy,
// both RX buffers may be in use and reading a single
// message does not clear the IRQ conditon.
while (CAN_MSGAVAIL == CAN.checkReceive()) {
// read data, len: data length, buf: data buf
CAN.readMsgBuf(&len, buf);
// print the data
for(int i = 0; i<len; i++) {
Serial.print(buf[i]);Serial.print("\t");
}
Serial.println();
}
}
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/