Skip to content

Commit 6f0476e

Browse files
ioannis-karachaliosMaureenHelm
authored andcommitted
drivers: rtc: smartbond: Update RTC driver
This commit should fix the followings: 1. When an alarm event is initialized, the driver should only consume the valid alarm fields, based on the given mask. Otherwise, the driver should use default valid subfield values. 2. Setting an alarm event should not return with error code if the timer and/or calendar counters have yet to be initialized, explicitly. Instead, a log warning should be issued. Signed-off-by: Ioannis Karachalios <[email protected]>
1 parent b8836ab commit 6f0476e

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

drivers/rtc/rtc_smartbond.c

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <DA1469xAB.h>
1717
#include <da1469x_config.h>
1818
#include <da1469x_pdc.h>
19+
#include "rtc_utils.h"
1920
#include <zephyr/logging/log.h>
2021

2122
LOG_MODULE_REGISTER(rtc_smartbond, CONFIG_RTC_LOG_LEVEL);
@@ -266,8 +267,7 @@ static int rtc_smartbond_get_time(const struct device *dev, struct rtc_time *tim
266267
}
267268

268269
if (!data->is_rtc_configured) {
269-
LOG_ERR("RTC is not initialized yet");
270-
return -ENODATA;
270+
LOG_WRN("RTC is not initialized yet");
271271
}
272272

273273
k_mutex_lock(&data->lock, K_FOREVER);
@@ -286,7 +286,10 @@ static int rtc_smartbond_get_time(const struct device *dev, struct rtc_time *tim
286286
#if defined(CONFIG_RTC_ALARM)
287287
BUILD_ASSERT(RTC_ALARMS_COUNT, "At least one alarm event should be supported");
288288

289-
/* Define a valid calendar value as a zero sub-field is not valid for the alarm calendar value */
289+
/*
290+
* Parse only the alarm fields indicated by the mask. Default valid values should be assigned
291+
* to unused fields as it might happen that application has provided with invalid values.
292+
*/
290293
static uint32_t alarm_calendar_to_bcd(const struct rtc_time *timeptr, uint16_t mask)
291294
{
292295
uint32_t rtc_calendar_alarm_reg = 0x0108;
@@ -304,14 +307,28 @@ static uint32_t alarm_calendar_to_bcd(const struct rtc_time *timeptr, uint16_t m
304307
return rtc_calendar_alarm_reg;
305308
}
306309

307-
/* No need to parse the alarm mask as a zero sub-field is valid for the alarm time counter. */
308-
static inline uint32_t alarm_time_to_bcd(const struct rtc_time *timeptr)
310+
/*
311+
* Parse only the alarm fields indicated by the mask. Default valid values should be assigned
312+
* to unused fields as it might happen that application has provided with invalid values.
313+
*/
314+
static inline uint32_t alarm_time_to_bcd(const struct rtc_time *timeptr, uint16_t mask)
309315
{
310316
uint32_t rtc_time_alarm_reg = 0;
311317

312-
RTC_TIME_ALARM_REG_SET_FIELD(S, rtc_time_alarm_reg, bin2bcd(timeptr->tm_sec)); /*[0, 59]*/
313-
RTC_TIME_ALARM_REG_SET_FIELD(M, rtc_time_alarm_reg, bin2bcd(timeptr->tm_min)); /*[0, 59]*/
314-
RTC_TIME_ALARM_REG_SET_FIELD(HR, rtc_time_alarm_reg, bin2bcd(timeptr->tm_hour)); /*[0, 23]*/
318+
if (mask & RTC_ALARM_TIME_MASK_SECOND) {
319+
/*[0, 59]*/
320+
RTC_TIME_ALARM_REG_SET_FIELD(S, rtc_time_alarm_reg, bin2bcd(timeptr->tm_sec));
321+
}
322+
323+
if (mask & RTC_ALARM_TIME_MASK_MINUTE) {
324+
/*[0, 59]*/
325+
RTC_TIME_ALARM_REG_SET_FIELD(M, rtc_time_alarm_reg, bin2bcd(timeptr->tm_min));
326+
}
327+
328+
if (mask & RTC_ALARM_TIME_MASK_HOUR) {
329+
/*[0, 23]*/
330+
RTC_TIME_ALARM_REG_SET_FIELD(HR, rtc_time_alarm_reg, bin2bcd(timeptr->tm_hour));
331+
}
315332

316333
return rtc_time_alarm_reg;
317334
}
@@ -408,6 +425,11 @@ static int rtc_smartbond_alarm_set_time(const struct device *dev, uint16_t id, u
408425
return -EINVAL;
409426
}
410427

428+
if (!rtc_utils_validate_rtc_time(timeptr, mask)) {
429+
LOG_ERR("Invalid alarm fields values");
430+
return -EINVAL;
431+
}
432+
411433
if (!data->is_rtc_configured) {
412434
LOG_WRN("RTC is not initialized yet");
413435
}
@@ -425,7 +447,7 @@ static int rtc_smartbond_alarm_set_time(const struct device *dev, uint16_t id, u
425447
rtc_time_alarm_reg = RTC->RTC_TIME_ALARM_REG;
426448
rtc_calendar_alarm_reg = RTC->RTC_CALENDAR_ALARM_REG;
427449

428-
RTC->RTC_TIME_ALARM_REG = alarm_time_to_bcd(timeptr);
450+
RTC->RTC_TIME_ALARM_REG = alarm_time_to_bcd(timeptr, mask);
429451
RTC->RTC_CALENDAR_ALARM_REG = alarm_calendar_to_bcd(timeptr, mask);
430452

431453
rtc_status_reg = RTC->RTC_STATUS_REG;

0 commit comments

Comments
 (0)