py32f003xx_Start_Kit.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /**
  2. ******************************************************************************
  3. * @file py32f003xx_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 "py32f003xx_Start_Kit.h"
  33. /**
  34. * @brief PY32F003xx STK BSP Driver version number
  35. */
  36. #define __PY32F003xx_STK_BSP_VERSION_MAIN (0x01U) /*!< [31:24] main version */
  37. #define __PY32F003xx_STK_BSP_VERSION_SUB1 (0x00U) /*!< [23:16] sub1 version */
  38. #define __PY32F003xx_STK_BSP_VERSION_SUB2 (0x00U) /*!< [15:8] sub2 version */
  39. #define __PY32F003xx_STK_BSP_VERSION_RC (0x00U) /*!< [7:0] release candidate */
  40. #define __PY32F003xx_STK_BSP_VERSION ((__PY32F003xx_STK_BSP_VERSION_MAIN << 24) \
  41. |(__PY32F003xx_STK_BSP_VERSION_SUB1 << 16) \
  42. |(__PY32F003xx_STK_BSP_VERSION_SUB2 << 8 ) \
  43. |(__PY32F003xx_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 PY32F003xx_STK_Exported_Functions
  53. * @{
  54. */
  55. /**
  56. * @brief This method returns the PY32F030 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 __PY32F003xx_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. #if StartKitVersion == 2
  194. return (HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]) == GPIO_PIN_RESET);
  195. #else
  196. return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
  197. #endif
  198. }
  199. #ifdef HAL_UART_MODULE_ENABLED
  200. /**
  201. * @brief DEBUG_USART GPIO Config,Mode Config,115200 8-N-1
  202. * @param None
  203. * @retval None
  204. */
  205. void BSP_USART_Config(void)
  206. {
  207. GPIO_InitTypeDef GPIO_InitStruct;
  208. #if defined(__GNUC__)
  209. setvbuf(stdout, NULL, _IONBF, 0 );
  210. #endif
  211. DEBUG_USART_CLK_ENABLE();
  212. DebugUartHandle.Instance = DEBUG_USART;
  213. DebugUartHandle.Init.BaudRate = DEBUG_USART_BAUDRATE;
  214. DebugUartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  215. DebugUartHandle.Init.StopBits = UART_STOPBITS_1;
  216. DebugUartHandle.Init.Parity = UART_PARITY_NONE;
  217. DebugUartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  218. DebugUartHandle.Init.Mode = UART_MODE_TX_RX;
  219. DebugUartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
  220. DebugUartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  221. HAL_UART_Init(&DebugUartHandle);
  222. DEBUG_USART_RX_GPIO_CLK_ENABLE();
  223. DEBUG_USART_TX_GPIO_CLK_ENABLE();
  224. /**USART GPIO Configuration
  225. PA2 ------> USART2_TX
  226. PA3 ------> USART2_RX
  227. */
  228. GPIO_InitStruct.Pin = DEBUG_USART_TX_PIN;
  229. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  230. GPIO_InitStruct.Pull = GPIO_PULLUP;
  231. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  232. GPIO_InitStruct.Alternate = DEBUG_USART_TX_AF;
  233. HAL_GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStruct);
  234. GPIO_InitStruct.Pin = DEBUG_USART_RX_PIN;
  235. GPIO_InitStruct.Alternate = DEBUG_USART_RX_AF;
  236. HAL_GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStruct);
  237. /* ENABLE NVIC */
  238. HAL_NVIC_SetPriority(DEBUG_USART_IRQ,0,1);
  239. HAL_NVIC_EnableIRQ(DEBUG_USART_IRQ );
  240. }
  241. #if (defined (__CC_ARM)) || (defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
  242. /**
  243. * @brief writes a character to the usart
  244. * @param ch
  245. * *f
  246. * @retval the character
  247. */
  248. int fputc(int ch, FILE *f)
  249. {
  250. /* Send a byte to USART */
  251. HAL_UART_Transmit(&DebugUartHandle, (uint8_t *)&ch, 1, 1000);
  252. return (ch);
  253. }
  254. /**
  255. * @brief get a character from the usart
  256. * @param *f
  257. * @retval a character
  258. */
  259. int fgetc(FILE *f)
  260. {
  261. int ch;
  262. HAL_UART_Receive(&DebugUartHandle, (uint8_t *)&ch, 1, 1000);
  263. return (ch);
  264. }
  265. #elif defined(__ICCARM__)
  266. /**
  267. * @brief writes a character to the usart
  268. * @param ch
  269. * *f
  270. * @retval the character
  271. */
  272. int putchar(int ch)
  273. {
  274. /* Send a byte to USART */
  275. HAL_UART_Transmit(&DebugUartHandle, (uint8_t *)&ch, 1, 1000);
  276. return (ch);
  277. }
  278. #elif defined(__GNUC__)
  279. /**
  280. * @brief writes a character to the usart
  281. * @param ch
  282. * @retval the character
  283. */
  284. int __io_putchar(int ch)
  285. {
  286. /* Send a byte to USART */
  287. HAL_UART_Transmit(&DebugUartHandle, (uint8_t *)&ch, 1, 1000);
  288. return ch;
  289. }
  290. int _write(int file, char *ptr, int len)
  291. {
  292. int DataIdx;
  293. for (DataIdx=0;DataIdx<len;DataIdx++)
  294. {
  295. __io_putchar(*ptr++);
  296. }
  297. return len;
  298. }
  299. #endif
  300. #else
  301. static uint32_t ReloadD = 0;
  302. static uint32_t MinD = 0;
  303. /**
  304. * @brief GPIO Config As USART
  305. * @param None
  306. * @retval None
  307. */
  308. void BSP_USART_Config(void)
  309. {
  310. GPIO_InitTypeDef GPIO_InitStruct = {0};
  311. DEBUG_USART_TX_GPIO_CLK_ENABLE();
  312. GPIO_InitStruct.Pin = DEBUG_USART_TX_PIN;
  313. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; /* Push-pull output */
  314. GPIO_InitStruct.Pull = GPIO_PULLUP; /* Enable pull-up */
  315. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; /* GPIO speed */
  316. /* GPIO Initialization */
  317. HAL_GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStruct);
  318. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,(GPIO_PinState)1);
  319. ReloadD = SysTick->LOAD;
  320. MinD = (ReloadD+1)*10/96;
  321. }
  322. /**
  323. * @brief writes a character to the usart
  324. * @param SystickStart
  325. * @param ReloadData
  326. * @param MinData
  327. * @retval NULL
  328. */
  329. static void BSP_Delay(uint32_t SystickStart,uint32_t ReloadData, uint32_t MinData)
  330. {
  331. uint32_t SystickCurrent = 0;
  332. while(1)
  333. {
  334. SystickCurrent = SysTick->VAL;
  335. if(SystickCurrent < SystickStart)
  336. {
  337. if((SystickStart-SystickCurrent) >= MinData)
  338. {
  339. break;
  340. }
  341. }
  342. else
  343. {
  344. if(SystickCurrent <= (ReloadData+1 - MinData + SystickStart))
  345. {
  346. break;
  347. }
  348. }
  349. }
  350. }
  351. /**
  352. * @brief writes a character to the usart
  353. * @param ch
  354. * *f
  355. * @retval the character
  356. */
  357. static void BSP_TransmitChar(uint8_t ch)
  358. {
  359. uint8_t temp = 0;
  360. uint32_t SystickStart = SysTick->VAL ;
  361. for(uint8_t i=0;i<10;i++)
  362. {
  363. if(i==0)
  364. {
  365. HAL_GPIO_WritePin(DEBUG_USART_TX_GPIO_PORT,DEBUG_USART_TX_PIN,GPIO_PIN_RESET);
  366. }
  367. else if(i == 9)
  368. {
  369. BSP_Delay(SystickStart, ReloadD, MinD);
  370. HAL_GPIO_WritePin(DEBUG_USART_TX_GPIO_PORT,DEBUG_USART_TX_PIN,GPIO_PIN_SET);
  371. if(SystickStart >= MinD)
  372. {
  373. SystickStart = SystickStart - MinD;
  374. }
  375. else
  376. {
  377. SystickStart = ReloadD+1 + SystickStart - MinD;
  378. }
  379. BSP_Delay(SystickStart, ReloadD, MinD);
  380. }
  381. else
  382. {
  383. temp=ch&0x01;
  384. BSP_Delay(SystickStart, ReloadD, MinD);
  385. switch(temp)
  386. {
  387. case 1:
  388. {
  389. HAL_GPIO_WritePin(DEBUG_USART_TX_GPIO_PORT,DEBUG_USART_TX_PIN,GPIO_PIN_SET);
  390. break;
  391. }
  392. case 0:
  393. {
  394. HAL_GPIO_WritePin(DEBUG_USART_TX_GPIO_PORT,DEBUG_USART_TX_PIN,GPIO_PIN_RESET);
  395. break;
  396. }
  397. default: break;
  398. }
  399. ch>>=1;
  400. if(SystickStart > MinD)
  401. {
  402. SystickStart = SystickStart - MinD;
  403. }
  404. else
  405. {
  406. SystickStart = ReloadD + 1 + SystickStart - MinD;
  407. }
  408. }
  409. }
  410. }
  411. #if (defined (__CC_ARM)) || (defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
  412. /**
  413. * @brief writes a character to the usart
  414. * @param ch
  415. * *f
  416. * @retval the character
  417. */
  418. int fputc(int ch, FILE *f)
  419. {
  420. BSP_TransmitChar(ch);
  421. return (ch);
  422. }
  423. #elif defined(__ICCARM__)
  424. /**
  425. * @brief writes a character to the usart
  426. * @param ch
  427. * *f
  428. * @retval the character
  429. */
  430. int putchar(int ch)
  431. {
  432. /* Send a byte to USART */
  433. BSP_TransmitChar(ch);
  434. return (ch);
  435. }
  436. #elif defined(__GNUC__)
  437. /**
  438. * @brief writes a character to the usart
  439. * @param ch
  440. * @retval the character
  441. */
  442. int __io_putchar(int ch)
  443. {
  444. /* Send a byte to USART */
  445. BSP_TransmitChar(ch);
  446. return ch;
  447. }
  448. int _write(int file, char *ptr, int len)
  449. {
  450. int DataIdx;
  451. for (DataIdx=0;DataIdx<len;DataIdx++)
  452. {
  453. __io_putchar(*ptr++);
  454. }
  455. return len;
  456. }
  457. #endif
  458. #endif
  459. /**
  460. * @}
  461. */
  462. /**
  463. * @}
  464. */
  465. /**
  466. * @}
  467. */
  468. /************************ (C) COPYRIGHT Puya *****END OF FILE****/