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