6 releases

0.1.5 Apr 30, 2024
0.1.4 Apr 22, 2024
0.1.1 Dec 11, 2023

#1404 in Hardware support

GPL-3.0 license

1MB
281 lines

Contains (static library, 1MB) c_lib/libcan2040.a

can2040-rust

This is a port of can2040 in C. Based on this, we can enable CAN on RP2040 with Rust.

Run CAN demo

Demo devices

Wiring

  • RP2040 GPIO 26 <=> CAN RX

  • RP2040 GPIO 27 <=> CAN TX

  • RP2040 GND <=> CAN GND

  • RP2040 3V3 <=> CAN 3V3

  • CAN CAN-H <=> USB CAN-H

  • CAN CAN-L <=> USB CAN-L

Run can2040_demo

The demo is run on Linux, using a relatively low baud rate of 10_000. After debugging, you can choose a higher baud rate yourself.

Make USB ready

Plug the USB into your computer and make sure the corresponding socket is enabled (here it corresponds to socket can0).

sudo ip link set down can0 && \
sudo ip link set can0 type can bitrate 10000 && \
sudo ip link set up can0

Then, run the following command to monitor the messages on the CAN bus for debugging:

candump can0

Run Can2040_demo on RP2040.

Make sure all the wiring is correct, and if you adjust the GPIOs, make sure the corresponding GPIOs in the code are adjusted as well. Once everything is ready, you can run the following command, and you should see RP2040 continuously sending CAN frames to the USB:

cargo run --release --example can2040_demo

Send to test the receive

Run the following command on your computer and make sure the RP2040 console window receives the information:

cansend can0 123#DEADBEEF12345678 

Some tips

  • A logic analyzer can be connected to CAN-H / L in differential mode to obtain the correct CAN signals. The connection method is to connect CAN-H to the signal and CAN-L to GND.
  • A dual-channel oscilloscope can also be connected to CAN-H/L to view the differential signal.
  • Global common ground is not required. Even if the obtained signal is interfered with, SN65HVD230 can still extract the normal CAN signal for TX/RX.

Dependencies

~11MB
~222K SLoC