handshake_timer.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "asm/includes.h"
  2. #include "system/includes.h"
  3. #include "app_config.h"
  4. #include "chargebox.h"
  5. #if TCFG_HANDSHAKE_ENABLE
  6. #define HS_TIMER JL_TIMER2
  7. static u16 delay_tap[HS_DELAY_16US + 1] = {
  8. 19, 39, 52, 141, 158, 309, 355,//timer clk = 24M, sys clk = 48m
  9. };
  10. /*------------------------------------------------------------------------------------*/
  11. /**@brief lighting握手延时
  12. @param 无
  13. @return 无
  14. @note 提供不同的ms级延时
  15. */
  16. /*------------------------------------------------------------------------------------*/
  17. void handshake_timer_delay_ms(u8 ms)
  18. {
  19. HS_TIMER->CNT = 0;
  20. #if (TCFG_CLOCK_SYS_SRC == SYS_CLOCK_INPUT_PLL_RCL)
  21. HS_TIMER->PRD = ms * (clk_get("lsb") / (2 * 1000));
  22. HS_TIMER->CON = BIT(0) | BIT(6) | BIT(14); //系统时钟,lsb, div 2
  23. #else
  24. HS_TIMER->PRD = ms * (clk_get("timer") / 1000);
  25. HS_TIMER->CON = BIT(0) | BIT(3) | BIT(14); //1分频,osc时钟,24m,24次就1us
  26. #endif
  27. while (!(HS_TIMER->CON & BIT(15))); //等pending
  28. HS_TIMER->CON = 0;
  29. }
  30. /*------------------------------------------------------------------------------------*/
  31. /**@brief lighting握手延时
  32. @param 无
  33. @return 无
  34. @note 提供不同的us级延时
  35. */
  36. /*------------------------------------------------------------------------------------*/
  37. SEC(.chargebox_code)
  38. void handshake_timer_delay_us(u8 us)
  39. {
  40. //delay 值要根据不同的频率去调整,小于48m的要先设置48m,方便延时
  41. HS_TIMER->CNT = 0;
  42. #if (TCFG_CLOCK_SYS_SRC == SYS_CLOCK_INPUT_PLL_RCL)
  43. HS_TIMER->PRD = delay_tap[us];//这里不允许做乘除法
  44. HS_TIMER->CON = BIT(0) | BIT(6) | BIT(14); //系统时钟,lsb, div 2, 必须为24M
  45. #else
  46. HS_TIMER->PRD = delay_tap[us];
  47. HS_TIMER->CON = BIT(0) | BIT(3) | BIT(14); //1分频,osc时钟,24m,24次就1us
  48. #endif
  49. while (!(HS_TIMER->CON & BIT(15))); //等pending
  50. HS_TIMER->CON = 0;
  51. }
  52. #endif