luat_timer_rtt.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "luat_base.h"
  2. #include "luat_malloc.h"
  3. #include "luat_msgbus.h"
  4. #include "luat_timer.h"
  5. #include "rtthread.h"
  6. #include "rthw.h"
  7. #define DBG_TAG "rtt.timer"
  8. #define DBG_LVL DBG_INFO
  9. #include <rtdbg.h>
  10. static char timer_name[32];
  11. static void rt_timer_callback(void *param) {
  12. rtos_msg_t msg;
  13. luat_timer_t *timer = (luat_timer_t*)param;
  14. msg.handler = timer->func;
  15. msg.ptr = param;
  16. luat_msgbus_put(&msg, 1);
  17. }
  18. int luat_timer_start(luat_timer_t* timer) {
  19. rt_sprintf(timer_name, "t%06X", timer->id);
  20. LOG_D("rtt timer name=%s", timer_name);
  21. rt_tick_t tick = rt_tick_from_millisecond(timer->timeout);
  22. rt_uint8_t flag = timer->repeat ? RT_TIMER_FLAG_PERIODIC : RT_TIMER_FLAG_ONE_SHOT;
  23. rt_timer_t r_timer = rt_timer_create(timer_name, rt_timer_callback, timer, tick, flag);
  24. if (r_timer == NULL) {
  25. LOG_E("rt_timer_create FAIL!!!");
  26. return 1;
  27. }
  28. if (rt_timer_start(r_timer) != RT_EOK) {
  29. LOG_E("rt_timer_start FAIL!!!");
  30. rt_timer_delete(r_timer);
  31. return 1;
  32. };
  33. timer->os_timer = r_timer;
  34. LOG_D("rt_timer_start complete");
  35. return 0;
  36. }
  37. int luat_timer_stop(luat_timer_t* timer) {
  38. if (!timer)
  39. return 0;
  40. if (!timer->os_timer)
  41. return 0;
  42. rt_timer_stop((rt_timer_t)timer->os_timer);
  43. rt_timer_delete((rt_timer_t)timer->os_timer);
  44. return 0;
  45. }
  46. luat_timer_t* luat_timer_get(size_t timer_id) {
  47. rt_sprintf(timer_name, "t%06X", timer_id);
  48. rt_object_t obj = rt_object_find(timer_name, RT_Object_Class_Timer);
  49. if (obj != RT_NULL) {
  50. return (luat_timer_t*)((rt_timer_t)obj)->parameter;
  51. }
  52. return RT_NULL;
  53. }
  54. int luat_timer_mdelay(size_t ms) {
  55. if (ms > 0)
  56. rt_thread_mdelay(ms);
  57. return 0;
  58. }