[应用相关] stm32f10x.h文件分析理解

[复制链接]
 楼主| 和下土 发表于 2021-5-19 10:41 | 显示全部楼层 |阅读模式
今天再看过半年前自己写的这篇发现自己当时理解有误,stm32f10x.h与库开发并未存在太大关系,只是一个最为重要的寄存器地址到寄存器结构体变量的映射。

  stm32f10x.h 这个头文件是STM32开发最为重要的一个头文件相当于51单片机那个 reg52.h 。但对于STM32来说,它的寄存器数量是非常多的,如果按照操作51一样的方法来操作32的话,查数据手册来配置寄存器是非常麻烦的。所以ST开发了这个库,方便大家开发,缩短开发周期。
在 stm32f10x.h 中前面一开始就出现了:

  1. #ifndef __STM32F10x_H
  2. #define __STM32F10x_H

  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
 楼主| 和下土 发表于 2021-5-19 10:42 | 显示全部楼层
一开始不解   extern "C" {   这个语句的意思,后来了解到原来是用来说明后面的定义都是使用C语言写的。这个 __cplusplus 是指C++,4、5句的意思就是说如果用C++编译器的话,它里面是定义有__cplusplus 的,而通过 extern "C" { 告知编译器,这段代码是用C编写的,要按照C语言编译。这是因为C++里面有函数重载,编译的时候把参数也编译了,而C的话,编译只编译函数名。
 楼主| 和下土 发表于 2021-5-19 10:45 | 显示全部楼层
下面这段是用来定义器件容量,可以通过自己取消注释来选择,也可以在KEIL里面设置全局宏定义,2种方式。
 楼主| 和下土 发表于 2021-5-19 10:51 | 显示全部楼层
  1. #if !defined (STM32F10X_LD) && !defined (STM32F10X_LD_VL) && !defined (STM32F10X_MD) && !defined (STM32F10X_MD_VL) && !defined (STM32F10X_HD) && !defined (STM32F10X_HD_VL) && !defined (STM32F10X_XL) && !defined (STM32F10X_CL)
  2.   /* #define STM32F10X_LD */     /*!< STM32F10X_LD: STM32 Low density devices */
  3.   /* #define STM32F10X_LD_VL */  /*!< STM32F10X_LD_VL: STM32 Low density Value Line devices */  
  4.   /* #define STM32F10X_MD */     /*!< STM32F10X_MD: STM32 Medium density devices */
  5.   /* #define STM32F10X_MD_VL */  /*!< STM32F10X_MD_VL: STM32 Medium density Value Line devices */  
  6.   /* #define STM32F10X_HD */     /*!< STM32F10X_HD: STM32 High density devices */
  7.   /* #define STM32F10X_HD_VL */  /*!< STM32F10X_HD_VL: STM32 High density value line devices */  
  8.   /* #define STM32F10X_XL */     /*!< STM32F10X_XL: STM32 XL-density devices */
  9.   /* #define STM32F10X_CL */     /*!< STM32F10X_CL: STM32 Connectivity line devices */
  10. #endif
  11. #if !defined (STM32F10X_LD) && !defined (STM32F10X_LD_VL) && !defined (STM32F10X_MD) && !defined (STM32F10X_MD_VL) && !defined (STM32F10X_HD) && !defined (STM32F10X_HD_VL) && !defined (STM32F10X_XL) && !defined (STM32F10X_CL)
  12. #error "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"
  13. #endif
 楼主| 和下土 发表于 2021-5-19 13:56 | 显示全部楼层
如果没有定义器件的话,编译的时候就会出现以下错误:
  1. "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"
 楼主| 和下土 发表于 2021-5-19 14:00 | 显示全部楼层
用于定义是否使用外设驱动,如果注释掉或者keil中没设全局宏定义的话,则代表不使用ST库提供的外设驱动库,在本文倒数第二段代码中有句

  #ifdef USE_STDPERIPH_DRIVER

   #include "stm32f10x_conf.h"

  #endif

  stm32f10x_conf.h用于外设注释配置。
 楼主| 和下土 发表于 2021-5-19 14:04 | 显示全部楼层
#if !defined  USE_STDPERIPH_DRIVER
/**
* @brief Comment the line below if you will not use the peripherals drivers.
   In this case, these drivers will not be included and the application code will
   be based on direct access to peripherals registers
   */
  /*#define USE_STDPERIPH_DRIVER*/
#endif
 楼主| 和下土 发表于 2021-5-19 14:22 | 显示全部楼层
下面应该是对于器件HSE的设置
  1. /**
  2. * @brief In the following line adjust the value of External High Speed oscillator (HSE)
  3.    used in your application
  4.    
  5.    Tip: To avoid modifying this file each time you need to use different HSE, you
  6.         can define the HSE value in your toolchain compiler preprocessor.
  7.   */           
  8. #if !defined  HSE_VALUE
  9. #ifdef STM32F10X_CL   
  10.   #define HSE_VALUE    ((uint32_t)25000000) /*!< Value of the External oscillator in Hz */
  11. #else
  12.   #define HSE_VALUE    ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */
  13. #endif /* STM32F10X_CL */
  14. #endif /* HSE_VALUE */
 楼主| 和下土 发表于 2021-5-19 14:29 | 显示全部楼层
 设置启动超时值和HSI
  1. /**
  2. * @brief In the following line adjust the External High Speed oscillator (HSE) Startup
  3.    Timeout value
  4.    */
  5. #define HSE_STARTUP_TIMEOUT   ((uint16_t)0x0500) /*!< Time out for HSE start up */
  6. #define HSI_VALUE    ((uint32_t)8000000) /*!< Value of the Internal oscillator in Hz*/
 楼主| 和下土 发表于 2021-5-19 14:31 | 显示全部楼层
标准外设库版本号的定义
  1. /**
  2. * @brief STM32F10x Standard Peripheral Library version number
  3.    */
  4. #define __STM32F10X_STDPERIPH_VERSION_MAIN   (0x03) /*!< [31:24] main version */                                 
  5. #define __STM32F10X_STDPERIPH_VERSION_SUB1   (0x05) /*!< [23:16] sub1 version */
  6. #define __STM32F10X_STDPERIPH_VERSION_SUB2   (0x00) /*!< [15:8]  sub2 version */
  7. #define __STM32F10X_STDPERIPH_VERSION_RC     (0x00) /*!< [7:0]  release candidate */
  8. #define __STM32F10X_STDPERIPH_VERSION       ( (__STM32F10X_STDPERIPH_VERSION_MAIN << 24)\
  9.                                              |(__STM32F10X_STDPERIPH_VERSION_SUB1 << 16)\
  10.                                              |(__STM32F10X_STDPERIPH_VERSION_SUB2 << 8)\
  11.                                              |(__STM32F10X_STDPERIPH_VERSION_RC))
 楼主| 和下土 发表于 2021-5-19 14:34 | 显示全部楼层
配置Cortex-M3处理器和核内外设

  1. /**
  2. * [url=home.php?mod=space&uid=247401]@brief[/url] Configuration of the Cortex-M3 Processor and Core Peripherals
  3. */
  4. #ifdef STM32F10X_XL
  5. #define __MPU_PRESENT             1 /*!< STM32 XL-density devices provide an MPU */
  6. #else
  7. #define __MPU_PRESENT             0 /*!< Other STM32 devices does not provide an MPU */
  8. #endif /* STM32F10X_XL */
  9. #define __NVIC_PRIO_BITS          4 /*!< STM32 uses 4 Bits for the Priority Levels    */
  10. #define __Vendor_SysTickConfig    0 /*!< Set to 1 if different SysTick Config is used */

 楼主| 和下土 发表于 2021-5-19 14:38 | 显示全部楼层
中断线定义,前部分对于所有STM32F10x都有相同定义,后部分根据不同器件容量采用条件编译定义不同中断线。
 楼主| 和下土 发表于 2021-5-19 14:45 | 显示全部楼层

  1. /**
  2. * @brief STM32F10x Interrupt Number Definition, according to the selected device
  3. *        in [url=home.php?mod=space&uid=144993]@ref[/url] Library_configuration_section
  4. */
  5. typedef enum IRQn
  6. {
  7. /******  Cortex-M3 Processor Exceptions Numbers ***************************************************/
  8.   NonMaskableInt_IRQn         = -14,    /*!< 2 Non Maskable Interrupt                             */
  9.   MemoryManagement_IRQn       = -12,    /*!< 4 Cortex-M3 Memory Management Interrupt              */
  10.   BusFault_IRQn               = -11,    /*!< 5 Cortex-M3 Bus Fault Interrupt                      */
  11.   UsageFault_IRQn             = -10,    /*!< 6 Cortex-M3 Usage Fault Interrupt                    */
  12.   SVCall_IRQn                 = -5,     /*!< 11 Cortex-M3 SV Call Interrupt                       */
  13.   DebugMonitor_IRQn           = -4,     /*!< 12 Cortex-M3 Debug Monitor Interrupt                 */
  14.   PendSV_IRQn                 = -2,     /*!< 14 Cortex-M3 Pend SV Interrupt                       */
  15.   SysTick_IRQn                = -1,     /*!< 15 Cortex-M3 System Tick Interrupt                   */
  16. /******  STM32 specific Interrupt Numbers *********************************************************/
  17.   WWDG_IRQn                   = 0,      /*!< Window WatchDog Interrupt                            */
  18.   PVD_IRQn                    = 1,      /*!< PVD through EXTI Line detection Interrupt            */
  19.   TAMPER_IRQn                 = 2,      /*!< Tamper Interrupt                                     */
  20.   RTC_IRQn                    = 3,      /*!< RTC global Interrupt                                 */
  21.   FLASH_IRQn                  = 4,      /*!< FLASH global Interrupt                               */
  22.   RCC_IRQn                    = 5,      /*!< RCC global Interrupt                                 */
  23.   EXTI0_IRQn                  = 6,      /*!< EXTI Line0 Interrupt                                 */
  24.   EXTI1_IRQn                  = 7,      /*!< EXTI Line1 Interrupt                                 */
  25.   EXTI2_IRQn                  = 8,      /*!< EXTI Line2 Interrupt                                 */
  26.   EXTI3_IRQn                  = 9,      /*!< EXTI Line3 Interrupt                                 */
  27.   EXTI4_IRQn                  = 10,     /*!< EXTI Line4 Interrupt                                 */
  28.   DMA1_Channel1_IRQn          = 11,     /*!< DMA1 Channel 1 global Interrupt                      */
  29.   DMA1_Channel2_IRQn          = 12,     /*!< DMA1 Channel 2 global Interrupt                      */
  30.   DMA1_Channel3_IRQn          = 13,     /*!< DMA1 Channel 3 global Interrupt                      */
  31.   DMA1_Channel4_IRQn          = 14,     /*!< DMA1 Channel 4 global Interrupt                      */
  32.   DMA1_Channel5_IRQn          = 15,     /*!< DMA1 Channel 5 global Interrupt                      */
  33.   DMA1_Channel6_IRQn          = 16,     /*!< DMA1 Channel 6 global Interrupt                      */
  34.   DMA1_Channel7_IRQn          = 17,     /*!< DMA1 Channel 7 global Interrupt                      */
  35.   #ifdef STM32F10X_LD
  36.   ...
  37.   #endif /* STM32F10X_LD */  
  38.   #ifdef STM32F10X_LD_VL
  39.   ...
  40.   #endif /* STM32F10X_LD_VL */
  41.   #ifdef STM32F10X_MD
  42.   ...
  43.   #endif /* STM32F10X_MD */  
  44.   #ifdef STM32F10X_MD_VL
  45.   ...
  46.   #endif /* STM32F10X_MD_VL */
  47.   #ifdef STM32F10X_HD
  48.   ...
  49.   #endif /* STM32F10X_HD */  
  50.   ...
  51.   ...
  52.   ...
  53. } IRQn_Type;
 楼主| 和下土 发表于 2021-5-19 14:48 | 显示全部楼层
为兼容旧版本。
  1. @addtogroup Exported_types
  2. /*!< STM32F10x Standard Peripheral Library old types (maintained for legacy purpose) */
  3. typedef int32_t  s32;
  4. typedef int16_t s16;
  5. typedef int8_t  s8;
  6. typedef const int32_t sc32;  /*!< Read Only */
  7. typedef const int16_t sc16;  /*!< Read Only */
  8. typedef const int8_t sc8;   /*!< Read Only */
  9. typedef __IO int32_t  vs32;
  10. typedef __IO int16_t  vs16;
  11. typedef __IO int8_t   vs8;
  12. typedef __I int32_t vsc32;  /*!< Read Only */
  13. typedef __I int16_t vsc16;  /*!< Read Only */
  14. typedef __I int8_t vsc8;   /*!< Read Only */
  15. typedef uint32_t  u32;
  16. typedef uint16_t u16;
  17. typedef uint8_t  u8;
  18. typedef const uint32_t uc32;  /*!< Read Only */
  19. typedef const uint16_t uc16;  /*!< Read Only */
  20. typedef const uint8_t uc8;   /*!< Read Only */
  21. typedef __IO uint32_t  vu32;
  22. typedef __IO uint16_t vu16;
  23. typedef __IO uint8_t  vu8;
  24. typedef __I uint32_t vuc32;  /*!< Read Only */
  25. typedef __I uint16_t vuc16;  /*!< Read Only */
  26. typedef __I uint8_t vuc8;   /*!< Read Only */
  27. typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus;
  28. typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;
  29. #define IS_FUNCTIONAL_STATE(STATE) (((STATE) == DISABLE) || ((STATE) == ENABLE))
  30. typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus;
  31. /*!< STM32F10x Standard Peripheral Library old definitions (maintained for legacy purpose) */
  32. #define HSEStartUp_TimeOut   HSE_STARTUP_TIMEOUT
  33. #define HSE_Value            HSE_VALUE
  34. #define HSI_Value            HSI_VALUE
 楼主| 和下土 发表于 2021-5-19 14:50 | 显示全部楼层
把全部外设寄存器定义成结构体
  1. /* @addtogroup Peripheral_registers_structures  */  

  2. /**
  3.   * @brief Analog to Digital Converter  
  4.   */
  5. typedef struct
  6. {
  7. } ADC_TypeDef;

  8. /**
  9.   * @brief Backup Registers  
  10.   */
  11. typedef struct
  12. {
  13. } BKP_TypeDef;

  14. /**
  15.   * @brief Controller Area Network TxMailBox
  16.   */
  17. typedef struct
  18. {
  19. } CAN_TxMailBox_TypeDef;

  20. /**
  21.   * @brief Controller Area Network FIFOMailBox
  22.   */
  23. typedef struct
  24. {
  25. } CAN_FIFOMailBox_TypeDef;

  26. /**
  27.   * @brief Controller Area Network FilterRegister
  28.   */
  29. typedef struct
  30. {
  31. } CAN_FilterRegister_TypeDef;

  32. /**
  33.   * @brief Controller Area Network
  34.   */
  35. typedef struct
  36. {
  37. } CAN_TypeDef;

  38. /**
  39.   * @brief Consumer Electronics Control (CEC)
  40.   */
  41. typedef struct
  42. {
  43. } CEC_TypeDef;

  44. /**
  45.   * @brief CRC calculation unit
  46.   */
  47. typedef struct
  48. {
  49. } CRC_TypeDef;

  50. /**
  51.   * @brief Digital to Analog Converter
  52.   */
  53. typedef struct
  54. {
  55. } DAC_TypeDef;

  56. /**
  57.   * @brief Debug MCU
  58.   */
  59. typedef struct
  60. {
  61. }DBGMCU_TypeDef;

  62. /**
  63.   * @brief DMA Controller
  64.   */
  65. typedef struct
  66. {
  67. } DMA_Channel_TypeDef;
  68. typedef struct
  69. {
  70. } DMA_TypeDef;

  71. /**
  72.   * @brief Ethernet MAC
  73.   */
  74. typedef struct
  75. {
  76. } ETH_TypeDef;

  77. /**
  78.   * @brief External Interrupt/Event Controller
  79.   */
  80. typedef struct
  81. {
  82. } EXTI_TypeDef;

  83. /**
  84.   * @brief FLASH Registers
  85.   */
  86. typedef struct
  87. {
  88. } FLASH_TypeDef;

  89. /**
  90.   * @brief Option Bytes Registers
  91.   */
  92. typedef struct
  93. {
  94. } OB_TypeDef;

  95. /**
  96.   * @brief Flexible Static Memory Controller
  97.   */
  98. typedef struct
  99. {
  100. } FSMC_Bank1_TypeDef;

  101. /**
  102.   * @brief Flexible Static Memory Controller Bank1E
  103.   */
  104. typedef struct
  105. {
  106. } FSMC_Bank1E_TypeDef;

  107. /**
  108.   * @brief Flexible Static Memory Controller Bank2
  109.   */
  110. typedef struct
  111. {
  112. } FSMC_Bank2_TypeDef;  

  113. /**
  114.   * @brief Flexible Static Memory Controller Bank3
  115.   */
  116. typedef struct
  117. {
  118. } FSMC_Bank3_TypeDef;

  119. /**
  120.   * @brief General Purpose I/O
  121.   */
  122. typedef struct
  123. {
  124. } GPIO_TypeDef;

  125. /**
  126.   * @brief Alternate Function I/O
  127.   */
  128. typedef struct
  129. {
  130. } AFIO_TypeDef;

  131. /**
  132.   * @brief Inter Integrated Circuit Interface
  133.   */
  134. typedef struct
  135. {
  136. } I2C_TypeDef;

  137. /**
  138.   * @brief Independent WATCHDOG
  139.   */
  140. typedef struct
  141. {
  142. } IWDG_TypeDef;

  143. /**
  144.   * @brief Power Control
  145.   */
  146. typedef struct
  147. {
  148. } PWR_TypeDef;

  149. /**
  150.   * @brief Reset and Clock Control
  151.   */
  152. typedef struct
  153. {
  154. } RCC_TypeDef;

  155. /**
  156.   * @brief Real-Time Clock
  157.   */
  158. typedef struct
  159. {
  160. } RTC_TypeDef;

  161. /**
  162.   * @brief SD host Interface
  163.   */
  164. typedef struct
  165. {
  166. } SDIO_TypeDef;

  167. /**
  168.   * @brief Serial Peripheral Interface
  169.   */
  170. typedef struct
  171. {
  172. } SPI_TypeDef;

  173. /**
  174.   * @brief TIM
  175.   */
  176. typedef struct
  177. {
  178. } TIM_TypeDef;

  179. /**
  180.   * @brief Universal Synchronous Asynchronous Receiver Transmitter
  181.   */
  182. typedef struct
  183. {
  184. } USART_TypeDef;

  185. /**
  186.   * @brief Window WATCHDOG
  187.   */
  188. typedef struct
  189. {
  190. } WWDG_TypeDef;
 楼主| 和下土 发表于 2021-5-19 14:54 | 显示全部楼层
对上面定义的结构体的成员,即寄存器映射到相应的地址上,以下包括一个是地址映射,一个是寄存器声明。
  1. /** @addtogroup Peripheral_memory_map
  2.   * @{
  3.   */
  4. #define FLASH_BASE            ((uint32_t)0x08000000) /*!< FLASH base address in the alias region */
  5. #define SRAM_BASE             ((uint32_t)0x20000000) /*!< SRAM base address in the alias region */
  6. #define PERIPH_BASE           ((uint32_t)0x40000000) /*!< Peripheral base address in the alias region */
  7. #define SRAM_BB_BASE          ((uint32_t)0x22000000) /*!< SRAM base address in the bit-band region */
  8. #define PERIPH_BB_BASE        ((uint32_t)0x42000000) /*!< Peripheral base address in the bit-band region */
  9. #define FSMC_R_BASE           ((uint32_t)0xA0000000) /*!< FSMC registers base address */
  10. /*!< Peripheral memory map */
  11. #define APB1PERIPH_BASE       PERIPH_BASE
  12. #define APB2PERIPH_BASE       (PERIPH_BASE + 0x10000)
  13. #define AHBPERIPH_BASE        (PERIPH_BASE + 0x20000)
  14. #define TIM2_BASE             (APB1PERIPH_BASE + 0x0000)
  15. #define TIM3_BASE             (APB1PERIPH_BASE + 0x0400)
  16. ...
  17. ...
  18. /** @addtogroup Peripheral_declaration
  19.   * @{
  20.   */  
  21. #define TIM2                ((TIM_TypeDef *) TIM2_BASE)
  22. #define TIM3                ((TIM_TypeDef *) TIM3_BASE)
  23. #define TIM4                ((TIM_TypeDef *) TIM4_BASE)
  24. #define TIM5                ((TIM_TypeDef *) TIM5_BASE)
  25. #define TIM6                ((TIM_TypeDef *) TIM6_BASE)
  26. ...
  27. ...
 楼主| 和下土 发表于 2021-5-19 14:55 | 显示全部楼层
一开头讲过,如果定义使用外设驱动,则包含 stm32f10x_conf.h  用来配置所使用外设的库函数
  1. #ifdef USE_STDPERIPH_DRIVER
  2.   #include "stm32f10x_conf.h"
  3. #endif
 楼主| 和下土 发表于 2021-5-19 14:56 | 显示全部楼层
定义一些宏的操作,下面是位操作。
/** @addtogroup Exported_macro
  * @{
  */

#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)))
 楼主| 和下土 发表于 2021-5-19 14:57 | 显示全部楼层

  1. #ifdef __cplusplus
  2. }
  3. #endif

  4. #endif /* __STM32F10x_H */
 楼主| 和下土 发表于 2021-5-19 15:00 | 显示全部楼层
 上面的代码源自 ST3.5版本库的 stm32f10x.h 文件。综合以上,可以看出 stm32f10x.h 用于定义了器件、中断线、数据类型、结构体封装的寄存器、寄存器地址映射、寄存器位操作以及防C++编译的条件编译。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

108

主题

1366

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部