You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32l1xx_ll_gpio.c 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_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 "stm32l1xx_ll_gpio.h"
  22. #include "stm32l1xx_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 STM32L1xx_LL_Driver
  29. * @{
  30. */
  31. #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) || defined (GPIOH)
  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. else
  147. {
  148. status = ERROR;
  149. }
  150. return (status);
  151. }
  152. /**
  153. * @brief Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
  154. * @param GPIOx GPIO Port
  155. * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
  156. * that contains the configuration information for the specified GPIO peripheral.
  157. * @retval An ErrorStatus enumeration value:
  158. * - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
  159. * - ERROR: Not applicable
  160. */
  161. ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, LL_GPIO_InitTypeDef *GPIO_InitStruct)
  162. {
  163. uint32_t pinpos = 0x00000000U;
  164. uint32_t currentpin = 0x00000000U;
  165. /* Check the parameters */
  166. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  167. assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
  168. assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
  169. assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
  170. /* ------------------------- Configure the port pins ---------------- */
  171. /* Initialize pinpos on first pin set */
  172. pinpos = POSITION_VAL(GPIO_InitStruct->Pin);
  173. /* Configure the port pins */
  174. while (((GPIO_InitStruct->Pin) >> pinpos) != 0x00000000U)
  175. {
  176. /* Get current io position */
  177. currentpin = (GPIO_InitStruct->Pin) & (0x00000001U << pinpos);
  178. if (currentpin)
  179. {
  180. /* Pin Mode configuration */
  181. LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
  182. if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
  183. {
  184. /* Check Speed mode parameters */
  185. assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
  186. /* Speed mode configuration */
  187. LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
  188. }
  189. /* Pull-up Pull down resistor configuration*/
  190. LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
  191. if (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE)
  192. {
  193. /* Check Alternate parameter */
  194. assert_param(IS_LL_GPIO_ALTERNATE(GPIO_InitStruct->Alternate));
  195. /* Speed mode configuration */
  196. if (POSITION_VAL(currentpin) < 0x00000008U)
  197. {
  198. LL_GPIO_SetAFPin_0_7(GPIOx, currentpin, GPIO_InitStruct->Alternate);
  199. }
  200. else
  201. {
  202. LL_GPIO_SetAFPin_8_15(GPIOx, currentpin, GPIO_InitStruct->Alternate);
  203. }
  204. }
  205. }
  206. pinpos++;
  207. }
  208. if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
  209. {
  210. /* Check Output mode parameters */
  211. assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
  212. /* Output mode configuration*/
  213. LL_GPIO_SetPinOutputType(GPIOx, GPIO_InitStruct->Pin, GPIO_InitStruct->OutputType);
  214. }
  215. return (SUCCESS);
  216. }
  217. /**
  218. * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
  219. * @param GPIO_InitStruct: pointer to a @ref LL_GPIO_InitTypeDef structure
  220. * whose fields will be set to default values.
  221. * @retval None
  222. */
  223. void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
  224. {
  225. /* Reset GPIO init structure parameters values */
  226. GPIO_InitStruct->Pin = LL_GPIO_PIN_ALL;
  227. GPIO_InitStruct->Mode = LL_GPIO_MODE_ANALOG;
  228. GPIO_InitStruct->Speed = LL_GPIO_SPEED_FREQ_LOW;
  229. GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  230. GPIO_InitStruct->Pull = LL_GPIO_PULL_NO;
  231. GPIO_InitStruct->Alternate = LL_GPIO_AF_0;
  232. }
  233. /**
  234. * @}
  235. */
  236. /**
  237. * @}
  238. */
  239. /**
  240. * @}
  241. */
  242. #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) || defined (GPIOH) */
  243. /**
  244. * @}
  245. */
  246. #endif /* USE_FULL_LL_DRIVER */
  247. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/