/stm32_bluepill_in_rust__Template

The updated project template that I use for STM32 BluePill micro-controller in Rust.

Primary LanguageGDB

STM32 BluePill in Rust - Project template

The updated project template that I use for STM32 BluePill micro-controller in Rust for Linux Ubuntu 20.04 .

BluePill STM32F103 - Development board

This is my starting point to make a project with the BluePill in Rust. It was made by following the following 3 github repository notes and files and modifying them to update them, following video playlist and the 2 free books of embedded Rust. Those modifications take care of my present needs in term of STM32 BluePill development in Rust

The STM32 BluePill diagram

Specifications:

  • STM32F103C8T6 ARM Cortex M3
  • 72MHz, 90 DMIPS / 1.25 DMIPS / MHz
  • 2.0V - 3.6V operating voltage
  • 8MHz system crystal
  • 32.768KHz RTC crystal
  • 2.54mm pitch pins
  • JTAG/SWD header
  • 64 KByte Flash, 20 KByte SRAM
  • 2x SPI, 3x USART, 2x I2C, 1x CAN
  • 38 I/Os almost all 5V-tolerant
  • 1x USB 2.0 FS
  • 2x ADC (12-bit / 16-channel)
  • 3x general timers, 1x advanced timer
  • RT9193 3.3V LDO voltage regulator, max current 300mA
  • Micro USB for power and data
  • Blue user LED (PC13)
  • 2x jumpers for bootloader selection
  • Reset button
  • 2x20 side pins + 1x4 SWD pins
  • Dimensions: 53.0mm x 22.5mm

IMPORTANT note

  While programming with the ST-Link_V2 dongle, don't have another USB cable
  connected to the Micro USB plug.

  MAX Absolute 150 mA total source/sink for entire Micro-controller.
  
  MAX 20 mA per pin, 8 mA recommended.
  
  There are pins that are 5 V tolerant but there are many that are not,
  see the diagrams of the chip and the specifications on the data chip.

  The max current at 3.3 V is not equal in all the pins, 
     PC13, PC14 e PC15 are weaker pins Source 0 mA e sink 3 mA max 2 MHz e max 30 pF.

Connecting the STM32 Blue Pill to ST-Link V2 USB Debugger

STM32 Blue Pill        ST-Link V2 USB Debugger
V3      [Red]         3.3V     (Pin 8)
IO      [Orange]      SWDIO    (Pin 4)
CLK     [Brown]       SWDCLK   (Pin 2)
GND     [Black]       GND      (Pin 6)

