ATmega4808 Run CAN module over SPI2

I made my own PCB with ATmega4808 (32pin, with MegaCoreX) and connected to it a CAN bus module based on MCP2515 & TJA1050. I connected it to the SPI2 (pins PC0, PC1, PC2). Now I’m having problems communicating with it.

I’m using the arduino-CAN library by Sandeep Mistry GitHub - sandeepmistry/arduino-CAN: An Arduino library for sending and receiving data using CAN bus. (I used it in the past with an UNO with no problems).

I could not figure out how to tell the code to use the second SPI bus.

I’m Using the library "CANReciver.ino" example.

I tried to add to the setup()

SPI.setMOSI(PIN_PC0);
SPI.setMISO(PIN_PC1);
SPI.setSCLK(PIN_PC2);

Or

SPIClass SPI_2(PIN_PC0, PIN_PC1, PIN_PC2);

But they both don’t compile.

What is the correct way to let the library use the SPI2 for the CAN module and not the default SPI1?

Either

  SPI.pins(PIN_SPI_MOSI_PINSWAP_1, PIN_SPI_MISO_PINSWAP_1, PIN_SPI_SCK_PINSWAP_1, PIN_SPI_SS_PINSWAP_1);

or, simpler,

  SPI.swap(1);

should do what you want, which appears to be to use the alternate pins for the 4808's one SPI peripheral.

1 Like

Tested this and it works well.
Thanks

1 Like