summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBernard Xiong <[email protected]>2020-09-24 08:04:07 +0800
committerGitHub <[email protected]>2020-09-24 08:04:07 +0800
commitd8f35fc9519de15dbc8c731e60407c55f8fc7637 (patch)
tree6d56c198579d1a32f5baecc029d79e427f4e1983
parentcc8de794c4bc4f8e324aa6290dc8c35b03f3b2dd (diff)
parentc5b0a848dc86b6e0d79dbf1df3728e9249cd4855 (diff)
Merge pull request #3911 from chenyingchun0312/master
add rtc device driver for nrf52x
-rw-r--r--bsp/nrf5x/libraries/drivers/SConscript3
-rw-r--r--bsp/nrf5x/libraries/drivers/drv_rtc.c162
-rw-r--r--bsp/nrf5x/nrf52832/board/Kconfig33
-rw-r--r--bsp/nrf5x/nrf52840/board/Kconfig35
4 files changed, 232 insertions, 1 deletions
diff --git a/bsp/nrf5x/libraries/drivers/SConscript b/bsp/nrf5x/libraries/drivers/SConscript
index 2b29998d75..f8629162f2 100644
--- a/bsp/nrf5x/libraries/drivers/SConscript
+++ b/bsp/nrf5x/libraries/drivers/SConscript
@@ -32,6 +32,9 @@ if GetDepend(['BSP_USING_PWM']):
if GetDepend(['BSP_USING_WDT']):
src += ['drv_wdt.c']
+if GetDepend(['BSP_USING_ONCHIP_RTC']):
+ src += ['drv_rtc.c']
+
path = [cwd]
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = path)
diff --git a/bsp/nrf5x/libraries/drivers/drv_rtc.c b/bsp/nrf5x/libraries/drivers/drv_rtc.c
new file mode 100644
index 0000000000..e7a7359523
--- /dev/null
+++ b/bsp/nrf5x/libraries/drivers/drv_rtc.c
@@ -0,0 +1,162 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2020-09-08 Chenyingchun first version
+ */
+
+#include "board.h"
+#include <rtthread.h>
+#include <rtdevice.h>
+
+#include <nrfx_rtc.h>
+#include <nrfx_clock.h>
+
+#ifdef BSP_USING_ONCHIP_RTC
+
+#define LOG_TAG "drv.rtc"
+#define DBG_LVL DBG_LOG
+#include <rtdbg.h>
+
+/* 2018-01-30 14:44:50 = RTC_TIME_INIT(2018, 1, 30, 14, 44, 50) */
+#define RTC_TIME_INIT(year, month, day, hour, minute, second) \
+ {.tm_year = year - 1900, .tm_mon = month - 1, .tm_mday = day, .tm_hour = hour, .tm_min = minute, .tm_sec = second}
+
+#ifndef ONCHIP_RTC_TIME_DEFAULT
+#define ONCHIP_RTC_TIME_DEFAULT RTC_TIME_INIT(2018, 1, 1, 0, 0 ,0)
+#endif
+
+#ifndef RTC_INSTANCE_ID
+#define RTC_INSTANCE_ID (2)
+#endif
+
+#define TICK_FREQUENCE_HZ (RT_TICK_PER_SECOND) // RTC tick frequence, in HZ
+
+static struct rt_device rtc;
+static time_t init_time;
+static uint32_t tick = 0;
+
+static void rtc_callback(nrfx_rtc_int_type_t int_type)
+{
+ static uint32_t count = 0;
+
+ if (int_type == NRFX_RTC_INT_TICK)
+ {
+ count++;
+ if((count % TICK_FREQUENCE_HZ) == 0)
+ {
+ tick++;
+ }
+ }
+}
+
+static rt_err_t rt_rtc_config(struct rt_device *dev)
+{
+ #define SYSTICK_CLOCK_HZ (32768UL)
+ #define RTC_PRESCALER ((uint32_t) (NRFX_ROUNDED_DIV(SYSTICK_CLOCK_HZ, TICK_FREQUENCE_HZ) - 1))
+
+ const nrfx_rtc_t rtc_instance = NRFX_RTC_INSTANCE(RTC_INSTANCE_ID);
+ nrf_clock_lf_src_set(NRF_CLOCK, (nrf_clock_lfclk_t)NRFX_CLOCK_CONFIG_LF_SRC);
+ nrfx_clock_lfclk_start();
+
+ //Initialize RTC instance
+ nrfx_rtc_config_t config = NRFX_RTC_DEFAULT_CONFIG;
+ config.prescaler = RTC_PRESCALER;
+
+ nrfx_rtc_init(&rtc_instance, &config, rtc_callback);
+
+ nrfx_rtc_tick_enable(&rtc_instance, true);
+
+ //Power on RTC instance
+ nrfx_rtc_enable(&rtc_instance);
+
+ return RT_EOK;
+}
+
+static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
+{
+ time_t *time;
+ RT_ASSERT(dev != RT_NULL);
+
+ switch (cmd)
+ {
+ case RT_DEVICE_CTRL_RTC_GET_TIME:
+ {
+ time = (time_t *) args;
+ *time = init_time + tick;
+ break;
+ }
+ case RT_DEVICE_CTRL_RTC_SET_TIME:
+ {
+ time = (time_t *) args;
+ init_time = *time - tick;
+ break;
+ }
+ }
+ return RT_EOK;
+}
+
+
+#ifdef RT_USING_DEVICE_OPS
+const static struct rt_device_ops rtc_ops =
+{
+ RT_NULL,
+ RT_NULL,
+ RT_NULL,
+ RT_NULL,
+ RT_NULL,
+ rt_rtc_control
+};
+#endif
+
+static rt_err_t rt_hw_rtc_register(rt_device_t device, const char *name, rt_uint32_t flag)
+{
+ struct tm time_new = ONCHIP_RTC_TIME_DEFAULT;
+
+ RT_ASSERT(device != RT_NULL);
+
+ init_time = mktime(&time_new);
+ if (rt_rtc_config(device) != RT_EOK)
+ {
+ return -RT_ERROR;
+ }
+#ifdef RT_USING_DEVICE_OPS
+ device->ops = &rtc_ops;
+#else
+ device->init = RT_NULL;
+ device->open = RT_NULL;
+ device->close = RT_NULL;
+ device->read = RT_NULL;
+ device->write = RT_NULL;
+ device->control = rt_rtc_control;
+#endif
+ device->type = RT_Device_Class_RTC;
+ device->rx_indicate = RT_NULL;
+ device->tx_complete = RT_NULL;
+ device->user_data = RT_NULL;
+
+ /* register a character device */
+ rt_device_register(device, name, flag);
+
+ return RT_EOK;
+}
+
+int rt_hw_rtc_init(void)
+{
+ rt_err_t result;
+ result = rt_hw_rtc_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
+ if (result != RT_EOK)
+ {
+ LOG_E("rtc register err code: %d", result);
+ return result;
+ }
+ LOG_D("rtc init success");
+ return RT_EOK;
+}
+INIT_DEVICE_EXPORT(rt_hw_rtc_init);
+
+#endif /* BSP_USING_ONCHIP_RTC */
+
diff --git a/bsp/nrf5x/nrf52832/board/Kconfig b/bsp/nrf5x/nrf52832/board/Kconfig
index 80756b5372..41a3aa6c39 100644
--- a/bsp/nrf5x/nrf52832/board/Kconfig
+++ b/bsp/nrf5x/nrf52832/board/Kconfig
@@ -313,6 +313,39 @@ endif
int
default 1
endif
+
+ menuconfig BSP_USING_ONCHIP_RTC
+ bool "Enable RTC"
+ select RT_USING_RTC
+ select RT_USING_LIBC
+ default n
+ if BSP_USING_ONCHIP_RTC
+ config NRFX_CLOCK_ENABLED
+ int
+ default 1
+ config NRFX_CLOCK_DEFAULT_CONFIG_IRQ_PRIORITY
+ int
+ default 7
+ config NRFX_RTC_ENABLED
+ int
+ default 1
+ config NRFX_RTC0_ENABLED
+ int
+ default 1
+ config NRFX_RTC1_ENABLED
+ int
+ default 1
+ config NRFX_RTC2_ENABLED
+ int
+ default 1
+ config RTC_INSTANCE_ID
+ int
+ default 2
+ config RTC_INSTANCE_ID
+ int "select RTC instance id, must be 0, 1, 2"
+ range 0 2
+ default 2
+ endif
endmenu
endmenu
diff --git a/bsp/nrf5x/nrf52840/board/Kconfig b/bsp/nrf5x/nrf52840/board/Kconfig
index 1a3fd7d8ba..9ec3b5c71f 100644
--- a/bsp/nrf5x/nrf52840/board/Kconfig
+++ b/bsp/nrf5x/nrf52840/board/Kconfig
@@ -346,8 +346,41 @@ menu "On-chip Peripheral Drivers"
int
default 1
endif
-endmenu
+ menuconfig BSP_USING_ONCHIP_RTC
+ bool "Enable RTC"
+ select RT_USING_RTC
+ select RT_USING_LIBC
+ default n
+ if BSP_USING_ONCHIP_RTC
+ config NRFX_CLOCK_ENABLED
+ int
+ default 1
+ config NRFX_CLOCK_DEFAULT_CONFIG_IRQ_PRIORITY
+ int
+ default 7
+ config NRFX_RTC_ENABLED
+ int
+ default 1
+ config NRFX_RTC0_ENABLED
+ int
+ default 1
+ config NRFX_RTC1_ENABLED
+ int
+ default 1
+ config NRFX_RTC2_ENABLED
+ int
+ default 1
+ config RTC_INSTANCE_ID
+ int
+ default 2
+ config RTC_INSTANCE_ID
+ int "select RTC instance id, must be 0, 1, 2"
+ range 0 2
+ default 2
+ endif
+
+endmenu
choice
prompt "BLE STACK"