stm32l4xx_ll_gpio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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</center></h2>
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met:
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. ******************************************************************************
  34. */
  35. #if defined(USE_FULL_LL_DRIVER)
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "stm32l4xx_ll_gpio.h"
  38. #include "stm32l4xx_ll_bus.h"
  39. #ifdef USE_FULL_ASSERT
  40. #include "stm32_assert.h"
  41. #else
  42. #define assert_param(expr) ((void)0U)
  43. #endif
  44. /** @addtogroup STM32L4xx_LL_Driver
  45. * @{
  46. */
  47. #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) || defined (GPIOH) || defined (GPIOI)
  48. /** @addtogroup GPIO_LL
  49. * @{
  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__) (((0x00000000U) < (__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. ((__VALUE__) == LL_GPIO_SPEED_FREQ_VERY_HIGH))
  69. #define IS_LL_GPIO_PULL(__VALUE__) (((__VALUE__) == LL_GPIO_PULL_NO) ||\
  70. ((__VALUE__) == LL_GPIO_PULL_UP) ||\
  71. ((__VALUE__) == LL_GPIO_PULL_DOWN))
  72. #define IS_LL_GPIO_ALTERNATE(__VALUE__) (((__VALUE__) == LL_GPIO_AF_0 ) ||\
  73. ((__VALUE__) == LL_GPIO_AF_1 ) ||\
  74. ((__VALUE__) == LL_GPIO_AF_2 ) ||\
  75. ((__VALUE__) == LL_GPIO_AF_3 ) ||\
  76. ((__VALUE__) == LL_GPIO_AF_4 ) ||\
  77. ((__VALUE__) == LL_GPIO_AF_5 ) ||\
  78. ((__VALUE__) == LL_GPIO_AF_6 ) ||\
  79. ((__VALUE__) == LL_GPIO_AF_7 ) ||\
  80. ((__VALUE__) == LL_GPIO_AF_8 ) ||\
  81. ((__VALUE__) == LL_GPIO_AF_9 ) ||\
  82. ((__VALUE__) == LL_GPIO_AF_10 ) ||\
  83. ((__VALUE__) == LL_GPIO_AF_11 ) ||\
  84. ((__VALUE__) == LL_GPIO_AF_12 ) ||\
  85. ((__VALUE__) == LL_GPIO_AF_13 ) ||\
  86. ((__VALUE__) == LL_GPIO_AF_14 ) ||\
  87. ((__VALUE__) == LL_GPIO_AF_15 ))
  88. /**
  89. * @}
  90. */
  91. /* Private function prototypes -----------------------------------------------*/
  92. /* Exported functions --------------------------------------------------------*/
  93. /** @addtogroup GPIO_LL_Exported_Functions
  94. * @{
  95. */
  96. /** @addtogroup GPIO_LL_EF_Init
  97. * @{
  98. */
  99. /**
  100. * @brief De-initialize GPIO registers (Registers restored to their default values).
  101. * @param GPIOx GPIO Port
  102. * @retval An ErrorStatus enumeration value:
  103. * - SUCCESS: GPIO registers are de-initialized
  104. * - ERROR: Wrong GPIO Port
  105. */
  106. ErrorStatus LL_GPIO_DeInit(GPIO_TypeDef *GPIOx)
  107. {
  108. ErrorStatus status = SUCCESS;
  109. /* Check the parameters */
  110. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  111. /* Force and Release reset on clock of GPIOx Port */
  112. if (GPIOx == GPIOA)
  113. {
  114. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOA);
  115. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOA);
  116. }
  117. else if (GPIOx == GPIOB)
  118. {
  119. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOB);
  120. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOB);
  121. }
  122. else if (GPIOx == GPIOC)
  123. {
  124. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOC);
  125. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOC);
  126. }
  127. #if defined(GPIOD)
  128. else if (GPIOx == GPIOD)
  129. {
  130. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOD);
  131. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOD);
  132. }
  133. #endif /* GPIOD */
  134. #if defined(GPIOE)
  135. else if (GPIOx == GPIOE)
  136. {
  137. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOE);
  138. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOE);
  139. }
  140. #endif /* GPIOE */
  141. #if defined(GPIOF)
  142. else if (GPIOx == GPIOF)
  143. {
  144. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOF);
  145. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOF);
  146. }
  147. #endif /* GPIOF */
  148. #if defined(GPIOG)
  149. else if (GPIOx == GPIOG)
  150. {
  151. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOG);
  152. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOG);
  153. }
  154. #endif /* GPIOG */
  155. #if defined(GPIOH)
  156. else if (GPIOx == GPIOH)
  157. {
  158. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOH);
  159. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOH);
  160. }
  161. #endif /* GPIOH */
  162. #if defined(GPIOI)
  163. else if (GPIOx == GPIOI)
  164. {
  165. LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOI);
  166. LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOI);
  167. }
  168. #endif /* GPIOI */
  169. else
  170. {
  171. status = ERROR;
  172. }
  173. return (status);
  174. }
  175. /**
  176. * @brief Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
  177. * @param GPIOx GPIO Port
  178. * @param GPIO_InitStruct: pointer to a @ref LL_GPIO_InitTypeDef structure
  179. * that contains the configuration information for the specified GPIO peripheral.
  180. * @retval An ErrorStatus enumeration value:
  181. * - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
  182. * - ERROR: Not applicable
  183. */
  184. ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, LL_GPIO_InitTypeDef *GPIO_InitStruct)
  185. {
  186. uint32_t pinpos = 0x00000000U;
  187. uint32_t currentpin = 0x00000000U;
  188. /* Check the parameters */
  189. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  190. assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
  191. assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
  192. assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
  193. /* ------------------------- Configure the port pins ---------------- */
  194. /* Initialize pinpos on first pin set */
  195. pinpos = POSITION_VAL(GPIO_InitStruct->Pin);
  196. /* Configure the port pins */
  197. while (((GPIO_InitStruct->Pin) >> pinpos) != 0x00000000U)
  198. {
  199. /* Get current io position */
  200. currentpin = (GPIO_InitStruct->Pin) & (0x00000001U << pinpos);
  201. if (currentpin)
  202. {
  203. /* Pin Mode configuration */
  204. LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
  205. if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
  206. {
  207. /* Check Speed mode parameters */
  208. assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
  209. /* Speed mode configuration */
  210. LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
  211. }
  212. /* Pull-up Pull down resistor configuration*/
  213. LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
  214. if (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE)
  215. {
  216. /* Check Alternate parameter */
  217. assert_param(IS_LL_GPIO_ALTERNATE(GPIO_InitStruct->Alternate));
  218. /* Speed mode configuration */
  219. if (POSITION_VAL(currentpin) < 0x00000008U)
  220. {
  221. LL_GPIO_SetAFPin_0_7(GPIOx, currentpin, GPIO_InitStruct->Alternate);
  222. }
  223. else
  224. {
  225. LL_GPIO_SetAFPin_8_15(GPIOx, currentpin, GPIO_InitStruct->Alternate);
  226. }
  227. }
  228. }
  229. pinpos++;
  230. }
  231. if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
  232. {
  233. /* Check Output mode parameters */
  234. assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
  235. /* Output mode configuration*/
  236. LL_GPIO_SetPinOutputType(GPIOx, GPIO_InitStruct->Pin, GPIO_InitStruct->OutputType);
  237. }
  238. return (SUCCESS);
  239. }
  240. /**
  241. * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
  242. * @param GPIO_InitStruct: pointer to a @ref LL_GPIO_InitTypeDef structure
  243. * whose fields will be set to default values.
  244. * @retval None
  245. */
  246. void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
  247. {
  248. /* Reset GPIO init structure parameters values */
  249. GPIO_InitStruct->Pin = LL_GPIO_PIN_ALL;
  250. GPIO_InitStruct->Mode = LL_GPIO_MODE_ANALOG;
  251. GPIO_InitStruct->Speed = LL_GPIO_SPEED_FREQ_LOW;
  252. GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  253. GPIO_InitStruct->Pull = LL_GPIO_PULL_NO;
  254. GPIO_InitStruct->Alternate = LL_GPIO_AF_0;
  255. }
  256. /**
  257. * @}
  258. */
  259. /**
  260. * @}
  261. */
  262. /**
  263. * @}
  264. */
  265. #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) || defined (GPIOH) || defined (GPIOI) */
  266. /**
  267. * @}
  268. */
  269. #endif /* USE_FULL_LL_DRIVER */
  270. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/