Эх сурвалжийг харах

update(osal): add mq api

Signed-off-by: sakumisu <1203593632@qq.com>
sakumisu 1 сар өмнө
parent
commit
16db90474d

+ 6 - 0
include/ec_osal.h

@@ -26,6 +26,7 @@
 typedef void *ec_osal_thread_t;
 typedef void *ec_osal_sem_t;
 typedef void *ec_osal_mutex_t;
+typedef void *ec_osal_mq_t;
 typedef void (*ec_thread_entry_t)(CONFIG_EC_OSAL_THREAD_SET_ARGV);
 typedef void (*ec_timer_handler_t)(void *argument);
 struct ec_osal_timer {
@@ -55,6 +56,11 @@ void ec_osal_mutex_delete(ec_osal_mutex_t mutex);
 int ec_osal_mutex_take(ec_osal_mutex_t mutex);
 int ec_osal_mutex_give(ec_osal_mutex_t mutex);
 
+ec_osal_mq_t ec_osal_mq_create(uint32_t max_msgs);
+void ec_osal_mq_delete(ec_osal_mq_t mq);
+int ec_osal_mq_send(ec_osal_mq_t mq, uintptr_t addr);
+int ec_osal_mq_recv(ec_osal_mq_t mq, uintptr_t *addr, uint32_t timeout);
+
 struct ec_osal_timer *ec_osal_timer_create(const char *name, uint32_t timeout_ms, ec_timer_handler_t handler, void *argument, bool is_period);
 void ec_osal_timer_delete(struct ec_osal_timer *timer);
 void ec_osal_timer_start(struct ec_osal_timer *timer);

+ 47 - 0
osal/ec_osal_freertos.c

@@ -109,6 +109,53 @@ int ec_osal_mutex_give(ec_osal_mutex_t mutex)
     return (xSemaphoreGive((SemaphoreHandle_t)mutex) == pdPASS) ? 0 : -EC_ERR_TIMEOUT;
 }
 
+ec_osal_mq_t ec_osal_mq_create(uint32_t max_msgs)
+{
+    return (ec_osal_mq_t)xQueueCreate(max_msgs, sizeof(uintptr_t));
+}
+
+void ec_osal_mq_delete(ec_osal_mq_t mq)
+{
+    vQueueDelete((QueueHandle_t)mq);
+}
+
+int ec_osal_mq_send(ec_osal_mq_t mq, uintptr_t addr)
+{
+    BaseType_t xHigherPriorityTaskWoken = pdFALSE;
+    int ret;
+
+    if (xPortIsInsideInterrupt()) {
+        ret = xQueueSendFromISR((ec_osal_mq_t)mq, &addr, &xHigherPriorityTaskWoken);
+        if (ret == pdPASS) {
+            portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
+        }
+    } else {
+        ret = xQueueSend((ec_osal_mq_t)mq, &addr, 0);
+    }
+
+    return (ret == pdPASS) ? 0 : -EC_ERR_TIMEOUT;
+}
+
+int ec_osal_mq_recv(ec_osal_mq_t mq, uintptr_t *addr, uint32_t timeout)
+{
+    BaseType_t xHigherPriorityTaskWoken = pdFALSE;
+    int ret;
+
+    if (xPortIsInsideInterrupt()) {
+        ret = xQueueReceiveFromISR((ec_osal_mq_t)mq, addr, &xHigherPriorityTaskWoken);
+        if (ret == pdPASS) {
+            portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
+        }
+        return (ret == pdPASS) ? 0 : -EC_ERR_TIMEOUT;
+    } else {
+        if (timeout == EC_OSAL_WAITING_FOREVER) {
+            return (xQueueReceive((ec_osal_mq_t)mq, addr, portMAX_DELAY) == pdPASS) ? 0 : -EC_ERR_TIMEOUT;
+        } else {
+            return (xQueueReceive((ec_osal_mq_t)mq, addr, pdMS_TO_TICKS(timeout)) == pdPASS) ? 0 : -EC_ERR_TIMEOUT;
+        }
+    }
+}
+
 static void __ec_timeout(TimerHandle_t *handle)
 {
     struct ec_osal_timer *timer = (struct ec_osal_timer *)pvTimerGetTimerID((TimerHandle_t)handle);

+ 37 - 0
osal/ec_osal_rtthread.c

@@ -114,6 +114,43 @@ int ec_osal_mutex_give(ec_osal_mutex_t mutex)
     return (int)rt_mutex_release((rt_mutex_t)mutex);
 }
 
