yc_timer.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * COPYRIGHT NOTICE
  3. *Copyright(c) 2014,YICHIP
  4. *
  5. * @file yc_timer.c
  6. * @brief ...
  7. *
  8. * @version 1.0
  9. * @author jingzou
  10. * @data Jan 23, 2018
  11. **/
  12. #include "yc_timer.h"
  13. #include "yc11xx.h"
  14. #include "core_cm0.h"
  15. #include "LH_TaskManager.h"
  16. uint32_t sys_tick_count;
  17. uint8_t sys_Timer_Check_Flag;
  18. uint32_t gSystemTimerAdjustClknbt;
  19. static unsigned int sys_run_tickms=0; //从定时器初始化运行到当前的毫秒
  20. static unsigned int sys_run_ticks=0; //从定时器初始化运行到当前的秒
  21. void SYS_TimerExpireDefaultHandle(int params)
  22. {
  23. // YC_LOG_ERROR("default timer expire !\r\n");
  24. while(0);
  25. }
  26. void SYS_TimerInit()
  27. {
  28. SysTick_Config(SYSTEM_CLOCK/CLOCK_DIVISOR); //each systick interrupt is 1ms
  29. }
  30. extern uint32_t UserCnt;
  31. static unsigned int system_time_get_run_tickms(void)
  32. {
  33. return sys_run_tickms;
  34. }
  35. static unsigned int system_time_get_run_ticks(void)
  36. {
  37. return sys_run_ticks;
  38. }
  39. static unsigned short MsCnt = 0;
  40. void SYStick_handle() //1ms中断一次
  41. {
  42. //SYS_ClkTicks();
  43. //sys_Timer_Check_Flag = true;
  44. // SYS_timerPolling();
  45. if(++MsCnt>999)
  46. {
  47. MsCnt=0;
  48. sys_run_ticks++;
  49. }
  50. sys_run_tickms++;
  51. TaskManager_Scheduling();
  52. }
  53. void SYS_delay_ms(uint32_t nms)
  54. {
  55. SYS_delay_us(nms*1000);
  56. }
  57. void SYS_delay_us(uint32_t nus){
  58. uint32_t ticks;
  59. uint32_t told,tnow,tcnt=0;
  60. uint32_t reload=SysTick->LOAD;
  61. ticks=nus*(SYSTEM_CLOCK/1000000);
  62. tcnt=0;
  63. told=SysTick->VAL;
  64. while(1){
  65. tnow=SysTick->VAL;
  66. if(tnow!=told){
  67. if(tnow<told)
  68. tcnt+=told-tnow;
  69. else
  70. tcnt+=reload-tnow+told;
  71. told=tnow;
  72. if(tcnt>=ticks)
  73. break;
  74. }
  75. };
  76. }
  77. sys_time_handle_t sys_time_handle={
  78. .init= SYS_TimerInit,
  79. .get_run_ticks=system_time_get_run_ticks,
  80. .get_run_tickms=system_time_get_run_tickms
  81. } ;