Good references

  1. Video - An Overview of the Embedded Rust Ecosystem
    https://www.youtube.com/watch?v=vLYit_HHPaY

  2. Crate stm32f1xx-hal in Rust
    https://github.com/stm32-rs/stm32f1xx-hal

  3. stm32-rs - stm32f1xx-hal - Examples
    https://github.com/stm32-rs/stm32f1xx-hal/tree/master/examples

  4. stm32f1xx_hal Documentation version 0.8
    https://docs.rs/stm32f1xx-hal/0.8.0/stm32f1xx_hal/

  5. Crate cortex_m Documentation
    Cortex core support and assembly instructions.
    https://docs.rs/cortex-m/latest/cortex_m/

  6. Crate cortex_m_rt Documentation
    Startup code and minimal runtime for Cortex-M micro-controllers
    https://docs.rs/cortex-m-rt/latest/cortex_m_rt/

  7. Play List - Embedded Rust BluePill - Vers Binarii
    https://www.youtube.com/playlist?list=PLP_X41VhYn5X6Wwjnm0bRwI3n2pdaszxU

  8. Play List - Embedded Rust course - JaJakub - 2022
    https://www.youtube.com/playlist?list=PLL2SCPK5xSRWBPj-nKOVYIhxRw7C4kYeI

  9. TeXitoi - blue-pill-quickstart
    The Example source code is modified from here.
    https://github.com/TeXitoi/blue-pill-quickstart

  10. lupyuen - stm32-blue-pill-rust
    This uses debugger multi architecture and VSCode.
    https://github.com/lupyuen/stm32-blue-pill-rust

  11. Rust Embedded BluePill Workshop Notes
    https://cgit.pinealservo.com/BluePill_Rust/resources/src/branch/master/notes.org

  12. rust-embedded - cortex-m-quickstart
    https://github.com/rust-embedded/cortex-m-quickstart

  13. STM32 - Discovery - Book
    https://docs.rust-embedded.org/discovery/

  14. The Embedded Rust Book
    https://docs.rust-embedded.org/book/

  15. Video - How can we write the best device driver for a Hal in Rust?
    https://www.youtube.com/watch?v=z9z74VpqO9A

  16. 258 drivers para Embedded-HAL
    https://crates.io/search?q=embedded-hal%20driver

  17. More gdb commands cheat-sheet
    https://darkdust.net/files/GDB%20Cheat%20Sheet.pdf

  18. Video - Getting Started with Debugging using GDB
    Find Bugs in Your Code with A Couple Easy Commands
    https://www.youtube.com/watch?v=Dq8l1_-QgAc

  19. Video - RTIC - Real Time Interrupt driven Concurrency
    RTIC is a RTOS - Real Time Operating System.
    https://www.youtube.com/watch?v=saNdh0m_qHc

  20. RTIC Book
    Real-Time Interrupt-driven Concurrency.
    A very efficient preemptive multitasking framework that supports task prioritization and dead lock free execution.
    https://rtic.rs/1/book/en/

  21. Github - rtic-rs - cortex-m-rtic
    https://github.com/rtic-rs/cortex-m-rtic

  22. Video - Grepit about the Rust RTIC framework
    https://www.youtube.com/watch?v=sSJ-Md8nwIM

  23. The Embedonomicon Book
    Deep dive into the inner workings.
    https://docs.rust-embedded.org/embedonomicon/

  24. usb-device - Experimental device-side USB stack for embedded devices
    https://crates.io/crates/usb-device

  25. probe-rs - A modern, embedded debugging toolkit, written in Rust
    https://probe.rs/

  26. GitHub probe-rs
    https://github.com/probe-rs/probe-rs

  27. Video - probe-rs: Your Embedded Tome
    https://www.youtube.com/watch?v=esNPoXbhHkU

  28. All relevant Info and a starting project Template.
    Where I have put all the info that I consider to be relevant for Pico development in Rust.
    Raspberry Pi Pico in Rust Proj Template with RTIC USB-Serial and UF2
    https://github.com/joaocarvalhoopen/Raspberry_Pi_Pico_in_Rust__Proj_Template_with_RTIC_USB-Serial_UF2

  29. Awesome Embedded Rust
    https://github.com/rust-embedded/awesome-embedded-rust

  30. How to learn modern Rust
    https://github.com/joaocarvalhoopen/How_to_learn_modern_Rust

Datasheets

  1. STM32F103C8 Website
    https://www.st.com/en/microcontrollers/stm32f103c8.html

  2. STM32F103C8 Datasheet
    https://www.st.com/resource/en/datasheet/stm32f103c8.pdf

  3. STM32F103C8 Reference Manual
    https://www.st.com/content/ccc/resource/technical/document/reference_manual/59/b9/ba/7f/11/af/43/d5/CD00171190.pdf/files/CD00171190.pdf/jcr:content/translations/en.CD00171190.pdf

  4. STM32F103C8 Flash Programming
    https://www.st.com/content/ccc/resource/technical/document/programming_manual/10/98/e8/d4/2b/51/4b/f5/CD00283419.pdf/files/CD00283419.pdf/jcr:content/translations/en.CD00283419.pdf

  5. STM32F103C8 ARM Cortex M3 Programming
    https://www.st.com/content/ccc/resource/technical/document/programming_manual/5b/ca/8d/83/56/7f/40/08/CD00228163.pdf/files/CD00228163.pdf/jcr:content/translations/en.CD00228163.pdf

