Skip to content

Commit 0433088

Browse files
drivers: rtc: add hwclock
The hwclock is selected using the chosen node "zephyr,rtc". It will automatically set the system real-time clock on boot if the RTCs time is valid. Signed-off-by: Bjarki Arge Andreasen <[email protected]>
1 parent 5b3cba8 commit 0433088

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

drivers/rtc/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ zephyr_library_sources_ifdef(CONFIG_RTC_SMARTBOND rtc_smartbond.c)
2121
zephyr_library_sources_ifdef(CONFIG_RTC_ATMEL_SAM rtc_sam.c)
2222
zephyr_library_sources_ifdef(CONFIG_RTC_RPI_PICO rtc_rpi_pico.c)
2323
zephyr_library_sources_ifdef(CONFIG_RTC_NUMAKER rtc_numaker.c)
24+
zephyr_library_sources_ifdef(CONFIG_RTC_HWCLOCK rtc_hwclock.c)

drivers/rtc/Kconfig

+1
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@ source "drivers/rtc/Kconfig.sam"
5353
source "drivers/rtc/Kconfig.smartbond"
5454
source "drivers/rtc/Kconfig.stm32"
5555
source "drivers/rtc/Kconfig.numaker"
56+
source "drivers/rtc/Kconfig.hwclock"
5657

5758
endif # RTC

drivers/rtc/Kconfig.hwclock

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright (c) 2024 Bjarki Arge Andreasen
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
DT_CHOSEN_RTC := zephyr,rtc
5+
6+
config RTC_HWCLOCK
7+
bool "Hardware clock"
8+
default y
9+
depends on $(dt_chosen_enabled,$(DT_CHOSEN_RTC))
10+
depends on SYS_REALTIME
11+
help
12+
The hardware clock is used to set the system
13+
real-time clock on boot.
14+
15+
if RTC_HWCLOCK
16+
17+
config RTC_HWCLOCK_INIT_PRIORITY
18+
int "Initialization priority of hardware clock"
19+
default 51
20+
help
21+
The initialization priority of the hardware clock
22+
must be higher than the initialization priority of
23+
the chosen RTC device driver.
24+
25+
endif # RTC_HWCLOCK

drivers/rtc/rtc_hwclock.c

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2024 Bjarki Arge Andreasen
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/init.h>
8+
#include <zephyr/drivers/rtc.h>
9+
#include <zephyr/sys/realtime.h>
10+
11+
BUILD_ASSERT(
12+
CONFIG_RTC_HWCLOCK_INIT_PRIORITY > CONFIG_RTC_INIT_PRIORITY,
13+
"Hardware clock init prio must be higher than the RTC device driver"
14+
);
15+
16+
static const struct device *rtc = DEVICE_DT_GET(DT_CHOSEN(zephyr_rtc));
17+
18+
static const struct sys_datetime *rtc_time_to_sys_datetime(const struct rtc_time *timeptr)
19+
{
20+
return (const struct sys_datetime *)timeptr;
21+
}
22+
23+
static int rtc_hwclock_init(void)
24+
{
25+
int ret;
26+
struct rtc_time rtctime;
27+
const struct sys_datetime *datetime;
28+
29+
if (!device_is_ready(rtc)) {
30+
return -ENODEV;
31+
}
32+
33+
ret = rtc_get_time(rtc, &rtctime);
34+
if (ret == -ENODATA) {
35+
return 0;
36+
} else if (ret < 0) {
37+
return ret;
38+
}
39+
40+
datetime = rtc_time_to_sys_datetime(&rtctime);
41+
return sys_realtime_set_datetime(datetime);
42+
}
43+
44+
SYS_INIT(rtc_hwclock_init, POST_KERNEL, CONFIG_RTC_HWCLOCK_INIT_PRIORITY);

0 commit comments

Comments
 (0)