time_sync.c 827 B

12345678910111213141516171819202122232425262728293031323334
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Helmut Pozimski <helmut@pozimski.eu>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0-only
  5. */
  6. #include <apps/esp_sntp.h>
  7. #include <esp_log.h>
  8. #include "ds3231.h"
  9. #define TAG "sntp"
  10. static void time_sync_callback(struct timeval *tv) {
  11. struct tm date_time;
  12. localtime_r(&tv->tv_sec, &date_time);
  13. ds3231_write_date_time(date_time);
  14. }
  15. void sntp_start(uint32_t sync_interval) {
  16. sntp_setoperatingmode(SNTP_OPMODE_POLL);
  17. sntp_setservername(0, "pool.ntp.org");
  18. sntp_set_time_sync_notification_cb(time_sync_callback);
  19. sntp_init();
  20. sntp_set_sync_interval(sync_interval);
  21. }
  22. void await_sntp_sync() {
  23. while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET) {
  24. ESP_LOGI(TAG, "Waiting for system time to be set...");
  25. vTaskDelay(2000 / portTICK_PERIOD_MS);
  26. }
  27. }