stm32wbxx_hal_gpio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_hal_gpio.c
  4. * @author MCD Application Team
  5. * @brief GPIO HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the General Purpose Input/Output (GPIO) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. ******************************************************************************
  11. * @attention
  12. *
  13. * Copyright (c) 2019 STMicroelectronics.
  14. * All rights reserved.
  15. *
  16. * This software is licensed under terms that can be found in the LICENSE file
  17. * in the root directory of this software component.
  18. * If no LICENSE file comes with this software, it is provided AS-IS.
  19. *
  20. ******************************************************************************
  21. @verbatim
  22. ==============================================================================
  23. ##### GPIO Peripheral features #####
  24. ==============================================================================
  25. [..]
  26. (+) Each port bit of the general-purpose I/O (GPIO) ports can be individually
  27. configured by software in several modes:
  28. (++) Input mode
  29. (++) Analog mode
  30. (++) Output mode
  31. (++) Alternate function mode
  32. (++) External interrupt/event lines
  33. (+) During and just after reset, the alternate functions and external interrupt
  34. lines are not active and the I/O ports are configured in input floating mode.
  35. (+) All GPIO pins have weak internal pull-up and pull-down resistors, which can be
  36. activated or not.
  37. (+) In Output or Alternate mode, each IO can be configured on open-drain or push-pull
  38. type and the IO speed can be selected depending on the VDD value.
  39. (+) The microcontroller IO pins are connected to onboard peripherals/modules through a
  40. multiplexer that allows only one peripheral alternate function (AF) connected
  41. to an IO pin at a time. In this way, there can be no conflict between peripherals
  42. sharing the same IO pin.
  43. (+) All ports have external interrupt/event capability. To use external interrupt
  44. lines, the port must be configured in input mode. All available GPIO pins are
  45. connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.
  46. (+) The external interrupt/event controller consists of up to 28 edge detectors
  47. (16 lines are connected to GPIO) for generating event/interrupt requests (each
  48. input line can be independently configured to select the type (interrupt or event)
  49. and the corresponding trigger event (rising or falling or both). Each line can
  50. also be masked independently.
  51. ##### How to use this driver #####
  52. ==============================================================================
  53. [..]
  54. (#) Enable the GPIO AHB clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE().
  55. (#) Configure the GPIO pin(s) using HAL_GPIO_Init().
  56. (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure
  57. (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef
  58. structure.
  59. (++) In case of Output or alternate function mode selection: the speed is
  60. configured through "Speed" member from GPIO_InitTypeDef structure.
  61. (++) In alternate mode is selection, the alternate function connected to the IO
  62. is configured through "Alternate" member from GPIO_InitTypeDef structure.
  63. (++) Analog mode is required when a pin is to be used as ADC channel
  64. or DAC output.
  65. (++) In case of external interrupt/event selection the "Mode" member from
  66. GPIO_InitTypeDef structure select the type (interrupt or event) and
  67. the corresponding trigger event (rising or falling or both).
  68. (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
  69. mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
  70. HAL_NVIC_EnableIRQ().
  71. (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
  72. (#) To set/reset the level of a pin configured in output mode use
  73. HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
  74. (#) To set the level of several pins and reset level of several other pins in
  75. same cycle, use HAL_GPIO_WriteMultipleStatePin().
  76. (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
  77. (#) During and just after reset, the alternate functions are not
  78. active and the GPIO pins are configured in input floating mode (except JTAG
  79. pins).
  80. (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
  81. (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
  82. priority over the GPIO function.
  83. (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
  84. general purpose PH0 and PH1, respectively, when the HSE oscillator is off.
  85. The HSE has priority over the GPIO function.
  86. @endverbatim
  87. ******************************************************************************
  88. */
  89. /* Includes ------------------------------------------------------------------*/
  90. #include "stm32wbxx_hal.h"
  91. /** @addtogroup STM32WBxx_HAL_Driver
  92. * @{
  93. */
  94. /** @addtogroup GPIO
  95. * @{
  96. */
  97. /** MISRA C:2012 deviation rule has been granted for following rules:
  98. * Rule-12.2 - Medium: RHS argument is in interval [0,INF] which is out of
  99. * range of the shift operator in following API :
  100. * HAL_GPIO_Init
  101. * HAL_GPIO_DeInit
  102. */
  103. #ifdef HAL_GPIO_MODULE_ENABLED
  104. /* Private typedef -----------------------------------------------------------*/
  105. /* Private defines ------------------------------------------------------------*/
  106. /** @addtogroup GPIO_Private_Constants GPIO Private Constants
  107. * @{
  108. */
  109. #define GPIO_NUMBER (16u)
  110. /**
  111. * @}
  112. */
  113. /* Private macros ------------------------------------------------------------*/
  114. /* Private variables ---------------------------------------------------------*/
  115. /* Private function prototypes -----------------------------------------------*/
  116. /* Exported functions --------------------------------------------------------*/
  117. /** @addtogroup GPIO_Exported_Functions
  118. * @{
  119. */
  120. /** @addtogroup GPIO_Exported_Functions_Group1
  121. * @brief Initialization and Configuration functions
  122. *
  123. @verbatim
  124. ===============================================================================
  125. ##### Initialization and de-initialization functions #####
  126. ===============================================================================
  127. @endverbatim
  128. * @{
  129. */
  130. /**
  131. * @brief Initialize the GPIOx peripheral according to the specified parameters in the GPIO_Init.
  132. * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32WBxx family
  133. * @param GPIO_Init pointer to a GPIO_InitTypeDef structure that contains
  134. * the configuration information for the specified GPIO peripheral.
  135. * @retval None
  136. */
  137. void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
  138. {
  139. uint32_t position = 0x00u;
  140. uint32_t iocurrent;
  141. uint32_t temp;
  142. /* Check the parameters */
  143. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  144. assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
  145. assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
  146. assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
  147. /* Configure the port pins */
  148. while (((GPIO_Init->Pin) >> position) != 0x00u)
  149. {
  150. /* Get current io position */
  151. iocurrent = (GPIO_Init->Pin) & (1uL << position);
  152. if (iocurrent != 0x00u)
  153. {
  154. /*--------------------- GPIO Mode Configuration ------------------------*/
  155. /* In case of Output or Alternate function mode selection */
  156. if (((GPIO_Init->Mode & GPIO_MODE) == MODE_OUTPUT) || ((GPIO_Init->Mode & GPIO_MODE) == MODE_AF))
  157. {
  158. /* Check the Speed parameter */
  159. assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
  160. /* Configure the IO Speed */
  161. temp = GPIOx->OSPEEDR;
  162. temp &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2u));
  163. temp |= (GPIO_Init->Speed << (position * 2u));
  164. GPIOx->OSPEEDR = temp;
  165. /* Configure the IO Output Type */
  166. temp = GPIOx->OTYPER;
  167. temp &= ~(GPIO_OTYPER_OT0 << position) ;
  168. temp |= (((GPIO_Init->Mode & OUTPUT_TYPE) >> OUTPUT_TYPE_Pos) << position);
  169. GPIOx->OTYPER = temp;
  170. }
  171. /* Activate the Pull-up or Pull down resistor for the current IO */
  172. if ((GPIO_Init->Mode & GPIO_MODE) != MODE_ANALOG)
  173. {
  174. temp = GPIOx->PUPDR;
  175. temp &= ~(GPIO_PUPDR_PUPD0 << (position * 2U));
  176. temp |= ((GPIO_Init->Pull) << (position * 2U));
  177. GPIOx->PUPDR = temp;
  178. }
  179. /* In case of Alternate function mode selection */
  180. if ((GPIO_Init->Mode & GPIO_MODE) == MODE_AF)
  181. {
  182. /* Check the Alternate function parameters */
  183. assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
  184. assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
  185. /* Configure Alternate function mapped with the current IO */
  186. temp = GPIOx->AFR[position >> 3u];
  187. temp &= ~(0xFu << ((position & 0x07u) * 4u));
  188. temp |= ((GPIO_Init->Alternate) << ((position & 0x07u) * 4u));
  189. GPIOx->AFR[position >> 3u] = temp;
  190. }
  191. /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
  192. temp = GPIOx->MODER;
  193. temp &= ~(GPIO_MODER_MODE0 << (position * 2u));
  194. temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2u));
  195. GPIOx->MODER = temp;
  196. /*--------------------- EXTI Mode Configuration ------------------------*/
  197. /* Configure the External Interrupt or event for the current IO */
  198. if ((GPIO_Init->Mode & EXTI_MODE) != 0x00u)
  199. {
  200. temp = SYSCFG->EXTICR[position >> 2u];
  201. temp &= ~(0x0FuL << (4u * (position & 0x03u)));
  202. temp |= (GPIO_GET_INDEX(GPIOx) << (4u * (position & 0x03u)));
  203. SYSCFG->EXTICR[position >> 2u] = temp;
  204. /* Clear Rising Falling edge configuration */
  205. temp = EXTI->RTSR1;
  206. temp &= ~(iocurrent);
  207. if ((GPIO_Init->Mode & TRIGGER_RISING) != 0x00u)
  208. {
  209. temp |= iocurrent;
  210. }
  211. EXTI->RTSR1 = temp;
  212. temp = EXTI->FTSR1;
  213. temp &= ~(iocurrent);
  214. if ((GPIO_Init->Mode & TRIGGER_FALLING) != 0x00u)
  215. {
  216. temp |= iocurrent;
  217. }
  218. EXTI->FTSR1 = temp;
  219. /* Clear EXTI line configuration */
  220. temp = EXTI->IMR1;
  221. temp &= ~(iocurrent);
  222. if ((GPIO_Init->Mode & EXTI_IT) != 0x00u)
  223. {
  224. temp |= iocurrent;
  225. }
  226. EXTI->IMR1 = temp;
  227. temp = EXTI->EMR1;
  228. temp &= ~(iocurrent);
  229. if ((GPIO_Init->Mode & EXTI_EVT) != 0x00u)
  230. {
  231. temp |= iocurrent;
  232. }
  233. EXTI->EMR1 = temp;
  234. }
  235. }
  236. position++;
  237. }
  238. }
  239. /**
  240. * @brief De-initialize the GPIOx peripheral registers to their default reset values.
  241. * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32WBxx family
  242. * @param GPIO_Pin specifies the port bit to be written.
  243. * This parameter can be one of GPIO_PIN_x where x can be (0..15).
  244. * @retval None
  245. */
  246. void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
  247. {
  248. uint32_t position = 0x00u;
  249. uint32_t iocurrent;
  250. uint32_t tmp;
  251. /* Check the parameters */
  252. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  253. assert_param(IS_GPIO_PIN(GPIO_Pin));
  254. /* Configure the port pins */
  255. while ((GPIO_Pin >> position) != 0x00u)
  256. {
  257. /* Get current io position */
  258. iocurrent = (GPIO_Pin) & (1uL << position);
  259. if (iocurrent != 0x00u)
  260. {
  261. /*------------------------- EXTI Mode Configuration --------------------*/
  262. /* Clear the External Interrupt or Event for the current IO */
  263. tmp = SYSCFG->EXTICR[position >> 2u];
  264. tmp &= (0x0FUL << (4u * (position & 0x03u)));
  265. if (tmp == (GPIO_GET_INDEX(GPIOx) << (4u * (position & 0x03u))))
  266. {
  267. /* Clear EXTI line configuration */
  268. EXTI->IMR1 &= ~(iocurrent);
  269. EXTI->EMR1 &= ~(iocurrent);
  270. /* Clear Rising Falling edge configuration */
  271. EXTI->RTSR1 &= ~(iocurrent);
  272. EXTI->FTSR1 &= ~(iocurrent);
  273. tmp = 0x0FuL << (4u * (position & 0x03u));
  274. SYSCFG->EXTICR[position >> 2u] &= ~tmp;
  275. }
  276. /*------------------------- GPIO Mode Configuration --------------------*/
  277. /* Configure IO in Analog Mode */
  278. GPIOx->MODER |= (GPIO_MODER_MODE0 << (position * 2u));
  279. /* Configure the default Alternate Function in current IO */
  280. GPIOx->AFR[position >> 3u] &= ~(0xFu << ((position & 0x07u) * 4u)) ;
  281. /* Deactivate the Pull-up and Pull-down resistor for the current IO */
  282. GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPD0 << (position * 2u));
  283. /* Configure the default value IO Output Type */
  284. GPIOx->OTYPER &= ~(GPIO_OTYPER_OT0 << position) ;
  285. /* Configure the default value for IO Speed */
  286. GPIOx->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2u));
  287. }
  288. position++;
  289. }
  290. }
  291. /**
  292. * @}
  293. */
  294. /** @addtogroup GPIO_Exported_Functions_Group2
  295. * @brief GPIO Read, Write, Toggle, Lock and EXTI management functions.
  296. *
  297. @verbatim
  298. ===============================================================================
  299. ##### IO operation functions #####
  300. ===============================================================================
  301. @endverbatim
  302. * @{
  303. */
  304. /**
  305. * @brief Read the specified input port pin.
  306. * @param GPIOx where x can be (A..F) to select the GPIO peripheral for STM32WBxx family
  307. * @param GPIO_Pin specifies the port bit to read.
  308. * This parameter can be GPIO_PIN_x where x can be (0..15).
  309. * @retval The input port pin value.
  310. */
  311. GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  312. {
  313. GPIO_PinState bitstatus;
  314. /* Check the parameters */
  315. assert_param(IS_GPIO_PIN(GPIO_Pin));
  316. if ((GPIOx->IDR & GPIO_Pin) != 0x00u)
  317. {
  318. bitstatus = GPIO_PIN_SET;
  319. }
  320. else
  321. {
  322. bitstatus = GPIO_PIN_RESET;
  323. }
  324. return bitstatus;
  325. }
  326. /**
  327. * @brief Set or clear the selected data port bit.
  328. * @note This function uses GPIOx_BSRR and GPIOx_BRR registers to allow atomic read/modify
  329. * accesses. In this way, there is no risk of an IRQ occurring between
  330. * the read and the modify access.
  331. * @param GPIOx where x can be (A..F) to select the GPIO peripheral for STM32WBxx family
  332. * @param GPIO_Pin specifies the port bit to be written.
  333. * This parameter can be one of GPIO_PIN_x where x can be (0..15).
  334. * @param PinState specifies the value to be written to the selected bit.
  335. * This parameter can be one of the GPIO_PinState enum values:
  336. * @arg GPIO_PIN_RESET: to clear the port pin
  337. * @arg GPIO_PIN_SET: to set the port pin
  338. * @retval None
  339. */
  340. void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
  341. {
  342. /* Check the parameters */
  343. assert_param(IS_GPIO_PIN(GPIO_Pin));
  344. assert_param(IS_GPIO_PIN_ACTION(PinState));
  345. if (PinState != GPIO_PIN_RESET)
  346. {
  347. GPIOx->BSRR = (uint32_t)GPIO_Pin;
  348. }
  349. else
  350. {
  351. GPIOx->BRR = (uint32_t)GPIO_Pin;
  352. }
  353. }
  354. /**
  355. * @brief Set and clear several pins of a dedicated port in same cycle.
  356. * @note This function uses GPIOx_BSRR and GPIOx_BRR registers to allow atomic read/modify
  357. * accesses.
  358. * @param GPIOx where x can be (A..F) to select the GPIO peripheral for STM32WLxx family
  359. * @param PinReset specifies the port bits to be reset
  360. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15) or zero.
  361. * @param PinSet specifies the port bits to be set
  362. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15) or zero.
  363. * @note Both PinReset and PinSet combinations shall not get any common bit, else
  364. * assert would be triggered.
  365. * @note At least one of the two parameters used to set or reset shall be different from zero.
  366. * @retval None
  367. */
  368. void HAL_GPIO_WriteMultipleStatePin(GPIO_TypeDef *GPIOx, uint16_t PinReset, uint16_t PinSet)
  369. {
  370. uint32_t tmp;
  371. /* Check the parameters */
  372. /* Make sure at least one parameter is different from zero and that there is no common pin */
  373. assert_param(IS_GPIO_PIN((uint32_t)PinReset | (uint32_t)PinSet));
  374. assert_param(IS_GPIO_COMMON_PIN(PinReset, PinSet));
  375. tmp = (((uint32_t)PinReset << 16) | PinSet);
  376. GPIOx->BSRR = tmp;
  377. }
  378. /**
  379. * @brief Toggle the specified GPIO pin.
  380. * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32WBxx family
  381. * @param GPIO_Pin specifies the pin to be toggled.
  382. * @retval None
  383. */
  384. void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  385. {
  386. uint32_t odr;
  387. /* Check the parameters */
  388. assert_param(IS_GPIO_PIN(GPIO_Pin));
  389. /* get current Output Data Register value */
  390. odr = GPIOx->ODR;
  391. /* Set selected pins that were at low level, and reset ones that were high */
  392. GPIOx->BSRR = ((odr & GPIO_Pin) << GPIO_NUMBER) | (~odr & GPIO_Pin);
  393. }
  394. /**
  395. * @brief Lock GPIO Pins configuration registers.
  396. * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
  397. * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
  398. * @note The configuration of the locked GPIO pins can no longer be modified
  399. * until the next reset.
  400. * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32WBxx family
  401. * @param GPIO_Pin specifies the port bits to be locked.
  402. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  403. * @retval None
  404. */
  405. HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  406. {
  407. __IO uint32_t tmp = GPIO_LCKR_LCKK;
  408. /* Check the parameters */
  409. assert_param(IS_GPIO_LOCK_INSTANCE(GPIOx));
  410. assert_param(IS_GPIO_PIN(GPIO_Pin));
  411. /* Apply lock key write sequence */
  412. tmp |= GPIO_Pin;
  413. /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
  414. GPIOx->LCKR = tmp;
  415. /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
  416. GPIOx->LCKR = GPIO_Pin;
  417. /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
  418. GPIOx->LCKR = tmp;
  419. /* Read LCKK register. This read is mandatory to complete key lock sequence */
  420. tmp = GPIOx->LCKR;
  421. /* read again in order to confirm lock is active */
  422. if ((GPIOx->LCKR & GPIO_LCKR_LCKK) != 0x00u)
  423. {
  424. return HAL_OK;
  425. }
  426. else
  427. {
  428. return HAL_ERROR;
  429. }
  430. }
  431. /**
  432. * @brief Handle EXTI interrupt request.
  433. * @param GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
  434. * @retval None
  435. */
  436. void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
  437. {
  438. /* EXTI line interrupt detected */
  439. if (__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != 0x00u)
  440. {
  441. __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
  442. HAL_GPIO_EXTI_Callback(GPIO_Pin);
  443. }
  444. }
  445. /**
  446. * @brief EXTI line detection callback.
  447. * @param GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
  448. * @retval None
  449. */
  450. __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  451. {
  452. /* Prevent unused argument(s) compilation warning */
  453. UNUSED(GPIO_Pin);
  454. /* NOTE: This function should not be modified, when the callback is needed,
  455. the HAL_GPIO_EXTI_Callback could be implemented in the user file
  456. */
  457. }
  458. /**
  459. * @}
  460. */
  461. /**
  462. * @}
  463. */
  464. #endif /* HAL_GPIO_MODULE_ENABLED */
  465. /**
  466. * @}
  467. */
  468. /**
  469. * @}
  470. */