stm32g0xx_ll_gpio.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_ll_gpio.c
  4. * @author MCD Application Team
  5. * @brief GPIO LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2018 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 "stm32g0xx_ll_gpio.h"
  21. #include "stm32g0xx_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 STM32G0xx_LL_Driver
  28. * @{
  29. */
  30. #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF)
  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. #if defined(GPIOE)
  61. #define IS_LL_GPIO_ALTERNATE(__VALUE__) (((__VALUE__) == LL_GPIO_AF_0 ) ||\
  62. ((__VALUE__) == LL_GPIO_AF_1 ) ||\
  63. ((__VALUE__) == LL_GPIO_AF_2 ) ||\
  64. ((__VALUE__) == LL_GPIO_AF_3 ) ||\
  65. ((__VALUE__) == LL_GPIO_AF_4 ) ||\
  66. ((__VALUE__) == LL_GPIO_AF_5 ) ||\
  67. ((__VALUE__) == LL_GPIO_AF_6 ) ||\
  68. ((__VALUE__) == LL_GPIO_AF_7 ) ||\
  69. ((__VALUE__) == LL_GPIO_AF_8 ) ||\
  70. ((__VALUE__) == LL_GPIO_AF_9 ) ||\
  71. ((__VALUE__) == LL_GPIO_AF_10 ))
  72. #else
  73. #define IS_LL_GPIO_ALTERNATE(__VALUE__) (((__VALUE__) == LL_GPIO_AF_0 ) ||\
  74. ((__VALUE__) == LL_GPIO_AF_1 ) ||\
  75. ((__VALUE__) == LL_GPIO_AF_2 ) ||\
  76. ((__VALUE__) == LL_GPIO_AF_3 ) ||\
  77. ((__VALUE__) == LL_GPIO_AF_4 ) ||\
  78. ((__VALUE__) == LL_GPIO_AF_5 ) ||\
  79. ((__VALUE__) == LL_GPIO_AF_6 ) ||\
  80. ((__VALUE__) == LL_GPIO_AF_7 ))
  81. #endif /* GPIOE */
  82. /**
  83. * @}
  84. */
  85. /* Private function prototypes -----------------------------------------------*/
  86. /* Exported functions --------------------------------------------------------*/
  87. /** @addtogroup GPIO_LL_Exported_Functions
  88. * @{
  89. */
  90. /** @addtogroup GPIO_LL_EF_Init
  91. * @{
  92. */
  93. /**
  94. * @brief De-initialize GPIO registers (Registers restored to their default values).
  95. * @param GPIOx GPIO Port
  96. * @retval An ErrorStatus enumeration value:
  97. * - SUCCESS: GPIO registers are de-initialized
  98. * - ERROR: Wrong GPIO Port
  99. */
  100. ErrorStatus LL_GPIO_DeInit(GPIO_TypeDef *GPIOx)
  101. {
  102. ErrorStatus status = SUCCESS;
  103. /* Check the parameters */
  104. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  105. /* Force and Release reset on clock of GPIOx Port */
  106. if (GPIOx == GPIOA)
  107. {
  108. LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOA);
  109. LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOA);
  110. }
  111. else if (GPIOx == GPIOB)
  112. {
  113. LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOB);
  114. LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOB);
  115. }
  116. else if (GPIOx == GPIOC)
  117. {
  118. LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOC);
  119. LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOC);
  120. }
  121. #if defined(GPIOD)
  122. else if (GPIOx == GPIOD)
  123. {
  124. LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOD);
  125. LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOD);
  126. }
  127. #endif /* GPIOD */
  128. #if defined(GPIOE)
  129. else if (GPIOx == GPIOE)
  130. {
  131. LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOE);
  132. LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOE);
  133. }
  134. #endif /* GPIOE */
  135. #if defined(GPIOF)
  136. else if (GPIOx == GPIOF)
  137. {
  138. LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOF);
  139. LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOF);
  140. }
  141. #endif /* GPIOF */
  142. else
  143. {
  144. status = ERROR;
  145. }
  146. return (status);
  147. }
  148. /**
  149. * @brief Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
  150. * @param GPIOx GPIO Port
  151. * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
  152. * that contains the configuration information for the specified GPIO peripheral.
  153. * @retval An ErrorStatus enumeration value:
  154. * - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
  155. * - ERROR: Not applicable
  156. */
  157. ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, LL_GPIO_InitTypeDef *GPIO_InitStruct)
  158. {
  159. uint32_t pinpos;
  160. uint32_t currentpin;
  161. /* Check the parameters */
  162. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  163. assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
  164. assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
  165. assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
  166. /* ------------------------- Configure the port pins ---------------- */
  167. /* Initialize pinpos on first pin set */
  168. pinpos = 0;
  169. /* Configure the port pins */
  170. while (((GPIO_InitStruct->Pin) >> pinpos) != 0x00u)
  171. {
  172. /* Get current io position */
  173. currentpin = (GPIO_InitStruct->Pin) & (0x00000001uL << pinpos);
  174. if (currentpin != 0x00u)
  175. {
  176. if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
  177. {
  178. /* Check Speed mode parameters */
  179. assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
  180. /* Speed mode configuration */
  181. LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
  182. /* Check Output mode parameters */
  183. assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
  184. /* Output mode configuration*/
  185. LL_GPIO_SetPinOutputType(GPIOx, currentpin, GPIO_InitStruct->OutputType);
  186. }
  187. /* Pull-up Pull down resistor configuration*/
  188. LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
  189. if (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE)
  190. {
  191. /* Check Alternate parameter */
  192. assert_param(IS_LL_GPIO_ALTERNATE(GPIO_InitStruct->Alternate));
  193. /* Speed mode configuration */
  194. if (currentpin < LL_GPIO_PIN_8)
  195. {
  196. LL_GPIO_SetAFPin_0_7(GPIOx, currentpin, GPIO_InitStruct->Alternate);
  197. }
  198. else
  199. {
  200. LL_GPIO_SetAFPin_8_15(GPIOx, currentpin, GPIO_InitStruct->Alternate);
  201. }
  202. }
  203. /* Pin Mode configuration */
  204. LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
  205. }
  206. pinpos++;
  207. }
  208. return (SUCCESS);
  209. }
  210. /**
  211. * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
  212. * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
  213. * whose fields will be set to default values.
  214. * @retval None
  215. */
  216. void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
  217. {
  218. /* Reset GPIO init structure parameters values */
  219. GPIO_InitStruct->Pin = LL_GPIO_PIN_ALL;
  220. GPIO_InitStruct->Mode = LL_GPIO_MODE_ANALOG;
  221. GPIO_InitStruct->Speed = LL_GPIO_SPEED_FREQ_LOW;
  222. GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  223. GPIO_InitStruct->Pull = LL_GPIO_PULL_NO;
  224. GPIO_InitStruct->Alternate = LL_GPIO_AF_0;
  225. }
  226. /**
  227. * @}
  228. */
  229. /**
  230. * @}
  231. */
  232. /**
  233. * @}
  234. */
  235. #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) */
  236. /**
  237. * @}
  238. */
  239. #endif /* USE_FULL_LL_DRIVER */