Skip to content

Commit ac9bbc5

Browse files
committed
bsp: k230: add rtc driver
Requirement: The BSP for the k230 platform in the RT-Thread repository does not yet have an RTC driver. Solution: Provide RTC drivers for the k230 platform in the RT-Thread repository, implementing timekeeping and alarm functionalities, with interrupt support for the alarm feature. Signed-off-by: Ze-Hou <yingkezhou@qq.com>
1 parent 8c158ca commit ac9bbc5

File tree

7 files changed

+76
-70
lines changed

7 files changed

+76
-70
lines changed

bsp/k230/.config

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ CONFIG_RT_BLK_PARTITION_EFI=y
358358
# CONFIG_RT_USING_VIRTIO is not set
359359
CONFIG_RT_USING_PIN=y
360360
CONFIG_RT_USING_KTIME=y
361-
CONFIG_RT_USING_HWTIMER=y
361+
# CONFIG_RT_USING_HWTIMER is not set
362362
# CONFIG_RT_USING_CHERRYUSB is not set
363363
# end of Device Drivers
364364

@@ -521,11 +521,7 @@ CONFIG_RT_LWIP_USING_PING=y
521521
#
522522
# CONFIG_RT_USING_RYM is not set
523523
# CONFIG_RT_USING_ULOG is not set
524-
CONFIG_RT_USING_UTEST=y
525-
CONFIG_UTEST_THR_STACK_SIZE=4096
526-
CONFIG_UTEST_THR_PRIORITY=20
527-
# CONFIG_RT_UTEST_USING_AUTO_RUN is not set
528-
# CONFIG_RT_UTEST_USING_ALL_CASES is not set
524+
# CONFIG_RT_USING_UTEST is not set
529525
# CONFIG_RT_USING_VAR_EXPORT is not set
530526
CONFIG_RT_USING_RESOURCE_ID=y
531527
CONFIG_RT_USING_ADT=y
@@ -1621,7 +1617,7 @@ CONFIG_PKG_ZLIB_VER="latest"
16211617
#
16221618
# Drivers Configuration
16231619
#
1624-
CONFIG_BSP_USING_RTC=y
1620+
# CONFIG_BSP_USING_RTC is not set
16251621
# CONFIG_BSP_USING_ADC is not set
16261622
# CONFIG_BSP_USING_TS is not set
16271623
CONFIG_BSP_USING_UART=y
@@ -1634,13 +1630,15 @@ CONFIG_BSP_USING_UART0=y
16341630
# CONFIG_BSP_USING_PWM is not set
16351631
CONFIG_BSP_USING_HARDLOCK=y
16361632
CONFIG_BSP_USING_SDIO=y
1637-
# CONFIG_BSP_USING_SDIO0 is not set
1638-
CONFIG_BSP_USING_SDIO1=y
1639-
CONFIG_BSP_SD_MNT_DEVNAME="sd1p1"
1633+
CONFIG_BSP_USING_SDIO0=y
1634+
# CONFIG_BSP_SDIO0_EMMC is not set
1635+
# CONFIG_BSP_SDIO0_1V8 is not set
1636+
# CONFIG_BSP_USING_SDIO1 is not set
1637+
CONFIG_BSP_SD_MNT_DEVNAME="sd0p1"
16401638
# CONFIG_BSP_USING_TIMERS is not set
16411639
# CONFIG_BSP_USING_WDT is not set
16421640
# CONFIG_BSP_USING_PDMA is not set
1643-
CONFIG_BSP_UTEST_DRIVERS=y
1641+
# CONFIG_BSP_UTEST_DRIVERS is not set
16441642
# end of Drivers Configuration
16451643

16461644
CONFIG_BOARD_C908=y

