stm32f1xx_ll_gpio.c 9.1 KB

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