Skip to content

Commit e58c7bb

Browse files
committed
bsp: ls2k: internal rtc driver
hardware date format year, the same as ctime month 1 ~ 12 no leap second Signed-off-by: Du Huanpeng <[email protected]>
1 parent d7d6dbe commit e58c7bb

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed

bsp/ls2kdev/drivers/drv_rtc.c

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
* Copyright (c) 2020, Du Huanpeng <[email protected]>
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* Change Logs:
8+
* Date Author Notes
9+
* 2018-01-30 armink the first version
10+
* 2020-06-23 Du Huanpeng based on components/drivers/rtc/soft_rtc.c
11+
*/
12+
13+
14+
#include <rthw.h>
15+
#include <rtthread.h>
16+
#include <rtdevice.h>
17+
#include <rtthread.h>
18+
#include "ls2k1000.h"
19+
20+
struct loongson_rtc {
21+
rt_uint32_t sys_toytrim;
22+
rt_uint32_t sys_toywrite0;
23+
rt_uint32_t sys_toywrite1;
24+
rt_uint32_t sys_toyread0;
25+
rt_uint32_t sys_toyread1;
26+
rt_uint32_t sys_toymatch0;
27+
rt_uint32_t sys_toymatch1;
28+
rt_uint32_t sys_toymatch2;
29+
rt_uint32_t sys_rtcctrl;
30+
rt_uint32_t __pad4[3];
31+
rt_uint32_t __pad5[4];
32+
rt_uint32_t sys_rtctrim;
33+
rt_uint32_t sys_rtcwrite0;
34+
rt_uint32_t sys_rtcread0;
35+
rt_uint32_t sys_rtcmatch0;
36+
rt_uint32_t sys_rtcmatch1;
37+
rt_uint32_t sys_rtcmatch2;
38+
};
39+
40+
/* bit field helpers. */
41+
#define __M(n) (~(~0<<(n)))
42+
#define __RBF(number, n) ((number)&__M(n))
43+
#define __BF(number, n, m) __RBF((number>>m), (n-m+1))
44+
#define BF(number, n, m) (m<n ? __BF(number, n, m) : __BF(number, m, n))
45+
46+
struct rtctime {
47+
rt_uint32_t sys_toyread0;
48+
rt_uint32_t sys_toyread1;
49+
rt_uint32_t sys_rtcread0;
50+
};
51+
typedef struct rtctime rtctime_t;
52+
53+
struct tm *localrtctime(const rtctime_t *rtctp)
54+
{
55+
static struct tm time;
56+
int msec;
57+
58+
msec = BF(rtctp->sys_toyread0, 3, 0);
59+
msec *= 100;
60+
61+
time.tm_sec = BF(rtctp->sys_toyread0, 9, 4);
62+
time.tm_min = BF(rtctp->sys_toyread0, 15, 10);
63+
time.tm_hour = BF(rtctp->sys_toyread0, 20, 16);
64+
time.tm_mday = BF(rtctp->sys_toyread0, 21, 25);
65+
time.tm_mon = BF(rtctp->sys_toyread0, 26, 31);
66+
/* struct tm has three more members:
67+
time.tm_isdst
68+
time.tm_wday
69+
time.tm_yday
70+
*/
71+
time.tm_mon -= 1;
72+
time.tm_year = rtctp->sys_toyread1;
73+
return &time;
74+
}
75+
76+
rtctime_t mkrtctime(struct tm *tm)
77+
{
78+
rtctime_t rtctm;
79+
struct tm tmptime;
80+
81+
rtctm.sys_toyread0 <<= 31 - 26 + 1;
82+
rtctm.sys_toyread0 |= tm->tm_mon + 1;
83+
rtctm.sys_toyread0 <<= 25 - 21 + 1;
84+
rtctm.sys_toyread0 |= tm->tm_mday;
85+
rtctm.sys_toyread0 <<= 20 - 16 + 1;
86+
rtctm.sys_toyread0 |= tm->tm_hour;
87+
rtctm.sys_toyread0 <<= 15 - 10 + 1;
88+
rtctm.sys_toyread0 |= tm->tm_min;
89+
rtctm.sys_toyread0 <<= 9 - 4 + 1;
90+
rtctm.sys_toyread0 |= tm->tm_sec;
91+
/* Fixme: 0.1 second */
92+
rtctm.sys_toyread0 <<= 3 - 0 + 1;
93+
rtctm.sys_toyread0 |= 0;
94+
95+
rtctm.sys_toyread1 = tm->tm_year;
96+
97+
tmptime = *localrtctime(&rtctm);
98+
99+
return rtctm;
100+
}
101+
102+
static rt_err_t rt_rtc_open(rt_device_t dev, rt_uint16_t oflag)
103+
{
104+
return RT_EOK;
105+
}
106+
107+
static rt_size_t rt_rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
108+
{
109+
return 0;
110+
}
111+
112+
static rt_err_t rt_rtc_ioctl(rt_device_t dev, int cmd, void *args)
113+
{
114+
rt_err_t err = RT_ENOSYS;
115+
116+
static int count = 0;
117+
118+
struct loongson_rtc *hw_rtc;
119+
rtctime_t rtctm;
120+
struct tm time;
121+
struct tm tmptime;
122+
time_t *t;
123+
124+
hw_rtc = dev->user_data;
125+
126+
t = (time_t *)args;
127+
time = *localtime(t);
128+
129+
rtctm.sys_toyread0 = hw_rtc->sys_toyread0;
130+
rtctm.sys_toyread1 = hw_rtc->sys_toyread1;
131+
rtctm.sys_rtcread0 = hw_rtc->sys_rtcread0;
132+
tmptime = *localrtctime(&rtctm);
133+
134+
switch (cmd) {
135+
case RT_DEVICE_CTRL_RTC_GET_TIME:
136+
*t = mktime(&tmptime);
137+
break;
138+
case RT_DEVICE_CTRL_RTC_SET_TIME:
139+
tmptime.tm_hour = time.tm_hour;
140+
tmptime.tm_min = time.tm_min;
141+
tmptime.tm_sec = time.tm_sec;
142+
143+
tmptime.tm_year = time.tm_year;
144+
tmptime.tm_mon = time.tm_mon;
145+
tmptime.tm_mday = time.tm_mday;
146+
147+
rtctm = mkrtctime(&tmptime);
148+
/* write to hw RTC */
149+
hw_rtc->sys_toywrite0 = rtctm.sys_toyread0;
150+
hw_rtc->sys_toywrite1 = rtctm.sys_toyread1;
151+
break;
152+
case RT_DEVICE_CTRL_RTC_GET_ALARM:
153+
break;
154+
case RT_DEVICE_CTRL_RTC_SET_ALARM:
155+
break;
156+
default:
157+
break;
158+
}
159+
160+
return RT_EOK;
161+
}
162+
163+
int rt_hw_rtc_init(void)
164+
{
165+
static struct rt_device rtc = {
166+
.type = RT_Device_Class_RTC,
167+
.init = RT_NULL,
168+
.open = rt_rtc_open,
169+
.close = RT_NULL,
170+
.read = rt_rtc_read,
171+
.write = RT_NULL,
172+
.control = rt_rtc_ioctl,
173+
.user_data = (void *)RTC_BASE,
174+
};
175+
rt_device_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
176+
}
177+
178+
INIT_DEVICE_EXPORT(rt_hw_rtc_init);

bsp/ls2kdev/drivers/ls2k1000.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#define GPIO_BASE 0xFFFFFFFFBFE10500
1111
#define PLL_SYS_BASE 0xFFFFFFFFBFE10480
12+
#define RTC_BASE 0xFFFFFFFFBFE07820
1213

1314
void rt_hw_timer_handler(void);
1415
void rt_hw_uart_init(void);

0 commit comments

Comments
 (0)