bsp/k230/board/Kconfig

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ menu "Drivers Configuration"
99
bool "Enable ADC"
1010
select RT_USING_ADC
1111
default n
12-
12+
1313
config BSP_USING_TS
1414
bool "Enable Temperature Sensor"
1515
select RT_USING_TS
@@ -32,15 +32,15 @@ menu "Drivers Configuration"
3232
config BSP_USING_UART1
3333
bool "Enable UART1"
3434
default n
35-
35+
3636
config BSP_USING_UART2
3737
bool "Enable UART2"
3838
default n
39-
39+
4040
config BSP_USING_UART3
4141
bool "Enable UART3"
4242
default n
43-
43+
4444
config BSP_USING_UART4
4545
bool "Enable UART4"
4646
default n
@@ -151,37 +151,37 @@ menu "Drivers Configuration"
151151
menuconfig BSP_USING_PDMA
152152
bool "Enable PDMA"
153153
select RT_USING_PDMA
154-
default n
155-
154+
default n
155+
156156
if BSP_USING_PDMA
157157
config BSP_USING_PDMA_CHANNEL0
158158
bool "Enable PDMA Channel 0"
159159
default n
160-
160+
161161
config BSP_USING_PDMA_CHANNEL1
162162
bool "Enable PDMA Channel 1"
163163
default n
164-
164+
165165
config BSP_USING_PDMA_CHANNEL2
166166
bool "Enable PDMA Channel 2"
167167
default n
168-
168+
169169
config BSP_USING_PDMA_CHANNEL3
170170
bool "Enable PDMA Channel 3"
171171
default n
172-
172+
173173
config BSP_USING_PDMA_CHANNEL4
174174
bool "Enable PDMA Channel 4"
175175
default n
176-
176+
177177
config BSP_USING_PDMA_CHANNEL5
178178
bool "Enable PDMA Channel 5"
179179
default n
180-
180+
181181
config BSP_USING_PDMA_CHANNEL6
182182
bool "Enable PDMA Channel 6"
183183
default n
184-
184+
185185
config BSP_USING_PDMA_CHANNEL7
186186
bool "Enable PDMA Channel 7"
187187
default n

bsp/k230/drivers/interdrv/rtc/SConscript

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ CPPPATH = [cwd]
88

99
group = DefineGroup('RTC', src, depend = ['BSP_USING_RTC'], CPPPATH = CPPPATH)
1010

11-
Return('group')
11+
Return('group')

bsp/k230/drivers/interdrv/rtc/drv_rtc.c

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ static void pmu_isolation_rtc(void)
6363

6464
/* disable pmu isolation */
6565
data = *addr;
66-
data &= ~0x20;
67-
*addr = data;
66+
data &= ~0x20;
67+
*addr = data;
6868
rt_iounmap(reg_pmu_pwr);
6969

7070
/* map pmu base address */
@@ -249,7 +249,7 @@ static void rtc_date_time_set(struct k230_rtc_dev *dev, int year, int month, int
249249
}
250250

251251
rtc->int_ctrl.timer_w_en = 1;
252-
252+
253253
date.year_h = year_h;
254254
date.year_l = year_l;
255255
date.month = month;
@@ -273,7 +273,7 @@ static void rtc_timer_get(struct k230_rtc_dev *dev, time_t *t)
273273
{
274274
rtc->int_ctrl.timer_r_en = 1;
275275
}
276-
276+
277277
tm.tm_sec = rtc->time.second;
278278
tm.tm_min = rtc->time.minute;
279279
tm.tm_hour = rtc->time.hour;
@@ -289,7 +289,7 @@ static void rtc_timer_set(struct k230_rtc_dev *dev, time_t *t)
289289
{
290290
struct tm p_tm;
291291
gmtime_r(t, &p_tm);
292-
292+
293293
rtc_date_time_set(dev, (p_tm.tm_year + 1900), p_tm.tm_mon + 1, p_tm.tm_mday, \
294294
p_tm.tm_hour, p_tm.tm_min, p_tm.tm_sec, p_tm.tm_wday);
295295

@@ -302,7 +302,7 @@ static void rtc_alarm_get(struct k230_rtc_dev *dev, void *args)
302302
volatile rtc_t *rtc = (rtc_t *)dev->base;
303303
rtc_alarm_date_t alarm_date = rtc->alarm_date;
304304
rtc_alarm_time_t alarm_time = rtc->alarm_time;
305-
305+
306306
tm->tm_year = (alarm_date.alarm_year_h * 100 + alarm_date.alarm_year_l) -1900;
307307
tm->tm_mon = alarm_date.alarm_month - 1;
308308
tm->tm_mday = alarm_date.alarm_day;
@@ -313,16 +313,21 @@ static void rtc_alarm_get(struct k230_rtc_dev *dev, void *args)
313313

314314
static void rtc_alarm_set(struct k230_rtc_dev *dev, void *args)
315315
{
316-
struct kd_alarm_setup *setup = (struct kd_alarm_setup*)args;
316+
rtc_alarm_setup_t *setup = (rtc_alarm_setup_t *)args;
317317
struct tm tm = setup->tm;
318+
time_t t;
319+
struct tm p_tm;
318320
volatile rtc_t *rtc = (rtc_t *)dev->base;
319321
rtc_alarm_time_t alarm_time;
320322
rtc_alarm_date_t alarm_date;
321323
rtc_date_t date = rtc->date;
324+
int year, year_l, year_h, val;
322325

323-
int year = tm.tm_year + 1900;
324-
int val = year % 100;
325-
int year_l,year_h;
326+
t = mktime(&tm);
327+
gmtime_r(&t, &p_tm);
328+
329+
year = p_tm.tm_year + 1900;
330+
val = year % 100;
326331

327332
if(val == 0)
328333
{
@@ -337,11 +342,11 @@ static void rtc_alarm_set(struct k230_rtc_dev *dev, void *args)
337342

338343
alarm_date.alarm_year_h = year_h;
339344
alarm_date.alarm_year_l = year_l;
340-
alarm_date.alarm_month = tm.tm_mon + 1;
341-
alarm_date.alarm_day = tm.tm_mday;
342-
alarm_time.alarm_hour = tm.tm_hour;
343-
alarm_time.alarm_minute = tm.tm_min;
344-
alarm_time.alarm_second = tm.tm_sec;
345+
alarm_date.alarm_month = p_tm.tm_mon + 1;
346+
alarm_date.alarm_day = p_tm.tm_mday;
347+
alarm_time.alarm_hour = p_tm.tm_hour;
348+
alarm_time.alarm_minute = p_tm.tm_min;
349+
alarm_time.alarm_second = p_tm.tm_sec;
345350

346351
rtc->alarm_date = alarm_date;
347352
rtc->alarm_time = alarm_time;
@@ -424,7 +429,7 @@ static rt_err_t rtc_device_control(rt_device_t dev, int cmd, void *args)
424429
rtc_dev->vector_callback = args;
425430
break;
426431
default:
427-
return RT_EINVAL;
432+
return -RT_EINVAL;
428433
}
429434
return RT_EOK;
430435
}
@@ -439,7 +444,7 @@ const static struct rt_device_ops rtc_ops =
439444
.control = rtc_device_control,
440445
};
441446

