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  * 2023-03-13   wcx1024979076   the first version
9  */
10 
11 #include <rtdevice.h>
12 #include "board.h"
13 #include "drv_rtc.h"
14 
15 #ifdef BSP_USING_RTC
16 
17 #define DBG_TAG "DRV.RTC"
18 #define DBG_LVL DBG_WARNING
19 #include <rtdbg.h>
20 
21 static struct rt_device rtc;
22 static rt_uint32_t rtc_time;
23 
_rtc_open(rt_device_t dev,rt_uint16_t oflag)24 static rt_err_t _rtc_open(rt_device_t dev, rt_uint16_t oflag)
25 {
26     if (dev->rx_indicate != RT_NULL)
27     {
28         /* Open Interrupt */
29     }
30 
31     return RT_EOK;
32 }
33 
_rtc_read(rt_device_t dev,rt_off_t pos,void * buffer,rt_size_t size)34 static rt_ssize_t _rtc_read(
35     rt_device_t     dev,
36     rt_off_t        pos,
37     void*           buffer,
38     rt_size_t       size)
39 {
40     return 0;
41 }
42 
_rtc_control(rt_device_t dev,int cmd,void * args)43 static rt_err_t _rtc_control(rt_device_t dev, int cmd, void *args)
44 {
45     RT_ASSERT(dev != RT_NULL);
46     struct bflb_device_s* bflb_rtc = bflb_device_get_by_name("rtc");
47     switch (cmd)
48     {
49     case RT_DEVICE_CTRL_RTC_GET_TIME:
50         *(rt_uint32_t *)args = rtc_time + BFLB_RTC_TIME2SEC(bflb_rtc_get_time(bflb_rtc));
51         break;
52 
53     case RT_DEVICE_CTRL_RTC_SET_TIME:
54         rtc_time = *(rt_uint32_t *)args;
55         bflb_rtc_set_time(bflb_rtc, 0);
56         break;
57     }
58 
59     return RT_EOK;
60 }
61 
rt_hw_rtc_init(void)62 int rt_hw_rtc_init(void)
63 {
64     int result = RT_EOK;
65 
66     struct bflb_device_s* bflb_rtc = bflb_device_get_by_name("rtc");
67     bflb_rtc_set_time(bflb_rtc, 0);
68     /* register rtc device */
69     rtc.type        = RT_Device_Class_RTC;
70     rtc.rx_indicate = RT_NULL;
71     rtc.tx_complete = RT_NULL;
72     rtc.init        = RT_NULL;
73     rtc.open        = _rtc_open;
74     rtc.close       = RT_NULL;
75     rtc.read        = _rtc_read;
76     rtc.write       = RT_NULL;
77     rtc.control     = _rtc_control;
78     rtc.user_data   = RT_NULL; /* no private */
79 
80     result = rt_device_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
81     if(result != RT_EOK)
82     {
83         LOG_E("rtc device register fail.");
84     }
85 
86     return result;
87 }
88 
89 INIT_DEVICE_EXPORT(rt_hw_rtc_init);
90 #endif /* BSP_USING_RTC */
91