stm32g4xx_ll_gpio.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. ******************************************************************************
  3. * @file stm32g4xx_ll_gpio.c
  4. * @author MCD Application Team
  5. * @brief GPIO LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2019 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 "stm32g4xx_ll_gpio.h"
  21. #include "stm32g4xx_ll_bus.h"
  22. #ifdef USE_FULL_ASSERT
  23. #include "stm32_assert.h"
  24. #else
  25. #define assert_param(expr) ((void)0U)
  26. #endif /* USE_FULL_ASSERT */
  27. /** @addtogroup STM32G4xx_LL_Driver
  28. * @{
  29. */
  30. #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG)
  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__) (((0x00000000U) < (__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. else if (GPIOx == GPIOD)
  116. {
  117. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOD);
  118. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOD);
  119. }
  120. else if (GPIOx == GPIOE)
  121. {
  122. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOE);
  123. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOE);
  124. }
  125. else if (GPIOx == GPIOF)
  126. {
  127. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOF);
  128. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOF);
  129. }
  130. else if (GPIOx == GPIOG)
  131. {
  132. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOG);
  133. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOG);
  134. }
  135. else
  136. {
  137. status = ERROR;
  138. }
  139. return (status);
  140. }
  141. /**
  142. * @brief Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
  143. * @param GPIOx GPIO Port
  144. * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
  145. * that contains the configuration information for the specified GPIO peripheral.
  146. * @retval An ErrorStatus enumeration value:
  147. * - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
  148. * - ERROR: Not applicable
  149. */
  150. ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, LL_GPIO_InitTypeDef *GPIO_InitStruct)
  151. {
  152. uint32_t pinpos;
  153. uint32_t currentpin;
  154. /* Check the parameters */
  155. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  156. assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
  157. assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
  158. assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
  159. /* ------------------------- Configure the port pins ---------------- */
  160. /* Initialize pinpos on first pin set */
  161. pinpos = POSITION_VAL(GPIO_InitStruct->Pin);
  162. /* Configure the port pins */
  163. while (((GPIO_InitStruct->Pin) >> pinpos) != 0x00000000U)
  164. {
  165. /* Get current io position */
  166. currentpin = (GPIO_InitStruct->Pin) & (0x00000001UL << pinpos);
  167. if (currentpin != 0x00u)
  168. {
  169. if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
  170. {
  171. /* Check Speed mode parameters */
  172. assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
  173. /* Speed mode configuration */
  174. LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
  175. /* Check Output mode parameters */
  176. assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
  177. /* Output mode configuration*/
  178. LL_GPIO_SetPinOutputType(GPIOx, GPIO_InitStruct->Pin, GPIO_InitStruct->OutputType);
  179. }
  180. /* Pull-up Pull down resistor configuration*/
  181. LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
  182. if (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE)
  183. {
  184. /* Check Alternate parameter */
  185. assert_param(IS_LL_GPIO_ALTERNATE(GPIO_InitStruct->Alternate));
  186. /* Speed mode configuration */
  187. if (currentpin < LL_GPIO_PIN_8)
  188. {
  189. LL_GPIO_SetAFPin_0_7(GPIOx, currentpin, GPIO_InitStruct->Alternate);
  190. }
  191. else
  192. {
  193. LL_GPIO_SetAFPin_8_15(GPIOx, currentpin, GPIO_InitStruct->Alternate);
  194. }
  195. }
  196. /* Pin Mode configuration */
  197. LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
  198. }
  199. pinpos++;
  200. }
  201. return (SUCCESS);
  202. }
  203. /**
  204. * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
  205. * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
  206. * whose fields will be set to default values.
  207. * @retval None
  208. */
  209. void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
  210. {
  211. /* Reset GPIO init structure parameters values */
  212. GPIO_InitStruct->Pin = LL_GPIO_PIN_ALL;
  213. GPIO_InitStruct->Mode = LL_GPIO_MODE_ANALOG;
  214. GPIO_InitStruct->Speed = LL_GPIO_SPEED_FREQ_LOW;
  215. GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  216. GPIO_InitStruct->Pull = LL_GPIO_PULL_NO;
  217. GPIO_InitStruct->Alternate = LL_GPIO_AF_0;
  218. }
  219. /**
  220. * @}
  221. */
  222. /**
  223. * @}
  224. */
  225. /**
  226. * @}
  227. */
  228. #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) */
  229. /**
  230. * @}
  231. */
  232. #endif /* USE_FULL_LL_DRIVER */