From ea836abe50e9c0ac05ac73c9d46bc0733b075593 Mon Sep 17 00:00:00 2001 From: zhangwenxu Date: Mon, 26 Apr 2021 16:25:15 +0800 Subject: [PATCH] openthread: support microsecond timer --- components/openthread/CMakeLists.txt | 2 + .../include/openthread-core-esp32x-config.h | 8 ++ .../openthread/port/esp_openthread_alarm.c | 80 ++++++++++++++----- 3 files changed, 70 insertions(+), 20 deletions(-) diff --git a/components/openthread/CMakeLists.txt b/components/openthread/CMakeLists.txt index 9c74e06323..bc805b3d02 100644 --- a/components/openthread/CMakeLists.txt +++ b/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) diff --git a/components/openthread/include/openthread-core-esp32x-config.h b/components/openthread/include/openthread-core-esp32x-config.h index 5e0f97d824..ff17008bb2 100644 --- a/components/openthread/include/openthread-core-esp32x-config.h +++ b/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 * diff --git a/components/openthread/port/esp_openthread_alarm.c b/components/openthread/port/esp_openthread_alarm.c index 46fd1619da..132d89f166 100644 --- a/components/openthread/port/esp_openthread_alarm.c +++ b/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"); } }