|
| 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 | +#ifndef RTC_INSTANCE_ID |
| 33 | +#define RTC_INSTANCE_ID (2) |
| 34 | +#endif |
| 35 | + |
| 36 | +#define TICK_FREQUENCE_HZ (RT_TICK_PER_SECOND) // RTC tick frequence, in HZ |
| 37 | + |
| 38 | +static struct rt_device rtc; |
| 39 | +static time_t init_time; |
| 40 | +static uint32_t tick = 0; |
| 41 | + |
| 42 | +static void rtc_callback(nrfx_rtc_int_type_t int_type) |
| 43 | +{ |
| 44 | + static uint32_t count = 0; |
| 45 | + |
| 46 | + if (int_type == NRFX_RTC_INT_TICK) |
| 47 | + { |
| 48 | + count++; |
| 49 | + if((count % TICK_FREQUENCE_HZ) == 0) |
| 50 | + { |
| 51 | + tick++; |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +static rt_err_t rt_rtc_config(struct rt_device *dev) |
| 57 | +{ |
| 58 | + #define SYSTICK_CLOCK_HZ (32768UL) |
| 59 | + #define RTC_PRESCALER ((uint32_t) (NRFX_ROUNDED_DIV(SYSTICK_CLOCK_HZ, TICK_FREQUENCE_HZ) - 1)) |
| 60 | + |
| 61 | + const nrfx_rtc_t rtc_instance = NRFX_RTC_INSTANCE(RTC_INSTANCE_ID); |
| 62 | + nrf_clock_lf_src_set(NRF_CLOCK, (nrf_clock_lfclk_t)NRFX_CLOCK_CONFIG_LF_SRC); |
| 63 | + nrfx_clock_lfclk_start(); |
| 64 | + |
| 65 | + //Initialize RTC instance |
| 66 | + nrfx_rtc_config_t config = NRFX_RTC_DEFAULT_CONFIG; |
| 67 | + config.prescaler = RTC_PRESCALER; |
| 68 | + |
| 69 | + nrfx_rtc_init(&rtc_instance, &config, rtc_callback); |
| 70 | + |
| 71 | + nrfx_rtc_tick_enable(&rtc_instance, true); |
| 72 | + |
| 73 | + //Power on RTC instance |
| 74 | + nrfx_rtc_enable(&rtc_instance); |
| 75 | + |
| 76 | + return RT_EOK; |
| 77 | +} |
| 78 | + |
| 79 | +static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args) |
| 80 | +{ |
| 81 | + time_t *time; |
| 82 | + RT_ASSERT(dev != RT_NULL); |
| 83 | + |
| 84 | + switch (cmd) |
| 85 | + { |
| 86 | + case RT_DEVICE_CTRL_RTC_GET_TIME: |
| 87 | + { |
| 88 | + time = (time_t *) args; |
| 89 | + *time = init_time + tick; |
| 90 | + break; |
| 91 | + } |
| 92 | + case RT_DEVICE_CTRL_RTC_SET_TIME: |
| 93 | + { |
| 94 | + time = (time_t *) args; |
| 95 | + init_time = *time - tick; |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + return RT_EOK; |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +#ifdef RT_USING_DEVICE_OPS |
| 104 | +const static struct rt_device_ops rtc_ops = |
| 105 | +{ |
| 106 | + RT_NULL, |
| 107 | + RT_NULL, |
| 108 | + RT_NULL, |
| 109 | + RT_NULL, |
| 110 | + RT_NULL, |
| 111 | + rt_rtc_control |
| 112 | +}; |
| 113 | +#endif |
| 114 | + |
| 115 | +static rt_err_t rt_hw_rtc_register(rt_device_t device, const char *name, rt_uint32_t flag) |
| 116 | +{ |
| 117 | + struct tm time_new = ONCHIP_RTC_TIME_DEFAULT; |
| 118 | + |
| 119 | + RT_ASSERT(device != RT_NULL); |
| 120 | + |
| 121 | + init_time = mktime(&time_new); |
| 122 | + if (rt_rtc_config(device) != RT_EOK) |
| 123 | + { |
| 124 | + return -RT_ERROR; |
| 125 | + } |
| 126 | +#ifdef RT_USING_DEVICE_OPS |
| 127 | + device->ops = &rtc_ops; |
| 128 | +#else |
| 129 | + device->init = RT_NULL; |
| 130 | + device->open = RT_NULL; |
| 131 | + device->close = RT_NULL; |
| 132 | + device->read = RT_NULL; |
| 133 | + device->write = RT_NULL; |
| 134 | + device->control = rt_rtc_control; |
| 135 | +#endif |
| 136 | + device->type = RT_Device_Class_RTC; |
| 137 | + device->rx_indicate = RT_NULL; |
| 138 | + device->tx_complete = RT_NULL; |
| 139 | + device->user_data = RT_NULL; |
| 140 | + |
| 141 | + /* register a character device */ |
| 142 | + rt_device_register(device, name, flag); |
| 143 | + |
| 144 | + return RT_EOK; |
| 145 | +} |
| 146 | + |
| 147 | +int rt_hw_rtc_init(void) |
| 148 | +{ |
| 149 | + rt_err_t result; |
| 150 | + result = rt_hw_rtc_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR); |
| 151 | + if (result != RT_EOK) |
| 152 | + { |
| 153 | + LOG_E("rtc register err code: %d", result); |
| 154 | + return result; |
| 155 | + } |
| 156 | + LOG_D("rtc init success"); |
| 157 | + return RT_EOK; |
| 158 | +} |
| 159 | +INIT_DEVICE_EXPORT(rt_hw_rtc_init); |
| 160 | + |
| 161 | +#endif /* BSP_USING_ONCHIP_RTC */ |
| 162 | + |
0 commit comments