From e3717b308647551573ca928db70242318aab62d5 Mon Sep 17 00:00:00 2001 From: hujun5 Date: Tue, 17 Dec 2024 15:37:28 +0800 Subject: [PATCH] esp32_rtc: use small lock in arch/xtensa/src/esp32[s2]/esp32s2_rtc.c reason: We would like to replace the big lock with a small lock. Signed-off-by: hujun5 --- arch/xtensa/src/esp32/esp32_rtc.c | 29 +++++++++++++++------------ arch/xtensa/src/esp32s2/esp32s2_rtc.c | 29 +++++++++++++++------------ 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/arch/xtensa/src/esp32/esp32_rtc.c b/arch/xtensa/src/esp32/esp32_rtc.c index 779ced3df3..926c2f4aae 100644 --- a/arch/xtensa/src/esp32/esp32_rtc.c +++ b/arch/xtensa/src/esp32/esp32_rtc.c @@ -293,6 +293,7 @@ static RTC_DATA_ATTR struct esp32_rtc_backup_s rtc_saved_data; static struct esp32_rtc_backup_s *g_rtc_save; static bool g_rt_timer_enabled = false; +static spinlock_t g_rtc_lock = SP_UNLOCKED; /**************************************************************************** * Public Data @@ -1913,7 +1914,7 @@ time_t up_rtc_time(void) uint64_t time_us; irqstate_t flags; - flags = spin_lock_irqsave(NULL); + flags = spin_lock_irqsave(&g_rtc_lock); /* NOTE: RT-Timer starts to work after the board is initialized, and the * RTC controller starts works after up_rtc_initialize is initialized. @@ -1942,7 +1943,7 @@ time_t up_rtc_time(void) esp32_rtc_get_boot_time(); } - spin_unlock_irqrestore(NULL, flags); + spin_unlock_irqrestore(&g_rtc_lock, flags); return (time_t)(time_us / USEC_PER_SEC); } @@ -1970,7 +1971,7 @@ int up_rtc_settime(const struct timespec *ts) uint64_t rtc_offset_us; DEBUGASSERT(ts != NULL && ts->tv_nsec < NSEC_PER_SEC); - flags = spin_lock_irqsave(NULL); + flags = spin_lock_irqsave(&g_rtc_lock); now_us = ((uint64_t) ts->tv_sec) * USEC_PER_SEC + ts->tv_nsec / NSEC_PER_USEC; @@ -1990,7 +1991,7 @@ int up_rtc_settime(const struct timespec *ts) g_rtc_save->offset = 0; esp32_rtc_set_boot_time(rtc_offset_us); - spin_unlock_irqrestore(NULL, flags); + spin_unlock_irqrestore(&g_rtc_lock, flags); return OK; } @@ -2063,7 +2064,7 @@ int up_rtc_gettime(struct timespec *tp) irqstate_t flags; uint64_t time_us; - flags = spin_lock_irqsave(NULL); + flags = spin_lock_irqsave(&g_rtc_lock); if (g_rt_timer_enabled == true) { @@ -2078,7 +2079,7 @@ int up_rtc_gettime(struct timespec *tp) tp->tv_sec = time_us / USEC_PER_SEC; tp->tv_nsec = (time_us % USEC_PER_SEC) * NSEC_PER_USEC; - spin_unlock_irqrestore(NULL, flags); + spin_unlock_irqrestore(&g_rtc_lock, flags); return OK; } @@ -2121,7 +2122,7 @@ int up_rtc_setalarm(struct alm_setalarm_s *alminfo) { /* Create the RT-Timer alarm */ - flags = spin_lock_irqsave(NULL); + flags = spin_lock_irqsave(&g_rtc_lock); if (cbinfo->alarm_hdl == NULL) { @@ -2132,7 +2133,7 @@ int up_rtc_setalarm(struct alm_setalarm_s *alminfo) if (ret < 0) { rtcerr("ERROR: Failed to create rt_timer error=%d\n", ret); - spin_unlock_irqrestore(NULL, flags); + spin_unlock_irqrestore(&g_rtc_lock, flags); return ret; } } @@ -2153,7 +2154,7 @@ int up_rtc_setalarm(struct alm_setalarm_s *alminfo) ret = OK; } - spin_unlock_irqrestore(NULL, flags); + spin_unlock_irqrestore(&g_rtc_lock, flags); } return ret; @@ -2188,7 +2189,8 @@ int up_rtc_cancelalarm(enum alm_id_e alarmid) if (cbinfo->ac_cb != NULL) { - flags = spin_lock_irqsave(NULL); + sched_lock(); + flags = spin_lock_irqsave(&g_rtc_lock); /* Stop and delete the alarm */ @@ -2199,7 +2201,8 @@ int up_rtc_cancelalarm(enum alm_id_e alarmid) cbinfo->deadline_us = 0; cbinfo->alarm_hdl = NULL; - spin_unlock_irqrestore(NULL, flags); + spin_unlock_irqrestore(&g_rtc_lock, flags); + sched_unlock(); ret = OK; } @@ -2230,7 +2233,7 @@ int up_rtc_rdalarm(struct timespec *tp, uint32_t alarmid) DEBUGASSERT((RTC_ALARM0 <= alarmid) && (alarmid < RTC_ALARM_LAST)); - flags = spin_lock_irqsave(NULL); + flags = spin_lock_irqsave(&g_rtc_lock); /* Get the alarm according to the alarmid */ @@ -2241,7 +2244,7 @@ int up_rtc_rdalarm(struct timespec *tp, uint32_t alarmid) tp->tv_nsec = ((rt_timer_time_us() + g_rtc_save->offset + cbinfo->deadline_us) % USEC_PER_SEC) * NSEC_PER_USEC; - spin_unlock_irqrestore(NULL, flags); + spin_unlock_irqrestore(&g_rtc_lock, flags); return OK; } diff --git a/arch/xtensa/src/esp32s2/esp32s2_rtc.c b/arch/xtensa/src/esp32s2/esp32s2_rtc.c index 3e59c397c4..1cc3ea36f3 100644 --- a/arch/xtensa/src/esp32s2/esp32s2_rtc.c +++ b/arch/xtensa/src/esp32s2/esp32s2_rtc.c @@ -415,6 +415,7 @@ static RTC_DATA_ATTR struct esp32s2_rtc_backup_s rtc_saved_data; static struct esp32s2_rtc_backup_s *g_rtc_save; static bool g_rt_timer_enabled = false; +static spinlock_t g_rtc_lock = SP_UNLOCKED; /**************************************************************************** * Private Function Prototypes @@ -2420,7 +2421,7 @@ time_t up_rtc_time(void) uint64_t time_us; irqstate_t flags; - flags = spin_lock_irqsave(NULL); + flags = spin_lock_irqsave(&g_rtc_lock); /* NOTE: RT-Timer starts to work after the board is initialized, and the * RTC controller starts works after up_rtc_initialize is initialized. @@ -2448,7 +2449,7 @@ time_t up_rtc_time(void) time_us = esp32s2_rtc_get_time_us() + esp32s2_rtc_get_boot_time(); } - spin_unlock_irqrestore(NULL, flags); + spin_unlock_irqrestore(&g_rtc_lock, flags); return (time_t)(time_us / USEC_PER_SEC); } @@ -2476,7 +2477,7 @@ int up_rtc_settime(const struct timespec *ts) uint64_t rtc_offset_us; DEBUGASSERT(ts != NULL && ts->tv_nsec < NSEC_PER_SEC); - flags = spin_lock_irqsave(NULL); + flags = spin_lock_irqsave(&g_rtc_lock); now_us = ((uint64_t) ts->tv_sec) * USEC_PER_SEC + ts->tv_nsec / NSEC_PER_USEC; @@ -2496,7 +2497,7 @@ int up_rtc_settime(const struct timespec *ts) g_rtc_save->offset = 0; esp32s2_rtc_set_boot_time(rtc_offset_us); - spin_unlock_irqrestore(NULL, flags); + spin_unlock_irqrestore(&g_rtc_lock, flags); return OK; } @@ -2562,7 +2563,7 @@ int up_rtc_gettime(struct timespec *tp) irqstate_t flags; uint64_t time_us; - flags = spin_lock_irqsave(NULL); + flags = spin_lock_irqsave(&g_rtc_lock); if (g_rt_timer_enabled == true) { @@ -2577,7 +2578,7 @@ int up_rtc_gettime(struct timespec *tp) tp->tv_sec = time_us / USEC_PER_SEC; tp->tv_nsec = (time_us % USEC_PER_SEC) * NSEC_PER_USEC; - spin_unlock_irqrestore(NULL, flags); + spin_unlock_irqrestore(&g_rtc_lock, flags); return OK; } @@ -2620,7 +2621,7 @@ int up_rtc_setalarm(struct alm_setalarm_s *alminfo) { /* Create the RT-Timer alarm */ - flags = spin_lock_irqsave(NULL); + flags = spin_lock_irqsave(&g_rtc_lock); if (cbinfo->alarm_hdl == NULL) { @@ -2631,7 +2632,7 @@ int up_rtc_setalarm(struct alm_setalarm_s *alminfo) if (ret < 0) { rtcerr("ERROR: Failed to create rt_timer error=%d\n", ret); - spin_unlock_irqrestore(NULL, flags); + spin_unlock_irqrestore(&g_rtc_lock, flags); return ret; } } @@ -2652,7 +2653,7 @@ int up_rtc_setalarm(struct alm_setalarm_s *alminfo) ret = OK; } - spin_unlock_irqrestore(NULL, flags); + spin_unlock_irqrestore(&g_rtc_lock, flags); } return ret; @@ -2687,7 +2688,8 @@ int up_rtc_cancelalarm(enum alm_id_e alarmid) if (cbinfo->ac_cb != NULL) { - flags = spin_lock_irqsave(NULL); + sched_lock(); + flags = spin_lock_irqsave(&g_rtc_lock); /* Stop and delete the alarm */ @@ -2698,7 +2700,8 @@ int up_rtc_cancelalarm(enum alm_id_e alarmid) cbinfo->deadline_us = 0; cbinfo->alarm_hdl = NULL; - spin_unlock_irqrestore(NULL, flags); + spin_unlock_irqrestore(&g_rtc_lock, flags); + sched_unlock(); ret = OK; } @@ -2729,7 +2732,7 @@ int up_rtc_rdalarm(struct timespec *tp, uint32_t alarmid) DEBUGASSERT((RTC_ALARM0 <= alarmid) && (alarmid < RTC_ALARM_LAST)); - flags = spin_lock_irqsave(NULL); + flags = spin_lock_irqsave(&g_rtc_lock); /* Get the alarm according to the alarmid */ @@ -2740,7 +2743,7 @@ int up_rtc_rdalarm(struct timespec *tp, uint32_t alarmid) tp->tv_nsec = ((rt_timer_time_us() + g_rtc_save->offset + cbinfo->deadline_us) % USEC_PER_SEC) * NSEC_PER_USEC; - spin_unlock_irqrestore(NULL, flags); + spin_unlock_irqrestore(&g_rtc_lock, flags); return OK; }