1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-03-04 stevetong459 first version
*/
#include "board.h"
#include <sys/time.h>
#ifdef BSP_USING_ONCHIP_RTC
#define LOG_TAG "drv.rtc"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#ifndef LSI_VALUE
#define LSI_VALUE ((uint32_t)40000)
#endif
#ifndef LSE_VALUE
#define LSE_VALUE ((uint32_t)32768)
#endif
#define DRV_RTC_TIME_OUT 0xFFF
static rt_rtc_dev_t apm32_rtc_dev;
/**
* @brief This function will initialize the rtc on chip.
*
* @return RT_EOK indicates successful initialize, other value indicates failed;
*/
static rt_err_t _rtc_init(void)
{
volatile rt_uint32_t counter = 0;
/* Enable RTC Clock */
RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_PMU | RCM_APB1_PERIPH_BAKR);
PMU_EnableBackupAccess();
/* Config RTC clock */
#ifdef BSP_RTC_USING_LSI
RCM_EnableLSI();
while (!RCM_ReadStatusFlag(RCM_FLAG_LSIRDY))
{
if (++counter > DRV_RTC_TIME_OUT)
{
return RT_ETIMEOUT;
}
}
RCM_ConfigRTCCLK(RCM_RTCCLK_LSI);
#else
RCM_ConfigLSE(RCM_LSE_OPEN);
while (!RCM_ReadStatusFlag(RCM_FLAG_LSERDY))
{
if (++counter > DRV_RTC_TIME_OUT)
{
return RT_ETIMEOUT;
}
}
RCM_ConfigRTCCLK(RCM_RTCCLK_LSE);
#endif
RCM_EnableRTCCLK();
RTC_WaitForSynchor();
counter = 0;
while (!RTC_ReadStatusFlag(RTC_FLAG_OC))
{
if (++counter > DRV_RTC_TIME_OUT)
{
return RT_ETIMEOUT;
}
}
RTC_EnableConfigMode();
RTC_ClearStatusFlag(RTC_FLAG_OVR | RTC_FLAG_ALR | RTC_FLAG_SEC);
#ifdef BSP_RTC_USING_LSI
RTC_ConfigPrescaler(LSI_VALUE - 1);
#else
RTC_ConfigPrescaler(LSE_VALUE - 1);
#endif
return RT_EOK;
}
/**
* @brief This function will initialize the rtc on chip.
*
* @return RT_EOK indicates successful initialize, other value indicates failed;
*/
static rt_err_t _rtc_get_secs(void *args)
{
volatile rt_uint32_t counter = 0;
while (!RTC_ReadStatusFlag(RTC_FLAG_OC))
{
if (++counter > DRV_RTC_TIME_OUT)
{
return RT_ETIMEOUT;
}
}
*(rt_uint32_t *) args = RTC_ReadCounter();
return RT_EOK;
}
static rt_err_t _rtc_set_secs(void *args)
{
volatile rt_uint32_t counter = 0;
while (!RTC_ReadStatusFlag(RTC_FLAG_OC))
{
if (++counter > DRV_RTC_TIME_OUT)
{
return RT_ETIMEOUT;
}
}
RTC_ConfigCounter(*(rt_uint32_t *)args);
return RT_EOK;
}
static const struct rt_rtc_ops _rtc_ops =
{
_rtc_init,
_rtc_get_secs,
_rtc_set_secs,
RT_NULL,
RT_NULL,
RT_NULL,
RT_NULL,
};
/**
* @brief RTC initialization function.
*
* @return RT_EOK indicates successful initialization, other value indicates failed;
*/
static int rt_hw_rtc_init(void)
{
rt_err_t result = RT_EOK;
apm32_rtc_dev.ops = &_rtc_ops;
if (rt_hw_rtc_register(&apm32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL) != RT_EOK)
{
LOG_E("rtc init failed");
result = RT_ERROR;
}
else
{
LOG_D("rtc init success");
}
return result;
}
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
#endif /* BSP_USING_ONCHIP_RTC */
|