Skip to content

Commit

Permalink
periodic_event: Don't use RTC for 1s event.
Browse files Browse the repository at this point in the history
It complicates the code and really doesn't add a anything.
  • Loading branch information
jakkra committed Sep 12, 2024
1 parent 50f6aed commit 56dc855
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 41 deletions.
45 changes: 7 additions & 38 deletions app/src/events/zsw_periodic_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,12 @@
#include "events/periodic_event.h"

#define PERIODIC_FAST_INTERVAL_MS 100
#define PERIODIC_SLOW_INTERVAL_MS 10000

#if CONFIG_RTC_UPDATE
#include <zephyr/drivers/rtc.h>

static const struct device *const rtc = DEVICE_DT_GET(DT_ALIAS(rtc));

static void handle_mid_timeout(const struct device *dev, void *user_data);
#else
#define PERIODIC_MID_INTERVAL_MS 1000
#endif
#define PERIODIC_SLOW_INTERVAL_MS 10000

ZBUS_CHAN_DECLARE(periodic_event_100ms_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 @@ -36,15 +27,12 @@ int zsw_periodic_chan_add_obs(const struct zbus_channel *chan, const struct zbus
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) {
#if CONFIG_RTC_UPDATE
ret = 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 @@ -60,17 +48,10 @@ int zsw_periodic_chan_rm_obs(const struct zbus_channel *chan, const struct zbus_
{
int 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
{
struct k_work_delayable *work = NULL;
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);
}
struct k_work_delayable *work = NULL;
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 @@ -88,15 +69,6 @@ static void handle_slow_timeout(struct k_work *item)
zbus_chan_pub(&periodic_event_10s_chan, &evt, K_MSEC(250));
}

#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 = {
Expand All @@ -109,7 +81,6 @@ 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 @@ -131,10 +102,8 @@ 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);

#ifndef CONFIG_RTC_UPDATE
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);
Expand Down
3 changes: 0 additions & 3 deletions app/src/zsw_clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,13 @@ typedef struct {
uint32_t tv_usec;
} zsw_timeval_t;


/**
* Sets the time of the clock.
*
* @param ztm A pointer to a `zsw_timeval_t` structure representing the desired time.
*/
void zsw_clock_set_time(zsw_timeval_t *ztm);


/**
* @brief Retrieves the current time from the.
*
Expand All @@ -53,7 +51,6 @@ void zsw_clock_set_time(zsw_timeval_t *ztm);
*/
void zsw_clock_get_time(zsw_timeval_t *ztm);


/**
* @brief Sets the timezone for the ZSW clock.
*
Expand Down

0 comments on commit 56dc855

Please sign in to comment.