You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

280 lines
10 KiB

  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</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 "stm32f0xx_ll_gpio.h"
  38. #include "stm32f0xx_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 STM32F0xx_LL_Driver
  45. * @{
  46. */
  47. #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF)
  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. #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 = 0x00000000U;
  157. uint32_t currentpin = 0x00000000U;
  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; useless as already done in default initialization */
  166. /* Configure the port pins */
  167. while (((GPIO_InitStruct->Pin) >> pinpos) != 0x00000000U)
  168. {
  169. /* Get current io position */
  170. currentpin = (GPIO_InitStruct->Pin) & (0x00000001U << pinpos);
  171. if (currentpin)
  172. {
  173. /* Pin Mode configuration */
  174. LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
  175. if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
  176. {
  177. /* Check Speed mode parameters */
  178. assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
  179. /* Speed mode configuration */
  180. LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
  181. }
  182. /* Pull-up Pull down resistor configuration*/
  183. LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
  184. if (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE)
  185. {
  186. /* Check Alternate parameter */
  187. assert_param(IS_LL_GPIO_ALTERNATE(GPIO_InitStruct->Alternate));
  188. /* Speed mode configuration */
  189. if (currentpin < LL_GPIO_PIN_8)
  190. {
  191. LL_GPIO_SetAFPin_0_7(GPIOx, currentpin, GPIO_InitStruct->Alternate);
  192. }
  193. else
  194. {
  195. LL_GPIO_SetAFPin_8_15(GPIOx, currentpin, GPIO_InitStruct->Alternate);
  196. }
  197. }
  198. }
  199. pinpos++;
  200. }
  201. if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
  202. {
  203. /* Check Output mode parameters */
  204. assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
  205. /* Output mode configuration*/
  206. LL_GPIO_SetPinOutputType(GPIOx, GPIO_InitStruct->Pin, GPIO_InitStruct->OutputType);
  207. }
  208. return (SUCCESS);
  209. }
  210. /**
  211. * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
  212. * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
  213. * whose fields will be set to default values.
  214. * @retval None
  215. */
  216. void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
  217. {
  218. /* Reset GPIO init structure parameters values */
  219. GPIO_InitStruct->Pin = LL_GPIO_PIN_ALL;
  220. GPIO_InitStruct->Mode = LL_GPIO_MODE_ANALOG;
  221. GPIO_InitStruct->Speed = LL_GPIO_SPEED_FREQ_LOW;
  222. GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  223. GPIO_InitStruct->Pull = LL_GPIO_PULL_NO;
  224. GPIO_InitStruct->Alternate = LL_GPIO_AF_0;
  225. }
  226. /**
  227. * @}
  228. */
  229. /**
  230. * @}
  231. */
  232. /**
  233. * @}
  234. */
  235. #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) */
  236. /**
  237. * @}
  238. */
  239. #endif /* USE_FULL_LL_DRIVER */
  240. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/