task.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. This file encapsulates the SDK-based task handling for the NodeMCU Lua firmware.
  3. */
  4. #include "task/task.h"
  5. #include "mem.h"
  6. #include "user_config.h"
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "freertos/queue.h"
  10. #include "freertos/semphr.h"
  11. #define TASK_HANDLE_MONIKER 0x68680000
  12. #define TASK_HANDLE_MASK 0xFFF80000
  13. #define TASK_HANDLE_UNMASK (~TASK_HANDLE_MASK)
  14. #define TASK_HANDLE_ALLOCATION_BRICK 4 // must be a power of 2
  15. #define TASK_DEFAULT_QUEUE_LEN 8
  16. #define CHECK(p,v,msg) if (!(p)) { NODE_DBG ( msg ); return (v); }
  17. typedef struct
  18. {
  19. task_handle_t sig;
  20. task_param_t par;
  21. } task_event_t;
  22. /*
  23. * Private arrays to hold the 3 event task queues and the dispatch callbacks
  24. */
  25. static xQueueHandle task_Q[TASK_PRIORITY_COUNT];
  26. /* Rather than using a QueueSet (which requires queues to be empty when created)
  27. * we use a binary semaphore to unblock the pump whenever something is posted */
  28. static xSemaphoreHandle pending;
  29. static task_callback_t *task_func;
  30. static int task_count;
  31. /*
  32. * Initialise the task handle callback for a given priority. This doesn't need
  33. * to be called explicitly as the get_id function will call this lazily.
  34. */
  35. bool task_init_handler(task_prio_t priority, uint8 qlen) {
  36. if (priority >= TASK_PRIORITY_COUNT)
  37. return false;
  38. if (task_Q[priority] == NULL)
  39. {
  40. task_Q[priority] = xQueueCreate (qlen, sizeof (task_event_t));
  41. return task_Q[priority] != NULL;
  42. }
  43. else
  44. return false;
  45. }
  46. task_handle_t task_get_id(task_callback_t t) {
  47. /* Initialise any uninitialised Qs with the default Q len */
  48. for (task_prio_t p = TASK_PRIORITY_LOW; p != TASK_PRIORITY_COUNT; ++p)
  49. {
  50. if (!task_Q[p]) {
  51. CHECK(task_init_handler( p, TASK_DEFAULT_QUEUE_LEN ), 0, "Task initialisation failed");
  52. }
  53. }
  54. if ( (task_count & (TASK_HANDLE_ALLOCATION_BRICK - 1)) == 0 ) {
  55. /* With a brick size of 4 this branch is taken at 0, 4, 8 ... and the new size is +4 */
  56. task_func =(task_callback_t *)realloc(
  57. task_func,
  58. sizeof(task_callback_t)*(task_count+TASK_HANDLE_ALLOCATION_BRICK));
  59. CHECK(task_func, 0 , "Malloc failure in task_get_id");
  60. memset (task_func+task_count, 0, sizeof(task_callback_t)*TASK_HANDLE_ALLOCATION_BRICK);
  61. }
  62. task_func[task_count] = t;
  63. return TASK_HANDLE_MONIKER | task_count++;
  64. }
  65. bool task_post (task_prio_t priority, task_handle_t handle, task_param_t param)
  66. {
  67. if (priority >= TASK_PRIORITY_COUNT ||
  68. !task_Q[priority] ||
  69. (handle & TASK_HANDLE_MASK) != TASK_HANDLE_MONIKER)
  70. return false;
  71. task_event_t ev = { handle, param };
  72. bool res = pdPASS == xQueueSendToBackFromISR (task_Q[priority], &ev, NULL);
  73. if (pending) /* only need to raise semaphore if it's been initialised */
  74. xSemaphoreGiveFromISR (pending, NULL);
  75. return res;
  76. }
  77. static bool next_event (task_event_t *ev, task_prio_t *prio)
  78. {
  79. for (task_prio_t pr = TASK_PRIORITY_COUNT; pr != TASK_PRIORITY_LOW; --pr)
  80. {
  81. task_prio_t p = pr -1;
  82. if (task_Q[p] && xQueueReceive (task_Q[p], ev, 0) == pdTRUE)
  83. {
  84. *prio = p;
  85. return true;
  86. }
  87. }
  88. return false; // no events queued
  89. }
  90. static void dispatch (task_event_t *e, uint8_t prio) {
  91. task_handle_t handle = e->sig;
  92. if ( (handle & TASK_HANDLE_MASK) == TASK_HANDLE_MONIKER) {
  93. uint16_t entry = (handle & TASK_HANDLE_UNMASK);
  94. if ( task_func && entry < task_count ){
  95. /* call the registered task handler with the specified parameter and priority */
  96. task_func[entry](e->par, prio);
  97. return;
  98. }
  99. }
  100. /* Invalid signals are ignored */
  101. NODE_DBG ( "Invalid signal issued: %08x", handle);
  102. }
  103. void task_pump_messages (void)
  104. {
  105. vSemaphoreCreateBinary (pending);
  106. for (;;)
  107. {
  108. task_event_t ev;
  109. task_prio_t prio;
  110. if (next_event (&ev, &prio))
  111. dispatch (&ev, prio);
  112. else
  113. xSemaphoreTake (pending, portMAX_DELAY);
  114. }
  115. }