This project implements a real-time speed sensor and CAN communication system on a STM32 B-U585I-IOT02A microcontroller using the ThreadX RTOS. The firmware reads pulses from a wheel speed sensor, calculates the wheel’s RPM, and transmits this data over the CAN bus. Debug messages are available via UART.
-
ThreadX RTOS: Manages concurrent threads for sensor reading and CAN transmission.
-
Wheel Speed Measurement: Uses a timer to count pulses from a speed sensor and calculates RPM.
-
CAN Bus Communication: Sends speed and other messages using STM32 FDCAN peripheral.
-
UART Debug Output: Prints debug and status messages for development and troubleshooting.
- Main application source files
- Source files under ThreadX(threads, CAN, sensor logic, main.c, etc.)
- Header files for application and hardware abstraction.
- STM32 HAL and CMSIS drivers.
- ThreadX kernel and threads are initialized.
- CAN queue is created for inter-thread communication.
- The sensor thread reads the timer counter, calculates RPM, and enqueues a CAN message.
- The CAN_TX thread dequeues messages and sends them over the CAN bus.
- UART output provides real-time feedback for development and troubleshooting.
- FDCAN1_RX -> PB_8 (CAN_RX)
- FDCAN1_TX -> PB_9 (CAN_TX)
- TIM1_CH1 -> PA_8 (Sensor speed)
- Add new CAN message types by extending the t_can_msg struct and switch-case in the CAN_TX thread.
- Integrate additional sensors or actuators by creating new threads.
- Implement a CAN_RX thread to receive, decode, and process incoming CAN frames.
- Integrate new thread to communicate via I2C to motors/servo.
- ARM GCC toolchain (
arm-none-eabi-gcc) - CMake
- ST-Link tools (
stlink-tools)
mkdir -p build
cd buildcmake -DCMAKE_TOOLCHAIN_FILE=../cmake/gcc-arm-none-eabi.cmake ..make -j$(nproc)arm-none-eabi-objcopy -O binary ThreadX_Os.elf ThreadX_Os.binst-info --probeExpected output should show your STM32 device information.
st-flash erasest-flash --reset write ThreadX_Os.bin 0x08000000Parameters:
0x08000000- Flash memory start address for STM32--reset- Automatically reset the microcontroller after flashing
After initial setup, you only need:
cd build
make -j$(nproc)
arm-none-eabi-objcopy -O binary ThreadX_Os.elf ThreadX_Os.bin
st-flash erase
st-flash --reset write ThreadX_Os.bin 0x08000000- Debug messages are sent via USART1 at 115200 baud, 8N1.
- Use a serial terminal (minicom, screen, etc.) to view output.
- Use a USB cable if your board has a Virtual COM Port (e.g., ST-Link VCP)
- Or use a USB-to-Serial adapter connected to the board's UART TX/RX pins
On Linux, run:
sudo dmesg | grep ttyLook for /dev/ttyACM0, /dev/ttyUSB0, or similar.
You can use minicom or screen.
minicom example:
minicom -b 115200 -D /dev/ttyACM0screen example:
screen /dev/ttyACM0 115200- Baud rate: 115200
- Data bits: 8
- Parity: None
- Stop bits: 1
You should now see the debug messages printed by your firmware.
- Press Ctrl+A then release both and press K
- When prompted, press y to confirm exit.