py32f002bxx_Start_Kit.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /**
  2. ******************************************************************************
  3. * @file py32f002bxx_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_Start_Kit.h"
  33. /**
  34. * @brief PY32F002Bxx STK BSP Driver version number
  35. */
  36. #define __PY32F002Bxx_STK_BSP_VERSION_MAIN (0x01U) /*!< [31:24] main version */
  37. #define __PY32F002Bxx_STK_BSP_VERSION_SUB1 (0x00U) /*!< [23:16] sub1 version */
  38. #define __PY32F002Bxx_STK_BSP_VERSION_SUB2 (0x00U) /*!< [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. #ifdef HAL_UART_MODULE_ENABLED
  45. UART_HandleTypeDef DebugUartHandle;
  46. #endif
  47. GPIO_TypeDef* LED_PORT[LEDn] = {LED3_GPIO_PORT};
  48. const uint16_t LED_PIN[LEDn] = {LED3_PIN};
  49. GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {USER_BUTTON_GPIO_PORT };
  50. const uint16_t BUTTON_PIN[BUTTONn] = {USER_BUTTON_PIN };
  51. const uint8_t BUTTON_IRQn[BUTTONn] = {USER_BUTTON_EXTI_IRQn };
  52. /** @addtogroup PY32F002Bxx_STK_Exported_Functions
  53. * @{
  54. */
  55. /**
  56. * @brief This method returns the PY32F002B STK BSP Driver revision.
  57. * @retval version : 0xXYZR (8bits for each decimal, R for RC)
  58. */
  59. uint32_t BSP_GetVersion(void)
  60. {
  61. return __PY32F002Bxx_STK_BSP_VERSION;
  62. }
  63. /** @addtogroup LED_Functions
  64. * @{
  65. */
  66. /**
  67. * @brief Configures LED GPIO.
  68. * @param Led Specifies the Led to be configured.
  69. * This parameter can be one of following parameters:
  70. * @arg LED3
  71. * @retval None
  72. */
  73. void BSP_LED_Init(Led_TypeDef Led)
  74. {
  75. GPIO_InitTypeDef GPIO_InitStruct;
  76. /* Enable the GPIO_LED Clock */
  77. LEDx_GPIO_CLK_ENABLE(Led);
  78. /* Configure the GPIO_LED pin */
  79. GPIO_InitStruct.Pin = LED_PIN[Led];
  80. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  81. GPIO_InitStruct.Pull = GPIO_NOPULL;
  82. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  83. HAL_GPIO_Init(LED_PORT[Led], &GPIO_InitStruct);
  84. HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_SET);
  85. }
  86. /**
  87. * @brief DeInitialize LED GPIO.
  88. * @param Led Specifies the Led to be deconfigured.
  89. * This parameter can be one of the following values:
  90. * @arg LED3
  91. * @note BSP_LED_DeInit() does not disable the GPIO clock
  92. * @retval None
  93. */
  94. void BSP_LED_DeInit(Led_TypeDef Led)
  95. {
  96. GPIO_InitTypeDef GPIO_InitStruct;
  97. /* Turn off LED */
  98. HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_RESET);
  99. /* DeInit the GPIO_LED pin */
  100. GPIO_InitStruct.Pin = LED_PIN[Led];
  101. HAL_GPIO_DeInit(LED_PORT[Led], GPIO_InitStruct.Pin);
  102. }
  103. /**
  104. * @brief Turns selected LED On.
  105. * @param Led Specifies the Led to be set on.
  106. * This parameter can be one of following parameters:
  107. * @arg LED3
  108. * @retval None
  109. */
  110. void BSP_LED_On(Led_TypeDef Led)
  111. {
  112. HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_RESET);
  113. }
  114. /**
  115. * @brief Turns selected LED Off.
  116. * @param Led Specifies the Led to be set off.
  117. * This parameter can be one of following parameters:
  118. * @arg LED3
  119. * @retval None
  120. */
  121. void BSP_LED_Off(Led_TypeDef Led)
  122. {
  123. HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_SET);
  124. }
  125. /**
  126. * @brief Toggles the selected LED.
  127. * @param Led Specifies the Led to be toggled.
  128. * This parameter can be one of following parameters:
  129. * @arg LED3
  130. * @retval None
  131. */
  132. void BSP_LED_Toggle(Led_TypeDef Led)
  133. {
  134. HAL_GPIO_TogglePin(LED_PORT[Led], LED_PIN[Led]);
  135. }
  136. /**
  137. * @brief Configures Button GPIO and EXTI Line.
  138. * @param Button: Specifies the Button to be configured.
  139. * This parameter should be: BUTTON_USER
  140. * @param ButtonMode: Specifies Button mode.
  141. * This parameter can be one of following parameters:
  142. * @arg BUTTON_MODE_GPIO: Button will be used as simple IO
  143. * @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt
  144. * generation capability
  145. * @retval None
  146. */
  147. void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode)
  148. {
  149. GPIO_InitTypeDef gpioinitstruct;
  150. /* Enable the BUTTON Clock */
  151. BUTTONx_GPIO_CLK_ENABLE(Button);
  152. gpioinitstruct.Pin = BUTTON_PIN[Button];
  153. gpioinitstruct.Pull = GPIO_NOPULL;
  154. gpioinitstruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  155. if(ButtonMode == BUTTON_MODE_GPIO)
  156. {
  157. /* Configure Button pin as input */
  158. gpioinitstruct.Mode = GPIO_MODE_INPUT;
  159. HAL_GPIO_Init(BUTTON_PORT[Button], &gpioinitstruct);
  160. }
  161. if(ButtonMode == BUTTON_MODE_EXTI)
  162. {
  163. /* Configure Button pin as input with External interrupt */
  164. gpioinitstruct.Mode = GPIO_MODE_IT_FALLING;
  165. HAL_GPIO_Init(BUTTON_PORT[Button], &gpioinitstruct);
  166. /* Enable and set Button EXTI Interrupt to the lowest priority */
  167. HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0);
  168. HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
  169. }
  170. }
  171. /**
  172. * @brief Push Button DeInit.
  173. * @param Button: Button to be configured
  174. * This parameter should be: BUTTON_USER
  175. * @note PB DeInit does not disable the GPIO clock
  176. * @retval None
  177. */
  178. void BSP_PB_DeInit(Button_TypeDef Button)
  179. {
  180. GPIO_InitTypeDef gpio_init_structure;
  181. gpio_init_structure.Pin = BUTTON_PIN[Button];
  182. HAL_NVIC_DisableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
  183. HAL_GPIO_DeInit(BUTTON_PORT[Button], gpio_init_structure.Pin);
  184. }
  185. /**
  186. * @brief Returns the selected Button state.
  187. * @param Button: Specifies the Button to be checked.
  188. * This parameter should be: BUTTON_USER
  189. * @retval Button state.
  190. */
  191. uint32_t BSP_PB_GetState(Button_TypeDef Button)
  192. {
  193. return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
  194. }
  195. #ifdef HAL_UART_MODULE_ENABLED
  196. /**
  197. * @brief DEBUG_USART GPIO Config,Mode Config,115200 8-N-1
  198. * @param None
  199. * @retval None
  200. */
  201. void BSP_USART_Config(void)
  202. {
  203. GPIO_InitTypeDef GPIO_InitStruct;
  204. #if defined (__GNUC__)
  205. setvbuf(stdout,NULL,_IONBF,0);
  206. #endif
  207. DEBUG_USART_CLK_ENABLE();
  208. DebugUartHandle.Instance = DEBUG_USART;
  209. DebugUartHandle.Init.BaudRate = DEBUG_USART_BAUDRATE;
  210. DebugUartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  211. DebugUartHandle.Init.StopBits = UART_STOPBITS_1;
  212. DebugUartHandle.Init.Parity = UART_PARITY_NONE;
  213. DebugUartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  214. DebugUartHandle.Init.Mode = UART_MODE_TX_RX;
  215. HAL_UART_Init(&DebugUartHandle);
  216. DEBUG_USART_RX_GPIO_CLK_ENABLE();
  217. DEBUG_USART_TX_GPIO_CLK_ENABLE();
  218. /**USART GPIO Configuration
  219. PB4 ------> USART1_TX
  220. PB5 ------> USART1_RX
  221. */
  222. GPIO_InitStruct.Pin = DEBUG_USART_TX_PIN;
  223. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  224. GPIO_InitStruct.Pull = GPIO_PULLUP;
  225. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  226. GPIO_InitStruct.Alternate = DEBUG_USART_TX_AF;
  227. HAL_GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStruct);
  228. GPIO_InitStruct.Pin = DEBUG_USART_RX_PIN;
  229. GPIO_InitStruct.Alternate = DEBUG_USART_RX_AF;
  230. HAL_GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStruct);
  231. /* ENABLE NVIC */
  232. HAL_NVIC_SetPriority(DEBUG_USART_IRQ,0,1);
  233. HAL_NVIC_EnableIRQ(DEBUG_USART_IRQ );
  234. }
  235. #if (defined (__CC_ARM)) || (defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
  236. /**
  237. * @brief writes a character to the usart
  238. * @param ch
  239. * *f
  240. * @retval the character
  241. */
  242. int fputc(int ch, FILE *f)
  243. {
  244. /* Send a byte to USART */
  245. HAL_UART_Transmit(&DebugUartHandle, (uint8_t *)&ch, 1, 1000);
  246. return (ch);
  247. }
  248. /**
  249. * @brief get a character from the usart
  250. * @param *f
  251. * @retval a character
  252. */
  253. int fgetc(FILE *f)
  254. {
  255. int ch;
  256. HAL_UART_Receive(&DebugUartHandle, (uint8_t *)&ch, 1, 1000);
  257. return (ch);
  258. }
  259. #elif defined(__ICCARM__)
  260. /**
  261. * @brief writes a character to the usart
  262. * @param ch
  263. * *f
  264. * @retval the character
  265. */
  266. int putchar(int ch)
  267. {
  268. /* Send a byte to USART */
  269. HAL_UART_Transmit(&DebugUartHandle, (uint8_t *)&ch, 1, 1000);
  270. return (ch);
  271. }
  272. #elif defined(__GNUC__)
  273. /**
  274. * @brief writes a character to the usart
  275. * @param ch
  276. * @retval the character
  277. */
  278. int __io_putchar(int ch)
  279. {
  280. /* Send a byte to USART */
  281. HAL_UART_Transmit(&DebugUartHandle, (uint8_t *)&ch, 1, 1000);
  282. return ch;
  283. }
  284. int _write(int file, char *ptr, int len)
  285. {
  286. int DataIdx;
  287. for (DataIdx=0;DataIdx<len;DataIdx++)
  288. {
  289. __io_putchar(*ptr++);
  290. }
  291. return len;
  292. }
  293. #endif
  294. #endif
  295. /**
  296. * @}
  297. */
  298. /**
  299. * @}
  300. */
  301. /**
  302. * @}
  303. */
  304. /************************ (C) COPYRIGHT Puya *****END OF FILE****/