py32f0xx_ll_gpio.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**
  2. ******************************************************************************
  3. * @file py32f0xx_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 "py32f0xx_ll_gpio.h"
  33. #include "py32f0xx_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 PY32F0xx_LL_Driver
  40. * @{
  41. */
  42. #if defined (GPIOA) || defined (GPIOB) || defined (GPIOF)
  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. ((__VALUE__) == LL_GPIO_AF_8 ) ||\
  92. ((__VALUE__) == LL_GPIO_AF_9 ) ||\
  93. ((__VALUE__) == LL_GPIO_AF_10 ) ||\
  94. ((__VALUE__) == LL_GPIO_AF_11 ) ||\
  95. ((__VALUE__) == LL_GPIO_AF_12 ) ||\
  96. ((__VALUE__) == LL_GPIO_AF_13 ) ||\
  97. ((__VALUE__) == LL_GPIO_AF_14 ) ||\
  98. ((__VALUE__) == LL_GPIO_AF_15 ))
  99. /**
  100. * @}
  101. */
  102. /* Private function prototypes -----------------------------------------------*/
  103. /* Exported functions --------------------------------------------------------*/
  104. /** @addtogroup GPIO_LL_Exported_Functions
  105. * @{
  106. */
  107. /** @addtogroup GPIO_LL_EF_Init
  108. * @{
  109. */
  110. /**
  111. * @brief De-initialize GPIO registers (Registers restored to their default values).
  112. * @param GPIOx GPIO Port
  113. * @retval An ErrorStatus enumeration value:
  114. * - SUCCESS: GPIO registers are de-initialized
  115. * - ERROR: Wrong GPIO Port
  116. */
  117. ErrorStatus LL_GPIO_DeInit(GPIO_TypeDef *GPIOx)
  118. {
  119. ErrorStatus status = SUCCESS;
  120. /* Check the parameters */
  121. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  122. /* Force and Release reset on clock of GPIOx Port */
  123. if (GPIOx == GPIOA)
  124. {
  125. LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOA);
  126. LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOA);
  127. }
  128. else if (GPIOx == GPIOB)
  129. {
  130. LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOB);
  131. LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOB);
  132. }
  133. else if (GPIOx == GPIOF)
  134. {
  135. LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOF);
  136. LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOF);
  137. }
  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;
  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. 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 = 0;
  165. /* Configure the port pins */
  166. while (((GPIO_InitStruct->Pin) >> pinpos) != 0x00u)
  167. {
  168. /* Get current io position */
  169. currentpin = (GPIO_InitStruct->Pin) & (0x00000001uL << pinpos);
  170. if (currentpin != 0x00u)
  171. {
  172. /* Pin Mode configuration */
  173. LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
  174. if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
  175. {
  176. /* Check Speed mode parameters */
  177. assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
  178. /* Speed mode configuration */
  179. LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
  180. }
  181. /* Pull-up Pull down resistor configuration*/
  182. LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
  183. if (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE)
  184. {
  185. /* Check Alternate parameter */
  186. assert_param(IS_LL_GPIO_ALTERNATE(GPIO_InitStruct->Alternate));
  187. /* Speed mode configuration */
  188. if (currentpin < LL_GPIO_PIN_8)
  189. {
  190. LL_GPIO_SetAFPin_0_7(GPIOx, currentpin, GPIO_InitStruct->Alternate);
  191. }
  192. else
  193. {
  194. LL_GPIO_SetAFPin_8_15(GPIOx, currentpin, GPIO_InitStruct->Alternate);
  195. }
  196. }
  197. }
  198. pinpos++;
  199. }
  200. if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
  201. {
  202. /* Check Output mode parameters */
  203. assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
  204. /* Output mode configuration*/
  205. LL_GPIO_SetPinOutputType(GPIOx, GPIO_InitStruct->Pin, GPIO_InitStruct->OutputType);
  206. }
  207. return (SUCCESS);
  208. }
  209. /**
  210. * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
  211. * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
  212. * whose fields will be set to default values.
  213. * @retval None
  214. */
  215. void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
  216. {
  217. /* Reset GPIO init structure parameters values */
  218. GPIO_InitStruct->Pin = LL_GPIO_PIN_ALL;
  219. GPIO_InitStruct->Mode = LL_GPIO_MODE_ANALOG;
  220. GPIO_InitStruct->Speed = LL_GPIO_SPEED_FREQ_LOW;
  221. GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  222. GPIO_InitStruct->Pull = LL_GPIO_PULL_NO;
  223. GPIO_InitStruct->Alternate = LL_GPIO_AF_0;
  224. }
  225. /**
  226. * @}
  227. */
  228. /**
  229. * @}
  230. */
  231. /**
  232. * @}
  233. */
  234. #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOF) */
  235. /**
  236. * @}
  237. */
  238. #endif /* USE_FULL_LL_DRIVER */
  239. /************************ (C) COPYRIGHT Puya Semiconductor *****END OF FILE****/