Drivers from scratch for STM32F413XX

Rohit Imandi
3 min readAug 29, 2023

--

This was my attempt at writing the drivers of GPIO, I2C, SPI and UART for STM32F413XX containing the ARM Cortex M4 from scratch after completing the Mastering Microcontroller and Embedded Driver Development course by Kiran Nayak. I recommend this course to anyone wanting to learn firmware development.

Driver layer

The driver layer consists of header files for each of the four peripherals which act as an interface for the client (generally, the application layer in an embedded system).

Driver Layer

The MCU header file usr_stm32f413xx.h contains:

  1. Base addresses of flash, SRAM1, ROM, peripheral buses and the various peripherals
  2. Peripheral register definition structure and peripheral definitions
  3. System clock and peripheral clock enable/disable
  4. Peripheral interrupt position number in the vector table
  5. Bit positions of various registers for all peripherals

The header file of peripheral driver implementation contains:

  1. Peripheral handle structure
  2. Peripheral configuration structure
  3. API prototypes
  4. And, macros for various modes that need to be configured

Implementation of Driver Layer (APIs) — General Structure

The drivers for the following peripherals in any STM32F413XX are implemented:

  1. GPIOx (x = A, B, C, D, E, F, G, H)
  2. SPIx (x = 1, 2, 3, 4)
  3. I2Cx (x = 1, 2, 3)
  4. USARTx (x = 1, 2, 3, 6)
  5. UARTx (x = 4, 5, 7, 8, 9, 10)
GPIO Implementation
I2C Implementation
SPI Implementation
UART Implementation

The first block from the left indicates the header file in the driver layer containing a list of APIs — the middle block. The third block indicates the implementation of the APIs to be used to interact with the corresponding peripheral in .c files.

For instance, the GPIO driver layer contains a list of APIs implemented in usr_stm32f413xx_gpio_driver.c to control the GPIOx peripheral on the hardware (where x = A, B, C, D, E, F, G, H).

Configurable Items of Each Peripheral

The client/user can configure a list of items for each of the peripheral.

GPIOx User Configurable Items
SPIx User Configurable Items
I2Cx User Configurable Items
USARTx User Configurable Items

Examples and Other Notes

The drivers were tested on STM32F413ZH Nucleo-144 containing ARM Cortex M4 and the communication protocols were tested with an Arduino Uno Rev3.

This link provides examples that test all the drivers.

The reference manual RM0430 for ARM cortex M4 can be found here.
The links to datasheet and user manual of the board can be found here.

I couldn’t find all the documents on the board’s product page at ST’s official website.

Github link

--

--

Rohit Imandi
Rohit Imandi

Written by Rohit Imandi

Electronics and Embedded Systems | Programmer | Software Development

No responses yet