Skip to content

Commit

Permalink
Merge branch 'jakkra:main' into RTC-Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kampi committed Jul 30, 2024
2 parents 3d1fe61 + 8384fc9 commit 1540492
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 28 deletions.
4 changes: 2 additions & 2 deletions app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_5.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 9 additions & 5 deletions app/patches/rv8263_rtc.patch
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ new file mode 100644
index 0000000000..bb82ed7d9b
--- /dev/null
+++ b/drivers/rtc/rtc_rv8263.c
@@ -0,0 +1,771 @@
@@ -0,0 +1,775 @@
+/* Copyright (c) 2024 Daniel Kampert
+ * Author: Daniel Kampert <[email protected]>
+ */
Expand Down Expand Up @@ -404,10 +404,14 @@ 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. */
+ /* The flag is set again if the error condition for the oscillator is still active. */
+ if (regs[0] & RV8263_BM_OS) {
+ regs[0] &= ~RV8263_BM_OS;
+ i2c_reg_write_byte_dt(&config->i2c_bus, RV8263C8_REGISTER_SECONDS,
+ regs[0]);
+ return -ECANCELED;
+ }
+
+ timeptr->tm_sec = bcd2bin(regs[0] & SECONDS_BITS);
+ timeptr->tm_min = bcd2bin(regs[1] & MINUTES_BITS);
Expand Down
36 changes: 18 additions & 18 deletions app/src/events/zsw_periodic_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ 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
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 = {
Expand All @@ -97,7 +97,7 @@ 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)
{
Expand All @@ -119,16 +119,16 @@ 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
if (!device_is_ready(rtc)) {
return -EBUSY;
}

rtc_update_set_callback(rtc, handle_mid_timeout, NULL);
#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);
Expand Down
17 changes: 14 additions & 3 deletions app/src/zsw_clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ ZBUS_LISTENER_DEFINE(zsw_clock_lis, zbus_periodic_slow_callback);
LOG_MODULE_REGISTER(zsw_clock, LOG_LEVEL_INF);

#ifndef CONFIG_RTC

static time_t zsw_clock_get_time_unix(void)
{
struct timeval tv;
Expand Down Expand Up @@ -91,11 +90,23 @@ void zsw_clock_set_time(zsw_timeval_t *ztm)

void zsw_clock_get_time(zsw_timeval_t *ztm)
{
// Set the buffer to 0 to avoid issues with invalid values.
memset(ztm, 0, sizeof(struct rtc_time));

#if CONFIG_RTC
struct rtc_time tm;

rtc_get_time(rtc, &tm);
memcpy(ztm, &tm, sizeof(struct rtc_time));
if (rtc_get_time(rtc, &tm) == 0) {
memcpy(ztm, &tm, sizeof(struct rtc_time));

LOG_INF("Seconds: %u", ztm->tm.tm_sec);
LOG_INF("Minutes: %u", ztm->tm.tm_min);
LOG_INF("Hours: %u", ztm->tm.tm_hour);
LOG_INF("Weekday: %u", ztm->tm.tm_wday);
LOG_INF("Day: %u", ztm->tm.tm_mday);
LOG_INF("Month: %u", ztm->tm.tm_mon);
LOG_INF("Year: %u", ztm->tm.tm_year);
}
#else
struct tm *tm;
struct timeval tv;
Expand Down

0 comments on commit 1540492

Please sign in to comment.