platform_timer.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * @Author: jiejie
  3. * @Github: https://github.com/jiejieTop
  4. * @Date: 2019-12-10 22:16:41
  5. * @LastEditTime: 2020-06-17 21:24:24
  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 (RT_TICK_PER_SECOND == 1000)
  12. return (uint32_t)rt_tick_get();
  13. #else
  14. rt_tick_t tick = 0u;
  15. tick = rt_tick_get() * 1000;
  16. return (uint32_t)((tick + RT_TICK_PER_SECOND - 1) / RT_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 = 0;
  48. if(usec != 0) {
  49. ms = usec / 1000;
  50. if (ms == 0) {
  51. ms = 1;
  52. }
  53. }
  54. rt_thread_mdelay(ms);
  55. }