Skip to content

use a scheduled function for settimeofday_cb #6600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cores/esp8266/coredecls.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ extern bool timeshift64_is_set;
void esp_yield();
void esp_schedule();
void tune_timeshift64 (uint64_t now_us);
void settimeofday_cb (void (*cb)(void));
void disable_extra4k_at_link_time (void) __attribute__((noinline));

uint32_t sqrt32 (uint32_t n);
uint32_t crc32 (const void* data, size_t length, uint32_t crc = 0xffffffff);

#ifdef __cplusplus
}

#include <functional>
void settimeofday_cb (std::function<void()>&& cb);
void settimeofday_cb (const std::function<void()>& cb);

#endif

#endif // __COREDECLS_H
14 changes: 10 additions & 4 deletions cores/esp8266/sntp-lwip2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,22 @@
#include <osapi.h>
#include <os_type.h>
#include "coredecls.h"
#include "Schedule.h"

extern "C" {
std::function<void()> _settimeofday_cb;

static void (*_settimeofday_cb)(void) = NULL;
void settimeofday_cb (std::function<void()>&& cb)
{
_settimeofday_cb = std::move(cb);
}

void settimeofday_cb (void (*cb)(void))
void settimeofday_cb (const std::function<void()>& cb)
{
_settimeofday_cb = cb;
}

extern "C" {

#if LWIP_VERSION_MAJOR == 1

#include <pgmspace.h>
Expand Down Expand Up @@ -478,7 +484,7 @@ int settimeofday(const struct timeval* tv, const struct timezone* tz)
sntp_set_system_time(tv->tv_sec);

if (_settimeofday_cb)
_settimeofday_cb();
schedule_function(_settimeofday_cb);
}
return 0;
}
Expand Down
19 changes: 5 additions & 14 deletions libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ void printTm(const char* what, const tm* tm) {
void time_is_set_scheduled() {
// everything is allowed in this function

if (time_machine_days == 0) {
time_machine_running = !time_machine_running;
}

// time machine demo
if (time_machine_running) {
if (time_machine_days == 0)
Expand All @@ -178,19 +182,6 @@ void time_is_set_scheduled() {
}
}

void time_is_set_callback() {
// As in an ISR,
// it is not allowed to call "heavy" core API
// like yield()/delay()/print()/network/...
// If this is needed, use a scheduled function.

// This scheduled function is used for the demo, it is normaly unneeded
schedule_function(time_is_set_scheduled);
if (time_machine_days == 0) {
time_machine_running = !time_machine_running;
}
}

void setup() {
Serial.begin(115200);
Serial.println("\nStarting...\n");
Expand All @@ -204,7 +195,7 @@ void setup() {

// install callback - called when settimeofday is called (by SNTP or us)
// once enabled (by DHCP), SNTP is updated every hour
settimeofday_cb(time_is_set_callback);
settimeofday_cb(time_is_set_scheduled);

// NTP servers may be overriden by your DHCP server for a more local one
// (see below)
Expand Down