Crates and code size optimization

  1. Crate heapless
    https://github.com/japaric/heapless
   Arc         - Thread-safe reference-counting pointer backed by a memory pool
   BinaryHeap  - Priority queue
   IndexMap    - Hash table
   IndexSet    - Hash set
   LinearMap
   Pool        - Lock-free memory pool
   String
   Vec
   mpmc::Q*    - Multiple producer multiple consumer lock-free queue
   spsc::Queue - Single producer single consumer lock-free queue
  1. Crate ufmt
    A (6-40x) smaller, (2-9x) faster and panic-free alternative to core::fmt
    https://github.com/japaric/ufmt

  2. LCD 16x2 - Crate hd44780-driver
    Implementation of the embedded-hal traits for the HD44780, 16x1, 16x2 and 16x4.
    https://github.com/JohnDoneth/hd44780-driver

  3. Crate Embedded graphics
    It's a 2D graphics library that is focused on memory constrained embedded devices.
    https://github.com/embedded-graphics/embedded-graphics

  4. Crate flip-link
    Adds zero-cost stack overflow protection to your embedded programs.
    https://github.com/knurling-rs/flip-link/

  5. Crate defmt
    defmt ("de format", short for "deferred formatting") is a highly efficient logging framework that targets resource-constrained devices, like micro-controllers.
    https://github.com/knurling-rs/defmt/

  6. TOML compilation flag options to generate smaller code size

....

[profile.release.package."*"]
opt-level = "z"

[profile.release]
codegen-units = 1
debug = true
opt-level = "z"

After installation in process to compile, flash and debug you only need to do

######
# Window CMD 1
$ openocd

# Windows CMD 2
$ cargo clean
$ cargo build
$ cargo run

or just do: 

######
# Window CMD 1
$ openocd

# Window CMD 2
$ cargo build
$ cargo run

or

(gdb) continue

or

(gdb) ctrl + c
(gdb) list

or
(gdb) ctrl + c
(gdb) layout src

######
Or inside VSCode in a debug sectiondo,

# Window CMD 1
$ openocd

Just put one breakpoint and start running the Debug play icon. 
(VSCode) Set Breakpoint in line 10.
(VSCode) icon play debug.

Installation of embedded Rust for STM32

After installing Rust on you PC do:

$ rustup update
$ rustc --version

$ rustup target add thumbv7m-none-eabi

Installation of the debugger (gdb-multiarch) and the tool to flash do chip, the onm circuit debugger SWD (openocd):

(arm-none-eabi-gdb is obsolete, we will use gdb-multiarch)

$ sudo apt-get install  gdb-multiarch openocd

$ sudo ln -s /usr/bin/gdb-multiarch /usr/bin/arm-none-eabi-gdb

Install ARM Cross-Compiler and Linker

$ sudo apt install binutils-arm-none-eabi gcc-arm-none-eabi

$ arm-none-eabi-gcc -v

It should appear something like this:

"version 9.2.1 20191025 (release)"

If not you have a problem in the path:

  1. If you see no errors, close the command prompt.

  2. If you see an error, update your PATH environment variable so that it includes the folder for the ARM ".exe" files.

To have a graphical debugger front-end, inside VSCode, install the following plugin:

To use the debugger inside VSCode the last command in the script for the debugger gdb "openocd.gdb" has to terminate in step, and has to be waiting at the beginning of the program. And you must have the openocd running on a independent shell window.

How to use this repository

1. Download the repository or clone it to a directory.
2. Change the directory name from "stm32_bluepill_in_rust__Template"
   to "stm32_bluepill_in_rust".
3. Connect the BluePill ST-Link_V2 programmer to the Blupill and to
   the computer, only the programmer can be connected while programming.
