Skip to content

Commit e29c414

Browse files
cyliangtwjhedberg
authored andcommitted
drivers: rtc: modify Nuvoton numaker driver for compatibility
For compatibility, do not use spare registers to store mask data. RTC mask data can be generated through the calendar and time mask registers at runtime. Signed-off-by: cyliang tw <[email protected]>
1 parent cbe94ca commit e29c414

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

drivers/rtc/rtc_numaker.c

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ static int rtc_numaker_set_time(const struct device *dev, const struct rtc_time
6262
struct rtc_numaker_time curr_time;
6363
struct rtc_numaker_data *data = dev->data;
6464
uint32_t real_year = timeptr->tm_year + TM_YEAR_REF;
65-
#ifdef CONFIG_RTC_ALARM
66-
const struct rtc_numaker_config *config = dev->config;
67-
RTC_T *rtc_base = config->rtc_base;
68-
#endif
6965

7066
if (real_year < NVT_RTC_YEAR_MIN || real_year > NVT_RTC_YEAR_MAX) {
7167
/* RTC can't support years out of 2000 ~ 2099 */
@@ -88,12 +84,6 @@ static int rtc_numaker_set_time(const struct device *dev, const struct rtc_time
8884
k_spinlock_key_t key = k_spin_lock(&data->lock);
8985

9086
RTC_SetDateAndTime((S_RTC_TIME_DATA_T *)&curr_time);
91-
92-
#ifdef CONFIG_RTC_ALARM
93-
/* Restore RTC alarm mask */
94-
rtc_base->CAMSK = rtc_base->SPR[1];
95-
rtc_base->TAMSK = rtc_base->SPR[2];
96-
#endif
9787
k_spin_unlock(&data->lock, key);
9888

9989
return 0;
@@ -149,8 +139,6 @@ static void rtc_numaker_isr(const struct device *dev)
149139

150140
/* Clear RTC Alarm interrupt flag */
151141
rtc_base->INTSTS = RTC_INTSTS_ALMIF_Msk;
152-
rtc_base->CAMSK = 0x00;
153-
rtc_base->TAMSK = 0x00;
154142
callback = data->alarm_callback;
155143
user_data = data->alarm_user_data;
156144
data->alarm_pending = callback ? false : true;
@@ -211,13 +199,10 @@ static int rtc_numaker_alarm_set_time(const struct device *dev, uint16_t id, uin
211199
irq_disable(DT_INST_IRQN(0));
212200
if ((mask == 0) || (timeptr == NULL)) {
213201
/* Disable the alarm */
214-
rtc_base->SPR[0] = mask;
215-
rtc_base->SPR[1] = 0x00;
216-
rtc_base->SPR[2] = 0x00;
217202
irq_enable(DT_INST_IRQN(0));
218203
k_spin_unlock(&data->lock, key);
219-
rtc_base->CAMSK = rtc_base->SPR[1];
220-
rtc_base->TAMSK = rtc_base->SPR[2];
204+
rtc_base->CAMSK = 0x00;
205+
rtc_base->TAMSK = 0x00;
221206
/* Disable RTC Alarm Interrupt */
222207
RTC_DisableInt(RTC_INTEN_ALMIEN_Msk);
223208
return 0;
@@ -265,12 +250,8 @@ static int rtc_numaker_alarm_set_time(const struct device *dev, uint16_t id, uin
265250
/* Clear RTC alarm interrupt flag */
266251
RTC_CLEAR_ALARM_INT_FLAG();
267252

268-
rtc_base->SPR[0] = mask;
269-
rtc_base->SPR[1] = camsk;
270-
rtc_base->SPR[2] = tamsk;
271-
272-
rtc_base->CAMSK = rtc_base->SPR[1];
273-
rtc_base->TAMSK = rtc_base->SPR[2];
253+
rtc_base->CAMSK = camsk;
254+
rtc_base->TAMSK = tamsk;
274255

275256
k_spin_unlock(&data->lock, key);
276257
irq_enable(DT_INST_IRQN(0));
@@ -281,6 +262,33 @@ static int rtc_numaker_alarm_set_time(const struct device *dev, uint16_t id, uin
281262
return 0;
282263
}
283264

265+
static uint16_t rtc_numaker_get_mask(uint32_t camsk, uint32_t tamsk)
266+
{
267+
uint16_t mask = 0x00;
268+
269+
/* Set H/W care to match bits */
270+
if (!((camsk >> RTC_CAMSK_MYEAR_Pos) & NVT_ALARM_UNIT_MSK)) {
271+
mask |= RTC_ALARM_TIME_MASK_YEAR;
272+
}
273+
if (!((camsk >> RTC_CAMSK_MMON_Pos) & NVT_ALARM_UNIT_MSK)) {
274+
mask |= RTC_ALARM_TIME_MASK_MONTH;
275+
}
276+
if (!((camsk >> RTC_CAMSK_MDAY_Pos) & NVT_ALARM_UNIT_MSK)) {
277+
mask |= RTC_ALARM_TIME_MASK_MONTHDAY;
278+
}
279+
if (!((tamsk >> RTC_TAMSK_MHR_Pos) & NVT_ALARM_UNIT_MSK)) {
280+
mask |= RTC_ALARM_TIME_MASK_HOUR;
281+
}
282+
if (!((tamsk >> RTC_TAMSK_MMIN_Pos) & NVT_ALARM_UNIT_MSK)) {
283+
mask |= RTC_ALARM_TIME_MASK_MINUTE;
284+
}
285+
if (!((tamsk >> RTC_TAMSK_MSEC_Pos) & NVT_ALARM_UNIT_MSK)) {
286+
mask |= RTC_ALARM_TIME_MASK_SECOND;
287+
}
288+
289+
return mask;
290+
}
291+
284292
static int rtc_numaker_alarm_get_time(const struct device *dev, uint16_t id, uint16_t *mask,
285293
struct rtc_time *timeptr)
286294
{
@@ -299,7 +307,7 @@ static int rtc_numaker_alarm_get_time(const struct device *dev, uint16_t id, uin
299307
RTC_GetAlarmDateAndTime((S_RTC_TIME_DATA_T *)&alarm_time);
300308
}
301309

302-
*mask = rtc_base->SPR[0];
310+
*mask = rtc_numaker_get_mask(rtc_base->CAMSK, rtc_base->TAMSK);
303311
if (*mask & RTC_ALARM_TIME_MASK_YEAR) {
304312
timeptr->tm_year = alarm_time.year - TM_YEAR_REF;
305313
}
@@ -379,7 +387,6 @@ static int rtc_numaker_init(const struct device *dev)
379387
{
380388
const struct rtc_numaker_config *cfg = dev->config;
381389
struct numaker_scc_subsys scc_subsys;
382-
RTC_T *rtc_base = cfg->rtc_base;
383390
int err;
384391

385392
/* CLK controller */
@@ -396,8 +403,6 @@ static int rtc_numaker_init(const struct device *dev)
396403
}
397404

398405
RTC_SetClockSource(cfg->oscillator);
399-
/* Enable spare registers */
400-
rtc_base->SPRCTL = RTC_SPRCTL_SPRRWEN_Msk;
401406

402407
irq_disable(DT_INST_IRQN(0));
403408

0 commit comments

Comments
 (0)