USART WWDG 通用同步/异步收发器 窗口看门狗 2. 命名规则
标准外设库遵从以下命名规则 PPP表示任一外设缩写,例如:ADC。源程序文件和头文件命名都以“stm32f10x_”作为开头,例如:。常量仅被应用于一个文件的,定义于该文件中;被应用于多个文件的,在对应头文件中定义。所有常量都由英文字母大写书写。寄存器作为常量处理。他们的命名都由英文字母大写书写。在大多数情况下,他们采用与缩写规范一致。外设函数的命名以该外设的缩写加下划线为开头。每个单词的第一个字母都由英文字母大写书写,例如:SPI_SendData。在函数名中,只允许存在一个下划线,用以分隔外设缩写和函数名的其它部分。对于函数命名,总的来说有以下规则:
l 名为PPP_Init的函数,其功能是根据PPP_InitTypeDef中指定的参数,初始化外设PPP,例如TIM_Init.
l 名为PPP_DeInit的函数,其功能为复位外设PPP的所有寄存器至缺省值,例如TIM_DeInit.
l 名为PPP_Init的函数,其功能为通过设置PPP_InitTypeDef 结构中的各种参数来定义外设的功能,例如:USART_Init .
l 名为PPP_Cmd的函数,其功能为使能或者失能外设PPP,例如: SPI_Cmd. l 名为PPP_ITConfig的函数,其功能为使能或者失能来自外设PPP某中断源,例如: RCC_ITConfig.
l 名为PPP_DMAConfig的函数,其功能为使能或者失能外设PPP的DMA接口,例如:TIM1_DMAConfig.
l 用以配置外设功能的函数,总是以字符串“Config”结尾,例如GPIO_PinRemapConfig.
l 名为PPP_GetFlagStatus的函数,其功能为检查外设PPP某标志位被设置与否,例如:I2C_GetFlagStatus.
l 名为PPP_ClearFlag的函数,其功能为清除外设PPP标志位,例如:I2C_ClearFlag. l 名为PPP_GetITStatus的函数,其功能为判断来自外设PPP的中断发生与否,例如:I2C_GetITStatus.
l 名为PPP_ClearITPendingBit的函数,其功能为清除外设PPP中断待处理标志位,例如: I2C_ClearITPendingBit.
这样的命名方式非常便于程序的编写和阅读,以标准外设库中的示例函数为例,下面摘录了STM32F10x_StdPeriph_Examples\\ADC\\3ADCs_DMA\\中的一段程序。
1 DMA_InitType Def DMA_InitStructure; 2
3 /* DMA1 channel1 configuration
----------------------------------------------*/ 4
5 DMA_DeInit(DMA1_Channel1); 6
7 = ADC1_DR_Address; 8
9 = (uint32_t)&ADC1ConvertedValue; 10
11 = DMA_DIR_PeripheralSRC; 12
13 = 1; 14
15 = DMA_PeripheralInc_Disable; 16
17 = DMA_MemoryInc_Disable; 18
19 = DMA_PeripheralDataSize_HalfWord; 20
21 = DMA_MemoryDataSize_HalfWord; 22
23 = DMA_Mode_Circular; 24
25 = DMA_Priority_High; 26
27 = DMA_M2M_Disable; 28
29 DMA_Init(DMA1_Channel1, &DMA_InitStructure); 30
31 /* Enable DMA1 channel1 */ 32
33 DMA_Cmd(DMA1_Channel1, ENABLE);
这段程序完成了DMA1通道的配置,首先定义了DMA_InitType DMA_InitStructure,接着配置DMA_InitType的各种参数,各参数的命名方式也均使用约定的命名方式,从命名就能够很容易的看出各参数所指代的具体功能。功能参数配置完成后,使用
DMA_Init(DMA1_Channel1, &DMA_InitStructure);完成相应外设的初始化,最后使用DMA_Cmd(DMA1_Channel1, ENABLE) 使能相应外设。从这个例子就能够很容易的
看出标准外设库这种规范化的命名规则给编写和阅读程序带来的好处。
3. 变量定义
在早期的版本中有24个变量定义,在Keil的安装根目录下,可以找到对应的定义,路径为:Keil\\ARM\\INC\\ST\\STM32F10x\\
1 /* Includes
------------------------------------------------------------------*/ 2
3 /* Exported types
------------------------------------------------------------*/ 4
5 typedef signed long s32; 6
7 typedef signed short s16; 8
9 typedef signed char s8; 10
11 typedef signed long const sc32; /* Read Only */ 12
13 typedef signed short const sc16; /* Read Only */ 14
15 typedef signed char const sc8; /* Read Only */ 16
17 typedef volatile signed long vs32; 18
19 typedef volatile signed short vs16; 20
21 typedef volatile signed char vs8; 22
23 typedef volatile signed long const vsc32; /* Read Only */ 24
25 typedef volatile signed short const vsc16; /* Read Only */ 26
27 typedef volatile signed char const vsc8; /* Read Only */ 28
29 typedef unsigned long u32;
30
31 typedef unsigned short u16; 32
33 typedef unsigned char u8; 34
35 typedef unsigned long const uc32; /* Read Only */ 36
37 typedef unsigned short const uc16; /* Read Only */ 38
39 typedef unsigned char const uc8; /* Read Only */ 40
41 typedef volatile unsigned long vu32; 42
43 typedef volatile unsigned short vu16; 44
45 typedef volatile unsigned char vu8; 46
47 typedef volatile unsigned long const vuc32; /* Read Only */ 48
49 typedef volatile unsigned short const vuc16; /* Read Only */ 50
51 typedef volatile unsigned char const vuc8; /* Read Only */
以后的版本中使用了CMSIS数据类型,变量的定义有所不同,但是出于兼容旧版本的目的,以上的数据类型仍然兼容。CMSIS的IO类型限定词如表 5-7所示,CMSIS和STM32固件库的数据类型对比如表 5-8所示。这些数据类型可以在中找到具体的定义,此部分定义如下。
1 /*!< STM32F10x Standard Peripheral Library old types (maintained for legacy purpose) */ 2
3 typedef int32_t s32; 4
5 typedef int16_t s16; 6
7 typedef int8_t s8; 8
9 typedef const int32_t sc32; /*!< Read Only */ 10
11 typedef const int16_t sc16; /*!< Read Only */
12
13 typedef const int8_t sc8; /*!< Read Only */ 14
15 typedef __IO int32_t vs32; 16
17 typedef __IO int16_t vs16; 18
19 typedef __IO int8_t vs8; 20
21 typedef __I int32_t vsc32; /*!< Read Only */ 22
23 typedef __I int16_t vsc16; /*!< Read Only */ 24
25 typedef __I int8_t vsc8; /*!< Read Only */ 26
27 typedef uint32_t u32; 28
29 typedef uint16_t u16; 30
31 typedef uint8_t u8; 32
33 typedef const uint32_t uc32; /*!< Read Only */ 34
35 typedef const uint16_t uc16; /*!< Read Only */ 36
37 typedef const uint8_t uc8; /*!< Read Only */ 38
39 typedef __IO uint32_t vu32; 40
41 typedef __IO uint16_t vu16; 42
43 typedef __IO uint8_t vu8; 44
45 typedef __I uint32_t vuc32; /*!< Read Only */ 46
47 typedef __I uint16_t vuc16; /*!< Read Only */ 48
49 typedef __I uint8_t vuc8; /*!< Read Only */
表 5-7 CMSIS IO类型限定词
STM32固件库详解



