2
\$\begingroup\$

Recently I have new request to add CAN interface for communication, but there is no CAN interface in current MCU. Then I found I can add a CAN controller with SPI interface to achieve.

My question is that if I want to reserve my only one SPI interface for other application, is there any choice to select a CAN controller with UART interface?

Since I find some CAN controller IC with SPI interface, such as microchip. But it is hard to select a IC that with UART interface built-in CAN controller.

or I have only two plan, 1 is using CAN controller with SPI interface, and another is to change the MCU to an built-in CAN, SPI interfaces?

\$\endgroup\$
3
  • 5
    \$\begingroup\$ Is there a reason you can't use the single SPI interface for more than one chip? \$\endgroup\$
    – Justme
    Commented Dec 2, 2022 at 15:55
  • 1
    \$\begingroup\$ As @Justme says, SPI is a multidrop bus, so it can connect to lots of SPI target ICs. The SPI bus master must provide a unique /CS for each target. MCUs often use GPIOs for the /CS pins. Can you do that with your SPI bus? \$\endgroup\$
    – TonyM
    Commented Dec 2, 2022 at 16:16
  • \$\begingroup\$ Using external CAN controllers is the stone age way of implementing CAN however... it is most definitely the very last resort. You would only do it when you have to use a certain MCU with very exotic requirements, which at the same time doesn't come with a CAN controller on-chip. If you can change to a MCU with CAN controllers, then that's the correct solution, period. \$\endgroup\$
    – Lundin
    Commented Dec 5, 2022 at 9:00

3 Answers 3

0
\$\begingroup\$

You could implement a bit banged version of CAN if the rates are slow, but this is not recommended

Really you need the dedicated hardware interface.

I would try and use the SPI bus if possible. Usually that would be adding another CS line, but if you are limited by bandwidth or timing then that would not be a good option. If you are not, then that is the way I would do it.

There are also I2C to CAN IC's available, so if you have a space I2C port then try that.

\$\endgroup\$
0
3
\$\begingroup\$

Any MCU with CAN and UART can do it, if you write software for it.

However, it may be best to share the SPI port using the chip select lines. SPI is a one-to-many bus so you should be fine.

\$\endgroup\$
2
\$\begingroup\$

I do not know what processor you are using but if it is an arduino Cory J. Fowler has written a fantastic library for the MPC2515 that includes both send and receive code for the Arduinos. The MPC2515 modules are available a lot of vendors, even from your favorite china supplier for a few dollars. They come with the CAN controller and the Phy layer interface. Bit banging will take a lot of code and has very critical timing. That controller removes a lot of software from the processor while keeping track of errors etc.

The only limit to the number of devices you put on the SPI bus is the number of select lines you have. You select the device you want and then communicate with it. You can select more than one but this can generate some very unique problems if the targets were not designed for this. This is what the SPI was designed to do. It is actually bi-directional as it has two wires. Data is clocked out by the master and is received by the slave but uses the master clock eliminating potential timing errors. You can change polarity and phase to however you want it or in realaity what format the target SPI device needs.

CAN can be a bit fussy and needs to be terminated at only the two physical ends with a 120 Ohm Resistor. I have found I need at least 1 meter of cable in the bus. Another gotcha is it will not send messages unless it receives an ACK in the appropriate place in the frame it is sending. Yes it monitors what it sends, the bus has both dominant and recessive states that allow the remote to put a dominate bit on the bus during the packet transmission. If you do not have the tools you can use an Arduino with the MPC2515 module and monitor the bus via the serial monitor. The receive.ino in the library will do this.

\$\endgroup\$
1
  • \$\begingroup\$ There is really no reason why anyone would use an Arduino and an external CAN controller if it can be avoided. It's a clunky, outdated solution - external controllers is a thing of the 1990s. Also it's a particularly bad way of learning about CAN, since you'd be distracted with the SPI crap. Also MPC2515 is an old technology controller, since some 10+ years back modern CAN controllers use mailbox features instead of those old, bad filter/acceptance mask solutions. And of course more IC = more expensive BoM. \$\endgroup\$
    – Lundin
    Commented Dec 5, 2022 at 9:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.