Canbus and Software serial issues

Hi there, i am running the software Serial library to interface with an RFID unit, and this canbus library to interface with a MCP2515 can bus chip

the problem is that they seem to disable each other, either can bus messages don’t come through or the RFID doesn’t respond.

i assume that there must be an interrupt timer that is conflicting but its beyond my skill

below is my code. in this example the can bus is simply sent a 1 or a 0, which turns on or off an LED, the RFID does NOT work, but if i remove or disable the can bus software then the RFID does work.

this means that hardware wise everything works as it should, and software wise each individual part works, but together they don’t.

i am stuck with the pin configurations so using AltSoftwareSerial isn’t possible,

does anyone have any ideas? thanks!

#include "mcp_can.h"
#include <SPI.h>
#include <SoftwareSerial.h>

#define RFID_RX_PIN 10
#define RFID_TX_PIN 9


SoftwareSerial RFID(RFID_RX_PIN, RFID_TX_PIN);
MCP_CAN CAN(A4);

int pinLED     = A3;
int pinCANrst  = 6;

int dataLen = 0;
int raw[4];
unsigned long cardNumber = 0;
int tagReceived = false;

volatile int  canReceiveFlag  = false;
unsigned char len = 0;
unsigned char buf[8];


void setup()
{
  Serial.begin   (9600);
  RFID.begin(9600);
  pinMode(pinLED,OUTPUT);
  pinMode(pinCANrst,  OUTPUT);
  digitalWrite(pinCANrst, HIGH);


  attachInterrupt(0, canMessageReceived, FALLING); // start interrupt

  if (CAN.begin(CAN_500KBPS) == CAN_OK) Serial.println("CANBUS initialised");
  else Serial.println("CANBUS initialisation failure");

}

void canMessageReceived()
{
  canReceiveFlag = 1;
}

void loop(){
  if(canReceiveFlag){
    canReceiveFlag = false;
    CAN.readMsgBuf(&len, buf);    // read data,  len: data length, buf: data buf
    digitalWrite(pinLED, buf[0]);
  }  
  
  while (RFID.available()){
    raw[dataLen++] = RFID.read();
    delay(10);
    if (dataLen >= 4)generateNumber();
  }
  if (tagReceived) {
    Serial.println(cardNumber);
    tagReceived = false;
  }
}


void generateNumber(){
  unsigned long a = 0;
  if(0 != raw[0]){
    a = a + raw[0];
    a = a<<24;
  }
  a = a + raw[1];
  a = a<<16;
  unsigned long b = 0;
  b = b  + raw[2];
  b = b <<8;
  b = b  + raw[3];
  a = a + b;
  cardNumber = a;
  tagReceived = true;
  dataLen = 0;
}

You need to post a link to the mcp_can library and to any shield you are using. I’m going to guess that the shield uses SPI, which uses pins 11, 12, and 13, and requires that pin 10 be an OUTPUT pin, which, then, means that you can’t use pin 10 for SoftwareSerial. Post the links and prove me wrong.

i am stuck with the pin configurations so

Hope you got come Vaseline with those pin assignments.

PaulS:
You need to post a link to the mcp_can library and to any shield you are using. I'm going to guess that the shield uses SPI, which uses pins 11, 12, and 13, and requires that pin 10 be an OUTPUT pin, which, then, means that you can't use pin 10 for SoftwareSerial. Post the links and prove me wrong.
Hope you got come Vaseline with those pin assignments.

thanks for the reply,

the MCP_CAN library is this one: GitHub - Seeed-Studio/Seeed_Arduino_CAN: Seeed Arduino CAN-BUS library - MCP2518FD&MCP2515&MCP2551

there are no shields, this is custom hardware, but it matches the shield design of things like the Seeeduino canbus shield except the CS pin is on A4 instead of D10,

RFID RX is on pin D10, but i can't remember if that is receive for the RFID or what is received from the RFID.

but yea its pretty restrictive with the pin assignments, i was hoping i could move to a different timer in the library or something. i just need to get software serial and this MCP_CAN library to play nice and then its downhill from there.

there are no shields, this is custom hardware, but it matches the shield design of things like the Seeeduino canbus shield except the CS pin is on A4 instead of D10,

See lines 48 and 49 of the mcp_can.cpp file. The library does use SPI, which will mean that pins 10 to 13 are not available for you to use AND that the custom hardware MUST be using those pins.

Pin 10 must still be declared an OUTPUT pin, to make the Arduino an SPI master.

PaulS:
See lines 48 and 49 of the mcp_can.cpp file. The library does use SPI, which will mean that pins 10 to 13 are not available for you to use AND that the custom hardware MUST be using those pins.

Pin 10 must still be declared an OUTPUT pin, to make the Arduino an SPI master.

thanks for the reply, sorry but i am a bit confused, why is pin 10 used? is that part of the SPI library? is it a hardware based thing or software?

the reason i am confused is because the canbus works just fine without pin 10 connected (As it uses A4) so why can't i use that pin for something else?

thank you for your help, at least i know more about what the issue is

why is pin 10 used? is that part of the SPI library? is it a hardware based thing or software?

See the last line of reply # 3, which you quoted. It's a hardware thing.

the reason i am confused is because the canbus works just fine without pin 10 connected

Of course it does. There doesn't need to actually be anything connected to the pin. But, there could be, if some external device needed to know if it was connected to an SPI master or an SPI slave.

The point is that pin 10 MUST be an OUTPUT and you are trying to use it as an INPUT.

PaulS:
See the last line of reply # 3, which you quoted. It's a hardware thing.
Of course it does. There doesn't need to actually be anything connected to the pin. But, there could be, if some external device needed to know if it was connected to an SPI master or an SPI slave.

The point is that pin 10 MUST be an OUTPUT and you are trying to use it as an INPUT.

thank you for the reply, i did a little more research and now i understand. the SPI is a hardware based device inside the ATmega328, and pin 10 is both the common CS pin but also the ATmega's way of setting the master or slave on the SPI hardware.

but i assume that if i was able to swap the RX and TX lines on the RFID so that pin 10 was the transmit, in theory it would work?