Basic examples of STM32 ARM bare-metal programming. No high level libraries or IDE. Pure C. It's still in progress.
  • C 76%
  • Makefile 11.2%
  • Assembly 10.8%
  • Linker Script 2%
Find a file
2022-12-28 17:15:05 +01:00
01_STM32_bare_metal_blink Refactor 3 (indentation) 2022-12-20 04:21:04 +01:00
02_STM32_bare_metal_pll_clock Refactor 3 (indentation) 2022-12-20 04:21:04 +01:00
03_STM32_bare_metal_blink_systick_delay Refactor 3 (indentation) 2022-12-20 04:21:04 +01:00
04_STM32_bare_metal_button Refactor 3 (indentation) 2022-12-20 04:21:04 +01:00
05_STM32_bare_metal_button_switch Refactor 3 (indentation) 2022-12-20 04:21:04 +01:00
06_STM32_bare_metal_button_blink_systick_counter Refactor 3 (indentation) 2022-12-20 04:21:04 +01:00
07_STM32_bare_metal_button_interrupt Refactor 3 (indentation) 2022-12-20 04:21:04 +01:00
08_STM32_bare_metal_timer Refactor part 1 2022-12-19 02:42:55 +01:00
09_STM32_bare_metal_pwm Refactor part 1 2022-12-19 02:42:55 +01:00
10_STM32_bare_metal_rgb_pwm Refactor 2 2022-12-20 04:06:05 +01:00
11_STM32_bare_metal_servo Refactor 2 2022-12-20 04:06:05 +01:00
12_STM32_bare_metal_serial_receive Refactor 3 (indentation) 2022-12-20 04:21:04 +01:00
13_STM32_bare_metal_serial_receive_interrupt Refactor 2 2022-12-20 04:06:05 +01:00
14_STM32_bare_metal_serial_transmit Refactor 2 2022-12-20 04:06:05 +01:00
15_STM32_bare_metal_ultrasonic_sensor Update readme.md 2022-12-24 21:37:58 +01:00
16_STM32_bare_metal_adc Refactor 3 (indentation) 2022-12-20 04:21:04 +01:00
17_STM32_bare_metal_adc_interrupt Refactor 2 2022-12-20 04:06:05 +01:00
18_STM32_bare_metal_adc_two Refactor 3 (indentation) 2022-12-20 04:21:04 +01:00
20_STM32_bare_metal_adc_two_dma Refactor 3 (indentation) 2022-12-20 04:21:04 +01:00
21_STM32_bare_metal_adc_two_watchdog Refactor 2 2022-12-20 04:06:05 +01:00
22_STM32_bare_metal_serial_dma Refactor 3 (indentation) 2022-12-20 04:21:04 +01:00
23_STM32_bare_metal_i2c_mpu6050 Refactor 3 (indentation) 2022-12-20 04:21:04 +01:00
STLink Blink, systick & button 2022-10-16 02:43:08 +02:00
STM32F446RE ADC 2022-10-19 02:04:51 +02:00
.gitattributes Initial commit 2022-10-03 16:10:03 +02:00
README.md Update README.md 2022-12-28 17:15:05 +01:00

ARM STM32 bare metal examples

Basic examples of STM32 bare-metal programming.

If you've never programmed on MCU registers I suggest to see my bare metal AVR repository before going into STM32. I found that there aren't many resources that cover the very basics of STM32 without HAL. There is a huge gap between working with Arduino and without it, with ARM difficulty increases exponentially.

Link to my AVR repository: https://github.com/ErniW/AVR-bare-metal-examples

Examples and their code are made and changed gradually so functions may differ between examples.

TODO

  • Fix the DMA serial bi-directional communication.
  • I2C interrupts (and DMA?).
  • SPI.
  • RTC.
  • IWDG, WWDG.

Board:

I'm using the STM32f446RE board with ARM Cortex M4 CPU. If you're using other STM32 board be aware that things may not work on your board and you must change files and settings.

Documentation:

Toolchain:

  • Make sure you've installed ARM toolchain. To check if everything is ok type arm-none-eabi-gcc -v in your terminal.
  • We will use GNU Make for build process.
  • To upload the code I'm using STLink https://github.com/stlink-org/stlink which is included.

You can see that most of STM32 examples found in the internet are using STM32 IDE and most tutorials are using STM32 HAL library - there are many reasons to not use it so I will work with ARM toolchain and Visual Studio Code. More complex builds are using CMake instead of GNU Make but I want to stay with the basics so you can follow along.

Folder structure & libraries.

You need to download the STM32 drivers unless you probably already have them if you used STM32 IDE. In Makefile change the include directory for CMSIS.

I've put the necessary files in STM32F446RE folder:

  • startup_stm32f446xx.s
  • STM32F446RETX_FLASH.ld
  • stm32f446xx.h stm32f4xx.h system_stm32f4xx.h headers.

These files can be found inside drivers. If you are using different board you have to include similar files. Remember about changing the makefile variables.

Setting the Visual Studio Code:

In c_cpp_properties.json inside .vscode folder:

  • Add includes: for CMSIS in STM32 drivers, they should be located in: STM32Cube_FW_F4_V1.26.1/Drivers/CMSIS/Include
  • Add defines: STM32F446xx, __CC_ARM

Otherwise the code will compile but it will highlight undefined references.

Compile and upload:

In terminal type:

make compile

make upload

or make compile upload