stm32f10x.h解析

  • Post author:
  • Post category:其他


stm32f10x.h解析


https://blog.csdn.net/qq2671368111/article/details/49822401





这里是STM32比较重要的头文件


******************************************************************************

ST版本库的 stm32f10x.h 文件,包含整个头文件代码。综合以上,可以看出 stm32f10x.h 用于定义了器件、中断线、数据类型、结构体封装的寄存器、寄存器地址映射、寄存器位操作以及防C++编译的条件编译。

  * @file    stm32f10x.h                      ST 标准头文件

  * @author  MCD Application Team     微控制器开发小组。
  * @version V3.5.0                               版本
  * @date    11-March-2011                    2011年3月11
  * @brief   CMSIS Cortex-M3 Device Peripheral Access Layer Header File.(CMSIS:Cortex Microcontroller Software Interface Standard) 是 Cortex-M 处理器系列的与供应商无关的硬件抽象层。  
  *          This file contains all the peripheral register's definitions, bits    
  *          definitions and memory mapping for STM32F10x Connectivity line, 
  *          High density, High density value line, Medium density, 
  *          Medium density Value line, Low density, Low density Value line 
  *          and XL-density devices.


* 这个文件包含了芯片STM32F10X(接口增强型)内部的寄存器定义,位定义,内存地址变换定义,还有一些相应的高密度,低密度产品线的设备。

  *          The file is the unique include file that the application programmer
  *          is using in the C source code, usually in main.c. This file contains:
  *           - Configuration section that allows to select:
  *              - The device used in the target application
  *              - To use or not the peripherals drivers in application code(i.e. 
  *                code will be based on direct access to peripherals registers 
  *                rather than drivers API), this option is controlled by 
  *                "#define USE_STDPERIPH_DRIVER"
  *              - To change few application-specific parameters such as the HSE 
  *                crystal frequency
  *           - Data structures and the address mapping for all peripherals
  *           - Peripheral's registers declarations and bits definition
  *           - Macros to access peripheral registers hardware


*这个文件在应用程序中是至关重要的头文件,它是用C代码编写而成。通常包括在MAIN主程序中。它包括:1 设备中使用的目标应用程序。

2 决定是否使用外部设备的代码(例如使用基于寄存器的代码,而不是我们所熟悉的API应用程序接口代码)。这一设备可以通过设置 “#define USE_STDPERIPH_DRIVER” 来实现。

3 通过这一文件可以改变诸如外部设备的频率参数。

4 可以了解程序的数据结构以及外设的地址映射。

5 外设的寄存器声明和位定义可以在这个文件里面了解到。

  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *注意:这一文件代码供应给用户,旨在帮助用户提升效率节约时间。ST将不会提供任何对于用户自己代码的解释和帮助。也不会解决其在代码中的任何BUG。
  * <h2><center>? COPYRIGHT 2011 STMicroelectronics</center></h2>

*/

/** @addtogroup CMSIS

  • @{


    */这个不需要关心,只是说明这个文件的作者,公司产品等等,你贴出来的估计是小小的广而告之,这也是程序员的一点小小的特权嘛

/** @addtogroup stm32f10x

  • @{


    */

#ifndef __STM32F10x_H//如果没有定义__STM32F10x_H ,则定义__STM32F10x_H

#define __STM32F10x_H

#ifdef __cplusplus//

