Skip to content

A thread based application running on ThreadX (RTOS) with continuous CAN communication to a Raspberry Pi. It is also responsible for controlling the car’s motors and servo via I²C.

Notifications You must be signed in to change notification settings

SEAME-pt/STM32_Microcontroller

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

STM32 ThreadX_Os

Overview

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.

Features

  • 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.

Directory Structure

Core.

  • Main application source files

Core/SRC.

  • Source files under ThreadX(threads, CAN, sensor logic, main.c, etc.)

Core/INC.

  • Header files for application and hardware abstraction.

Drivers.

  • STM32 HAL and CMSIS drivers.

How It Works

1. Initialization:

  • ThreadX kernel and threads are initialized.
  • CAN queue is created for inter-thread communication.

2. Speed Measurement:

  • The sensor thread reads the timer counter, calculates RPM, and enqueues a CAN message.

3. CAN Transmission:

  • The CAN_TX thread dequeues messages and sends them over the CAN bus.

4. Debugging:

  • UART output provides real-time feedback for development and troubleshooting.

STM32 Pins Usage

  • FDCAN1_RX -> PB_8 (CAN_RX)
  • FDCAN1_TX -> PB_9 (CAN_TX)
  • TIM1_CH1 -> PA_8 (Sensor speed)

How to Extend The Project

  • 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.

Instructions to Build and Flash to STM32 Microcontroller

Prerequisites

  • ARM GCC toolchain (arm-none-eabi-gcc)
  • CMake
  • ST-Link tools (stlink-tools)

Build Process

1. Create Build Directory

mkdir -p build
cd build

2. Configure CMake with ARM GCC Toolchain

cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/gcc-arm-none-eabi.cmake ..

3. Compile the Project

make -j$(nproc)

Flashing to STM32

4. Convert ELF to Binary Format

arm-none-eabi-objcopy -O binary ThreadX_Os.elf ThreadX_Os.bin

5. Verify ST-Link Connection

st-info --probe

Expected output should show your STM32 device information.

6. Erase Flash (Optional but Recommended)

st-flash erase

7. Write Binary to Flash Memory

st-flash --reset write ThreadX_Os.bin 0x08000000

Parameters:

  • 0x08000000 - Flash memory start address for STM32
  • --reset - Automatically reset the microcontroller after flashing

Quick Rebuild and Flash

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

UART Debug Messages

  • Debug messages are sent via USART1 at 115200 baud, 8N1.
  • Use a serial terminal (minicom, screen, etc.) to view output.

1. Connect the Board

  • 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

2. Find the Serial Port

On Linux, run:

sudo dmesg | grep tty

Look for /dev/ttyACM0, /dev/ttyUSB0, or similar.

3. Open a Serial Terminal

You can use minicom or screen.

minicom example:

minicom -b 115200 -D /dev/ttyACM0

screen example:

screen /dev/ttyACM0 115200

4. UART Settings

  • Baud rate: 115200
  • Data bits: 8
  • Parity: None
  • Stop bits: 1

5. View Output

You should now see the debug messages printed by your firmware.

6. Kill UART Window

  • Press Ctrl+A then release both and press K
  • When prompted, press y to confirm exit.

About

A thread based application running on ThreadX (RTOS) with continuous CAN communication to a Raspberry Pi. It is also responsible for controlling the car’s motors and servo via I²C.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages