1 /*
2  * Copyright (c) 2024 Nuvoton Technology Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdio.h>
8 #include <time.h>
9 #include <zephyr/kernel.h>
10 #include <zephyr/drivers/rtc.h>
11 #include <zephyr/sys/poweroff.h>
12 
13 /* Fri Jan 01 2021 13:29:50 GMT+0000 */
14 #define RTC_TEST_ALARM_SET_TIME		      1609507790
15 #define RTC_TEST_ALARM_TIME_MINUTE	      30
16 #define RTC_TEST_ALARM_TIME_HOUR	      13
17 
18 static const struct device *rtc = DEVICE_DT_GET(DT_ALIAS(rtc));
19 
set_alarm_10s(void)20 static void set_alarm_10s(void)
21 {
22 	int ret;
23 	time_t timer_set;
24 	struct rtc_time time_set;
25 	struct rtc_time alarm_time_set;
26 	uint16_t alarm_time_mask_set;
27 
28 	/* Initialize RTC time to set */
29 	timer_set = RTC_TEST_ALARM_SET_TIME;
30 
31 	gmtime_r(&timer_set, (struct tm *)(&time_set));
32 
33 	time_set.tm_isdst = -1;
34 	time_set.tm_nsec = 0;
35 
36 	/* Set RTC time */
37 	ret = rtc_set_time(rtc, &time_set);
38 
39 	/* Set alarm time */
40 	alarm_time_set.tm_min = RTC_TEST_ALARM_TIME_MINUTE;
41 	alarm_time_set.tm_hour = RTC_TEST_ALARM_TIME_HOUR;
42 	alarm_time_mask_set = (RTC_ALARM_TIME_MASK_MINUTE | RTC_ALARM_TIME_MASK_HOUR);
43 	ret = rtc_alarm_set_time(rtc, 0, alarm_time_mask_set, &alarm_time_set);
44 	if (ret) {
45 		printk("Failed to set alarm\n");
46 	}
47 }
48 
main(void)49 int main(void)
50 {
51 	const struct device *dev = DEVICE_DT_GET(DT_NODELABEL(rtc));
52 
53 	if (!device_is_ready(dev)) {
54 		printf("Counter device not ready\n");
55 		return 0;
56 	}
57 
58 	printk("Wake-up alarm set for 10 seconds.\n");
59 	set_alarm_10s();
60 
61 	/* Set PF.4~PF.11 as digital path disable to save power at power down */
62 	GPIO_DISABLE_DIGITAL_PATH(PF, BIT11 | BIT10 | BIT9 | BIT8 | BIT7 | BIT6 | BIT5 | BIT4);
63 	printk("Disable digital path pins\n");
64 
65 	printk("Powering off ....................\n");
66 	sys_poweroff();
67 
68 	return 0;
69 }
70