1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2012-10-10     aozima       first version.
9  * 2021-06-11     iysheng      implement RTC framework V2.0
10  * 2021-07-30     Meco Man     move rtc_core.h to rtc.h
11  * 2022-04-05     tyx          add timestamp function
12  */
13 
14 #ifndef __DEV_RTC_H__
15 #define __DEV_RTC_H__
16 
17 #include <rtdef.h>
18 #include <sys/time.h>
19 /**
20  * @defgroup    group_drivers_rtc RTC
21  * @brief       RTC driver api
22  * @ingroup     group_device_driver
23  *
24  * <b>Example</b>
25  * @code {.c}
26  *
27  * #include <rtthread.h>
28  * #include <rtdevice.h>
29  *
30  * #define RTC_NAME       "rtc"
31  *
32  * static int rtc_sample(int argc, char *argv[])
33  * {
34  *     rt_err_t ret = RT_EOK;
35  *     time_t now;
36  *     rt_device_t device = RT_NULL;
37  *
38  *     device = rt_device_find(RTC_NAME);
39  *     if (!device)
40  *     {
41  *       LOG_E("find %s failed!", RTC_NAME);
42  *       return -RT_ERROR;
43  *     }
44  *
45  *     if(rt_device_open(device, 0) != RT_EOK)
46  *     {
47  *       LOG_E("open %s failed!", RTC_NAME);
48  *       return -RT_ERROR;
49  *     }
50  *
51  *     ret = set_date(2018, 12, 3);
52  *     if (ret != RT_EOK)
53  *     {
54  *         rt_kprintf("set RTC date failed\n");
55  *         return ret;
56  *     }
57  *
58  *     ret = set_time(11, 15, 50);
59  *     if (ret != RT_EOK)
60  *     {
61  *         rt_kprintf("set RTC time failed\n");
62  *         return ret;
63  *     }
64  *
65  *     rt_thread_mdelay(3000);
66  *
67  *     now = time(RT_NULL);
68  *     rt_kprintf("%s\n", ctime(&now));
69  *
70  *     return ret;
71  * }
72  * MSH_CMD_EXPORT(rtc_sample, rtc sample);
73  * @endcode
74  */
75 
76 /*!
77  * @addtogroup group_drivers_rtc
78  * @{
79  */
80 #ifdef __cplusplus
81 extern "C" {
82 #endif
83 
84 #define RT_DEVICE_CTRL_RTC_GET_TIME     (RT_DEVICE_CTRL_BASE(RTC) + 0x01)              /**< get second time */
85 #define RT_DEVICE_CTRL_RTC_SET_TIME     (RT_DEVICE_CTRL_BASE(RTC) + 0x02)              /**< set second time */
86 #define RT_DEVICE_CTRL_RTC_GET_TIMEVAL  (RT_DEVICE_CTRL_BASE(RTC) + 0x03)              /**< get timeval for gettimeofday */
87 #define RT_DEVICE_CTRL_RTC_SET_TIMEVAL  (RT_DEVICE_CTRL_BASE(RTC) + 0x04)              /**< set timeval for gettimeofday */
88 #define RT_DEVICE_CTRL_RTC_GET_ALARM    (RT_DEVICE_CTRL_BASE(RTC) + 0x05)              /**< get alarm */
89 #define RT_DEVICE_CTRL_RTC_SET_ALARM    (RT_DEVICE_CTRL_BASE(RTC) + 0x06)              /**< set alarm */
90 #define RT_DEVICE_CTRL_RTC_GET_TIMESPEC (RT_DEVICE_CTRL_BASE(RTC) + 0x07)              /**< get timespec for clock_gettime */
91 #define RT_DEVICE_CTRL_RTC_SET_TIMESPEC (RT_DEVICE_CTRL_BASE(RTC) + 0x08)              /**< set timespec for clock_settime */
92 #define RT_DEVICE_CTRL_RTC_GET_TIMERES  (RT_DEVICE_CTRL_BASE(RTC) + 0x09)              /**< get resolution for clock_getres */
93 
94 /**
95  * @brief RTC alarm structure
96  */
97 struct rt_rtc_wkalarm
98 {
99     rt_bool_t  enable;               /* 0 = alarm disabled, 1 = alarm enabled */
100     rt_int32_t tm_sec;               /* alarm at tm_sec */
101     rt_int32_t tm_min;               /* alarm at tm_min */
102     rt_int32_t tm_hour;              /* alarm at tm_hour */
103     rt_int32_t tm_mday;              /* alarm at tm_mday */
104     rt_int32_t tm_mon;               /* alarm at tm_mon */
105     rt_int32_t tm_year;              /* alarm at tm_year */
106 };
107 /**
108  * @brief RTC operations
109  */
110 struct rt_rtc_ops
111 {
112     rt_err_t (*init)(void);
113     rt_err_t (*get_secs)(time_t *sec);
114     rt_err_t (*set_secs)(time_t *sec);
115     rt_err_t (*get_alarm)(struct rt_rtc_wkalarm *alarm);
116     rt_err_t (*set_alarm)(struct rt_rtc_wkalarm *alarm);
117     rt_err_t (*get_timeval)(struct timeval *tv);
118     rt_err_t (*set_timeval)(struct timeval *tv);
119 };
120 
121 /**
122  * @brief RTC device structure
123  */
124 typedef struct rt_rtc_device
125 {
126     struct rt_device parent;
127     const struct rt_rtc_ops *ops;
128 } rt_rtc_dev_t;
129 
130 /**
131  * @brief Register a RTC device
132  *
133  * @param rtc RTC device
134  * @param name RTC device name
135  * @param flag RTC device flag
136  * @param data RTC device data
137  * @return rt_err_t error code
138  */
139 rt_err_t rt_hw_rtc_register(rt_rtc_dev_t  *rtc,
140                             const char    *name,
141                             rt_uint32_t    flag,
142                             void          *data);
143 
144 /**
145  * @brief set date
146  *
147  * @param year year
148  * @param month month
149  * @param day day
150  * @return rt_err_t error code
151  */
152 rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day);
153 
154 /**
155  * @brief set time
156  *
157  * @param hour hour
158  * @param minute minute
159  * @param second second
160  * @return rt_err_t error code
161 */
162 rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second);
163 
164 /**
165  * @brief set timestamp
166  *
167  * @param timestamp A pointer to time
168  * @return rt_err_t error code
169  */
170 rt_err_t set_timestamp(time_t timestamp);
171 
172 /**
173  * @brief get timestamp
174  *
175  * @param timestamp A secondary pointer to time
176  * @return rt_err_t error code
177  */
178 rt_err_t get_timestamp(time_t *timestamp);
179 
180 #ifdef RT_USING_SYSTEM_WORKQUEUE
181 rt_err_t rt_soft_rtc_sync(void);
182 rt_err_t rt_soft_rtc_set_source(const char *name);
183 #endif
184 
185 #ifdef __cplusplus
186 }
187 #endif
188 
189 /*! @}*/
190 
191 #endif /* __DEV_RTC_H__ */
192