1 /*
2  * Copyright (c) 2006-2025, 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 #include <sys/time.h>
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_rtc_device rtc;
39 static time_t init_time;
40 static uint32_t tick = 0;
41 
rtc_callback(nrfx_rtc_int_type_t int_type)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 
rt_rtc_config(struct rt_device * dev)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 
rt_rtc_control(rt_device_t dev,int cmd,void * args)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 
rt_hw_rtc_init(void)116 int rt_hw_rtc_init(void)
117 {
118     rt_err_t result;
119 
120     result = rt_hw_rtc_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR,RT_NULL);
121 
122     if (result != RT_EOK)
123     {
124         LOG_E("rtc register err code: %d", result);
125         return result;
126     }
127     LOG_D("rtc init success");
128     return RT_EOK;
129 }
130 INIT_DEVICE_EXPORT(rt_hw_rtc_init);
131 
132 #endif /* BSP_USING_ONCHIP_RTC */
133 
134