1 /*
2  * Copyright (c) 2025 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/drivers/rtc.h>
9 
10 #define RTC_ALARM_FIELDS                                                                           \
11 	(RTC_ALARM_TIME_MASK_SECOND | RTC_ALARM_TIME_MASK_MINUTE | RTC_ALARM_TIME_MASK_HOUR)
12 
13 static const struct device *rtc = DEVICE_DT_GET(DT_NODELABEL(rtc));
14 
15 static k_tid_t my_tid;
16 
17 #define MY_STACK_SIZE 500
18 #define MY_PRIORITY   5
19 
20 struct k_thread my_thread_data;
21 K_THREAD_STACK_DEFINE(my_stack_area, MY_STACK_SIZE);
22 
my_entry_point(void *,void *,void *)23 void my_entry_point(void *, void *, void *)
24 {
25 	printk("Going sleep.\n");
26 	k_msleep(3000);
27 }
28 
29 /* Fri Jan 01 2021 13:29:50 GMT+0000 */
30 static const struct rtc_time rtc_time = {
31 	.tm_sec = 50,
32 	.tm_min = 29,
33 	.tm_hour = 13,
34 	.tm_mday = 1,
35 	.tm_mon = 0,
36 	.tm_year = 121,
37 	.tm_wday = 5,
38 	.tm_yday = 1,
39 	.tm_isdst = -1,
40 	.tm_nsec = 0,
41 };
42 
43 /* Fri Jan 01 2021 13:29:51 GMT+0000 */
44 static const struct rtc_time alarm_time = {
45 	.tm_sec = 51,
46 	.tm_min = 29,
47 	.tm_hour = 13,
48 	.tm_mday = 1,
49 	.tm_mon = 0,
50 	.tm_year = 121,
51 	.tm_wday = 5,
52 	.tm_yday = 1,
53 	.tm_isdst = -1,
54 	.tm_nsec = 0,
55 };
56 
wakeup_cb(const struct device * dev,uint16_t id,void * user_data)57 static void wakeup_cb(const struct device *dev, uint16_t id, void *user_data)
58 {
59 	printk("Wake up by alarm.\n");
60 	k_thread_abort(my_tid);
61 }
62 
main(void)63 int main(void)
64 {
65 	int ret;
66 	uint16_t mask = RTC_ALARM_FIELDS;
67 
68 	ret = rtc_set_time(rtc, &rtc_time);
69 	if (ret < 0) {
70 		return 0;
71 	}
72 
73 	ret = rtc_alarm_set_time(rtc, 0, mask, &alarm_time);
74 	if (ret < 0) {
75 		return 0;
76 	}
77 
78 	ret = rtc_alarm_set_callback(rtc, 0, wakeup_cb, NULL);
79 	if (ret < 0) {
80 		return 0;
81 	}
82 
83 	printk("Created the thread.\n");
84 	my_tid = k_thread_create(&my_thread_data, my_stack_area,
85 				 K_THREAD_STACK_SIZEOF(my_stack_area), my_entry_point, NULL, NULL,
86 				 NULL, MY_PRIORITY, 0, K_NO_WAIT);
87 
88 	k_thread_join(my_tid, K_FOREVER);
89 
90 	while (1) {
91 		arch_nop();
92 	}
93 	return 0;
94 }
95