luat_timer_air105.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "luat_base.h"
  22. #include "luat_malloc.h"
  23. #include "luat_timer.h"
  24. #include "luat_msgbus.h"
  25. #include "app_interface.h"
  26. #include "FreeRTOS.h"
  27. #include "task.h"
  28. #include "timers.h"
  29. #define LUAT_LOG_TAG "luat.timer"
  30. #include "luat_log.h"
  31. #define FREERTOS_TIMER_COUNT 128
  32. static luat_timer_t* timers[FREERTOS_TIMER_COUNT] = {0};
  33. /*
  34. static void luat_timer_callback(TimerHandle_t xTimer) {
  35. //LLOGD("timer callback");
  36. rtos_msg_t msg;
  37. luat_timer_t *timer = (luat_timer_t*) pvTimerGetTimerID(xTimer);
  38. msg.handler = timer->func;
  39. msg.ptr = timer;
  40. msg.arg1 = 0;
  41. msg.arg2 = 0;
  42. int re = luat_msgbus_put(&msg, 0);
  43. //LLOGD("timer msgbus re=%ld", re);
  44. }
  45. */
  46. static int32_t luat_timer_callback(void *pData, void *pParam)
  47. {
  48. rtos_msg_t msg;
  49. size_t timer_id = (size_t)pParam;
  50. luat_timer_t *timer = luat_timer_get(timer_id);
  51. if (timer == NULL)
  52. return 0;
  53. msg.handler = timer->func;
  54. msg.ptr = timer;
  55. msg.arg1 = timer_id;
  56. msg.arg2 = 0;
  57. luat_msgbus_put(&msg, 0);
  58. return 0;
  59. }
  60. static int nextTimerSlot() {
  61. for (size_t i = 0; i < FREERTOS_TIMER_COUNT; i++)
  62. {
  63. if (timers[i] == NULL) {
  64. return i;
  65. }
  66. }
  67. return -1;
  68. }
  69. int luat_timer_start(luat_timer_t* timer) {
  70. Timer_t *os_timer;
  71. int timerIndex;
  72. //LLOGD(">>luat_timer_start timeout=%ld", timer->timeout);
  73. timerIndex = nextTimerSlot();
  74. //LLOGD("timer id=%ld", timerIndex);
  75. if (timerIndex < 0) {
  76. return 1; // too many timer!!
  77. }
  78. os_timer = Timer_Create(luat_timer_callback, (void*)timer->id, NULL);
  79. //LLOGD("timer id=%ld, osTimerNew=%p", timerIndex, os_timer);
  80. if (!os_timer) {
  81. return -1;
  82. }
  83. timers[timerIndex] = timer;
  84. timer->os_timer = os_timer;
  85. if (timer->type)
  86. {
  87. return Timer_StartUS(os_timer, timer->timeout, timer->repeat);
  88. }
  89. else
  90. {
  91. return Timer_StartMS(os_timer, timer->timeout, timer->repeat);
  92. }
  93. }
  94. int luat_timer_stop(luat_timer_t* timer) {
  95. if (!timer)
  96. return 1;
  97. for (size_t i = 0; i < FREERTOS_TIMER_COUNT; i++)
  98. {
  99. if (timers[i] == timer) {
  100. timers[i] = NULL;
  101. break;
  102. }
  103. }
  104. Timer_Release(timer->os_timer);
  105. return 0;
  106. };
  107. luat_timer_t* luat_timer_get(size_t timer_id) {
  108. for (size_t i = 0; i < FREERTOS_TIMER_COUNT; i++)
  109. {
  110. if (timers[i] && timers[i]->id == timer_id) {
  111. return timers[i];
  112. }
  113. }
  114. return NULL;
  115. }
  116. int luat_timer_mdelay(size_t ms) {
  117. Task_DelayMS(ms);
  118. return 0;
  119. }
  120. void luat_timer_us_delay(size_t time){
  121. Task_DelayUS(time);
  122. }