Skip to content

Commit 7c996c8

Browse files
Jeppe Odgaardkartben
authored andcommitted
drivers: rtc: rtc_ll_stm32: replace k_mutex with k_spinlock
This allows using this RTC driver from contexts where `k_mutex` use is not allowed. Signed-off-by: Jeppe Odgaard <[email protected]>
1 parent 230280e commit 7c996c8

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed

drivers/rtc/rtc_ll_stm32.c

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <zephyr/kernel.h>
1515
#include <zephyr/init.h>
1616
#include <zephyr/devicetree.h>
17+
#include <zephyr/spinlock.h>
1718
#include <zephyr/drivers/rtc.h>
1819
#include <zephyr/drivers/clock_control/stm32_clock_control.h>
1920
#include <zephyr/drivers/clock_control.h>
@@ -132,7 +133,7 @@ struct rtc_stm32_alrm {
132133
#endif /* STM32_RTC_ALARM_ENABLED */
133134

134135
struct rtc_stm32_data {
135-
struct k_mutex lock;
136+
struct k_spinlock lock;
136137
#ifdef STM32_RTC_ALARM_ENABLED
137138
struct rtc_stm32_alrm rtc_alrm_a;
138139
struct rtc_stm32_alrm rtc_alrm_b;
@@ -335,7 +336,7 @@ static int rtc_stm32_init(const struct device *dev)
335336
{
336337
const struct device *const clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
337338
const struct rtc_stm32_config *cfg = dev->config;
338-
struct rtc_stm32_data *data = dev->data;
339+
__maybe_unused struct rtc_stm32_data *data = dev->data;
339340

340341
int err = 0;
341342

@@ -380,8 +381,6 @@ static int rtc_stm32_init(const struct device *dev)
380381
}
381382
#endif /* CONFIG_SOC_SERIES_STM32WB0X */
382383

383-
k_mutex_init(&data->lock);
384-
385384
/* Enable Backup access */
386385
#if RTC_STM32_BACKUP_DOMAIN_WRITE_PROTECTION
387386
LL_PWR_EnableBkUpAccess();
@@ -426,11 +425,11 @@ static int rtc_stm32_init(const struct device *dev)
426425

427426
ll_func_exti_enable_rtc_alarm_it(RTC_STM32_EXTI_LINE);
428427

429-
k_mutex_lock(&data->lock, K_FOREVER);
430-
memset(&(data->rtc_alrm_a), 0, sizeof(struct rtc_stm32_alrm));
431-
memset(&(data->rtc_alrm_b), 0, sizeof(struct rtc_stm32_alrm));
432-
k_mutex_unlock(&data->lock);
433-
#endif /* STM32_RTC_ALARM_ENABLED */
428+
K_SPINLOCK(&data->lock) {
429+
memset(&(data->rtc_alrm_a), 0, sizeof(struct rtc_stm32_alrm));
430+
memset(&(data->rtc_alrm_b), 0, sizeof(struct rtc_stm32_alrm));
431+
}
432+
#endif /* CONFIG_RTC_ALARM */
434433

435434
return err;
436435
}
@@ -453,10 +452,7 @@ static int rtc_stm32_set_time(const struct device *dev, const struct rtc_time *t
453452
return -EINVAL;
454453
}
455454

456-
err = k_mutex_lock(&data->lock, K_NO_WAIT);
457-
if (err) {
458-
return err;
459-
}
455+
k_spinlock_key_t key = k_spin_lock(&data->lock);
460456

461457
LOG_DBG("Setting clock");
462458

@@ -494,7 +490,7 @@ static int rtc_stm32_set_time(const struct device *dev, const struct rtc_time *t
494490
}
495491
#endif /* CONFIG_SOC_SERIES_STM32F2X */
496492

497-
k_mutex_unlock(&data->lock);
493+
k_spin_unlock(&data->lock, key);
498494

499495
LOG_DBG("Calendar set : %d/%d/%d - %dh%dm%ds",
500496
LL_RTC_DATE_GetDay(RTC),
@@ -524,18 +520,14 @@ static int rtc_stm32_get_time(const struct device *dev, struct rtc_time *timeptr
524520
return -EINVAL;
525521
}
526522

527-
int err = k_mutex_lock(&data->lock, K_NO_WAIT);
528-
529-
if (err) {
530-
return err;
531-
}
523+
k_spinlock_key_t key = k_spin_lock(&data->lock);
532524

533525
if (!LL_RTC_IsActiveFlag_INITS(RTC)) {
534526
/* INITS flag is set when the calendar has been initialiazed. This flag is
535527
* reset only on backup domain reset, so it can be read after a system
536528
* reset to check if the calendar has been initialized.
537529
*/
538-
k_mutex_unlock(&data->lock);
530+
k_spin_unlock(&data->lock, key);
539531
return -ENODATA;
540532
}
541533

@@ -555,7 +547,7 @@ static int rtc_stm32_get_time(const struct device *dev, struct rtc_time *timeptr
555547
} while (rtc_time != LL_RTC_TIME_Get(RTC));
556548
} while (rtc_date != LL_RTC_DATE_Get(RTC));
557549

558-
k_mutex_unlock(&data->lock);
550+
k_spin_unlock(&data->lock, key);
559551

560552
/* tm_year is the value since 1900 and Rtc year is from 2000 */
561553
timeptr->tm_year = bcd2bin(__LL_RTC_GET_YEAR(rtc_date)) + (RTC_YEAR_REF - TM_YEAR_REF);
@@ -757,7 +749,7 @@ static int rtc_stm32_alarm_get_time(const struct device *dev, uint16_t id, uint1
757749
return -EINVAL;
758750
}
759751

760-
k_mutex_lock(&data->lock, K_FOREVER);
752+
k_spinlock_key_t key = k_spin_lock(&data->lock);
761753

762754
if (id == RTC_STM32_ALRM_A) {
763755
p_rtc_alrm = &(data->rtc_alrm_a);
@@ -785,7 +777,7 @@ static int rtc_stm32_alarm_get_time(const struct device *dev, uint16_t id, uint1
785777
timeptr->tm_min, timeptr->tm_sec, *mask);
786778

787779
unlock:
788-
k_mutex_unlock(&data->lock);
780+
k_spin_unlock(&data->lock, key);
789781

790782
return err;
791783
}
@@ -799,7 +791,7 @@ static int rtc_stm32_alarm_set_time(const struct device *dev, uint16_t id, uint1
799791
LL_RTC_TimeTypeDef *p_ll_rtc_alrm_time;
800792
int err = 0;
801793

802-
k_mutex_lock(&data->lock, K_FOREVER);
794+
k_spinlock_key_t key = k_spin_lock(&data->lock);
803795

804796
if (id == RTC_STM32_ALRM_A) {
805797
p_rtc_alrm = &(data->rtc_alrm_a);
@@ -918,7 +910,7 @@ static int rtc_stm32_alarm_set_time(const struct device *dev, uint16_t id, uint1
918910
#endif /* RTC_STM32_BACKUP_DOMAIN_WRITE_PROTECTION */
919911

920912
unlock:
921-
k_mutex_unlock(&data->lock);
913+
k_spin_unlock(&data->lock, key);
922914

923915
if (id == RTC_STM32_ALRM_A) {
924916
LOG_DBG("Alarm A : %dh%dm%ds mask = 0x%x",
@@ -946,7 +938,7 @@ static int rtc_stm32_alarm_set_callback(const struct device *dev, uint16_t id,
946938
struct rtc_stm32_alrm *p_rtc_alrm;
947939
int err = 0;
948940

949-
k_mutex_lock(&data->lock, K_FOREVER);
941+
k_spinlock_key_t key = k_spin_lock(&data->lock);
950942

951943
if (id == RTC_STM32_ALRM_A) {
952944
p_rtc_alrm = &(data->rtc_alrm_a);
@@ -963,7 +955,7 @@ static int rtc_stm32_alarm_set_callback(const struct device *dev, uint16_t id,
963955
p_rtc_alrm->user_data = user_data;
964956

965957
unlock:
966-
k_mutex_unlock(&data->lock);
958+
k_spin_unlock(&data->lock, key);
967959

968960
return err;
969961
}
@@ -974,7 +966,7 @@ static int rtc_stm32_alarm_is_pending(const struct device *dev, uint16_t id)
974966
struct rtc_stm32_alrm *p_rtc_alrm;
975967
int ret = 0;
976968

977-
k_mutex_lock(&data->lock, K_FOREVER);
969+
k_spinlock_key_t key = k_spin_lock(&data->lock);
978970

979971
if (id == RTC_STM32_ALRM_A) {
980972
p_rtc_alrm = &(data->rtc_alrm_a);
@@ -992,7 +984,7 @@ static int rtc_stm32_alarm_is_pending(const struct device *dev, uint16_t id)
992984
__enable_irq();
993985

994986
unlock:
995-
k_mutex_unlock(&data->lock);
987+
k_spin_unlock(&data->lock, key);
996988
return ret;
997989
}
998990
#endif /* STM32_RTC_ALARM_ENABLED */

0 commit comments

Comments
 (0)