Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

553 lines
19 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_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. @verbatim
  12. ==============================================================================
  13. ##### GPIO Peripheral features #####
  14. ==============================================================================
  15. [..]
  16. Each port bit of the general-purpose I/O (GPIO) ports can be individually
  17. configured by software in several modes:
  18. (+) Input mode
  19. (+) Analog mode
  20. (+) Output mode
  21. (+) Alternate function mode
  22. (+) External interrupt/event lines
  23. [..]
  24. During and just after reset, the alternate functions and external interrupt
  25. lines are not active and the I/O ports are configured in input floating mode.
  26. [..]
  27. All GPIO pins have weak internal pull-up and pull-down resistors, which can be
  28. activated or not.
  29. [..]
  30. In Output or Alternate mode, each IO can be configured on open-drain or push-pull
  31. type and the IO speed can be selected depending on the VDD value.
  32. [..]
  33. The microcontroller IO pins are connected to onboard peripherals/modules through a
  34. multiplexer that allows only one peripheral s alternate function (AF) connected
  35. to an IO pin at a time. In this way, there can be no conflict between peripherals
  36. sharing the same IO pin.
  37. [..]
  38. All ports have external interrupt/event capability. To use external interrupt
  39. lines, the port must be configured in input mode. All available GPIO pins are
  40. connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.
  41. [..]
  42. The external interrupt/event controller consists of up to 28 edge detectors
  43. (depending on products 16 lines are connected to GPIO) for generating event/interrupt
  44. requests (each input line can be independently configured to select the type
  45. (interrupt or event) and the corresponding trigger event (rising or falling or both).
  46. Each line can also be masked independently.
  47. ##### How to use this driver #####
  48. ==============================================================================
  49. [..]
  50. (#) Enable the GPIO AHB clock using the following function : __GPIOx_CLK_ENABLE().
  51. (#) Configure the GPIO pin(s) using HAL_GPIO_Init().
  52. (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure
  53. (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef
  54. structure.
  55. (++) In case of Output or alternate function mode selection: the speed is
  56. configured through "Speed" member from GPIO_InitTypeDef structure,
  57. the speed is configurable: Low, Medium and High.
  58. (++) If alternate mode is selected, the alternate function connected to the IO
  59. is configured through "Alternate" member from GPIO_InitTypeDef structure
  60. (++) Analog mode is required when a pin is to be used as ADC channel
  61. or DAC output.
  62. (++) In case of external interrupt/event selection the "Mode" member from
  63. GPIO_InitTypeDef structure select the type (interrupt or event) and
  64. the corresponding trigger event (rising or falling or both).
  65. (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
  66. mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
  67. HAL_NVIC_EnableIRQ().
  68. (#) HAL_GPIO_DeInit allows to set register values to their reset value. It's also
  69. recommended to use it to unconfigure pin which was used as an external interrupt
  70. or in event mode. That's the only way to reset corresponding bit in EXTI & SYSCFG
  71. registers.
  72. (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
  73. (#) To set/reset the level of a pin configured in output mode use
  74. HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
  75. (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
  76. (#) During and just after reset, the alternate functions are not
  77. active and the GPIO pins are configured in input floating mode (except JTAG
  78. pins).
  79. (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
  80. (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
  81. priority over the GPIO function.
  82. (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
  83. general purpose PH0 and PH1, respectively, when the HSE oscillator is off.
  84. The HSE has priority over the GPIO function.
  85. @endverbatim
  86. ******************************************************************************
  87. * @attention
  88. *
  89. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  90. * All rights reserved.</center></h2>
  91. *
  92. * This software component is licensed by ST under BSD 3-Clause license,
  93. * the "License"; You may not use this file except in compliance with the
  94. * License. You may obtain a copy of the License at:
  95. * opensource.org/licenses/BSD-3-Clause
  96. *
  97. ******************************************************************************
  98. */
  99. /* Includes ------------------------------------------------------------------*/
  100. #include "stm32l1xx_hal.h"
  101. /** @addtogroup STM32L1xx_HAL_Driver
  102. * @{
  103. */
  104. /** @addtogroup GPIO
  105. * @brief GPIO HAL module driver
  106. * @{
  107. */
  108. #ifdef HAL_GPIO_MODULE_ENABLED
  109. /* Private typedef -----------------------------------------------------------*/
  110. /* Private define ------------------------------------------------------------*/
  111. /** @addtogroup GPIO_Private_Constants
  112. * @{
  113. */
  114. #define GPIO_MODE (0x00000003U)
  115. #define EXTI_MODE (0x10000000U)
  116. #define GPIO_MODE_IT (0x00010000U)
  117. #define GPIO_MODE_EVT (0x00020000U)
  118. #define RISING_EDGE (0x00100000U)
  119. #define FALLING_EDGE (0x00200000U)
  120. #define GPIO_OUTPUT_TYPE (0x00000010U)
  121. #define GPIO_NUMBER (16U)
  122. /**
  123. * @}
  124. */
  125. /* Private macro -------------------------------------------------------------*/
  126. /* Private variables ---------------------------------------------------------*/
  127. /* Private function prototypes -----------------------------------------------*/
  128. /* Exported functions ---------------------------------------------------------*/
  129. /** @addtogroup GPIO_Exported_Functions
  130. * @{
  131. */
  132. /** @addtogroup GPIO_Exported_Functions_Group1
  133. * @brief Initialization and Configuration functions
  134. *
  135. @verbatim
  136. ===============================================================================
  137. ##### Initialization and Configuration functions #####
  138. ===============================================================================
  139. @endverbatim
  140. * @{
  141. */
  142. /**
  143. * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
  144. * @param GPIOx where x can be (A..G depending on device used) to select the GPIO peripheral for STM32L1XX family devices
  145. * @param GPIO_Init pointer to a GPIO_InitTypeDef structure that contains
  146. * the configuration information for the specified GPIO peripheral.
  147. * @retval None
  148. */
  149. void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
  150. {
  151. uint32_t position = 0x00;
  152. uint32_t iocurrent = 0x00;
  153. uint32_t temp = 0x00;
  154. /* Check the parameters */
  155. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  156. assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
  157. assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
  158. assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
  159. /* Configure the port pins */
  160. while (((GPIO_Init->Pin) >> position) != 0)
  161. {
  162. /* Get current io position */
  163. iocurrent = (GPIO_Init->Pin) & (1U << position);
  164. if(iocurrent)
  165. {
  166. /*--------------------- GPIO Mode Configuration ------------------------*/
  167. /* In case of Alternate function mode selection */
  168. if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
  169. {
  170. /* Check the Alternate function parameters */
  171. assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
  172. assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
  173. /* Configure Alternate function mapped with the current IO */
  174. /* Identify AFRL or AFRH register based on IO position*/
  175. temp = GPIOx->AFR[position >> 3];
  176. CLEAR_BIT(temp, 0xFU << ((uint32_t)(position & 0x07U) * 4)) ;
  177. SET_BIT(temp, (uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & 0x07U) * 4));
  178. GPIOx->AFR[position >> 3] = temp;
  179. }
  180. /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
  181. temp = GPIOx->MODER;
  182. CLEAR_BIT(temp, GPIO_MODER_MODER0 << (position * 2));
  183. SET_BIT(temp, (GPIO_Init->Mode & GPIO_MODE) << (position * 2));
  184. GPIOx->MODER = temp;
  185. /* In case of Output or Alternate function mode selection */
  186. if ((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
  187. (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
  188. {
  189. /* Check the Speed parameter */
  190. assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
  191. /* Configure the IO Speed */
  192. temp = GPIOx->OSPEEDR;
  193. CLEAR_BIT(temp, GPIO_OSPEEDER_OSPEEDR0 << (position * 2));
  194. SET_BIT(temp, GPIO_Init->Speed << (position * 2));
  195. GPIOx->OSPEEDR = temp;
  196. /* Configure the IO Output Type */
  197. temp = GPIOx->OTYPER;
  198. CLEAR_BIT(temp, GPIO_OTYPER_OT_0 << position) ;
  199. SET_BIT(temp, ((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4) << position);
  200. GPIOx->OTYPER = temp;
  201. }
  202. /* Activate the Pull-up or Pull down resistor for the current IO */
  203. temp = GPIOx->PUPDR;
  204. CLEAR_BIT(temp, GPIO_PUPDR_PUPDR0 << (position * 2));
  205. SET_BIT(temp, (GPIO_Init->Pull) << (position * 2));
  206. GPIOx->PUPDR = temp;
  207. /*--------------------- EXTI Mode Configuration ------------------------*/
  208. /* Configure the External Interrupt or event for the current IO */
  209. if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
  210. {
  211. /* Enable SYSCFG Clock */
  212. __HAL_RCC_SYSCFG_CLK_ENABLE();
  213. temp = SYSCFG->EXTICR[position >> 2];
  214. CLEAR_BIT(temp, (0x0FU) << (4 * (position & 0x03)));
  215. SET_BIT(temp, (GPIO_GET_INDEX(GPIOx)) << (4 * (position & 0x03)));
  216. SYSCFG->EXTICR[position >> 2] = temp;
  217. /* Clear EXTI line configuration */
  218. temp = EXTI->IMR;
  219. CLEAR_BIT(temp, (uint32_t)iocurrent);
  220. if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
  221. {
  222. SET_BIT(temp, iocurrent);
  223. }
  224. EXTI->IMR = temp;
  225. temp = EXTI->EMR;
  226. CLEAR_BIT(temp, (uint32_t)iocurrent);
  227. if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
  228. {
  229. SET_BIT(temp, iocurrent);
  230. }
  231. EXTI->EMR = temp;
  232. /* Clear Rising Falling edge configuration */
  233. temp = EXTI->RTSR;
  234. CLEAR_BIT(temp, (uint32_t)iocurrent);
  235. if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
  236. {
  237. SET_BIT(temp, iocurrent);
  238. }
  239. EXTI->RTSR = temp;
  240. temp = EXTI->FTSR;
  241. CLEAR_BIT(temp, (uint32_t)iocurrent);
  242. if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
  243. {
  244. SET_BIT(temp, iocurrent);
  245. }
  246. EXTI->FTSR = temp;
  247. }
  248. }
  249. position++;
  250. }
  251. }
  252. /**
  253. * @brief De-initializes the GPIOx peripheral registers to their default reset values.
  254. * @param GPIOx where x can be (A..G depending on device used) to select the GPIO peripheral for STM32L1XX family devices
  255. * @param GPIO_Pin specifies the port bit to be written.
  256. * This parameter can be one of GPIO_PIN_x where x can be (0..15).
  257. * @retval None
  258. */
  259. void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
  260. {
  261. uint32_t position = 0x00;
  262. uint32_t iocurrent = 0x00;
  263. uint32_t tmp = 0x00;
  264. /* Check the parameters */
  265. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  266. assert_param(IS_GPIO_PIN(GPIO_Pin));
  267. /* Configure the port pins */
  268. while ((GPIO_Pin >> position) != 0)
  269. {
  270. /* Get current io position */
  271. iocurrent = (GPIO_Pin) & (1U << position);
  272. if (iocurrent)
  273. {
  274. /*------------------------- EXTI Mode Configuration --------------------*/
  275. /* Clear the External Interrupt or Event for the current IO */
  276. tmp = SYSCFG->EXTICR[position >> 2];
  277. tmp &= ((0x0FU) << (4 * (position & 0x03)));
  278. if(tmp == (GPIO_GET_INDEX(GPIOx) << (4 * (position & 0x03))))
  279. {
  280. tmp = (0x0FU) << (4 * (position & 0x03));
  281. CLEAR_BIT(SYSCFG->EXTICR[position >> 2], tmp);
  282. /* Clear EXTI line configuration */
  283. CLEAR_BIT(EXTI->IMR, (uint32_t)iocurrent);
  284. CLEAR_BIT(EXTI->EMR, (uint32_t)iocurrent);
  285. /* Clear Rising Falling edge configuration */
  286. CLEAR_BIT(EXTI->RTSR, (uint32_t)iocurrent);
  287. CLEAR_BIT(EXTI->FTSR, (uint32_t)iocurrent);
  288. }
  289. /*------------------------- GPIO Mode Configuration --------------------*/
  290. /* Configure IO Direction in Input Floting Mode */
  291. CLEAR_BIT(GPIOx->MODER, GPIO_MODER_MODER0 << (position * 2));
  292. /* Configure the default Alternate Function in current IO */
  293. CLEAR_BIT(GPIOx->AFR[position >> 3], 0xFU << ((uint32_t)(position & 0x07U) * 4)) ;
  294. /* Configure the default value for IO Speed */
  295. CLEAR_BIT(GPIOx->OSPEEDR, GPIO_OSPEEDER_OSPEEDR0 << (position * 2));
  296. /* Configure the default value IO Output Type */
  297. CLEAR_BIT(GPIOx->OTYPER, GPIO_OTYPER_OT_0 << position) ;
  298. /* Deactivate the Pull-up oand Pull-down resistor for the current IO */
  299. CLEAR_BIT(GPIOx->PUPDR, GPIO_PUPDR_PUPDR0 << (position * 2));
  300. }
  301. position++;
  302. }
  303. }
  304. /**
  305. * @}
  306. */
  307. /** @addtogroup GPIO_Exported_Functions_Group2
  308. * @brief GPIO Read, Write, Toggle, Lock and EXTI management functions.
  309. *
  310. @verbatim
  311. ===============================================================================
  312. ##### IO operation functions #####
  313. ===============================================================================
  314. @endverbatim
  315. * @{
  316. */
  317. /**
  318. * @brief Reads the specified input port pin.
  319. * @param GPIOx where x can be (A..G depending on device used) to select the GPIO peripheral for STM32L1XX family devices
  320. * @param GPIO_Pin specifies the port bit to read.
  321. * This parameter can be GPIO_PIN_x where x can be (0..15).
  322. * @retval The input port pin value.
  323. */
  324. GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  325. {
  326. GPIO_PinState bitstatus;
  327. /* Check the parameters */
  328. assert_param(IS_GPIO_PIN(GPIO_Pin));
  329. if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
  330. {
  331. bitstatus = GPIO_PIN_SET;
  332. }
  333. else
  334. {
  335. bitstatus = GPIO_PIN_RESET;
  336. }
  337. return bitstatus;
  338. }
  339. /**
  340. * @brief Sets or clears the selected data port bit.
  341. * @note This function uses GPIOx_BSRR register to allow atomic read/modify
  342. * accesses. In this way, there is no risk of an IRQ occurring between
  343. * the read and the modify access.
  344. * @param GPIOx where x can be (A..G depending on device used) to select the GPIO peripheral for STM32L1XX family devices
  345. * @param GPIO_Pin specifies the port bit to be written.
  346. * This parameter can be one of GPIO_PIN_x where x can be (0..15).
  347. * @param PinState specifies the value to be written to the selected bit.
  348. * This parameter can be one of the GPIO_PinState enum values:
  349. * @arg GPIO_PIN_RESET: to clear the port pin
  350. * @arg GPIO_PIN_SET: to set the port pin
  351. * @retval None
  352. */
  353. void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
  354. {
  355. /* Check the parameters */
  356. assert_param(IS_GPIO_PIN(GPIO_Pin));
  357. assert_param(IS_GPIO_PIN_ACTION(PinState));
  358. if (PinState != GPIO_PIN_RESET)
  359. {
  360. GPIOx->BSRR = (uint32_t)GPIO_Pin;
  361. }
  362. else
  363. {
  364. GPIOx->BSRR = (uint32_t)GPIO_Pin << 16 ;
  365. }
  366. }
  367. /**
  368. * @brief Toggles the specified GPIO pin
  369. * @param GPIOx where x can be (A..G depending on device used) to select the GPIO peripheral for STM32L1XX family devices
  370. * @param GPIO_Pin specifies the pins to be toggled.
  371. * @retval None
  372. */
  373. void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  374. {
  375. /* Check the parameters */
  376. assert_param(IS_GPIO_PIN(GPIO_Pin));
  377. if ((GPIOx->ODR & GPIO_Pin) != 0x00u)
  378. {
  379. GPIOx->BSRR = (uint32_t)GPIO_Pin << GPIO_NUMBER;
  380. }
  381. else
  382. {
  383. GPIOx->BSRR = (uint32_t)GPIO_Pin;
  384. }
  385. }
  386. /**
  387. * @brief Locks GPIO Pins configuration registers.
  388. * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
  389. * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
  390. * @note The configuration of the locked GPIO pins can no longer be modified
  391. * until the next reset.
  392. * @note Limitation concerning GPIOx_OTYPER: Locking of GPIOx_OTYPER[i] with i = 15..8
  393. * depends from setting of GPIOx_LCKR[i-8] and not from GPIOx_LCKR[i].
  394. * GPIOx_LCKR[i-8] is locking GPIOx_OTYPER[i] together with GPIOx_OTYPER[i-8].
  395. * It is not possible to lock GPIOx_OTYPER[i] with i = 15..8, without locking also
  396. * GPIOx_OTYPER[i-8].
  397. * Workaround: When calling HAL_GPIO_LockPin with GPIO_Pin from GPIO_PIN_8 to GPIO_PIN_15,
  398. * you must call also HAL_GPIO_LockPin with GPIO_Pin - 8.
  399. * (When locking a pin from GPIO_PIN_8 to GPIO_PIN_15, you must lock also the corresponding
  400. * GPIO_PIN_0 to GPIO_PIN_7).
  401. * @param GPIOx where x can be (A..G depending on device used) to select the GPIO peripheral for STM32L1XX family devices
  402. * @param GPIO_Pin Specifies the port bit to be locked.
  403. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  404. * @retval None
  405. */
  406. HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  407. {
  408. __IO uint32_t tmp = GPIO_LCKR_LCKK;
  409. /* Check the parameters */
  410. assert_param(IS_GPIO_LOCK_INSTANCE(GPIOx));
  411. assert_param(IS_GPIO_PIN(GPIO_Pin));
  412. /* Apply lock key write sequence */
  413. SET_BIT(tmp, GPIO_Pin);
  414. /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
  415. GPIOx->LCKR = tmp;
  416. /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
  417. GPIOx->LCKR = GPIO_Pin;
  418. /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
  419. GPIOx->LCKR = tmp;
  420. /* Read LCKK register. This read is mandatory to complete key lock sequence */
  421. tmp = GPIOx->LCKR;
  422. /* Read again in order to confirm lock is active */
  423. if((GPIOx->LCKR & GPIO_LCKR_LCKK) != RESET)
  424. {
  425. return HAL_OK;
  426. }
  427. else
  428. {
  429. return HAL_ERROR;
  430. }
  431. }
  432. /**
  433. * @brief This function handles EXTI interrupt request.
  434. * @param GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
  435. * @retval None
  436. */
  437. void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
  438. {
  439. /* EXTI line interrupt detected */
  440. if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
  441. {
  442. __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
  443. HAL_GPIO_EXTI_Callback(GPIO_Pin);
  444. }
  445. }
  446. /**
  447. * @brief EXTI line detection callbacks.
  448. * @param GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
  449. * @retval None
  450. */
  451. __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  452. {
  453. /* Prevent unused argument(s) compilation warning */
  454. UNUSED(GPIO_Pin);
  455. /* NOTE : This function Should not be modified, when the callback is needed,
  456. the HAL_GPIO_EXTI_Callback could be implemented in the user file
  457. */
  458. }
  459. /**
  460. * @}
  461. */
  462. /**
  463. * @}
  464. */
  465. #endif /* HAL_GPIO_MODULE_ENABLED */
  466. /**
  467. * @}
  468. */
  469. /**
  470. * @}
  471. */
  472. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/