關於嵌入式學習隨筆->7《IO引腳復用和映射原理》


對於M4/M7來說引腳的復用和映射原理是一模一樣的,M3與M4/M7是有區別的。現在主要學習M4/M7。

1、什么是復用,STM32引腳怎么復用?

   眾所周知,一般對於一個芯片來講都有很多個引腳,但是,如果這些引腳都做IO口來說,這是十分浪費的。比如我們的項目工程只需要40個IO口,那么其他引腳是不是都會被閑置呢。如果沒有復用功能,那么顯然會浪費很多引腳。STM32有很多的內置外設,這些外設的外部引腳都是與GPIO復用的。也就是說,一個GPIO如果可以復用為內置外設的功能引腳,那么當這個GPIO作為內置外設使用的時候,就叫復用。

   例如串口1的發送接收引腳是PA9、PA10,當我們把PA9、PA10不用做GPIO,而是用作復用功能串口1的發送接收引腳時,就叫做端口復用。一般來說,一個引腳有很多個復用功能,當我們需要一些特殊功能的時候,我們可以到對應芯片的數據手冊查找對應的引腳。(STM32F767IGT6引腳復用功能查詢

   STN32系列微控制器IO引腳通過一個復用器連接到內置外設或模塊。該復用器一次只允許一個外設的復用功能(AF)連接到對應的IO口。這樣就可以確保共用同一個IO引腳的外設之間不會發生沖突。每個IO引腳都有一個復用器,該復用器采用16路復用功能輸入(AF0到AF15),可通過GPIOx_AFRL(針對引腳0-7)和GPIOx_AFRH(針對引腳8-15)寄存器對這些輸入進行配置,沒思維控制一路復用。端口復用示意圖如下:

imageimage

2、復用功能寄存器AFRL(低位0-7)

image

3、復用功能寄存器AFRH(高位8-15)

image

image

4、復用功能映射配置

--》系統功能

  -->將I/O連接到AF0(系統默認的),然后根據所用功能進行配置:

    -->JTAG/SWD:在各器件復位后,會將這些引腳指定為專用引腳,可供片上調試模塊立即使用(不受GPIO控制器控制)

    -->RTC_REFIN:此引腳應配置為浮空模式。

    -->MCO1和MCO2:這些引腳必須配置為復用功能模式。

--》在GPIOx_MODER寄存器中將所需I/O配置為輸出或是輸入。

--》外設復用功能

   -->對於ADC和DAC,在GPIOx_MODER寄存器中將所需I/O配置為模擬通道(DAC與ADC都是)。

   -->對於其它外設:

      -->在GPIOx_MODER寄存器中將所需I/O配置為復用功能

      -->通過GPIOx_OTYPER、GPIOx_PUPDR和GPIOx_OSPEEDER寄存器,分別配置類型、上/下拉及速度。

      -->在GPIOx_AFRL或是GPIOx_AFRH寄存器中,將I/O連接到所需AFx。

5、端口復用配置過程

--》首先,GPIO端口時鍾使能,所需函數(例如GPIOA時鍾使能):

  1 __HAL_RCC_GPIOA_CLK_ENABLE();			//使能GPIOA時鍾

--》其次,復用外設時鍾使能,所需函數(例如串口功能):

  1 __HAL_RCC_USART1_CLK_ENABLE();			//使能USART1時鍾

--》然后,將端口模式配置為復用功能,所需函數:

  1 HAL_GPIO_Init(GPIO_TypeDef  *GPIOx, GPIO_InitTypeDef *GPIO_Init)

-->其中GPIO_InitTypeDef結構體為:

(Pin為引腳、Mode為輸入輸出模式、Pull為上/下拉、Speed為速度、Alternate為功能)

  1 typedef struct
  2 {
  3   uint32_t Pin;       /*!< Specifies the GPIO pins to be configured.
 4                            This parameter can be any value of @ref GPIO_pins_define */
  5 
  6   uint32_t Mode;      /*!< Specifies the operating mode for the selected pins.
 7                            This parameter can be a value of @ref GPIO_mode_define */
  8 
  9   uint32_t Pull;      /*!< Specifies the Pull-up or Pull-Down activation for the selected pins.
 10                            This parameter can be a value of @ref GPIO_pull_define */
 11 
 12   uint32_t Speed;     /*!< Specifies the speed for the selected pins.
 13                            This parameter can be a value of @ref GPIO_speed_define */
 14 
 15   uint32_t Alternate;  /*!< Peripheral to be connected to the selected pins.
 16                             This parameter can be a value of @ref GPIO_Alternate_function_selection */
 17 }GPIO_InitTypeDef;

6、AF(0-15)功能選擇

  1 /**
 2   * @brief   AF 0 selection
 3   */
  4 #define GPIO_AF0_RTC_50Hz      ((uint8_t)0x00U)  /* RTC_50Hz Alternate Function mapping                       */
  5 #define GPIO_AF0_MCO           ((uint8_t)0x00U)  /* MCO (MCO1 and MCO2) Alternate Function mapping            */
  6 #define GPIO_AF0_SWJ           ((uint8_t)0x00U)  /* SWJ (SWD and JTAG) Alternate Function mapping             */
  7 #define GPIO_AF0_TRACE         ((uint8_t)0x00U)  /* TRACE Alternate Function mapping                          */
  8 
  9 /**
 10   * @brief   AF 1 selection
 11   */
 12 #define GPIO_AF1_TIM1          ((uint8_t)0x01U)  /* TIM1 Alternate Function mapping */
 13 #define GPIO_AF1_TIM2          ((uint8_t)0x01U)  /* TIM2 Alternate Function mapping */
 14 #if defined (STM32F765xx) || defined(STM32F767xx) || defined(STM32F769xx) || defined(STM32F777xx) || defined(STM32F779xx)
 15 #define GPIO_AF1_UART5         ((uint8_t)0x01U)  /* UART5 Alternate Function mapping */
 16 #define GPIO_AF1_I2C4          ((uint8_t)0x01U)  /* I2C4 Alternate Function mapping  */
 17 #endif /* STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
 18 
 19 /**
 20   * @brief   AF 2 selection
 21   */
 22 #define GPIO_AF2_TIM3          ((uint8_t)0x02U)  /* TIM3 Alternate Function mapping */
 23 #define GPIO_AF2_TIM4          ((uint8_t)0x02U)  /* TIM4 Alternate Function mapping */
 24 #define GPIO_AF2_TIM5          ((uint8_t)0x02U)  /* TIM5 Alternate Function mapping */
 25 
 26 /**
 27   * @brief   AF 3 selection
 28   */
 29 #define GPIO_AF3_TIM8          ((uint8_t)0x03U)  /* TIM8 Alternate Function mapping  */
 30 #define GPIO_AF3_TIM9          ((uint8_t)0x03U)  /* TIM9 Alternate Function mapping  */
 31 #define GPIO_AF3_TIM10         ((uint8_t)0x03U)  /* TIM10 Alternate Function mapping */
 32 #define GPIO_AF3_TIM11         ((uint8_t)0x03U)  /* TIM11 Alternate Function mapping */
 33 #define GPIO_AF3_LPTIM1        ((uint8_t)0x03U)  /* LPTIM1 Alternate Function mapping */
 34 #define GPIO_AF3_CEC           ((uint8_t)0x03U)  /* CEC Alternate Function mapping */
 35 #if defined (STM32F765xx) || defined(STM32F767xx) || defined(STM32F769xx) || defined(STM32F777xx) || defined(STM32F779xx)
 36 #define GPIO_AF3_DFSDM1         ((uint8_t)0x03U)  /* DFSDM1 Alternate Function mapping */
 37 #endif /* STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
 38 /**
 39   * @brief   AF 4 selection
 40   */
 41 #define GPIO_AF4_I2C1          ((uint8_t)0x04U)  /* I2C1 Alternate Function mapping */
 42 #define GPIO_AF4_I2C2          ((uint8_t)0x04U)  /* I2C2 Alternate Function mapping */
 43 #define GPIO_AF4_I2C3          ((uint8_t)0x04U)  /* I2C3 Alternate Function mapping */
 44 #define GPIO_AF4_I2C4          ((uint8_t)0x04U)  /* I2C4 Alternate Function mapping */
 45 #define GPIO_AF4_CEC           ((uint8_t)0x04U)  /* CEC Alternate Function mapping */
 46 #if defined (STM32F765xx) || defined(STM32F767xx) || defined(STM32F769xx) || defined(STM32F777xx) || defined(STM32F779xx)
 47 #define GPIO_AF4_USART1        ((uint8_t)0x04)  /* USART1 Alternate Function mapping */
 48 #endif /* STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
 49 
 50 /**
 51   * @brief   AF 5 selection
 52   */
 53 #define GPIO_AF5_SPI1          ((uint8_t)0x05U)  /* SPI1 Alternate Function mapping        */
 54 #define GPIO_AF5_SPI2          ((uint8_t)0x05U)  /* SPI2/I2S2 Alternate Function mapping   */
 55 #define GPIO_AF5_SPI3          ((uint8_t)0x05U)  /* SPI3/I2S3 Alternate Function mapping   */
 56 #define GPIO_AF5_SPI4          ((uint8_t)0x05U)  /* SPI4 Alternate Function mapping        */
 57 #define GPIO_AF5_SPI5          ((uint8_t)0x05U)  /* SPI5 Alternate Function mapping        */
 58 #define GPIO_AF5_SPI6          ((uint8_t)0x05U)  /* SPI6 Alternate Function mapping        */
 59 
 60 /**
 61   * @brief   AF 6 selection
 62   */
 63 #define GPIO_AF6_SPI3          ((uint8_t)0x06U)  /* SPI3/I2S3 Alternate Function mapping  */
 64 #define GPIO_AF6_SAI1          ((uint8_t)0x06U)  /* SAI1 Alternate Function mapping       */
 65 #if defined (STM32F765xx) || defined(STM32F767xx) || defined(STM32F769xx) || defined(STM32F777xx) || defined(STM32F779xx)
 66 #define GPIO_AF6_UART4         ((uint8_t)0x06U)   /* UART4 Alternate Function mapping     */
 67 #define GPIO_AF6_DFSDM1        ((uint8_t)0x06U)  /* DFSDM1 Alternate Function mapping     */
 68 #endif /* STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
 69 
 70 /**
 71   * @brief   AF 7 selection
 72   */
 73 #define GPIO_AF7_USART1        ((uint8_t)0x07U)  /* USART1 Alternate Function mapping     */
 74 #define GPIO_AF7_USART2        ((uint8_t)0x07U)  /* USART2 Alternate Function mapping     */
 75 #define GPIO_AF7_USART3        ((uint8_t)0x07U)  /* USART3 Alternate Function mapping     */
 76 #define GPIO_AF7_UART5         ((uint8_t)0x07U)  /* UART5 Alternate Function mapping      */
 77 #define GPIO_AF7_SPDIFRX       ((uint8_t)0x07U)  /* SPDIF-RX Alternate Function mapping   */
 78 #define GPIO_AF7_SPI2          ((uint8_t)0x07U)  /* SPI2 Alternate Function mapping       */
 79 #define GPIO_AF7_SPI3          ((uint8_t)0x07U)  /* SPI3 Alternate Function mapping       */
 80 #if defined (STM32F765xx) || defined(STM32F767xx) || defined(STM32F769xx) || defined(STM32F777xx) || defined(STM32F779xx)
 81 #define GPIO_AF7_SPI6          ((uint8_t)0x07U)  /* SPI6 Alternate Function mapping       */
 82 #define GPIO_AF7_DFSDM1         ((uint8_t)0x07U) /* DFSDM1 Alternate Function mapping      */
 83 #endif /* STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
 84 
 85 /**
 86   * @brief   AF 8 selection
 87   */
 88 #define GPIO_AF8_UART4         ((uint8_t)0x08U)  /* UART4 Alternate Function mapping  */
 89 #define GPIO_AF8_UART5         ((uint8_t)0x08U)  /* UART5 Alternate Function mapping  */
 90 #define GPIO_AF8_USART6        ((uint8_t)0x08U)  /* USART6 Alternate Function mapping */
 91 #define GPIO_AF8_UART7         ((uint8_t)0x08U)  /* UART7 Alternate Function mapping  */
 92 #define GPIO_AF8_UART8         ((uint8_t)0x08U)  /* UART8 Alternate Function mapping  */
 93 #define GPIO_AF8_SPDIFRX       ((uint8_t)0x08U)  /* SPIDIF-RX Alternate Function mapping */
 94 #define GPIO_AF8_SAI2          ((uint8_t)0x08U)  /* SAI2 Alternate Function mapping   */
 95 #if defined (STM32F765xx) || defined(STM32F767xx) || defined(STM32F769xx) || defined(STM32F777xx) || defined(STM32F779xx)
 96 #define GPIO_AF8_SPI6          ((uint8_t)0x08U)  /* SPI6 Alternate Function mapping   */
 97 #endif /* STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
 98 
 99 
100 /**
101   * @brief   AF 9 selection
102   */
103 #define GPIO_AF9_CAN1          ((uint8_t)0x09U)  /* CAN1 Alternate Function mapping    */
104 #define GPIO_AF9_CAN2          ((uint8_t)0x09U)  /* CAN2 Alternate Function mapping    */
105 #define GPIO_AF9_TIM12         ((uint8_t)0x09U)  /* TIM12 Alternate Function mapping   */
106 #define GPIO_AF9_TIM13         ((uint8_t)0x09U)  /* TIM13 Alternate Function mapping   */
107 #define GPIO_AF9_TIM14         ((uint8_t)0x09U)  /* TIM14 Alternate Function mapping   */
108 #define GPIO_AF9_QUADSPI       ((uint8_t)0x09U)  /* QUADSPI Alternate Function mapping */
109 #if defined(STM32F746xx) || defined(STM32F756xx) || defined(STM32F767xx) || defined(STM32F769xx) || defined(STM32F777xx) || defined(STM32F779xx)
110 #define GPIO_AF9_LTDC          ((uint8_t)0x09U)  /* LCD-TFT Alternate Function mapping */
111 #endif /* STM32F746xx || STM32F756xx || STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
112 #if defined(STM32F746xx) || defined(STM32F756xx) || defined(STM32F765xx) || defined(STM32F765xx) || defined(STM32F767xx) || defined(STM32F769xx) || defined(STM32F777xx) || defined(STM32F779xx)
113 #define GPIO_AF9_FMC           ((uint8_t)0x09U)   /* FMC Alternate Function mapping     */
114 #endif /* STM32F746xx || STM32F756xx || STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
115 /**
116   * @brief   AF 10 selection
117   */
118 #define GPIO_AF10_OTG_FS        ((uint8_t)0xAU)  /* OTG_FS Alternate Function mapping */
119 #define GPIO_AF10_OTG_HS        ((uint8_t)0xAU)  /* OTG_HS Alternate Function mapping */
120 #define GPIO_AF10_QUADSPI       ((uint8_t)0xAU)  /* QUADSPI Alternate Function mapping */
121 #define GPIO_AF10_SAI2          ((uint8_t)0xAU)  /* SAI2 Alternate Function mapping */
122 #if defined (STM32F765xx) || defined(STM32F767xx) || defined(STM32F769xx) || defined(STM32F777xx) || defined(STM32F779xx)
123 #define GPIO_AF10_DFSDM1         ((uint8_t)0x0AU)  /* DFSDM1 Alternate Function mapping  */
124 #define GPIO_AF10_SDMMC2        ((uint8_t)0x0AU)  /* SDMMC2 Alternate Function mapping */
125 #endif /* STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
126 
127 /**
128   * @brief   AF 11 selection
129   */
130 #define GPIO_AF11_ETH           ((uint8_t)0x0BU)  /* ETHERNET Alternate Function mapping */
131 #if defined(STM32F765xx) || defined (STM32F767xx) || defined (STM32F769xx) || defined (STM32F777xx) || defined (STM32F779xx)
132 #define GPIO_AF11_CAN3          ((uint8_t)0x0BU)  /* CAN3 Alternate Function mapping     */
133 #define GPIO_AF11_SDMMC2        ((uint8_t)0x0BU)  /* SDMMC2 Alternate Function mapping   */
134 #define GPIO_AF11_I2C4          ((uint8_t)0x0BU)  /* I2C4 Alternate Function mapping     */
135 #endif /* STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
136 
137 /**
138   * @brief   AF 12 selection
139   */
140 #define GPIO_AF12_FMC           ((uint8_t)0xCU)  /* FMC Alternate Function mapping                      */
141 #define GPIO_AF12_OTG_HS_FS     ((uint8_t)0xCU)  /* OTG HS configured in FS, Alternate Function mapping */
142 #define GPIO_AF12_SDMMC1        ((uint8_t)0xCU)  /* SDMMC1 Alternate Function mapping                   */
143 #if defined(STM32F765xx) || defined (STM32F767xx) || defined (STM32F769xx) || defined (STM32F777xx) || defined (STM32F779xx)
144 #define GPIO_AF12_MDIOS        ((uint8_t)0xCU)  /* SDMMC1 Alternate Function mapping                    */
145 #define GPIO_AF12_UART7        ((uint8_t)0xCU)  /* UART7 Alternate Function mapping                     */
146 #endif /* STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
147 
148 /**
149   * @brief   AF 13 selection
150   */
151 #define GPIO_AF13_DCMI          ((uint8_t)0x0DU)  /* DCMI Alternate Function mapping */
152 #if defined (STM32F769xx) || defined (STM32F779xx)
153 #define GPIO_AF13_DSI           ((uint8_t)0x0DU)  /* DSI Alternate Function mapping  */
154 #endif /* STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
155 #if defined(STM32F746xx) || defined(STM32F756xx) || defined(STM32F767xx) || defined(STM32F769xx) || defined(STM32F777xx) || defined(STM32F779xx)
156 #define GPIO_AF13_LTDC          ((uint8_t)0x0DU)  /* LTDC Alternate Function mapping */
157 
158 /**
159   * @brief   AF 14 selection
160   */
161 #define GPIO_AF14_LTDC          ((uint8_t)0x0EU)  /* LCD-TFT Alternate Function mapping */
162 #endif /* STM32F746xx || STM32F756xx || STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
163 /**
164   * @brief   AF 15 selection
165   */
166 #define GPIO_AF15_EVENTOUT      ((uint8_t)0x0FU)  /* EVENTOUT Alternate Function mapping */

7、串口配置復用功能過程(例),HAL_UART_MspInit函數會被HAL_UART_Init函數調用。

  1 void HAL_UART_MspInit(UART_HandleTypeDef *huart)
  2 {
  3     //GPIO端口設置
  4 	GPIO_InitTypeDef GPIO_Initure;
  5 
  6 	if(huart->Instance==USART1)//如果是串口1,進行串口1 MSP初始化
  7 	{
  8 		__HAL_RCC_GPIOA_CLK_ENABLE();			//使能GPIOA時鍾
  9 		__HAL_RCC_USART1_CLK_ENABLE();			//使能USART1時鍾
 10 
 11 		GPIO_Initure.Pin=GPIO_PIN_9;			//PA9
 12 		GPIO_Initure.Mode=GPIO_MODE_AF_PP;		//復用推挽輸出
 13 		GPIO_Initure.Pull=GPIO_PULLUP;			//上拉
 14 		GPIO_Initure.Speed=GPIO_SPEED_FAST;		//高速
 15 		GPIO_Initure.Alternate=GPIO_AF7_USART1;	//復用為USART1
 16 		HAL_GPIO_Init(GPIOA,&GPIO_Initure);	   	//初始化PA9
 17 
 18 		GPIO_Initure.Pin=GPIO_PIN_10;			//PA10
 19 		HAL_GPIO_Init(GPIOA,&GPIO_Initure);	   	//初始化PA10
 20 
 21 #if EN_USART1_RX
 22 		__HAL_UART_ENABLE_IT(huart,UART_IT_RXNE);		//開啟接收中斷
 23 		HAL_NVIC_EnableIRQ(USART1_IRQn);				//使能USART1中斷通道
 24 		HAL_NVIC_SetPriority(USART1_IRQn,3,3);			//搶占優先級3,子優先級3
 25 #endif
 26 	}
 27 }

   這樣就配置好了PA9與PA10為串口復用功能了,另外,想要使用串口通訊還需要對串口進行初始化配置,比如波特率及其數據格式、收發模式等。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM