stm32f4xx_gpio.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_gpio.c
  4. * @author MCD Application Team
  5. * @version V1.0.2
  6. * @date 05-March-2012
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the GPIO peripheral:
  9. * - Initialization and Configuration
  10. * - GPIO Read and Write
  11. * - GPIO Alternate functions configuration
  12. *
  13. * @verbatim
  14. *
  15. * ===================================================================
  16. * How to use this driver
  17. * ===================================================================
  18. * 1. Enable the GPIO AHB clock using the following function
  19. * RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOx, ENABLE);
  20. *
  21. * 2. Configure the GPIO pin(s) using GPIO_Init()
  22. * Four possible configuration are available for each pin:
  23. * - Input: Floating, Pull-up, Pull-down.
  24. * - Output: Push-Pull (Pull-up, Pull-down or no Pull)
  25. * Open Drain (Pull-up, Pull-down or no Pull).
  26. * In output mode, the speed is configurable: 2 MHz, 25 MHz,
  27. * 50 MHz or 100 MHz.
  28. * - Alternate Function: Push-Pull (Pull-up, Pull-down or no Pull)
  29. * Open Drain (Pull-up, Pull-down or no Pull).
  30. * - Analog: required mode when a pin is to be used as ADC channel
  31. * or DAC output.
  32. *
  33. * 3- Peripherals alternate function:
  34. * - For ADC and DAC, configure the desired pin in analog mode using
  35. * GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AN;
  36. * - For other peripherals (TIM, USART...):
  37. * - Connect the pin to the desired peripherals' Alternate
  38. * Function (AF) using GPIO_PinAFConfig() function
  39. * - Configure the desired pin in alternate function mode using
  40. * GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
  41. * - Select the type, pull-up/pull-down and output speed via
  42. * GPIO_PuPd, GPIO_OType and GPIO_Speed members
  43. * - Call GPIO_Init() function
  44. *
  45. * 4. To get the level of a pin configured in input mode use GPIO_ReadInputDataBit()
  46. *
  47. * 5. To set/reset the level of a pin configured in output mode use
  48. * GPIO_SetBits()/GPIO_ResetBits()
  49. *
  50. * 6. During and just after reset, the alternate functions are not
  51. * active and the GPIO pins are configured in input floating mode
  52. * (except JTAG pins).
  53. *
  54. * 7. The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as
  55. * general-purpose (PC14 and PC15, respectively) when the LSE
  56. * oscillator is off. The LSE has priority over the GPIO function.
  57. *
  58. * 8. The HSE oscillator pins OSC_IN/OSC_OUT can be used as
  59. * general-purpose PH0 and PH1, respectively, when the HSE
  60. * oscillator is off. The HSE has priority over the GPIO function.
  61. *
  62. * @endverbatim
  63. *
  64. ******************************************************************************
  65. * @attention
  66. *
  67. * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  68. *
  69. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  70. * You may not use this file except in compliance with the License.
  71. * You may obtain a copy of the License at:
  72. *
  73. * http://www.st.com/software_license_agreement_liberty_v2
  74. *
  75. * Unless required by applicable law or agreed to in writing, software
  76. * distributed under the License is distributed on an "AS IS" BASIS,
  77. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  78. * See the License for the specific language governing permissions and
  79. * limitations under the License.
  80. *
  81. ******************************************************************************
  82. */
  83. /* Includes ------------------------------------------------------------------*/
  84. #include "stm32f4xx_gpio.h"
  85. #include "stm32f4xx_rcc.h"
  86. /** @addtogroup STM32F4xx_StdPeriph_Driver
  87. * @{
  88. */
  89. /** @defgroup GPIO
  90. * @brief GPIO driver modules
  91. * @{
  92. */
  93. /* Private typedef -----------------------------------------------------------*/
  94. /* Private define ------------------------------------------------------------*/
  95. /* Private macro -------------------------------------------------------------*/
  96. /* Private variables ---------------------------------------------------------*/
  97. /* Private function prototypes -----------------------------------------------*/
  98. /* Private functions ---------------------------------------------------------*/
  99. /** @defgroup GPIO_Private_Functions
  100. * @{
  101. */
  102. /** @defgroup GPIO_Group1 Initialization and Configuration
  103. * @brief Initialization and Configuration
  104. *
  105. @verbatim
  106. ===============================================================================
  107. Initialization and Configuration
  108. ===============================================================================
  109. @endverbatim
  110. * @{
  111. */
  112. /**
  113. * @brief Deinitializes the GPIOx peripheral registers to their default reset values.
  114. * @note By default, The GPIO pins are configured in input floating mode (except JTAG pins).
  115. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  116. * @retval None
  117. */
  118. void GPIO_DeInit(GPIO_TypeDef* GPIOx)
  119. {
  120. /* Check the parameters */
  121. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  122. if (GPIOx == GPIOA)
  123. {
  124. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  125. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA, DISABLE);
  126. }
  127. else if (GPIOx == GPIOB)
  128. {
  129. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  130. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOB, DISABLE);
  131. }
  132. else if (GPIOx == GPIOC)
  133. {
  134. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  135. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOC, DISABLE);
  136. }
  137. else if (GPIOx == GPIOD)
  138. {
  139. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  140. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOD, DISABLE);
  141. }
  142. else if (GPIOx == GPIOE)
  143. {
  144. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOE, ENABLE);
  145. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOE, DISABLE);
  146. }
  147. else if (GPIOx == GPIOF)
  148. {
  149. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOF, ENABLE);
  150. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOF, DISABLE);
  151. }
  152. else if (GPIOx == GPIOG)
  153. {
  154. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOG, ENABLE);
  155. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOG, DISABLE);
  156. }
  157. else if (GPIOx == GPIOH)
  158. {
  159. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOH, ENABLE);
  160. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOH, DISABLE);
  161. }
  162. else
  163. {
  164. if (GPIOx == GPIOI)
  165. {
  166. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOI, ENABLE);
  167. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOI, DISABLE);
  168. }
  169. }
  170. }
  171. /**
  172. * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_InitStruct.
  173. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  174. * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that contains
  175. * the configuration information for the specified GPIO peripheral.
  176. * @retval None
  177. */
  178. void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
  179. {
  180. uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;
  181. /* Check the parameters */
  182. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  183. assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
  184. assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
  185. assert_param(IS_GPIO_PUPD(GPIO_InitStruct->GPIO_PuPd));
  186. /* -------------------------Configure the port pins---------------- */
  187. /*-- GPIO Mode Configuration --*/
  188. for (pinpos = 0x00; pinpos < 0x10; pinpos++)
  189. {
  190. pos = ((uint32_t)0x01) << pinpos;
  191. /* Get the port pins position */
  192. currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
  193. if (currentpin == pos)
  194. {
  195. GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (pinpos * 2));
  196. GPIOx->MODER |= (((uint32_t)GPIO_InitStruct->GPIO_Mode) << (pinpos * 2));
  197. if ((GPIO_InitStruct->GPIO_Mode == GPIO_Mode_OUT) || (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_AF))
  198. {
  199. /* Check Speed mode parameters */
  200. assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
  201. /* Speed mode configuration */
  202. GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (pinpos * 2));
  203. GPIOx->OSPEEDR |= ((uint32_t)(GPIO_InitStruct->GPIO_Speed) << (pinpos * 2));
  204. /* Check Output mode parameters */
  205. assert_param(IS_GPIO_OTYPE(GPIO_InitStruct->GPIO_OType));
  206. /* Output mode configuration*/
  207. GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)pinpos)) ;
  208. GPIOx->OTYPER |= (uint16_t)(((uint16_t)GPIO_InitStruct->GPIO_OType) << ((uint16_t)pinpos));
  209. }
  210. /* Pull-up Pull down resistor configuration*/
  211. GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)pinpos * 2));
  212. GPIOx->PUPDR |= (((uint32_t)GPIO_InitStruct->GPIO_PuPd) << (pinpos * 2));
  213. }
  214. }
  215. }
  216. /**
  217. * @brief Fills each GPIO_InitStruct member with its default value.
  218. * @param GPIO_InitStruct : pointer to a GPIO_InitTypeDef structure which will be initialized.
  219. * @retval None
  220. */
  221. void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)
  222. {
  223. /* Reset GPIO init structure parameters values */
  224. GPIO_InitStruct->GPIO_Pin = GPIO_Pin_All;
  225. GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN;
  226. GPIO_InitStruct->GPIO_Speed = GPIO_Speed_2MHz;
  227. GPIO_InitStruct->GPIO_OType = GPIO_OType_PP;
  228. GPIO_InitStruct->GPIO_PuPd = GPIO_PuPd_NOPULL;
  229. }
  230. /**
  231. * @brief Locks GPIO Pins configuration registers.
  232. * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
  233. * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
  234. * @note The configuration of the locked GPIO pins can no longer be modified
  235. * until the next reset.
  236. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  237. * @param GPIO_Pin: specifies the port bit to be locked.
  238. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  239. * @retval None
  240. */
  241. void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  242. {
  243. __IO uint32_t tmp = 0x00010000;
  244. /* Check the parameters */
  245. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  246. assert_param(IS_GPIO_PIN(GPIO_Pin));
  247. tmp |= GPIO_Pin;
  248. /* Set LCKK bit */
  249. GPIOx->LCKR = tmp;
  250. /* Reset LCKK bit */
  251. GPIOx->LCKR = GPIO_Pin;
  252. /* Set LCKK bit */
  253. GPIOx->LCKR = tmp;
  254. /* Read LCKK bit*/
  255. tmp = GPIOx->LCKR;
  256. /* Read LCKK bit*/
  257. tmp = GPIOx->LCKR;
  258. }
  259. /**
  260. * @}
  261. */
  262. /** @defgroup GPIO_Group2 GPIO Read and Write
  263. * @brief GPIO Read and Write
  264. *
  265. @verbatim
  266. ===============================================================================
  267. GPIO Read and Write
  268. ===============================================================================
  269. @endverbatim
  270. * @{
  271. */
  272. /**
  273. * @brief Reads the specified input port pin.
  274. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  275. * @param GPIO_Pin: specifies the port bit to read.
  276. * This parameter can be GPIO_Pin_x where x can be (0..15).
  277. * @retval The input port pin value.
  278. */
  279. uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  280. {
  281. uint8_t bitstatus = 0x00;
  282. /* Check the parameters */
  283. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  284. assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
  285. if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)Bit_RESET)
  286. {
  287. bitstatus = (uint8_t)Bit_SET;
  288. }
  289. else
  290. {
  291. bitstatus = (uint8_t)Bit_RESET;
  292. }
  293. return bitstatus;
  294. }
  295. /**
  296. * @brief Reads the specified GPIO input data port.
  297. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  298. * @retval GPIO input data port value.
  299. */
  300. uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx)
  301. {
  302. /* Check the parameters */
  303. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  304. return ((uint16_t)GPIOx->IDR);
  305. }
  306. /**
  307. * @brief Reads the specified output data port bit.
  308. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  309. * @param GPIO_Pin: specifies the port bit to read.
  310. * This parameter can be GPIO_Pin_x where x can be (0..15).
  311. * @retval The output port pin value.
  312. */
  313. uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  314. {
  315. uint8_t bitstatus = 0x00;
  316. /* Check the parameters */
  317. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  318. assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
  319. if ((GPIOx->ODR & GPIO_Pin) != (uint32_t)Bit_RESET)
  320. {
  321. bitstatus = (uint8_t)Bit_SET;
  322. }
  323. else
  324. {
  325. bitstatus = (uint8_t)Bit_RESET;
  326. }
  327. return bitstatus;
  328. }
  329. /**
  330. * @brief Reads the specified GPIO output data port.
  331. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  332. * @retval GPIO output data port value.
  333. */
  334. uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx)
  335. {
  336. /* Check the parameters */
  337. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  338. return ((uint16_t)GPIOx->ODR);
  339. }
  340. /**
  341. * @brief Sets the selected data port bits.
  342. * @note This functions uses GPIOx_BSRR register to allow atomic read/modify
  343. * accesses. In this way, there is no risk of an IRQ occurring between
  344. * the read and the modify access.
  345. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  346. * @param GPIO_Pin: specifies the port bits to be written.
  347. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  348. * @retval None
  349. */
  350. void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  351. {
  352. /* Check the parameters */
  353. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  354. assert_param(IS_GPIO_PIN(GPIO_Pin));
  355. GPIOx->BSRRL = GPIO_Pin;
  356. }
  357. /**
  358. * @brief Clears the selected data port bits.
  359. * @note This functions uses GPIOx_BSRR register to allow atomic read/modify
  360. * accesses. In this way, there is no risk of an IRQ occurring between
  361. * the read and the modify access.
  362. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  363. * @param GPIO_Pin: specifies the port bits to be written.
  364. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  365. * @retval None
  366. */
  367. void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  368. {
  369. /* Check the parameters */
  370. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  371. assert_param(IS_GPIO_PIN(GPIO_Pin));
  372. GPIOx->BSRRH = GPIO_Pin;
  373. }
  374. /**
  375. * @brief Sets or clears the selected data port bit.
  376. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  377. * @param GPIO_Pin: specifies the port bit to be written.
  378. * This parameter can be one of GPIO_Pin_x where x can be (0..15).
  379. * @param BitVal: specifies the value to be written to the selected bit.
  380. * This parameter can be one of the BitAction enum values:
  381. * @arg Bit_RESET: to clear the port pin
  382. * @arg Bit_SET: to set the port pin
  383. * @retval None
  384. */
  385. void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal)
  386. {
  387. /* Check the parameters */
  388. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  389. assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
  390. assert_param(IS_GPIO_BIT_ACTION(BitVal));
  391. if (BitVal != Bit_RESET)
  392. {
  393. GPIOx->BSRRL = GPIO_Pin;
  394. }
  395. else
  396. {
  397. GPIOx->BSRRH = GPIO_Pin ;
  398. }
  399. }
  400. /**
  401. * @brief Writes data to the specified GPIO data port.
  402. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  403. * @param PortVal: specifies the value to be written to the port output data register.
  404. * @retval None
  405. */
  406. void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal)
  407. {
  408. /* Check the parameters */
  409. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  410. GPIOx->ODR = PortVal;
  411. }
  412. /**
  413. * @brief Toggles the specified GPIO pins..
  414. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  415. * @param GPIO_Pin: Specifies the pins to be toggled.
  416. * @retval None
  417. */
  418. void GPIO_ToggleBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  419. {
  420. /* Check the parameters */
  421. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  422. GPIOx->ODR ^= GPIO_Pin;
  423. }
  424. /**
  425. * @}
  426. */
  427. /** @defgroup GPIO_Group3 GPIO Alternate functions configuration function
  428. * @brief GPIO Alternate functions configuration function
  429. *
  430. @verbatim
  431. ===============================================================================
  432. GPIO Alternate functions configuration function
  433. ===============================================================================
  434. @endverbatim
  435. * @{
  436. */
  437. /**
  438. * @brief Changes the mapping of the specified pin.
  439. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  440. * @param GPIO_PinSource: specifies the pin for the Alternate function.
  441. * This parameter can be GPIO_PinSourcex where x can be (0..15).
  442. * @param GPIO_AFSelection: selects the pin to used as Alternate function.
  443. * This parameter can be one of the following values:
  444. * @arg GPIO_AF_RTC_50Hz: Connect RTC_50Hz pin to AF0 (default after reset)
  445. * @arg GPIO_AF_MCO: Connect MCO pin (MCO1 and MCO2) to AF0 (default after reset)
  446. * @arg GPIO_AF_TAMPER: Connect TAMPER pins (TAMPER_1 and TAMPER_2) to AF0 (default after reset)
  447. * @arg GPIO_AF_SWJ: Connect SWJ pins (SWD and JTAG)to AF0 (default after reset)
  448. * @arg GPIO_AF_TRACE: Connect TRACE pins to AF0 (default after reset)
  449. * @arg GPIO_AF_TIM1: Connect TIM1 pins to AF1
  450. * @arg GPIO_AF_TIM2: Connect TIM2 pins to AF1
  451. * @arg GPIO_AF_TIM3: Connect TIM3 pins to AF2
  452. * @arg GPIO_AF_TIM4: Connect TIM4 pins to AF2
  453. * @arg GPIO_AF_TIM5: Connect TIM5 pins to AF2
  454. * @arg GPIO_AF_TIM8: Connect TIM8 pins to AF3
  455. * @arg GPIO_AF_TIM9: Connect TIM9 pins to AF3
  456. * @arg GPIO_AF_TIM10: Connect TIM10 pins to AF3
  457. * @arg GPIO_AF_TIM11: Connect TIM11 pins to AF3
  458. * @arg GPIO_AF_I2C1: Connect I2C1 pins to AF4
  459. * @arg GPIO_AF_I2C2: Connect I2C2 pins to AF4
  460. * @arg GPIO_AF_I2C3: Connect I2C3 pins to AF4
  461. * @arg GPIO_AF_SPI1: Connect SPI1 pins to AF5
  462. * @arg GPIO_AF_SPI2: Connect SPI2/I2S2 pins to AF5
  463. * @arg GPIO_AF_SPI3: Connect SPI3/I2S3 pins to AF6
  464. * @arg GPIO_AF_I2S3ext: Connect I2S3ext pins to AF7
  465. * @arg GPIO_AF_USART1: Connect USART1 pins to AF7
  466. * @arg GPIO_AF_USART2: Connect USART2 pins to AF7
  467. * @arg GPIO_AF_USART3: Connect USART3 pins to AF7
  468. * @arg GPIO_AF_UART4: Connect UART4 pins to AF8
  469. * @arg GPIO_AF_UART5: Connect UART5 pins to AF8
  470. * @arg GPIO_AF_USART6: Connect USART6 pins to AF8
  471. * @arg GPIO_AF_CAN1: Connect CAN1 pins to AF9
  472. * @arg GPIO_AF_CAN2: Connect CAN2 pins to AF9
  473. * @arg GPIO_AF_TIM12: Connect TIM12 pins to AF9
  474. * @arg GPIO_AF_TIM13: Connect TIM13 pins to AF9
  475. * @arg GPIO_AF_TIM14: Connect TIM14 pins to AF9
  476. * @arg GPIO_AF_OTG_FS: Connect OTG_FS pins to AF10
  477. * @arg GPIO_AF_OTG_HS: Connect OTG_HS pins to AF10
  478. * @arg GPIO_AF_ETH: Connect ETHERNET pins to AF11
  479. * @arg GPIO_AF_FSMC: Connect FSMC pins to AF12
  480. * @arg GPIO_AF_OTG_HS_FS: Connect OTG HS (configured in FS) pins to AF12
  481. * @arg GPIO_AF_SDIO: Connect SDIO pins to AF12
  482. * @arg GPIO_AF_DCMI: Connect DCMI pins to AF13
  483. * @arg GPIO_AF_EVENTOUT: Connect EVENTOUT pins to AF15
  484. * @retval None
  485. */
  486. void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)
  487. {
  488. uint32_t temp = 0x00;
  489. uint32_t temp_2 = 0x00;
  490. /* Check the parameters */
  491. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  492. assert_param(IS_GPIO_PIN_SOURCE(GPIO_PinSource));
  493. assert_param(IS_GPIO_AF(GPIO_AF));
  494. temp = ((uint32_t)(GPIO_AF) << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
  495. GPIOx->AFR[GPIO_PinSource >> 0x03] &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
  496. temp_2 = GPIOx->AFR[GPIO_PinSource >> 0x03] | temp;
  497. GPIOx->AFR[GPIO_PinSource >> 0x03] = temp_2;
  498. }
  499. /**
  500. * @}
  501. */
  502. /**
  503. * @}
  504. */
  505. /**
  506. * @}
  507. */
  508. /**
  509. * @}
  510. */
  511. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/