stm32f4xx_gpio.c 24 KB

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