This implementation provides a CAN Bus to USB adapter using the STM32F103C8 microcontroller (commonly known as Bluepill). It supports three configurations for interacting with a CAN Bus network, enabling communication via USB or UART, while leveraging different CAN transceiver options. The adapter uses the slcan protocol (CAN over Serial), making it compatible with popular software tools such as SocketCAN, Python-can, and other slcan-compatible applications.
The Bluepill implementation supports the following configurations:
-
USB and MCP2515 (SPI to CAN)
- The USB port of the Bluepill is used for communication with the host system.
- The MCP2515 (SPI to CAN) module is used for CAN Bus communication.
- This configuration allows the device to interface with a CAN network while communicating with the host via USB.
Connections:
Function Bluepill MCP2515 Vcc 5v 5v GND GND GND MOSI A7 SI MISO A6 SO Clock A5 SCK CS A4 CS -
UART and MCP2515 (SPI to CAN)
- The UART port of the Bluepill is used to communicate with the host system.
- The MCP2515 (SPI to CAN) module is used for CAN Bus communication.
- This configuration is useful when the USB port is unavailable or when using a serial connection instead of USB.
Connections:
Function Bluepill MCP2515 USB-UART Vcc 5v 5v 5v GND GND GND GND MOSI A7 SI - MISO A6 SO - Clock A5 SCK - CS A4 CS - TX A2 - RX RX A3 - TX -
UART and Internal CAN Controller
- The UART port of the Bluepill is used to communicate with the host system.
- The internal CAN controller of the STM32F103C8 microcontroller is used for CAN Bus communication and one tranceiver (MCP2551 in this case).
- Note: The Bluepill's USB port and internal CAN controller cannot be used simultaneously. If the internal CAN controller is selected, the only available communication interface with the host is UART.
Connections:
Function Bluepill MCP2551 USB-UART Vcc 5v VDD 5v GND GND VSS GND CAN TX B8 TX - CAN RX B9 RX - RS GND RS - TX A2 - RX RX A3 - TX
Installing stlink tools
sudo apt update
sudo apt install stlink-tools
Alternatively, build the tools from source (if you need the latest version):
git clone https://github.com/stlink-org/stlink.git
cd stlink
make release
sudo make install
Ensure your firmware binary is compiled and ready to flash. You could download the release file doggie_bluepill_{serial}_{can}
with the desired configuration from the Release page.
- Connect the ST-LINK programmer to your computer via USB.
-
ST-LINK SWDIO
→Bluepill SWDIO
-
ST-LINK SWCLK
→Bluepill SWCLK
-
ST-LINK GND
→Bluepill GND
-
ST-LINK 3.3V
→Bluepill 3.3V
(if not powered externally)
- Power the Bluepill (either through the ST-LINK or an external power source).
- Open a terminal and navigate to the folder containing your downloaded firmware binary.
- Flash the firmware using
st-flash
:Explanation:st-flash write doggie_bluepill_usb_mcp 0x8000000
write
: Command to write the firmware.doggie_bluepill_usb_mcp
: The binary firmware file to be flashed.0x8000000
: Starting address of the STM32F103C8 flash memory.
-
Install Rust and cargo with support for ARM architecture.
Follow the installation instructions from the official Rust website. -
Add the target architecture:
rustup target add thumbv7m-none-eabi
-
Install
probe-rs
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/probe-rs/probe-rs/releases/latest/download/probe-rs-tools-installer.sh | sh
-
Connect Bluepill to the programmer
-
Build and flash with selected features
- USB and MCP2515:
cargo run --release --bin doggie_bluepill_usb_mcp
- UART and MCP2515:
cargo run --release --bin doggie_bluepill_uart_mcp
- UART and internal CAN:
cargo run --release --bin doggie_bluepill_uart_int
- USB and MCP2515: