stm32f4xx_ll_gpio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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 "stm32f4xx_ll_gpio.h"
  38. #include "stm32f4xx_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 STM32F4xx_LL_Driver
  45. * @{
  46. */
  47. #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) || defined (GPIOH) || defined (GPIOI) || defined (GPIOJ) || defined (GPIOK)
  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_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOA);
  115. LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOA);
  116. }
  117. else if (GPIOx == GPIOB)
  118. {
  119. LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOB);
  120. LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOB);
  121. }
  122. else if (GPIOx == GPIOC)
  123. {
  124. LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOC);
  125. LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOC);
  126. }
  127. #if defined(GPIOD)
  128. else if (GPIOx == GPIOD)
  129. {
  130. LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOD);
  131. LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOD);
  132. }
  133. #endif /* GPIOD */
  134. #if defined(GPIOE)
  135. else if (GPIOx == GPIOE)
  136. {
  137. LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOE);
  138. LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOE);
  139. }
  140. #endif /* GPIOE */
  141. #if defined(GPIOF)
  142. else if (GPIOx == GPIOF)
  143. {
  144. LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOF);
  145. LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOF);
  146. }
  147. #endif /* GPIOF */
  148. #if defined(GPIOG)
  149. else if (GPIOx == GPIOG)
  150. {
  151. LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOG);
  152. LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOG);
  153. }
  154. #endif /* GPIOG */
  155. #if defined(GPIOH)
  156. else if (GPIOx == GPIOH)
  157. {
  158. LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOH);
  159. LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOH);
  160. }
  161. #endif /* GPIOH */
  162. #if defined(GPIOI)
  163. else if (GPIOx == GPIOI)
  164. {
  165. LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOI);
  166. LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOI);
  167. }
  168. #endif /* GPIOI */
  169. #if defined(GPIOJ)
  170. else if (GPIOx == GPIOJ)
  171. {
  172. LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOJ);
  173. LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOJ);
  174. }
  175. #endif /* GPIOJ */
  176. #if defined(GPIOK)
  177. else if (GPIOx == GPIOK)
  178. {
  179. LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOK);
  180. LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOK);
  181. }
  182. #endif /* GPIOK */
  183. else
  184. {
  185. status = ERROR;
  186. }
  187. return (status);
  188. }
  189. /**
  190. * @brief Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
  191. * @param GPIOx GPIO Port
  192. * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
  193. * that contains the configuration information for the specified GPIO peripheral.
  194. * @retval An ErrorStatus enumeration value:
  195. * - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
  196. * - ERROR: Not applicable
  197. */
  198. ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, LL_GPIO_InitTypeDef *GPIO_InitStruct)
  199. {
  200. uint32_t pinpos = 0x00000000U;
  201. uint32_t currentpin = 0x00000000U;
  202. /* Check the parameters */
  203. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  204. assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
  205. assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
  206. assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
  207. /* ------------------------- Configure the port pins ---------------- */
  208. /* Initialize pinpos on first pin set */
  209. pinpos = POSITION_VAL(GPIO_InitStruct->Pin);
  210. /* Configure the port pins */
  211. while (((GPIO_InitStruct->Pin) >> pinpos) != 0x00000000U)
  212. {
  213. /* Get current io position */
  214. currentpin = (GPIO_InitStruct->Pin) & (0x00000001U << pinpos);
  215. if (currentpin)
  216. {
  217. /* Pin Mode configuration */
  218. LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
  219. if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
  220. {
  221. /* Check Speed mode parameters */
  222. assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
  223. /* Speed mode configuration */
  224. LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
  225. }
  226. /* Pull-up Pull down resistor configuration*/
  227. LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
  228. if (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE)
  229. {
  230. /* Check Alternate parameter */
  231. assert_param(IS_LL_GPIO_ALTERNATE(GPIO_InitStruct->Alternate));
  232. /* Speed mode configuration */
  233. if (POSITION_VAL(currentpin) < 0x00000008U)
  234. {
  235. LL_GPIO_SetAFPin_0_7(GPIOx, currentpin, GPIO_InitStruct->Alternate);
  236. }
  237. else
  238. {
  239. LL_GPIO_SetAFPin_8_15(GPIOx, currentpin, GPIO_InitStruct->Alternate);
  240. }
  241. }
  242. }
  243. pinpos++;
  244. }
  245. if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
  246. {
  247. /* Check Output mode parameters */
  248. assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
  249. /* Output mode configuration*/
  250. LL_GPIO_SetPinOutputType(GPIOx, GPIO_InitStruct->Pin, GPIO_InitStruct->OutputType);
  251. }
  252. return (SUCCESS);
  253. }
  254. /**
  255. * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
  256. * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
  257. * whose fields will be set to default values.
  258. * @retval None
  259. */
  260. void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
  261. {
  262. /* Reset GPIO init structure parameters values */
  263. GPIO_InitStruct->Pin = LL_GPIO_PIN_ALL;
  264. GPIO_InitStruct->Mode = LL_GPIO_MODE_ANALOG;
  265. GPIO_InitStruct->Speed = LL_GPIO_SPEED_FREQ_LOW;
  266. GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  267. GPIO_InitStruct->Pull = LL_GPIO_PULL_NO;
  268. GPIO_InitStruct->Alternate = LL_GPIO_AF_0;
  269. }
  270. /**
  271. * @}
  272. */
  273. /**
  274. * @}
  275. */
  276. /**
  277. * @}
  278. */
  279. #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) || defined (GPIOH) || defined (GPIOI) || defined (GPIOJ) || defined (GPIOK) */
  280. /**
  281. * @}
  282. */
  283. #endif /* USE_FULL_LL_DRIVER */
  284. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/