File tree 4 files changed +71
-0
lines changed
4 files changed +71
-0
lines changed Original file line number Diff line number Diff line change @@ -21,3 +21,4 @@ zephyr_library_sources_ifdef(CONFIG_RTC_SMARTBOND rtc_smartbond.c)
21
21
zephyr_library_sources_ifdef(CONFIG_RTC_ATMEL_SAM rtc_sam.c)
22
22
zephyr_library_sources_ifdef(CONFIG_RTC_RPI_PICO rtc_rpi_pico.c)
23
23
zephyr_library_sources_ifdef(CONFIG_RTC_NUMAKER rtc_numaker.c)
24
+ zephyr_library_sources_ifdef(CONFIG_RTC_HWCLOCK rtc_hwclock.c)
Original file line number Diff line number Diff line change @@ -53,5 +53,6 @@ source "drivers/rtc/Kconfig.sam"
53
53
source "drivers/rtc/Kconfig.smartbond"
54
54
source "drivers/rtc/Kconfig.stm32"
55
55
source "drivers/rtc/Kconfig.numaker"
56
+ source "drivers/rtc/Kconfig.hwclock"
56
57
57
58
endif # RTC
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 );
You can’t perform that action at this time.
0 commit comments