summaryrefslogtreecommitdiff
path: root/bsp/xuantie/libraries/xuantie_libraries/chip_riscv_dummy/src/drivers/rtc.c
blob: f3f7083447fc6275de18cd2a820cbe0587a04bf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 *    Copyright (C) 2017-2024 Alibaba Group Holding Limited
 */

/*******************************************************
 * @file       rtc.c
 * @brief      source file for rtc csi driver
 * @version    V1.0
 * @date       23. Sep 2020
 * ******************************************************/

#include <drv/rtc.h>
#include <drv/irq.h>
#include <time.h>

#define NORMAL_RTC_FREQ         (32768U)
#define REAL_RTC_FREQ           (soc_get_rtc_freq(0U))
#define RTC_WAIT_WRITED_10S     (10000U)

#define RTC_WAIT_IS_TIMEOUT(_time_ms_, _result_)      {        \
        do {                                                      \
            if (_time_ms_ >= RTC_WAIT_WRITED_10S) {            \
                _result_ = -1;                                    \
            }                                                     \
            mdelay(1U);                                           \
        } while(0);                                               \
    }

static uint32_t load_time = 0U;
static uint32_t int_number = 0U;
static uint32_t count = 0U;

extern int32_t clock_check_tm_ok(const struct tm *rtctime);
extern int32_t clock_update_basetime(const struct tm *rtctime);
extern time_t mktime(struct tm *tp);

/**
  \brief       Internal timeout interrupt process function
  \param[in]   rtc    handle rtc handle to operate
  \return      None
*/
void qx_rtc_irq_handler(void *arg)
{
    csi_rtc_t *rtc = (csi_rtc_t *)arg;
    uint32_t timecount = 0, ret = 0, intr_status = 0;

    ///< TODO:获取RTC中断的状态到intr_status

    do {
        ///< TODO:清除RTC的中断
        RTC_WAIT_IS_TIMEOUT(++timecount, ret);

        if (ret < 0) {
            break;
        }
    } while (intr_status);

    count++;

    if (count >= (int_number * (REAL_RTC_FREQ / NORMAL_RTC_FREQ))) {
        count = 0U;

        if (rtc->callback) {
            rtc->callback(rtc, rtc->arg);
        }
    }

}
/**
  \brief       Initialize RTC Interface. Initializes the resources needed for the RTC interface
  \param[in]   rtc    rtc handle to operate
  \param[in]   idx    rtc index
  \return      error code \ref csi_error_t
*/
csi_error_t csi_rtc_init(csi_rtc_t *rtc, uint32_t idx)
{
    CSI_PARAM_CHK(rtc, CSI_ERROR);
    csi_error_t ret = CSI_OK;

    ///< 获取中断号、基地址等相关信息
    if (0 == target_get(DEV_QX_RTC_TAG, idx, &rtc->dev)) {
        ///< TODO:相关寄存器复位清零
    } else {
        ret = CSI_ERROR;
    }

    return ret;
}
/**
  \brief       De-initialize RTC Interface. stops operation and releases the software resources used by the interface
  \param[in]   rtc    rtc handle to operate
  \return      None
*/
void csi_rtc_uninit(csi_rtc_t *rtc)
{
    CSI_PARAM_CHK_NORETVAL(rtc);

    ///< TODO:相关寄存器复位清零
}

/**
  \brief       Set system date
  \param[in]   rtc        handle rtc handle to operate
  \param[in]   rtctime    pointer to rtc time
  \return      error code \ref csi_error_t
*/
csi_error_t csi_rtc_set_time(csi_rtc_t *rtc, const csi_rtc_time_t *rtctime)
{
    CSI_PARAM_CHK(rtc, CSI_ERROR);
    CSI_PARAM_CHK(rtctime, CSI_ERROR);
    csi_error_t ret = CSI_OK;
    int32_t load_sec;

    do {
        ret = (csi_error_t)clock_check_tm_ok((const struct tm *)rtctime);

        if (ret < CSI_OK) {
            break;
        }

        load_sec = clock_update_basetime((const struct tm *)rtctime);

        if (load_sec <= 0) {        ///< Error returned when total seconds overflow is negative
            ret = CSI_ERROR;
            break;
        }

        load_time = (uint32_t)load_sec;
        ///< TODO:用load_sec设置RTC的加载值寄存器,该处操作有一段等待同步时间

        if (ret < CSI_OK) {
            break;
        }

        ///< TODO:使能计数器

    } while (0);

    return ret;
}
/**
  \brief       Set system date but no wait
  \param[in]   rtc        rtc handle to operate
  \param[in]   rtctime    pointer to rtc time
  \return      error code \ref csi_error_t
*/
csi_error_t csi_rtc_set_time_no_wait(csi_rtc_t *rtc, const csi_rtc_time_t *rtctime)
{
    CSI_PARAM_CHK(rtc, CSI_ERROR);
    CSI_PARAM_CHK(rtctime, CSI_ERROR);
    csi_error_t ret = CSI_OK;
    int32_t load_sec;

    do {

        ret = (csi_error_t)clock_check_tm_ok((const struct tm *)rtctime);

        if (ret < CSI_OK) {
            break;
        }

        load_sec = clock_update_basetime((const struct tm *)rtctime);

        if (load_sec <= 0) {        ///< Error returned when total seconds overflow is negative
            ret = CSI_ERROR;
            break;
        }

        ///< TODO:用load_sec设置RTC的加载值寄存器

        ///< TODO:使能计数器

    } while (0);

    return ret;
}
/**
  \brief       Get system date
  \param[in]   rtc        handle rtc handle to operate
  \param[out]  rtctime    pointer to rtc time
  \return      error code \ref csi_error_t
*/
csi_error_t csi_rtc_get_time(csi_rtc_t *rtc, csi_rtc_time_t *rtctime)
{
    uint32_t cur_time = 0;
    time_t time;

    CSI_PARAM_CHK(rtc, CSI_ERROR);
    CSI_PARAM_CHK(rtctime, CSI_ERROR);

    ///< TODO:获取当前时间到cur_time
    time = (time_t)(((cur_time - load_time) / (REAL_RTC_FREQ / NORMAL_RTC_FREQ)) + load_time);
    gmtime_r(&time, (struct tm *)rtctime);

    return CSI_OK;
}

/**
  \brief       Get alarm remaining time
  \param[in]   rtc    rtc handle to operate
  \return      the remaining time(s)
*/
uint32_t csi_rtc_get_alarm_remaining_time(csi_rtc_t *rtc)
{
    CSI_PARAM_CHK(rtc, 0U);

    ///< 返回RTC剩余多长时间产生闹钟中断
    return (int_number - (count / (REAL_RTC_FREQ / NORMAL_RTC_FREQ)));
}

/**
  \brief       Config RTC alarm ture timer
  \param[in]   rtc         handle rtc handle to operate
  \param[in]   rtctime     time(s) to wake up
  \param[in]   callback    callback function
  \param[in]   arg         callback's param
  \return      error code \ref csi_error_t
*/
csi_error_t csi_rtc_set_alarm(csi_rtc_t *rtc, const csi_rtc_time_t *rtctime, void *callback, void *arg)
{
    CSI_PARAM_CHK(rtc, CSI_ERROR);
    CSI_PARAM_CHK(rtctime, CSI_ERROR);
    csi_error_t ret = (csi_error_t)clock_check_tm_ok((const struct tm *)rtctime);
    csi_rtc_time_t current_time;

    if (CSI_OK == ret) {
        rtc->callback = callback;
        rtc->arg = arg;
        ///< 注册RTC的中断服务函数,使能中断控制器中对应的中断
        csi_irq_attach((uint32_t)rtc->dev.irq_num, &qx_rtc_irq_handler, &rtc->dev);
        csi_irq_enable((uint32_t)rtc->dev.irq_num);

        csi_rtc_get_time(rtc, &current_time);            ///< get current time
        count = 0U;
        int_number = ((uint32_t)mktime((struct tm *)rtctime) - (uint32_t)mktime((struct tm *)&current_time));
        ///< TODO:使能RTC的中断
    }

    return ret;
}

/**
  \brief       Cancel the rtc alarm
  \param[in]   rtc    rtc handle to operate
  \return      error code \ref csi_error_t
*/
csi_error_t csi_rtc_cancel_alarm(csi_rtc_t *rtc)
{
    CSI_PARAM_CHK(rtc, CSI_ERROR);

    rtc->callback = NULL;
    rtc->arg = NULL;

    ///< TODO:禁止中断

    ///< 关闭中断控制器中对应的使能,注销RTC的中断服务函数
    csi_irq_disable((uint32_t)rtc->dev.irq_num);
    csi_irq_detach((uint32_t)rtc->dev.irq_num);

    return CSI_OK;
}

/**
  \brief       Judge rtc is working
  \param[in]   rtc    handle rtc handle to operate
  \return      state of work
               ture - rtc is running
               false -rtc is not running
*/
bool csi_rtc_is_running(csi_rtc_t *rtc)
{
    CSI_PARAM_CHK(rtc, false);

    bool status = 0;

    ///< TODO:获取RTC运行的状态

    return status;
}

#ifdef CONFIG_PM
csi_error_t csi_rtc_enable_pm(csi_rtc_t *rtc)
{
    return CSI_UNSUPPORTED;
}

void csi_rtc_disable_pm(csi_rtc_t *rtc)
{
    return;
}
#endif