16
16
#include <DA1469xAB.h>
17
17
#include <da1469x_config.h>
18
18
#include <da1469x_pdc.h>
19
+ #include "rtc_utils.h"
19
20
#include <zephyr/logging/log.h>
20
21
21
22
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
266
267
}
267
268
268
269
if (!data -> is_rtc_configured ) {
269
- LOG_ERR ("RTC is not initialized yet" );
270
- return - ENODATA ;
270
+ LOG_WRN ("RTC is not initialized yet" );
271
271
}
272
272
273
273
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
286
286
#if defined(CONFIG_RTC_ALARM )
287
287
BUILD_ASSERT (RTC_ALARMS_COUNT , "At least one alarm event should be supported" );
288
288
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
+ */
290
293
static uint32_t alarm_calendar_to_bcd (const struct rtc_time * timeptr , uint16_t mask )
291
294
{
292
295
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
304
307
return rtc_calendar_alarm_reg ;
305
308
}
306
309
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 )
309
315
{
310
316
uint32_t rtc_time_alarm_reg = 0 ;
311
317
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
+ }
315
332
316
333
return rtc_time_alarm_reg ;
317
334
}
@@ -408,6 +425,11 @@ static int rtc_smartbond_alarm_set_time(const struct device *dev, uint16_t id, u
408
425
return - EINVAL ;
409
426
}
410
427
428
+ if (!rtc_utils_validate_rtc_time (timeptr , mask )) {
429
+ LOG_ERR ("Invalid alarm fields values" );
430
+ return - EINVAL ;
431
+ }
432
+
411
433
if (!data -> is_rtc_configured ) {
412
434
LOG_WRN ("RTC is not initialized yet" );
413
435
}
@@ -425,7 +447,7 @@ static int rtc_smartbond_alarm_set_time(const struct device *dev, uint16_t id, u
425
447
rtc_time_alarm_reg = RTC -> RTC_TIME_ALARM_REG ;
426
448
rtc_calendar_alarm_reg = RTC -> RTC_CALENDAR_ALARM_REG ;
427
449
428
- RTC -> RTC_TIME_ALARM_REG = alarm_time_to_bcd (timeptr );
450
+ RTC -> RTC_TIME_ALARM_REG = alarm_time_to_bcd (timeptr , mask );
429
451
RTC -> RTC_CALENDAR_ALARM_REG = alarm_calendar_to_bcd (timeptr , mask );
430
452
431
453
rtc_status_reg = RTC -> RTC_STATUS_REG ;
0 commit comments