Skip to content

Commit 70217ef

Browse files
committed
[bsp][gd32103c-eval] Add internal rtc driver
1 parent 7b28f2b commit 70217ef

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed

bsp/gd32103c-eval/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,10 @@ menu "On-chip Peripheral Drivers"
100100
bool "Enable Watchdog Timer"
101101
select RT_USING_WDT
102102
default n
103+
104+
config BSP_USING_RTC
105+
bool "using internal rtc"
106+
default n
107+
select RT_USING_RTC
108+
103109
endmenu

bsp/gd32103c-eval/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ msh />
4848
| GPIO | 支持 | GPIOA~G |
4949
| ADC | 支持 | ADC0~1 |
5050
| HWTIMER | 支持 | TIMER0~7 |
51+
| RTC | 支持 | RTC |
5152
| WDT | 支持 | Free watchdog timer |
5253
| IIC | 未支持 | I2C0~1 |
5354
| SPI | 未支持 | SPI0~2 |

bsp/gd32103c-eval/drivers/SConscript

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ if GetDepend('RT_USING_ADC'):
2424
if GetDepend('RT_USING_HWTIMER'):
2525
src += ['drv_hwtimer.c']
2626

27+
if GetDepend('RT_USING_RTC'):
28+
src += ['drv_rtc.c']
29+
2730
if GetDepend('RT_USING_WDT'):
2831
src += ['drv_iwdt.c']
2932

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright (c) 2006-2021, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2021-02-20 iysheng first version
9+
*/
10+
11+
#include <board.h>
12+
#include <sys/time.h>
13+
#include <drivers/drv_comm.h>
14+
15+
#define DBG_TAG "drv.rtc"
16+
#define DBG_LVL DBG_INFO
17+
18+
#include <rtdbg.h>
19+
20+
#ifdef RT_USING_RTC
21+
22+
typedef struct {
23+
struct rt_device rtc_dev;
24+
} gd32_rtc_device;
25+
26+
static gd32_rtc_device g_gd32_rtc_dev;
27+
28+
static time_t get_rtc_timestamp(void)
29+
{
30+
time_t rtc_counter;
31+
32+
rtc_counter = (time_t)RTC_GetCounter();
33+
34+
return rtc_counter;
35+
}
36+
37+
static rt_err_t set_rtc_timestamp(time_t time_stamp)
38+
{
39+
uint32_t rtc_counter;
40+
41+
rtc_counter = (uint32_t)time_stamp;
42+
43+
/* wait until LWOFF bit in RTC_CTL to 1 */
44+
RTC_WaitLWOFF();
45+
/* enter configure mode */
46+
RTC_EnterConfigMode();
47+
/* write data to rtc register */
48+
RTC_SetCounter(rtc_counter);
49+
/* exit configure mode */
50+
RTC_ExitConfigMode();
51+
/* wait until LWOFF bit in RTC_CTL to 1 */
52+
RTC_WaitLWOFF();
53+
54+
return RT_EOK;
55+
}
56+
57+
static rt_err_t rt_gd32_rtc_control(rt_device_t dev, int cmd, void *args)
58+
{
59+
rt_err_t result = RT_EOK;
60+
61+
RT_ASSERT(dev != RT_NULL);
62+
switch (cmd)
63+
{
64+
case RT_DEVICE_CTRL_RTC_GET_TIME:
65+
*(rt_uint32_t *)args = get_rtc_timestamp();
66+
break;
67+
68+
case RT_DEVICE_CTRL_RTC_SET_TIME:
69+
if (set_rtc_timestamp(*(rt_uint32_t *)args))
70+
{
71+
result = -RT_ERROR;
72+
}
73+
break;
74+
}
75+
76+
return result;
77+
}
78+
79+
#ifdef RT_USING_DEVICE_OPS
80+
const static struct rt_device_ops g_gd32_rtc_ops =
81+
{
82+
RT_NULL,
83+
RT_NULL,
84+
RT_NULL,
85+
RT_NULL,
86+
RT_NULL,
87+
rt_gd32_rtc_control
88+
};
89+
#endif
90+
91+
static int rt_hw_rtc_init(void)
92+
{
93+
rt_err_t ret;
94+
time_t rtc_counter;
95+
96+
rcu_periph_clock_enable(RCU_PMU);
97+
PWR_BackupAccess_Enable(ENABLE);
98+
rcu_periph_clock_enable(RCU_BKPI);
99+
100+
rtc_counter = get_rtc_timestamp();
101+
/* once the rtc clock source has been selected, if can't be changed
102+
* anymore unless the Backup domain is reset */
103+
rcu_bkp_reset_enable();
104+
rcu_bkp_reset_disable();
105+
rcu_periph_clock_enable(RCU_RTC);
106+
rcu_osci_on(RCU_LXTAL);
107+
if (SUCCESS == rcu_osci_stab_wait(RCU_LXTAL))
108+
{
109+
/* set lxtal as rtc clock source */
110+
rcu_rtc_clock_config(RCU_RTCSRC_LXTAL);
111+
}
112+
set_rtc_timestamp(rtc_counter);
113+
114+
#ifdef RT_USING_DEVICE_OPS
115+
g_gd32_rtc_dev.rtc_dev.ops = &g_gd32_rtc_ops;
116+
#else
117+
g_gd32_rtc_dev.rtc_dev.init = RT_NULL;
118+
g_gd32_rtc_dev.rtc_dev.open = RT_NULL;
119+
g_gd32_rtc_dev.rtc_dev.close = RT_NULL;
120+
g_gd32_rtc_dev.rtc_dev.read = RT_NULL;
121+
g_gd32_rtc_dev.rtc_dev.write = RT_NULL;
122+
g_gd32_rtc_dev.rtc_dev.control = rt_gd32_rtc_control;
123+
#endif
124+
g_gd32_rtc_dev.rtc_dev.type = RT_Device_Class_RTC;
125+
g_gd32_rtc_dev.rtc_dev.rx_indicate = RT_NULL;
126+
g_gd32_rtc_dev.rtc_dev.tx_complete = RT_NULL;
127+
g_gd32_rtc_dev.rtc_dev.user_data = RT_NULL;
128+
129+
ret = rt_device_register(&g_gd32_rtc_dev.rtc_dev, "rtc", \
130+
RT_DEVICE_FLAG_RDWR);
131+
if (ret != RT_EOK)
132+
{
133+
LOG_E("failed register internal rtc device, err=%d", ret);
134+
}
135+
136+
return ret;
137+
}
138+
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
139+
#endif

0 commit comments

Comments
 (0)