Pārlūkot izejas kodu

fix: https://gitee.com/openLuat/LuatOS/issues/I1Z7I2

Wendal Chen 5 gadi atpakaļ
vecāks
revīzija
5577ebb0f7

+ 29 - 0
luat/cmsis_os2/luat_msgbus_cmsis_os2.c

@@ -0,0 +1,29 @@
+
+#include "luat_msgbus.h"
+
+#include "cmsis_os2.h"
+
+#define LUAT_MSGBUS_MAXCOUNT 0xFF
+//#define LUAT_MSGBUS_MAXSIZE 8
+static osMessageQueueId_t queue = {0}; 
+
+void luat_msgbus_init(void) {
+    if (!queue) {
+        queue = osMessageQueueNew(LUAT_MSGBUS_MAXCOUNT, sizeof(rtos_msg_t), NULL);
+    }
+}
+uint32_t luat_msgbus_put(rtos_msg_t* msg, size_t timeout) {
+    if (queue == NULL)
+        return 1;
+    return osMessageQueuePut(queue, msg, 0, timeout);
+}
+uint32_t luat_msgbus_get(rtos_msg_t* msg, size_t timeout) {
+    if (queue == NULL)
+        return 1;
+    return osMessageQueueGet(queue, msg, 0, timeout);
+}
+uint32_t luat_msgbus_freesize(void) {
+    if (queue == NULL)
+        return 1;
+    return osMessageQueueGetSpace(queue);
+}

+ 96 - 0
luat/cmsis_os2/luat_timer_cmsis_os2.c

@@ -0,0 +1,96 @@
+
+#include "luat_base.h"
+#include "luat_malloc.h"
+#include "luat_timer.h"
+#include "luat_msgbus.h"
+#include "cmsis_os2.h"
+#include "FreeRTOS.h"
+#include "task.h"
+
+#define LUAT_LOG_TAG "luat.timer"
+#include "luat_log.h"
+
+#define FREERTOS_TIMER_COUNT 32
+static luat_timer_t* timers[FREERTOS_TIMER_COUNT] = {0};
+
+static void luat_timer_callback(void* param) {
+    //LLOGD("timer callback");
+    rtos_msg_t msg;
+    luat_timer_t *timer = (luat_timer_t*)param;
+    msg.handler = timer->func;
+    msg.ptr = param;
+    msg.arg1 = 0;
+    msg.arg2 = 0;
+    int re = luat_msgbus_put(&msg, 1);
+    //LLOGD("timer msgbus re=%ld", re);
+}
+
+static int nextTimerSlot() {
+    for (size_t i = 0; i < FREERTOS_TIMER_COUNT; i++)
+    {
+        if (timers[i] == NULL) {
+            return i;
+        }
+    }
+    return -1;
+}
+
+int luat_timer_start(luat_timer_t* timer) {
+    osTimerId_t os_timer;
+    int timerIndex;
+    //LLOGD(">>luat_timer_start timeout=%ld", timer->timeout);
+    timerIndex = nextTimerSlot();
+    //LLOGD("timer id=%ld", timerIndex);
+    if (timerIndex < 0) {
+        return 1; // too many timer!!
+    }
+    os_timer = osTimerNew(luat_timer_callback, timer->repeat ? osTimerPeriodic : osTimerOnce, timer, NULL);
+    //LLOGD("timer id=%ld, osTimerNew=%08X", timerIndex, (int)timer);
+    if (!os_timer) {
+        return NULL;
+    }
+    timers[timerIndex] = timer;
+    
+    timer->os_timer = os_timer;
+    int re = osTimerStart(os_timer, timer->timeout);
+    //LLOGD("timer id=%ld timeout=%ld start=%ld", timerIndex, timer->timeout, re);
+    if (re != 0) {
+        osTimerDelete(os_timer);
+        timers[timerIndex] = 0;
+    }
+    return re;
+}
+
+int luat_timer_stop(luat_timer_t* timer) {
+    if (!timer)
+        return 1;
+    for (size_t i = 0; i < FREERTOS_TIMER_COUNT; i++)
+    {
+        if (timers[i] == timer) {
+            timers[i] = NULL;
+            break;
+        }
+    }
+    osTimerStop(timer->os_timer);
+    osTimerDelete(timer->os_timer);
+    return 0;
+};
+
+luat_timer_t* luat_timer_get(size_t timer_id) {
+    for (size_t i = 0; i < FREERTOS_TIMER_COUNT; i++)
+    {
+        if (timers[i] && timers[i]->id == timer_id) {
+            return timers[i];
+        }
+    }
+    return NULL;
+}
+
+
+int luat_timer_mdelay(size_t ms) {
+    if (ms > 0) {
+        vTaskDelay(ms);
+    }
+    return 0;
+}
+

+ 19 - 12
luat/freertos/luat_msgbus_freertos.c