3. Then do:

Window CMD 1
$ cd stm32_bluepill_in_rust
$ openocd

Window CMD 2
$ cd stm32_bluepill_in_rust
$ cargo clean
$ cargo build
$ cargo run

Then to do your own project you will have to create a project directory with the name of the project (lowercase and "_"), then copy the all the files there, then change the files where is "stm32_bluepill_in_rust" to your project name.
The files to change are:

  • openocd.gdb
  • launch.json

How to start learning about the stm32f1xx-hal API and the low level details of the micro-controller.

My process of studying will be to start from the beginning of this list of program examples and analyse each program one at a time, see / find in the comments and in the HAL documentation in detail what each function of method does. It's a simple process but laborious that will rapidly fill you in into the API. For more detail a second pass will be in the Datasheet and Reference manual for this micro-controller. The examples that have in there name rtic are the ones that use the rtic - real time operating system. The following links are the documentation you will need to understand the examples.

  1. stm32-rs - stm32f1xx-hal - Examples
    https://github.com/stm32-rs/stm32f1xx-hal/tree/master/examples

  2. stm32f1xx_hal Documentation version 0.8
    https://docs.rs/stm32f1xx-hal/0.8.0/stm32f1xx_hal/

  3. Crate cortex_m Documentation
    Core support and assembly instructions.
    https://docs.rs/cortex-m/latest/cortex_m/

  4. Crate cortex_m_rt Documentation
    Startup code and minimal runtime for Cortex-M micro-controllers
    https://docs.rs/cortex-m-rt/latest/cortex_m_rt/

  5. STM32F103C8 Reference Manual
    https://www.st.com/content/ccc/resource/technical/document/reference_manual/59/b9/ba/7f/11/af/43/d5/CD00171190.pdf/files/CD00171190.pdf/jcr:content/translations/en.CD00171190.pdf

Order in which one should study the examples to learn how to use the stm32f1xx-hal crate:

01. - blinky.rs
02. - blinky_generic.rs
03. - blinky_rtc.rs
04. - blinky_rtcalarm_irq.rs
05. - blinky_timer_irq.rs

06. - timer-interrupt-rtic.rs

07. - delay.rs

08. - hello.rs

09. - led.rs

10. - gpio_input.rs
11. - dynamic_gpio.rs
12. - multi_mode_gpio.rs

13. - panics.rs

14. - pwm.rs
15. - pwm_custom.rs
16. - pwm_input.rs

17. - exti.rs
18. - exti_rtic.rs

19. - adc.rs
20. - adc_temperature.rs
21. - adc-dma-circ.rs
22. - adc-dma-rx.rs

23. - rtc.rs

24. - serial.rs
25. - serial_config.rs
26. - serial_reconfigure.rs
27. - serial-fmt.rs
28. - serial-interrupt-idle.rs
29. - serial-dma-circ.rs
30. - serial-dma-peek.rs
31. - serial-dma-rx.rs
32. - serial-dma-tx.rs

33. - i2c-bme280 - Example of using i2c with the bme280 air sensor

34. - spi.rs
35. - spi-dma.rs

36. - mfrc522.rs

37. - usb_serial.rs
38. - usb_serial_interrupt.rs
39. - usb_serial_rtic.rs

40. - nojtag.rs
41. - crc.rs
42. - qei.rs

43. - itm.rs

44. - can-echo.rs
45. - can-loopback.rs
46. - can-rtic.rs

47. - motor.rs.disabled
48. - mpu9250.rs.disabled

49. - enc28j60-coap.rs.disabled
50. - enc28j60.rs.disabled

Notes

  • block! - Turns the non-blocking expression $e into a blocking operation.
    From the crate nb.

License

MIT Open Source license.
This are very basic configuration files and a simple blink example that I made by updating and modifying the upper referenced links. This is just my starting point for a project with the bluepill in Rust.

Have fun!

Best regards,
João Nuno Carvalho