diff --git a/hw/drivers/stm32_rtc/stm32_rtc.c b/hw/drivers/stm32_rtc/stm32_rtc.c index ca285ccb..3d81a223 100644 --- a/hw/drivers/stm32_rtc/stm32_rtc.c +++ b/hw/drivers/stm32_rtc/stm32_rtc.c @@ -146,6 +146,22 @@ struct tm *hw_get_time(void) return &time_now; } +void hw_set_time(struct tm *time_now) +{ + RTC_TimeTypeDef RTC_TimeStructure; + RTC_DateTypeDef RTC_DateStructure; + + RTC_DateStructure.RTC_Year = time_now->tm_year - 2000 + 1900; // idk why but get_time does it too... + RTC_DateStructure.RTC_Month = time_now->tm_mon + 1; // same applies to this offset + RTC_DateStructure.RTC_Date = time_now->tm_mday; + RTC_TimeStructure.RTC_Hours = time_now->tm_hour; + RTC_TimeStructure.RTC_Minutes = time_now->tm_min; + RTC_TimeStructure.RTC_Seconds = time_now->tm_sec; + + RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure); + RTC_SetDate(RTC_Format_BIN, &RTC_DateStructure); +} + void hw_set_alarm(struct tm alarm) { // RTC_AlarmStructure.RTC_AlarmTime.RTC_H12 = RTC_H12_AM; @@ -163,23 +179,6 @@ void hw_set_alarm(struct tm alarm) // RTC_ClearFlag(RTC_FLAG_ALRAF); } -void hw_set_date_time(struct tm date_time) -{ -// RTC_DateStructure.RTC_Year = 0x13; -// RTC_DateStructure.RTC_Month = RTC_Month_January; -// RTC_DateStructure.RTC_Date = 0x11; -// RTC_DateStructure.RTC_WeekDay = RTC_Weekday_Saturday; -// RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure); -// -// RTC_TimeStructure.RTC_H12 = RTC_H12_AM; -// RTC_TimeStructure.RTC_Hours = 0x05; -// RTC_TimeStructure.RTC_Minutes = 0x20; -// RTC_TimeStructure.RTC_Seconds = 0x00; -// -// RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure); -} - - void RTC_WKUP_IRQHandler(void) { if(RTC_GetITStatus(RTC_IT_WUT) != RESET) diff --git a/hw/drivers/stm32_rtc/stm32_rtc.h b/hw/drivers/stm32_rtc/stm32_rtc.h index 91e0bcc9..65e1a5fe 100644 --- a/hw/drivers/stm32_rtc/stm32_rtc.h +++ b/hw/drivers/stm32_rtc/stm32_rtc.h @@ -23,6 +23,7 @@ void rtc_init(void); void rtc_config(void); struct tm *hw_get_time(void); +void hw_set_time(struct tm *time_now); #endif diff --git a/hw/platform/tintin/tintin.c b/hw/platform/tintin/tintin.c index 0a7b3876..97c83b3c 100644 --- a/hw/platform/tintin/tintin.c +++ b/hw/platform/tintin/tintin.c @@ -19,7 +19,6 @@ #include "rebbleos.h" #include "stm32_power.h" -#include "stm32_rtc.h" // extern void *strcpy(char *a2, const char *a1); diff --git a/hw/platform/tintin/tintin.h b/hw/platform/tintin/tintin.h index 1e2ef76f..3b519a4f 100644 --- a/hw/platform/tintin/tintin.h +++ b/hw/platform/tintin/tintin.h @@ -34,13 +34,6 @@ uint8_t *hw_display_get_buffer(void); void hw_watchdog_init(); void hw_watchdog_reset(); -void rtc_init(); -void rtc_config(); -void hw_get_time_str(char *buf); -struct tm *hw_get_time(void); -void rtc_set_timer_interval(TimeUnits tick_units); -void rtc_disable_timer_interval(void); - void hw_vibrate_init(); void hw_vibrate_enable(uint8_t enabled); diff --git a/rcore/rebble_time.c b/rcore/rebble_time.c index d75ea947..1a3f3ff4 100644 --- a/rcore/rebble_time.c +++ b/rcore/rebble_time.c @@ -8,18 +8,13 @@ #include "rebbleos.h" #include "strftime.h" -static TickType_t _boot_ticks; -static time_t _boot_time_t; +static TickType_t _sync_ticks; +static time_t _sync_time_t; + void rcore_time_init(void) { - struct tm *tm; - - /* Read the time out of the RTC, then convert to a time_t (ugh!), then - * begin offsetting ticks in ms from there. */ - _boot_ticks = xTaskGetTickCount(); - tm = hw_get_time(); - _boot_time_t = rcore_mktime(tm); + _sync_time_rtc(); } time_t rcore_mktime(struct tm *tm) @@ -34,16 +29,16 @@ void rcore_localtime(struct tm *tm, time_t time) void rcore_time_ms(time_t *tutc, uint16_t *ms) { - TickType_t ticks_since_boot = xTaskGetTickCount() - _boot_ticks; + TickType_t ticks_since_sync = xTaskGetTickCount() - _sync_ticks; - *tutc = _boot_time_t + ticks_since_boot / configTICK_RATE_HZ; - *ms = (ticks_since_boot % configTICK_RATE_HZ) * 1000 / configTICK_RATE_HZ; + *tutc = _sync_time_t + ticks_since_sync / configTICK_RATE_HZ; + *ms = (ticks_since_sync % configTICK_RATE_HZ) * 1000 / configTICK_RATE_HZ; } TickType_t rcore_time_to_ticks(time_t t, uint16_t ms) { - if (t < _boot_time_t) + if (t < _sync_time_t) return 0; - return (t - _boot_time_t) * configTICK_RATE_HZ + pdMS_TO_TICKS(ms); + return (t - _sync_time_t) * configTICK_RATE_HZ + pdMS_TO_TICKS(ms); } size_t rcore_strftime(char* buffer, size_t maxSize, const char* format, const struct tm* tm) { @@ -63,6 +58,30 @@ struct tm *rebble_time_get_tm(void) return &_global_tm; } +// Sync time and set RTC +void rebble_time_set_tm(struct tm *time_now) +{ + _sync_ticks = xTaskGetTickCount(); + _sync_time_t = rcore_mktime(time_now); + + hw_set_time(time_now); +} + +// Read the current time from the RTC +void _sync_time_rtc(void) +{ + struct tm *tm; + + /* Read the time out of the RTC, then convert to a time_t (ugh!), then + * begin offsetting ticks in ms from there. */ + _sync_ticks = xTaskGetTickCount(); + tm = hw_get_time(); + _sync_time_t = rcore_mktime(tm); +} + + + + uint16_t pbl_time_deprecated(time_t *tloc) { /* XXX time zones: utc vs local time */ diff --git a/rcore/rebble_time.h b/rcore/rebble_time.h index f00f8278..461bc6a5 100644 --- a/rcore/rebble_time.h +++ b/rcore/rebble_time.h @@ -26,8 +26,10 @@ void rcore_localtime(struct tm *tm, time_t time); void rcore_time_ms(time_t *tutc, uint16_t *ms); TickType_t rcore_time_to_ticks(time_t t, uint16_t ms); size_t rcore_strftime(char* buffer, size_t maxSize, const char* format, const struct tm* tm); +void rebble_time_set_tm(struct tm *time_now); // private +void _sync_time_rtc(void); struct tm *rebble_time_get_tm(void); int pbl_clock_is_24h_style(); uint16_t pbl_time_deprecated(time_t *tloc);