yc_timer.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. static unsigned int system_time_get_run_tickms(void)
  31. {
  32. return sys_run_tickms;
  33. }
  34. static unsigned int system_time_get_run_ticks(void)
  35. {
  36. return sys_run_ticks;
  37. }
  38. static unsigned short MsCnt = 0;
  39. void SYStick_handle() //1ms中断一次
  40. {
  41. //SYS_ClkTicks();
  42. //sys_Timer_Check_Flag = true;
  43. // SYS_timerPolling();
  44. if(++MsCnt>999)
  45. {
  46. MsCnt=0;
  47. sys_run_ticks++;
  48. }
  49. sys_run_tickms++;
  50. TaskManager_Scheduling();
  51. }
  52. void SYS_delay_ms(uint32_t nms)
  53. {
  54. SYS_delay_us(nms*1000);
  55. }
  56. void SYS_delay_us(uint32_t nus){
  57. uint32_t ticks;
  58. uint32_t told,tnow,tcnt=0;
  59. uint32_t reload=SysTick->LOAD;
  60. ticks=nus*(SYSTEM_CLOCK/1000000);
  61. tcnt=0;
  62. told=SysTick->VAL;
  63. while(1){
  64. tnow=SysTick->VAL;
  65. if(tnow!=told){
  66. if(tnow<told)
  67. tcnt+=told-tnow;
  68. else
  69. tcnt+=reload-tnow+told;
  70. told=tnow;
  71. if(tcnt>=ticks)
  72. break;
  73. }
  74. };
  75. }
  76. sys_time_handle_t sys_time_handle={
  77. .init= SYS_TimerInit,
  78. .get_run_ticks=system_time_get_run_ticks,
  79. .get_run_tickms=system_time_get_run_tickms
  80. } ;