1 /*
2 * Copyright (c) 2006-2018, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2021-03-15 picospuch Porting for maxiam32660
9 */
10
11 #include "drivers/dev_rtc.h"
12 #include "board.h"
13 #include <sys/time.h>
14
15 #ifdef BSP_USING_ONCHIP_RTC
16
17 #define DBG_LEVEL DBG_INFO
18 #include <rtdbg.h>
19 #define LOG_TAG "drv.rtc"
20
21 static struct rt_device rtc;
22
23 static sys_cfg_rtc_t sys_cfg;
24
get_rtc_timestamp(void)25 static time_t get_rtc_timestamp(void)
26 {
27 LOG_D("get rtc time.");
28 return RTC_GetSecond();
29 }
30
set_rtc_time_stamp(time_t time_stamp)31 static rt_err_t set_rtc_time_stamp(time_t time_stamp)
32 {
33 LOG_D("set rtc time.");
34
35 if (RTC_Init(MXC_RTC, time_stamp, 0, &sys_cfg) != E_SUCCESS) {
36 return -RT_ERROR;
37 }
38
39 if (RTC_EnableRTCE(MXC_RTC) != E_SUCCESS) {
40 return -RT_ERROR;
41 }
42
43 return RT_EOK;
44 }
45
rt_rtc_init(void)46 static void rt_rtc_init(void)
47 {
48 sys_cfg.tmr = MXC_TMR0;
49 RTC_Init(MXC_RTC, 0, 0, &sys_cfg);
50 }
51
rt_rtc_config(struct rt_device * dev)52 static rt_err_t rt_rtc_config(struct rt_device *dev)
53 {
54 if (RTC_EnableRTCE(MXC_RTC) != E_SUCCESS) {
55 return -RT_ERROR;
56 }
57 return RT_EOK;
58 }
59
rt_rtc_control(rt_device_t dev,int cmd,void * args)60 static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
61 {
62 rt_err_t result = RT_EOK;
63 RT_ASSERT(dev != RT_NULL);
64 switch (cmd)
65 {
66 case RT_DEVICE_CTRL_RTC_GET_TIME:
67 *(rt_uint32_t *)args = get_rtc_timestamp();
68 LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)args);
69 break;
70
71 case RT_DEVICE_CTRL_RTC_SET_TIME:
72 if (set_rtc_time_stamp(*(rt_uint32_t *)args))
73 {
74 result = -RT_ERROR;
75 }
76 LOG_D("RTC: set rtc_time %x\n", *(rt_uint32_t *)args);
77 break;
78 }
79
80 return result;
81 }
82
83 #ifdef RT_USING_DEVICE_OPS
84 const static struct rt_device_ops rtc_ops =
85 {
86 RT_NULL,
87 RT_NULL,
88 RT_NULL,
89 RT_NULL,
90 RT_NULL,
91 rt_rtc_control
92 };
93 #endif
94
rt_hw_rtc_register(rt_device_t device,const char * name,rt_uint32_t flag)95 static rt_err_t rt_hw_rtc_register(rt_device_t device, const char *name, rt_uint32_t flag)
96 {
97 RT_ASSERT(device != RT_NULL);
98
99 rt_rtc_init();
100 if (rt_rtc_config(device) != RT_EOK)
101 {
102 return -RT_ERROR;
103 }
104 #ifdef RT_USING_DEVICE_OPS
105 device->ops = &rtc_ops;
106 #else
107 device->init = RT_NULL;
108 device->open = RT_NULL;
109 device->close = RT_NULL;
110 device->read = RT_NULL;
111 device->write = RT_NULL;
112 device->control = rt_rtc_control;
113 #endif
114 device->type = RT_Device_Class_RTC;
115 device->rx_indicate = RT_NULL;
116 device->tx_complete = RT_NULL;
117 device->user_data = RT_NULL;
118
119 /* register a character device */
120 return rt_device_register(device, name, flag);
121 }
122
rt_hw_rtc_init(void)123 int rt_hw_rtc_init(void)
124 {
125 rt_err_t result;
126 result = rt_hw_rtc_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
127 if (result != RT_EOK)
128 {
129 LOG_E("rtc register err code: %d", result);
130 return result;
131 }
132 LOG_D("rtc init success");
133 return RT_EOK;
134 }
135 INIT_DEVICE_EXPORT(rt_hw_rtc_init);
136
137 #endif /* BSP_USING_ONCHIP_RTC */
138