Port multiplexing

The STM32 has a number of built-in peripherals whose external pins are multiplexed with GPIO. That is, if a GPIO can reuse the functional pins of a built-in peripheral, then when the GPIO is used as a built-in peripheral, it is called multiplexing. (Built-in peripheral is integrated in the MCU internal peripheral, there is a corresponding register.)

For example, the sending and receiving pins of serial port 1 are PA9 and PA10. When PA9 and PA10 are not used as GPIO, but as the sending and receiving pins of serial port 1 with multiplexing function, it is called port multiplexing.

Configure port overcommitment

For example, PA9 and PA10 are configured as serial port 1

  1. The clock on the GPIO port was enabled. Procedure
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
Copy the code
  1. The multiplexing peripheral clock function was enabled
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // Enable the serial port clockCopy the code
  1. Port mode configuration:GPIO_Init () function(look up table)

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);// Enable the GPIO clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);// Enable the serial port clock
	
// Initialize I/0 to the corresponding mode
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;// Reuse push-pull output
GPIO_Init(GPIOA,&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;// Float null input
GPIO_Init(GPIOA,&GPIO_InitStructure);
Copy the code

Port remapping

Each built-in peripherals has several input and output pins, the general output port of the pin are fixed, to make the design engineer can better arrange trend and function of the pins in the STM32 introduced the concept of heavy peripherals pin mapping, that is, a pin of peripheral besides has the default port, you can also register by setting the weight map, Map the pins of this peripheral to other ports.

Take serial port 1 as an example.The default send and receive pins of serial port 1 are PA9 and PA10

Partial remap & complete remap

  • Partial remapping: partial pins of functional peripherals are remapped, and some pins are the original default pins;
  • Full remap: All pins of a functional peripheral are remapped.

Pin remapping Configuration (for example, serial port 1)

  1. Enabling GPIO clock (IO after remapping)
  2. Enable peripheral clock (for example, serial port 1)
  3. If THE AFIO clock is enabled, the AFIO clock must be enabled for remapping.
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
Copy the code
  1. Turn on remapping (partial or total remapping, based on the first parameter)
void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState);
Copy the code

Situations in which the AFIO auxiliary clock is enabled

When the AFIO_MAPR AFIO_EXTICRX AFIO_EVCR register is read and write

  • AFIO_MAPR: remapping the multiplexing function is configured
  • AFIO_EXTICRX: Configure external disconnection mapping
  • AFIO_EVCR: Configure the EVENTOUT event output

The resources

  1. Stm32 Port overcommitment and remapping
  2. Teach you to learn STM32 microcontroller teaching video