stm32f3xx_ll_gpio.c 10.0 KB

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