stm32f0xx_gpio.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_gpio.c
  4. * @author MCD Application Team
  5. * @version V1.5.0
  6. * @date 05-December-2014
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the GPIO peripheral:
  9. * + Initialization and Configuration functions
  10. * + GPIO Read and Write functions
  11. * + GPIO Alternate functions configuration functions
  12. *
  13. * @verbatim
  14. *
  15. *
  16. ===========================================================================
  17. ##### How to use this driver #####
  18. ===========================================================================
  19. [..]
  20. (#) Enable the GPIO AHB clock using RCC_AHBPeriphClockCmd()
  21. (#) 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: Low, Medium, Fast or High.
  27. (++) Alternate Function: Push-Pull (Pull-up, Pull-down or no Pull)
  28. Open Drain (Pull-up, Pull-down or no Pull).
  29. (++) Analog: required mode when a pin is to be used as ADC channel,
  30. DAC output or comparator input.
  31. (#) Peripherals alternate function:
  32. (++) For ADC, DAC and comparators, configure the desired pin in analog
  33. mode using GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AN
  34. (++) For other peripherals (TIM, USART...):
  35. (+++) Connect the pin to the desired peripherals' Alternate
  36. Function (AF) using GPIO_PinAFConfig() function. For PortC,
  37. PortD and PortF, no configuration is needed.
  38. (+++) Configure the desired pin in alternate function mode using
  39. GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
  40. (+++) Select the type, pull-up/pull-down and output speed via
  41. GPIO_PuPd, GPIO_OType and GPIO_Speed members
  42. (+++) Call GPIO_Init() function
  43. (#) To get the level of a pin configured in input mode use GPIO_ReadInputDataBit()
  44. (#) To set/reset the level of a pin configured in output mode use
  45. GPIO_SetBits()/GPIO_ResetBits()
  46. (#) During and just after reset, the alternate functions are not active and
  47. the GPIO pins are configured in input floating mode (except JTAG pins).
  48. (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as
  49. general-purpose (PC14 and PC15, respectively) when the LSE oscillator
  50. is off. The LSE has priority over the GPIO function.
  51. (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as general-purpose
  52. PD0 and PD1, respectively, when the HSE oscillator is off. The HSE has
  53. priority over the GPIO function.
  54. @endverbatim
  55. ******************************************************************************
  56. * @attention
  57. *
  58. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  59. *
  60. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  61. * You may not use this file except in compliance with the License.
  62. * You may obtain a copy of the License at:
  63. *
  64. * http://www.st.com/software_license_agreement_liberty_v2
  65. *
  66. * Unless required by applicable law or agreed to in writing, software
  67. * distributed under the License is distributed on an "AS IS" BASIS,
  68. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  69. * See the License for the specific language governing permissions and
  70. * limitations under the License.
  71. *
  72. ******************************************************************************
  73. */
  74. /* Includes ------------------------------------------------------------------*/
  75. #include "stm32f0xx_gpio.h"
  76. #include "stm32f0xx_rcc.h"
  77. /** @addtogroup STM32F0xx_StdPeriph_Driver
  78. * @{
  79. */
  80. /** @defgroup GPIO
  81. * @brief GPIO driver modules
  82. * @{
  83. */
  84. /* Private typedef -----------------------------------------------------------*/
  85. /* Private define ------------------------------------------------------------*/
  86. /* Private macro -------------------------------------------------------------*/
  87. /* Private variables ---------------------------------------------------------*/
  88. /* Private function prototypes -----------------------------------------------*/
  89. /* Private functions ---------------------------------------------------------*/
  90. /** @defgroup GPIO_Private_Functions
  91. * @{
  92. */
  93. /** @defgroup GPIO_Group1 Initialization and Configuration
  94. * @brief Initialization and Configuration
  95. *
  96. @verbatim
  97. ===============================================================================
  98. ##### Initialization and Configuration #####
  99. ===============================================================================
  100. @endverbatim
  101. * @{
  102. */
  103. /**
  104. * @brief Deinitializes the GPIOx peripheral registers to their default reset
  105. * values.
  106. * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
  107. * @note GPIOE is available only for STM32F072.
  108. * @note GPIOD is not available for STM32F031.
  109. * @retval None
  110. */
  111. void GPIO_DeInit(GPIO_TypeDef* GPIOx)
  112. {
  113. /* Check the parameters */
  114. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  115. if(GPIOx == GPIOA)
  116. {
  117. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  118. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA, DISABLE);
  119. }
  120. else if(GPIOx == GPIOB)
  121. {
  122. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  123. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB, DISABLE);
  124. }
  125. else if(GPIOx == GPIOC)
  126. {
  127. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOC, ENABLE);
  128. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOC, DISABLE);
  129. }
  130. else if(GPIOx == GPIOD)
  131. {
  132. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOD, ENABLE);
  133. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOD, DISABLE);
  134. }
  135. else if(GPIOx == GPIOE)
  136. {
  137. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOE, ENABLE);
  138. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOE, DISABLE);
  139. }
  140. else
  141. {
  142. if(GPIOx == GPIOF)
  143. {
  144. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOF, ENABLE);
  145. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOF, DISABLE);
  146. }
  147. }
  148. }
  149. /**
  150. * @brief Initializes the GPIOx peripheral according to the specified
  151. * parameters in the GPIO_InitStruct.
  152. * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
  153. * @note GPIOE is available only for STM32F072.
  154. * @note GPIOD is not available for STM32F031.
  155. * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that contains
  156. * the configuration information for the specified GPIO peripheral.
  157. * @retval None
  158. */
  159. void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
  160. {
  161. uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;
  162. /* Check the parameters */
  163. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  164. assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
  165. assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
  166. assert_param(IS_GPIO_PUPD(GPIO_InitStruct->GPIO_PuPd));
  167. /*-------------------------- Configure the port pins -----------------------*/
  168. /*-- GPIO Mode Configuration --*/
  169. for (pinpos = 0x00; pinpos < 0x10; pinpos++)
  170. {
  171. pos = ((uint32_t)0x01) << pinpos;
  172. /* Get the port pins position */
  173. currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
  174. if (currentpin == pos)
  175. {
  176. if ((GPIO_InitStruct->GPIO_Mode == GPIO_Mode_OUT) || (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_AF))
  177. {
  178. /* Check Speed mode parameters */
  179. assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
  180. /* Speed mode configuration */
  181. GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (pinpos * 2));
  182. GPIOx->OSPEEDR |= ((uint32_t)(GPIO_InitStruct->GPIO_Speed) << (pinpos * 2));
  183. /* Check Output mode parameters */
  184. assert_param(IS_GPIO_OTYPE(GPIO_InitStruct->GPIO_OType));
  185. /* Output mode configuration */
  186. GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)pinpos));
  187. GPIOx->OTYPER |= (uint16_t)(((uint16_t)GPIO_InitStruct->GPIO_OType) << ((uint16_t)pinpos));
  188. }
  189. GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (pinpos * 2));
  190. GPIOx->MODER |= (((uint32_t)GPIO_InitStruct->GPIO_Mode) << (pinpos * 2));
  191. /* Pull-up Pull down resistor configuration */
  192. GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)pinpos * 2));
  193. GPIOx->PUPDR |= (((uint32_t)GPIO_InitStruct->GPIO_PuPd) << (pinpos * 2));
  194. }
  195. }
  196. }
  197. /**
  198. * @brief Fills each GPIO_InitStruct member with its default value.
  199. * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure which will
  200. * be initialized.
  201. * @retval None
  202. */
  203. void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)
  204. {
  205. /* Reset GPIO init structure parameters values */
  206. GPIO_InitStruct->GPIO_Pin = GPIO_Pin_All;
  207. GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN;
  208. GPIO_InitStruct->GPIO_Speed = GPIO_Speed_Level_2;
  209. GPIO_InitStruct->GPIO_OType = GPIO_OType_PP;
  210. GPIO_InitStruct->GPIO_PuPd = GPIO_PuPd_NOPULL;
  211. }
  212. /**
  213. * @brief Locks GPIO Pins configuration registers.
  214. * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
  215. * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
  216. * @note The configuration of the locked GPIO pins can no longer be modified
  217. * until the next device reset.
  218. * @param GPIOx: where x can be (A or B) to select the GPIO peripheral.
  219. * @param GPIO_Pin: specifies the port bit to be written.
  220. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  221. * @retval None
  222. */
  223. void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  224. {
  225. __IO uint32_t tmp = 0x00010000;
  226. /* Check the parameters */
  227. assert_param(IS_GPIO_LIST_PERIPH(GPIOx));
  228. assert_param(IS_GPIO_PIN(GPIO_Pin));
  229. tmp |= GPIO_Pin;
  230. /* Set LCKK bit */
  231. GPIOx->LCKR = tmp;
  232. /* Reset LCKK bit */
  233. GPIOx->LCKR = GPIO_Pin;
  234. /* Set LCKK bit */
  235. GPIOx->LCKR = tmp;
  236. /* Read LCKK bit */
  237. tmp = GPIOx->LCKR;
  238. /* Read LCKK bit */
  239. tmp = GPIOx->LCKR;
  240. }
  241. /**
  242. * @}
  243. */
  244. /** @defgroup GPIO_Group2 GPIO Read and Write
  245. * @brief GPIO Read and Write
  246. *
  247. @verbatim
  248. ===============================================================================
  249. ##### GPIO Read and Write #####
  250. ===============================================================================
  251. @endverbatim
  252. * @{
  253. */
  254. /**
  255. * @brief Reads the specified input port pin.
  256. * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
  257. * @note GPIOE is available only for STM32F072.
  258. * @note GPIOD is not available for STM32F031.
  259. * @param GPIO_Pin: specifies the port bit to read.
  260. * @note This parameter can be GPIO_Pin_x where x can be:
  261. * For STM32F051 and STM32F030: (0..15) for GPIOA, GPIOB, GPIOC, (2) for GPIOD and (0..1, 4..7) for GIIOF.
  262. * For STM32F072: (0..15) for GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, (0..10) for GPIOF.
  263. * For STM32F031: (0..15) for GPIOA, GPIOB, (13..15) for GPIOC and (0..1, 6..7) for GPIOF.
  264. * @retval The input port pin value.
  265. */
  266. uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  267. {
  268. uint8_t bitstatus = 0x00;
  269. /* Check the parameters */
  270. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  271. assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
  272. if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)Bit_RESET)
  273. {
  274. bitstatus = (uint8_t)Bit_SET;
  275. }
  276. else
  277. {
  278. bitstatus = (uint8_t)Bit_RESET;
  279. }
  280. return bitstatus;
  281. }
  282. /**
  283. * @brief Reads the specified input port pin.
  284. * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
  285. * @note GPIOE is available only for STM32F072.
  286. * @note GPIOD is not available for STM32F031.
  287. * @retval The input port pin value.
  288. */
  289. uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx)
  290. {
  291. /* Check the parameters */
  292. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  293. return ((uint16_t)GPIOx->IDR);
  294. }
  295. /**
  296. * @brief Reads the specified output data port bit.
  297. * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
  298. * @note GPIOE is available only for STM32F072.
  299. * @note GPIOD is not available for STM32F031.
  300. * @param GPIO_Pin: Specifies the port bit to read.
  301. * @note This parameter can be GPIO_Pin_x where x can be:
  302. * For STM32F051 and STM32F030: (0..15) for GPIOA, GPIOB, GPIOC, (2) for GPIOD and (0..1, 4..7) for GIIOF.
  303. * For STM32F072: (0..15) for GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, (0..10) for GPIOF.
  304. * For STM32F031: (0..15) for GPIOA, GPIOB, (13..15) for GPIOC and (0..1, 6..7) for GPIOF.
  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, B, C, D, E or F) to select the GPIO peripheral.
  326. * @note GPIOE is available only for STM32F072.
  327. * @note GPIOD is not available for STM32F031.
  328. * @retval GPIO output data port value.
  329. */
  330. uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx)
  331. {
  332. /* Check the parameters */
  333. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  334. return ((uint16_t)GPIOx->ODR);
  335. }
  336. /**
  337. * @brief Sets the selected data port bits.
  338. * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
  339. * @note GPIOE is available only for STM32F072.
  340. * @note GPIOD is not available for STM32F031.
  341. * @param GPIO_Pin: specifies the port bits to be written.
  342. * @note This parameter can be GPIO_Pin_x where x can be:
  343. * For STM32F051 and STM32F030: (0..15) for GPIOA, GPIOB, GPIOC, (2) for GPIOD and (0..1, 4..7) for GIIOF.
  344. * For STM32F072: (0..15) for GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, (0..10) for GPIOF.
  345. * For STM32F031: (0..15) for GPIOA, GPIOB, (13..15) for GPIOC and (0..1, 6..7) for GPIOF.
  346. * @retval None
  347. */
  348. void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  349. {
  350. /* Check the parameters */
  351. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  352. assert_param(IS_GPIO_PIN(GPIO_Pin));
  353. GPIOx->BSRR = GPIO_Pin;
  354. }
  355. /**
  356. * @brief Clears the selected data port bits.
  357. * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
  358. * @note GPIOE is available only for STM32F072.
  359. * @note GPIOD is not available for STM32F031.
  360. * @param GPIO_Pin: specifies the port bits to be written.
  361. * @note This parameter can be GPIO_Pin_x where x can be:
  362. * For STM32F051 and STM32F030: (0..15) for GPIOA, GPIOB, GPIOC, (2) for GPIOD and (0..1, 4..7) for GIIOF.
  363. * For STM32F072: (0..15) for GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, (0..10) for GPIOF.
  364. * For STM32F031: (0..15) for GPIOA, GPIOB, (13..15) for GPIOC and (0..1, 6..7) for GPIOF.
  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->BRR = GPIO_Pin;
  373. }
  374. /**
  375. * @brief Sets or clears the selected data port bit.
  376. * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
  377. * @note GPIOE is available only for STM32F072.
  378. * @note GPIOD is not available for STM32F031.
  379. * @param GPIO_Pin: specifies the port bit to be written.
  380. * @param BitVal: specifies the value to be written to the selected bit.
  381. * This parameter can be one of the BitAction enumeration values:
  382. * @arg Bit_RESET: to clear the port pin
  383. * @arg Bit_SET: to set the port pin
  384. * @note This parameter can be GPIO_Pin_x where x can be:
  385. * For STM32F051 and STM32F030: (0..15) for GPIOA, GPIOB, GPIOC, (2) for GPIOD and (0..1, 4..7) for GIIOF.
  386. * For STM32F072: (0..15) for GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, (0..10) for GPIOF.
  387. * For STM32F031: (0..15) for GPIOA, GPIOB, (13..15) for GPIOC and (0..1, 6..7) for GPIOF.
  388. * @retval None
  389. */
  390. void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal)
  391. {
  392. /* Check the parameters */
  393. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  394. assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
  395. assert_param(IS_GPIO_BIT_ACTION(BitVal));
  396. if (BitVal != Bit_RESET)
  397. {
  398. GPIOx->BSRR = GPIO_Pin;
  399. }
  400. else
  401. {
  402. GPIOx->BRR = GPIO_Pin ;
  403. }
  404. }
  405. /**
  406. * @brief Writes data to the specified GPIO data port.
  407. * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
  408. * @note GPIOE is available only for STM32F072.
  409. * @note GPIOD is not available for STM32F031.
  410. * @param PortVal: specifies the value to be written to the port output data register.
  411. * @retval None
  412. */
  413. void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal)
  414. {
  415. /* Check the parameters */
  416. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  417. GPIOx->ODR = PortVal;
  418. }
  419. /**
  420. * @}
  421. */
  422. /** @defgroup GPIO_Group3 GPIO Alternate functions configuration functions
  423. * @brief GPIO Alternate functions configuration functions
  424. *
  425. @verbatim
  426. ===============================================================================
  427. ##### GPIO Alternate functions configuration functions #####
  428. ===============================================================================
  429. @endverbatim
  430. * @{
  431. */
  432. /**
  433. * @brief Writes data to the specified GPIO data port.
  434. * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
  435. * @note GPIOC, GPIOD, GPIOE and GPIOF are available only for STM32F072 and STM32F091.
  436. * @param GPIO_PinSource: specifies the pin for the Alternate function.
  437. * This parameter can be GPIO_PinSourcex where x can be (0..15) for GPIOA, GPIOB, GPIOD, GPIOE
  438. * and (0..12) for GPIOC and (0, 2..5, 9..10) for GPIOF.
  439. * @param GPIO_AF: selects the pin to used as Alternate function.
  440. * This parameter can be one of the following value:
  441. * @arg GPIO_AF_0: WKUP, EVENTOUT, TIM15, SPI1, TIM17, MCO, SWDAT, SWCLK,
  442. * TIM14, BOOT, USART1, CEC, IR_OUT, SPI2, TIM3, USART4,
  443. * CAN, USART2, CRS, TIM16, TIM1, TS, USART8
  444. * @arg GPIO_AF_1: USART2, CEC, TIM3, USART1, USART2, EVENTOUT, I2C1,
  445. * I2C2, TIM15, SPI2, USART3, TS, SPI1, USART7, USART8
  446. * USART5, USART4, USART6, I2C1
  447. * @arg GPIO_AF_2: TIM2, TIM1, EVENTOUT, TIM16, TIM17, USB, USART6, USART5,
  448. * USART8, USART7, USART6
  449. * @arg GPIO_AF_3: TS, I2C1, TIM15, EVENTOUT
  450. * @arg GPIO_AF_4: TIM14, USART4, USART3, CRS, CAN, I2C1, USART5
  451. * @arg GPIO_AF_5: TIM16, TIM17, TIM15, SPI2, I2C2, USART6, MCO
  452. * @arg GPIO_AF_6: EVENTOUT
  453. * @arg GPIO_AF_7: COMP1 OUT, COMP2 OUT
  454. * @note The pin should already been configured in Alternate Function mode(AF)
  455. * using GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
  456. * @note Refer to the Alternate function mapping table in the device datasheet
  457. * for the detailed mapping of the system and peripherals'alternate
  458. * function I/O pins.
  459. * @retval None
  460. */
  461. void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)
  462. {
  463. uint32_t temp = 0x00;
  464. uint32_t temp_2 = 0x00;
  465. /* Check the parameters */
  466. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  467. assert_param(IS_GPIO_PIN_SOURCE(GPIO_PinSource));
  468. assert_param(IS_GPIO_AF(GPIO_AF));
  469. temp = ((uint32_t)(GPIO_AF) << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4));
  470. GPIOx->AFR[GPIO_PinSource >> 0x03] &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4));
  471. temp_2 = GPIOx->AFR[GPIO_PinSource >> 0x03] | temp;
  472. GPIOx->AFR[GPIO_PinSource >> 0x03] = temp_2;
  473. }
  474. /**
  475. * @}
  476. */
  477. /**
  478. * @}
  479. */
  480. /**
  481. * @}
  482. */
  483. /**
  484. * @}
  485. */
  486. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/