Skip to content

Commit 762cdcc

Browse files
authored
Merge pull request #2532 from wangyq2018/es32f0334
[bsp/es32f0334] add rtc driver
2 parents 8d5c456 + f3e4da9 commit 762cdcc

File tree

9 files changed

+198
-6
lines changed

9 files changed

+198
-6
lines changed

bsp/es32f0334/.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@ CONFIG_BSP_USING_UART1=y
352352
# CONFIG_BSP_USING_HWTIMER2 is not set
353353
# CONFIG_BSP_USING_HWTIMER3 is not set
354354

355+
#
356+
# RTC Drivers
357+
#
358+
# CONFIG_BSP_USING_RTC is not set
359+
355360
#
356361
# Onboard Peripheral Drivers
357362
#

bsp/es32f0334/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ ES-PDS-ES32F0334-V1.1
4343
| I2C | 支持 | I2C0/1 |
4444
| PWM | 支持 | PWM0/1/2/3 |
4545
| TIMER | 支持 | TIMER0/1/2/3 |
46+
| RTC | 支持 | RTC |
4647

4748
更多详细信息请咨询[上海东软载波微电子技术支持](http://www.essemi.com/)
4849

bsp/es32f0334/drivers/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ menu "Hardware Drivers Config"
8888
default n
8989
endmenu
9090

91+
menu "RTC Drivers"
92+
config BSP_USING_RTC
93+
bool "Using RTC"
94+
select RT_USING_RTC
95+
default n
96+
endmenu
97+
9198
endmenu
9299

93100
menu "Onboard Peripheral Drivers"

bsp/es32f0334/drivers/SConscript

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ if GetDepend('BSP_USING_PWM0') or GetDepend('BSP_USING_PWM1') or GetDepend('BSP_
3535
if GetDepend('BSP_USING_HWTIMER0') or GetDepend('BSP_USING_HWTIMER1') or GetDepend('BSP_USING_HWTIMER2') or GetDepend('BSP_USING_HWTIMER3'):
3636
src += ['drv_hwtimer.c']
3737

38+
# add rtc driver code
39+
if GetDepend(['BSP_USING_RTC']):
40+
src += ['drv_rtc.c']
41+
3842
CPPPATH = [cwd]
3943
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = CPPPATH)
4044

bsp/es32f0334/drivers/drv_rtc.c

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
* Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2019-04-01 wangyq the first version
9+
*/
10+
11+
#include <rthw.h>
12+
#include <rtthread.h>
13+
#include <rtdevice.h>
14+
#include <string.h>
15+
#include "board.h"
16+
#include "drv_rtc.h"
17+
#include <ald_cmu.h>
18+
#include <ald_rtc.h>
19+
20+
#ifdef RT_USING_RTC
21+
22+
static void rtc_init(rtc_init_t *init)
23+
{
24+
assert_param(IS_RTC_HOUR_FORMAT(init->hour_format));
25+
assert_param(IS_RTC_OUTPUT_SEL(init->output));
26+
assert_param(IS_RTC_OUTPUT_POLARITY(init->output_polarity));
27+
28+
rtc_reset();
29+
RTC_UNLOCK();
30+
31+
MODIFY_REG(RTC->CON, RTC_CON_HFM_MSK, init->hour_format << RTC_CON_HFM_POS);
32+
MODIFY_REG(RTC->CON, RTC_CON_EOS_MSK, init->output << RTC_CON_EOS_POSS);
33+
MODIFY_REG(RTC->CON, RTC_CON_POL_MSK, init->output_polarity << RTC_CON_POL_POS);
34+
MODIFY_REG(RTC->PSR, RTC_PSR_SPRS_MSK, init->synch_pre_div << RTC_PSR_SPRS_POSS);
35+
MODIFY_REG(RTC->PSR, RTC_PSR_APRS_MSK, init->asynch_pre_div << RTC_PSR_APRS_POSS);
36+
37+
RTC_LOCK();
38+
return;
39+
}
40+
41+
static rt_err_t es32f0_rtc_control(rt_device_t dev, int cmd, void *args)
42+
{
43+
rt_err_t result = RT_EOK;
44+
45+
struct tm time_temp;
46+
struct tm *pNow;
47+
rtc_date_t date;
48+
rtc_time_t time;
49+
50+
switch (cmd)
51+
{
52+
case RT_DEVICE_CTRL_RTC_GET_TIME:
53+
54+
rtc_get_date_time(&date, &time, RTC_FORMAT_DEC);
55+
time_temp.tm_sec = time.second;
56+
time_temp.tm_min = time.minute;
57+
time_temp.tm_hour = time.hour;
58+
time_temp.tm_mday = date.day;
59+
time_temp.tm_mon = date.month - 1;
60+
time_temp.tm_year = date.year - 1900 + 2000;
61+
*((time_t *)args) = mktime(&time_temp);
62+
break;
63+
64+
case RT_DEVICE_CTRL_RTC_SET_TIME:
65+
66+
rt_enter_critical();
67+
/* converts calendar time time into local time. */
68+
pNow = localtime((const time_t *)args);
69+
/* copy the statically located variable */
70+
memcpy(&time_temp, pNow, sizeof(struct tm));
71+
/* unlock scheduler. */
72+
rt_exit_critical();
73+
74+
time.hour = time_temp.tm_hour;
75+
time.minute = time_temp.tm_min;
76+
time.second = time_temp.tm_sec;
77+
date.year = time_temp.tm_year + 1900 - 2000;
78+
date.month = time_temp.tm_mon + 1;
79+
date.day = time_temp.tm_mday;
80+
rtc_set_time(&time, RTC_FORMAT_DEC);
81+
rtc_set_date(&date, RTC_FORMAT_DEC);
82+
/* start RTC */
83+
RTC_UNLOCK();
84+
SET_BIT(RTC->CON, RTC_CON_GO_MSK);
85+
RTC_LOCK();
86+
break;
87+
88+
case RT_DEVICE_CTRL_RTC_GET_ALARM:
89+
break;
90+
91+
case RT_DEVICE_CTRL_RTC_SET_ALARM:
92+
break;
93+
94+
default:
95+
break;
96+
}
97+
98+
return result;
99+
}
100+
101+
#ifdef RT_USING_DEVICE_OPS
102+
const static struct rt_device_ops es32f0_rtc_ops =
103+
{
104+
RT_NULL,
105+
RT_NULL,
106+
RT_NULL,
107+
RT_NULL,
108+
RT_NULL,
109+
es32f0_rtc_control
110+
};
111+
#endif
112+
113+
int rt_hw_rtc_init(void)
114+
{
115+
rt_err_t ret = RT_EOK;
116+
static struct rt_device rtc_dev;
117+
rtc_init_t rtc_initstruct;
118+
119+
/* enable external 32.768kHz */
120+
CMU_LOSC_ENABLE();
121+
cmu_losc_safe_config(ENABLE);
122+
/* set default time */
123+
RTC_UNLOCK();
124+
WRITE_REG(RTC->TIME, 0x134251);
125+
WRITE_REG(RTC->DATE, 0x1190401);
126+
RTC_LOCK();
127+
/* RTC function initialization */
128+
rtc_initstruct.hour_format = RTC_HOUR_FORMAT_24;
129+
rtc_initstruct.asynch_pre_div = 0;
130+
rtc_initstruct.synch_pre_div = 32767;
131+
rtc_initstruct.output = RTC_OUTPUT_DISABLE;
132+
rtc_init(&rtc_initstruct);
133+
134+
rtc_dev.type = RT_Device_Class_RTC;
135+
rtc_dev.rx_indicate = RT_NULL;
136+
rtc_dev.tx_complete = RT_NULL;
137+
138+
#ifdef RT_USING_DEVICE_OPS
139+
rtc_dev.ops = &es32f0_rtc_ops;
140+
#else
141+
rtc_dev.init = RT_NULL;
142+
rtc_dev.open = RT_NULL;
143+
rtc_dev.close = RT_NULL;
144+
rtc_dev.read = RT_NULL;
145+
rtc_dev.write = RT_NULL;
146+
rtc_dev.control = es32f0_rtc_control;
147+
#endif
148+
149+
rtc_dev.user_data = RTC;
150+
151+
ret = rt_device_register(&rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR);
152+
153+
return ret;
154+
}
155+
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
156+
157+
#endif

bsp/es32f0334/drivers/drv_rtc.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2019-04-01 wangyq the first version
9+
*/
10+
11+
#ifndef DRV_RTC_H__
12+
#define DRV_RTC_H__
13+
14+
int rt_hw_rtc_init(void);
15+
16+
#endif

bsp/es32f0334/libraries/SConscript

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@ src = []
1010

1111
src += Glob('ES32F033x_ALD_StdPeriph_Driver/Source/*.c')
1212

13-
1413
#add for startup script
1514
if rtconfig.CROSS_TOOL == 'gcc':
1615
src = src + ['CMSIS/Device/EastSoft/es32f033x/Startup/gcc/startup_es32f033x.s']
1716
elif rtconfig.CROSS_TOOL == 'keil':
1817
src = src + ['CMSIS/Device/EastSoft/es32f033x/Startup/keil/startup_es32f033x.s']
1918
elif rtconfig.CROSS_TOOL == 'iar':
20-
src = src + ['CMSIS/Device/EastSoft/es32f033x/Startup/iar/startup_es32f033x.s']
19+
src = src + ['CMSIS/Device/EastSoft/es32f033x/Startup/iar/startup_es32f033x.s']
2120

22-
path = [cwd + '/CMSIS/Device/EastSoft/es32f033x/Include',
23-
cwd + '/CMSIS/Include',
24-
cwd + '/ES32F033x_ALD_StdPeriph_Driver/Include']
21+
path = [cwd + '/CMSIS/Device/EastSoft/es32f033x/Include',
22+
cwd + '/CMSIS/Include',
23+
cwd + '/ES32F033x_ALD_StdPeriph_Driver/Include']
2524

2625
group = DefineGroup('Libraries', src, depend = [''], CPPPATH = path)
2726

bsp/es32f0334/rtconfig.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@
175175
/* HWtimer Drivers */
176176

177177

178+
/* RTC Drivers */
179+
180+
178181
/* Onboard Peripheral Drivers */
179182

180183

bsp/es32f0334/rtconfig.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
DEVICE = ' -mcpu=' + CPU + ' -mthumb -ffunction-sections -fdata-sections'
4747
CFLAGS = DEVICE
4848
AFLAGS = ' -c' + DEVICE + ' -x assembler-with-cpp -Wa,-mimplicit-it=thumb'
49-
LFLAGS = DEVICE + ' -Wl,--gc-sections,-Map=rtthread.map,-cref,-u,Reset_Handler -T board/linker_scripts/link.lds'
49+
LFLAGS = DEVICE + ' -Wl,--gc-sections,-Map=rtthread.map,-cref,-u,Reset_Handler -T drivers/linker_scripts/link.lds'
5050

5151
CPATH = ''
5252
LPATH = ''

0 commit comments

Comments
 (0)