+
+ec_osal_mq_t ec_osal_mq_create(uint32_t max_msgs)
+{
+    return (ec_osal_mq_t)rt_mq_create("ec_mq", sizeof(uintptr_t), max_msgs, RT_IPC_FLAG_FIFO);
+}
+
+void ec_osal_mq_delete(ec_osal_mq_t mq)
+{
+    rt_mq_delete((rt_mq_t)mq);
+}
+
+int ec_osal_mq_send(ec_osal_mq_t mq, uintptr_t addr)
+{
+    return rt_mq_send((rt_mq_t)mq, &addr, sizeof(uintptr_t));
+}
+
+int ec_osal_mq_recv(ec_osal_mq_t mq, uintptr_t *addr, uint32_t timeout)
+{
+    int ret = 0;
+    rt_err_t result = RT_EOK;
+
+    if (timeout == EC_OSAL_WAITING_FOREVER) {
+        result = rt_mq_recv((rt_mq_t)mq, addr, sizeof(uintptr_t), RT_WAITING_FOREVER);
+    } else {
+        result = rt_mq_recv((rt_mq_t)mq, addr, sizeof(uintptr_t), rt_tick_from_millisecond(timeout));
+    }
+    if (result == -RT_ETIMEOUT) {
+        ret = -EC_ERR_TIMEOUT;
+    } else if (result == -RT_ERROR) {
+        ret = -EC_ERR_INVAL;
+    } else {
+        ret = 0;
+    }
+
+    return (int)ret;
+}
+
 struct ec_osal_timer *ec_osal_timer_create(const char *name, uint32_t timeout_ms, ec_timer_handler_t handler, void *argument, bool is_period)
 {
     struct ec_osal_timer *timer;

+ 42 - 0
osal/ec_osal_threadx.c

@@ -144,6 +144,48 @@ int ec_osal_mutex_give(ec_osal_mutex_t mutex)
     return (tx_mutex_put((TX_MUTEX *)mutex) == TX_SUCCESS) ? 0 : -EC_ERR_INVAL;
 }
 
+ec_osal_mq_t ec_osal_mq_create(uint32_t max_msgs)
+{
+    TX_QUEUE *queue_ptr = TX_NULL;
+
+    tx_byte_allocate(&ec_byte_pool, (VOID **)&queue_ptr, EC_ALIGN_UP(sizeof(TX_QUEUE), 4) + sizeof(uintptr_t) * max_msgs, TX_NO_WAIT);
+    if (queue_ptr == TX_NULL) {
+        EC_LOG_ERR("Create TX_QUEUE failed\r\n");
+        while (1) {
+        }
+    }
+
+    tx_queue_create(queue_ptr, "ec_mq", sizeof(uintptr_t) / 4, (CHAR *)queue_ptr + EC_ALIGN_UP(sizeof(TX_QUEUE), 4), sizeof(uintptr_t) * max_msgs);
+    return (ec_osal_mq_t)queue_ptr;
+}
+
+void ec_osal_mq_delete(ec_osal_mq_t mq)
+{
+    tx_queue_delete((TX_QUEUE *)mq);
+    tx_byte_release(mq);
+}
+
+int ec_osal_mq_send(ec_osal_mq_t mq, uintptr_t addr)
+{
+    return (tx_queue_send((TX_QUEUE *)mq, &addr, TX_NO_WAIT) == TX_SUCCESS) ? 0 : -EC_ERR_INVAL;
+}
+
+int ec_osal_mq_recv(ec_osal_mq_t mq, uintptr_t *addr, uint32_t timeout)
+{
+    int ret = 0;
+
+    ret = tx_queue_receive((TX_QUEUE *)mq, addr, timeout);
+    if (ret == TX_SUCCESS) {
+        ret = 0;
+    } else if (ret == TX_QUEUE_EMPTY) {
+        ret = -EC_ERR_TIMEOUT;
+    } else {
+        ret = -EC_ERR_INVAL;
+    }
+
+    return (int)ret;
+}
+
 struct ec_osal_timer *ec_osal_timer_create(const char *name, uint32_t timeout_ms, ec_timer_handler_t handler, void *argument, bool is_period)
 {
     TX_TIMER *timer_ptr = TX_NULL;