py32f002xx_ll_Start_Kit.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /**
  2. ******************************************************************************
  3. * @file py32f002xx_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 "py32f002xx_ll_Start_Kit.h"
  33. /**
  34. * @brief PY32F002xx STK BSP Driver version number
  35. */
  36. #define __PY32F002xx_STK_BSP_VERSION_MAIN (0x01U) /*!< [31:24] main version */
  37. #define __PY32F002xx_STK_BSP_VERSION_SUB1 (0x00U) /*!< [23:16] sub1 version */
  38. #define __PY32F002xx_STK_BSP_VERSION_SUB2 (0x00U) /*!< [15:8] sub2 version */
  39. #define __PY32F002xx_STK_BSP_VERSION_RC (0x00U) /*!< [7:0] release candidate */
  40. #define __PY32F002xx_STK_BSP_VERSION ((__PY32F002xx_STK_BSP_VERSION_MAIN << 24) \
  41. |(__PY32F002xx_STK_BSP_VERSION_SUB1 << 16) \
  42. |(__PY32F002xx_STK_BSP_VERSION_SUB2 << 8 ) \
  43. |(__PY32F002xx_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 PY32F002xx_STK_Exported_Functions
  51. * @{
  52. */
  53. /**
  54. * @brief This method returns the PY32F002 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 __PY32F002xx_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. #if StartKitVersion == 2
  186. return (LL_GPIO_IsInputPinSet(BUTTON_PORT[Button], BUTTON_PIN[Button]) == RESET);
  187. #else
  188. return LL_GPIO_IsInputPinSet(BUTTON_PORT[Button], BUTTON_PIN[Button]);
  189. #endif
  190. }
  191. #ifdef USART1
  192. /**
  193. * @brief DEBUG_USART GPIO Config,Mode Config,115200 8-N-1
  194. * @param None
  195. * @retval None
  196. */
  197. void BSP_USART_Config(void)
  198. {
  199. #if defined(__GNUC__)
  200. setvbuf(stdout, NULL, _IONBF, 0 );
  201. #endif
  202. DEBUG_USART_CLK_ENABLE();
  203. /* USART Init */
  204. LL_USART_SetBaudRate(DEBUG_USART, SystemCoreClock, LL_USART_OVERSAMPLING_16, DEBUG_USART_BAUDRATE);
  205. LL_USART_SetDataWidth(DEBUG_USART, LL_USART_DATAWIDTH_8B);
  206. LL_USART_SetStopBitsLength(DEBUG_USART, LL_USART_STOPBITS_1);
  207. LL_USART_SetParity(DEBUG_USART, LL_USART_PARITY_NONE);
  208. LL_USART_SetHWFlowCtrl(DEBUG_USART, LL_USART_HWCONTROL_NONE);
  209. LL_USART_SetTransferDirection(DEBUG_USART, LL_USART_DIRECTION_TX_RX);
  210. LL_USART_Enable(DEBUG_USART);
  211. LL_USART_ClearFlag_TC(DEBUG_USART);
  212. /**USART GPIO Configuration
  213. PA2 ------> USART1_TX
  214. PA3 ------> USART1_RX
  215. */
  216. DEBUG_USART_RX_GPIO_CLK_ENABLE();
  217. DEBUG_USART_TX_GPIO_CLK_ENABLE();
  218. LL_GPIO_SetPinMode(DEBUG_USART_TX_GPIO_PORT, DEBUG_USART_TX_PIN, LL_GPIO_MODE_ALTERNATE);
  219. LL_GPIO_SetPinSpeed(DEBUG_USART_TX_GPIO_PORT, DEBUG_USART_TX_PIN, LL_GPIO_SPEED_FREQ_VERY_HIGH);
  220. LL_GPIO_SetPinPull(DEBUG_USART_TX_GPIO_PORT, DEBUG_USART_TX_PIN, LL_GPIO_PULL_UP);
  221. LL_GPIO_SetAFPin_0_7(DEBUG_USART_TX_GPIO_PORT, DEBUG_USART_TX_PIN, DEBUG_USART_TX_AF);
  222. LL_GPIO_SetPinMode(DEBUG_USART_RX_GPIO_PORT, DEBUG_USART_RX_PIN, LL_GPIO_MODE_ALTERNATE);
  223. LL_GPIO_SetPinSpeed(DEBUG_USART_RX_GPIO_PORT, DEBUG_USART_RX_PIN, LL_GPIO_SPEED_FREQ_VERY_HIGH);
  224. LL_GPIO_SetPinPull(DEBUG_USART_RX_GPIO_PORT, DEBUG_USART_RX_PIN, LL_GPIO_PULL_UP);
  225. LL_GPIO_SetAFPin_0_7(DEBUG_USART_RX_GPIO_PORT, DEBUG_USART_RX_PIN, DEBUG_USART_RX_AF);
  226. }
  227. #if (defined (__CC_ARM)) || (defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
  228. /**
  229. * @brief writes a character to the usart
  230. * @param ch
  231. * *f
  232. * @retval the character
  233. */
  234. int fputc(int ch, FILE *f)
  235. {
  236. /* Send a byte to USART */
  237. LL_USART_TransmitData8(DEBUG_USART, ch);
  238. while (!LL_USART_IsActiveFlag_TC(DEBUG_USART));
  239. LL_USART_ClearFlag_TC(DEBUG_USART);
  240. return (ch);
  241. }
  242. /**
  243. * @brief get a character from the usart
  244. * @param *f
  245. * @retval a character
  246. */
  247. int fgetc(FILE *f)
  248. {
  249. int ch;
  250. while (!LL_USART_IsActiveFlag_RXNE(DEBUG_USART));
  251. ch = LL_USART_ReceiveData8(DEBUG_USART);
  252. return (ch);
  253. }
  254. #elif defined(__ICCARM__)
  255. /**
  256. * @brief writes a character to the usart
  257. * @param ch
  258. * *f
  259. * @retval the character
  260. */
  261. int putchar(int ch)
  262. {
  263. /* Send a byte to USART */
  264. LL_USART_TransmitData8(DEBUG_USART, ch);
  265. while (!LL_USART_IsActiveFlag_TC(DEBUG_USART));
  266. LL_USART_ClearFlag_TC(DEBUG_USART);
  267. return (ch);
  268. }
  269. #elif defined(__GNUC__)
  270. /**
  271. * @brief writes a character to the usart
  272. * @param ch
  273. * @retval the character
  274. */
  275. int __io_putchar(int ch)
  276. {
  277. /* Send a byte to USART */
  278. LL_USART_TransmitData8(DEBUG_USART, ch);
  279. while (!LL_USART_IsActiveFlag_TC(DEBUG_USART));
  280. LL_USART_ClearFlag_TC(DEBUG_USART);
  281. return ch;
  282. }
  283. int _write(int file, char *ptr, int len)
  284. {
  285. int DataIdx;
  286. for (DataIdx=0;DataIdx<len;DataIdx++)
  287. {
  288. __io_putchar(*ptr++);
  289. }
  290. return len;
  291. }
  292. #endif
  293. #else
  294. static uint32_t ReloadD = 0;
  295. static uint32_t MinD = 0;
  296. /**
  297. * @brief GPIO Config As USART
  298. * @param None
  299. * @retval None
  300. */
  301. void BSP_USART_Config(void)
  302. {
  303. DEBUG_USART_TX_GPIO_CLK_ENABLE();
  304. LL_GPIO_SetPinMode(DEBUG_USART_TX_GPIO_PORT, DEBUG_USART_TX_PIN, LL_GPIO_MODE_OUTPUT);
  305. LL_GPIO_SetPinSpeed(DEBUG_USART_TX_GPIO_PORT, DEBUG_USART_TX_PIN, LL_GPIO_SPEED_FREQ_VERY_HIGH);
  306. LL_GPIO_SetPinPull(DEBUG_USART_TX_GPIO_PORT, DEBUG_USART_TX_PIN, LL_GPIO_PULL_UP);
  307. ReloadD = SysTick->LOAD;
  308. MinD = (ReloadD+1)*10/96;
  309. }
  310. /**
  311. * @brief writes a character to the usart
  312. * @param SystickStart
  313. * @param ReloadData
  314. * @param MinData
  315. * @retval NULL
  316. */
  317. static void BSP_Delay(uint32_t SystickStart,uint32_t ReloadData, uint32_t MinData)
  318. {
  319. uint32_t SystickCurrent = 0;
  320. while(1)
  321. {
  322. SystickCurrent = SysTick->VAL;
  323. if(SystickCurrent < SystickStart)
  324. {
  325. if((SystickStart-SystickCurrent) >= MinData)
  326. {
  327. break;
  328. }
  329. }
  330. else
  331. {
  332. if(SystickCurrent <= (ReloadData+1 - MinData + SystickStart))
  333. {
  334. break;
  335. }
  336. }
  337. }
  338. }
  339. /**
  340. * @brief writes a character to the usart
  341. * @param ch
  342. * *f
  343. * @retval the character
  344. */
  345. static void BSP_TransmitChar(uint8_t ch)
  346. {
  347. uint8_t temp = 0;
  348. uint32_t SystickStart = SysTick->VAL ;
  349. for(uint8_t i=0;i<10;i++)
  350. {
  351. if(i==0)
  352. {
  353. LL_GPIO_ResetOutputPin(DEBUG_USART_TX_GPIO_PORT,DEBUG_USART_TX_PIN);
  354. }
  355. else if(i == 9)
  356. {
  357. BSP_Delay(SystickStart, ReloadD, MinD);
  358. LL_GPIO_SetOutputPin(DEBUG_USART_TX_GPIO_PORT,DEBUG_USART_TX_PIN);
  359. if(SystickStart >= MinD)
  360. {
  361. SystickStart = SystickStart - MinD;
  362. }
  363. else
  364. {
  365. SystickStart = ReloadD+1 + SystickStart - MinD;
  366. }
  367. BSP_Delay(SystickStart, ReloadD, MinD);
  368. }
  369. else
  370. {
  371. temp=ch&0x01;
  372. BSP_Delay(SystickStart, ReloadD, MinD);
  373. switch(temp)
  374. {
  375. case 1:
  376. {
  377. LL_GPIO_SetOutputPin(DEBUG_USART_TX_GPIO_PORT,DEBUG_USART_TX_PIN);
  378. break;
  379. }
  380. case 0:
  381. {
  382. LL_GPIO_ResetOutputPin(DEBUG_USART_TX_GPIO_PORT,DEBUG_USART_TX_PIN);
  383. break;
  384. }
  385. default: break;
  386. }
  387. ch>>=1;
  388. if(SystickStart > MinD)
  389. {
  390. SystickStart = SystickStart - MinD;
  391. }
  392. else
  393. {
  394. SystickStart = ReloadD + 1 + SystickStart - MinD;
  395. }
  396. }
  397. }
  398. }
  399. #if (defined (__CC_ARM)) || (defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
  400. /**
  401. * @brief writes a character to the usart
  402. * @param ch
  403. * *f
  404. * @retval the character
  405. */
  406. int fputc(int ch, FILE *f)
  407. {
  408. BSP_TransmitChar(ch);
  409. return (ch);
  410. }
  411. #elif defined(__ICCARM__)
  412. /**
  413. * @brief writes a character to the usart
  414. * @param ch
  415. * *f
  416. * @retval the character
  417. */
  418. int putchar(int ch)
  419. {
  420. /* Send a byte to USART */
  421. BSP_TransmitChar(ch);
  422. return (ch);
  423. }
  424. #elif defined(__GNUC__)
  425. /**
  426. * @brief writes a character to the usart
  427. * @param ch
  428. * @retval the character
  429. */
  430. int __io_putchar(int ch)
  431. {
  432. /* Send a byte to USART */
  433. BSP_TransmitChar(ch);
  434. return ch;
  435. }
  436. int _write(int file, char *ptr, int len)
  437. {
  438. int DataIdx;
  439. for (DataIdx=0;DataIdx<len;DataIdx++)
  440. {
  441. __io_putchar(*ptr++);
  442. }
  443. return len;
  444. }
  445. #endif
  446. #endif
  447. //#endif
  448. /**
  449. * @}
  450. */
  451. /**
  452. * @}
  453. */
  454. /**
  455. * @}
  456. */
  457. /************************ (C) COPYRIGHT Puya *****END OF FILE****/