diff options
author | Thomas White <taw@physics.org> | 2023-03-02 22:57:42 +0100 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2023-03-02 22:57:42 +0100 |
commit | fceab499c21eb01a7fafbeb1e6850dd021cbbcf2 (patch) | |
tree | 51a4997be5a1916679c8e7569c4b4eca716263c3 | |
parent | fcd95895036b22220f940f7bb38b049bd26534c0 (diff) |
Use pico_cyw43_arch_lwip_poll
So little can be done in lwIP callbacks with the background driver, we
end up polling anyway. So we might as well poll the driver directly,
and be able to do more in the callbacks.
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | morningtown.c | 8 | ||||
-rw-r--r-- | ntp_client.c | 60 |
3 files changed, 27 insertions, 43 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index bb2f06d..6f7c803 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,7 @@ add_executable(morningtown morningtown.c ntp_client.c) target_link_libraries(morningtown pico_stdlib hardware_rtc - pico_cyw43_arch_lwip_threadsafe_background) + pico_cyw43_arch_lwip_poll) pico_add_extra_outputs(morningtown) diff --git a/morningtown.c b/morningtown.c index ed44aa3..42b7746 100644 --- a/morningtown.c +++ b/morningtown.c @@ -152,7 +152,7 @@ int main() rtc_init(); ntp_state = ntp_init(ntp_callback); - last_conn = 10; + last_conn = 200; while (1) { watchdog_update(); @@ -176,19 +176,15 @@ int main() } if ( ntp_ok ) { - debug_print("connected OK. Checking clock..\n"); check_clock(); } - ntp_poll(ntp_state); - if ( ntp_ok && !gpio_get(TEST_BUTTON) ) { gpio_put(LED_GREEN, 1); } - debug_print("tick\n"); - last_conn += 1; + cyw43_arch_poll(); sleep_ms(100); } diff --git a/ntp_client.c b/ntp_client.c index 1c25a83..38275c9 100644 --- a/ntp_client.c +++ b/ntp_client.c @@ -45,9 +45,6 @@ typedef struct NTP_T_ { struct udp_pcb *ntp_pcb; alarm_id_t ntp_resend_alarm; void (*callback)(int status); - time_t utc; - int have_new_reply; - int last_status; } NTP_T; @@ -58,14 +55,32 @@ typedef struct NTP_T_ { #define NTP_RESEND_TIME (10 * 1000) +static void set_rtc(time_t iutc) +{ + time_t tv = iutc + UTC_OFFSET_SEC; + struct tm *utc = gmtime(&tv); + datetime_t t; + + t.year = utc->tm_year + 1900; + t.month = utc->tm_mon + 1; + t.day = utc->tm_mday; + t.dotw = utc->tm_wday; + t.hour = utc->tm_hour; + t.min = utc->tm_min; + t.sec = utc->tm_sec; + + debug_print("time is %i/%i/%i %i %i:%i:%i\n", + t.year, t.month, t.day, t.dotw, t.hour, t.min, t.sec); + + rtc_set_datetime(&t); +} + + static void ntp_result(NTP_T *state, int status, time_t *result) { debug_print("NTP result:\n"); - state->have_new_reply = 1; - state->last_status = status; if (status == NTP_REPLY_RECEIVED && result) { - state->utc = *result; debug_print("yay!\n"); if ( state->ntp_resend_alarm > 0 ) { @@ -73,10 +88,13 @@ static void ntp_result(NTP_T *state, int status, time_t *result) state->ntp_resend_alarm = 0; } + set_rtc(*result); + } else { debug_print("other reply.\n"); } + state->callback(status); state->dns_request_sent = false; } @@ -158,7 +176,6 @@ NTP_T *ntp_init(void (*reply_func)(int)) if (!state) return NULL; state->callback = reply_func; - state->have_new_reply = 0; state->ntp_pcb = udp_new_ip_type(IPADDR_TYPE_ANY); if (!state->ntp_pcb) { @@ -196,32 +213,3 @@ void ntp_send_request(NTP_T *state) ntp_result(state, NTP_DNS_ERROR, NULL); } } - - -void ntp_poll(NTP_T *state) -{ - if ( !state->have_new_reply ) return; - state->have_new_reply = 0; - - if ( state->last_status == NTP_REPLY_RECEIVED ) { - - time_t tv = state->utc + UTC_OFFSET_SEC; - struct tm *utc = gmtime(&tv); - datetime_t t; - - t.year = utc->tm_year + 1900; - t.month = utc->tm_mon + 1; - t.day = utc->tm_mday; - t.dotw = utc->tm_wday; - t.hour = utc->tm_hour; - t.min = utc->tm_min; - t.sec = utc->tm_sec; - - debug_print("time is %i/%i/%i %i %i:%i:%i\n", - t.year, t.month, t.day, t.dotw, t.hour, t.min, t.sec); - - rtc_set_datetime(&t); - - } - state->callback(state->last_status); -} |