442-
static struct k230_rtc_dev rtc_dev =
447+
static struct k230_rtc_dev rtc_dev =
443448
{
444449
.name = "rtc",
445450
.base = RTC_BASE_ADDR,
@@ -478,4 +483,4 @@ static int rt_hw_rtc_init(void)
478483
rtc_tick_stop(&rtc_dev);
479484
return ret;
480485
}
481-
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
486+
INIT_DEVICE_EXPORT(rt_hw_rtc_init);

bsp/k230/drivers/interdrv/rtc/drv_rtc.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ typedef enum _rtc_tick_interrupt_mode_e
152152
RTC_INT_TICK_S64,
153153
} rtc_interrupt_mode_t;
154154

155-
struct kd_alarm_setup
155+
typedef struct _rtc_alarm_setup
156156
{
157157
rt_uint32_t flag; /* alarm flag */
158158
struct tm tm; /* when will the alarm wake up user */
159-
};
159+
} rtc_alarm_setup_t;
160160

161-
#endif /* __DRV_RTC_H__ */
161+
#endif /* __DRV_RTC_H__ */

bsp/k230/drivers/utest/test_rtc.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,29 @@
3636
#include <finsh.h>
3737
#include "drv_rtc.h"
3838

39-
/*
40-
* 测试RTC的时间功能与闹钟功能,在RTCd的寄存器中保存的时间信息
41-
* 是以UTC时间保存的,要使用本地时间需要配置时区,当前配置的时
39+
/*
40+
* 测试 RTC 的时间功能与闹钟功能,在RTCd的寄存器中保存的时间信息
41+
* 是以 UTC 时间保存的,要使用本地时间需要配置时区,当前配置的时
4242
* 区是东八区,即北京时间(CST)
43-
*
44-
* 在设置时间日期时使用set_time()与set_date()接口,设置的时间
45-
* 这两个接口内部会做时区转换
43+
*
44+
* 在设置时间日期时使用 set_time() 与 set_date() 接口,设置的时
45+
* 间这两个接口内部会做时区转换
4646
*
4747
* 测试说明:
48-
* 基于庐山派开发板测试
49-
* RTC为K230自带的RTC
50-
* RTC的时钟源为外部32.768KHz晶振
51-
* 在测试终端运行该测试后,会分别进行test_rtc_set(),
52-
* test_rtc_alarm()与test_rtc_interface()三个测试
48+
* 基于庐山派开发板测试(01Studio 的开发板无法使用硬件RTC,
49+
* 因为没有接 int0/4 的上拉)
50+
* RTC 为 K230 自带的 RTC
51+
* RTC 的时钟源为外部 32.768KHz 晶振
52+
* 在测试终端运行该测试后,会分别进行 test_rtc_set(),
53+
* test_rtc_alarm() 与 test_rtc_interface() 三个测试。
54+
* 其中 test_rtc_set() 会通过 set_time() 与 set_date() 设置时间,
55+
* 注意设置的时间需要为本地时间,内部会转换成 UTC 时间,然后调用
56+
* drv_rtc.c 内的接口设置到 RTC 寄存器中;
57+
* test_rtc_alarm() 会设置一个 5 秒后的闹钟时间,并注册一个闹钟
58+
* 中断回调函数,同样用户设置的闹钟时间为本地时间,内部会转换成UTC
59+
* 时间然后保存进 RTC 中;
60+
* test_rtc_interface() 会测试读写 RTC 的接口,写 RTC 时同样需要
61+
* 提供本地时间,读出来后需要转换成本地时间(如果有需要)。
5362
*/
5463

