Skip to content

Commit

Permalink
Merge branch 'feature/openthread_support_microsecond_timer' into 'mas…
Browse files Browse the repository at this point in the history
…ter'

openthread: support microsecond timer

See merge request espressif/esp-idf!13353
  • Loading branch information
chshu committed Apr 27, 2021
2 parents 86b19d6 + ea836ab commit 85565b8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 20 deletions.
2 changes: 2 additions & 0 deletions components/openthread/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions components/openthread/include/openthread-core-esp32x-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
80 changes: 60 additions & 20 deletions components/openthread/port/esp_openthread_alarm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -47,37 +51,68 @@ 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)
{
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;
Expand All @@ -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()) {
Expand All @@ -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");
}
}

0 comments on commit 85565b8

Please sign in to comment.