Skip to content

Commit 9d0f088

Browse files
添加HC32F460系列RTC外设驱动 (#6069)
* 添加HC32F460系列RTC外设驱动
1 parent 0827ca6 commit 9d0f088

File tree

3 files changed

+181
-1
lines changed

3 files changed

+181
-1
lines changed

bsp/hc32/ev_hc32f460_lqfp100_v2/board/Kconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ menu "On-chip Peripheral Drivers"
106106
bool "Enable RTC"
107107
select RT_USING_RTC
108108
default n
109-
110109
if BSP_USING_RTC
111110
choice
112111
prompt "Select clock source"

bsp/hc32/libraries/hc32_drivers/SConscript

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ if GetDepend(['RT_USING_ADC']):
2929

3030
if GetDepend(['RT_USING_CAN']):
3131
src += ['drv_can.c']
32+
33+
if GetDepend(['RT_USING_RTC']):
34+
src += ['drv_rtc.c']
3235

3336
path = [cwd]
3437

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* Copyright (c) 2006-2022, RT-Thread Development Team
3+
* Copyright (c) 2022, xiaoxiaolisunny
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* Change Logs:
8+
* Date Author Notes
9+
* 2022-06-10 xiaoxiaolisunny first version
10+
*/
11+
12+
#include <board.h>
13+
#include <rtdbg.h>
14+
#include <rtthread.h>
15+
#include <rtdevice.h>
16+
#include <sys/time.h>
17+
18+
#ifdef BSP_USING_RTC
19+
20+
static rt_rtc_dev_t hc32_rtc_dev;
21+
22+
static rt_err_t hc32_rtc_get_time_stamp(struct timeval *tv)
23+
{
24+
stc_rtc_time_t stcRtcTime = {0};
25+
stc_rtc_date_t stcRtcDate = {0};
26+
struct tm tm_new = {0};
27+
28+
RTC_GetTime(RTC_DATA_FMT_DEC, &stcRtcTime);
29+
RTC_GetDate(RTC_DATA_FMT_DEC, &stcRtcDate);
30+
31+
tm_new.tm_sec = stcRtcTime.u8Second;
32+
tm_new.tm_min = stcRtcTime.u8Minute;
33+
tm_new.tm_hour = stcRtcTime.u8Hour;
34+
if(stcRtcDate.u8Month == 0)
35+
{
36+
tm_new.tm_mday = stcRtcDate.u8Day + 1;
37+
tm_new.tm_mon = stcRtcDate.u8Month;
38+
}
39+
else
40+
{
41+
tm_new.tm_mday = stcRtcDate.u8Day ;
42+
tm_new.tm_mon = stcRtcDate.u8Month - 1;
43+
}
44+
tm_new.tm_year = stcRtcDate.u8Year + 100;
45+
46+
tv->tv_sec = timegm(&tm_new);
47+
48+
return RT_EOK;
49+
}
50+
51+
static rt_err_t hc32_rtc_set_time_stamp(time_t time_stamp)
52+
{
53+
stc_rtc_time_t stcRtcTime = {0};
54+
stc_rtc_date_t stcRtcDate = {0};
55+
struct tm p_tm = {0};
56+
57+
gmtime_r(&time_stamp, &p_tm);
58+
59+
if (p_tm.tm_year < 100)
60+
{
61+
return -RT_ERROR;
62+
}
63+
64+
stcRtcTime.u8Second = p_tm.tm_sec ;
65+
stcRtcTime.u8Minute = p_tm.tm_min ;
66+
stcRtcTime.u8Hour = p_tm.tm_hour;
67+
stcRtcDate.u8Day = p_tm.tm_mday;
68+
stcRtcDate.u8Month = p_tm.tm_mon + 1;
69+
stcRtcDate.u8Year = p_tm.tm_year - 100;
70+
stcRtcDate.u8Weekday = p_tm.tm_wday;
71+
72+
if (LL_OK != RTC_SetTime(RTC_DATA_FMT_DEC, &stcRtcTime))
73+
{
74+
return -RT_ERROR;
75+
}
76+
if (LL_OK != RTC_SetDate(RTC_DATA_FMT_DEC, &stcRtcDate))
77+
{
78+
return -RT_ERROR;
79+
}
80+
81+
LOG_D("set rtc time.");
82+
return RT_EOK;
83+
}
84+
85+
static rt_err_t hc32_rtc_init(void)
86+
{
87+
stc_rtc_init_t stcRtcInit;
88+
89+
#ifdef BSP_RTC_USING_XTAL32
90+
stc_clock_xtal32_init_t stcXtal32Init;
91+
/* Xtal32 config */
92+
stcXtal32Init.u8State = CLK_XTAL32_ON;
93+
stcXtal32Init.u8Drv = CLK_XTAL32_DRV_HIGH;
94+
stcXtal32Init.u8Filter = CLK_XTAL32_FILTER_RUN_MD;
95+
(void)CLK_Xtal32Init(&stcXtal32Init);
96+
/* Waiting for XTAL32 stabilization */
97+
rt_thread_delay(100);
98+
#endif
99+
100+
/* RTC stopped */
101+
if (DISABLE == RTC_GetCounterState())
102+
{
103+
/* Reset RTC counter */
104+
if (LL_ERR_TIMEOUT == RTC_DeInit())
105+
{
106+
return -RT_ERROR;
107+
}
108+
else
109+
{
110+
/* Configure structure initialization */
111+
(void)RTC_StructInit(&stcRtcInit);
112+
/* Configuration RTC structure */
113+
#ifdef BSP_RTC_USING_XTAL32
114+
stcRtcInit.u8ClockSrc = RTC_CLK_SRC_XTAL32;
115+
#else
116+
stcRtcInit.u8ClockSrc = RTC_CLK_SRC_LRC;
117+
#endif
118+
stcRtcInit.u8HourFormat = RTC_HOUR_FMT_24H;
119+
(void)RTC_Init(&stcRtcInit);
120+
/* Startup RTC count */
121+
RTC_Cmd(ENABLE);
122+
}
123+
}
124+
return RT_EOK;
125+
}
126+
127+
static rt_err_t hc32_rtc_get_secs(time_t *sec)
128+
{
129+
struct timeval tv;
130+
131+
hc32_rtc_get_time_stamp(&tv);
132+
*(time_t *) sec = tv.tv_sec;
133+
LOG_D("RTC: get rtc_time %d", *sec);
134+
135+
return RT_EOK;
136+
}
137+
138+
static rt_err_t hc32_rtc_set_secs(time_t *sec)
139+
{
140+
rt_err_t result = RT_EOK;
141+
142+
if (hc32_rtc_set_time_stamp(*sec))
143+
{
144+
result = -RT_ERROR;
145+
}
146+
LOG_D("RTC: set rtc_time %d", *sec);
147+
148+
return result;
149+
}
150+
151+
const static struct rt_rtc_ops hc32_rtc_ops =
152+
{
153+
hc32_rtc_init,
154+
hc32_rtc_get_secs,
155+
hc32_rtc_set_secs,
156+
RT_NULL,
157+
RT_NULL,
158+
hc32_rtc_get_time_stamp,
159+
RT_NULL
160+
};
161+
162+
int rt_hw_rtc_init(void)
163+
{
164+
rt_err_t result;
165+
166+
hc32_rtc_dev.ops = &hc32_rtc_ops;
167+
result = rt_hw_rtc_register(&hc32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL);
168+
if (result != RT_EOK)
169+
{
170+
LOG_E("rtc register err code: %d", result);
171+
return result;
172+
}
173+
LOG_D("rtc init success");
174+
return RT_EOK;
175+
}
176+
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
177+
178+
#endif /* BSP_USING_RTC */

0 commit comments

Comments
 (0)