diff --git a/app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_3.overlay b/app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_3.overlay index 4266ef71..daccebdc 100644 --- a/app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_3.overlay +++ b/app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_3.overlay @@ -11,7 +11,6 @@ low-power-enable; }; }; - }; &uicr { diff --git a/app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_4.overlay b/app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_4.overlay index 3402482f..453aaae1 100644 --- a/app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_4.overlay +++ b/app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_4.overlay @@ -54,7 +54,6 @@ bias-pull-up; }; }; - }; &uicr { diff --git a/app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_5.conf b/app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_5.conf index 82b0c797..50d00823 100644 --- a/app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_5.conf +++ b/app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_5.conf @@ -42,8 +42,8 @@ CONFIG_NVS=y CONFIG_DEBUG_COREDUMP_BACKEND_OTHER=y -#CONFIG_RTC=y -#CONFIG_RTC_UPDATE=y +CONFIG_RTC=y +CONFIG_RTC_UPDATE=y CONFIG_MISC_ENABLE_SYSTEM_RESET=n # Implemented in nPM, hence not needed in FW diff --git a/app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_5.overlay b/app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_5.overlay index d81dd5c4..39f7fec8 100644 --- a/app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_5.overlay +++ b/app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_5.overlay @@ -84,7 +84,6 @@ bias-pull-up; }; }; - }; &uicr { @@ -198,7 +197,7 @@ reg = <0x51>; status = "okay"; clkout = <0>; - int-gpios = <&gpio1 13 (GPIO_PULL_DOWN | GPIO_ACTIVE_HIGH)>; + int-gpios = <&gpio1 13 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>; }; bmi270: bmi270@68 { diff --git a/app/patches/rv8263_rtc.patch b/app/patches/rv8263_rtc.patch index 35fdada1..31969cba 100644 --- a/app/patches/rv8263_rtc.patch +++ b/app/patches/rv8263_rtc.patch @@ -404,10 +404,10 @@ index 0000000000..bb82ed7d9b + return err; + } + -+ /* Return an error when the oscillator is stopped. */ -+ if (regs[0] & RV8263_BM_OS) { -+ return -ECANCELED; -+ } ++ /* Clear the oscillator stop flag and return an error when the oscillator is stopped. */ ++ if (regs[0] & RV8263_BM_OS) { ++ return -ENODATA; ++ } + + timeptr->tm_sec = bcd2bin(regs[0] & SECONDS_BITS); + timeptr->tm_min = bcd2bin(regs[1] & MINUTES_BITS); diff --git a/app/src/events/zsw_periodic_event.c b/app/src/events/zsw_periodic_event.c index d585fa80..3c143389 100644 --- a/app/src/events/zsw_periodic_event.c +++ b/app/src/events/zsw_periodic_event.c @@ -1,8 +1,9 @@ #include #include #include +#include -#if CONFIG_RTC +#if CONFIG_RTC_UPDATE #include #endif @@ -13,7 +14,7 @@ #define PERIODIC_MID_INTERVAL_MS 1000 #define PERIODIC_SLOW_INTERVAL_MS 10000 -#if CONFIG_RTC +#if CONFIG_RTC_UPDATE static const struct device *const rtc = DEVICE_DT_GET(DT_ALIAS(rtc)); #endif @@ -21,53 +22,14 @@ ZBUS_CHAN_DECLARE(periodic_event_1s_chan); ZBUS_CHAN_DECLARE(periodic_event_10s_chan); ZBUS_CHAN_DECLARE(periodic_event_100ms_chan); -int zsw_periodic_chan_add_obs(const struct zbus_channel *chan, const struct zbus_observer *obs) -{ - struct k_work_delayable *work = NULL; - int ret; - - ret = zbus_chan_add_obs(chan, obs, K_MSEC(100)); - - if (ret != 0) { - return ret; - } - - zbus_chan_claim(chan, K_FOREVER); - work = (struct k_work_delayable *)zbus_chan_user_data(chan); - __ASSERT(work != NULL, "Invalid channel"); - if (!k_work_delayable_is_pending(work)) { - if (chan == &periodic_event_10s_chan) { - ret = k_work_reschedule(work, K_MSEC(PERIODIC_SLOW_INTERVAL_MS)); - } else if (chan == &periodic_event_1s_chan) { - ret = k_work_reschedule(work, K_MSEC(PERIODIC_MID_INTERVAL_MS)); - } else if (chan == &periodic_event_100ms_chan) { - ret = k_work_reschedule(work, K_MSEC(PERIODIC_FAST_INTERVAL_MS)); - } else { - __ASSERT(false, "Unknown channel"); - } - } - zbus_chan_finish(chan); - - return ret; -} - -int zsw_periodic_chan_rm_obs(const struct zbus_channel *chan, const struct zbus_observer *obs) -{ - struct k_work_delayable *work = NULL; - int ret = zbus_chan_rm_obs(chan, obs, K_MSEC(100)); - if (ret == 0 && sys_slist_is_empty(&chan->data->observers)) { - work = (struct k_work_delayable *)zbus_chan_user_data(chan); - __ASSERT(k_work_delayable_is_pending(work), "Periodic slow work is not pending"); - ret = k_work_cancel_delayable(work); - } - return ret; -} +LOG_MODULE_REGISTER(zsw_periodic_event, LOG_LEVEL_INF); static void handle_slow_timeout(struct k_work *item) { struct periodic_event evt = { }; struct k_work_delayable *work = NULL; + zbus_chan_claim(&periodic_event_10s_chan, K_FOREVER); work = (struct k_work_delayable *)zbus_chan_user_data(&periodic_event_10s_chan); k_work_reschedule(work, K_MSEC(PERIODIC_SLOW_INTERVAL_MS)); @@ -76,20 +38,21 @@ static void handle_slow_timeout(struct k_work *item) zbus_chan_pub(&periodic_event_10s_chan, &evt, K_MSEC(250)); } -//#if CONFIG_RTC -//void handle_mid_timeout(const struct device *dev, void *user_data) -//{ -// struct periodic_event evt = { -// }; -// -// zbus_chan_pub(&periodic_event_1s_chan, &evt, K_MSEC(250)); -//} -//#else +#if CONFIG_RTC_UPDATE +static void handle_mid_timeout(const struct device *dev, void *user_data) +{ + struct periodic_event evt = { + }; + + zbus_chan_pub(&periodic_event_1s_chan, &evt, K_MSEC(250)); +} +#else static void handle_mid_timeout(struct k_work *item) { struct periodic_event evt = { }; struct k_work_delayable *work = NULL; + zbus_chan_claim(&periodic_event_1s_chan, K_FOREVER); work = (struct k_work_delayable *)zbus_chan_user_data(&periodic_event_1s_chan); k_work_reschedule(work, K_MSEC(PERIODIC_MID_INTERVAL_MS)); @@ -97,13 +60,14 @@ static void handle_mid_timeout(struct k_work *item) zbus_chan_pub(&periodic_event_1s_chan, &evt, K_MSEC(250)); } -//#endif +#endif static void handle_fast_timeout(struct k_work *item) { struct periodic_event evt = { }; struct k_work_delayable *work = NULL; + zbus_chan_claim(&periodic_event_100ms_chan, K_FOREVER); work = (struct k_work_delayable *)zbus_chan_user_data(&periodic_event_100ms_chan); k_work_reschedule(work, K_MSEC(PERIODIC_FAST_INTERVAL_MS)); @@ -112,6 +76,61 @@ static void handle_fast_timeout(struct k_work *item) zbus_chan_pub(&periodic_event_100ms_chan, &evt, K_MSEC(250)); } +int zsw_periodic_chan_add_obs(const struct zbus_channel *chan, const struct zbus_observer *obs) +{ + struct k_work_delayable *work = NULL; + int ret; + + ret = zbus_chan_add_obs(chan, obs, K_MSEC(100)); + + if (ret != 0) { + return ret; + } + + zbus_chan_claim(chan, K_FOREVER); + work = (struct k_work_delayable *)zbus_chan_user_data(chan); + __ASSERT(work != NULL, "Invalid channel"); + if (!k_work_delayable_is_pending(work)) { + if (chan == &periodic_event_10s_chan) { + ret = k_work_reschedule(work, K_MSEC(PERIODIC_SLOW_INTERVAL_MS)); + } else if (chan == &periodic_event_1s_chan) { +#ifdef CONFIG_RTC_UPDATE + rtc_update_set_callback(rtc, handle_mid_timeout, NULL); +#else + ret = k_work_reschedule(work, K_MSEC(PERIODIC_MID_INTERVAL_MS)); +#endif + } else if (chan == &periodic_event_100ms_chan) { + ret = k_work_reschedule(work, K_MSEC(PERIODIC_FAST_INTERVAL_MS)); + } else { + __ASSERT(false, "Unknown channel"); + } + } + zbus_chan_finish(chan); + + return ret; +} + +int zsw_periodic_chan_rm_obs(const struct zbus_channel *chan, const struct zbus_observer *obs) +{ + struct k_work_delayable *work = NULL; + int ret; + + ret = zbus_chan_rm_obs(chan, obs, K_MSEC(100)); + if (ret == 0 && sys_slist_is_empty(&chan->data->observers)) { +#if CONFIG_RTC_UPDATE + if (chan == &periodic_event_1s_chan) { + ret = rtc_update_set_callback(rtc, NULL, NULL); + } else +#endif + { + work = (struct k_work_delayable *)zbus_chan_user_data(chan); + __ASSERT(k_work_delayable_is_pending(work), "Periodic slow work is not pending"); + ret = k_work_cancel_delayable(work); + } + } + return ret; +} + static int zsw_timer_init(void) { struct k_work_delayable *work = NULL; @@ -119,16 +138,14 @@ static int zsw_timer_init(void) work = (struct k_work_delayable *)zbus_chan_user_data(&periodic_event_10s_chan); k_work_init_delayable(work, handle_slow_timeout); -//#if CONFIG_RTC -// if (!device_is_ready(rtc)) { -// return -EBUSY; -// } -// -// rtc_update_set_callback(rtc, handle_mid_timeout, NULL); -//#else +#if CONFIG_RTC_UPDATE + if (!device_is_ready(rtc)) { + return -EBUSY; + } +#else work = (struct k_work_delayable *)zbus_chan_user_data(&periodic_event_1s_chan); k_work_init_delayable(work, handle_mid_timeout); -//#endif +#endif work = (struct k_work_delayable *)zbus_chan_user_data(&periodic_event_100ms_chan); k_work_init_delayable(work, handle_fast_timeout); diff --git a/app/src/filesystem/zsw_filesystem.c b/app/src/filesystem/zsw_filesystem.c index 526ba554..c4639144 100644 --- a/app/src/filesystem/zsw_filesystem.c +++ b/app/src/filesystem/zsw_filesystem.c @@ -58,10 +58,10 @@ static int lsdir(const char *path) } if (entry.type == FS_DIR_ENTRY_DIR) { - LOG_PRINTK("[DIR ] %s\n", entry.name); + LOG_DBG("[DIR ] %s\n", entry.name); } else { - LOG_PRINTK("[FILE] %s (size = %zu)\n", - entry.name, entry.size); + LOG_DBG("[FILE] %s (size = %zu)\n", + entry.name, entry.size); } } @@ -78,7 +78,7 @@ int zsw_filesystem_ls(void) rc = fs_statvfs(mountpoint->mnt_point, &sbuf); if (rc < 0) { - LOG_PRINTK("FAIL: statvfs: %d\n", rc); + LOG_ERR("FAIL: statvfs: %d\n", rc); return -1; } @@ -90,7 +90,7 @@ int zsw_filesystem_ls(void) rc = lsdir(mountpoint->mnt_point); if (rc < 0) { - LOG_PRINTK("FAIL: lsdir %s: %d\n", mountpoint->mnt_point, rc); + LOG_ERR("FAIL: lsdir %s: %d\n", mountpoint->mnt_point, rc); return -1; } diff --git a/app/src/main.c b/app/src/main.c index 1e79e5b4..0db8eb27 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -16,8 +16,6 @@ */ #include -#include -#include #include #include #include @@ -25,43 +23,47 @@ #include #include #include -#include -#include -#include #include -#include #include #include #include #include #include #include + +#include +#include +#include +#include + #include "dfu.h" -#include "ui/zsw_ui.h" #include "ble/ble_comm.h" #include "ble/ble_aoa.h" +#include "ble/ble_ams.h" +#include "ble/ble_ancs.h" +#include "ble/ble_cts.h" #include "events/accel_event.h" #include "events/ble_event.h" -#include "sensors/zsw_imu.h" #include "drivers/zsw_buzzer.h" +#include "drivers/zsw_vibration_motor.h" +#include "drivers/zsw_display_control.h" +#include "sensors/zsw_imu.h" #include "sensors/zsw_magnetometer.h" #include "sensors/zsw_pressure_sensor.h" #include "sensors/zsw_light_sensor.h" #include "sensors/zsw_environment_sensor.h" -#include -#include "drivers/zsw_vibration_motor.h" -#include "drivers/zsw_display_control.h" +#include "zsw_retained_ram_storage.h" #include "managers/zsw_power_manager.h" #include "managers/zsw_app_manager.h" #include "managers/zsw_notification_manager.h" #include "applications/watchface/watchface_app.h" -#include -#include +#include "filesystem/zsw_rtt_flash_loader.h" +#include "filesystem/zsw_filesystem.h" #include "ui/popup/zsw_popup_window.h" -#include "ble/ble_ams.h" -#include "ble/ble_ancs.h" -#include "ble/ble_cts.h" -#include +#include "ui/zsw_ui.h" +#include "zsw_coredump.h" +#include "zsw_cpu_freq.h" +#include "zsw_clock.h" #include "fuel_gauge/zsw_pmic.h" LOG_MODULE_REGISTER(main, CONFIG_ZSW_APP_LOG_LEVEL); @@ -83,7 +85,6 @@ struct input_event last_input_event; static void run_input_work(struct k_work *item); static void run_init_work(struct k_work *item); - static void run_wdt_work(struct k_work *item); static void enable_bluetooth(void); static void print_retention_ram(void); @@ -287,22 +288,6 @@ static void run_wdt_work(struct k_work *item) int main(void) { - /* - while (1) { - struct i2c_msg msgs[1]; - uint8_t dst; - - for (uint8_t i = 0; i < 127; i++) { - msgs[0].buf = &dst; - msgs[0].len = 0U; - msgs[0].flags = I2C_MSG_WRITE | I2C_MSG_STOP; - if (i2c_transfer(i2c, &msgs[0], 1, i) == 0) { - LOG_INF("Found device at 0x%X", i); - } - } - - k_msleep(1000); - }*/ #ifdef CONFIG_SPI_FLASH_LOADER if (bootmode_check(ZSW_BOOT_MODE_RTT_FLASH_LOADER)) { LOG_WRN("SPI Flash Loader Boot Mode"); @@ -610,26 +595,23 @@ static void on_zbus_ble_data_callback(const struct zbus_channel *chan) switch (event->data.type) { case BLE_COMM_DATA_TYPE_SET_TIME: { if (event->data.data.time.seconds > 0) { - // TODO: Replace with set time - struct timespec tspec; - tspec.tv_sec = event->data.data.time.seconds; - tspec.tv_nsec = 0; - - clock_settime(CLOCK_REALTIME, &tspec); + zsw_timeval_t ztm; + memcpy(&ztm.tm, localtime((time_t *)&event->data.data.time.seconds), sizeof(ztm.tm)); + zsw_clock_set_time(&ztm); } if (event->data.data.time.tz_offset != 0) { char tz[sizeof("UTC+01")] = { '\0' }; char sign = (event->data.data.time.tz_offset < 0) ? '+' : '-'; snprintf(tz, sizeof(tz), "UTC%c%d", sign, MIN(abs(event->data.data.time.tz_offset), 99)); - setenv("TZ", tz, 1); - tzset(); + zsw_clock_set_timezone(tz); } break; } - case BLE_COMM_DATA_TYPE_WEATHER: + case BLE_COMM_DATA_TYPE_WEATHER: { break; - case BLE_COMM_DATA_TYPE_REMOTE_CONTROL: + } + case BLE_COMM_DATA_TYPE_REMOTE_CONTROL: { // TODO: Add correct enum if (event->data.data.remote_control.button == 4) { zsw_power_manager_reset_idle_timout(); @@ -646,8 +628,10 @@ static void on_zbus_ble_data_callback(const struct zbus_channel *chan) } break; - default: + } + default: { break; + } } } diff --git a/app/src/sensors/zsw_imu.c b/app/src/sensors/zsw_imu.c index fbb33281..5ce580f9 100644 --- a/app/src/sensors/zsw_imu.c +++ b/app/src/sensors/zsw_imu.c @@ -28,15 +28,15 @@ LOG_MODULE_REGISTER(zsw_imu, CONFIG_ZSW_SENSORS_LOG_LEVEL); -static void zbus_periodic_slow_callback(const struct zbus_channel *chan); +static void zbus_periodic_mid_callback(const struct zbus_channel *chan); ZBUS_CHAN_DECLARE(accel_data_chan); ZBUS_CHAN_DECLARE(periodic_event_1s_chan); -ZBUS_LISTENER_DEFINE(zsw_imu_lis, zbus_periodic_slow_callback); +ZBUS_LISTENER_DEFINE(zsw_imu_lis, zbus_periodic_mid_callback); static const struct device *const bmi270 = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(bmi270)); static struct sensor_trigger bmi270_trigger; -static void zbus_periodic_slow_callback(const struct zbus_channel *chan) +static void zbus_periodic_mid_callback(const struct zbus_channel *chan) { zsw_timeval_t time; struct accel_event evt = { diff --git a/app/src/sensors/zsw_magnetometer.c b/app/src/sensors/zsw_magnetometer.c index bc2d8ada..e732ce4b 100644 --- a/app/src/sensors/zsw_magnetometer.c +++ b/app/src/sensors/zsw_magnetometer.c @@ -42,14 +42,14 @@ static double min_z; static bool is_calibrating; static magn_calib_data_t calibration_data; -static void zbus_periodic_slow_callback(const struct zbus_channel *chan); +static void zbus_periodic_mid_callback(const struct zbus_channel *chan); ZBUS_CHAN_DECLARE(magnetometer_data_chan); ZBUS_CHAN_DECLARE(periodic_event_1s_chan); -ZBUS_LISTENER_DEFINE(zsw_magnetometer_lis, zbus_periodic_slow_callback); +ZBUS_LISTENER_DEFINE(zsw_magnetometer_lis, zbus_periodic_mid_callback); static const struct device *const magnetometer = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(lis2mdl)); -static void zbus_periodic_slow_callback(const struct zbus_channel *chan) +static void zbus_periodic_mid_callback(const struct zbus_channel *chan) { float x; float y; diff --git a/app/src/sensors/zsw_pressure_sensor.c b/app/src/sensors/zsw_pressure_sensor.c index 7da17044..871197f2 100644 --- a/app/src/sensors/zsw_pressure_sensor.c +++ b/app/src/sensors/zsw_pressure_sensor.c @@ -24,14 +24,14 @@ LOG_MODULE_REGISTER(zsw_pressure_sensor, CONFIG_ZSW_SENSORS_LOG_LEVEL); -static void zbus_periodic_slow_callback(const struct zbus_channel *chan); +static void zbus_periodic_mid_callback(const struct zbus_channel *chan); ZBUS_CHAN_DECLARE(pressure_data_chan); ZBUS_CHAN_DECLARE(periodic_event_1s_chan); -ZBUS_LISTENER_DEFINE(zsw_pressure_sensor_lis, zbus_periodic_slow_callback); +ZBUS_LISTENER_DEFINE(zsw_pressure_sensor_lis, zbus_periodic_mid_callback); static const struct device *const bmp581 = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(bmp581)); -static void zbus_periodic_slow_callback(const struct zbus_channel *chan) +static void zbus_periodic_mid_callback(const struct zbus_channel *chan) { float pressure; float temperature; diff --git a/app/src/zsw_clock.c b/app/src/zsw_clock.c index ec5b178c..ddb66b01 100644 --- a/app/src/zsw_clock.c +++ b/app/src/zsw_clock.c @@ -38,16 +38,15 @@ #if CONFIG_RTC static const struct device *const rtc = DEVICE_DT_GET(DT_ALIAS(rtc)); #else -static void zbus_periodic_slow_callback(const struct zbus_channel *chan); +static void zbus_periodic_mid_callback(const struct zbus_channel *chan); ZBUS_CHAN_DECLARE(periodic_event_1s_chan); -ZBUS_LISTENER_DEFINE(zsw_clock_lis, zbus_periodic_slow_callback); +ZBUS_LISTENER_DEFINE(zsw_clock_lis, zbus_periodic_mid_callback); #endif LOG_MODULE_REGISTER(zsw_clock, LOG_LEVEL_INF); #ifndef CONFIG_RTC - static time_t zsw_clock_get_time_unix(void) { struct timeval tv; @@ -57,14 +56,34 @@ static time_t zsw_clock_get_time_unix(void) return tv.tv_sec; } -static void zbus_periodic_slow_callback(const struct zbus_channel *chan) +static void zbus_periodic_mid_callback(const struct zbus_channel *chan) { retained.current_time_seconds = zsw_clock_get_time_unix(); zsw_retained_ram_update(); } #endif -void zsw_clock_set_time(zsw_timeval_t *ztm) +void zsw_clock_set_timezone(char *tz) +{ +#if CONFIG_RTC + +#else + if (strlen(tz) > 0) { + setenv("TZ", tz, 1); + tzset(); + } +#endif +} + +void zsw_clock_get_timezone(char *tz) +{ +#if CONFIG_RTC +#else +#endif + tz = getenv("TZ"); +} + +int zsw_clock_set_time(zsw_timeval_t *ztm) { // Substract one from the month because we want to count from December instead of January ztm->tm.tm_mon -= 1; @@ -73,29 +92,30 @@ void zsw_clock_set_time(zsw_timeval_t *ztm) ztm->tm.tm_year -= 1900; #if CONFIG_RTC - rtc_set_time(rtc, &ztm->tm); + if (rtc_set_time(rtc, &ztm->tm) != 0) { + return -EBUSY; + } #else struct timespec tspec; -#warning "Not implemented" - tspec.tv_sec = 0; + tspec.tv_sec = mktime(&ztm->tm); tspec.tv_nsec = 0; clock_settime(CLOCK_REALTIME, &tspec); - if (strlen(retained.timezone) > 0) { - setenv("TZ", retained.timezone, 1); - tzset(); - } #endif + + return 0; } -void zsw_clock_get_time(zsw_timeval_t *ztm) +int zsw_clock_get_time(zsw_timeval_t *ztm) { -#if CONFIG_RTC - struct rtc_time tm; + // Set the buffer to 0 to avoid issues with invalid values + memset(ztm, 0, sizeof(zsw_timeval_t)); - rtc_get_time(rtc, &tm); - memcpy(ztm, &tm, sizeof(struct rtc_time)); +#if CONFIG_RTC + if (rtc_get_time(rtc, &ztm->tm) != 0) { + return -ENODATA; + } #else struct tm *tm; struct timeval tv; @@ -110,26 +130,36 @@ void zsw_clock_get_time(zsw_timeval_t *ztm) // Add 1900 to the year because we want to count from 1900 ztm->tm.tm_year += 1900; + + return 0; } static int zsw_clock_init(void) { + zsw_timeval_t ztm; + #if CONFIG_RTC if (!device_is_ready(rtc)) { LOG_ERR("Device not ready!"); return -EBUSY; } -#else - struct timespec tspec; - - tspec.tv_sec = retained.current_time_seconds; - tspec.tv_nsec = 0; - clock_settime(CLOCK_REALTIME, &tspec); - if (strlen(retained.timezone) > 0) { - setenv("TZ", retained.timezone, 1); - tzset(); + // Check if the oscillator stop flag is set and set a default time to reset it + if (rtc_get_time(rtc, &ztm.tm) != 0) { + ztm.tm.tm_hour = 0; + ztm.tm.tm_min = 1; + ztm.tm.tm_sec = 0; + ztm.tm.tm_wday = 6; + ztm.tm.tm_mday = 1; + ztm.tm.tm_mon = 1; + ztm.tm.tm_year = 2000; + + return zsw_clock_set_time(&ztm); } +#else + memcpy(&ztm.tm, localtime((time_t *)&retained.current_time_seconds), sizeof(ztm.tm)); + zsw_clock_set_time(&ztm); + zsw_clock_set_timezone(retained.timezone); zsw_periodic_chan_add_obs(&periodic_event_1s_chan, &zsw_clock_lis); #endif diff --git a/app/src/zsw_clock.h b/app/src/zsw_clock.h index a9e05c00..5e33311d 100644 --- a/app/src/zsw_clock.h +++ b/app/src/zsw_clock.h @@ -17,10 +17,13 @@ #pragma once +#if CONFIG_RTC #include +#else +#include +#endif #include -#include #if CONFIG_RTC typedef void (*zsw_clock_on_update)(void); @@ -36,13 +39,24 @@ typedef struct { uint32_t tv_usec; } zsw_timeval_t; -/** @brief - * NOTE: This function needs the time as seconds - * @param ztm +/** @brief Set the system time zone. + * @param tz Pointer to time zone + */ +void zsw_clock_set_timezone(char *tz); + +/** @brief Get the system time zone. + * @param tz Pointer to time zone + */ +void zsw_clock_get_timezone(char *tz); + +/** @brief Set the system time. + * @param ztm Pointer to time object + * @return 0 when successful */ -void zsw_clock_set_time(zsw_timeval_t *ztm); +int zsw_clock_set_time(zsw_timeval_t *ztm); -/** @brief - * @param ztm +/** @brief Get the system time. + * @param ztm Pointer to time object + * @return 0 when successful */ -void zsw_clock_get_time(zsw_timeval_t *ztm); \ No newline at end of file +int zsw_clock_get_time(zsw_timeval_t *ztm); \ No newline at end of file