5 releases

new 0.1.4 Apr 22, 2024
0.1.3 Apr 22, 2024
0.1.2 Apr 4, 2024
0.1.1 Dec 11, 2023
0.1.0 Dec 8, 2023

#852 in Hardware support

Download history 2/week @ 2024-02-20 4/week @ 2024-02-27 4/week @ 2024-03-12 2/week @ 2024-03-26 186/week @ 2024-04-02 9/week @ 2024-04-09 2/week @ 2024-04-16

199 downloads per month

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

~6.5–8.5MB
~173K SLoC