stm32f1xx_ll_gpio.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_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 "stm32f1xx_ll_gpio.h"
  38. #include "stm32f1xx_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 STM32F1xx_LL_Driver
  45. * @{
  46. */
  47. #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG)
  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__) ((((__VALUE__) & LL_GPIO_PIN_ALL)!= 0U) &&\
  59. (((__VALUE__) & (~LL_GPIO_PIN_ALL))== 0U))
  60. #define IS_LL_GPIO_MODE(__VALUE__) (((__VALUE__) == LL_GPIO_MODE_ANALOG) ||\
  61. ((__VALUE__) == LL_GPIO_MODE_FLOATING) ||\
  62. ((__VALUE__) == LL_GPIO_MODE_INPUT) ||\
  63. ((__VALUE__) == LL_GPIO_MODE_OUTPUT) ||\
  64. ((__VALUE__) == LL_GPIO_MODE_ALTERNATE))
  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_OUTPUT_TYPE(__VALUE__) (((__VALUE__) == LL_GPIO_OUTPUT_PUSHPULL) ||\
  69. ((__VALUE__) == LL_GPIO_OUTPUT_OPENDRAIN))
  70. #define IS_LL_GPIO_PULL(__VALUE__) (((__VALUE__) == LL_GPIO_PULL_DOWN) ||\
  71. ((__VALUE__) == LL_GPIO_PULL_UP))
  72. /**
  73. * @}
  74. */
  75. /* Private function prototypes -----------------------------------------------*/
  76. /* Exported functions --------------------------------------------------------*/
  77. /** @addtogroup GPIO_LL_Exported_Functions
  78. * @{
  79. */
  80. /** @addtogroup GPIO_LL_EF_Init
  81. * @{
  82. */
  83. /**
  84. * @brief De-initialize GPIO registers (Registers restored to their default values).
  85. * @param GPIOx GPIO Port
  86. * @retval An ErrorStatus enumeration value:
  87. * - SUCCESS: GPIO registers are de-initialized
  88. * - ERROR: Wrong GPIO Port
  89. */
  90. ErrorStatus LL_GPIO_DeInit(GPIO_TypeDef *GPIOx)
  91. {
  92. ErrorStatus status = SUCCESS;
  93. /* Check the parameters */
  94. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  95. /* Force and Release reset on clock of GPIOx Port */
  96. if (GPIOx == GPIOA)
  97. {
  98. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_GPIOA);
  99. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_GPIOA);
  100. }
  101. else if (GPIOx == GPIOB)
  102. {
  103. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_GPIOB);
  104. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_GPIOB);
  105. }
  106. else if (GPIOx == GPIOC)
  107. {
  108. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_GPIOC);
  109. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_GPIOC);
  110. }
  111. else if (GPIOx == GPIOD)
  112. {
  113. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_GPIOD);
  114. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_GPIOD);
  115. }
  116. #if defined(GPIOE)
  117. else if (GPIOx == GPIOE)
  118. {
  119. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_GPIOE);
  120. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_GPIOE);
  121. }
  122. #endif
  123. #if defined(GPIOF)
  124. else if (GPIOx == GPIOF)
  125. {
  126. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_GPIOF);
  127. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_GPIOF);
  128. }
  129. #endif
  130. #if defined(GPIOG)
  131. else if (GPIOx == GPIOG)
  132. {
  133. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_GPIOG);
  134. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_GPIOG);
  135. }
  136. #endif
  137. else
  138. {
  139. status = ERROR;
  140. }
  141. return (status);
  142. }
  143. /**
  144. * @brief Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
  145. * @param GPIOx GPIO Port
  146. * @param GPIO_InitStruct: pointer to a @ref LL_GPIO_InitTypeDef structure
  147. * that contains the configuration information for the specified GPIO peripheral.
  148. * @retval An ErrorStatus enumeration value:
  149. * - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
  150. * - ERROR: Not applicable
  151. */
  152. ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, LL_GPIO_InitTypeDef *GPIO_InitStruct)
  153. {
  154. uint32_t pinmask;
  155. uint32_t pinpos;
  156. uint32_t currentpin;
  157. /* Check the parameters */
  158. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  159. assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
  160. /* ------------------------- Configure the port pins ---------------- */
  161. /* Initialize pinpos on first pin set */
  162. pinmask = ((GPIO_InitStruct->Pin) << GPIO_PIN_MASK_POS) >> GPIO_PIN_NB;
  163. pinpos = POSITION_VAL(pinmask);
  164. /* Configure the port pins */
  165. while ((pinmask >> pinpos) != 0U)
  166. {
  167. /* skip if bit is not set */
  168. if ((pinmask & (1U << pinpos)) != 0U)
  169. {
  170. /* Get current io position */
  171. if (pinpos < GPIO_PIN_MASK_POS)
  172. {
  173. currentpin = (0x00000101U << pinpos);
  174. }
  175. else
  176. {
  177. currentpin = ((0x00010001U << (pinpos - GPIO_PIN_MASK_POS)) | 0x04000000U);
  178. }
  179. /* Check Pin Mode and Pin Pull parameters */
  180. assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
  181. assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
  182. /* Pin Mode configuration */
  183. LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
  184. /* Pull-up Pull-down resistor configuration*/
  185. LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
  186. if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
  187. {
  188. /* Check speed and Output mode parameters */
  189. assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
  190. assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
  191. /* Speed mode configuration */
  192. LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
  193. /* Output mode configuration*/
  194. LL_GPIO_SetPinOutputType(GPIOx, currentpin, GPIO_InitStruct->OutputType);
  195. }
  196. }
  197. pinpos++;
  198. }
  199. return (SUCCESS);
  200. }
  201. /**
  202. * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
  203. * @param GPIO_InitStruct: pointer to a @ref LL_GPIO_InitTypeDef structure
  204. * whose fields will be set to default values.
  205. * @retval None
  206. */
  207. void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
  208. {
  209. /* Reset GPIO init structure parameters values */
  210. GPIO_InitStruct->Pin = LL_GPIO_PIN_ALL;
  211. GPIO_InitStruct->Mode = LL_GPIO_MODE_FLOATING;
  212. GPIO_InitStruct->Speed = LL_GPIO_SPEED_FREQ_LOW;
  213. GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
  214. GPIO_InitStruct->Pull = LL_GPIO_PULL_DOWN;
  215. }
  216. /**
  217. * @}
  218. */
  219. /**
  220. * @}
  221. */
  222. /**
  223. * @}
  224. */
  225. #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) */
  226. /**
  227. * @}
  228. */
  229. #endif /* USE_FULL_LL_DRIVER */
  230. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/