stm32f4xx_ll_gpio.c 11 KB

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