Ex-04: CMSIS Driver Introduction

In this exercise, you will redo the Edge Detection and Debouncing exercise using the CMSIS driver. First, read the theory on the CMSIS driver, then use it to reimplement Ex-03.

Objectives
  • CMSIS device header structure

  • CMSIS register access via peripheral structs

  • CMSIS bit manipulation macros

Outcomes
  • Understand the structure of the CMSIS driver

  • Implement software using the CMSIS driver

CMSIS Driver

CMSIS description

Writing directly to hard-coded register addresses, as in the previous exercises, is error-prone, hard to read and impractical for reusing code. Arm, the designer of the Cortex-M processor core, solves these problems with a standardised software interface. The Common Microcontroller Software Interface Standard (CMSIS) provides device support and a simple software interface to the processor and its peripherals. Based on this standard, the MCU manufacturer (STMicroelectronics in our case) supplies the device header files describing all peripherals of the MCU.

To use the driver with our MCU, only one header file has to be included:

#include "stm32f4xx.h"

In addition, the MCU-specific symbol must be defined, usually as a compiler option in the project settings:

-DSTM32F446xx

The header file stm32f4xx.h is a generic one for all STM32 F4 microcontrollers. Depending on the defined symbol, it loads the device-specific header file stm32f446xx.h, which contains all the addresses and register types of our MCU. This hierarchical loading avoids code duplication and provides a separation of concerns.

CMSIS macros

The names of these macros always follow the same structure.

<PERIPHERAL>_<REGISTER>_<BITFIELD>_<ADDITIONAL>
GPIO_IDR_ID1
GPIO_MODER_MODE4_Pos

Description of the structure:

  • PERIPHERAL

    The peripherals are the functional units of the MCU, such as GPIO, TIM, ADC, etc.

  • REGISTER

    The name of the register in the peripheral according to the reference manual RM0390 reference manual.

  • BITFIELD

    The bit or bits to be changed. For example, the input data register of a GPIO uses one bit per pin, whereas the GPIO port mode register uses two bits per pin.

  • ADDITIONAL

    There are four possibilities:

    1. Nothing: The masked bitfield itself (e.g. GPIO_MODER_MODE4).

    2. Bit number: A single bit within the bitfield (e.g. GPIO_MODER_MODE4_0 and GPIO_MODER_MODE4_1). For example, in the GPIO port mode register, bits 0 and 1 form the bitfield of pin 0.

    3. _Pos: The position of the bitfield in the register, i.e. its lowest bit number.

    4. _Msk: The masked bitfield, as you have used it in the previous exercises.

CMSIS TypeDefs

In addition to the macros, CMSIS provides a struct type for each peripheral of the MCU containing all its registers (e.g. GPIO_TypeDef, RCC_TypeDef). The struct has exactly the memory layout of the peripheral’s registers, so each member is mapped onto the corresponding physical register. CMSIS also defines a macro for each peripheral instance (e.g. GPIOA, RCC), which is a pointer to such a struct at the peripheral’s base address. This has the advantage that every register can be accessed through a pointer with a readable name instead of a raw address.

Register Access with CMSIS

Here is an example of how to access the clock control register of the reset and clock control (RCC) peripheral:

RCC->CR

Compare this with the raw access you used in the previous exercises:

*((volatile uint32_t *)0x40023800)

CMSIS bit manipulation

In the previous exercises, you learnt how to manipulate the registers of a peripheral with bitwise operators. In a real-world implementation, a set of macros is usually used for this instead of plain bit operations. The code snippet below shows the most commonly used bit manipulation macros defined in the stm32f4xx.h header file.

Extract of the most important bit manipulation macros
#define SET_BIT(REG, BIT)     ((REG) |= (BIT))
#define CLEAR_BIT(REG, BIT)   ((REG) &= ~(BIT))
#define READ_BIT(REG, BIT)    ((REG) & (BIT))
#define CLEAR_REG(REG)        ((REG) = (0x0))
#define WRITE_REG(REG, VAL)   ((REG) = (VAL))
#define READ_REG(REG)         ((REG))
#define MODIFY_REG(REG, CLEARMASK, SETMASK)  WRITE_REG((REG), (((READ_REG(REG)) & (~(CLEARMASK))) | (SETMASK)))
Hint

As a little help, here are some examples of how to use these macros together with CMSIS:

SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIODEN);

Sets the desired bit in the register. In this case, the GPIOD peripheral clock is enabled.

CLEAR_BIT(GPIOB->ODR, GPIO_ODR_OD4);

Clears the desired bit in the register. In this example, the output PB4 is switched off.

READ_BIT(GPIOC->IDR, GPIO_IDR_ID12);

Reads only the state of the desired bit in the register. In this example, the input PC12 is read.

Note

The result is not 0 or 1, but 0 or the value of the bit mask (here 0x1000). Keep this in mind when comparing or storing input states.

CLEAR_REG(GPIOC->PUPDR);

Sets the whole register to zero. In this case, all pull-up and pull-down resistors of the GPIOC peripheral are disabled.

WRITE_REG(SDIO->ARG, 0x2);

Writes the value into the register. In this case, the hexadecimal value 0x2 is written into the SDIO argument register.

READ_REG(SPI1->DR);

Reads the whole register. Here, the SPI1 data register is read.

MODIFY_REG(GPIOB->MODER, GPIO_MODER_MODE4_Msk, 0b01 << GPIO_MODER_MODE4_Pos);

Sets a bitfield to a desired value without changing the other bits of the register. In this example, pin 4 of GPIOB is configured as an output via the port mode register.

Note

Make sure to shift the value to the position of the bitfield (_Pos), otherwise other bits of the register will be modified.

Tasks

The aim is to reimplement the edge detection and debouncing from Ex-03. The functional requirements are the same as in the tasks section of Ex-03.

Exercise
  1. Open stm32f446xx.h and find the definition of GPIO_TypeDef and the macro GPIOA. Compare the base address with the one given in the reference manual RM0390 reference manual.

  2. Replace all hard-coded register addresses in your Ex-03 solution with the CMSIS peripheral structs (e.g. GPIOA->MODER).

  3. Replace all hand-written masks and bit operations with the CMSIS bitfield macros and the bit manipulation macros.

  4. Test your program: it must behave exactly as your Ex-03 solution.

Question
  1. Which advantages and disadvantages of CMSIS compared to direct register access do you see?

  2. Does the CMSIS implementation produce more machine code? Compare the size of both binaries.

Tip

You don’t need to rewrite the complete code. Only change the implementation where register addresses are used and bit manipulations take place.