stm32f1xx_ll_gpio.c 7.9 KB

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