stm32l4xx_ll_gpio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_ll_gpio.c
  4. * @author MCD Application Team
  5. * @brief GPIO LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2017 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. #if defined(USE_FULL_LL_DRIVER)
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "stm32l4xx_ll_gpio.h"
  21. #include "stm32l4xx_ll_bus.h"
  22. #ifdef USE_FULL_ASSERT
  23. #include "stm32_assert.h"
  24. #else
  25. #define assert_param(expr) ((void)0U)
  26. #endif
  27. /** @addtogroup STM32L4xx_LL_Driver
  28. * @{
  29. */
  30. #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) || defined (GPIOH) || defined (GPIOI)
  31. /** @addtogroup GPIO_LL
  32. * @{
  33. */
  34. /** MISRA C:2012 deviation rule has been granted for following rules:
  35. * Rule-12.2 - Medium: RHS argument is in interval [0,INF] which is out of
  36. * range of the shift operator in following API :
  37. * LL_GPIO_Init
  38. */
  39. /* Private types -------------------------------------------------------------*/
  40. /* Private variables ---------------------------------------------------------*/
  41. /* Private constants ---------------------------------------------------------*/
  42. /* Private macros ------------------------------------------------------------*/
  43. /** @addtogroup GPIO_LL_Private_Macros
  44. * @{
  45. */
  46. #define IS_LL_GPIO_PIN(__VALUE__) (((0x00u) < (__VALUE__)) && ((__VALUE__) <= (LL_GPIO_PIN_ALL)))
  47. #define IS_LL_GPIO_MODE(__VALUE__) (((__VALUE__) == LL_GPIO_MODE_INPUT) ||\
  48. ((__VALUE__) == LL_GPIO_MODE_OUTPUT) ||\
  49. ((__VALUE__) == LL_GPIO_MODE_ALTERNATE) ||\
  50. ((__VALUE__) == LL_GPIO_MODE_ANALOG))
  51. #define IS_LL_GPIO_OUTPUT_TYPE(__VALUE__) (((__VALUE__) == LL_GPIO_OUTPUT_PUSHPULL) ||\
  52. ((__VALUE__) == LL_GPIO_OUTPUT_OPENDRAIN))
  53. #define IS_LL_GPIO_SPEED(__VALUE__) (((__VALUE__) == LL_GPIO_SPEED_FREQ_LOW) ||\
  54. ((__VALUE__) == LL_GPIO_SPEED_FREQ_MEDIUM) ||\
  55. ((__VALUE__) == LL_GPIO_SPEED_FREQ_HIGH) ||\
  56. ((__VALUE__) == LL_GPIO_SPEED_FREQ_VERY_HIGH))
  57. #define IS_LL_GPIO_PULL(__VALUE__) (((__VALUE__) == LL_GPIO_PULL_NO) ||\
  58. ((__VALUE__) == LL_GPIO_PULL_UP) ||\
  59. ((__VALUE__) == LL_GPIO_PULL_DOWN))
  60. #define IS_LL_GPIO_ALTERNATE(__VALUE__) (((__VALUE__) == LL_GPIO_AF_0 ) ||\
  61. ((__VALUE__) == LL_GPIO_AF_1 ) ||\
  62. ((__VALUE__) == LL_GPIO_AF_2 ) ||\
  63. ((__VALUE__) == LL_GPIO_AF_3 ) ||\
  64. ((__VALUE__) == LL_GPIO_AF_4 ) ||\
  65. ((__VALUE__) == LL_GPIO_AF_5 ) ||\
  66. ((__VALUE__) == LL_GPIO_AF_6 ) ||\
  67. ((__VALUE__) == LL_GPIO_AF_7 ) ||\
  68. ((__VALUE__) == LL_GPIO_AF_8 ) ||\
  69. ((__VALUE__) == LL_GPIO_AF_9 ) ||\
  70. ((__VALUE__) == LL_GPIO_AF_10 ) ||\
  71. ((__VALUE__) == LL_GPIO_AF_11 ) ||\
  72. ((__VALUE__) == LL_GPIO_AF_12 ) ||\
  73. ((__VALUE__) == LL_GPIO_AF_13 ) ||\
  74. ((__VALUE__) == LL_GPIO_AF_14 ) ||\
  75. ((__VALUE__) == LL_GPIO_AF_15 ))
  76. /**
  77. * @}
  78. */
  79. /* Private function prototypes -----------------------------------------------*/
  80. /* Exported functions --------------------------------------------------------*/
  81. /** @addtogroup GPIO_LL_Exported_Functions
  82. * @{
  83. */
  84. /** @addtogroup GPIO_LL_EF_Init
  85. * @{
  86. */
  87. /**
  88. * @brief De-initialize GPIO registers (Registers restored to their default values).
  89. * @param GPIOx GPIO Port
  90. * @retval An ErrorStatus enumeration value:
  91. * - SUCCESS: GPIO registers are de-initialized
  92. * - ERROR: Wrong GPIO Port
  93. */
  94. ErrorStatus LL_GPIO_DeInit(GPIO_TypeDef *GPIOx)
  95. {
  96. ErrorStatus status = SUCCESS;
  97. /* Check the parameters */
  98. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  99. /* Force and Release reset on clock of GPIOx Port */
  100. if (GPIOx == GPIOA)
  101. {
  102. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOA);
  103. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOA);
  104. }
  105. else if (GPIOx == GPIOB)
  106. {
  107. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOB);
  108. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOB);
  109. }
  110. else if (GPIOx == GPIOC)
  111. {
  112. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOC);
  113. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOC);
  114. }
  115. #if defined(GPIOD)
  116. else if (GPIOx == GPIOD)
  117. {
  118. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOD);
  119. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOD);
  120. }
  121. #endif /* GPIOD */
  122. #if defined(GPIOE)
  123. else if (GPIOx == GPIOE)
  124. {
  125. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOE);
  126. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOE);
  127. }
  128. #endif /* GPIOE */
  129. #if defined(GPIOF)
  130. else if (GPIOx == GPIOF)
  131. {
  132. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOF);
  133. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOF);
  134. }
  135. #endif /* GPIOF */
  136. #if defined(GPIOG)
  137. else if (GPIOx == GPIOG)
  138. {
  139. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOG);
  140. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOG);
  141. }
  142. #endif /* GPIOG */
  143. #if defined(GPIOH)
  144. else if (GPIOx == GPIOH)
  145. {
  146. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOH);
  147. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOH);
  148. }
  149. #endif /* GPIOH */
  150. #if defined(GPIOI)
  151. else if (GPIOx == GPIOI)
  152. {
  153. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOI);
  154. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOI);
  155. }
  156. #endif /* GPIOI */
  157. else
  158. {
  159. status = ERROR;
  160. }
  161. return (status);
  162. }
  163. /**
  164. * @brief Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
  165. * @param GPIOx GPIO Port
  166. * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
  167. * that contains the configuration information for the specified GPIO peripheral.
  168. * @retval An ErrorStatus enumeration value:
  169. * - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
  170. * - ERROR: Not applicable
  171. */
  172. ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, LL_GPIO_InitTypeDef *GPIO_InitStruct)
  173. {
  174. uint32_t pinpos;
  175. uint32_t currentpin;
  176. /* Check the parameters */
  177. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  178. assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
  179. assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
  180. assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
  181. /* ------------------------- Configure the port pins ---------------- */
  182. /* Initialize pinpos on first pin set */
  183. pinpos = POSITION_VAL(GPIO_InitStruct->Pin);
  184. /* Configure the port pins */
  185. while (((GPIO_InitStruct->Pin) >> pinpos) != 0x00u)
  186. {
  187. /* Get current io position */
  188. currentpin = (GPIO_InitStruct->Pin) & (0x00000001uL << pinpos);
  189. if (currentpin != 0x00u)
  190. {
  191. if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
  192. {
  193. /* Check Speed mode parameters */
  194. assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
  195. /* Speed mode configuration */
  196. LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
  197. /* Check Output mode parameters */
  198. assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
  199. /* Output mode configuration*/
  200. LL_GPIO_SetPinOutputType(GPIOx, GPIO_InitStruct->Pin, GPIO_InitStruct->OutputType);
  201. }
  202. /* Pull-up Pull down resistor configuration*/
  203. LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
  204. if (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE)
  205. {
  206. /* Check Alternate parameter */
  207. assert_param(IS_LL_GPIO_ALTERNATE(GPIO_InitStruct->Alternate));
  208. /* Speed mode configuration */
  209. if (currentpin < LL_GPIO_PIN_8)
  210. {
  211. LL_GPIO_SetAFPin_0_7(GPIOx, currentpin, GPIO_InitStruct->Alternate);
  212. }
  213. else
  214. {
  215. LL_GPIO_SetAFPin_8_15(GPIOx, currentpin, GPIO_InitStruct->Alternate);
  216. }
  217. }
  218. /* Pin Mode configuration */
  219. LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
  220. }
  221. pinpos++;
  222. }
  223. return (SUCCESS);
  224. }
  225. /**
  226. * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
  227. * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
  228. * whose fields will be set to default values.
  229. * @retval None
  230. */
  231. void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
  232. {
  233. /* Reset GPIO init structure parameters values */
  234. GPIO_InitStruct->Pin = LL_GPIO_PIN_ALL;
  235. GPIO_InitStruct->Mode = LL_GPIO_MODE_ANALOG;
  236. GPIO_InitStruct->Speed = LL_GPIO_SPEED_FREQ_LOW;
  237. GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  238. GPIO_InitStruct->Pull = LL_GPIO_PULL_NO;
  239. GPIO_InitStruct->Alternate = LL_GPIO_AF_0;
  240. }
  241. /**
  242. * @}
  243. */
  244. /**
  245. * @}
  246. */
  247. /**
  248. * @}
  249. */
  250. #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) || defined (GPIOH) || defined (GPIOI) */
  251. /**
  252. * @}
  253. */
  254. #endif /* USE_FULL_LL_DRIVER */