Skip to content

Commit

Permalink
Take version from board overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
Kampi committed Aug 8, 2024
1 parent 473a5bb commit 3d7d1f8
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 163 deletions.
6 changes: 0 additions & 6 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,7 @@ else()
add_subdirectory(src/basic_battery)
endif()

# TODO
target_sources(app PRIVATE src/zsw_clock.c)
#if (CONFIG_RTC)
# target_sources(app PRIVATE src/zsw_rtc.c)
#else()
#endif()

target_sources(app PRIVATE src/main.c)
target_sources(app PRIVATE src/zsw_cpu_freq.c)
target_sources(app PRIVATE src/zsw_retained_ram_storage.c)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ CONFIG_DEBUG_COREDUMP_BACKEND_OTHER=y

CONFIG_RTC=y
CONFIG_RTC_UPDATE=y
CONFIG_RTC_ALARM=y

CONFIG_MISC_ENABLE_SYSTEM_RESET=n # Implemented in nPM, hence not needed in FW

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
aliases {
spi-flash0 = &mx25u51245g;
buzzer-pwm = &buzzer_pwm;
rtc = &rv_8263_c8;
};

longpress: longpress {
Expand Down
Binary file added app/lvgl_resources
Binary file not shown.
48 changes: 32 additions & 16 deletions app/patches/zephyr/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,787 @@
+/* Copyright (c) 2024 Daniel Kampert
+ * Author: Daniel Kampert <DanielKampert@kampis-Elektroecke.de>
+ */
Expand Down Expand Up @@ -275,6 +275,25 @@ index 0000000000..bb82ed7d9b
+}
+
+#if (CONFIG_RTC_ALARM || CONFIG_RTC_UPDATE) && DT_ANY_INST_HAS_PROP_STATUS_OKAY(int_gpios)
+static int rv8263c8_disable_alarm(const struct device *dev)
+{
+ int err;
+ const struct rv8263c8_config *config = dev->config;
+ uint8_t buf[] = {RV8263C8_REGISTER_SECONDS_ALARM, RV8263C8_BM_ALARM_DISABLE,
+ RV8263C8_BM_ALARM_DISABLE, RV8263C8_BM_ALARM_DISABLE,
+ RV8263C8_BM_ALARM_DISABLE, RV8263C8_BM_ALARM_DISABLE};
+
+ /* We start with the seconds alarm to disable the alarm and continue for all other alarm */
+ /* registers. */
+ err = i2c_write_dt(&config->i2c_bus, buf, 6);
+ if (err) {
+ return err;
+ }
+
+ return i2c_reg_update_byte_dt(&config->i2c_bus, RV8263C8_REGISTER_CONTROL_2,
+ RV8263C8_BM_ALARM_INT_ENABLE, RV8263C8_BM_ALARM_INT_DISABLE);
+}
+
+static void rv8263c8_gpio_callback_handler(const struct device *p_port, struct gpio_callback *p_cb,
+ gpio_port_pins_t pins)
+{
Expand Down Expand Up @@ -404,10 +423,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);
Expand Down Expand Up @@ -566,9 +585,7 @@ index 0000000000..bb82ed7d9b
+
+ /* Disable the alarm when mask is zero. */
+ if (mask == 0) {
+ return i2c_reg_update_byte_dt(&config->i2c_bus, RV8263C8_REGISTER_CONTROL_2,
+ RV8263C8_BM_ALARM_INT_ENABLE,
+ RV8263C8_BM_ALARM_INT_DISABLE);
+ return rv8263c8_disable_alarm(dev);
+ }
+
+ if (!rtc_utils_validate_rtc_time(timeptr, mask)) {
Expand Down Expand Up @@ -671,27 +688,28 @@ index 0000000000..bb82ed7d9b
+ return err;
+ }
+
+ if (value[0] <= MAX_SEC) {
+ /* Check if the highest bit is not set. If so the alarm is enabled. */
+ if ((value[0] & RV8263C8_BM_ALARM_DISABLE) == 0) {
+ timeptr->tm_sec = bcd2bin(value[0]) & SECONDS_BITS;
+ (*p_mask) |= RTC_ALARM_TIME_MASK_SECOND;
+ }
+
+ if (value[1] <= MAX_MIN) {
+ if ((value[1] & RV8263C8_BM_ALARM_DISABLE) == 0) {
+ timeptr->tm_min = bcd2bin(value[1]) & MINUTES_BITS;
+ (*p_mask) |= RTC_ALARM_TIME_MASK_MINUTE;
+ }
+
+ if (value[2] <= MAX_HOUR) {
+ if ((value[2] & RV8263C8_BM_ALARM_DISABLE) == 0) {
+ timeptr->tm_hour = bcd2bin(value[2]) & HOURS_BITS;
+ (*p_mask) |= RTC_ALARM_TIME_MASK_HOUR;
+ }
+
+ if (value[3] <= MAX_MDAY) {
+ if ((value[3] & RV8263C8_BM_ALARM_DISABLE) == 0) {
+ timeptr->tm_mday = bcd2bin(value[3]) & DATE_BITS;
+ (*p_mask) |= RTC_ALARM_TIME_MASK_MONTHDAY;
+ }
+
+ if (value[4] <= MAX_WDAY) {
+ if ((value[4] & RV8263C8_BM_ALARM_DISABLE) == 0) {
+ timeptr->tm_wday = bcd2bin(value[4]) & WEEKDAY_BITS;
+ (*p_mask) |= RTC_ALARM_TIME_MASK_WEEKDAY;
+ }
Expand Down Expand Up @@ -727,9 +745,7 @@ index 0000000000..bb82ed7d9b
+ if ((callback == NULL) && (user_data == NULL)) {
+ LOG_DBG("Disable alarm function");
+
+ err = i2c_reg_update_byte_dt(&config->i2c_bus, RV8263C8_REGISTER_CONTROL_2,
+ RV8263C8_BM_ALARM_INT_ENABLE,
+ RV8263C8_BM_ALARM_INT_DISABLE);
+ err = rv8263c8_disable_alarm(dev);
+ } else {
+ LOG_DBG("Enable alarm function");
+
Expand Down
5 changes: 3 additions & 2 deletions app/src/ble/ble_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ BT_GATT_SERVICE_DEFINE(nus_service,
NULL, NULL, NULL),
BT_GATT_CCC(NULL,
#if CONFIG_BLE_DISABLE_PAIRING_REQUIRED
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE
#else
BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT),
BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT
#endif
),
BT_GATT_CHARACTERISTIC(BLE_TRANSPORT_UUID_RX,
BT_GATT_CHRC_WRITE |
BT_GATT_CHRC_WRITE_WITHOUT_RESP,
Expand Down
53 changes: 44 additions & 9 deletions app/src/events/zsw_periodic_event.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
#include <zephyr/kernel.h>
#include <zephyr/init.h>
#include <zephyr/kernel.h>
#include <zephyr/zbus/zbus.h>

#if CONFIG_RTC
#include <zephyr/drivers/rtc.h>
#endif

#include "zsw_clock.h"
#include "events/periodic_event.h"

#define PERIODIC_FAST_INTERVAL_MS 100
#define PERIODIC_MID_INTERVAL_MS 1000
#define PERIODIC_SLOW_INTERVAL_MS 10000

static void handle_slow_timeout(struct k_work *item);
static void handle_fast_timeout(struct k_work *item);
static void handle_mid_timeout(struct k_work *item);
#if CONFIG_RTC
static const struct device *const rtc = DEVICE_DT_GET(DT_ALIAS(rtc));

void handle_mid_timeout(const struct device *dev, void *user_data);
#endif

ZBUS_CHAN_DECLARE(periodic_event_10s_chan);
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)
Expand All @@ -34,7 +41,11 @@ int zsw_periodic_chan_add_obs(const struct zbus_channel *chan, const struct zbus
if (chan == &periodic_event_10s_chan) {
ret = k_work_reschedule(work, K_MSEC(PERIODIC_SLOW_INTERVAL_MS));
} else if (chan == &periodic_event_1s_chan) {
#if CONFIG_RTC
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 {
Expand All @@ -51,9 +62,16 @@ int zsw_periodic_chan_rm_obs(const struct zbus_channel *chan, const struct zbus_
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);
#if CONFIG_RTC_UPDATE
if (chan == &periodic_event_1s_chan) {
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;
}
Expand All @@ -71,6 +89,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
static void handle_mid_timeout(struct k_work *item)
{
struct periodic_event evt = {
Expand All @@ -83,6 +110,7 @@ static void handle_mid_timeout(struct k_work *item)

zbus_chan_pub(&periodic_event_1s_chan, &evt, K_MSEC(250));
}
#endif

static void handle_fast_timeout(struct k_work *item)
{
Expand All @@ -100,15 +128,22 @@ static void handle_fast_timeout(struct k_work *item)
static int zsw_timer_init(void)
{
struct k_work_delayable *work = NULL;

work = (struct k_work_delayable *)zbus_chan_user_data(&periodic_event_10s_chan);
k_work_init_delayable(work, handle_slow_timeout);

// TODO: Use the RTC instead of the timer in rev 5 to generate this
#if CONFIG_RTC
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

work = (struct k_work_delayable *)zbus_chan_user_data(&periodic_event_100ms_chan);
k_work_init_delayable(work, handle_fast_timeout);

return 0;
}

Expand Down
28 changes: 20 additions & 8 deletions app/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,22 @@ 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");
Expand Down Expand Up @@ -594,20 +610,16 @@ 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((const 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;
}
Expand Down
Loading

0 comments on commit 3d7d1f8

Please sign in to comment.