platform_timer.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * @Author: jiejie
  3. * @Github: https://github.com/jiejieTop
  4. * @Date: 2019-12-10 22:16:41
  5. * @LastEditTime: 2020-04-27 22:35:34
  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. #include "FreeRTOS.h"
  10. #include "task.h"
  11. static uint32_t platform_uptime_ms(void)
  12. {
  13. #if (configTICK_RATE_HZ == 1000)
  14. return (uint32_t)xTaskGetTickCount();
  15. #else
  16. TickType_t tick = 0u;
  17. tick = xTaskGetTickCount() * 1000;
  18. return (uint32_t)((tick + configTICK_RATE_HZ - 1) / configTICK_RATE_HZ);
  19. #endif
  20. }
  21. void platform_timer_init(platform_timer_t* timer)
  22. {
  23. timer->time = 0;
  24. }
  25. void platform_timer_cutdown(platform_timer_t* timer, unsigned int timeout)
  26. {
  27. timer->time = platform_uptime_ms();
  28. timer->time += timeout;
  29. }
  30. char platform_timer_is_expired(platform_timer_t* timer)
  31. {
  32. return platform_uptime_ms() > timer->time ? 1 : 0;
  33. }
  34. int platform_timer_remain(platform_timer_t* timer)
  35. {
  36. uint32_t now;
  37. now = platform_uptime_ms();
  38. if (timer->time <= now) {
  39. return 0;
  40. }
  41. return timer->time - now;
  42. }
  43. unsigned long platform_timer_now(void)
  44. {
  45. return (unsigned long) platform_uptime_ms();
  46. }
  47. void platform_timer_usleep(unsigned long usec)
  48. {
  49. TickType_t tick;
  50. if(usec != 0) {
  51. tick = usec / portTICK_PERIOD_MS;
  52. if (tick == 0)
  53. tick = 1;
  54. }
  55. vTaskDelay(tick);
  56. }