stm32f0xx_ll_gpio.c 9.0 KB

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