@@ -1,29 +1,36 @@
 
 #include "luat_msgbus.h"
 
-#include "cmsis_os2.h"
+#include "FreeRTOS.h"
+#include "queue.h"
 
-#define LUAT_MSGBUS_MAXCOUNT 0xFF
-//#define LUAT_MSGBUS_MAXSIZE 8
-static osMessageQueueId_t queue = {0}; 
+#define QUEUE_LENGTH 0xFF
+#define ITEM_SIZE sizeof(rtos_msg_t)
+
+static StaticQueue_t xStaticQueue = {0};
+static QueueHandle_t xQueue = {0};
+static uint8_t ucQueueStorageArea[ QUEUE_LENGTH * ITEM_SIZE ];
 
 void luat_msgbus_init(void) {
-    if (!queue) {
-        queue = osMessageQueueNew(LUAT_MSGBUS_MAXCOUNT, sizeof(rtos_msg_t), NULL);
+    if (!xQueue) {
+        xQueue = xQueueCreateStatic( QUEUE_LENGTH,
+                                 ITEM_SIZE,
+                                 ucQueueStorageArea,
+                                 &xStaticQueue );
     }
 }
 uint32_t luat_msgbus_put(rtos_msg_t* msg, size_t timeout) {
-    if (queue == NULL)
+    if (xQueue == NULL)
         return 1;
-    return osMessageQueuePut(queue, msg, 0, timeout);
+    return xQueueSendFromISR(xQueue, msg, NULL);
 }
 uint32_t luat_msgbus_get(rtos_msg_t* msg, size_t timeout) {
-    if (queue == NULL)
+    if (xQueue == NULL)
         return 1;
-    return osMessageQueueGet(queue, msg, 0, timeout);
+    return xQueueReceive(xQueue, msg, timeout); // 要不要除portTICK_RATE_MS呢?
 }
 uint32_t luat_msgbus_freesize(void) {
-    if (queue == NULL)
+    if (xQueue == NULL)
         return 1;
-    return osMessageQueueGetSpace(queue);
+    return 1;
 }

+ 9 - 9
luat/freertos/luat_timer_freertos.c

@@ -3,7 +3,7 @@
 #include "luat_malloc.h"
 #include "luat_timer.h"
 #include "luat_msgbus.h"
-#include "cmsis_os2.h"
+
 #include "FreeRTOS.h"
 #include "task.h"
 
@@ -13,10 +13,10 @@
 #define FREERTOS_TIMER_COUNT 32
 static luat_timer_t* timers[FREERTOS_TIMER_COUNT] = {0};
 
-static void luat_timer_callback(void* param) {
+static void luat_timer_callback(TimerHandle_t xTimer) {
     //LLOGD("timer callback");
     rtos_msg_t msg;
-    luat_timer_t *timer = (luat_timer_t*)param;
+    luat_timer_t *timer = (luat_timer_t*) pvTimerGetTimerID(xTimer);
     msg.handler = timer->func;
     msg.ptr = param;
     msg.arg1 = 0;
@@ -44,7 +44,7 @@ int luat_timer_start(luat_timer_t* timer) {
     if (timerIndex < 0) {
         return 1; // too many timer!!
     }
-    os_timer = osTimerNew(luat_timer_callback, timer->repeat ? osTimerPeriodic : osTimerOnce, timer, NULL);
+    os_timer = xTimerCreate("luat_timer", timer->timeout / portTICK_RATE_MS, timer->repeat, timer, luat_timer_callback);
     //LLOGD("timer id=%ld, osTimerNew=%08X", timerIndex, (int)timer);
     if (!os_timer) {
         return NULL;
@@ -52,10 +52,10 @@ int luat_timer_start(luat_timer_t* timer) {
     timers[timerIndex] = timer;
     
     timer->os_timer = os_timer;
-    int re = osTimerStart(os_timer, timer->timeout);
+    int re = xTimerStart(os_timer, 0);
     //LLOGD("timer id=%ld timeout=%ld start=%ld", timerIndex, timer->timeout, re);
     if (re != 0) {
-        osTimerDelete(timer);
+        xTimerDelete(os_timer, 0);
         timers[timerIndex] = 0;
     }
     return re;
@@ -71,8 +71,8 @@ int luat_timer_stop(luat_timer_t* timer) {
             break;
         }
     }
-    osTimerStop(timer->os_timer);
-    osTimerDelete(timer->os_timer);
+    xTimerStop(timer->os_timer);
+    xTimerDelete(timer->os_timer);
     return 0;
 };
 
@@ -89,7 +89,7 @@ luat_timer_t* luat_timer_get(size_t timer_id) {
 
 int luat_timer_mdelay(size_t ms) {
     if (ms > 0) {
-        vTaskDelay(ms);
+        vTaskDelay(ms / portTICK_RATE_MS);
     }
     return 0;
 }