学习日志之stm32——固件库编程规范

对stm32固件库驱动的一点理解

stm32f4固件库主要有三个抽象层:

> 用C语言定义的寄存器地址与所有bits,bit fields和寄存器的映射(所有后面这些都可以寻址)

> 所有的片上外设可用功能的程序接口和运行时需要用到的数据结构

> 在基本模板中应用的所有外设的例子

这个固件驱动用的MISRA-C语言编写,这是一个C的子集,由于C语言的安全性不够所以专门设计的。用于编译的工具是

EWARM C/C++Compiler for ARM
 

编程规范

外设别名缩写表 Acronyms(略)

函数变量命名规则

  • PPP refers to any peripheral acronym, for example ADC.
  • System and source/header file names are preceded by the prefix ‘stm32f4xx_’.
  • Constants used in one file are defined within this file. A constant used in more than one file is defined in a header file. All constants are written in upper case.
  • Registers are considered as constants. Their names are in upper case. In most cases, the same acronyms as in the STM32F4xx reference manual document are used.
  • Names of peripheral’s functions are preceded by the corresponding peripheral acronym in upper case followed by an underscore. The first letter in each word is in upper case, for example USART_SendData. Only one underscore is allowed in a function name to separate the peripheral acronym from the rest of the function name.
  • Functions used to initialize the PPP peripheral according to parameters specified in PPP_InitTypeDef are named PPP_Init, for example TIM_Init.
  • Functions used to reset the PPP peripheral registers to their default values are named PPP_DeInit, for example TIM_DeInit.
  • Functions used to fill the PPP_InitTypeDef structure with the reset values of each member are named PPP_StructInit, for example USART_StructInit.
  • Functions used to enable or disable the specified PPP peripheral are named PPP_Cmd, for example USART_Cmd.
  • Functions used to enable or disable an interrupt source of the specified PPP peripheral are named PPP_ITConfig, for example RCC_ITConfig.
  • Functions used to enable or disable the DMA interface of the specified PPP peripheral are named PPP_DMAConfig, for example TIM_DMAConfig.
  • Functions used to configure a peripheral function always end with the string ‘Config’, for example GPIO_PinRemapConfig.
  • Functions used to check whether the specified PPP flag is set or reset are named PPP_GetFlagStatus, for example I2C_GetFlagStatus.
  • Functions used to clear a PPP flag are named PPP_ClearFlag, for example I2C_ClearFlag.
  • Functions used to check whether the specified PPP interrupt has occurred or not are named PPP_GetITStatus, for example I2C_GetITStatus.
  • Functions used to clear a PPP interrupt pending bit are named PPP_ClearITPendingBit, for example I2C_ClearITPendingBit.

其他的东西直接查固件库官方资料看

位带操作

通俗的理解就是将位带区中的bit和别名区中的地址相互映射,其可以通过更新一个寄存器中的值达到改变位带区中寄存器中某一位的值的目的,通常的操作要改变一个word中的某一位需要先将内容从mem中放到寄存器改了值后再写回mem中,位带操作这个就直接在mem中执行了,没必要经过寄存器的手,只要更新位带区mem的内容即可。

学习日志之stm32——固件库编程规范_第1张图片

映射规则如下

bit_word_offset = (byte_offset * 32) + (bit_number * 4)
bit_word_addr = bit_band_base + bit_word_offse
t

在程序中的例子

/*!< Peripheral base address in the alias region */
#define PERIPH_BASE     ((uint32_t)0x40000000)
...
/*!< Peripheral base address in the bit-band region */
#define PERIPH_BB_BASE  ((uint32_t)0x42000000)
...
/* ------------ RCC registers bit address in the alias region ----------- */
#define RCC_OFFSET      (RCC_BASE - PERIPH_BASE)
...
/* --- CR Register ---*/
/* Alias word address of PLLON bit */
#define CR_OFFSET        (RCC_OFFSET + 0x00)
#define PLLON_BitNumber  0x18
#define CR_PLLON_BB      (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PLLON_BitNumber * 4))

你可能感兴趣的:(笔记)