Skip to content

Commit 29adf2f

Browse files
author
Jeppe Odgaard
committedMar 20, 2025·
drivers: rtc: rtc_ll_stm32: rework lock usage in rtc_stm32_set_time
Lock just before and just after accessing functions that modifies register values. Signed-off-by: Jeppe Odgaard <jeppe.odgaard@prevas.dk>
1 parent 0e8dc2a commit 29adf2f

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed
 

‎drivers/rtc/rtc_ll_stm32.c

+13-13
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,6 @@ static int rtc_stm32_set_time(const struct device *dev, const struct rtc_time *t
440440
LL_RTC_TimeTypeDef rtc_time;
441441
LL_RTC_DateTypeDef rtc_date;
442442
uint32_t real_year = timeptr->tm_year + TM_YEAR_REF;
443-
int err = 0;
444443

445444
if (real_year < RTC_YEAR_REF) {
446445
/* RTC does not support years before 2000 */
@@ -452,19 +451,10 @@ static int rtc_stm32_set_time(const struct device *dev, const struct rtc_time *t
452451
return -EINVAL;
453452
}
454453

455-
k_spinlock_key_t key = k_spin_lock(&data->lock);
456-
457-
LOG_DBG("Setting clock");
458-
459-
#if RTC_STM32_BACKUP_DOMAIN_WRITE_PROTECTION
460-
LL_PWR_EnableBkUpAccess();
461-
#endif /* RTC_STM32_BACKUP_DOMAIN_WRITE_PROTECTION */
462-
463454
/* Enter Init mode inside the LL_RTC_Time and Date Init functions */
464455
rtc_time.Hours = bin2bcd(timeptr->tm_hour);
465456
rtc_time.Minutes = bin2bcd(timeptr->tm_min);
466457
rtc_time.Seconds = bin2bcd(timeptr->tm_sec);
467-
LL_RTC_TIME_Init(RTC, LL_RTC_FORMAT_BCD, &rtc_time);
468458

469459
/* Set Date after Time to be sure the DR is correctly updated on stm32F2 serie. */
470460
rtc_date.Year = bin2bcd((real_year - RTC_YEAR_REF));
@@ -474,6 +464,16 @@ static int rtc_stm32_set_time(const struct device *dev, const struct rtc_time *t
474464
/* WeekDay sunday (tm_wday = 0) is not represented by the same value in hardware,
475465
* all the other values are consistent with what is expected by hardware.
476466
*/
467+
468+
LOG_DBG("Setting clock");
469+
470+
k_spinlock_key_t key = k_spin_lock(&data->lock);
471+
472+
#if RTC_STM32_BACKUP_DOMAIN_WRITE_PROTECTION
473+
LL_PWR_EnableBkUpAccess();
474+
#endif /* RTC_STM32_BACKUP_DOMAIN_WRITE_PROTECTION */
475+
476+
LL_RTC_TIME_Init(RTC, LL_RTC_FORMAT_BCD, &rtc_time);
477477
LL_RTC_DATE_Init(RTC, LL_RTC_FORMAT_BCD, &rtc_date);
478478

479479
#if RTC_STM32_BACKUP_DOMAIN_WRITE_PROTECTION
@@ -490,8 +490,6 @@ static int rtc_stm32_set_time(const struct device *dev, const struct rtc_time *t
490490
}
491491
#endif /* CONFIG_SOC_SERIES_STM32F2X */
492492

493-
k_spin_unlock(&data->lock, key);
494-
495493
LOG_DBG("Calendar set : %d/%d/%d - %dh%dm%ds",
496494
LL_RTC_DATE_GetDay(RTC),
497495
LL_RTC_DATE_GetMonth(RTC),
@@ -501,7 +499,9 @@ static int rtc_stm32_set_time(const struct device *dev, const struct rtc_time *t
501499
LL_RTC_TIME_GetSecond(RTC)
502500
);
503501

504-
return err;
502+
k_spin_unlock(&data->lock, key);
503+
504+
return 0;
505505
}
506506

507507
static int rtc_stm32_get_time(const struct device *dev, struct rtc_time *timeptr)

0 commit comments

Comments
 (0)
Please sign in to comment.