1/368
译文英文原版为 UM0427 Oct. 2007 Rev 2, 译文仅供参考,与英文版冲突的,以英文版为准
32 位基于 ARM 微控制器 STM32F101xx 与 STM32F103xx
固件函数库
UM0427 用户手册
介绍
本手册介绍了 32 位基于 ARM 微控制器 STM32F101xx 与 STM32F103xx 的固件函数库。
该函数库是一个固件函数包,它由程序、数据结构和宏组成,包括了微控制器所有外设的性能特征。该函
数库还包括每一个外设的驱动描述和应用实例。通过使用本固件函数库,无需深入掌握细节,用户也可以
轻松应用每一个外设。因此,使用本固态函数库可以大大减少用户的程序编写时间,进而降低开发成本。
每个外设驱动都由一组函数组成,这组函数覆盖了该外设所有功能。每个器件的开发都由一个通用 API
(application programming interface 应用编程界面)驱动,API 对该驱动程序的结构,函数和参数名称都进行
了标准化。
所有的驱动源代码都符合“Strict ANSI-C”标准(项目于范例文件符合扩充 ANSI-C 标准)。我们已经把驱动
源代码文档化,他们同时兼容 MISRA-C 2004 标准(根据需要,我们可以提供兼容矩阵)。由于整个固态
函数库按照“Strict ANSI-C”标准编写,它不受不同开发环境的影响。仅对话启动文件取决于开发环境。
该固态函数库通过校验所有库函数的输入值来实现实时错误检测。该动态校验提高了软件的鲁棒性。实时
检测适合于用户应用程序的开发和调试。但这会增加了成本,可以在最终应用程序代码中移去,以优化代
码大小和执行速度。想要了解更多细节,请参阅 Section 2.5。
因为该固件库是通用的,并且包括了所有外设的功能,所以应用程序代码的大小和执行速度可能不是最优
的。对大多数应用程序来说,用户可以直接使用之,对于那些在代码大小和执行速度方面有严格要求的应
用程序,该固件库驱动程序可以作为如何设置外设的一份参考资料,根据实际需求对其进行调整。
此份固件库用户手册的整体架构如下:
定义,文档约定和固态函数库规则。
固态函数库概述(包的内容,库的架构),安装指南,库使用实例。
固件库具体描述:设置架构和每个外设的函数。
STM32F101xx 和 STM32F103xx 在整个文档中被写作 STM32F101x。
目录
2/368
译文英文原版为 UM0427 Oct. 2007 Rev 2, 译文仅供参考,与英文版冲突的,以英文版为准
目录
1.1 缩写 .......................................................................................................................................................... 27
1.2 命名规则 .................................................................................................................................................. 27
1.3 编码规则 .................................................................................................................................................. 28
1.3.1 变量 ................................................................................................................................................ 28
1.3.2 布尔型 ............................................................................................................................................ 28
1.3.3 标志位状态类型 ........................................................................................................................... 29
1.3.4 功能状态类型 ............................................................................................................................... 29
1.3.5 错误状态类型 ............................................................................................................................... 29
1.3.6 外设 ............................................................................................................................................... 29
2.1 压缩包描述 .............................................................................................................................................. 32
2.1.1 文件夹Examples............................................................................................................................ 32
2.1.2 文件夹Library ............................................................................................................................... 32
2.1.3 文件夹Project................................................................................................................................ 33
2.2 固件函数库文件描述 .............................................................................................................................. 33
2.3 外设的初始化和设置 .............................................................................................................................. 34
2.4 位段(Bit-Banding)............................................................................................................................... 35
2.4.1 映射公式 ....................................................................................................................................... 35
2.4.2 应用实例 ....................................................................................................................................... 35
2.5 运行时间检测 .......................................................................................................................................... 36
4.1 ADC寄存器结构 ....................................................................................................................................... 39
4.2 ADC库函数............................................................................................................................................... 40
4.2.1 函数ADC_DeInit........................................................................................................................... 42
4.2.2 函数ADC_Init ............................................................................................................................... 42
4.2.3 函数ADC_StructInit...................................................................................................................... 44
4.2.4 函数ADC_Cmd ............................................................................................................................. 44
4.2.5 函数ADC_DMACmd.................................................................................................................... 45
4.2.6 函数ADC_ITConfig...................................................................................................................... 45
4.2.7 函数ADC_ResetCalibration.......................................................................................................... 46
4.2.8 函数ADC_GetResetCalibrationStatus........................................................................................... 46
4.2.9 函数ADC_StartCalibration ........................................................................................................... 46
4.2.10 函数ADC_GetCalibrationStatus.................................................................................................. 47
4.2.11 函数ADC_SoftwareStartConvCmd............................................................................................. 47
4.2.12 函数ADC_GetSoftwareStartConvStatus..................................................................................... 48
4.2.13 函数ADC_DiscModeChannelCountConfig ................................................................................ 48
4.2.14 函数ADC_DiscModeCmd........................................................................................................... 49
4.2.15 函数ADC_RegularChannelConfig.............................................................................................. 49
4.2.16 函数ADC_ExternalTrigConvConfig........................................................................................... 51
4.2.17 函数ADC_GetConversionValue.................................................................................................. 51
4.2.18 函数ADC_GetDuelModeConversionValue................................................................................. 52
4.2.19 函数ADC_AutoInjectedConvCmd.............................................................................................. 52
4.2.20 函数ADC_InjectedDiscModeCmd.............................................................................................. 53
4.2.21 函数ADC_ExternalTrigInjectedConvConfig .............................................................................. 53
4.2.22 函数ADC_ExternalTrigInjectedConvCmd.................................................................................. 54
4.2.23 函数ADC_SoftwareStartinjectedConvCmd................................................................................ 54
4.2.24 函数ADC_GetsoftwareStartinjectedConvStatus ......................................................................... 55
4.2.25 函数ADC_InjectedChannleConfig.............................................................................................. 55
4.2.26 函数ADC_InjectedSequencerLengthConfig ............................................................................... 56
4.2.27 函数ADC_SetinjectedOffset ....................................................................................................... 56
4.2.28 函数ADC_GetInjectedConversionValue..................................................................................... 57
4.2.29 函数ADC_AnalogWatchdogCmd ............................................................................................... 57
目录
3/368
译文英文原版为 UM0427 Oct. 2007 Rev 2, 译文仅供参考,与英文版冲突的,以英文版为准
4.2.30 函数ADC_AnalogWatchdongThresholdsConfig ........................................................................ 58
4.2.31 函数ADC_AnalogWatchdongSingleChannelConfig................................................................... 58
4.2.32 函数ADC_TampSensorVrefintCmd............................................................................................ 59
4.2.33 函数ADC_GetFlagStatus ............................................................................................................ 59
4.2.34 函数ADC_ClearFlag ................................................................................................................... 60
4.2.35 函数ADC_GetITStatus................................................................................................................ 60
4.2.36 函数ADC_ClearITPendingBit..................................................................................................... 61
5.1 BKP寄存器结构..................................................................................................................................... 62
5.2 BKP库函数 ............................................................................................................................................... 63
5.2.1 函数BKP_DeInit ........................................................................................................................... 64
5.2.2 函数BKP_TamperPinLevelConfig................................................................................................ 64
5.2.3 函数BKP_TamperPinCmd ............................................................................................................ 65
5.2.4 函数BKP_ITConfig....................................................................................................................... 65
5.2.5 函数BKP_RTCOutputConfig........................................................................................................ 66
5.2.6 函数BKP_SetRTCCalibrationValue.............................................................................................. 66
5.2.7 函数BKP_WriteBackupRegister ................................................................................................... 67
5.2.8 函数BKP_ReadBackupRegister.................................................................................................... 67
5.2.9 函数BKP_GetFlagStatus............................................................................................................... 68
5.2.10 函数BKP_ClearFlag.................................................................................................................... 68
5.2.11 函数BKP_GetITStatus................................................................................................................. 69
5.2.12 函数BKP_ClearITPendingBit ..................................................................................................... 69
6.1 CAN寄存器结构....................................................................................................................................... 70
6.2 CAN库函数............................................................................................................................................... 72
6.2.1 函数CAN_DeInit........................................................................................................................... 72
6.2.2 函数CAN_Init ............................................................................................................................... 73
6.2.3 函数CAN_FilterInit....................................................................................................................... 75
6.2.4 函数CAN_StructInit...................................................................................................................... 76
6.2.5 函数CAN_ITConfig...................................................................................................................... 77
6.2.6 函数CAN_Transmit....................................................................................................................... 78
6.2.7 函数CAN_TransmitStatus............................................................................................................. 79
6.2.8 函数CAN_CancelTransmit............................................................................................................ 79
6.2.9 函数CAN_FIFORelease................................................................................................................ 80
6.2.10 函数CAN_MessagePending........................................................................................................ 80
6.2.11 函数CAN_Receive...................................................................................................................... 81
6.2.12 函数CAN_Sleep.......................................................................................................................... 82
6.2.13 函数CAN_WakeUp..................................................................................................................... 82
6.2.14 函数CAN_GetFlagStatus ............................................................................................................ 83
6.2.15 函数CAN_ClearFlag ................................................................................................................... 83
6.2.16 函数CAN_GetITStatus................................................................................................................ 84
6.2.17 函数CAN_ClearITPendingBit..................................................................................................... 85
7.1 DMA寄存器结构...................................................................................................................................... 86
7.2 DMA库函数.............................................................................................................................................. 88
7.2.1 函数DMA_DeInit.......................................................................................................................... 89
7.2.2 函数DMA_Init .............................................................................................................................. 89
7.2.3 函数DMA_StructInit..................................................................................................................... 92
7.2.4 函数DMA_Cmd ............................................................................................................................ 92
7.2.5 函数DMA_ITConfig ..................................................................................................................... 93
7.2.6 函数DMA_GetCurrDataCounte.................................................................................................... 93
7.2.7 函数DMA_GetFlagStatus ............................................................................................................. 94
7.2.8 函数DMA_ClearFlag .................................................................................................................... 95
7.2.9 函数DMA_GetITStatus................................................................................................................. 95
7.2.10 函数DMA_ClearITPendingBit.................................................................................................... 96
8.1 EXTI寄存器结构 ...................................................................................................................................... 97
目录
4/368
译文英文原版为 UM0427 Oct. 2007 Rev 2, 译文仅供参考,与英文版冲突的,以英文版为准
8.2 EXTI库函数 .............................................................................................................................................. 98
8.2.1 函数EXTI_DeInit.......................................................................................................................... 98
8.2.2 函数EXTI_Init............................................................................................................................... 99
8.2.3 函数EXTI_StructInit................................................................................................................... 100
8.2.4 函数EXTI_GenerateSWInterrupt................................................................................................ 101
8.2.5 函数EXTI_GetFlagStatus............................................................................................................ 101
8.2.6 函数EXTI_ClearFlag .................................................................................................................. 102
8.2.7 函数EXTI_GetITStatus............................................................................................................... 102
8.2.8 函数EXTI_ClearITPendingBit.................................................................................................... 103
9.1 FLASH寄存器结构................................................................................................................................. 104
9.2 FLASH库函数......................................................................................................................................... 105
9.2.1 函数FLASH_SetLatency............................................................................................................. 106
9.2.2 函数FLASH_HalfCycleAccessCmd ........................................................................................... 107
9.2.3 函数FLASH_PrefetchBufferCmd ............................................................................................... 107
9.2.4 函数FLASH_Unlock................................................................................................................... 108
9.2.5 函数FLASH_Lock ...................................................................................................................... 108
9.2.6 函数FLASH_ErasePage.............................................................................................................. 109
9.2.7 函数FLASH_EraseAllPages ....................................................................................................... 109
9.2.8 函数FLASH_EraseOptionBytes...................................................................................................110
9.2.9 函数FLASH_ProgramWord .........................................................................................................110
9.2.10 函数FLASH_ProgramHalfWord ................................................................................................111
9.2.11 函数FLASH_ProgramOptionByteData......................................................................................111
9.2.12 函数FLASH_EnableWriteProtection .........................................................................................112
9.2.13 函数FLASH_ReadOutProtection ...............................................................................................113
9.2.14 函数FLASH_UserOptionByteConfig.........................................................................................114
9.2.15 函数FLASH_GetUserOptionByte..............................................................................................115
9.2.16 函数FLASH_GetWriteProtectionOptionByte............................................................................115
9.2.17 函数FLASH_GetReadOutProtectionStatus................................................................................116
9.2.18 函数FLASH_GetPrefetchBufferStatus.......................................................................................116
9.2.19 函数FLASH_ITConfig...............................................................................................................117
9.2.20 函数FLASH_GetFlagStatus .......................................................................................................117
9.2.21 函数FLASH_ClearFlag..............................................................................................................118
9.2.22 函数FLASH_GetStatus ..............................................................................................................119
9.2.23 函数FLASH_WaitForLastOperation..........................................................................................119
10.1 GPIO寄存器结构.................................................................................................................................. 120
10.2 GPIO库函数.......................................................................................................................................... 122
10.2.1 函数GPIO_DeInit...................................................................................................................... 123
10.2.2 函数GPIO_AFIODeInit............................................................................................................. 123
10.2.3 函数GPIO_Init .......................................................................................................................... 124
10.2.4 函数GPIO_StructInit................................................................................................................. 126
10.2.5 函数GPIO_ReadInputDataBit................................................................................................... 126
10.2.6 函数GPIO_ReadInputData........................................................................................................ 127
10.2.7 函数GPIO_ReadOutputDataBit ................................................................................................ 127
10.2.8 函数GPIO_ReadOutputData ..................................................................................................... 128
10.2.9 函数GPIO_SetBits .................................................................................................................... 128
10.2.10 函数GPIO_ResetBits............................................................................................................... 129
10.2.11 函数GPIO_WriteBit ................................................................................................................ 129
10.2.12 函数GPIO_Write ..................................................................................................................... 130
10.2.13 函数GPIO_PinLockConfig ..................................................................................................... 130
10.2.14 函数GPIO_EventOutputConfig............................................................................................... 131
10.2.15 函数GPIO_EventOutputCmd.................................................................................................. 131
10.2.16 函数GPIO_PinRemapConfig .................................................................................................. 132
10.2.17 函数GPIO_EXTILineConfig................................................................................................... 133
目录
5/368
译文英文原版为 UM0427 Oct. 2007 Rev 2, 译文仅供参考,与英文版冲突的,以英文版为准
11.1 I2C寄存器结构 ..................................................................................................................................... 134
11.2 I2C库函数 ............................................................................................................................................. 135
11.2.1 函数I2C_DeInit ......................................................................................................................... 136
11.2.2 函数I2C_ Init............................................................................................................................. 137
11.2.3 函数I2C_ StructInit ................................................................................................................... 138
11.2.4 函数I2C_ Cmd........................................................................................................................... 139
11.2.5 函数I2C_ DMACmd ................................................................................................................. 139
11.2.6 函数I2C_ DMALastTransferCmd ............................................................................................. 140
11.2.7 函数I2C_ GenerateSTART ........................................................................................................ 140
11.2.8 函数I2C_ GenerateSTOP .......................................................................................................... 141
11.2.9 函数I2C_ AcknowledgeConfig.................................................................................................. 141
11.2.10 函数I2C_ OwnAddress2Config............................................................................................... 142
11.2.11 函数I2C_ DualAddressCmd.................................................................................................... 142
11.2.12 函数I2C_ GeneralCallCmd...................................................................................................... 143
11.2.13 函数I2C_ ITConfig.................................................................................................................. 143
11.2.14 函数I2C_ SendData................................................................................................................. 144
11.2.15 函数I2C_ ReceiveData............................................................................................................ 144
11.2.16 函数I2C_ Send7bitAddress ..................................................................................................... 145
11.2.17 函数I2C_ ReadRegister........................................................................................................... 145
11.2.18 函数I2C_ SoftwareResetCmd.................................................................................................. 146
11.2.19 函数I2C_ SMBusAlertConfig ................................................................................................. 147
11.2.20 函数I2C_ TransmitPEC........................................................................................................... 147
11.2.21 函数I2C_ PECPositionConfig................................................................................................. 148
11.2.22 函数I2C_ CalculatePEC.......................................................................................................... 148
11.2.23 函数I2C_ GetPEC ................................................................................................................... 149
11.2.24 函数I2C_ ARPCmd ................................................................................................................. 149
11.2.25 函数I2C_ StretchClockCmd.................................................................................................... 150
11.2.26 函数I2C_ FastModeDutyCycleConfig .................................................................................... 150
11.2.27 函数I2C_ GetLastEvent........................................................................................................... 151
11.2.29 函数I2C_ GetFlagStatus.......................................................................................................... 152
11.2.30 函数I2C_ ClearFlag................................................................................................................. 153
11.2.31 函数I2C_ GetITStatus ............................................................................................................. 154
11.2.32 函数I2C_ ClearITPendingBit .................................................................................................. 155
12.1 IWDG寄存器结构 ................................................................................................................................156
12.2 IWDG库函数 ........................................................................................................................................ 157
12.2.1 函数IWDG_WriteAccessCmd................................................................................................... 157
12.2.2 函数IWDG_SetPrescaler........................................................................................................... 158
12.2.3 函数IWDG_SetReload.............................................................................................................. 158
12.2.4 函数IWDG_ReloadCounter ...................................................................................................... 159
12.2.5 函数IWDG_Enable ................................................................................................................... 159
12.2.6 函数IWDG_GetFlagStatus........................................................................................................ 159
13.1 NVIC寄存器结构.................................................................................................................................. 161
13.2 NVIC库函数 ......................................................................................................................................... 163
13.2.1 函数NVIC_DeInit ..................................................................................................................... 164
13.2.2 函数NVIC_SCBDeInit.............................................................................................................. 164
13.2.3 函数NVIC_PriorityGroupConfig.............................................................................................. 165
13.2.4 函数NVIC_Init.......................................................................................................................... 166
13.2.5 函数NVIC_StructInit................................................................................................................. 168
13.2.6 函数NVIC_SETPRIMASK....................................................................................................... 169
13.2.7 函数NVIC_RESETPRIMASK.................................................................................................. 169
13.2.8 函数NVIC_SETFAULTMASK................................................................................................. 170
13.2.9 函数NVIC_RESETFAULTMASK............................................................................................ 170
13.2.10 函数NVIC_BASEPRICONFIG .............................................................................................. 171