Skip to content

Commit 21b2c4c

Browse files
committed
M2351: Fix RTC clock selection
1 parent 0445b6a commit 21b2c4c

File tree

1 file changed

+65
-27
lines changed

1 file changed

+65
-27
lines changed

targets/TARGET_NUVOTON/TARGET_M2351/rtc_api.c

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626
#include "partition_M2351.h"
2727
#include "hal_secure.h"
2828

29-
/* NOTE: BSP RTC driver judges secure/non-secure RTC by PC. This implementation cannot support non-secure RTC
30-
* controlled by secure executable. A better way would be that secure/non-secure RTC base is passed
31-
* to RTC API as an argument like most other APIs. With BSP RTC driver unchanged, we must enforce
32-
* secure RTC. */
29+
/* Secure attribution of RTC
30+
*
31+
* We need RTC to be secure for security concern.
32+
*
33+
* On M2351, configured to secure
34+
* On M2354, hard-wired to secure
35+
*/
3336
#if defined(SCU_INIT_PNSSET2_VAL) && (SCU_INIT_PNSSET2_VAL & (1 << 1))
3437
#error("Limited by BSP/RTC, we can only support secure RTC.")
3538
#endif
@@ -67,7 +70,7 @@ void rtc_write(time_t t)
6770
*
6871
* NOTE: This dependents on real hardware.
6972
*/
70-
#define NU_RTCCLK_PER_SEC ((CLK->CLKSEL3 & CLK_CLKSEL3_SC0SEL_Msk) ? __LIRC : __LXT)
73+
#define NU_RTCCLK_PER_SEC (__LXT)
7174

7275
/* Strategy for implementation of RTC HAL
7376
*
@@ -121,52 +124,53 @@ static time_t t_write = 0;
121124
/* Convert date time from H/W RTC to struct TM */
122125
static void rtc_convert_datetime_hwrtc_to_tm(struct tm *datetime_tm, const S_RTC_TIME_DATA_T *datetime_hwrtc);
123126

124-
static const struct nu_modinit_s rtc_modinit = {RTC_0, RTC_MODULE, 0, 0, 0, RTC_IRQn, NULL};
127+
static const struct nu_modinit_s rtc_modinit = {RTC_0, RTC_MODULE, CLK_CLKSEL3_RTCSEL_LXT, 0, 0, RTC_IRQn, NULL};
125128

126-
__NONSECURE_ENTRY
127-
void rtc_init_s(void)
129+
static void rtc_init_impl(void);
130+
static void rtc_free_impl(void);
131+
static int32_t rtc_isenabled_impl(void);
132+
static int64_t rtc_read_impl(void);
133+
static void rtc_write_impl(int64_t t);
134+
135+
static void rtc_init_impl(void)
128136
{
129-
if (rtc_isenabled()) {
137+
if (rtc_isenabled_impl()) {
130138
return;
131139
}
132140

133141
RTC_Open(NULL);
134142

135143
/* POSIX time origin (00:00:00 UTC, Thursday, 1 January 1970) */
136-
rtc_write(0);
144+
rtc_write_impl(0);
137145
}
138146

139-
__NONSECURE_ENTRY
140-
void rtc_free_s(void)
147+
static void rtc_free_impl(void)
141148
{
142149
CLK_DisableModuleClock_S(rtc_modinit.clkidx);
143150
}
144151

145-
__NONSECURE_ENTRY
146-
int32_t rtc_isenabled_s(void)
152+
static int32_t rtc_isenabled_impl(void)
147153
{
148-
// NOTE: To access (RTC) registers, clock must be enabled first.
149-
if (! (CLK->APBCLK0 & CLK_APBCLK0_RTCCKEN_Msk)) {
150-
// Enable IP clock
151-
CLK_EnableModuleClock_S(rtc_modinit.clkidx);
152-
}
154+
// To access (RTC) registers, clock must be enabled first.
155+
// For TZ, with RTC being secure, we needn't call the secure gateway versions.
156+
CLK_EnableModuleClock(rtc_modinit.clkidx);
157+
CLK_SetModuleClock(rtc_modinit.clkidx, rtc_modinit.clksrc, rtc_modinit.clkdiv);
153158

154159
RTC_T *rtc_base = (RTC_T *) NU_MODBASE(rtc_modinit.modname);
155160

156161
// NOTE: Check RTC Init Active flag to support crossing reset cycle.
157162
return !! (rtc_base->INIT & RTC_INIT_ACTIVE_Msk);
158163
}
159164

160-
__NONSECURE_ENTRY
161-
int64_t rtc_read_s(void)
165+
static int64_t rtc_read_impl(void)
162166
{
163167
/* NOTE: After boot, RTC time registers are not synced immediately, about 1 sec latency.
164168
* RTC time got (through RTC_GetDateAndTime()) in this sec would be last-synced and incorrect.
165169
* NUC472/M453: Known issue
166170
* M487: Fixed
167171
*/
168-
if (! rtc_isenabled()) {
169-
rtc_init();
172+
if (! rtc_isenabled_impl()) {
173+
rtc_init_impl();
170174
}
171175

172176
/* Used for intermediary between date time of H/W RTC and POSIX time */
@@ -206,11 +210,10 @@ int64_t rtc_read_s(void)
206210
return t_present;
207211
}
208212

209-
__NONSECURE_ENTRY
210-
void rtc_write_s(int64_t t)
213+
static void rtc_write_impl(int64_t t)
211214
{
212-
if (! rtc_isenabled()) {
213-
rtc_init();
215+
if (! rtc_isenabled_impl()) {
216+
rtc_init_impl();
214217
}
215218

216219
t_write = t;
@@ -255,4 +258,39 @@ static void rtc_convert_datetime_hwrtc_to_tm(struct tm *datetime_tm, const S_RTC
255258
}
256259

257260
#endif
261+
262+
#if defined(__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
263+
264+
__NONSECURE_ENTRY
265+
void rtc_init_s(void)
266+
{
267+
rtc_init_impl();
268+
}
269+
270+
__NONSECURE_ENTRY
271+
void rtc_free_s(void)
272+
{
273+
rtc_free_impl();
274+
}
275+
276+
__NONSECURE_ENTRY
277+
int32_t rtc_isenabled_s(void)
278+
{
279+
return rtc_isenabled_impl();
280+
}
281+
282+
__NONSECURE_ENTRY
283+
int64_t rtc_read_s(void)
284+
{
285+
return rtc_read_impl();
286+
}
287+
288+
__NONSECURE_ENTRY
289+
void rtc_write_s(int64_t t)
290+
{
291+
rtc_write_impl(t);
292+
}
293+
294+
#endif
295+
258296
#endif

0 commit comments

Comments
 (0)