stm32 port multiplexing and remapping

Port multiplexing and remapping are related to the I/O port of the microcontroller. Port multiplexing is to assign multiple functions to one I/O, and switch different functions by setting the working mode of the I/O. Remapping is to map the functions on some I/O ports to other I/O ports. But pay attention to one thing: the remapped I/Os are set by the factory and cannot be changed by yourself.

Port multiplexing

Peripherals

Before talking about port multiplexing, let's understand a concept: what is a peripheral? What are built-in peripherals?

Peripherals are referred to as "peripherals", which refer to hardware devices that are connected to other than the computer host. It plays the role of transmission, transfer and storage of data and information, and is an important part of the computer system.
-Baidu Encyclopedia

Built-in peripherals are peripherals integrated inside the microcontroller, and there are corresponding registers. Reference: What are the internal peripherals and peripherals in the microcontroller

Port multiplexing

STM32 has many built-in peripherals, and the external pins of these peripherals are multiplexed with GPIO. That is to say, if a GPIO can be multiplexed as a function pin of a built-in peripheral, then when the GPIO is used as a built-in peripheral, it is called multiplexing.

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

Port multiplexing setup steps

  1. GPIO clock enable;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  2. Multiplexing peripheral clock enable For
    example , if you want to multiplex ports PA9 and PA10 as serial ports, you need to enable serial port clocks.
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  3. Port mode configuration. GPIO_Init() function.

Specifically, the third step is to configure the port mode. When using I/O, it must be initialized first.

void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)

The first parameter is which group of I/O ports, and the second group of I/O is a structure pointer

typedef struct
{
  uint16_t GPIO_Pin;             /*!< Specifies the GPIO pins to be configured.
                                      This parameter can be any value of @ref GPIO_pins_define */

  GPIOSpeed_TypeDef GPIO_Speed;  /*!< Specifies the speed for the selected pins.
                                      This parameter can be a value of @ref GPIOSpeed_TypeDef */

  GPIOMode_TypeDef GPIO_Mode;    /*!< Specifies the operating mode for the selected pins.
                                      This parameter can be a value of @ref GPIOMode_TypeDef */
}GPIO_InitTypeDef;

Mainly look at the value of the GPIO_Mode parameter

typedef enum
{ GPIO_Mode_AIN = 0x0,      //模拟输入
  GPIO_Mode_IN_FLOATING = 0x04,//浮空输入
  GPIO_Mode_IPD = 0x28,//下拉输入 
  GPIO_Mode_IPU = 0x48,//上拉输入
  GPIO_Mode_Out_OD = 0x14,//开漏输出
  GPIO_Mode_Out_PP = 0x10,//推挽输出
  GPIO_Mode_AF_OD = 0x1C,//复用开漏输出
  GPIO_Mode_AF_PP = 0x18//复用推挽输出
}GPIOMode_TypeDef;

write picture description here
It can be seen in the data sheet that PA9 and PA10 can be multiplexed as a serial port (Default)
write picture description here
. As can be seen from the above two figures, multiplexing as a serial port transmission requires setting PA9 as a push-pull multiplexing output.

Code example:

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//GPIO时钟使能

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//外设时钟(串口)使能

//初始化IO为对应的模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9//复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;  //PA10 PA.10 浮空输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);  

Summarize

The GPIO multiplexing function is that the same several I/O ports can implement different functions by setting the I/O mode and enabling the corresponding clock. For specific settings, please refer to the stm32 Chinese reference manual and the dataSheet of the corresponding chip.

Remap

Each built-in peripheral has several input and output pins. Generally, the output ports of these pins are fixed. In order to allow design engineers to better arrange the direction and function of the pins, an external Set the concept of pin remapping, that is, in addition to the default port, the pin of a peripheral can be mapped to other ports by setting the remapping register.

