luat_rtos_lua.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "luat_base.h"
  2. #include "luat_msgbus.h"
  3. #include "luat_rtos.h"
  4. #include "luat_mem.h"
  5. #include "luat_timer.h"
  6. #define LUAT_LOG_TAG "rtos"
  7. #include "luat_log.h"
  8. #define RTOS_LUA_DEBUG 0
  9. #if RTOS_LUA_DEBUG == 0
  10. #undef LLOGD
  11. #define LLOGD(...)
  12. #endif
  13. /*timer*/
  14. #define LUA_TIMER_COUNT 32
  15. static luat_timer_t* timers[LUA_TIMER_COUNT] = {0};;
  16. static void luat_timer_callback(LUAT_RT_CB_PARAM) {
  17. rtos_msg_t msg;
  18. luat_timer_t *timer = luat_timer_get((int)param);
  19. LLOGD("timer callback param:%d timer:%p",param,timer);
  20. if (timer == NULL)
  21. return;
  22. msg.handler = timer->func;
  23. msg.ptr = timer;
  24. msg.arg1 = (int)param;
  25. msg.arg2 = 0;
  26. luat_msgbus_put(&msg, 0);
  27. }
  28. static int nextTimerSlot() {
  29. for (size_t i = 0; i < LUA_TIMER_COUNT; i++){
  30. if (timers[i] == NULL) {
  31. return i;
  32. }
  33. }
  34. return -1;
  35. }
  36. LUAT_WEAK int luat_timer_start(luat_timer_t* timer) {
  37. int timerIndex;
  38. LLOGD(">>luat_timer_start timeout=%ld", timer->timeout);
  39. timerIndex = nextTimerSlot();
  40. if (timerIndex < 0) {
  41. LLOGE("too many timers");
  42. return 1; // too many timer!!
  43. }
  44. luat_rtos_timer_create((luat_rtos_timer_t *)&timer->os_timer);
  45. LLOGD("timer id=%ld, timer=%p, os_timer=%p", timerIndex, timer,timer->os_timer);
  46. if (!timer->os_timer) {
  47. LLOGE("xTimerCreate FAIL");
  48. return -1;
  49. }
  50. timer->id=timerIndex;
  51. timers[timerIndex] = timer;
  52. int re = luat_rtos_timer_start(timer->os_timer, timer->timeout, timer->repeat, luat_timer_callback, (void*)timerIndex);
  53. LLOGD("timer id=%ld timeout=%ld start=%ld", timerIndex, timer->timeout, re);
  54. if (re) {
  55. LLOGE("xTimerStart FAIL");
  56. luat_rtos_timer_delete(timer->os_timer);
  57. timers[timerIndex] = NULL;
  58. }
  59. return 0;
  60. }
  61. LUAT_WEAK int luat_timer_stop(luat_timer_t* timer) {
  62. if (timer == NULL || timer->os_timer == NULL)
  63. return 1;
  64. for (int i = 0; i < LUA_TIMER_COUNT; i++)
  65. {
  66. if (timers[i] == timer) {
  67. timers[i] = NULL;
  68. break;
  69. }
  70. }
  71. luat_rtos_timer_stop(timer->os_timer);
  72. luat_rtos_timer_delete(timer->os_timer);
  73. timer->os_timer = NULL;
  74. return 0;
  75. };
  76. LUAT_WEAK luat_timer_t* luat_timer_get(size_t timer_id) {
  77. for (int i = 0; i < LUA_TIMER_COUNT; i++){
  78. // LLOGD("luat_timer_get timers[%d]:%p,timers[%d]->id:%d", i,timers[i],i,timers[i]->id);
  79. if (timers[i] && timers[i]->id == timer_id) {
  80. return timers[i];
  81. }
  82. }
  83. return NULL;
  84. }
  85. LUAT_WEAK int luat_timer_mdelay(size_t ms) {
  86. if (ms > 0) {
  87. luat_rtos_task_sleep(ms);
  88. }
  89. return 0;
  90. }
  91. /*msgbus*/
  92. #ifndef LUA_QUEUE_COUNT
  93. #define LUA_QUEUE_COUNT 256
  94. #endif
  95. static luat_rtos_queue_t lua_queue_handle;
  96. LUAT_WEAK void luat_msgbus_init(void) {
  97. luat_rtos_queue_create(&lua_queue_handle, LUA_QUEUE_COUNT, sizeof(rtos_msg_t));
  98. }
  99. LUAT_WEAK uint32_t luat_msgbus_put(rtos_msg_t* msg, size_t timeout) {
  100. return luat_rtos_queue_send(lua_queue_handle, msg, 0, timeout);
  101. }
  102. LUAT_WEAK uint32_t luat_msgbus_get(rtos_msg_t* msg, size_t timeout) {
  103. return luat_rtos_queue_recv(lua_queue_handle, msg, 0, timeout);
  104. }
  105. LUAT_WEAK uint32_t luat_msgbus_freesize(void) {
  106. uint32_t item_cnt;
  107. luat_rtos_queue_get_cnt(lua_queue_handle, &item_cnt);
  108. return LUA_QUEUE_COUNT - item_cnt;
  109. }
  110. LUAT_WEAK uint8_t luat_msgbus_is_empty(void) {
  111. uint32_t item_cnt;
  112. luat_rtos_queue_get_cnt(lua_queue_handle, &item_cnt);
  113. return !item_cnt;
  114. }