extern “C” {


#endif

这里是一种条件编译。extern “C”指令非常有用,因为C和C++的近亲关系。注意:extern “C”指令中的C,表示的一种编译和连接规约,而不是一种语言。C表示符合C语言的编译和连接规约的任何语言,如Fortran、assembler等。

还有要说明的是,extern “C”指令仅指定编译和连接规约,但不影响语义。例如在函数声明中,指定了extern “C”,仍然要遵守C++的类型检测、参数转换规则。

一开始我不解 extern “C” { 这个语句的意思,原来是用来说明后面的定义都是使用C语言写的。这个 __cplusplus 是指C++来的,4、5句的意思就是说如果用C++编译器的话,它里面是定义有__cplusplus 这个的,而通过 extern “C” { 告知编译器,这段代码是用C编写的,要按照C语言编译。这是因为C++里面有函数重载,编译的时候把参数也编译了,而C的话,编译只编译函数名。

/** @addtogroup Library_configuration_section

  • @{


    */

/* Uncomment the line below according to the target STM32 device used in your

application

*/

#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)

/* #define STM32F10X_LD

/ /

!< STM32F10X_LD: STM32 Low density devices

/

/

#define STM32F10X_LD_VL

/ /

!< STM32F10X_LD_VL: STM32 Low density Value Line devices

/

/

#define STM32F10X_MD

/ /

!< STM32F10X_MD: STM32 Medium density devices

/

/

#define STM32F10X_MD_VL

/ /

!< STM32F10X_MD_VL: STM32 Medium density Value Line devices

/

/

#define STM32F10X_HD

/ /

!< STM32F10X_HD: STM32 High density devices

/

/

#define STM32F10X_HD_VL

/ /

!< STM32F10X_HD_VL: STM32 High density value line devices

/

/

#define STM32F10X_XL

/ /

!< STM32F10X_XL: STM32 XL-density devices

/

/

#define STM32F10X_CL

/ /

!< STM32F10X_CL: STM32 Connectivity line devices */

#endif

STM32启动文件选择:

  • startup_stm32f10x_ld_vl.s: for STM32 Low density Value line devices – startup_stm32f10x_ld.s: for STM32 Low density devices – startup_stm32f10x_md_vl.s: for STM32 Medium density Value line devices – startup_stm32f10x_md.s: for STM32 Medium density devices – startup_stm32f10x_hd.s: for STM32 High density devices – startup_stm32f10x_xl.s: for STM32 XL density devices – startup_stm32f10x_cl.s: for STM32 Connectivity line devices

    cl:互联型产品,stm32f105/107系列

    vl:超值型产品,stm32f100系列

    xl:超高密度产品,stm32f101/103系列

    ld:低密度产品,FLASH小于64K

    md:中等密度产品,FLASH=64 or 128

    hd:高密度产品,FLASH大于128

    /* Tip: To avoid modifying this file each time you need to switch between these

    devices, you can define the device in your toolchain compiler preprocessor.

    提示:为了避免每次编译都要修改这些文件,意思是可以使用编译器的preprocessor选项来定义芯片型号等,免得反复更新这个文件。…
  • Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx microcontrollers

    where the Flash memory density ranges between 16 and 32 Kbytes.
  • Low-density value line devices are STM32F100xx microcontrollers where the Flash

    memory density ranges between 16 and 32 Kbytes.
  • Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx microcontrollers这里定义的是各种产品线的FLASH的大小。

    where the Flash memory density ranges between 64 and 128 Kbytes.
  • Medium-density value line devices are STM32F100xx microcontrollers where the

    Flash memory density ranges between 64 and 128 Kbytes.
  • High-density devices are STM32F101xx and STM32F103xx microcontrollers where

    the Flash memory density ranges between 256 and 512 Kbytes.
  • High-density value line devices are STM32F100xx microcontrollers where the

    Flash memory density ranges between 256 and 512 Kbytes.
  • XL-density devices are STM32F101xx and STM32F103xx microcontrollers where

    the Flash memory density ranges between 512 and 1024 Kbytes.
  • Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.

    */

#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)

#error “Please select first the target STM32F10x device used in your application (in stm32f10x.h file)”

#endif 如果设备类型选择不正确,这里会报错的。

#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

/**

  • @brief In the following line adjust the value of External High Speed oscillator (HSE)

    used in your application

    Tip: To avoid modifying this file each time you need to use different HSE, you

    can define the HSE value in your toolchain compiler preprocessor.

    这些提示:为了避免每次编译都要修改文件,意思是可以使用编译器的preprocessor选项来定义芯片型号等,免得反复更新这个文件。…


    /

    #if !defined HSE_VALUE

    #ifdef STM32F10X_CL

    #define HSE_VALUE ((uint32_t)25000000) /

    !< Value of the External oscillator in Hz

    /

    #else

    #define HSE_VALUE ((uint32_t)8000000) /

    !< Value of the External oscillator in Hz

    /

    #endif /

    STM32F10X_CL

    /

    #endif /

    HSE_VALUE */

/**

  • @brief In the following line adjust the External High Speed oscillator (HSE) Startup

    Timeout value


    /①、HSI是高速内部时钟,RC振荡器,频率为8MHz。  ②、HSE是高速外部时钟,可接石英/陶瓷谐振器,或者接外部时钟源,频率范围为4MHz~16MHz。

    ③、LSI是低速内部时钟,RC振荡器,频率为40kHz。

    ④、LSE是低速外部时钟,接频率为32.768kHz的石英晶体。

    ⑤、PLL为锁相环倍频输出,其时钟输入源可选择为HSI/2、HSE或者HSE/2。倍频可选择为2~16倍,但是其输出频率最大不得超过72MHz。

    #define HSE_STARTUP_TIMEOUT ((uint16_t)0x0500) /

    !< Time out for HSE start up

    /

    高速外部时钟启动时间

    #define HSI_VALUE ((uint32_t)8000000) /

    !< Value of the Internal oscillator in Hz*/

    外部时钟设置的值

    /**
  • @brief STM32F10x Standard Peripheral Library version number标准外设库版本号的定义


    /

    #define __STM32F10X_STDPERIPH_VERSION_MAIN (0x03) /

    !< [31:24] main version

    /

    #define __STM32F10X_STDPERIPH_VERSION_SUB1 (0x05) /

    !< [23:16] sub1 version

    /

    #define __STM32F10X_STDPERIPH_VERSION_SUB2 (0x00) /

    !< [15:8] sub2 version

    /

    #define __STM32F10X_STDPERIPH_VERSION_RC (0x00) /

    !< [7:0] release candidate */

    #define __STM32F10X_STDPERIPH_VERSION ( (__STM32F10X_STDPERIPH_VERSION_MAIN << 24)

    |(__STM32F10X_STDPERIPH_VERSION_SUB1 << 16)

    |(__STM32F10X_STDPERIPH_VERSION_SUB2 << 8)

    |(__STM32F10X_STDPERIPH_VERSION_RC))

/**

  • @}

    */

/** @addtogroup Configuration_section_for_CMSIS

  • @{


    */

/**

  • @brief Configuration of the Cortex-M3 Processor and Core Peripherals 配置Cortex-M3处理器和核内外设


    /

    #ifdef STM32F10X_XL

    #define __MPU_PRESENT 1 /

    !< STM32 XL-density devices provide an MPU

    /

    #else

    #define __MPU_PRESENT 0 /

    !< Other STM32 devices does not provide an MPU

    /

    #endif /

    STM32F10X_XL

    /

    #define __NVIC_PRIO_BITS 4 /

    !< STM32 uses 4 Bits for the Priority Levels

    /

    #define __Vendor_SysTickConfig 0 /

    !< Set to 1 if different SysTick Config is used */

/**

  • @brief STM32F10x Interrupt Number Definition, according to the selected device
  •    in @ref Library_configuration_section  中断线定义,前部分对于所有STM32F10x都有相同定义,后部分根据不同器件容量采用条件编译定义不同中断线。
    

*/

这里是STM32比较重要的头文件

typedef enum IRQn typedef enum表示定义了一个枚举型的数据结构,你可以用题目中的 IRQn 去定义变量

{系统异常ID:

标号

中断ID

描述

NonMaskableInt_IRQn

-14

不可屏蔽中断

MemoryManagement_IRQn

-12

Cortex-M3内存管理中断

BusFault_IRQn

-11

Cortex-M3 总线Fault中断

UsageFault_IRQn

-10

Cortex-M3 用法Fault 中断

SVCall_IRQn

-5

Cortex-M3 SV Call中断

DebugMonitor_IRQn

-4

Cortex-M3 调试监视中断

PendSV_IRQn

-2

Cortex-M3 Pend SV中断

SysTick_IRQn

-1

Cortex-M3 系统Tick中断

外设中断ID:

标号 中断ID 描述 标号 中断ID 描述

WDT_IRQn 0 看门狗 EINT3_IRQn 21 外中断3

TIMER0_IRQn 1 定时器0 ADC_IRQn 22 AD转换

TIMER1_IRQn 2 定时器1 BOD_IRQn 23 欠压检测

TIMER2_IRQn 3 定时器2 USB_IRQn 24 USB

TIMER3_IRQn 4 定时器3 CAN_IRQn 25 CAN

UART0_IRQn 5 UART0 DMA_IRQn 26 通用DMA

UART1_IRQn 6 UART1 I2S_IRQn 27 I2S

UART2_IRQn 7 UART2 ENET_IRQn 28 以太网

UART3_IRQn 8 UART3 MCI_IRQn 29 SD/MMC卡I/F

PWM1_IRQn 9 PWM1 MCPWM_IRQn 30 电机控制PWM

I2C0_IRQn 10 I2C0 QEI_IRQn 31 正交编码接口

I2C1_IRQn 11 I2C1 PLL1_IRQn 32 PLL1锁存

I2C2_IRQn 12 I2C2 USBActivity_IRQn 33 USB活动

Reserved0_IRQn 13 保留 CANActivity_IRQn 34 CAN活动

SSP0_IRQn 14 SSP0 UART4_IRQn 35 UART4

SSP1_IRQn 15 SSP1 SSP2_IRQn 36 SSP2

PLL0_IRQn 16 PLL0锁存 LCD_IRQn 37 LCD

RTC_IRQn 17 RTC GPIO_IRQn 38 GPIO

EINT0_IRQn 18 外中断0 PWM0_IRQn 39 PWM0

EINT1_IRQn 19 外中断1 EEPROM_IRQn 40 EEPROM

EINT2_IRQn 20 外中断2

/****** Cortex-M3 Processor Exceptions Numbers **************************************************

/CMSIS中的中断定义

NonMaskableInt_IRQn = -14, /

!< 2 Non Maskable Interrupt 非屏蔽中断

/

MemoryManagement_IRQn = -12, /

!< 4 Cortex-M3 Memory Management Interrupt Cortex-M3内存管理中断

/

BusFault_IRQn = -11, /

!< 5 Cortex-M3 Bus Fault Interrupt

/

UsageFault_IRQn = -10, /

!< 6 Cortex-M3 Usage Fault Interrupt

/

SVCall_IRQn = -5, /

!< 11 Cortex-M3 SV Call Interrupt

/

DebugMonitor_IRQn = -4, /

!< 12 Cortex-M3 Debug Monitor Interrupt

/

PendSV_IRQn = -2, /

!< 14 Cortex-M3 Pend SV Interrupt

/

SysTick_IRQn = -1, /

!< 15 Cortex-M3 System Tick Interrupt */

/****** STM32 specific Interrupt Numbers ********************************************************

/

WWDG_IRQn = 0, /

!< Window WatchDog Interrupt

/

PVD_IRQn = 1, /

!< PVD through EXTI Line detection Interrupt

/

TAMPER_IRQn = 2, /

!< Tamper Interrupt

/

RTC_IRQn = 3, /

!< RTC global Interrupt

/

FLASH_IRQn = 4, /

!< FLASH global Interrupt

/

RCC_IRQn = 5, /

!< RCC global Interrupt

/

EXTI0_IRQn = 6, /

!< EXTI Line0 Interrupt

/

EXTI1_IRQn = 7, /

!< EXTI Line1 Interrupt

/

EXTI2_IRQn = 8, /

!< EXTI Line2 Interrupt

/

EXTI3_IRQn = 9, /

!< EXTI Line3 Interrupt

/

EXTI4_IRQn = 10, /

!< EXTI Line4 Interrupt

/

DMA1_Channel1_IRQn = 11, /

!< DMA1 Channel 1 global Interrupt

/

DMA1_Channel2_IRQn = 12, /

!< DMA1 Channel 2 global Interrupt

/

DMA1_Channel3_IRQn = 13, /

!< DMA1 Channel 3 global Interrupt

/

DMA1_Channel4_IRQn = 14, /

!< DMA1 Channel 4 global Interrupt

/

DMA1_Channel5_IRQn = 15, /

!< DMA1 Channel 5 global Interrupt

/

DMA1_Channel6_IRQn = 16, /

!< DMA1 Channel 6 global Interrupt

/

DMA1_Channel7_IRQn = 17, /

!< DMA1 Channel 7 global Interrupt */

#ifdef STM32F10X_LD

ADC1_2_IRQn = 18, /*!< ADC1 and ADC2 global Interrupt

/

USB_HP_CAN1_TX_IRQn = 19, /

!< USB Device High Priority or CAN1 TX Interrupts

/

USB_LP_CAN1_RX0_IRQn = 20, /

!< USB Device Low Priority or CAN1 RX0 Interrupts

/

CAN1_RX1_IRQn = 21, /

!< CAN1 RX1 Interrupt

/

CAN1_SCE_IRQn = 22, /

!< CAN1 SCE Interrupt

/

EXTI9_5_IRQn = 23, /

!< External Line[9:5] Interrupts

/

TIM1_BRK_IRQn = 24, /

!< TIM1 Break Interrupt

/

TIM1_UP_IRQn = 25, /

!< TIM1 Update Interrupt

/

TIM1_TRG_COM_IRQn = 26, /

!< TIM1 Trigger and Commutation Interrupt

/

TIM1_CC_IRQn = 27, /

!< TIM1 Capture Compare Interrupt

/

TIM2_IRQn = 28, /

!< TIM2 global Interrupt

/

TIM3_IRQn = 29, /

!< TIM3 global Interrupt

/

I2C1_EV_IRQn = 31, /

!< I2C1 Event Interrupt

/

I2C1_ER_IRQn = 32, /

!< I2C1 Error Interrupt

/

SPI1_IRQn = 35, /

!< SPI1 global Interrupt

/

USART1_IRQn = 37, /

!< USART1 global Interrupt

/

USART2_IRQn = 38, /

!< USART2 global Interrupt

/

EXTI15_10_IRQn = 40, /

!< External Line[15:10] Interrupts

/

RTCAlarm_IRQn = 41, /

!< RTC Alarm through EXTI Line Interrupt

/

USBWakeUp_IRQn = 42 /

!< USB Device WakeUp from suspend through EXTI Line Interrupt

/

#endif /

STM32F10X_LD */

#ifdef STM32F10X_LD_VL

ADC1_IRQn = 18, /*!< ADC1 global Interrupt

/

EXTI9_5_IRQn = 23, /

!< External Line[9:5] Interrupts

/

TIM1_BRK_TIM15_IRQn = 24, /

!< TIM1 Break and TIM15 Interrupts

/

TIM1_UP_TIM16_IRQn = 25, /

!< TIM1 Update and TIM16 Interrupts

/

TIM1_TRG_COM_TIM17_IRQn = 26, /

!< TIM1 Trigger and Commutation and TIM17 Interrupt

/

TIM1_CC_IRQn = 27, /

!< TIM1 Capture Compare Interrupt

/

TIM2_IRQn = 28, /

!< TIM2 global Interrupt

/

TIM3_IRQn = 29, /

!< TIM3 global Interrupt

/

I2C1_EV_IRQn = 31, /

!< I2C1 Event Interrupt

/

I2C1_ER_IRQn = 32, /

!< I2C1 Error Interrupt

/

SPI1_IRQn = 35, /

!< SPI1 global Interrupt

/

USART1_IRQn = 37, /

!< USART1 global Interrupt

/

USART2_IRQn = 38, /

!< USART2 global Interrupt

/

EXTI15_10_IRQn = 40, /

!< External Line[15:10] Interrupts

/

RTCAlarm_IRQn = 41, /

!< RTC Alarm through EXTI Line Interrupt

/

CEC_IRQn = 42, /

!< HDMI-CEC Interrupt

/

TIM6_DAC_IRQn = 54, /

!< TIM6 and DAC underrun Interrupt

/

TIM7_IRQn = 55 /

!< TIM7 Interrupt

/

#endif /

STM32F10X_LD_VL */

这里是STM32比较重要的头文件

系统异常ID:

标号

中断ID

描述

NonMaskableInt_IRQn

-14

不可屏蔽中断

MemoryManagement_IRQn

-12

Cortex-M3内存管理中断

BusFault_IRQn

-11

Cortex-M3 总线Fault中断

UsageFault_IRQn

-10

Cortex-M3 用法Fault 中断

SVCall_IRQn

-5

Cortex-M3 SV Call中断

DebugMonitor_IRQn

-4

Cortex-M3 调试监视中断

PendSV_IRQn

-2

Cortex-M3 Pend SV中断

SysTick_IRQn

-1

Cortex-M3 系统Tick中断

外设中断ID:

标号 中断ID 描述 标号 中断ID 描述

WDT_IRQn 0 看门狗 EINT3_IRQn 21 外中断3

TIMER0_IRQn 1 定时器0 ADC_IRQn 22 AD转换

TIMER1_IRQn 2 定时器1 BOD_IRQn 23 欠压检测

TIMER2_IRQn 3 定时器2 USB_IRQn 24 USB

TIMER3_IRQn 4 定时器3 CAN_IRQn 25 CAN

UART0_IRQn 5 UART0 DMA_IRQn 26 通用DMA

UART1_IRQn 6 UART1 I2S_IRQn 27 I2S

UART2_IRQn 7 UART2 ENET_IRQn 28 以太网

UART3_IRQn 8 UART3 MCI_IRQn 29 SD/MMC卡I/F

PWM1_IRQn 9 PWM1 MCPWM_IRQn 30 电机控制PWM

I2C0_IRQn 10 I2C0 QEI_IRQn 31 正交编码接口

I2C1_IRQn 11 I2C1 PLL1_IRQn 32 PLL1锁存

I2C2_IRQn 12 I2C2 USBActivity_IRQn 33 USB活动

Reserved0_IRQn 13 保留 CANActivity_IRQn 34 CAN活动

SSP0_IRQn 14 SSP0 UART4_IRQn 35 UART4

SSP1_IRQn 15 SSP1 SSP2_IRQn 36 SSP2

PLL0_IRQn 16 PLL0锁存 LCD_IRQn 37 LCD

RTC_IRQn 17 RTC GPIO_IRQn 38 GPIO

EINT0_IRQn 18 外中断0 PWM0_IRQn 39 PWM0

EINT1_IRQn 19 外中断1 EEPROM_IRQn 40 EEPROM

EINT2_IRQn 20 外中断2

#ifdef STM32F10X_MD這是我們前面講過的不同的芯片型號,對應 著不同的中斷級別。請大家自行研究!

ADC1_2_IRQn = 18, /*!< ADC1 and ADC2 global Interrupt

/

USB_HP_CAN1_TX_IRQn = 19, /

!< USB Device High Priority or CAN1 TX Interrupts

/

USB_LP_CAN1_RX0_IRQn = 20, /

!< USB Device Low Priority or CAN1 RX0 Interrupts

/

CAN1_RX1_IRQn = 21, /

!< CAN1 RX1 Interrupt

/

CAN1_SCE_IRQn = 22, /

!< CAN1 SCE Interrupt

/

EXTI9_5_IRQn = 23, /

!< External Line[9:5] Interrupts

/

TIM1_BRK_IRQn = 24, /

!< TIM1 Break Interrupt

/

TIM1_UP_IRQn = 25, /

!< TIM1 Update Interrupt

/

TIM1_TRG_COM_IRQn = 26, /

!< TIM1 Trigger and Commutation Interrupt

/

TIM1_CC_IRQn = 27, /

!< TIM1 Capture Compare Interrupt

/

TIM2_IRQn = 28, /

!< TIM2 global Interrupt

/

TIM3_IRQn = 29, /

!< TIM3 global Interrupt

/

TIM4_IRQn = 30, /

!< TIM4 global Interrupt

/

I2C1_EV_IRQn = 31, /

!< I2C1 Event Interrupt

/

I2C1_ER_IRQn = 32, /

!< I2C1 Error Interrupt

/

I2C2_EV_IRQn = 33, /

!< I2C2 Event Interrupt

/

I2C2_ER_IRQn = 34, /

!< I2C2 Error Interrupt

/

SPI1_IRQn = 35, /

!< SPI1 global Interrupt

/

SPI2_IRQn = 36, /

!< SPI2 global Interrupt

/

USART1_IRQn = 37, /

!< USART1 global Interrupt

/

USART2_IRQn = 38, /

!< USART2 global Interrupt

/

USART3_IRQn = 39, /

!< USART3 global Interrupt

/

EXTI15_10_IRQn = 40, /

!< External Line[15:10] Interrupts

/

RTCAlarm_IRQn = 41, /

!< RTC Alarm through EXTI Line Interrupt

/

USBWakeUp_IRQn = 42 /

!< USB Device WakeUp from suspend through EXTI Line Interrupt

/

#endif /

STM32F10X_MD */

#ifdef STM32F10X_MD_VL這是我們前面講過的不同的芯片型號,對應 著不同的中斷級別。請大家自行研究!

ADC1_IRQn = 18, /*!< ADC1 global Interrupt

/

EXTI9_5_IRQn = 23, /

!< External Line[9:5] Interrupts

/

TIM1_BRK_TIM15_IRQn = 24, /

!< TIM1 Break and TIM15 Interrupts

/

TIM1_UP_TIM16_IRQn = 25, /

!< TIM1 Update and TIM16 Interrupts

/

TIM1_TRG_COM_TIM17_IRQn = 26, /

!< TIM1 Trigger and Commutation and TIM17 Interrupt

/

TIM1_CC_IRQn = 27, /

!< TIM1 Capture Compare Interrupt

/

TIM2_IRQn = 28, /

!< TIM2 global Interrupt

/

TIM3_IRQn = 29, /

!< TIM3 global Interrupt

/

TIM4_IRQn = 30, /

!< TIM4 global Interrupt

/

I2C1_EV_IRQn = 31, /

!< I2C1 Event Interrupt

/

I2C1_ER_IRQn = 32, /

!< I2C1 Error Interrupt

/

I2C2_EV_IRQn = 33, /

!< I2C2 Event Interrupt

/

I2C2_ER_IRQn = 34, /

!< I2C2 Error Interrupt

/

SPI1_IRQn = 35, /

!< SPI1 global Interrupt

/

SPI2_IRQn = 36, /

!< SPI2 global Interrupt

/

USART1_IRQn = 37, /

!< USART1 global Interrupt

/

USART2_IRQn = 38, /

!< USART2 global Interrupt

/

USART3_IRQn = 39, /

!< USART3 global Interrupt

/

EXTI15_10_IRQn = 40, /

!< External Line[15:10] Interrupts

/

RTCAlarm_IRQn = 41, /

!< RTC Alarm through EXTI Line Interrupt

/

CEC_IRQn = 42, /

!< HDMI-CEC Interrupt

/

TIM6_DAC_IRQn = 54, /

!< TIM6 and DAC underrun Interrupt

/

TIM7_IRQn = 55 /

!< TIM7 Interrupt

/

#endif /

STM32F10X_MD_VL */

系统异常ID:

标号

中断ID

描述

NonMaskableInt_IRQn

-14

不可屏蔽中断

MemoryManagement_IRQn

-12

Cortex-M3内存管理中断

BusFault_IRQn

-11

Cortex-M3 总线Fault中断

UsageFault_IRQn

-10

Cortex-M3 用法Fault 中断

SVCall_IRQn

-5

Cortex-M3 SV Call中断

DebugMonitor_IRQn

-4

Cortex-M3 调试监视中断

PendSV_IRQn

-2

Cortex-M3 Pend SV中断

SysTick_IRQn

-1

Cortex-M3 系统Tick中断

外设中断ID:

标号 中断ID 描述 标号 中断ID 描述

WDT_IRQn 0 看门狗 EINT3_IRQn 21 外中断3

TIMER0_IRQn 1 定时器0 ADC_IRQn 22 AD转换

TIMER1_IRQn 2 定时器1 BOD_IRQn 23 欠压检测

TIMER2_IRQn 3 定时器2 USB_IRQn 24 USB

TIMER3_IRQn 4 定时器3 CAN_IRQn 25 CAN

UART0_IRQn 5 UART0 DMA_IRQn 26 通用DMA

UART1_IRQn 6 UART1 I2S_IRQn 27 I2S

UART2_IRQn 7 UART2 ENET_IRQn 28 以太网

UART3_IRQn 8 UART3 MCI_IRQn 29 SD/MMC卡I/F

PWM1_IRQn 9 PWM1 MCPWM_IRQn 30 电机控制PWM

I2C0_IRQn 10 I2C0 QEI_IRQn 31 正交编码接口

I2C1_IRQn 11 I2C1 PLL1_IRQn 32 PLL1锁存

I2C2_IRQn 12 I2C2 USBActivity_IRQn 33 USB活动

Reserved0_IRQn 13 保留 CANActivity_IRQn 34 CAN活动

SSP0_IRQn 14 SSP0 UART4_IRQn 35 UART4

SSP1_IRQn 15 SSP1 SSP2_IRQn 36 SSP2

PLL0_IRQn 16 PLL0锁存 LCD_IRQn 37 LCD

RTC_IRQn 17 RTC GPIO_IRQn 38 GPIO

EINT0_IRQn 18 外中断0 PWM0_IRQn 39 PWM0

EINT1_IRQn 19 外中断1 EEPROM_IRQn 40 EEPROM

EINT2_IRQn 20 外中断2

#ifdef STM32F10X_HD 這是我們前面講過的不同的芯片型號,對應 著不同的中斷級別。請大家自行研究!

ADC1_2_IRQn = 18, /*!< ADC1 and ADC2 global Interrupt

/

USB_HP_CAN1_TX_IRQn = 19, /

!< USB Device High Priority or CAN1 TX Interrupts

/

USB_LP_CAN1_RX0_IRQn = 20, /

!< USB Device Low Priority or CAN1 RX0 Interrupts

/

CAN1_RX1_IRQn = 21, /

!< CAN1 RX1 Interrupt

/

CAN1_SCE_IRQn = 22, /

!< CAN1 SCE Interrupt

/

EXTI9_5_IRQn = 23, /

!< External Line[9:5] Interrupts

/

TIM1_BRK_IRQn = 24, /

!< TIM1 Break Interrupt

/

TIM1_UP_IRQn = 25, /

!< TIM1 Update Interrupt

/

TIM1_TRG_COM_IRQn = 26, /

!< TIM1 Trigger and Commutation Interrupt

/

TIM1_CC_IRQn = 27, /

!< TIM1 Capture Compare Interrupt

/

TIM2_IRQn = 28, /

!< TIM2 global Interrupt

/

TIM3_IRQn = 29, /

!< TIM3 global Interrupt

/

TIM4_IRQn = 30, /

!< TIM4 global Interrupt

/

I2C1_EV_IRQn = 31, /

!< I2C1 Event Interrupt

/

I2C1_ER_IRQn = 32, /

!< I2C1 Error Interrupt

/

I2C2_EV_IRQn = 33, /

!< I2C2 Event Interrupt

/

I2C2_ER_IRQn = 34, /

!< I2C2 Error Interrupt

/

SPI1_IRQn = 35, /

!< SPI1 global Interrupt

/

SPI2_IRQn = 36, /

!< SPI2 global Interrupt

/

USART1_IRQn = 37, /

!< USART1 global Interrupt

/

USART2_IRQn = 38, /

!< USART2 global Interrupt

/

USART3_IRQn = 39, /

!< USART3 global Interrupt

/

EXTI15_10_IRQn = 40, /

!< External Line[15:10] Interrupts

/

RTCAlarm_IRQn = 41, /

!< RTC Alarm through EXTI Line Interrupt

/

USBWakeUp_IRQn = 42, /

!< USB Device WakeUp from suspend through EXTI Line Interrupt

/

TIM8_BRK_IRQn = 43, /

!< TIM8 Break Interrupt

/

TIM8_UP_IRQn = 44, /

!< TIM8 Update Interrupt

/

TIM8_TRG_COM_IRQn = 45, /

!< TIM8 Trigger and Commutation Interrupt

/

TIM8_CC_IRQn = 46, /

!< TIM8 Capture Compare Interrupt

/

ADC3_IRQn = 47, /

!< ADC3 global Interrupt

/

FSMC_IRQn = 48, /

!< FSMC global Interrupt

/

SDIO_IRQn = 49, /

!< SDIO global Interrupt

/

TIM5_IRQn = 50, /

!< TIM5 global Interrupt

/

SPI3_IRQn = 51, /

!< SPI3 global Interrupt

/

UART4_IRQn = 52, /

!< UART4 global Interrupt

/

UART5_IRQn = 53, /

!< UART5 global Interrupt

/

TIM6_IRQn = 54, /

!< TIM6 global Interrupt

/

TIM7_IRQn = 55, /

!< TIM7 global Interrupt

/

DMA2_Channel1_IRQn = 56, /

!< DMA2 Channel 1 global Interrupt

/

DMA2_Channel2_IRQn = 57, /

!< DMA2 Channel 2 global Interrupt

/

DMA2_Channel3_IRQn = 58, /

!< DMA2 Channel 3 global Interrupt

/

DMA2_Channel4_5_IRQn = 59 /

!< DMA2 Channel 4 and Channel 5 global Interrupt

/

#endif /

STM32F10X_HD */

#include “core_cm3.h”STM32的内核是ARM CORTEX-M3,这个core_cm3.h包含的是一些内核相关的函数和宏定义,例如核内寄存器定义、部分核内外设的地址等等,其对应的是core_cm3.c文件。初学可以不管他,只要把它包含进工程里就行了,这里都是非常底层的函数,上层的一些函数直接调用它们了。如果对CORTEX-M3内核有兴趣或者基础较高,想深入了解这些函数,可以仔细研读一下,我们在后续的开讲中会仔细讲解这一个文件的内容。

#include “system_stm32f10x.h”这个文件就是我们现在所讲的文件了。

#include <stdint.h><stdint.h> 定义了 int16_t 、 uint32_t 、 int64_t 等整型,在需要确定大小的整型时可以使用它们代替 short 、 unsigned long long 等,在 C 整型中,只使用 int 。

/** @addtogroup Exported_types

  • @{


    */

/*!< STM32F10x Standard Peripheral Library old types (maintained for legacy purpose) */由于新的文件与老的文件会有不兼容问题,所以要转换变量类型。

typedef int32_t s32; typedef 在计算机编程语言中用来为复杂的声明定义简单的别名,与宏定义有些差异。它本身是一种存储类的关键字,与auto、extern、mutable、static、register等关键字不能出现在同一个表达式中。

《变量类型》,3.0以后的版本中使用了CMSIS数据类型,变量的定义有所不同,但是出于兼容旧版本的目的,以上的数据类型仍然兼容。

CMSIS IO类型限定词

IO类限定词

#define

描述

_I

volatile const

只读访问

_O

volatile

只写访问

_IO

volatile

读和写访问

件库与CMSIS数据类型对比

固件库类型

CMSIS类型

描述

s32

int32_t

易挥发只读有符号32位数据

s16

int16_t

易挥发只读有符号16位数据

s8

int8_t

易挥发只读有符号8位数据

sc32

const int32_t

只读有符号32位数据

sc16

const int16_t

只读有符号16位数据

sc8

const int8_t

只读有符号8位数据

vs32

_IO int32_t

易挥发读写访问有符号32位数据

vs16

_IO int16_t

易挥发读写访问有符号16位数据

vs8

_IO int8_t

易挥发读写访问有符号8位数据

vsc32

_I int32_t

易挥发只读有符号32位数据

vsc16

_I int16_t

易挥发只读有符号16位数据

vsc8

_I int8_t

易挥发只读有符号8位数据

u32

uint32_t

无符号32位数据

u16

uint16_t

无符号16位数据

u8

uint8_t

无符号8位数据

uc32

const uint32_t

只读无符号32位数据

uc16

const uint16_t

只读无符号16位数据

uc8

const uint8_t

只读无符号8位数据

vu32

_IO uint32_t

易挥发读写访问无符号32位数据

vu16

_IO uint16_t

易挥发读写访问无符号16位数据

vu8

_IO uint8_t

易挥发读写访问无符号8位数据

vuc32

_I uint32_t

易挥发只读无符号32位数据

vuc16

_I uint16_t

易挥发只读无符号16位数据

vuc8

_I uint8_t

易挥发只读无符号8位数据

stm32f10x.h文件中还包含了常用的布尔形变量定义,如:

1 typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus;

2

3 typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;

4

5 #define IS_FUNCTIONAL_STATE(STATE) (((STATE) == DISABLE) || ((STATE) == ENABLE))

6

7 typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus;

不同版本的标准外设库的变量定义略有不同,如3.4版本中就没有之前版本的TRUE和FALSE的定义,用户也可以根据自己的需求按照上面的格式定义自己的布尔形变量。在使用标准外设库进行开发遇到相关的定义问题时应首先找到对应的头文件定义。

typedef int16_t s16;

typedef int8_t s8;

typedef const int32_t sc32; /*!< Read Only

/

const是一个C语言的关键字,它限定一个变量不允许被改变。使用const在一定程度上可以提高程序的安全性和可靠性。另外,在观看别人代码的时候,清晰理解const所起的作用,对理解对方的程序也有一些帮助。另外CONST在其他编程语言中也有出现,如C++、PHP5、C#.net、HC08 C。

typedef const int16_t sc16; /

!< Read Only

/

typedef const int8_t sc8; /

!< Read Only */

typedef __IO int32_t vs32;

typedef __IO int16_t vs16;

typedef __IO int8_t vs8;

typedef __I int32_t vsc32; /*!< Read Only

/

typedef __I int16_t vsc16; /

!< Read Only

/

typedef __I int8_t vsc8; /

!< Read Only */

typedef uint32_t u32;

typedef uint16_t u16;

typedef uint8_t u8;

typedef const uint32_t uc32; /*!< Read Only

/

typedef const uint16_t uc16; /

!< Read Only

/

typedef const uint8_t uc8; /

!< Read Only */

typedef __IO uint32_t vu32;

typedef __IO uint16_t vu16;

typedef __IO uint8_t vu8;

typedef __I uint32_t vuc32; /*!< Read Only

/

typedef __I uint16_t vuc16; /

!< Read Only

/

typedef __I uint8_t vuc8; /

!< Read Only */

typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus;

typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;

#define IS_FUNCTIONAL_STATE(STATE) (((STATE) == DISABLE) || ((STATE) == ENABLE))

typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus;

/*!< STM32F10x Standard Peripheral Library old definitions (maintained for legacy purpose)

/

由于新的文件与老的文件会有不兼容问题,所以要转换变量类型。

#define HSEStartUp_TimeOut HSE_STARTUP_TIMEOUT 请大家注意这里的定义,高速外设时钟的启动时间。

#define HSE_Value HSE_VALUE

#define HSI_Value HSI_VALUE

/

*

  • @}

    */

/** @addtogroup Peripheral_registers_structures

  • @{


    */

/**

  • @brief Analog to Digital Converter

    */

typedef struct ADC_TypeDef结构体的声名,看看它内部都有哪些内容。请读者注意。

ADC_TypeDef,看起来是一个结构类型,是对ADC寄存器各地址的一个定义。

{__IO就是volatileuint32_t就是32位无符号整形数。

这都是ARM公司无聊滴在CMSIS库里面定义的。STM32库1.x 2.x都没有这玩意,到了3.0以后引入了CMSIS就有了。 __IO uint32_t SR;

__IO uint32_t CR1;

__IO uint32_t CR2;

__IO uint32_t SMPR1;

__IO uint32_t SMPR2;

__IO uint32_t JOFR1;

__IO uint32_t JOFR2;

__IO uint32_t JOFR3;

__IO uint32_t JOFR4;

__IO uint32_t HTR;

__IO uint32_t LTR;

__IO uint32_t SQR1;

__IO uint32_t SQR2;

__IO uint32_t SQR3;

__IO uint32_t JSQR;

__IO uint32_t JDR1;

__IO uint32_t JDR2;

__IO uint32_t JDR3;

__IO uint32_t JDR4;

__IO uint32_t DR;

} ADC_TypeDef;

/**

  • @brief Backup Registers

    */

typedef struct 这个结构体声明的是数据备份寄存器,它们是16位的。它可以管理防范入侵,可以存储RTC检验值,可以做数据备份。

{


uint32_t RESERVED0;

__IO uint16_t DR1; DR代表数据备份1—45

uint16_t RESERVED1;

__IO uint16_t DR2;

uint16_t RESERVED2;

__IO uint16_t DR3;

uint16_t RESERVED3;

__IO uint16_t DR4;

uint16_t RESERVED4;

__IO uint16_t DR5;

uint16_t RESERVED5;

__IO uint16_t DR6;

uint16_t RESERVED6;

__IO uint16_t DR7;

uint16_t RESERVED7;

__IO uint16_t DR8;

uint16_t RESERVED8;

__IO uint16_t DR9;

uint16_t RESERVED9;

__IO uint16_t DR10;

uint16_t RESERVED10;

__IO uint16_t RTCCR; RTC时钟校准寄存器

uint16_t RESERVED11;

__IO uint16_t CR; CR备份控制寄存器

uint16_t RESERVED12;

__IO uint16_t CSR; CSR 备份控制状态寄存器

uint16_t RESERVED13[5];

__IO uint16_t DR11;

uint16_t RESERVED14;

__IO uint16_t DR12;

uint16_t RESERVED15;

__IO uint16_t DR13;

uint16_t RESERVED16;

__IO uint16_t DR14;

uint16_t RESERVED17;

__IO uint16_t DR15;

uint16_t RESERVED18;

__IO uint16_t DR16;

uint16_t RESERVED19;

__IO uint16_t DR17;

uint16_t RESERVED20;

__IO uint16_t DR18;

uint16_t RESERVED21;

__IO uint16_t DR19;

uint16_t RESERVED22;

__IO uint16_t DR20;

uint16_t RESERVED23;

__IO uint16_t DR21;

uint16_t RESERVED24;

__IO uint16_t DR22;

uint16_t RESERVED25;

__IO uint16_t DR23;

uint16_t RESERVED26;

__IO uint16_t DR24;

uint16_t RESERVED27;

__IO uint16_t DR25;

uint16_t RESERVED28;

__IO uint16_t DR26;

uint16_t RESERVED29;

__IO uint16_t DR27;

uint16_t RESERVED30;

__IO uint16_t DR28;

uint16_t RESERVED31;

__IO uint16_t DR29;

uint16_t RESERVED32;

__IO uint16_t DR30;

uint16_t RESERVED33;

__IO uint16_t DR31;

uint16_t RESERVED34;

__IO uint16_t DR32;

uint16_t RESERVED35;

__IO uint16_t DR33;

uint16_t RESERVED36;

__IO uint16_t DR34;

uint16_t RESERVED37;

__IO uint16_t DR35;

uint16_t RESERVED38;

__IO uint16_t DR36;

uint16_t RESERVED39;

__IO uint16_t DR37;

uint16_t RESERVED40;

__IO uint16_t DR38;

uint16_t RESERVED41;

__IO uint16_t DR39;

uint16_t RESERVED42;

__IO uint16_t DR40;

uint16_t RESERVED43;

__IO uint16_t DR41;

uint16_t RESERVED44;

__IO uint16_t DR42;

uint16_t RESERVED45;

} BKP_TypeDef;

/**

  • @brief Controller Area Network TxMailBox

    */

typedef struct

{


__IO uint32_t TIR;

__IO uint32_t TDTR;

__IO uint32_t TDLR;

__IO uint32_t TDHR;

} CAN_TxMailBox_TypeDef; CAN的发送邮箱寄存器的结构体定义。

/**

  • @brief Controller Area Network FIFOMailBox

    */

typedef struct

{


__IO uint32_t RIR;

__IO uint32_t RDTR;

__IO uint32_t RDLR;

__IO uint32_t RDHR;

} CAN_FIFOMailBox_TypeDef;先进先出(FIFO)缓冲器,是一种寄存器。在这个头文件里定义了它的结构体类型。

/**

  • @brief Controller Area Network FilterRegister

    */

typedef struct

{


__IO uint32_t FR1;

__IO uint32_t FR2;

}CAN_Filter Register_TypeDef;CAN硬件过滤器工作模式、访问码和访问码掩码。

通过设置CAN过滤器,可以使CAN端口只接收符合规定的的CAN数据包,这样就能将CAN总线划分成若干网段用于支持不同应用。

/**

  • @brief Controller Area Network

    */

typedef struct

{


__IO uint32_t MCR;

__IO uint32_t MSR;

__IO uint32_t TSR;

__IO uint32_t RF0R;

__IO uint32_t RF1R;

__IO uint32_t IER;

__IO uint32_t ESR;

__IO uint32_t BTR;

uint32_t RESERVED0[88];

CAN_TxMailBox_TypeDef sTxMailBox[3];

CAN_FIFOMailBox_TypeDef sFIFOMailBox[2];

uint32_t RESERVED1[12];

__IO uint32_t FMR;

__IO uint32_t FM1R;

uint32_t RESERVED2;

__IO uint32_t FS1R;

uint32_t RESERVED3;

__IO uint32_t FFA1R;

uint32_t RESERVED4;

__IO uint32_t FA1R;

uint32_t RESERVED5[8];

#ifndef STM32F10X_CL

CAN_FilterRegister_TypeDef sFilterRegister[14];

#else

CAN_FilterRegister_TypeDef sFilterRegister[28];

#endif /* STM32F10X_CL */

} CAN_TypeDef;

/**

  • @brief Consumer Electronics Control (CEC)

    */

    typedef struct

    {


    __IO uint32_t CFGR;

    __IO uint32_t OAR;

    __IO uint32_t PRES;

    __IO uint32_t ESR;

    __IO uint32_t CSR;

    __IO uint32_t TXD;

    __IO uint32_t RXD;

    } CEC_TypeDef; CEC用于记录CAN总线发送数据错误的计数器

/**

  • @brief CRC calculation unit

    */

typedef struct

{


__IO uint32_t DR;

__IO uint8_t IDR;

uint8_t RESERVED0;

uint16_t RESERVED1;

__IO uint32_t CR;

} CRC_TypeDe CRC校验

/**

  • @brief Digital to Analog Converter 数模转换相应的结构体,DAC相关寄存器啊

    */

typedef struct

{


__IO uint32_t CR;

__IO uint32_t SWTRIGR; SWTRIGR 这个DAC软件触发寄存器

__IO uint32_t DHR12R1; DAC 通道 1 的 12 位右对齐数据保持寄存器:DAC_DHR12R1。这个我只讲一个,这里面全部是关于DAC转换的相关寄存器。请大家自行对照硬件手册查找。

__IO uint32_t DHR12L1;

__IO uint32_t DHR8R1;

__IO uint32_t DHR12R2;

__IO uint32_t DHR12L2;

__IO uint32_t DHR8R2;

__IO uint32_t DHR12RD;

__IO uint32_t DHR12LD;

__IO uint32_t DHR8RD;

__IO uint32_t DOR1;

__IO uint32_t DOR2;

#if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)

__IO uint32_t SR;

#endif

} DAC_TypeDef;

/**

  • @brief DMA Controller

    DMA 传输将数据从一个地址空间复制到另外一个地址空间。当CPU 初始化这个传输动作,传输动作本身是由 DMA 控制器来实行和完成。典型的例子就是移动一个外部内存的区块到芯片内部更快的内存区。像是这样的操作并没有让处理器工作拖延,反而可以被重新排程去处理其他的工作。DMA 传输对于高效能 嵌入式系统算法和网络是很重要的。

DMA

在实现DMA传输时,是由DMA控制器直接掌管总线,因此,存在着一个总线控制权转移问题。即DMA传输前,CPU要把总线控制权交给DMA控制器,而在结束DMA传输后,DMA控制器应立即把总线控制权再交回给CPU。一个完整的DMA传输过程必须经过DMA请求、DMA响应、DMA传输、DMA结束4个步骤。

*/

typedef struct 这里就是直接内存寻址寄存器相关定义,我想DMA大家应该不是太陌生。

{


__IO uint32_t CCR;

__IO uint32_t CNDTR;

__IO uint32_t CPAR;

__IO uint32_t CMAR;

} DMA_Channel_TypeDef;

typedef struct

{


__IO uint32_t ISR;

__IO uint32_t IFCR;

} DMA_TypeDef;

/**

  • @brief Ethernet MAC

    */ MAC(Medium Access Control)模块,实现符合 IEEE 802.3 标准的 MAC 逻辑。 这个结构体是网络模块的相关定义。

typedef struct

{


__IO uint32_t MACCR;

__IO uint32_t MACFFR;

__IO uint32_t MACHTHR;

__IO uint32_t MACHTLR;

__IO uint32_t MACMIIAR;

__IO uint32_t MACMIIDR;

__IO uint32_t MACFCR;

__IO uint32_t MACVLANTR; /* 8

/

uint32_t RESERVED0[2];

__IO uint32_t MACRWUFFR; /

11

/

__IO uint32_t MACPMTCSR;

uint32_t RESERVED1[2];

__IO uint32_t MACSR; /

15

/

__IO uint32_t MACIMR;

__IO uint32_t MACA0HR;

__IO uint32_t MACA0LR;

__IO uint32_t MACA1HR;

__IO uint32_t MACA1LR;

__IO uint32_t MACA2HR;

__IO uint32_t MACA2LR;

__IO uint32_t MACA3HR;

__IO uint32_t MACA3LR; /

24

/

uint32_t RESERVED2[40];

__IO uint32_t MMCCR; /

65

/

__IO uint32_t MMCRIR;

__IO uint32_t MMCTIR;

__IO uint32_t MMCRIMR;

__IO uint32_t MMCTIMR; /

69

/

uint32_t RESERVED3[14];

__IO uint32_t MMCTGFSCCR; /

84 */

__IO uint32_t MMCTGFMSCCR;

uint32_t RESERVED4[5];

__IO uint32_t MMCTGFCR;

uint32_t RESERVED5[10];

__IO uint32_t MMCRFCECR;

__IO uint32_t MMCRFAECR;

uint32_t RESERVED6[10];

__IO uint32_t MMCRGUFCR;

uint32_t RESERVED7[334];

__IO uint32_t PTPTSCR;

__IO uint32_t PTPSSIR;

__IO uint32_t PTPTSHR;

__IO uint32_t PTPTSLR;

__IO uint32_t PTPTSHUR;

__IO uint32_t PTPTSLUR;

__IO uint32_t PTPTSAR;

__IO uint32_t PTPTTHR;

__IO uint32_t PTPTTLR;

uint32_t RESERVED8[567];

__IO uint32_t DMABMR;

__IO uint32_t DMATPDR;

__IO uint32_t DMARPDR;

__IO uint32_t DMARDLAR;

__IO uint32_t DMATDLAR;

__IO uint32_t DMASR;

__IO uint32_t DMAOMR;

__IO uint32_t DMAIER;

__IO uint32_t DMAMFBOCR;

uint32_t RESERVED9[9];

__IO uint32_t DMACHTDR;

__IO uint32_t DMACHRDR;

__IO uint32_t DMACHTBAR;

__IO uint32_t DMACHRBAR;

} ETH_TypeDef;

/**

  • @brief External Interrupt/Event Controller

    */

    EXTI寄存器结构

EXTI 寄存器结构,EXTI_TypeDef,在文件stm2f10x_map.h中定义如下:

typedef struct {


vu32 IMR; vu32 EMR; vu32 RTSR; vu32 FTSR; vu32 SWIER; vu32 PR;

}EXTI_TypeDef;

Table 128. 例举了EXTI 所有寄存器 Table 128. EXTI寄存器

寄存器 描述 IMR 中断屏蔽寄存器 EMR 事件屏蔽寄存器 RTSR 上升沿触发选择寄存器 FTSR 下降沿触发选择寄存器 SWIR 软件中断事件寄存器 PR 挂起寄存器

typedef struct

{


__IO uint32_t IMR;

__IO uint32_t EMR;

__IO uint32_t RTSR;

__IO uint32_t FTSR;

__IO uint32_t SWIER;

__IO uint32_t PR;

} EXTI_TypeDef;

/**

  • @brief FLASH Registers


    /

    FLASH寄存器

    寄存器 描述 ACR FLASH访问控制寄存器 KEYR FPEC密钥寄存器 OPTKEYR 选择字节密钥寄存器 SR FLASH状态寄存器 CR FLASH控制寄存器 AR FLASH地址寄存器 OBR 选择字节和状态寄存器 WRPR 选择字节写保护寄存器

    typedef struct

    {


    __IO uint32_t ACR;

    __IO uint32_t KEYR;

    __IO uint32_t OPTKEYR;

    __IO uint32_t SR;

    __IO uint32_t CR;

    __IO uint32_t AR;

    __IO uint32_t RESERVED;

    __IO uint32_t OBR;

    __IO uint32_t WRPR;

    #ifdef STM32F10X_XL

    uint32_t RESERVED1[8];

    __IO uint32_t KEYR2;

    uint32_t RESERVED2;

    __IO uint32_t SR2;

    __IO uint32_t CR2;

    __IO uint32_t AR2;

    #endif /

    STM32F10X_XL */

    } FLASH_TypeDef;

本帖最后由 我爱你的吻123 于 2014-6-13 11:49 编辑


//本程序只供学习使用,如果用于工程应用,请仔细检查


//主要对于ST公司的库存文件stm32f10x.h进行解析


//修改日期:2014/6

//Copyright© 2014-2025

//All rights reserved

//******************************************************************************** */

/**

  • @brief Option Bytes Registers

    */选项字节寄存器

typedef struct

{


__IO uint16_t RDP;RDP 为 读出选项字节 ,USER 为 用户选项字节 , Data0 为 数据 0 选项字节,Data1 为 数据 1 选项字节,WRP0 为 写保护 0的先项字节。依此类推

__IO uint16_t USER;

__IO uint16_t Data0;

__IO uint16_t Data1;

__IO uint16_t WRP0;

__IO uint16_t WRP1;

__IO uint16_t WRP2;

__IO uint16_t WRP3;

} OB_TypeDef;

/**

  • @brief Flexible Static Memory Controller

    */灵活的静态存储器控制器

    FSMC(Flexible Static Memory Controller,可变静态存储控制器)是STM32系列采用一种新型的存储器扩展技术。在外部存储器扩展方面具有独特的优势,可根据系统的应用需要,方便地进行不同类型大容量静态存储器的扩展。

FSMC技术优势①支持多种静态存储器类型。STM32通过FSMC可以与SRAM、ROM、PSRAM、NOR Flash和NANDFlash存储器的引脚直接相连。

②支持丰富的存储操作方法。FSMC不仅支持多种数据宽度的异步读/写操作,而且支持对NOR/PSRAM/NAND存储器的同步突发访问方式。

③支持同时扩展多种存储器。FSMC的映射地址空间中,不同的BANK是独立的,可用于扩展不同类型的存储器。当系统中扩展和使用多个外部存储器时,FSMC会通过总线悬空延迟时间参数的设置,防止各存储器对总线的访问冲突。

④支持更为广泛的存储器型号。通过对FSMC的时间参数设置,扩大了系统中可用存储器的速度范围,为用户提供了灵活的存储芯片选择空间。

⑤支持代码从FSMC扩展的外部存储器中直接运行,而不需要首先调入内部SRAM。

typedef struct

{


__IO uint32_t BTCR[8];

} FSMC_Bank1_TypeDef;

/**

  • @brief Flexible Static Memory Controller Bank1E

    */

    FSMC映射地址空间FSMC管理1 GB的映射地址空间。该空间划分为4个大小为256 MB的BANK,每个BANK又划分为4个64 MB的子BANK,如表1所列。FSMC的2个控制器管理的映射地址空间不同。NOR Flash控制器管理第1个BANK,NAND/PC Card控制器管理第2~4个BANK。由于两个控制器管理的存储器类型不同,扩展时应根据选用的存储设备类型确定其映射位置。其中,BANK1的4个子BANK拥有独立的片选线和控制寄存器,可分别扩展一个独立的存储设备,而BANK2~BANK4只有一组控制寄存器。

    typedef struct

    {


    __IO uint32_t BWTR[7];

    } FSMC_Bank1E_TypeDef;那么这里就是FSMC的BANK1的结构体定义

/**

  • @brief Flexible Static Memory Controller Bank2

    */

typedef struct

{


__IO uint32_t PCR2;

__IO uint32_t SR2;

__IO uint32_t PMEM2;

__IO uint32_t PATT2;

uint32_t RESERVED0;

__IO uint32_t ECCR2;

} FSMC_Bank2_TypeDef;

/**

  • @brief Flexible Static Memory Controller Bank3

    */

typedef struct

{


__IO uint32_t PCR3;

__IO uint32_t SR3;

__IO uint32_t PMEM3;

__IO uint32_t PATT3;

uint32_t RESERVED0;

__IO uint32_t ECCR3;

} FSMC_Bank3_TypeDef;

/**

  • @brief Flexible Static Memory Controller Bank4

    */

typedef struct

{


__IO uint32_t PCR4;

__IO uint32_t SR4;

__IO uint32_t PMEM4;

__IO uint32_t PATT4;

__IO uint32_t PIO4;

} FSMC_Bank4_TypeDef;

/**

  • @brief General Purpose I/O

    */

typedef struct

{


__IO uint32_t CRL;

__IO uint32_t CRH;

__IO uint32_t IDR;

__IO uint32_t ODR;

__IO uint32_t BSRR;

__IO uint32_t BRR;

__IO uint32_t LCKR;

} GPIO_TypeDef;

CRL、CRH、IDR、ODR等都是一个端口中的寄存器,对寄存器的读写操作实现你需要的功能。STM32 的每个 IO 端口都有 7 个寄存器来控制。他们分别是:配置模式的 2 个 32 位的端口 配置寄存器 CRL 和 CRH;2 个 32 位的数据寄存器 IDR 和 ODR;1 个 32 位的置位/复位寄存器 BSRR;一个 16 位的复位寄存器 BRR; 1 个 32 位的锁存寄存器 LCKR

GPIO_Pin

该参数选择待设置的GPIO管脚,使用操作符“|”可以一次选中多个管脚。可以使用下表中的任意组合。

GPIO_Pin_None:无管脚被选中

GPIO_Pin_x:选中管脚x(0–15)

GPIO_Pin_All:选中全部管脚

GPIO_Speed

GPIO_Speed:用以设置选中管脚的速率。

GPIO_Speed_10MHz:最高输出速率10MHz

GPIO_Speed_2MHz:最高输出速率2MHz

GPIO_Speed_50MHz:最高输出速率50MHz

GPIO_Mode

GPIO_Mode:用以设置选中管脚的工作状态。

GPIO_Mode_AIN:模拟输入

GPIO_Mode_IN_FLOATING:浮空输入

GPIO_Mode_IPD:下拉输入

GPIO_Mode_IPU:上拉输入

GPIO_Mode_Out_OD:开漏输出

GPIO_Mode_Out_PP:推挽输出

GPIO_Mode_AF_OD:复用开漏输出

GPIO_Mode_AF_PP:复用推挽输出

函数GPIO_StructInit

功能描述:把GPIO_InitStruct中的每一个参数按缺省值填入

例:

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_StructInit(&GPIO_InitStructure);

GPIO_InitStruct:

GPIO_Pin:GPIO_Pin_All

GPIO_Speed:GPIO_Speed_2MHz

GPIO_Mode:GPIO_Mode_IN_FLOATING

函数GPIO_ReadInputDataBit

功能描述:读取指定端口管脚的输入

例:

u8 ReadValue;

ReadValue = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7);

函数GPIO_ReadInputData

功能描述:读取指定的GPIO端口输入

例:

u16 ReadValue;

ReadValue = GPIO_ReadInputData(GPIOC);

函数GPIO_ReadOutputDataBit

功能描述:读取指定端口管脚的输出

例:

u8 ReadValue;

ReadValue = GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_7);

函数GPIO_ReadOutputData

功能描述:读取指定的GPIO端口输出

例:

u16 ReadValue;

ReadValue = GPIO_ReadOutputData(GPIOC);

函数GPIO_SetBits

功能描述:置位指定的数据端口位

例:

GPIO_SetBits(GPIOA, GPIO_Pin_10 | GPIO_Pin_15);

函数GPIO_ResetBits

功能描述:清除指定的数据端口位

例:

GPIO_ResetBits(GPIOA, GPIO_Pin_10 | GPIO_Pin_15);