stm32f1xx_ll_gpio.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_ll_gpio.c
  4. * @author MCD Application Team
  5. * @brief GPIO LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2016 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 "stm32f1xx_ll_gpio.h"
  22. #include "stm32f1xx_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 STM32F1xx_LL_Driver
  29. * @{
  30. */
  31. #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG)
  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__) ((((__VALUE__) & LL_GPIO_PIN_ALL)!= 0u) &&\
  43. (((__VALUE__) & (~LL_GPIO_PIN_ALL))== 0u))
  44. #define IS_LL_GPIO_MODE(__VALUE__) (((__VALUE__) == LL_GPIO_MODE_ANALOG) ||\
  45. ((__VALUE__) == LL_GPIO_MODE_FLOATING) ||\
  46. ((__VALUE__) == LL_GPIO_MODE_INPUT) ||\
  47. ((__VALUE__) == LL_GPIO_MODE_OUTPUT) ||\
  48. ((__VALUE__) == LL_GPIO_MODE_ALTERNATE))
  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. #define IS_LL_GPIO_OUTPUT_TYPE(__VALUE__) (((__VALUE__) == LL_GPIO_OUTPUT_PUSHPULL) ||\
  53. ((__VALUE__) == LL_GPIO_OUTPUT_OPENDRAIN))
  54. #define IS_LL_GPIO_PULL(__VALUE__) (((__VALUE__) == LL_GPIO_PULL_DOWN) ||\
  55. ((__VALUE__) == LL_GPIO_PULL_UP))
  56. /**
  57. * @}
  58. */
  59. /* Private function prototypes -----------------------------------------------*/
  60. /* Exported functions --------------------------------------------------------*/
  61. /** @addtogroup GPIO_LL_Exported_Functions
  62. * @{
  63. */
  64. /** @addtogroup GPIO_LL_EF_Init
  65. * @{
  66. */
  67. /**
  68. * @brief De-initialize GPIO registers (Registers restored to their default values).
  69. * @param GPIOx GPIO Port
  70. * @retval An ErrorStatus enumeration value:
  71. * - SUCCESS: GPIO registers are de-initialized
  72. * - ERROR: Wrong GPIO Port
  73. */
  74. ErrorStatus LL_GPIO_DeInit(GPIO_TypeDef *GPIOx)
  75. {
  76. ErrorStatus status = SUCCESS;
  77. /* Check the parameters */
  78. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  79. /* Force and Release reset on clock of GPIOx Port */
  80. if (GPIOx == GPIOA)
  81. {
  82. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_GPIOA);
  83. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_GPIOA);
  84. }
  85. else if (GPIOx == GPIOB)
  86. {
  87. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_GPIOB);
  88. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_GPIOB);
  89. }
  90. else if (GPIOx == GPIOC)
  91. {
  92. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_GPIOC);
  93. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_GPIOC);
  94. }
  95. else if (GPIOx == GPIOD)
  96. {
  97. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_GPIOD);
  98. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_GPIOD);
  99. }
  100. #if defined(GPIOE)
  101. else if (GPIOx == GPIOE)
  102. {
  103. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_GPIOE);
  104. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_GPIOE);
  105. }
  106. #endif
  107. #if defined(GPIOF)
  108. else if (GPIOx == GPIOF)
  109. {
  110. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_GPIOF);
  111. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_GPIOF);
  112. }
  113. #endif
  114. #if defined(GPIOG)
  115. else if (GPIOx == GPIOG)
  116. {
  117. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_GPIOG);
  118. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_GPIOG);
  119. }
  120. #endif
  121. else
  122. {
  123. status = ERROR;
  124. }
  125. return (status);
  126. }
  127. /**
  128. * @brief Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
  129. * @param GPIOx GPIO Port
  130. * @param GPIO_InitStruct: pointer to a @ref LL_GPIO_InitTypeDef structure
  131. * that contains the configuration information for the specified GPIO peripheral.
  132. * @retval An ErrorStatus enumeration value:
  133. * - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
  134. * - ERROR: Not applicable
  135. */
  136. ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, LL_GPIO_InitTypeDef *GPIO_InitStruct)
  137. {
  138. uint32_t pinmask;
  139. uint32_t pinpos;
  140. uint32_t currentpin;
  141. /* Check the parameters */
  142. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  143. assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
  144. /* ------------------------- Configure the port pins ---------------- */
  145. /* Initialize pinpos on first pin set */
  146. pinmask = ((GPIO_InitStruct->Pin) << GPIO_PIN_MASK_POS) >> GPIO_PIN_NB;
  147. pinpos = POSITION_VAL(pinmask);
  148. /* Configure the port pins */
  149. while ((pinmask >> pinpos) != 0u)
  150. {
  151. /* skip if bit is not set */
  152. if ((pinmask & (1u << pinpos)) != 0u)
  153. {
  154. /* Get current io position */
  155. if (pinpos < GPIO_PIN_MASK_POS)
  156. {
  157. currentpin = (0x00000101uL << pinpos);
  158. }
  159. else
  160. {
  161. currentpin = ((0x00010001u << (pinpos - GPIO_PIN_MASK_POS)) | 0x04000000u);
  162. }
  163. /* Check Pin Mode and Pin Pull parameters */
  164. assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
  165. assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
  166. /* Pin Mode configuration */
  167. LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
  168. /* Pull-up Pull-down resistor configuration*/
  169. LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
  170. if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
  171. {
  172. /* Check speed and Output mode parameters */
  173. assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
  174. assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
  175. /* Speed mode configuration */
  176. LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
  177. /* Output mode configuration*/
  178. LL_GPIO_SetPinOutputType(GPIOx, currentpin, GPIO_InitStruct->OutputType);
  179. }
  180. }
  181. pinpos++;
  182. }
  183. return (SUCCESS);
  184. }
  185. /**
  186. * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
  187. * @param GPIO_InitStruct: pointer to a @ref LL_GPIO_InitTypeDef structure
  188. * whose fields will be set to default values.
  189. * @retval None
  190. */
  191. void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
  192. {
  193. /* Reset GPIO init structure parameters values */
  194. GPIO_InitStruct->Pin = LL_GPIO_PIN_ALL;
  195. GPIO_InitStruct->Mode = LL_GPIO_MODE_FLOATING;
  196. GPIO_InitStruct->Speed = LL_GPIO_SPEED_FREQ_LOW;
  197. GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
  198. GPIO_InitStruct->Pull = LL_GPIO_PULL_DOWN;
  199. }
  200. /**
  201. * @}
  202. */
  203. /**
  204. * @}
  205. */
  206. /**
  207. * @}
  208. */
  209. #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) */
  210. /**
  211. * @}
  212. */
  213. #endif /* USE_FULL_LL_DRIVER */
  214. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/