Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 16 additions & 17 deletions hw/drivers/stm32_rtc/stm32_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions hw/drivers/stm32_rtc/stm32_rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion hw/platform/tintin/tintin.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "rebbleos.h"

#include "stm32_power.h"
#include "stm32_rtc.h"

// extern void *strcpy(char *a2, const char *a1);

Expand Down
7 changes: 0 additions & 7 deletions hw/platform/tintin/tintin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
47 changes: 33 additions & 14 deletions rcore/rebble_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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 */
Expand Down
2 changes: 2 additions & 0 deletions rcore/rebble_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down