5564
#define RTC_NAME "rtc"
@@ -95,7 +104,7 @@ static void test_rtc_alarm(void)
95104
struct tm p_tm;
96105
rt_device_t rtc_dev = RT_NULL;
97106
struct rt_alarm *alarm = RT_NULL;
98-
struct kd_alarm_setup setup;
107+
rtc_alarm_setup_t setup;
99108

100109
LOG_I("rtc alarm test\n");
101110
rtc_dev = rt_device_find(RTC_NAME);
@@ -110,7 +119,7 @@ static void test_rtc_alarm(void)
110119
now = time(RT_NULL);
111120
LOG_I("%s\n", ctime(&now));
112121
now += 5; //alarm after 5s
113-
gmtime_r(&now, &p_tm);
122+
localtime_r(&now, &p_tm);
114123

115124
setup.flag = RTC_INT_ALARM_MINUTE | RTC_INT_ALARM_SECOND;
116125
setup.tm.tm_year = p_tm.tm_year;
@@ -120,7 +129,7 @@ static void test_rtc_alarm(void)
120129
setup.tm.tm_hour = p_tm.tm_hour;
121130
setup.tm.tm_min = p_tm.tm_min;
122131
setup.tm.tm_sec = p_tm.tm_sec;
123-
132+
124133
rt_device_control(rtc_dev, RT_DEVICE_CTRL_RTC_SET_CALLBACK, &test_rtc_alarm_callback); //set rtc intr callback
125134
rt_device_control(rtc_dev, RT_DEVICE_CTRL_RTC_SET_ALARM, &setup); //set alarm time
126135
rt_memset(&p_tm, 0, sizeof(p_tm));
@@ -163,7 +172,7 @@ static void test_rtc_interface(void)
163172
tm.tm_sec = 59;
164173
rt_device_write(rtc_dev, RT_NULL, (void*)&tm, sizeof(tm));
165174
rt_thread_mdelay(500);
166-
175+
167176
/* 设置完时间后打印10次时间 */
168177
for (i=0; i<10; i++)
169178
{
@@ -205,4 +214,4 @@ static rt_err_t utest_tc_cleanup(void)
205214
{
206215
return RT_EOK;
207216
}
208-
UTEST_TC_EXPORT(testcase, "bsp.k230.drivers.rtc", utest_tc_init, utest_tc_cleanup, 100);
217+
UTEST_TC_EXPORT(testcase, "bsp.k230.drivers.rtc", utest_tc_init, utest_tc_cleanup, 100);

bsp/k230/rtconfig.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@
232232
/* end of Partition Types */
233233
#define RT_USING_PIN
234234
#define RT_USING_KTIME
235-
#define RT_USING_HWTIMER
236235
/* end of Device Drivers */
237236

238237
/* C/C++ and POSIX layer */
@@ -342,9 +341,6 @@
342341

343342
/* Utilities */
344343

345-
#define RT_USING_UTEST
346-
#define UTEST_THR_STACK_SIZE 4096
347-
#define UTEST_THR_PRIORITY 20
348344
#define RT_USING_RESOURCE_ID
349345
#define RT_USING_ADT
350346
#define RT_USING_ADT_AVL
@@ -590,15 +586,13 @@
590586

591587
/* Drivers Configuration */
592588

593-
#define BSP_USING_RTC
594589
#define BSP_USING_UART
595590
#define BSP_UART_USING_DMA
596591
#define BSP_USING_UART0
597592
#define BSP_USING_HARDLOCK
598593
#define BSP_USING_SDIO
599-
#define BSP_USING_SDIO1
600-
#define BSP_SD_MNT_DEVNAME "sd1p1"
601-
#define BSP_UTEST_DRIVERS
594+
#define BSP_USING_SDIO0
595+
#define BSP_SD_MNT_DEVNAME "sd0p1"
602596
/* end of Drivers Configuration */
603597
#define BOARD_C908
604598
#define __STACKSIZE__ 65536

0 commit comments

Comments
 (0)