Framework - STM32 HAL

Created:2018-01-19  Last modified:2018-01-19


  1. Introduction

  2. Principles

    1. How does the registers is set?

      In AVR microcontroller, the register is defined as a macro.

      #define DDRB 0x20;
      

      In STM32, registers are organized with struct

      Background

      1. STM32 put the relevant registers together. For example, the RCC (reset control and clock) block's registers are addressed from 0x40021000 to 0x400213FF.
      2. Peripheral are divided into three ranges.
        1). connect to APB1, address started from 0x40000000
        2). connect to APB2, address started from 0x40010000
        3). connect to AHB, address started from 0x40020000

      Map RCC registers to a structure

      // all structs are defined in stm32f103xb.h
      typedef struct{ 
          __IO uint32_t CR;
          __IO uint32_t CFGR;
          __IO uint32_t CIR;
          __IO uint32_t APB2RSTR;
          __IO uint32_t APB1RSTR;
          __IO uint32_t AHBENR;
          __IO uint32_t APB2ENR;
          __IO uint32_t APB1ENR;
          __IO uint32_t BDCR;
          __IO uint32_t CSR;
      }RCC_TypeDef;
      
      // all address are defined in stm32f103xb.h
      #define PERIPH_BASE 0x40000000U
      #define AHBPERIPH_BASE (PERIPH_BASE + 0x00020000U)
      #define RCC_BASE (AHBPHERIPH_BASE + 0x00001000U)
      
      // all conversions are defined in stm32f103xb.h
      #define RCC ((RCC_TypeDef *)RCC_BASE)
      
      //defined in stm32f1xx.h
      #define SET_BIT(REG, BIT) ((REG) |= BIT)
      
      //The individual bits are defined in stm32f1xx_hal_rcc.h
      #define RCC_HSE_ON
      
      //usage
      SET_BIT(RCC->CR, RCC_HSE_ON);