stm32g0xx_ll_gpio.c 9.8 KB

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