CAN BUS shield with different mode SPI devices

Hello,

I am using the CANBUS shield in my proejct and I would like to use an OLED SPI display.

The problem is that the OLED display uses the US2066 controller and its SPI settings are LSBFIRST, MODE3.

I can successfuly use the CANBUS shield with a MAX31855 for example which uses the same settings as the CANBUS (MSBFIRST,MODE0), or use the OLED display with the MAX31855 alone but the CANBUS and the OLED display together are not working.

I am using transactional SPI to change the settings but if the CANBUS library and CAN.begin command are present, the sketch freezes.

I looked at the CANBUS library code and I see that it is not using transactional SPI. Changed the code to use beginTransaction in the _SELECT() function instead of just setting the CSPI to LOW and endTransaction in the _UNSELECT function but the result is the same.

Anyone has any idea how to overcome this issue?

Thank you

Transactional SPI should solve that issue. I guess that one of the involved libraries don't act correctly. Please provide links to the libraries used. Also post a wiring diagram of all connections from the Arduino to the three devices.

Hello, thanks for your answer..

I only use the CAN-BUS library: https://github.com/Seeed-Studio/CAN_BUS_Shield

If you see the dfs.h file in SELECT() and UNSELECT() functions it only digitalWrites LOW or HIGH to select or unselect the pin. I tried adding SPI.beginTransaction(4000000,MSBFIRST,SPI_MODE0); beofre the LOW write and SPI.endTransaction(); after the HIGH write but nothing changed.

Then in my main loop I tried using the transactions before and after each call to the library and again it is not working..

Now, 2 things I noticed. If I comment out the reset function (which only does SPI.transfer(0xC0)) it works! Sounds odd but this seems to cause the problem.. But then, how am i going to initialize the CANBUS shield?

Also I noticed something else.. With the change above it works but if I unplug the arduino from the PC and then plug it again it works until the moment the Windows USB recognition sound plays! Then it stops.. If I power it from a USB mobile phone charger it works.. Any ideas on that?

The connections are the typical SPI connections to the Arduino and CAN shield and I am using pin 9 for the CS for the CANBUS and pin 5 for the OLED display..

I only use the CAN-BUS library: https://github.com/Seeed-Studio/CAN_BUS_Shield

Then post the complete code you use to access the other SPI devices (MAX31855 and the OLED display).

If you see the dfs.h file in SELECT() and UNSELECT() functions it only digitalWrites LOW or HIGH to select or unselect the pin. I tried adding SPI.beginTransaction(4000000,MSBFIRST,SPI_MODE0); beofre the LOW write and SPI.endTransaction(); after the HIGH write but nothing changed.

Interesting that this compiles on your IDE because on mine it doesn’t.

Also I noticed something else… With the change above it works but if I unplug the arduino from the PC and then plug it again it works until the moment the Windows USB recognition sound plays! Then it stops… If I power it from a USB mobile phone charger it works… Any ideas on that?

What type of Arduino are you using? A standard UNO?

I only use the CAN-BUS library: https://github.com/Seeed-Studio/CAN_BUS_Shield

That library has support for SPI transactions (macros SPI_BEGIN and SPI_END). Why don’t you use that?

When I initially created the project the library didn't support transactions.. I now saw it thanks to you!

But the problem was not that finally.. I did bench test the code without the can bus shield connected and the init function from the can bus example is in a while loop.. Dah! So as the shield was not present it was infinitely trying to initiate and it froze..

So now everything is working as it should except from when I connect the canbus shield to the bus.. Then the OLED display is showing garbage!

I believe this is happening because the CAN-BUS is using interrupts and does not clear the SPI RX buffer.. Is there any way I can achieve that? I have found a Teensy thread using SPI_MCR0 but it does not compile for my Arduino Nano..

I believe this is happening because the CAN-BUS is using interrupts and does not clear the SPI RX buffer..

There is no SPI RX buffer. The hardware has one byte register to hold the data that is transfered and received.

I don't think that the CAN shield is the problem but probably the OLED display. But as you still didn't post the code you use to access all of your devices I cannot check that.