btstack_run_loop_rtthread.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (C) 2017 BlueKitchen GmbH
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. * 4. Any redistribution, use, or modification is done solely for
  17. * personal benefit and not for any commercial purpose or for
  18. * monetary gain.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
  24. * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * Please inquire about commercial licensing options at
  34. * contact@bluekitchen-gmbh.com
  35. *
  36. */
  37. #define BTSTACK_FILE__ "btstack_run_loop_rtthread.c"
  38. #include "btstack_run_loop_rtthread.h"
  39. #include "btstack_linked_list.h"
  40. #include "btstack_debug.h"
  41. #include "btstack_util.h"
  42. #include <stddef.h> // NULL
  43. #include <rtthread.h>
  44. #define RUN_LOOP_QUEUE_LENGTH 5
  45. #define RUN_LOOP_QUEUE_ITEM_SIZE sizeof(function_call_t)
  46. // bit 0 event group reserved to wakeup run loop
  47. #define EVENT_GROUP_FLAG_RUN_LOOP (1 << 0)
  48. typedef struct function_call {
  49. void (*fn)(void * arg);
  50. void * arg;
  51. } function_call_t;
  52. static rt_mq_t btstack_run_loop_queue;
  53. static rt_event_t btstack_run_loop_event_group;
  54. static rt_thread_t btstack_run_loop_task;
  55. static uint32_t btstack_run_loop_rtthread_get_time_ms(void) {
  56. rt_tick_t ticks = rt_tick_get();
  57. uint32_t ms = ticks * 1000 / RT_TICK_PER_SECOND;
  58. return ms;
  59. }
  60. // set timer
  61. static void btstack_run_loop_rtthread_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){
  62. ts->timeout = btstack_run_loop_rtthread_get_time_ms() + timeout_in_ms + 1;
  63. }
  64. static void btstack_run_loop_rtthread_init(void) {
  65. btstack_run_loop_base_init();
  66. btstack_run_loop_queue = rt_mq_create("btstack.run.loop.queue", RUN_LOOP_QUEUE_ITEM_SIZE, RUN_LOOP_QUEUE_LENGTH, RT_IPC_FLAG_PRIO);
  67. btstack_assert(btstack_run_loop_queue != NULL);
  68. btstack_run_loop_event_group = rt_event_create("btstack.run.loop.event", RT_IPC_FLAG_PRIO);
  69. btstack_assert(btstack_run_loop_event_group != NULL);
  70. btstack_run_loop_task = rt_thread_self();
  71. log_info("run loop init, task %p, queue item size %u", btstack_run_loop_task, (int) sizeof(function_call_t));
  72. }
  73. /**
  74. * Execute run_loop
  75. */
  76. static void btstack_run_loop_rtthread_execute(void) {
  77. log_debug("RL: execute");
  78. while (1) {
  79. // process data sources
  80. btstack_run_loop_base_poll_data_sources();
  81. // process registered function calls on run loop thread
  82. while (1) {
  83. function_call_t message = { NULL, NULL };
  84. rt_err_t err = rt_mq_recv(btstack_run_loop_queue, &message, sizeof(message), RT_WAITING_NO);
  85. if (err != RT_EOK) break;
  86. if (message.fn) {
  87. message.fn(message.arg);
  88. }
  89. }
  90. // process timers
  91. uint32_t now = btstack_run_loop_rtthread_get_time_ms();
  92. btstack_run_loop_base_process_timers(now);
  93. // wait for timeout or event group/task notification
  94. int32_t timeout_next_timer_ms = btstack_run_loop_base_get_time_until_timeout(now);
  95. int timeout_ms = RT_WAITING_FOREVER;
  96. if (timeout_next_timer_ms >= 0) {
  97. timeout_ms = timeout_next_timer_ms;
  98. }
  99. log_debug("RL: wait with timeout %u", (int) timeout_ms);
  100. rt_event_recv(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP, RT_EVENT_FLAG_CLEAR | RT_EVENT_FLAG_OR, timeout_ms, NULL);
  101. }
  102. }
  103. // schedules execution from regular thread
  104. void btstack_run_loop_rtthread_trigger(void) {
  105. rt_event_send(btstack_run_loop_event_group, EVENT_GROUP_FLAG_RUN_LOOP);
  106. }
  107. void btstack_run_loop_rtthread_execute_code_on_main_thread(void (*fn)(void *arg), void * arg) {
  108. // directly call function if already on btstack task
  109. if (rt_thread_self() == btstack_run_loop_task) {
  110. (*fn)(arg);
  111. return ;
  112. }
  113. function_call_t message;
  114. message.fn = fn;
  115. message.arg = arg;
  116. rt_err_t err = rt_mq_send(btstack_run_loop_queue, &message, sizeof(message));
  117. if (err != RT_EOK) {
  118. log_error("Failed to post fn %p", fn);
  119. }
  120. btstack_run_loop_rtthread_trigger();
  121. }
  122. static const btstack_run_loop_t btstack_run_loop_rtthread = {
  123. &btstack_run_loop_rtthread_init,
  124. &btstack_run_loop_base_add_data_source,
  125. &btstack_run_loop_base_remove_data_source,
  126. &btstack_run_loop_base_enable_data_source_callbacks,
  127. &btstack_run_loop_base_disable_data_source_callbacks,
  128. &btstack_run_loop_rtthread_set_timer,
  129. &btstack_run_loop_base_add_timer,
  130. &btstack_run_loop_base_remove_timer,
  131. &btstack_run_loop_rtthread_execute,
  132. &btstack_run_loop_base_dump_timer,
  133. &btstack_run_loop_rtthread_get_time_ms,
  134. };
  135. const btstack_run_loop_t * btstack_run_loop_rtthread_get_instance(void){
  136. return &btstack_run_loop_rtthread;
  137. }