1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <string.h>
8 
9 #include "aos/kernel.h"
10 #include "aos/hal/rtc.h"
11 #include "rtc_test.h"
12 
13 #define RTC_DELAY_MS 2000
14 #define RTC_TOLERANCE_RANGE 0.01f
15 
16 static rtc_time_t rtc_time_r;
17 
18 static rtc_time_t rtc_time = {.sec = 45, .min = 30, .hr = 15, .weekday = 5, .date = 1, .month = 3, .year = 19};
19 
20 static rtc_dev_t  rtc_dev;
21 
hal_rtc_test(void)22 void hal_rtc_test(void)
23 {
24     int ret = -1;
25 
26     printf("*********** rtc test start ! ***********\n");
27 
28     memset(&rtc_time_r, 0, sizeof(rtc_time_t));
29 
30     rtc_dev.port = PORT_RTC_TEST;
31     rtc_dev.config.format = HAL_RTC_FORMAT_DEC;
32 
33     printf("step1: set rtc date to\n");
34     printf("        year: %d month: %d date: %d weekday: %d hr: %d min: %d sec: %d\n",
35            rtc_time.year, rtc_time.month, rtc_time.date, rtc_time.weekday,
36            rtc_time.hr, rtc_time.min, rtc_time.sec);
37 
38     ret = hal_rtc_init(&rtc_dev);
39     if (ret != 0) {
40         printf("hal_rtc_init error !\n");
41         return;
42     }
43 
44     ret = hal_rtc_set_time(&rtc_dev, &rtc_time);
45     if (ret != 0) {
46         printf("hal_rtc_set_time error !\n");
47         return;
48     }
49 
50     printf("step2: sleep %d ms !\n", RTC_DELAY_MS);
51     aos_msleep(RTC_DELAY_MS * (1 + RTC_TOLERANCE_RANGE));
52 
53     ret = hal_rtc_get_time(&rtc_dev, &rtc_time_r);
54     if (ret != 0) {
55         printf("hal_rtc_get_time error !\n");
56         return;
57     }
58 
59     printf("step3: read rtc, current date is\n");
60     printf("        year: %d month: %d date: %d weekday: %d hr: %d min: %d sec: %d\n",
61            rtc_time_r.year, rtc_time_r.month, rtc_time_r.date, rtc_time_r.weekday,
62            rtc_time_r.hr, rtc_time_r.min, rtc_time_r.sec);
63 
64     if ((rtc_time_r.sec != rtc_time.sec + RTC_DELAY_MS / 1000)
65         ||(rtc_time_r.min != rtc_time.min)
66         ||(rtc_time_r.hr != rtc_time.hr)
67         ||(rtc_time_r.weekday != rtc_time.weekday)
68         ||(rtc_time_r.date != rtc_time.date)
69         ||(rtc_time_r.month != rtc_time.month)
70         ||(rtc_time_r.year != rtc_time.year)) {
71         printf("rtc value error !\n");
72         return;
73     }
74 
75     ret = hal_rtc_finalize(&rtc_dev);
76     if (ret != 0) {
77         printf("hal_rtc_finalize error !\n");
78         return;
79     }
80 
81     printf("rtc test result: succeed !\n");
82 
83     printf("*********** rtc test end ! ***********\n");
84 }
85