platform_timer.c 1.4 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: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. #if 1
  15. return (uint32_t)xTaskGetTickCount();
  16. #else
  17. TickType_t tick = 0u;
  18. tick = xTaskGetTickCount() * 1000;
  19. return (uint32_t)((tick + configTICK_RATE_HZ - 1) / configTICK_RATE_HZ);
  20. #endif
  21. }
  22. void platform_timer_init(platform_timer_t* timer)
  23. {
  24. timer->time = 0;
  25. }
  26. void platform_timer_cutdown(platform_timer_t* timer, unsigned int timeout)
  27. {
  28. timer->time = platform_uptime_ms();
  29. timer->time += timeout;
  30. }
  31. char platform_timer_is_expired(platform_timer_t* timer)
  32. {
  33. return platform_uptime_ms() > timer->time ? 1 : 0;
  34. }
  35. int platform_timer_remain(platform_timer_t* timer)
  36. {
  37. uint32_t now;
  38. now = platform_uptime_ms();
  39. if (timer->time <= now) {
  40. return 0;
  41. }
  42. return timer->time - now;
  43. }
  44. unsigned long platform_timer_now(void)
  45. {
  46. return (unsigned long) platform_uptime_ms();
  47. }
  48. void platform_timer_usleep(unsigned long usec)
  49. {
  50. TickType_t tick;
  51. if(usec != 0) {
  52. tick = usec / portTICK_PERIOD_MS;
  53. if (tick == 0)
  54. tick = 1;
  55. }
  56. vTaskDelay(tick);
  57. }