Self study note 3 of STM32 incomplete manual

Self study note 3 of STM32 incomplete manual

MDK firmware library quick organization code

Example: initializing a GPIO port
Define the GPIO initialization function as:

void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);

The entry parameter of the function is GPIO_TypeDef type pointer and GPIO_InitTypeDef type pointer, go to definition of 'GPIO_Init’

typedef struct
{ uint16_t GPIO_Pin;
GPIOSpeed_TypeDef GPIO_Speed;
GPIOMode_TypeDef GPIO_Mode; GPIOMode_TypeDef */

We first define a structure variable:
GPIO_InitTypeDef GPIO_InitStructure;
Next, we will initialize the structure variable GPIO_InitStructure. First, we need to initialize the member variable GPIO_Pin. In GPIO_ Assert in init function_ The first line of param function is to judge the validity of the first parameter GPIOx. Double click "is"_ GPIO_ ALL_ Right click "go to definition of..." The value of available GPIOx is only allowed to be between gpioa and gpiog. In the same way, you can view GPIO_Mode and GPIO_ Parameter limit for speed.

After understanding, you can organize the following codes:

GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Push pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);

tips: we can initialize multiple IO ports at the same time through| (or). Before doing so
It is mentioned that their Mode and Speed parameters are the same, because Mode and Speed parameters cannot define multiple parameters at one time. therefore
The ways to initialize multiple IO ports are as follows:

GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5| GPIO_Pin_6| GPIO_Pin_7; //Specify port
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Port mode: push pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //speed
GPIO_Init(GPIOB, &GPIO_InitStructure); //initialization

Problem: every time you enable the clock, you have to check the clock tree to see if the peripherals are mounted under the bus. That's a problem.
Solution: in the stm32f10x h file, we can see the following macro definitions:

#define RCC_APB2Periph_GPIOA ((uint32_t)0x00000004)
#define RCC_APB2Periph_GPIOB ((uint32_t)0x00000008)
#define RCC_APB2Periph_GPIOC ((uint32_t)0x00000010)
#define RCC_APB1Periph_TIM2 ((uint32_t)0x00000001)
#define RCC_APB1Periph_TIM3 ((uint32_t)0x00000002)
#define RCC_APB1Periph_TIM4 ((uint32_t)0x00000004)
#define RCC_AHBPeriph_DMA1 ((uint32_t)0x00000001)
#define RCC_AHBPeriph_DMA2 ((uint32_t)0x00000002)

It can be concluded that GPIOA~GPIOC is mounted under APB2, TIM2~TIM4 is mounted under APB1, and DMA is mounted under AHB. Enable DMA to call RCC_AHBPeriphClock() function, when GPIO is enabled, RCC is called_ Apb2periphresetcmd() function.

Posted on Sat, 20 Jun 2020 23:46:30 -0400 by PureDrive