stm32h7xx_ll_gpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_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 "stm32h7xx_ll_gpio.h"
  21. #include "stm32h7xx_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 STM32H7xx_LL_Driver
  28. * @{
  29. */
  30. #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) || defined (GPIOH) || defined (GPIOI) || defined (GPIOJ) || defined (GPIOK)
  31. /** @addtogroup GPIO_LL
  32. * @{
  33. */
  34. /* Private types -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private constants ---------------------------------------------------------*/
  37. /* Private macros ------------------------------------------------------------*/
  38. /** @addtogroup GPIO_LL_Private_Macros
  39. * @{
  40. */
  41. #define IS_LL_GPIO_PIN(__VALUE__) (((0x00000000U) < (__VALUE__)) && ((__VALUE__) <= (LL_GPIO_PIN_ALL)))
  42. #define IS_LL_GPIO_MODE(__VALUE__) (((__VALUE__) == LL_GPIO_MODE_INPUT) ||\
  43. ((__VALUE__) == LL_GPIO_MODE_OUTPUT) ||\
  44. ((__VALUE__) == LL_GPIO_MODE_ALTERNATE) ||\
  45. ((__VALUE__) == LL_GPIO_MODE_ANALOG))
  46. #define IS_LL_GPIO_OUTPUT_TYPE(__VALUE__) (((__VALUE__) == LL_GPIO_OUTPUT_PUSHPULL) ||\
  47. ((__VALUE__) == LL_GPIO_OUTPUT_OPENDRAIN))
  48. #define IS_LL_GPIO_SPEED(__VALUE__) (((__VALUE__) == LL_GPIO_SPEED_FREQ_LOW) ||\
  49. ((__VALUE__) == LL_GPIO_SPEED_FREQ_MEDIUM) ||\
  50. ((__VALUE__) == LL_GPIO_SPEED_FREQ_HIGH) ||\
  51. ((__VALUE__) == LL_GPIO_SPEED_FREQ_VERY_HIGH))
  52. #define IS_LL_GPIO_PULL(__VALUE__) (((__VALUE__) == LL_GPIO_PULL_NO) ||\
  53. ((__VALUE__) == LL_GPIO_PULL_UP) ||\
  54. ((__VALUE__) == LL_GPIO_PULL_DOWN))
  55. #define IS_LL_GPIO_ALTERNATE(__VALUE__) (((__VALUE__) == LL_GPIO_AF_0 ) ||\
  56. ((__VALUE__) == LL_GPIO_AF_1 ) ||\
  57. ((__VALUE__) == LL_GPIO_AF_2 ) ||\
  58. ((__VALUE__) == LL_GPIO_AF_3 ) ||\
  59. ((__VALUE__) == LL_GPIO_AF_4 ) ||\
  60. ((__VALUE__) == LL_GPIO_AF_5 ) ||\
  61. ((__VALUE__) == LL_GPIO_AF_6 ) ||\
  62. ((__VALUE__) == LL_GPIO_AF_7 ) ||\
  63. ((__VALUE__) == LL_GPIO_AF_8 ) ||\
  64. ((__VALUE__) == LL_GPIO_AF_9 ) ||\
  65. ((__VALUE__) == LL_GPIO_AF_10 ) ||\
  66. ((__VALUE__) == LL_GPIO_AF_11 ) ||\
  67. ((__VALUE__) == LL_GPIO_AF_12 ) ||\
  68. ((__VALUE__) == LL_GPIO_AF_13 ) ||\
  69. ((__VALUE__) == LL_GPIO_AF_14 ) ||\
  70. ((__VALUE__) == LL_GPIO_AF_15 ))
  71. /**
  72. * @}
  73. */
  74. /* Private function prototypes -----------------------------------------------*/
  75. /* Exported functions --------------------------------------------------------*/
  76. /** @addtogroup GPIO_LL_Exported_Functions
  77. * @{
  78. */
  79. /** @addtogroup GPIO_LL_EF_Init
  80. * @{
  81. */
  82. /**
  83. * @brief De-initialize GPIO registers (Registers restored to their default values).
  84. * @param GPIOx GPIO Port
  85. * @retval An ErrorStatus enumeration value:
  86. * - SUCCESS: GPIO registers are de-initialized
  87. * - ERROR: Wrong GPIO Port
  88. */
  89. ErrorStatus LL_GPIO_DeInit(GPIO_TypeDef *GPIOx)
  90. {
  91. ErrorStatus status = SUCCESS;
  92. /* Check the parameters */
  93. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  94. /* Force and Release reset on clock of GPIOx Port */
  95. if (GPIOx == GPIOA)
  96. {
  97. LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOA);
  98. LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOA);
  99. }
  100. else if (GPIOx == GPIOB)
  101. {
  102. LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOB);
  103. LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOB);
  104. }
  105. else if (GPIOx == GPIOC)
  106. {
  107. LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOC);
  108. LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOC);
  109. }
  110. #if defined(GPIOD)
  111. else if (GPIOx == GPIOD)
  112. {
  113. LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOD);
  114. LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOD);
  115. }
  116. #endif /* GPIOD */
  117. #if defined(GPIOE)
  118. else if (GPIOx == GPIOE)
  119. {
  120. LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOE);
  121. LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOE);
  122. }
  123. #endif /* GPIOE */
  124. #if defined(GPIOF)
  125. else if (GPIOx == GPIOF)
  126. {
  127. LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOF);
  128. LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOF);
  129. }
  130. #endif /* GPIOF */
  131. #if defined(GPIOG)
  132. else if (GPIOx == GPIOG)
  133. {
  134. LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOG);
  135. LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOG);
  136. }
  137. #endif /* GPIOG */
  138. #if defined(GPIOH)
  139. else if (GPIOx == GPIOH)
  140. {
  141. LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOH);
  142. LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOH);
  143. }
  144. #endif /* GPIOH */
  145. #if defined(GPIOI)
  146. else if (GPIOx == GPIOI)
  147. {
  148. LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOI);
  149. LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOI);
  150. }
  151. #endif /* GPIOI */
  152. #if defined(GPIOJ)
  153. else if (GPIOx == GPIOJ)
  154. {
  155. LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOJ);
  156. LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOJ);
  157. }
  158. #endif /* GPIOJ */
  159. #if defined(GPIOK)
  160. else if (GPIOx == GPIOK)
  161. {
  162. LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOK);
  163. LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOK);
  164. }
  165. #endif /* GPIOK */
  166. else
  167. {
  168. status = ERROR;
  169. }
  170. return (status);
  171. }
  172. /**
  173. * @brief Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
  174. * @param GPIOx GPIO Port
  175. * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
  176. * that contains the configuration information for the specified GPIO peripheral.
  177. * @retval An ErrorStatus enumeration value:
  178. * - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
  179. * - ERROR: Not applicable
  180. */
  181. ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, LL_GPIO_InitTypeDef *GPIO_InitStruct)
  182. {
  183. uint32_t pinpos, currentpin;
  184. /* Check the parameters */
  185. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  186. assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
  187. assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
  188. assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
  189. /* ------------------------- Configure the port pins ---------------- */
  190. /* Initialize pinpos on first pin set */
  191. pinpos = POSITION_VAL(GPIO_InitStruct->Pin);
  192. /* Configure the port pins */
  193. while (((GPIO_InitStruct->Pin) >> pinpos) != 0x00000000U)
  194. {
  195. /* Get current io position */
  196. currentpin = (GPIO_InitStruct->Pin) & (0x00000001UL << pinpos);
  197. if (currentpin != 0x00000000U)
  198. {
  199. if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
  200. {
  201. /* Check Speed mode parameters */
  202. assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
  203. /* Speed mode configuration */
  204. LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
  205. /* Check Output mode parameters */
  206. assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
  207. /* Output mode configuration*/
  208. LL_GPIO_SetPinOutputType(GPIOx, GPIO_InitStruct->Pin, GPIO_InitStruct->OutputType);
  209. }
  210. /* Pull-up Pull down resistor configuration*/
  211. LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
  212. if (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE)
  213. {
  214. /* Check Alternate parameter */
  215. assert_param(IS_LL_GPIO_ALTERNATE(GPIO_InitStruct->Alternate));
  216. /* Alternate function configuration */
  217. if (currentpin < LL_GPIO_PIN_8)
  218. {
  219. LL_GPIO_SetAFPin_0_7(GPIOx, currentpin, GPIO_InitStruct->Alternate);
  220. }
  221. else
  222. {
  223. LL_GPIO_SetAFPin_8_15(GPIOx, currentpin, GPIO_InitStruct->Alternate);
  224. }
  225. }
  226. /* Pin Mode configuration */
  227. LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
  228. }
  229. pinpos++;
  230. }
  231. return (SUCCESS);
  232. }
  233. /**
  234. * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
  235. * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
  236. * whose fields will be set to default values.
  237. * @retval None
  238. */
  239. void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
  240. {
  241. /* Reset GPIO init structure parameters values */
  242. GPIO_InitStruct->Pin = LL_GPIO_PIN_ALL;
  243. GPIO_InitStruct->Mode = LL_GPIO_MODE_ANALOG;
  244. GPIO_InitStruct->Speed = LL_GPIO_SPEED_FREQ_LOW;
  245. GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  246. GPIO_InitStruct->Pull = LL_GPIO_PULL_NO;
  247. GPIO_InitStruct->Alternate = LL_GPIO_AF_0;
  248. }
  249. /**
  250. * @}
  251. */
  252. /**
  253. * @}
  254. */
  255. /**
  256. * @}
  257. */
  258. #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) || defined (GPIOH) || defined (GPIOI) || defined (GPIOJ) || defined (GPIOK) */
  259. /**
  260. * @}
  261. */
  262. #endif /* USE_FULL_LL_DRIVER */