py32f002bxx_ll_Start_Kit.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**
  2. ******************************************************************************
  3. * @file py32f002bxx_ll_Start_Kit.c
  4. * @author MCU Application Team
  5. * @brief This file provides set of firmware functions to manage Leds,
  6. * push-button available on Start Kit.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  11. * All rights reserved.</center></h2>
  12. *
  13. * This software component is licensed by Puya under BSD 3-Clause license,
  14. * the "License"; You may not use this file except in compliance with the
  15. * License. You may obtain a copy of the License at:
  16. * opensource.org/licenses/BSD-3-Clause
  17. *
  18. ******************************************************************************
  19. * @attention
  20. *
  21. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  22. * All rights reserved.</center></h2>
  23. *
  24. * This software component is licensed by ST under BSD 3-Clause license,
  25. * the "License"; You may not use this file except in compliance with the
  26. * License. You may obtain a copy of the License at:
  27. * opensource.org/licenses/BSD-3-Clause
  28. *
  29. ******************************************************************************
  30. */
  31. /* Includes ------------------------------------------------------------------*/
  32. #include "py32f002bxx_ll_Start_Kit.h"
  33. /**
  34. * @brief PY32F002Bxx STK BSP Driver version number
  35. */
  36. #define __PY32F002Bxx_STK_BSP_VERSION_MAIN (0x00U) /*!< [31:24] main version */
  37. #define __PY32F002Bxx_STK_BSP_VERSION_SUB1 (0x00U) /*!< [23:16] sub1 version */
  38. #define __PY32F002Bxx_STK_BSP_VERSION_SUB2 (0x01U) /*!< [15:8] sub2 version */
  39. #define __PY32F002Bxx_STK_BSP_VERSION_RC (0x00U) /*!< [7:0] release candidate */
  40. #define __PY32F002Bxx_STK_BSP_VERSION ((__PY32F002Bxx_STK_BSP_VERSION_MAIN << 24) \
  41. |(__PY32F002Bxx_STK_BSP_VERSION_SUB1 << 16) \
  42. |(__PY32F002Bxx_STK_BSP_VERSION_SUB2 << 8 ) \
  43. |(__PY32F002Bxx_STK_BSP_VERSION_RC))
  44. GPIO_TypeDef* LED_PORT[LEDn] = {LED3_GPIO_PORT};
  45. const uint16_t LED_PIN[LEDn] = {LED3_PIN};
  46. GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {USER_BUTTON_GPIO_PORT };
  47. const uint16_t BUTTON_PIN[BUTTONn] = {USER_BUTTON_PIN };
  48. const uint8_t BUTTON_IRQn[BUTTONn] = {USER_BUTTON_EXTI_IRQn };
  49. const uint32_t BUTTON_EXTI_LINE[BUTTONn] = {USER_BUTTON_EXTI_LINE };
  50. /** @addtogroup PY32F002Bxx_STK_Exported_Functions
  51. * @{
  52. */
  53. /**
  54. * @brief This method returns the PY32F002B STK BSP Driver revision.
  55. * @retval version : 0xXYZR (8bits for each decimal, R for RC)
  56. */
  57. uint32_t BSP_GetVersion(void)
  58. {
  59. return __PY32F002Bxx_STK_BSP_VERSION;
  60. }
  61. /** @addtogroup LED_Functions
  62. * @{
  63. */
  64. /**
  65. * @brief Configures LED GPIO.
  66. * @param Led Specifies the Led to be configured.
  67. * This parameter can be one of following parameters:
  68. * @arg LED3
  69. * @retval None
  70. */
  71. void BSP_LED_Init(Led_TypeDef Led)
  72. {
  73. /* Enable the GPIO_LED Clock */
  74. LEDx_GPIO_CLK_ENABLE(Led);
  75. /* Configure the GPIO_LED pin */
  76. LL_GPIO_SetPinMode(LED_PORT[Led], LED_PIN[Led], LL_GPIO_MODE_OUTPUT);
  77. /* LL_GPIO_SetPinOutputType(LED_PORT[Led], LED_PIN[Led], LL_GPIO_OUTPUT_PUSHPULL); */
  78. /* LL_GPIO_SetPinSpeed(LED_PORT[Led], LED_PIN[Led], LL_GPIO_SPEED_FREQ_LOW); */
  79. /* LL_GPIO_SetPinPull(LED_PORT[Led], LED_PIN[Led], LL_GPIO_PULL_NO); */
  80. LL_GPIO_SetOutputPin(LED_PORT[Led], LED_PIN[Led]);
  81. }
  82. /**
  83. * @brief DeInitialize LED GPIO.
  84. * @param Led Specifies the Led to be deconfigured.
  85. * This parameter can be one of the following values:
  86. * @arg LED3
  87. * @note BSP_LED_DeInit() does not disable the GPIO clock
  88. * @retval None
  89. */
  90. void BSP_LED_DeInit(Led_TypeDef Led)
  91. {
  92. /* Turn off LED */
  93. LL_GPIO_ResetOutputPin(LED_PORT[Led], LED_PIN[Led]);
  94. /* DeInit the GPIO_LED pin */
  95. LL_GPIO_SetPinMode(LED_PORT[Led], LED_PIN[Led], LL_GPIO_MODE_ANALOG);
  96. /* LL_GPIO_SetPinOutputType(LED_PORT[Led], LED_PIN[Led], LL_GPIO_OUTPUT_PUSHPULL); */
  97. /* LL_GPIO_SetPinSpeed(LED_PORT[Led], LED_PIN[Led], LL_GPIO_SPEED_FREQ_LOW); */
  98. /* LL_GPIO_SetPinPull(LED_PORT[Led], LED_PIN[Led], LL_GPIO_PULL_NO); */
  99. }
  100. /**
  101. * @brief Turns selected LED On.
  102. * @param Led Specifies the Led to be set on.
  103. * This parameter can be one of following parameters:
  104. * @arg LED3
  105. * @retval None
  106. */
  107. void BSP_LED_On(Led_TypeDef Led)
  108. {
  109. LL_GPIO_ResetOutputPin(LED_PORT[Led], LED_PIN[Led]);
  110. }
  111. /**
  112. * @brief Turns selected LED Off.
  113. * @param Led Specifies the Led to be set off.
  114. * This parameter can be one of following parameters:
  115. * @arg LED3
  116. * @retval None
  117. */
  118. void BSP_LED_Off(Led_TypeDef Led)
  119. {
  120. LL_GPIO_SetOutputPin(LED_PORT[Led], LED_PIN[Led]);
  121. }
  122. /**
  123. * @brief Toggles the selected LED.
  124. * @param Led Specifies the Led to be toggled.
  125. * This parameter can be one of following parameters:
  126. * @arg LED3
  127. * @retval None
  128. */
  129. void BSP_LED_Toggle(Led_TypeDef Led)
  130. {
  131. LL_GPIO_TogglePin(LED_PORT[Led], LED_PIN[Led]);
  132. }
  133. /**
  134. * @brief Configures Button GPIO and EXTI Line.
  135. * @param Button: Specifies the Button to be configured.
  136. * This parameter should be: BUTTON_USER
  137. * @param ButtonMode: Specifies Button mode.
  138. * This parameter can be one of following parameters:
  139. * @arg BUTTON_MODE_GPIO: Button will be used as simple IO
  140. * @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt
  141. * generation capability
  142. * @retval None
  143. */
  144. void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode)
  145. {
  146. /* Enable the BUTTON Clock */
  147. BUTTONx_GPIO_CLK_ENABLE(Button);
  148. /* Configure GPIO for BUTTON */
  149. LL_GPIO_SetPinMode(BUTTON_PORT[Button], BUTTON_PIN[Button], LL_GPIO_MODE_INPUT);
  150. LL_GPIO_SetPinPull(BUTTON_PORT[Button], BUTTON_PIN[Button], LL_GPIO_PULL_NO);
  151. /* LL_GPIO_SetPinSpeed(BUTTON_PORT[Button], BUTTON_PIN[Button], LL_GPIO_SPEED_FREQ_HIGH); */
  152. if(ButtonMode == BUTTON_MODE_EXTI)
  153. {
  154. /* Configure Button pin as input with External interrupt */
  155. LL_EXTI_EnableIT(BUTTON_EXTI_LINE[Button]);
  156. LL_EXTI_EnableFallingTrig(BUTTON_EXTI_LINE[Button]);
  157. /* Enable and set Button EXTI Interrupt to the lowest priority */
  158. NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F);
  159. NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
  160. }
  161. }
  162. /**
  163. * @brief Push Button DeInit.
  164. * @param Button: Button to be configured
  165. * This parameter should be: BUTTON_USER
  166. * @note PB DeInit does not disable the GPIO clock
  167. * @retval None
  168. */
  169. void BSP_PB_DeInit(Button_TypeDef Button)
  170. {
  171. NVIC_DisableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
  172. LL_GPIO_SetPinMode(BUTTON_PORT[Button], BUTTON_PIN[Button], LL_GPIO_MODE_ANALOG);
  173. /* LL_GPIO_SetPinSpeed(BUTTON_PORT[Button], BUTTON_PIN[Button], LL_GPIO_SPEED_FREQ_LOW); */
  174. /* LL_GPIO_SetPinPull(BUTTON_PORT[Button], BUTTON_PIN[Button], LL_GPIO_PULL_NO); */
  175. /* LL_GPIO_SetAFPin_8_15(BUTTON_PORT[Button], BUTTON_PIN[Button], LL_GPIO_AF_0); */
  176. }
  177. /**
  178. * @brief Returns the selected Button state.
  179. * @param Button: Specifies the Button to be checked.
  180. * This parameter should be: BUTTON_USER
  181. * @retval Button state.
  182. */
  183. uint32_t BSP_PB_GetState(Button_TypeDef Button)
  184. {
  185. return LL_GPIO_IsInputPinSet(BUTTON_PORT[Button], BUTTON_PIN[Button]);
  186. }
  187. /**
  188. * @brief DEBUG_USART GPIO Config,Mode Config,115200 8-N-1
  189. * @param None
  190. * @retval None
  191. */
  192. void BSP_USART_Config(void)
  193. {
  194. #if defined (__GNUC__)
  195. setvbuf(stdout,NULL,_IONBF,0);
  196. #endif
  197. DEBUG_USART_CLK_ENABLE();
  198. /* USART Init */
  199. LL_USART_SetBaudRate(DEBUG_USART, SystemCoreClock, LL_USART_OVERSAMPLING_16, DEBUG_USART_BAUDRATE);
  200. LL_USART_SetDataWidth(DEBUG_USART, LL_USART_DATAWIDTH_8B);
  201. LL_USART_SetStopBitsLength(DEBUG_USART, LL_USART_STOPBITS_1);
  202. LL_USART_SetParity(DEBUG_USART, LL_USART_PARITY_NONE);
  203. LL_USART_SetHWFlowCtrl(DEBUG_USART, LL_USART_HWCONTROL_NONE);
  204. LL_USART_SetTransferDirection(DEBUG_USART, LL_USART_DIRECTION_TX_RX);
  205. LL_USART_Enable(DEBUG_USART);
  206. LL_USART_ClearFlag_TC(DEBUG_USART);
  207. /**USART GPIO Configuration
  208. PB4 ------> USART1_TX
  209. PB5 ------> USART1_RX
  210. */
  211. DEBUG_USART_RX_GPIO_CLK_ENABLE();
  212. DEBUG_USART_TX_GPIO_CLK_ENABLE();
  213. LL_GPIO_SetPinMode(DEBUG_USART_TX_GPIO_PORT, DEBUG_USART_TX_PIN, LL_GPIO_MODE_ALTERNATE);
  214. LL_GPIO_SetPinSpeed(DEBUG_USART_TX_GPIO_PORT, DEBUG_USART_TX_PIN, LL_GPIO_SPEED_FREQ_VERY_HIGH);
  215. LL_GPIO_SetPinPull(DEBUG_USART_TX_GPIO_PORT, DEBUG_USART_TX_PIN, LL_GPIO_PULL_UP);
  216. LL_GPIO_SetAFPin_0_7(DEBUG_USART_TX_GPIO_PORT, DEBUG_USART_TX_PIN, DEBUG_USART_TX_AF);
  217. LL_GPIO_SetPinMode(DEBUG_USART_RX_GPIO_PORT, DEBUG_USART_RX_PIN, LL_GPIO_MODE_ALTERNATE);
  218. LL_GPIO_SetPinSpeed(DEBUG_USART_RX_GPIO_PORT, DEBUG_USART_RX_PIN, LL_GPIO_SPEED_FREQ_VERY_HIGH);
  219. LL_GPIO_SetPinPull(DEBUG_USART_RX_GPIO_PORT, DEBUG_USART_RX_PIN, LL_GPIO_PULL_UP);
  220. LL_GPIO_SetAFPin_0_7(DEBUG_USART_RX_GPIO_PORT, DEBUG_USART_RX_PIN, DEBUG_USART_RX_AF);
  221. }
  222. #if (defined (__CC_ARM)) || (defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
  223. /**
  224. * @brief writes a character to the usart
  225. * @param ch
  226. * *f
  227. * @retval the character
  228. */
  229. int fputc(int ch, FILE *f)
  230. {
  231. /* Send a byte to USART */
  232. LL_USART_TransmitData8(DEBUG_USART, ch);
  233. while (!LL_USART_IsActiveFlag_TC(DEBUG_USART));
  234. LL_USART_ClearFlag_TC(DEBUG_USART);
  235. return (ch);
  236. }
  237. /**
  238. * @brief get a character from the usart
  239. * @param *f
  240. * @retval a character
  241. */
  242. int fgetc(FILE *f)
  243. {
  244. int ch;
  245. while (!LL_USART_IsActiveFlag_RXNE(DEBUG_USART));
  246. ch = LL_USART_ReceiveData8(DEBUG_USART);
  247. return (ch);
  248. }
  249. #elif defined(__ICCARM__)
  250. /**
  251. * @brief writes a character to the usart
  252. * @param ch
  253. * *f
  254. * @retval the character
  255. */
  256. int putchar(int ch)
  257. {
  258. /* Send a byte to USART */
  259. LL_USART_TransmitData8(DEBUG_USART, ch);
  260. while (!LL_USART_IsActiveFlag_TC(DEBUG_USART));
  261. LL_USART_ClearFlag_TC(DEBUG_USART);
  262. return (ch);
  263. }
  264. #elif defined(__GNUC__)
  265. /**
  266. * @brief writes a character to the usart
  267. * @param ch
  268. * @retval the character
  269. */
  270. int __io_putchar(int ch)
  271. {
  272. /* Send a byte to USART */
  273. LL_USART_TransmitData8(DEBUG_USART, ch);
  274. while (!LL_USART_IsActiveFlag_TC(DEBUG_USART));
  275. LL_USART_ClearFlag_TC(DEBUG_USART);
  276. return ch;
  277. }
  278. int _write(int file, char *ptr, int len)
  279. {
  280. int DataIdx;
  281. for (DataIdx=0;DataIdx<len;DataIdx++)
  282. {
  283. __io_putchar(*ptr++);
  284. }
  285. return len;
  286. }
  287. #endif
  288. /**
  289. * @}
  290. */
  291. /************************ (C) COPYRIGHT Puya *****END OF FILE****/