Port multiplexing - based on STM32F767IGT6

basic concept:

There are many built-in peripherals in STM32. The operations of these built-in peripherals are also through 140 GPIO pins. If a GPIO pin is multiplexed as a built-in peripheral function, then it is called GPIO function multiplexing:

For example: the transmit and receive pins of serial port 1 can be multiplexed with PA9 and PA10. When we do not use these two pins as GPIO, but multiplex them as the transceiver pins of serial port 1, it is called the port of the port. Du use;

Port multiplexing and mapping principle of STM32 (M4 core and above)

IO pins are connected to peripherals or modules through a multiplexer, which allows only the alternate function (AF) of one peripheral to be connected to the IO pin at a time, ensuring that there is no occurrence between peripherals sharing an IO pin Conflict; (One IO pin can only multiplex one peripheral function at a time, so can multiple IOs multiplex one function at the same time?)
write picture description here
write picture description here
We can see from the multiplexing relationship table that one IO may often multiplex many functions. The functional peripherals that may be reused by IO are all turned on. Will it cause interference? For STM32F1, it may cause confusion, while F7 is only connected to specific peripherals, and there is no mutual interference;
one function can be multiplexed by multiple IOs, and when one IO multiplexes a function, others can be reused The IO of this function can also be used as GPIO or multiplexed with other functions;

Function multiplexing related registers (AFRL and AFRH)

The pin multiplexing function selection is controlled by AFRL and AFRH, where AFRL controls the eight IO ports of 0~7, and AFRH controls the eight IO ports of 8~15. In MDK, AFRL and AFRH are defined as arrays AFR[ 2] Configure, where AFR[0] represents AFRL, AFR[1] represents AFRH, and AFR is an array of unsigned int type, that is, AFRL/AFRH register 4bit represents one IO, and one IO may multiplex up to 16 peripheral functions ;

Example 1: Multiplexing PA9 and PA10 into USART1_TX and USART1_RX functions

RCC->AHB1ENR|=1<<0;         //使能PORTA口时钟  
RCC->APB2ENR|=1<<4;         //使能串口1时钟
GPIO_Set(GPIOA,PIN9|PIN10,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_50M,GPIO_PUPD_PU);              //PA9,PA10,复用功能,上拉输出
GPIO_AF_Set(GPIOA,9,7);     //PA9,AF7
GPIO_AF_Set(GPIOA,10,7);    //PA10,AF7 
//GPIOx:GPIOA~GPIOI.       端口
//BITx:0~15                代表IO引脚编号.
//AFx:0~15                 代表AF0~AF15.功能编号
void GPIO_AF_Set(GPIO_TypeDef* GPIOx,u8 BITx,u8 AFx)
{  
    GPIOx->AFR[BITx>>3]&=~(0X0F<<((BITx&0X07)*4));
    GPIOx->AFR[BITx>>3]|=(u32)AFx<<((BITx&0X07)*4);
}
/**************************************************************
 *1、BITx>>3  在前面我们说了,AFRL和AFRH分别控制8个IO口,这样AFR[2]
 *            刚好可以表示一个端口,二进制3bit可以表示八种状态,即如
 *            果超过7则bit4肯定为1,这样就可以用一个函数直接判断使用
 *            AFR[0]还是AFR[1]了;
 * 2、(BITx&0X07)*4  一个IO用4个bit表示,故乘4;BITx&0X07 是为了防
 *                   止误操作,一个AFR只能设置8个IO,故3个bit就可以
 *                   表示,超过3bit的部分自动清零;
 **************************************************************
 * 总结:
 *     1、想要使用某个外设,先使能他的时钟(对于复用来说,要使能端口+
 * 功能外设);
 *     2、时钟使能完毕后,要对其进行初始化操作,进行相关配置;
 *     3、注意:设置寄存器,先清零,再设置;
 **************************************************************/

Summary of commonly used multiplexed function pins:

AF0:MCO/SWD/SWCLK/RTC;
AF1:TIM1/TIM2;
AF2:TIM3~5;
AF3:TIM8~11;
AF4:I2C1~I2C4;
AF5:SPI1~SPI6;
AF6:SPI3/SAI1;
AF7:SPI2/3/USART1~3/UART5/SPDIFRX;
AF8:USART4~8/SPDIFRX/SAI2;
AF9:CAN1~2/TIM12~14/LCD/QSPI;
AF10:USB_OTG/USB_HS/SAI2/QSPI;
AF11:ETH;
AF12:FMC/SDMMC/OTG/HS;
AF13:DCIM;
AF14:LCD;
AF15:EVENTOUT;

AF0 is a system function, and there are three kinds of special attention:
1. JTAG/SWD: After the device is reset, these pins will be designated as dedicated pins, which can be used by the on-chip debug module (not controlled by the GPIO controller);
2. RTC_REFIN: This pin should be configured as input floating mode;
3. MCO1/MCO2: These pins must be configured as alternate function mode;

For ADC and DAC, configure the required IO as an analog channel in the GPIOx_MODER register, and for other peripherals, only need to configure it as a multiplexing function;

Guess you like

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