luat_msgbus_freertos.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include "luat_msgbus.h"
  2. #include "FreeRTOS.h"
  3. #include "queue.h"
  4. #define QUEUE_LENGTH 0xFF
  5. #define ITEM_SIZE sizeof(rtos_msg_t)
  6. static StaticQueue_t xStaticQueue = {0};
  7. static QueueHandle_t xQueue = {0};
  8. #if configSUPPORT_STATIC_ALLOCATION
  9. static uint8_t ucQueueStorageArea[ QUEUE_LENGTH * ITEM_SIZE ];
  10. #endif
  11. void luat_msgbus_init(void) {
  12. if (!xQueue) {
  13. #if configSUPPORT_STATIC_ALLOCATION
  14. xQueue = xQueueCreateStatic( QUEUE_LENGTH,
  15. ITEM_SIZE,
  16. ucQueueStorageArea,
  17. &xStaticQueue );
  18. #else
  19. xQueue = xQueueCreate(QUEUE_LENGTH, ITEM_SIZE);
  20. #endif
  21. }
  22. }
  23. uint32_t luat_msgbus_put(rtos_msg_t* msg, size_t timeout) {
  24. if (xQueue == NULL)
  25. return 1;
  26. return xQueueSendFromISR(xQueue, msg, NULL) == pdTRUE ? 0 : 1;
  27. }
  28. uint32_t luat_msgbus_get(rtos_msg_t* msg, size_t timeout) {
  29. if (xQueue == NULL)
  30. return 1;
  31. return xQueueReceive(xQueue, msg, timeout) == pdTRUE ? 0 : 1; // 要不要除portTICK_RATE_MS呢?
  32. }
  33. uint32_t luat_msgbus_freesize(void) {
  34. if (xQueue == NULL)
  35. return 1;
  36. return 1;
  37. }