Skip to content

Commit 5b3cba8

Browse files
drivers: rtc: shell: Add sys_realtime sync command
Add rtc shell command "sync" which synchronizes the system realtime with the RTC time of the given RTC. The command is used like: $uart: rtc sync rtc to set the system realtime clock to the time of the rtc. Signed-off-by: Bjarki Arge Andreasen <[email protected]>
1 parent 8ab3bc6 commit 5b3cba8

File tree

1 file changed

+53
-5
lines changed

1 file changed

+53
-5
lines changed

drivers/rtc/rtc_shell.c

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <zephyr/kernel.h>
99
#include <zephyr/shell/shell.h>
1010
#include <zephyr/drivers/rtc.h>
11+
#include <zephyr/sys/realtime.h>
1112
#include <time.h>
1213
#include <stdlib.h>
1314

@@ -227,6 +228,43 @@ static void device_name_get(size_t idx, struct shell_static_entry *entry)
227228
entry->subcmd = NULL;
228229
}
229230

231+
#if CONFIG_SYS_REALTIME
232+
static int cmd_sync(const struct shell *sh, size_t argc, char **argv)
233+
{
234+
const struct device *dev = device_get_binding(argv[1]);
235+
236+
if (!device_is_ready(dev)) {
237+
shell_error(sh, "device %s not %s", argv[1], "ready");
238+
return -ENODEV;
239+
}
240+
241+
ARG_UNUSED(argc);
242+
ARG_UNUSED(argv);
243+
244+
struct rtc_time rtctime;
245+
246+
int res = rtc_get_time(dev, &rtctime);
247+
248+
if (res == -ENODATA) {
249+
shell_error(sh, "device %s not %s", argv[1], "set");
250+
return 0;
251+
}
252+
if (res < 0) {
253+
return res;
254+
}
255+
256+
struct sys_datetime *datetime = (struct sys_datetime *)(&rtctime);
257+
258+
res = sys_realtime_set_datetime(datetime);
259+
260+
if (res < 0) {
261+
shell_error(sh, "failed to set system realtime from %s", argv[1]);
262+
}
263+
264+
return res;
265+
}
266+
#endif /* CONFIG_SYS_REALTIME */
267+
230268
#define RTC_GET_HELP \
231269
("Get current time (UTC)\n" \
232270
"Usage: rtc get <device>")
@@ -235,12 +273,22 @@ static void device_name_get(size_t idx, struct shell_static_entry *entry)
235273
("Set UTC time\n" \
236274
"Usage: rtc set <device> <YYYY-MM-DDThh:mm:ss> | <YYYY-MM-DD> | <hh:mm:ss>")
237275

276+
#if CONFIG_SYS_REALTIME
277+
#define RTC_SYNC_HELP \
278+
("Set system realtime from RTC time\n" \
279+
"Usage: rtc sync <device>")
280+
#endif /* CONFIG_SYS_REALTIME */
281+
238282
SHELL_DYNAMIC_CMD_CREATE(dsub_device_name, device_name_get);
239283

240-
SHELL_STATIC_SUBCMD_SET_CREATE(sub_rtc,
241-
/* Alphabetically sorted */
242-
SHELL_CMD_ARG(set, &dsub_device_name, RTC_SET_HELP, cmd_set, 3, 0),
243-
SHELL_CMD_ARG(get, &dsub_device_name, RTC_GET_HELP, cmd_get, 2, 0),
244-
SHELL_SUBCMD_SET_END);
284+
SHELL_STATIC_SUBCMD_SET_CREATE(
285+
sub_rtc,
286+
SHELL_CMD_ARG(set, &dsub_device_name, RTC_SET_HELP, cmd_set, 3, 0),
287+
SHELL_CMD_ARG(get, &dsub_device_name, RTC_GET_HELP, cmd_get, 2, 0),
288+
#if CONFIG_SYS_REALTIME
289+
SHELL_CMD_ARG(sync, &dsub_device_name, RTC_SYNC_HELP, cmd_sync, 2, 0),
290+
#endif /* CONFIG_SYS_REALTIME */
291+
SHELL_SUBCMD_SET_END
292+
);
245293

246294
SHELL_CMD_REGISTER(rtc, &sub_rtc, "RTC commands", NULL);

0 commit comments

Comments
 (0)