stm32f4xx_gpio.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_gpio.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 30-September-2011
  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. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  68. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  69. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  70. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  71. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  72. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  73. *
  74. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  75. ******************************************************************************
  76. */
  77. /* Includes ------------------------------------------------------------------*/
  78. #include "stm32f4xx_gpio.h"
  79. #include "stm32f4xx_rcc.h"
  80. /** @addtogroup STM32F4xx_StdPeriph_Driver
  81. * @{
  82. */
  83. /** @defgroup GPIO
  84. * @brief GPIO driver modules
  85. * @{
  86. */
  87. /* Private typedef -----------------------------------------------------------*/
  88. /* Private define ------------------------------------------------------------*/
  89. /* Private macro -------------------------------------------------------------*/
  90. /* Private variables ---------------------------------------------------------*/
  91. /* Private function prototypes -----------------------------------------------*/
  92. /* Private functions ---------------------------------------------------------*/
  93. /** @defgroup GPIO_Private_Functions
  94. * @{
  95. */
  96. /** @defgroup GPIO_Group1 Initialization and Configuration
  97. * @brief Initialization and Configuration
  98. *
  99. @verbatim
  100. ===============================================================================
  101. Initialization and Configuration
  102. ===============================================================================
  103. @endverbatim
  104. * @{
  105. */
  106. /**
  107. * @brief Deinitializes the GPIOx peripheral registers to their default reset values.
  108. * @note By default, The GPIO pins are configured in input floating mode (except JTAG pins).
  109. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  110. * @retval None
  111. */
  112. void GPIO_DeInit(GPIO_TypeDef* GPIOx)
  113. {
  114. /* Check the parameters */
  115. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  116. if (GPIOx == GPIOA)
  117. {
  118. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  119. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA, DISABLE);
  120. }
  121. else if (GPIOx == GPIOB)
  122. {
  123. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  124. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOB, DISABLE);
  125. }
  126. else if (GPIOx == GPIOC)
  127. {
  128. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  129. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOC, DISABLE);
  130. }
  131. else if (GPIOx == GPIOD)
  132. {
  133. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  134. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOD, DISABLE);
  135. }
  136. else if (GPIOx == GPIOE)
  137. {
  138. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOE, ENABLE);
  139. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOE, DISABLE);
  140. }
  141. else if (GPIOx == GPIOF)
  142. {
  143. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOF, ENABLE);
  144. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOF, DISABLE);
  145. }
  146. else if (GPIOx == GPIOG)
  147. {
  148. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOG, ENABLE);
  149. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOG, DISABLE);
  150. }
  151. else if (GPIOx == GPIOH)
  152. {
  153. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOH, ENABLE);
  154. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOH, DISABLE);
  155. }
  156. else
  157. {
  158. if (GPIOx == GPIOI)
  159. {
  160. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOI, ENABLE);
  161. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOI, DISABLE);
  162. }
  163. }
  164. }
  165. /**
  166. * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_InitStruct.
  167. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  168. * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that contains
  169. * the configuration information for the specified GPIO peripheral.
  170. * @retval None
  171. */
  172. void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
  173. {
  174. uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;
  175. /* Check the parameters */
  176. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  177. assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
  178. assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
  179. assert_param(IS_GPIO_PUPD(GPIO_InitStruct->GPIO_PuPd));
  180. /* -------------------------Configure the port pins---------------- */
  181. /*-- GPIO Mode Configuration --*/
  182. for (pinpos = 0x00; pinpos < 0x10; pinpos++)
  183. {
  184. pos = ((uint32_t)0x01) << pinpos;
  185. /* Get the port pins position */
  186. currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
  187. if (currentpin == pos)
  188. {
  189. GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (pinpos * 2));
  190. GPIOx->MODER |= (((uint32_t)GPIO_InitStruct->GPIO_Mode) << (pinpos * 2));
  191. if ((GPIO_InitStruct->GPIO_Mode == GPIO_Mode_OUT) || (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_AF))
  192. {
  193. /* Check Speed mode parameters */
  194. assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
  195. /* Speed mode configuration */
  196. GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (pinpos * 2));
  197. GPIOx->OSPEEDR |= ((uint32_t)(GPIO_InitStruct->GPIO_Speed) << (pinpos * 2));
  198. /* Check Output mode parameters */
  199. assert_param(IS_GPIO_OTYPE(GPIO_InitStruct->GPIO_OType));
  200. /* Output mode configuration*/
  201. GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)pinpos)) ;
  202. GPIOx->OTYPER |= (uint16_t)(((uint16_t)GPIO_InitStruct->GPIO_OType) << ((uint16_t)pinpos));
  203. }
  204. /* Pull-up Pull down resistor configuration*/
  205. GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)pinpos * 2));
  206. GPIOx->PUPDR |= (((uint32_t)GPIO_InitStruct->GPIO_PuPd) << (pinpos * 2));
  207. }
  208. }
  209. }
  210. /**
  211. * @brief Fills each GPIO_InitStruct member with its default value.
  212. * @param GPIO_InitStruct : pointer to a GPIO_InitTypeDef structure which will be initialized.
  213. * @retval None
  214. */
  215. void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)
  216. {
  217. /* Reset GPIO init structure parameters values */
  218. GPIO_InitStruct->GPIO_Pin = GPIO_Pin_All;
  219. GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN;
  220. GPIO_InitStruct->GPIO_Speed = GPIO_Speed_2MHz;
  221. GPIO_InitStruct->GPIO_OType = GPIO_OType_PP;
  222. GPIO_InitStruct->GPIO_PuPd = GPIO_PuPd_NOPULL;
  223. }
  224. /**
  225. * @brief Locks GPIO Pins configuration registers.
  226. * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
  227. * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
  228. * @note The configuration of the locked GPIO pins can no longer be modified
  229. * until the next reset.
  230. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  231. * @param GPIO_Pin: specifies the port bit to be locked.
  232. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  233. * @retval None
  234. */
  235. void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  236. {
  237. __IO uint32_t tmp = 0x00010000;
  238. /* Check the parameters */
  239. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  240. assert_param(IS_GPIO_PIN(GPIO_Pin));
  241. tmp |= GPIO_Pin;
  242. /* Set LCKK bit */
  243. GPIOx->LCKR = tmp;
  244. /* Reset LCKK bit */
  245. GPIOx->LCKR = GPIO_Pin;
  246. /* Set LCKK bit */
  247. GPIOx->LCKR = tmp;
  248. /* Read LCKK bit*/
  249. tmp = GPIOx->LCKR;
  250. /* Read LCKK bit*/
  251. tmp = GPIOx->LCKR;
  252. }
  253. /**
  254. * @}
  255. */
  256. /** @defgroup GPIO_Group2 GPIO Read and Write
  257. * @brief GPIO Read and Write
  258. *
  259. @verbatim
  260. ===============================================================================
  261. GPIO Read and Write
  262. ===============================================================================
  263. @endverbatim
  264. * @{
  265. */
  266. /**
  267. * @brief Reads the specified input port pin.
  268. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  269. * @param GPIO_Pin: specifies the port bit to read.
  270. * This parameter can be GPIO_Pin_x where x can be (0..15).
  271. * @retval The input port pin value.
  272. */
  273. uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  274. {
  275. uint8_t bitstatus = 0x00;
  276. /* Check the parameters */
  277. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  278. assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
  279. if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)Bit_RESET)
  280. {
  281. bitstatus = (uint8_t)Bit_SET;
  282. }
  283. else
  284. {
  285. bitstatus = (uint8_t)Bit_RESET;
  286. }
  287. return bitstatus;
  288. }
  289. /**
  290. * @brief Reads the specified GPIO input data port.
  291. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  292. * @retval GPIO input data port value.
  293. */
  294. uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx)
  295. {
  296. /* Check the parameters */
  297. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  298. return ((uint16_t)GPIOx->IDR);
  299. }
  300. /**
  301. * @brief Reads the specified output data port bit.
  302. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  303. * @param GPIO_Pin: specifies the port bit to read.
  304. * This parameter can be GPIO_Pin_x where x can be (0..15).
  305. * @retval The output port pin value.
  306. */
  307. uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  308. {
  309. uint8_t bitstatus = 0x00;
  310. /* Check the parameters */
  311. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  312. assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
  313. if ((GPIOx->ODR & GPIO_Pin) != (uint32_t)Bit_RESET)
  314. {
  315. bitstatus = (uint8_t)Bit_SET;
  316. }
  317. else
  318. {
  319. bitstatus = (uint8_t)Bit_RESET;
  320. }
  321. return bitstatus;
  322. }
  323. /**
  324. * @brief Reads the specified GPIO output data port.
  325. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  326. * @retval GPIO output data port value.
  327. */
  328. uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx)
  329. {
  330. /* Check the parameters */
  331. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  332. return ((uint16_t)GPIOx->ODR);
  333. }
  334. /**
  335. * @brief Sets the selected data port bits.
  336. * @note This functions uses GPIOx_BSRR register to allow atomic read/modify
  337. * accesses. In this way, there is no risk of an IRQ occurring between
  338. * the read and the modify access.
  339. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  340. * @param GPIO_Pin: specifies the port bits to be written.
  341. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  342. * @retval None
  343. */
  344. void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  345. {
  346. /* Check the parameters */
  347. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  348. assert_param(IS_GPIO_PIN(GPIO_Pin));
  349. GPIOx->BSRRL = GPIO_Pin;
  350. }
  351. /**
  352. * @brief Clears the selected data port bits.
  353. * @note This functions uses GPIOx_BSRR register to allow atomic read/modify
  354. * accesses. In this way, there is no risk of an IRQ occurring between
  355. * the read and the modify access.
  356. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  357. * @param GPIO_Pin: specifies the port bits to be written.
  358. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  359. * @retval None
  360. */
  361. void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  362. {
  363. /* Check the parameters */
  364. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  365. assert_param(IS_GPIO_PIN(GPIO_Pin));
  366. GPIOx->BSRRH = GPIO_Pin;
  367. }
  368. /**
  369. * @brief Sets or clears the selected data port bit.
  370. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  371. * @param GPIO_Pin: specifies the port bit to be written.
  372. * This parameter can be one of GPIO_Pin_x where x can be (0..15).
  373. * @param BitVal: specifies the value to be written to the selected bit.
  374. * This parameter can be one of the BitAction enum values:
  375. * @arg Bit_RESET: to clear the port pin
  376. * @arg Bit_SET: to set the port pin
  377. * @retval None
  378. */
  379. void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal)
  380. {
  381. /* Check the parameters */
  382. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  383. assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
  384. assert_param(IS_GPIO_BIT_ACTION(BitVal));
  385. if (BitVal != Bit_RESET)
  386. {
  387. GPIOx->BSRRL = GPIO_Pin;
  388. }
  389. else
  390. {
  391. GPIOx->BSRRH = GPIO_Pin ;
  392. }
  393. }
  394. /**
  395. * @brief Writes data to the specified GPIO data port.
  396. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  397. * @param PortVal: specifies the value to be written to the port output data register.
  398. * @retval None
  399. */
  400. void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal)
  401. {
  402. /* Check the parameters */
  403. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  404. GPIOx->ODR = PortVal;
  405. }
  406. /**
  407. * @brief Toggles the specified GPIO pins..
  408. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  409. * @param GPIO_Pin: Specifies the pins to be toggled.
  410. * @retval None
  411. */
  412. void GPIO_ToggleBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  413. {
  414. /* Check the parameters */
  415. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  416. GPIOx->ODR ^= GPIO_Pin;
  417. }
  418. /**
  419. * @}
  420. */
  421. /** @defgroup GPIO_Group3 GPIO Alternate functions configuration function
  422. * @brief GPIO Alternate functions configuration function
  423. *
  424. @verbatim
  425. ===============================================================================
  426. GPIO Alternate functions configuration function
  427. ===============================================================================
  428. @endverbatim
  429. * @{
  430. */
  431. /**
  432. * @brief Changes the mapping of the specified pin.
  433. * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  434. * @param GPIO_PinSource: specifies the pin for the Alternate function.
  435. * This parameter can be GPIO_PinSourcex where x can be (0..15).
  436. * @param GPIO_AFSelection: selects the pin to used as Alternate function.
  437. * This parameter can be one of the following values:
  438. * @arg GPIO_AF_RTC_50Hz: Connect RTC_50Hz pin to AF0 (default after reset)
  439. * @arg GPIO_AF_MCO: Connect MCO pin (MCO1 and MCO2) to AF0 (default after reset)
  440. * @arg GPIO_AF_TAMPER: Connect TAMPER pins (TAMPER_1 and TAMPER_2) to AF0 (default after reset)
  441. * @arg GPIO_AF_SWJ: Connect SWJ pins (SWD and JTAG)to AF0 (default after reset)
  442. * @arg GPIO_AF_TRACE: Connect TRACE pins to AF0 (default after reset)
  443. * @arg GPIO_AF_TIM1: Connect TIM1 pins to AF1
  444. * @arg GPIO_AF_TIM2: Connect TIM2 pins to AF1
  445. * @arg GPIO_AF_TIM3: Connect TIM3 pins to AF2
  446. * @arg GPIO_AF_TIM4: Connect TIM4 pins to AF2
  447. * @arg GPIO_AF_TIM5: Connect TIM5 pins to AF2
  448. * @arg GPIO_AF_TIM8: Connect TIM8 pins to AF3
  449. * @arg GPIO_AF_TIM9: Connect TIM9 pins to AF3
  450. * @arg GPIO_AF_TIM10: Connect TIM10 pins to AF3
  451. * @arg GPIO_AF_TIM11: Connect TIM11 pins to AF3
  452. * @arg GPIO_AF_I2C1: Connect I2C1 pins to AF4
  453. * @arg GPIO_AF_I2C2: Connect I2C2 pins to AF4
  454. * @arg GPIO_AF_I2C3: Connect I2C3 pins to AF4
  455. * @arg GPIO_AF_SPI1: Connect SPI1 pins to AF5
  456. * @arg GPIO_AF_SPI2: Connect SPI2/I2S2 pins to AF5
  457. * @arg GPIO_AF_SPI3: Connect SPI3/I2S3 pins to AF6
  458. * @arg GPIO_AF_I2S3ext: Connect I2S3ext pins to AF7
  459. * @arg GPIO_AF_USART1: Connect USART1 pins to AF7
  460. * @arg GPIO_AF_USART2: Connect USART2 pins to AF7
  461. * @arg GPIO_AF_USART3: Connect USART3 pins to AF7
  462. * @arg GPIO_AF_UART4: Connect UART4 pins to AF8
  463. * @arg GPIO_AF_UART5: Connect UART5 pins to AF8
  464. * @arg GPIO_AF_USART6: Connect USART6 pins to AF8
  465. * @arg GPIO_AF_CAN1: Connect CAN1 pins to AF9
  466. * @arg GPIO_AF_CAN2: Connect CAN2 pins to AF9
  467. * @arg GPIO_AF_TIM12: Connect TIM12 pins to AF9
  468. * @arg GPIO_AF_TIM13: Connect TIM13 pins to AF9
  469. * @arg GPIO_AF_TIM14: Connect TIM14 pins to AF9
  470. * @arg GPIO_AF_OTG_FS: Connect OTG_FS pins to AF10
  471. * @arg GPIO_AF_OTG_HS: Connect OTG_HS pins to AF10
  472. * @arg GPIO_AF_ETH: Connect ETHERNET pins to AF11
  473. * @arg GPIO_AF_FSMC: Connect FSMC pins to AF12
  474. * @arg GPIO_AF_OTG_HS_FS: Connect OTG HS (configured in FS) pins to AF12
  475. * @arg GPIO_AF_SDIO: Connect SDIO pins to AF12
  476. * @arg GPIO_AF_DCMI: Connect DCMI pins to AF13
  477. * @arg GPIO_AF_EVENTOUT: Connect EVENTOUT pins to AF15
  478. * @retval None
  479. */
  480. void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)
  481. {
  482. uint32_t temp = 0x00;
  483. uint32_t temp_2 = 0x00;
  484. /* Check the parameters */
  485. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  486. assert_param(IS_GPIO_PIN_SOURCE(GPIO_PinSource));
  487. assert_param(IS_GPIO_AF(GPIO_AF));
  488. temp = ((uint32_t)(GPIO_AF) << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
  489. GPIOx->AFR[GPIO_PinSource >> 0x03] &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
  490. temp_2 = GPIOx->AFR[GPIO_PinSource >> 0x03] | temp;
  491. GPIOx->AFR[GPIO_PinSource >> 0x03] = temp_2;
  492. }
  493. /**
  494. * @}
  495. */
  496. /**
  497. * @}
  498. */
  499. /**
  500. * @}
  501. */
  502. /**
  503. * @}
  504. */
  505. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/