MCU STM32F103C8T6 Bare-metal in Rust
A bare metal (register level) STM32F103C8T6/STM32F103 MCU program written in pure Rust without any IDE, SDK, HAL or library, and no assembly code, the only tool required is the Rust compiler. Implement basic input and output, timer, serial communication, etc. by directly reading and writing registers of hardware.
Implemented Features
- GPIO read and write
- Set system clock
- SysTick and interrupt
- UART read and write
stm32f103-v2.mp4
Source Code Structure
src/register_*.rs
Definition of register structures and valuessrc/startup.rs
startup and vector arraysrc/main.rs
main program.vscode/launch.json
configuration file for VSCode debugsvd/
System View Description
file for displaying various register names and address values during GDB debuggingbuild.sh
script for compilingflash.sh
script for compiling and downloading firmware
Wires
- PC13: builtin LED -
- PB5: external LED +
- PA0: button (the another button pin connect to GND)
- PA9: USART1_TX -> CP2012 RX
- PA10: USART1_RX -> CP2012 TX
Compile
Install Rust
on your platform first, then run:
$ ./build.sh
Flash
Connect the MCU to your computer using the CMSIS_DAP debugger
(hardware) and then run:
$ ./flash.sh
Debug
$ ./server-gdb-server.sh
then open another terminal, and run:
$ ./start-gdb-client-with-svd.sh
Reference Documents
Hardware description and register structure and value description documentations:
-
STM32F103c8 Datasheet https://www.st.com/resource/en/datasheet/cd00161566.pdf
-
RM0008 Reference manual https://www.st.com/resource/en/reference_manual/cd00171190-stm32f101xx-stm32f102xx-stm32f103xx-stm32f105xx-and-stm32f107xx-advanced-arm-based-32-bit-mcus-stmicroelectronics.pdf
-
PM0214 Programming manual https://www.st.com/resource/en/programming_manual/pm0214-stm32-cortexm4-mcus-and-mpus-programming-manual-stmicroelectronics.pdf
For those who want to know how it works and who want to go further and implement more features, check out these:
-
Bare metal programming guide (C lang on STM32F429) https://github.com/cpq/bare-metal-programming-guide
-
The Embedonomicon https://docs.rust-embedded.org/embedonomicon/
-
libopencm3 project https://github.com/libopencm3/libopencm3
C language version
This project is also implemented in C by the author, for those interested please check out MCU STM32F030C8T6 Bare-metal in C