Browse Source

update(cherrymp): use own osal

sakumisu 1 năm trước cách đây
mục cha
commit
88d57eb99b

+ 5 - 0
cherryusb.cmake

@@ -298,4 +298,9 @@ endif()
 if(CONFIG_CHERRYMP)
 list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/third_party/cherrymp/chry_mempool.c)
 list(APPEND cherryusb_incs ${CMAKE_CURRENT_LIST_DIR}/third_party/cherrymp)
+    if("${CONFIG_CHERRYUSB_OSAL}" STREQUAL "freertos")
+    list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/third_party/cherrymp/chry_mempool_osal_freertos.c)
+    elseif("${CONFIG_CHERRYUSB_OSAL}" STREQUAL "rtthread")
+    list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/third_party/cherrymp/chry_mempool_osal_rtthread.c)
+    endif()
 endif()

+ 31 - 14
third_party/cherrymp/chry_mempool.c

@@ -11,37 +11,41 @@ int chry_mempool_create(struct chry_mempool *pool, void *block, uint32_t block_s
     uint8_t *ringbuf1;
     uint8_t *ringbuf2;
 
-    ringbuf1 = usb_osal_malloc(sizeof(uintptr_t) * block_count);
+    ringbuf1 = chry_mempool_osal_malloc(sizeof(uintptr_t) * block_count);
     if (ringbuf1 == NULL) {
         return -1;
     }
     memset(ringbuf1, 0, sizeof(uintptr_t) * block_count);
 
     if (chry_ringbuffer_init(&pool->in, ringbuf1, sizeof(uintptr_t) * block_count) == -1) {
-        usb_osal_free(ringbuf1);
+        chry_mempool_osal_free(ringbuf1);
         return -1;
     }
 
-    ringbuf2 = usb_osal_malloc(sizeof(uintptr_t) * block_count);
+    ringbuf2 = chry_mempool_osal_malloc(sizeof(uintptr_t) * block_count);
     if (ringbuf2 == NULL) {
-        usb_osal_free(ringbuf1);
+        chry_mempool_osal_free(ringbuf1);
         return -1;
     }
     memset(ringbuf2, 0, sizeof(uintptr_t) * block_count);
 
     if (chry_ringbuffer_init(&pool->out, ringbuf2, sizeof(uintptr_t) * block_count) == -1) {
-        usb_osal_free(ringbuf1);
-        usb_osal_free(ringbuf2);
+        chry_mempool_osal_free(ringbuf1);
+        chry_mempool_osal_free(ringbuf2);
         return -1;
     }
 
-    pool->out_sem = usb_osal_sem_create(block_count);
+    pool->out_sem = chry_mempool_osal_sem_create(block_count);
     if (pool->out_sem == NULL) {
-        usb_osal_free(ringbuf1);
-        usb_osal_free(ringbuf2);
+        chry_mempool_osal_free(ringbuf1);
+        chry_mempool_osal_free(ringbuf2);
         return -1;
     }
 
+    pool->block = block;
+    pool->block_size = block_size;
+    pool->block_count = block_count;
+
     for (uint32_t i = 0; i < block_count; i++) {
         addr = ((uintptr_t)block + i * block_size);
         chry_ringbuffer_write(&pool->in, &addr, sizeof(uintptr_t));
@@ -52,11 +56,11 @@ int chry_mempool_create(struct chry_mempool *pool, void *block, uint32_t block_s
 
 void chry_mempool_delete(struct chry_mempool *pool)
 {
-    usb_osal_sem_delete(pool->out_sem);
+    chry_mempool_osal_sem_delete(pool->out_sem);
     chry_ringbuffer_reset(&pool->in);
     chry_ringbuffer_reset(&pool->out);
-    usb_osal_free(pool->in.pool);
-    usb_osal_free(pool->out.pool);
+    chry_mempool_osal_free(pool->in.pool);
+    chry_mempool_osal_free(pool->out.pool);
 }
 
 uintptr_t *chry_mempool_alloc(struct chry_mempool *pool)
@@ -86,7 +90,7 @@ int chry_mempool_send(struct chry_mempool *pool, uintptr_t *item)
 
     addr = (uintptr_t)item;
     chry_ringbuffer_write(&pool->out, &addr, sizeof(uintptr_t));
-    return usb_osal_sem_give(pool->out_sem);
+    return chry_mempool_osal_sem_give(pool->out_sem);
 }
 
 int chry_mempool_recv(struct chry_mempool *pool, uintptr_t **item, uint32_t timeout)
@@ -94,7 +98,7 @@ int chry_mempool_recv(struct chry_mempool *pool, uintptr_t **item, uint32_t time
     uint32_t len;
     int ret;
 
-    ret = usb_osal_sem_take(pool->out_sem, timeout);
+    ret = chry_mempool_osal_sem_take(pool->out_sem, timeout);
     if (ret < 0) {
         return -1;
     }
@@ -105,4 +109,17 @@ int chry_mempool_recv(struct chry_mempool *pool, uintptr_t **item, uint32_t time
     } else {
         return 0;
     }
+}
+
+void chry_mempool_reset(struct chry_mempool *pool)
+{
+    uintptr_t addr;
+
+    chry_ringbuffer_reset(&pool->in);
+    chry_ringbuffer_reset(&pool->out);
+
+    for (uint32_t i = 0; i < pool->block_count; i++) {
+        addr = ((uintptr_t)pool->block + i * pool->block_size);
+        chry_ringbuffer_write(&pool->in, &addr, sizeof(uintptr_t));
+    }
 }

+ 19 - 2
third_party/cherrymp/chry_mempool.h

@@ -6,24 +6,41 @@
 #ifndef CHRY_MEMPOOL_H
 #define CHRY_MEMPOOL_H
 
-#include "usb_osal.h"
+#include <stdint.h>
+#include <string.h>
+#include <stdbool.h>
+
 #include "chry_ringbuffer.h"
 
+typedef void *chry_mempool_osal_sem_t;
+
 struct chry_mempool {
     chry_ringbuffer_t in;
     chry_ringbuffer_t out;
-    usb_osal_sem_t out_sem;
+    chry_mempool_osal_sem_t out_sem;
+
+    void *block;
+    uint32_t block_size;
+    uint32_t block_count;
 };
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+chry_mempool_osal_sem_t chry_mempool_osal_sem_create(uint32_t max_count);
+void chry_mempool_osal_sem_delete(chry_mempool_osal_sem_t sem);
+int chry_mempool_osal_sem_take(chry_mempool_osal_sem_t sem, uint32_t timeout);
+int chry_mempool_osal_sem_give(chry_mempool_osal_sem_t sem);
+void *chry_mempool_osal_malloc(size_t size);
+void chry_mempool_osal_free(void *ptr);
+
 int chry_mempool_create(struct chry_mempool *pool, void *block, uint32_t block_size, uint32_t block_count);
 uintptr_t *chry_mempool_alloc(struct chry_mempool *pool);
 int chry_mempool_free(struct chry_mempool *pool, uintptr_t *item);
 int chry_mempool_send(struct chry_mempool *pool, uintptr_t *item);
 int chry_mempool_recv(struct chry_mempool *pool, uintptr_t **item, uint32_t timeout);
+void chry_mempool_reset(struct chry_mempool *pool);
 
 #ifdef __cplusplus
 }

+ 54 - 0
third_party/cherrymp/chry_mempool_osal_freertos.c

@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2024, sakumisu
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+#include "chry_mempool.h"
+#include <FreeRTOS.h>
+#include "semphr.h"
+
+chry_mempool_osal_sem_t chry_mempool_osal_sem_create(uint32_t max_count)
+{
+    return (chry_mempool_osal_sem_t)xSemaphoreCreateCounting(max_count, 0);
+}
+
+void chry_mempool_osal_sem_delete(chry_mempool_osal_sem_t sem)
+{
+    vSemaphoreDelete((SemaphoreHandle_t)sem);
+}
+
+int chry_mempool_osal_sem_take(chry_mempool_osal_sem_t sem, uint32_t timeout)
+{
+    if (timeout == 0xffffffff) {
+        return (xSemaphoreTake((SemaphoreHandle_t)sem, portMAX_DELAY) == pdPASS) ? 0 : -1;
+    } else {
+        return (xSemaphoreTake((SemaphoreHandle_t)sem, pdMS_TO_TICKS(timeout)) == pdPASS) ? 0 : -1;
+    }
+}
+
+int chry_mempool_osal_sem_give(chry_mempool_osal_sem_t sem)
+{
+    BaseType_t xHigherPriorityTaskWoken = pdFALSE;
+    int ret;
+
+    if (xPortIsInsideInterrupt()) {
+        ret = xSemaphoreGiveFromISR((SemaphoreHandle_t)sem, &xHigherPriorityTaskWoken);
+        if (ret == pdPASS) {
+            portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
+        }
+    } else {
+        ret = xSemaphoreGive((SemaphoreHandle_t)sem);
+    }
+
+    return (ret == pdPASS) ? 0 : -1;
+}
+
+void *chry_mempool_osal_malloc(size_t size)
+{
+    return pvPortMalloc(size);
+}
+
+void chry_mempool_osal_free(void *ptr)
+{
+    vPortFree(ptr);
+}

+ 36 - 0
third_party/cherrymp/chry_mempool_osal_nonos.c

@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2024, sakumisu
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+#include "chry_mempool.h"
+#include "stdlib.h"
+
+chry_mempool_osal_sem_t chry_mempool_osal_sem_create(uint32_t max_count)
+{
+    return (chry_mempool_osal_sem_t)1;
+}
+
+void chry_mempool_osal_sem_delete(chry_mempool_osal_sem_t sem)
+{
+}
+
+int chry_mempool_osal_sem_take(chry_mempool_osal_sem_t sem, uint32_t timeout)
+{
+    return 0;
+}
+
+int chry_mempool_osal_sem_give(chry_mempool_osal_sem_t sem)
+{
+    return 0;
+}
+
+void *chry_mempool_osal_malloc(size_t size)
+{
+    return malloc(size);
+}
+
+void chry_mempool_osal_free(void *ptr)
+{
+    free(ptr);
+}

+ 54 - 0
third_party/cherrymp/chry_mempool_osal_rtthread.c

@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2024, sakumisu
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+#include "chry_mempool.h"
+#include <rtthread.h>
+#include <rthw.h>
+
+chry_mempool_osal_sem_t chry_mempool_osal_sem_create(uint32_t max_count)
+{
+    return (chry_mempool_osal_sem_t)rt_sem_create("chry_mempoolh_sem", max_count, RT_IPC_FLAG_FIFO);
+}
+
+void chry_mempool_osal_sem_delete(chry_mempool_osal_sem_t sem)
+{
+    rt_sem_delete((rt_sem_t)sem);
+}
+
+int chry_mempool_osal_sem_take(chry_mempool_osal_sem_t sem, uint32_t timeout)
+{
+    int ret = 0;
+    rt_err_t result = RT_EOK;
+
+    if (timeout == 0xfffffff) {
+        result = rt_sem_take((rt_sem_t)sem, RT_WAITING_FOREVER);
+    } else {
+        result = rt_sem_take((rt_sem_t)sem, rt_tick_from_millisecond(timeout));
+    }
+    if (result == -RT_ETIMEOUT) {
+        ret = -1;
+    } else if (result == -RT_ERROR) {
+        ret = -1;
+    } else {
+        ret = 0;
+    }
+
+    return (int)ret;
+}
+
+int chry_mempool_osal_sem_give(chry_mempool_osal_sem_t sem)
+{
+    return (int)rt_sem_release((rt_sem_t)sem);
+}
+
+void *chry_mempool_osal_malloc(size_t size)
+{
+    return rt_malloc(size);
+}
+
+void chry_mempool_osal_free(void *ptr)
+{
+    rt_free(ptr);
+}