Просмотр исходного кода

openthread: support microsecond timer

zhangwenxu 4 лет назад
Родитель
Сommit
ea836abe50

+ 2 - 0
components/openthread/CMakeLists.txt

@@ -15,6 +15,7 @@ if(CONFIG_OPENTHREAD_ENABLED)
 
     set(src_dirs
         "openthread/examples/apps/cli"
+        "openthread/examples/platforms/utils"
         "openthread/src/cli"
         "openthread/src/core/api"
         "openthread/src/core/backbone_router"
@@ -35,6 +36,7 @@ if(CONFIG_OPENTHREAD_ENABLED)
 
     set(exclude_srcs
         "openthread/examples/apps/cli/main.cpp"
+        "openthread/examples/platforms/utils/logging_rtt.c"
         "openthread/src/core/common/extension_example.cpp")
 
     if(CONFIG_OPENTHREAD_FTD)

+ 8 - 0
components/openthread/include/openthread-core-esp32x-config.h

@@ -247,6 +247,14 @@
  */
 #define OPENTHREAD_CONFIG_PLATFORM_RADIO_SPINEL_RX_FRAME_BUFFER_SIZE 1024
 
+/**
+ * @def OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
+ *
+ * Define as 1 to enable microsecond timer.
+ *
+ */
+#define OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE 1
+
 /**
  * @def OPENTHREAD_CONFIG_DTLS_MAX_CONTENT_LEN
  *

+ 60 - 20
components/openthread/port/esp_openthread_alarm.c

@@ -25,13 +25,17 @@
 #include "common/logging.hpp"
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
+#include "openthread/platform/alarm-micro.h"
 #include "openthread/platform/alarm-milli.h"
 #include "openthread/platform/diag.h"
 #include "openthread/platform/time.h"
 
-static uint64_t s_alarm_t0   = 0;
-static uint64_t s_alarm_dt   = 0;
-static bool     s_is_running = false;
+static uint64_t s_alarm_ms_t0   = 0;
+static uint64_t s_alarm_ms_dt   = 0;
+static bool     s_is_ms_running = false;
+static uint64_t s_alarm_us_t0   = 0;
+static uint64_t s_alarm_us_dt   = 0;
+static bool     s_is_us_running = false;
 
 uint64_t otPlatTimeGet(void)
 {
@@ -47,18 +51,18 @@ void otPlatAlarmMilliStartAt(otInstance *aInstance, uint32_t aT0, uint32_t aDt)
 {
     OT_UNUSED_VARIABLE(aInstance);
 
-    s_alarm_t0   = aT0;
-    s_alarm_dt   = aDt;
-    s_is_running = true;
+    s_alarm_ms_t0   = aT0;
+    s_alarm_ms_dt   = aDt;
+    s_is_ms_running = true;
 
-    otLogDebgPlat("alarm start running, t0=%llu, dt=%llu", s_alarm_t0, s_alarm_dt);
+    otLogDebgPlat("Millisecond timer alarm start running, t0=%llu, dt=%llu", s_alarm_ms_t0, s_alarm_ms_dt);
 }
 
 void otPlatAlarmMilliStop(otInstance *aInstance)
 {
     OT_UNUSED_VARIABLE(aInstance);
 
-    s_is_running = false;
+    s_is_ms_running = false;
 }
 
 uint32_t otPlatAlarmMilliGetNow(void)
@@ -66,18 +70,49 @@ uint32_t otPlatAlarmMilliGetNow(void)
     return esp_timer_get_time() / US_PER_MS;
 }
 
+void otPlatAlarmMicroStartAt(otInstance *aInstance, uint32_t aT0, uint32_t aDt)
+{
+    OT_UNUSED_VARIABLE(aInstance);
+
+    s_alarm_us_t0 = aT0;
+    s_alarm_us_dt = aDt;
+    s_is_us_running = true;
+
+    otLogDebgPlat("Microsecond timer alarm start running, t0=%llu, dt=%llu", s_alarm_us_t0, s_alarm_us_dt);
+}
+
+void otPlatAlarmMicroStop(otInstance *aInstance)
+{
+    OT_UNUSED_VARIABLE(aInstance);
+    s_is_us_running = false;
+}
+
+uint32_t otPlatAlarmMicroGetNow(void)
+{
+    return esp_timer_get_time();
+}
+
 void esp_openthread_alarm_update(esp_openthread_mainloop_context_t *mainloop)
 {
     struct timeval *timeout = &mainloop->timeout;
-    uint32_t        now     = otPlatAlarmMilliGetNow();
-
-    if (!s_is_running) {
-        timeout->tv_sec  = INT32_MAX;
-        timeout->tv_usec = 0;
-    } else if (s_alarm_t0 + s_alarm_dt > now) {
-        uint64_t remaining = s_alarm_dt + s_alarm_t0 - now;
-        timeout->tv_sec    = remaining / MS_PER_S;
-        timeout->tv_usec   = (remaining % MS_PER_S) * US_PER_MS;
+    uint32_t        now     = otPlatAlarmMicroGetNow();
+    int64_t         remain_min_time_us  = INT64_MAX;
+    int64_t         remaining_us = 0;
+    if (s_is_ms_running) {
+        remaining_us = (s_alarm_ms_dt + s_alarm_ms_t0) * US_PER_MS - now;
+        if (remain_min_time_us > remaining_us) {
+            remain_min_time_us = remaining_us;
+        }
+    }
+    if (s_is_us_running) {
+        remaining_us = s_alarm_us_dt + s_alarm_us_t0 - now;
+        if (remain_min_time_us > remaining_us) {
+            remain_min_time_us = remaining_us;
+        }
+    }
+    if (remain_min_time_us > 0) {
+        timeout->tv_sec    = remain_min_time_us / US_PER_S;
+        timeout->tv_usec   = remain_min_time_us % US_PER_S;
     } else {
         timeout->tv_sec  = 0;
         timeout->tv_usec = 0;
@@ -86,8 +121,8 @@ void esp_openthread_alarm_update(esp_openthread_mainloop_context_t *mainloop)
 
 void esp_openthread_alarm_process(otInstance *aInstance)
 {
-    if (s_is_running && s_alarm_t0 + s_alarm_dt <= otPlatAlarmMilliGetNow()) {
-        s_is_running = false;
+    if (s_is_ms_running && s_alarm_ms_t0 + s_alarm_ms_dt <= otPlatAlarmMilliGetNow()) {
+        s_is_ms_running = false;
 
 #if OPENTHREAD_CONFIG_DIAG_ENABLE
         if (otPlatDiagModeGet()) {
@@ -98,6 +133,11 @@ void esp_openthread_alarm_process(otInstance *aInstance)
             otPlatAlarmMilliFired(aInstance);
         }
 
-        otLogDebgPlat("alarm fired");
+        otLogDebgPlat("Millisecond timer alarm fired");
+    }
+    if (s_is_us_running && s_alarm_us_t0 + s_alarm_us_dt <= otPlatAlarmMicroGetNow()) {
+        s_is_us_running = false;
+        otPlatAlarmMicroFired(aInstance);
+        otLogDebgPlat("Microsecond timer alarm fired");
     }
 }