luat_msgbus_freertos.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "luat_base.h"
  2. #include "luat_msgbus.h"
  3. #include "luat_rtos.h"
  4. #ifdef LUAT_FREERTOS_FULL_INCLUDE
  5. #include "freertos/FreeRTOS.h"
  6. #include "freertos/queue.h"
  7. #else
  8. #include "FreeRTOS.h"
  9. #include "queue.h"
  10. #endif
  11. static QueueHandle_t xQueue = {0};
  12. void luat_msgbus_init(void) {
  13. if (!xQueue) {
  14. xQueue = xQueueCreate(256, sizeof(rtos_msg_t));
  15. }
  16. }
  17. uint32_t luat_msgbus_put(rtos_msg_t* msg, size_t timeout) {
  18. if (xQueue == NULL)
  19. return 1;
  20. #if 0
  21. if (luat_rtos_get_ipsr())
  22. {
  23. BaseType_t pxHigherPriorityTaskWoken;
  24. if (xQueueSendFromISR(xQueue, msg, &pxHigherPriorityTaskWoken) != pdPASS)
  25. return -1;
  26. portYIELD_FROM_ISR(pxHigherPriorityTaskWoken);
  27. return 0;
  28. }
  29. else
  30. {
  31. return xQueueSend(xQueue, msg, NULL) == pdTRUE ? 0 : 1;
  32. }
  33. #else
  34. return xQueueSendFromISR(xQueue, msg, NULL) == pdTRUE ? 0 : 1;
  35. #endif
  36. }
  37. uint32_t luat_msgbus_get(rtos_msg_t* msg, size_t timeout) {
  38. if (xQueue == NULL)
  39. return 1;
  40. return xQueueReceive(xQueue, msg, timeout) == pdTRUE ? 0 : 1;
  41. }
  42. uint32_t luat_msgbus_freesize(void) {
  43. return 1;
  44. }
  45. uint8_t luat_msgbus_is_empty(void) {
  46. return uxQueueMessagesWaiting(xQueue) == 0 ? 1 : 0;
  47. }
  48. uint8_t luat_msgbus_is_ready(void) {
  49. return xQueue?1:0;
  50. }