STM32 port multiplexing & remapping (USART Remap)

Let me tell you about the port remapping of the STM32 microcontroller, because I take myself as an example. Here is the remapping of USART1 as an example..

        Because I want a main control board with a TFT_LCD screen, considering the FSMC, I chose the CPU of the STM32F103VCT6 model, and accidentally connected the serial port to the USART1. Because I found a mistake when I adjusted the program, there is no way, only through the port Remapping is the solution. But I haven't used port remapping before, I only heard its name and didn't use it, so... hehe... I can only look at it from the beginning.

         There are many I/O ports on STM32, and there are also many built-in peripherals such as I2C, ADC, ISP, USART, etc. In order to save pins, these built-in peripherals basically share pins with I/O ports, that is, I Alternate function of /O pin. But another special feature of STM32 is that many I/O pins of multiplexed built-in peripherals can be led out from different I/O pins through remapping function, that is, the pins of multiplexed functions can be changed by program After reading this, I believe that everyone should understand some concepts of port remapping. I will not go into details on the principle. You can read the manual or search online. There are still a lot of information in this regard. Let me talk about my debugging. experience.

        I don't know why the serial port was connected to the USART1 during PCB drawing, but I didn't care at that time. When I wrote the USART test program and burned it into the hardware emulation, the serial port gave garbled characters. I felt strange at that time. Check the program. After several times, I couldn't find the problem. I thought it was a hardware problem, but I suddenly thought that STM has a multiplexing function. I wondered if there is a ghost here? So I found the datasheet and saw the truth.

 

The intersection of the three red boxes. 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..

   As we all know, each functional module of STM32 MCU has its own clock system, so when you want to call the functional module of STM32 MCU, you must configure the corresponding clock first, and then you can operate the corresponding functional module. The same is true for port remapping. As shown in the figure Show:

 

The remapping steps are:

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 the remapped pins, here you only need to configure the remapped I/Os, 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);

      Just need these three steps, the serial port can be used normally, is it simple? But I struggled for more than half an hour to get it done. Many things are easier said than done. I hope I can make more progress in the future. It is moving in this direction.



Finally, to sum up:

Simply put, STM32's io has 3 functions, one is the default one is multiplexing, and the other is the remapping function (this is actually multiplexing).

If it is configured for multiplexing, the first one will be used. If the 2 functions are configured for multiplexing and the corresponding remapping is configured, the third function will be used.

Usually, there are more than two multiplexing + remapping of one port. At this time, it depends on which device you enable (which one is enabled). Use whichever one can) 

open multiplexing + enable device + whether to remap or not, you can decide which function this io port uses

Guess you like

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