STM32 Development Notes

STM32 Notes

Embedded Wednesdays: Getting Started in Embedded Systems — Embedded

STM32F4 Discovery – Libraries and tutorials for STM32F4 series MCUs by Tilen Majerle. Working with STM32F4xx series and Standard peripheral drivers (STD, SPL) or with STM32F0xx, STM32F4xx or STM32F7xx using Hardware abstraction layer libraries (HAL) from STMicroelectronics. My libraries are built on these 2 packages and are highly optimized compared to them.



CubeMX may duplicate c files in C_SOURCES variable they must be removed of you will get multiple declaration errors in linking

HAL – Hardware abstraction layer, abstracts hardware commands


Delay – HAL_Delay( 500); * Stop for 1/2 second *


Port initialization

  /* Struct options are defined in stm32f1xx_hal_gpio.h */
  GPIO_InitStruct.Pin = GPIO_PIN_13;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

The following functions are in format Port, Pin

HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13); – Toggle Pin state


HAL_GPIO_WritePin(GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState ) – Write Specific state to Pin



both functions should be added un “USER CODE BEGIN 4”

Add following code to allow printf() to print out UART

int _write(int file, char *outgoing, int len) {
   HAL_UART_Transmit(&huart3, outgoing, len, 100);
   return len;

This code allows ch = getchar(); to work,

int _read(int file, char *result, size_t len) {
    HAL_StatusTypeDef status;
    int retcode = 0;

    if (len != 0) {
        status = HAL_UART_Receive( &huart3, (uint8_t *) result, len, HAL_MAX_DELAY);

        if (status == HAL_OK) {
            retcode = len;
        } else {
            retcode = -1;
    return( retcode);

also add setvbuf(stdin, NULL, _IONBF, 0); before while loop to tune off 1k buffer


I2C_HandleTypeDef *hi2c
uint8_t data[6];

Shift Address by 1 leaving room for read / write bit

status = HAL_I2C_Master_Receive(hi2c, HMC5883LADDRESS<<1, &data, 6, HMC5883LTIMEOUT);

status = HAL_I2C_Master_Transmit(hi2c, HMC5883LADDRESS<<1, &returntofirstvalue, 1, HMC5883LTIMEOUT);

Programer and Debug

Memory locations

0x2000000 – RAM

0x8000000 – Flash

Bluepill programing

  1. Set the ‘boot 0′ pin/jumper high (1), and keep ‘boot 1’ low (0)
  2. Upload code
  3. For normal use, set both boot pins low (0)
  4. Reset board

st-link utility

GitHub – texane/stlink: stm32 discovery line linux programmer

Programming STM32 Microcontroller Flash in Linux

st-flash write myflash.bin 0x8000000 – write code to flash

st-util – start debugging

./st-util - usage:

  -h, --help		Print this help
  -vXX, --verbose=XX	Specify a specific verbosity level (0..99)
  -v, --verbose		Specify generally verbose logging
  -s X, --stlink_version=X
			Choose what version of stlink to use, (defaults to 2)
  -1, --stlinkv1	Force stlink version 1
  -p 4242, --listen_port=1234
			Set the gdb server listen port. (default port: 4242)
  -m, --multi
			Set gdb server to extended mode.
			st-util will continue listening for connections after disconnect.
  -n, --no-reset
			Do not reset board on connection.


openocd -f interface/stlink-v2.cfg -f target/stm32f1x.cfg

telnet localhost 4444

GoJimmyPi: Using OpenOCD and GDB

Programming an STM32F103XXX with a generic “ST Link V2” programmer from Linux · rogerclarkmelbourne/Arduino_STM32 Wiki · GitHub


Qemu STM32


STM32CubeMX – STM32Cube initialization code generator – STMicroelectronics

SW4STM32 – System Workbench for STM32: free IDE on Windows, Linux and OS X – STMicroelectronics

The System Workbench toolchain, called SW4STM32, is a free multi-OS software development environment based on Eclipse, which supports the full range of STM32 microcontrollers and associated boards.


Build STM32 applications with Eclipse, GCC and STM32Cube

Setting up a GCC/Eclipse toolchain for STM32Nucleo – Part I | Carmine Noviello – A blog about programming and electronics

Setting up a GCC/Eclipse toolchain for STM32Nucleo – Part II | Carmine Noviello – A blog about programming and electronics

Setting up a GCC/Eclipse toolchain for STM32Nucleo – Part III | Carmine Noviello – A blog about programming and electronics

6+ IDEs to debug the $2 STM32 BluePill | Wassim |

Tutorial: STM32 – Integrated Debugging in Eclipse using GNU toolchain – ErikaWiki

Simon Burkhardt | STM32

#microcontroller/stm32 #7-notes