Skip to content

Commit 02a609e

Browse files
add rtc device driver for nrf52x
Signed-off-by: chenyingchun0312 <[email protected]>
1 parent 10a9c12 commit 02a609e

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed

bsp/nrf5x/libraries/drivers/SConscript

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ if GetDepend(['BSP_USING_PWM']):
3232
if GetDepend(['BSP_USING_WDT']):
3333
src += ['drv_wdt.c']
3434

35+
if GetDepend(['BSP_USING_ONCHIP_RTC']):
36+
src += ['drv_rtc.c']
37+
3538
path = [cwd]
3639

3740
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = path)
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2020-09-08 Chenyingchun first version
9+
*/
10+
11+
#include "board.h"
12+
#include <rtthread.h>
13+
#include <rtdevice.h>
14+
15+
#include <nrfx_rtc.h>
16+
#include <nrfx_clock.h>
17+
18+
#ifdef BSP_USING_ONCHIP_RTC
19+
20+
#define LOG_TAG "drv.rtc"
21+
#define DBG_LVL DBG_LOG
22+
#include <rtdbg.h>
23+
24+
/* 2018-01-30 14:44:50 = RTC_TIME_INIT(2018, 1, 30, 14, 44, 50) */
25+
#define RTC_TIME_INIT(year, month, day, hour, minute, second) \
26+
{.tm_year = year - 1900, .tm_mon = month - 1, .tm_mday = day, .tm_hour = hour, .tm_min = minute, .tm_sec = second}
27+
28+
#ifndef ONCHIP_RTC_TIME_DEFAULT
29+
#define ONCHIP_RTC_TIME_DEFAULT RTC_TIME_INIT(2018, 1, 1, 0, 0 ,0)
30+
#endif
31+
32+
#define TICK_FREQUENCE_HZ (RT_TICK_PER_SECOND) // RTC tick frequence, in HZ
33+
34+
static struct rt_device rtc;
35+
static time_t init_time;
36+
static uint32_t tick = 0;
37+
38+
static void rtc_callback(nrfx_rtc_int_type_t int_type)
39+
{
40+
static uint32_t count = 0;
41+
42+
if (int_type == NRFX_RTC_INT_TICK)
43+
{
44+
count++;
45+
if((count % TICK_FREQUENCE_HZ) == 0)
46+
{
47+
tick++;
48+
}
49+
}
50+
}
51+
52+
static rt_err_t rt_rtc_config(struct rt_device *dev)
53+
{
54+
#define SYSTICK_CLOCK_HZ (32768UL)
55+
#define RTC_PRESCALER ((uint32_t) (NRFX_ROUNDED_DIV(SYSTICK_CLOCK_HZ, TICK_FREQUENCE_HZ) - 1))
56+
57+
const nrfx_rtc_t rtc_instance = NRFX_RTC_INSTANCE(2);
58+
nrf_clock_lf_src_set(NRF_CLOCK, (nrf_clock_lfclk_t)NRFX_CLOCK_CONFIG_LF_SRC);
59+
nrfx_clock_lfclk_start();
60+
61+
//Initialize RTC instance
62+
nrfx_rtc_config_t config = NRFX_RTC_DEFAULT_CONFIG;
63+
config.prescaler = RTC_PRESCALER;
64+
65+
nrfx_rtc_init(&rtc_instance, &config, rtc_callback);
66+
67+
nrfx_rtc_tick_enable(&rtc_instance, true);
68+
69+
//Power on RTC instance
70+
nrfx_rtc_enable(&rtc_instance);
71+
72+
return RT_EOK;
73+
}
74+
75+
static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
76+
{
77+
time_t *time;
78+
RT_ASSERT(dev != RT_NULL);
79+
80+
switch (cmd)
81+
{
82+
case RT_DEVICE_CTRL_RTC_GET_TIME:
83+
{
84+
time = (time_t *) args;
85+
*time = init_time + tick;
86+
break;
87+
}
88+
case RT_DEVICE_CTRL_RTC_SET_TIME:
89+
{
90+
time = (time_t *) args;
91+
init_time = *time - tick;
92+
break;
93+
}
94+
}
95+
return RT_EOK;
96+
}
97+
98+
99+
#ifdef RT_USING_DEVICE_OPS
100+
const static struct rt_device_ops rtc_ops =
101+
{
102+
RT_NULL,
103+
RT_NULL,
104+
RT_NULL,
105+
RT_NULL,
106+
RT_NULL,
107+
rt_rtc_control
108+
};
109+
#endif
110+
111+
static rt_err_t rt_hw_rtc_register(rt_device_t device, const char *name, rt_uint32_t flag)
112+
{
113+
struct tm time_new = ONCHIP_RTC_TIME_DEFAULT;
114+
115+
RT_ASSERT(device != RT_NULL);
116+
117+
init_time = mktime(&time_new);
118+
if (rt_rtc_config(device) != RT_EOK)
119+
{
120+
return -RT_ERROR;
121+
}
122+
#ifdef RT_USING_DEVICE_OPS
123+
device->ops = &rtc_ops;
124+
#else
125+
device->init = RT_NULL;
126+
device->open = RT_NULL;
127+
device->close = RT_NULL;
128+
device->read = RT_NULL;
129+
device->write = RT_NULL;
130+
device->control = rt_rtc_control;
131+
#endif
132+
device->type = RT_Device_Class_RTC;
133+
device->rx_indicate = RT_NULL;
134+
device->tx_complete = RT_NULL;
135+
device->user_data = RT_NULL;
136+
137+
/* register a character device */
138+
rt_device_register(device, name, flag);
139+
140+
return RT_EOK;
141+
}
142+
143+
int rt_hw_rtc_init(void)
144+
{
145+
rt_err_t result;
146+
result = rt_hw_rtc_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
147+
if (result != RT_EOK)
148+
{
149+
LOG_E("rtc register err code: %d", result);
150+
return result;
151+
}
152+
LOG_D("rtc init success");
153+
return RT_EOK;
154+
}
155+
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
156+
157+
#endif /* BSP_USING_ONCHIP_RTC */
158+

bsp/nrf5x/nrf52832/board/Kconfig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,27 @@ endif
313313
int
314314
default 1
315315
endif
316+
317+
menuconfig BSP_USING_ONCHIP_RTC
318+
bool "Enable RTC"
319+
select RT_USING_RTC
320+
select RT_USING_LIBC
321+
select NRFX_CLOCK_ENABLED
322+
default n
323+
if BSP_USING_ONCHIP_RTC
324+
config NRFX_CLOCK_ENABLED
325+
int
326+
default 1
327+
config NRFX_RTC_ENABLED
328+
int
329+
default 1
330+
config NRFX_RTC2_ENABLED
331+
int
332+
default 1
333+
config NRFX_CLOCK_DEFAULT_CONFIG_IRQ_PRIORITY
334+
int
335+
default 7
336+
endif
316337
endmenu
317338

318339
endmenu

0 commit comments

Comments
 (0)