py32f002b_ll_gpio.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /**
  2. ******************************************************************************
  3. * @file py32f002b_ll_gpio.c
  4. * @author MCU Application Team
  5. * @brief GPIO LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by Puya under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. * @attention
  19. *
  20. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  21. * All rights reserved.</center></h2>
  22. *
  23. * This software component is licensed by ST under BSD 3-Clause license,
  24. * the "License"; You may not use this file except in compliance with the
  25. * License. You may obtain a copy of the License at:
  26. * opensource.org/licenses/BSD-3-Clause
  27. *
  28. ******************************************************************************
  29. */
  30. #if defined(USE_FULL_LL_DRIVER)
  31. /* Includes ------------------------------------------------------------------*/
  32. #include "py32f002b_ll_gpio.h"
  33. #include "py32f002b_ll_bus.h"
  34. #ifdef USE_FULL_ASSERT
  35. #include "py32_assert.h"
  36. #else
  37. #define assert_param(expr) ((void)0U)
  38. #endif
  39. /** @addtogroup PY32F002B_LL_Driver
  40. * @{
  41. */
  42. #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC)
  43. /** @addtogroup GPIO_LL
  44. * @{
  45. */
  46. /** MISRA C:2012 deviation rule has been granted for following rules:
  47. * Rule-12.2 - Medium: RHS argument is in interval [0,INF] which is out of
  48. * range of the shift operator in following API :
  49. * LL_GPIO_Init
  50. * LL_GPIO_DeInit
  51. * LL_GPIO_SetPinMode
  52. * LL_GPIO_GetPinMode
  53. * LL_GPIO_SetPinSpeed
  54. * LL_GPIO_GetPinSpeed
  55. * LL_GPIO_SetPinPull
  56. * LL_GPIO_GetPinPull
  57. * LL_GPIO_GetAFPin_0_7
  58. * LL_GPIO_SetAFPin_0_7
  59. * LL_GPIO_SetAFPin_8_15
  60. * LL_GPIO_GetAFPin_8_15
  61. */
  62. /* Private types -------------------------------------------------------------*/
  63. /* Private variables ---------------------------------------------------------*/
  64. /* Private constants ---------------------------------------------------------*/
  65. /* Private macros ------------------------------------------------------------*/
  66. /** @addtogroup GPIO_LL_Private_Macros
  67. * @{
  68. */
  69. #define IS_LL_GPIO_PIN(__VALUE__) (((0x00u) < (__VALUE__)) && ((__VALUE__) <= (LL_GPIO_PIN_ALL)))
  70. #define IS_LL_GPIO_MODE(__VALUE__) (((__VALUE__) == LL_GPIO_MODE_INPUT) ||\
  71. ((__VALUE__) == LL_GPIO_MODE_OUTPUT) ||\
  72. ((__VALUE__) == LL_GPIO_MODE_ALTERNATE) ||\
  73. ((__VALUE__) == LL_GPIO_MODE_ANALOG))
  74. #define IS_LL_GPIO_OUTPUT_TYPE(__VALUE__) (((__VALUE__) == LL_GPIO_OUTPUT_PUSHPULL) ||\
  75. ((__VALUE__) == LL_GPIO_OUTPUT_OPENDRAIN))
  76. #define IS_LL_GPIO_SPEED(__VALUE__) (((__VALUE__) == LL_GPIO_SPEED_FREQ_LOW) ||\
  77. ((__VALUE__) == LL_GPIO_SPEED_FREQ_MEDIUM) ||\
  78. ((__VALUE__) == LL_GPIO_SPEED_FREQ_HIGH) ||\
  79. ((__VALUE__) == LL_GPIO_SPEED_FREQ_VERY_HIGH))
  80. #define IS_LL_GPIO_PULL(__VALUE__) (((__VALUE__) == LL_GPIO_PULL_NO) ||\
  81. ((__VALUE__) == LL_GPIO_PULL_UP) ||\
  82. ((__VALUE__) == LL_GPIO_PULL_DOWN))
  83. #define IS_LL_GPIO_ALTERNATE(__VALUE__) (((__VALUE__) == LL_GPIO_AF_0 ) ||\
  84. ((__VALUE__) == LL_GPIO_AF_1 ) ||\
  85. ((__VALUE__) == LL_GPIO_AF_2 ) ||\
  86. ((__VALUE__) == LL_GPIO_AF_3 ) ||\
  87. ((__VALUE__) == LL_GPIO_AF_4 ) ||\
  88. ((__VALUE__) == LL_GPIO_AF_5 ) ||\
  89. ((__VALUE__) == LL_GPIO_AF_6 ) ||\
  90. ((__VALUE__) == LL_GPIO_AF_7 ))
  91. /**
  92. * @}
  93. */
  94. /* Private function prototypes -----------------------------------------------*/
  95. /* Exported functions --------------------------------------------------------*/
  96. /** @addtogroup GPIO_LL_Exported_Functions
  97. * @{
  98. */
  99. /** @addtogroup GPIO_LL_EF_Init
  100. * @{
  101. */
  102. /**
  103. * @brief De-initialize GPIO registers (Registers restored to their default values).
  104. * @param GPIOx GPIO Port
  105. * @retval An ErrorStatus enumeration value:
  106. * - SUCCESS: GPIO registers are de-initialized
  107. * - ERROR: Wrong GPIO Port
  108. */
  109. ErrorStatus LL_GPIO_DeInit(GPIO_TypeDef *GPIOx)
  110. {
  111. ErrorStatus status = SUCCESS;
  112. /* Check the parameters */
  113. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  114. /* Force and Release reset on clock of GPIOx Port */
  115. if (GPIOx == GPIOA)
  116. {
  117. LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOA);
  118. LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOA);
  119. }
  120. else if (GPIOx == GPIOB)
  121. {
  122. LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOB);
  123. LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOB);
  124. }
  125. else if (GPIOx == GPIOC)
  126. {
  127. LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOC);
  128. LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOC);
  129. }
  130. else
  131. {
  132. status = ERROR;
  133. }
  134. return (status);
  135. }
  136. /**
  137. * @brief Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
  138. * @param GPIOx GPIO Port
  139. * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
  140. * that contains the configuration information for the specified GPIO peripheral.
  141. * @retval An ErrorStatus enumeration value:
  142. * - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
  143. * - ERROR: Not applicable
  144. */
  145. ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, LL_GPIO_InitTypeDef *GPIO_InitStruct)
  146. {
  147. uint32_t pinpos;
  148. uint32_t currentpin;
  149. /* Check the parameters */
  150. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  151. assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
  152. assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
  153. assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
  154. /* ------------------------- Configure the port pins ---------------- */
  155. /* Initialize pinpos on first pin set */
  156. pinpos = 0;
  157. /* Configure the port pins */
  158. while (((GPIO_InitStruct->Pin) >> pinpos) != 0x00u)
  159. {
  160. /* Get current io position */
  161. currentpin = (GPIO_InitStruct->Pin) & (0x00000001uL << pinpos);
  162. if (currentpin != 0x00u)
  163. {
  164. /* Pin Mode configuration */
  165. LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
  166. if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
  167. {
  168. /* Check Speed mode parameters */
  169. assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
  170. /* Speed mode configuration */
  171. LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
  172. }
  173. /* Pull-up Pull down resistor configuration*/
  174. LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
  175. if (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE)
  176. {
  177. /* Check Alternate parameter */
  178. assert_param(IS_LL_GPIO_ALTERNATE(GPIO_InitStruct->Alternate));
  179. /* Speed mode configuration */
  180. if (currentpin < LL_GPIO_PIN_8)
  181. {
  182. LL_GPIO_SetAFPin_0_7(GPIOx, currentpin, GPIO_InitStruct->Alternate);
  183. }
  184. else
  185. {
  186. LL_GPIO_SetAFPin_8_15(GPIOx, currentpin, GPIO_InitStruct->Alternate);
  187. }
  188. }
  189. }
  190. pinpos++;
  191. }
  192. if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
  193. {
  194. /* Check Output mode parameters */
  195. assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
  196. /* Output mode configuration*/
  197. LL_GPIO_SetPinOutputType(GPIOx, GPIO_InitStruct->Pin, GPIO_InitStruct->OutputType);
  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_ANALOG;
  212. GPIO_InitStruct->Speed = LL_GPIO_SPEED_FREQ_LOW;
  213. GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  214. GPIO_InitStruct->Pull = LL_GPIO_PULL_NO;
  215. GPIO_InitStruct->Alternate = LL_GPIO_AF_0;
  216. }
  217. /**
  218. * @}
  219. */
  220. /**
  221. * @}
  222. */
  223. /**
  224. * @}
  225. */
  226. #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) */
  227. /**
  228. * @}
  229. */
  230. #endif /* USE_FULL_LL_DRIVER */
  231. /************************ (C) COPYRIGHT Puya Semiconductor *****END OF FILE****/