【STM32】AFIO and remapping


When configuring the external interrupt, when the GPIO clock is turned on, the AFIO clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO,ENABLE) is also turned on ;

AFI

simply put

MCU has external pins, including CPU pins and built-in peripherals (PWM, TIM, ADC...);
they all require external interface IO, but the total number of pins is limited, and some pins are used as ordinary IO is also used as peripheral IO. Sometimes even several built-in peripherals share one IO. This is the phenomenon of pin multiplexing.

For example, the schematic diagram of any pin:
PA2/USART2_TX/ADC123_IN2/TIM5_CH3/TIM2_CH3
indicates that in addition to being an ordinary PA2, this pin also serves as a multiplexed IO, including USART2, ADC, TIM5, TIM2, etc...

Summarize:

  1. Ordinary pins are GPIO, and multiplexed pins (non-ordinary pins) are AFIO;
  2. As long as the pins of built-in peripherals are used, multiplexed IO (AFIO) needs to be turned on, such as outputting PWM waveforms externally, using AD conversion, etc.

It does not mean that you must start the RCC_APB2Periph_AFIO Clock when using the IO multiplexing function. Only when using the AFIO event control register , the AFIO remapping function and the external interrupt (EXTI) control register, you need to turn on the AFIO clock.

The simple IO port multiplexing function does not require turning on the AFIO clock, remap does.

example

1.External interruption

GPIO_InitTypeDef GPIO_InitStructure;//
EXTI_InitTypeDef EXTI_InitStructure;//

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);//打开GPIO的时钟,打开AFIO的时钟,

AFIO needs to be turned on when using external interrupts.

2. Pin remapping

STM32 has many built-in peripherals I2C, ADC, ISP, USART, etc. In order to save pins, these built-in peripherals basically share pins with the I/O port, which is the multiplexing function of the I/O pin. .

But there is another special thing about STM32: many I/O pins that reuse built-in peripherals can be derived from different I/O pins through the remapping function, that is, the pins with multiplexed functions can be changed through the program. of.

The TX and RX of the USART2 peripheral used in the program correspond to PA2 and PA3 respectively, but the PA2 and PA3 pins on the board are connected to other devices, but in order to use USART2, "RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO" turns on GPIOD
remapping The function maps the TX and RX of the USART2 device to PD5 and PD6. We can use the USART2 serial port communication by connecting the MAX232 serial port chip to these two pins.

Insert image description here

Insert image description here

Refer to the STM32f10X manual

remapping steps

STM32F103VCT6 The USART1 of this CPU is connected to PB6/PB7, but the default function after power-on initialization is not USART1. So if you want to use the serial port function, you must use port remapping...

1. Turn on the remapping clock and the I/O port pin clock after USART remapping

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO,ENABLE);

2. I/O port remapping is enabled

 GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);

3. Configure remapping pins. Here you only need to configure the remapped I/O, and the original ones do not need to be configured.

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);

Simply put, STM32's io has 3 functions, one is the default, one is multiplexing, and the other is remapping function (this is actually multiplexing). If it is configured to enable the function after multiplexing, the second function will be used. If it is configured for multiplexing and the corresponding remapping is configured, the third function will be used, usually one port.

おすすめ

転載: blog.csdn.net/apythonlearner/article/details/131923892