Requirement background of remapping technology:

  • I/O multiplexing: GPIO and built-in peripherals share pinouts
  • Remapping of I/O: Alternate function (AFIO) is derived from different GPIO pins
  • It facilitates PCB design and reduces signal cross-interference
  • Time-division multiplexing some peripherals, virtually increasing the number of ports
  • In order to optimize the number of peripheral IO functions in different device packages, some multiplexed functions can be remapped to some other pins. The input and output pins of many built-in peripherals in STM32 have the function of remap.

Take serial port 1 as an example. In the
write picture description here
above figure , the I/O corresponding to Remap is the I/O that can be remapped to, and Default is the default reusable function of the I/O.
It can be seen from the above figure that serial port 1 can be remapped to the PB6 and PB7 pins, that is to say, if the PA9 and PA10 pins are not available, or have been occupied; then PB6 and PB7 can be used to implement serial port 1 function.

Operation steps of AFIO remapping:

  1. Enable times to remap the I/O port clock to
  2. Enable peripheral clocks that are remapped
  3. AFIO enabled clock (don't forget)
  4. remap

Pay attention to the third step, enable the AFIO function clock, why do you need to enable this clock & when you need to enable this clock, please refer to the answer below

Well, here comes the question!
  What is AFIO? When does the AFIO clock need to be turned on?
  
  We found it from the "STM32 Chinese Reference Manual _V10": Before reading and writing the registers AFIO_EVCR, AFIO_MAPR and AFIO_EXTICRX, you should first turn on the AFIO clock (set the APB2 peripheral clock enable register RCC_APB2ENR).
  That is to say: when you need to configure the AFIO registers, you need to turn on the AFIO clock by setting the AFIO bit of the RCC_APB2ENR register to '1'.
  
  The registers related to AFIO are:
  1. Event Control Register (AFIO_EVCR)
  2. Multiplexing Remapping and Debug I/O Configuration Register (AFIO_MAPR)
  3. External Interrupt Configuration Register 1 (AFIO_EXTICR1)
  4. External Interrupt Configuration Register 2 (AFIO_EXTICR2 )
  5. External interrupt configuration register 3 (AFIO_EXTICR3)
  6. External interrupt configuration register 4 (AFIO_EXTICR4)
  Looking at the definitions of these registers, we understand that these registers are used for "event control", "remapping", "debug IO" Configuration", "External Interrupt". For example, AFIO_EXTICRX is used to select the input source of EXTIx external interrupt.
  Summary: When we need to configure these AFIO registers, we need to open the AFIO clock of the RCC_APB2ENR register, not when the pin multiplexing function is used.
  ——From : When does the AFIO clock of STM32 need to be turned on

Partial Remap & Full Remap

  • Partial remapping: Some of the pins of the functional peripheral are remapped, and some of the pins are the original default pins.
  • Full remapping: All pins of functional peripherals are remapped.
    write picture description here
    How to configure partial remapping & full remapping in the back

Pin remapping configuration process (serial port 3 as an example):
1. Enable the GPIO clock (IO after remap);
2. Enable the functional peripheral clock (eg serial port 3);
3. Enable the AFIO clock. The AFIO clock must be enabled for remapping:
4. Turn on remapping.

  GPIO_PinRemapConfig(GPIO_FullRemap_USART3, ENABLE);
  //根据第一个参数,来确定是部分重映射还是全部重映射

write picture description here
Check the value of the first parameter, as shown in the figure below:
write picture description here
Take serial port 3 as an example, if you want to partially map: the value of the first parameter of GPIO_PinRemapConfig() should be: the first parameter in the red box in the above figure, namely GPIO_PartialRemap_USART3 , If it is fully mapped: GPIO_FullRemap_USART3

Summarize

Remapping is to map a function implemented in a certain I/O to another I/O port, mainly for the convenience of wiring and signal interference. The specific configuration steps and partial remapping & full remapping have been described above.

References

Punctuality Atomic Video
STM32 GPIO 8 working modes
When does the AFIO clock of STM32 need to be turned on? What
are the internal peripherals and peripherals in the microcontroller?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325580060&siteId=291194637