Sei sulla pagina 1di 527
UM0427 User manual ARM®-based 32-bit MCU STM32F101xx and STM32F103xx firmware library Introduction This document

UM0427

User manual

ARM®-based 32-bit MCU STM32F101xx and STM32F103xx firmware library

Introduction

This document describes the ARM ® -based 32-bit MCU STM32F101xx and STM32F103xx firmware library.

This library is a firmware package which contains a collection of routines, data structures and macros covering the features of all peripherals. It includes a description of the device drivers plus a set of examples for each peripheral. The firmware library allows any device to be used in the user application without the need for in-depth study of each peripheral specifications. As a result, using the firmware library saves significant time that would otherwise be spent in coding, while reducing the application development and integration cost.

Each device driver consists of a set of functions covering all peripheral functionalities. The development of each driver is driven by a common API (application programming interface) which standardizes the driver structure, the functions and the names of parameters.

The driver source code is developed in ‘Strict ANSI-C’ (relaxed ANSI-C for projects and examples files). It is fully documented and is MISRA-C 2004 compliant (the compliancy matrix is available upon request). Writing the whole library in ‘Strict ANSI-C’ makes it independent from the software toolchain. Only the start-up files depend on the toolchain.

The firmware library implements run-time failure detection by checking the input values for all library functions. This dynamic checking contributes to enhance the robustness of the software. Run-time detection is suitable for user application development and debugging. It adds an overhead and can be removed from the final application code to minimize code size and execution speed. For more details refer to Section 2.5: Run-time checking on page 48.

Since the firmware library is generic and covers all peripherals functionalities, the size and/or execution speed of the application code may not be optimized. For many applications, the library may be used as is. However, for applications having tough constraints in terms of code size and/or execution speed, the library drivers should be used as a reference on how to configure the peripheral and tailor them to specific application requirements.

The firmware library user manual is structured as follows:

Definitions, document conventions and firmware library rules

Overview of the firmware library (package content, library structure), installation guidelines, and example on how to use the library.

Detailed description the firmware library: configuration structure and software functions for each peripheral.

STM32F101xx and STM32F103xx will be referred to as STM32F10xxx throughout the document.

September 2008

Rev 6

1/527

Contents

UM0427

Contents

1 Document and library rules

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

36

 

1.1 Acronyms

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

36

1.2 Naming conventions

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

37

1.3 Coding rules

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

38

1.3.1 Variables

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

38

1.3.2 Boolean type

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

38

1.3.3 FlagStatus type

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

. 39

1.3.4 FunctionalState type

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

39

1.3.5 ErrorStatus type

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

39

1.3.6 Peripherals

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

39

2 Firmware library

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

42

 

2.1 Package description

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

42

2.1.1 Examples folder

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

43

2.1.2 Library folder

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

43

2.1.3 Project folder

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

43

2.2 Description of firmware library files

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

44

2.3 Peripheral initialization and configuration

 

46

2.4 Bit-Banding

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

47

2.4.1 Mapping formula

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

47

2.4.2 Example of implementation

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

47

2.5 Run-time checking

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

48

3 Peripheral firmware overview

 

51

4 Analog/digital converter (ADC)

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

52

 

4.1 ADC register structure

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

52

4.2 ADC library functions

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

55

4.2.1 ADC_DeInit function

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

56

4.2.2 ADC_Init function

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

57

ADC_InitTypeDef structure

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

57

4.2.3 ADC_StructInit function

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

60

4.2.4 ADC_Cmd function

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

. 61

2/527

2/527

UM0427

Contents

 

4.2.5 ADC_DMACmd function

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

61

4.2.6 ADC_ITConfig function

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

62

4.2.7 ADC_ResetCalibration function

 

63

4.2.8 ADC_GetResetCalibrationStatus function

 

63

4.2.9 ADC_StartCalibration function

 

64

4.2.10 ADC_GetCalibrationStatus function

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

64

4.2.11 ADC_SoftwareStartConvCmd function

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

65

4.2.12 ADC_GetSoftwareStartConvStatus function

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

65

4.2.13 ADC_DiscModeChannelCountConfig function

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

66

4.2.14 ADC_DiscModeCmd function

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

66

4.2.15 ADC_RegularChannelConfig function

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

. 67

4.2.16 ADC_ExternalTrigConvCmd function

 

69

4.2.17 ADC_GetConversionValue function

 

69

4.2.18 ADC_GetDualModeConversionValue function

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

. 70

4.2.19 ADC_AutoInjectedConvCmd function

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

70

4.2.20 ADC_InjectedDiscModeCmd function

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

. 71

4.2.21 ADC_ExternalTrigInjectedConvConfig function

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

71

4.2.22 ADC_ExternalTrigInjectedConvCmd function

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

73

4.2.23 ADC_SoftwareStartInjectedConvCmd function

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

73

4.2.24 ADC_GetSoftwareStartInjectedConvStatus function

 

74

4.2.25 ADC_InjectedChannelConfig function

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

75

4.2.26 ADC_InjectedSequencerLengthConfig function

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

76

4.2.27 ADC_SetInjectedOffset function

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

76

4.2.28 ADC_GetInjectedConversionValue function

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

77

4.2.29 ADC_AnalogWatchdogCmd function

 

78

4.2.30 ADC_AnalogWatchdogThresholdsConfig function

 

.

.

.

.

.

.

.

.

.

.

.

.

.

79

4.2.31 ADC_AnalogWatchdogSingleChannelConfig function

 

79

4.2.32 ADC_TempSensorVrefintCmd function

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

80

4.2.33 ADC_GetFlagStatus function

 

80

4.2.34 ADC_ClearFlag function

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

81

4.2.35 ADC_GetITStatus function

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

82

4.2.36 ADC_ClearITPendingBit function

 

82

5

Backup registers (BKP)

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

83

5.1 BKP register structure

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

83

5.2 Firmware library functions

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

86

 

5.2.1

BKP_DeInit function

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

. 86

. . . . . . . . . . . . . . . .

3/527

Contents

UM0427

 

5.2.2 BKP_TamperPinLevelConfig function

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

87

5.2.3 BKP_TamperPinCmd function

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

87

5.2.4 BKP_ITConfig function

 

.

.

.