platform_timer.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * @Author: jiejie
  3. * @Github: https://github.com/jiejieTop
  4. * @Date: 2019-12-10 22:16:41
  5. * @LastEditTime: 2020-04-27 22:37:33
  6. * @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
  7. */
  8. #include "platform_timer.h"
  9. static uint32_t platform_uptime_ms(void)
  10. {
  11. #if (TOS_CFG_CPU_TICK_PER_SECOND == 1000)
  12. return (uint32_t)tos_systick_get();
  13. #else
  14. k_tick_t tick = 0u;
  15. tick = tos_systick_get() * 1000;
  16. return (uint32_t)((tick + TOS_CFG_CPU_TICK_PER_SECOND - 1) / TOS_CFG_CPU_TICK_PER_SECOND);
  17. #endif
  18. }
  19. void platform_timer_init(platform_timer_t* timer)
  20. {
  21. timer->time = 0;
  22. }
  23. void platform_timer_cutdown(platform_timer_t* timer, unsigned int timeout)
  24. {
  25. timer->time = platform_uptime_ms();
  26. timer->time += timeout;
  27. }
  28. char platform_timer_is_expired(platform_timer_t* timer)
  29. {
  30. return platform_uptime_ms() > timer->time ? 1 : 0;
  31. }
  32. int platform_timer_remain(platform_timer_t* timer)
  33. {
  34. uint32_t now;
  35. now = platform_uptime_ms();
  36. if (timer->time <= now) {
  37. return 0;
  38. }
  39. return timer->time - now;
  40. }
  41. unsigned long platform_timer_now(void)
  42. {
  43. return (unsigned long) platform_uptime_ms();
  44. }
  45. void platform_timer_usleep(unsigned long usec)
  46. {
  47. uint32_t ms;
  48. k_tick_t tick;
  49. if(usec != 0) {
  50. ms = usec / TOS_CFG_CPU_TICK_PER_SECOND;
  51. if (ms == 0) {
  52. ms = 1;
  53. }
  54. }
  55. tick = tos_millisec2tick(ms);
  56. tos_sleep_ms(tick);
  57. }