stm32l4xx_ll_gpio.c 11 KB

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