1  /*
2  * Copyright (C) 2017-2024 Alibaba Group Holding Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 /******************************************************************************
20  * @file       drv/rtc.h
21  * @brief      Header File for RTC Driver
22  * @version    V1.0
23  * @date       9. Oct 2020
24  * @model      rtc
25  ******************************************************************************/
26 
27 #ifndef _DRV_RTC_H_
28 #define _DRV_RTC_H_
29 
30 #include <drv/common.h>
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 /****** RTC time ******/
37 typedef struct {
38     int tm_sec;             ///< Second.      [0-59]
39     int tm_min;             ///< Minute.      [0-59]
40     int tm_hour;            ///< Hour.        [0-23]
41     int tm_mday;            ///< Day.         [1-31]
42     int tm_mon;             ///< Month.       [0-11]
43     int tm_year;            ///< Year-1900.   [70- ]      !NOTE:Set 100 mean 2000
44     int tm_wday;            ///< Day of week. [0-6 ]      !NOTE:Set 0 mean Sunday
45     int tm_yday;            ///< Days in year.[0-365]     !NOTE:Set 0 mean January 1st
46 } csi_rtc_time_t;
47 
48 /****** definition for RTC ******/
49 typedef struct csi_rtc csi_rtc_t;
50 
51 struct csi_rtc {
52     csi_dev_t           dev;
53     void (*callback)(csi_rtc_t *rtc, void *arg);
54     void               *arg;
55     void               *priv;
56 };
57 
58 /**
59   \brief       Initialize RTC interface. Initializes the resources needed for the RTC interface
60   \param[in]   rtc    Handle to operate
61   \param[in]   idx    RTC index
62   \return      Error code \ref csi_error_t
63 */
64 csi_error_t csi_rtc_init(csi_rtc_t *rtc, uint32_t idx);
65 
66 /**
67   \brief       De-initialize RTC interface. Stops operation and releases the software resources used by the interface
68   \param[in]   rtc    Handle to operate
69   \return      None
70 */
71 void csi_rtc_uninit(csi_rtc_t *rtc);
72 
73 /**
74   \brief       Set system date and wait for synchro
75   \param[in]   rtc        Handle to operate
76   \param[in]   rtctime    Pointer to RTC time
77   \return      Error code \ref csi_error_t
78 */
79 csi_error_t csi_rtc_set_time(csi_rtc_t *rtc, const csi_rtc_time_t *rtctime);
80 
81 /**
82   \brief       Set system date but no wait
83   \param[in]   rtc        Handle to operate
84   \param[in]   rtctime    Pointer to RTC time
85   \return      Error code \ref csi_error_t
86 */
87 csi_error_t csi_rtc_set_time_no_wait(csi_rtc_t *rtc, const csi_rtc_time_t *rtctime);
88 
89 /**
90   \brief       Get system date
91   \param[in]   rtc        Handle to operate
92   \param[out]  rtctime    Pointer to RTC time
93   \return      Error code \ref csi_error_t
94 */
95 csi_error_t csi_rtc_get_time(csi_rtc_t *rtc, csi_rtc_time_t *rtctime);
96 
97 /**
98   \brief       Get alarm remaining time
99   \param[in]   rtc    Handle to operate
100   \return      The remaining time(s)
101 */
102 uint32_t csi_rtc_get_alarm_remaining_time(csi_rtc_t *rtc);
103 
104 /**
105   \brief       Config RTC alarm timer
106   \param[in]   rtc         Handle to operate
107   \param[in]   rtctime     Time to wake up
108   \param[in]   callback    Callback function
109   \param[in]   arg         Callback's param
110   \return      Error code \ref csi_error_t
111 */
112 csi_error_t csi_rtc_set_alarm(csi_rtc_t *rtc, const csi_rtc_time_t *rtctime, void *callback, void *arg);
113 
114 /**
115   \brief       Cancel the RTC alarm
116   \param[in]   rtc    Handle to operate
117   \return      Error code \ref csi_error_t
118 */
119 csi_error_t csi_rtc_cancel_alarm(csi_rtc_t *rtc);
120 
121 /**
122   \brief       Judge RTC is running
123   \param[in]   rtc    Handle to operate
124   \return
125                true  - RTC is running
126                false - RTC is not running
127 */
128 bool csi_rtc_is_running(csi_rtc_t *rtc);
129 
130 /**
131   \brief       Enable RTC power manage
132   \param[in]   rtc    Handle to operate
133   \return      Error code \ref csi_error_t
134 */
135 csi_error_t csi_rtc_enable_pm(csi_rtc_t *rtc);
136 
137 /**
138   \brief       Disable RTC power manage
139   \param[in]   rtc    Handle to operate
140   \return      None
141 */
142 void csi_rtc_disable_pm(csi_rtc_t *rtc);
143 
144 #ifdef __cplusplus
145 }
146 #endif
147 
148 #endif /* _DRV_RTC_H_ */
149