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 <hujun5@xiaomi.com>
This commit is contained in:
hujun5 2024-12-17 15:37:28 +08:00 committed by Xiang Xiao
parent ea020a4eae
commit e3717b3086
2 changed files with 32 additions and 26 deletions

View file

@ -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 struct esp32_rtc_backup_s *g_rtc_save;
static bool g_rt_timer_enabled = false; static bool g_rt_timer_enabled = false;
static spinlock_t g_rtc_lock = SP_UNLOCKED;
/**************************************************************************** /****************************************************************************
* Public Data * Public Data
@ -1913,7 +1914,7 @@ time_t up_rtc_time(void)
uint64_t time_us; uint64_t time_us;
irqstate_t flags; 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 /* NOTE: RT-Timer starts to work after the board is initialized, and the
* RTC controller starts works after up_rtc_initialize is initialized. * 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(); 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); 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; uint64_t rtc_offset_us;
DEBUGASSERT(ts != NULL && ts->tv_nsec < NSEC_PER_SEC); 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 + now_us = ((uint64_t) ts->tv_sec) * USEC_PER_SEC +
ts->tv_nsec / NSEC_PER_USEC; ts->tv_nsec / NSEC_PER_USEC;
@ -1990,7 +1991,7 @@ int up_rtc_settime(const struct timespec *ts)
g_rtc_save->offset = 0; g_rtc_save->offset = 0;
esp32_rtc_set_boot_time(rtc_offset_us); esp32_rtc_set_boot_time(rtc_offset_us);
spin_unlock_irqrestore(NULL, flags); spin_unlock_irqrestore(&g_rtc_lock, flags);
return OK; return OK;
} }
@ -2063,7 +2064,7 @@ int up_rtc_gettime(struct timespec *tp)
irqstate_t flags; irqstate_t flags;
uint64_t time_us; uint64_t time_us;
flags = spin_lock_irqsave(NULL); flags = spin_lock_irqsave(&g_rtc_lock);
if (g_rt_timer_enabled == true) 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_sec = time_us / USEC_PER_SEC;
tp->tv_nsec = (time_us % USEC_PER_SEC) * NSEC_PER_USEC; 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; return OK;
} }
@ -2121,7 +2122,7 @@ int up_rtc_setalarm(struct alm_setalarm_s *alminfo)
{ {
/* Create the RT-Timer alarm */ /* Create the RT-Timer alarm */
flags = spin_lock_irqsave(NULL); flags = spin_lock_irqsave(&g_rtc_lock);
if (cbinfo->alarm_hdl == NULL) if (cbinfo->alarm_hdl == NULL)
{ {
@ -2132,7 +2133,7 @@ int up_rtc_setalarm(struct alm_setalarm_s *alminfo)
if (ret < 0) if (ret < 0)
{ {
rtcerr("ERROR: Failed to create rt_timer error=%d\n", ret); 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; return ret;
} }
} }
@ -2153,7 +2154,7 @@ int up_rtc_setalarm(struct alm_setalarm_s *alminfo)
ret = OK; ret = OK;
} }
spin_unlock_irqrestore(NULL, flags); spin_unlock_irqrestore(&g_rtc_lock, flags);
} }
return ret; return ret;
@ -2188,7 +2189,8 @@ int up_rtc_cancelalarm(enum alm_id_e alarmid)
if (cbinfo->ac_cb != NULL) if (cbinfo->ac_cb != NULL)
{ {
flags = spin_lock_irqsave(NULL); sched_lock();
flags = spin_lock_irqsave(&g_rtc_lock);
/* Stop and delete the alarm */ /* Stop and delete the alarm */
@ -2199,7 +2201,8 @@ int up_rtc_cancelalarm(enum alm_id_e alarmid)
cbinfo->deadline_us = 0; cbinfo->deadline_us = 0;
cbinfo->alarm_hdl = NULL; cbinfo->alarm_hdl = NULL;
spin_unlock_irqrestore(NULL, flags); spin_unlock_irqrestore(&g_rtc_lock, flags);
sched_unlock();
ret = OK; ret = OK;
} }
@ -2230,7 +2233,7 @@ int up_rtc_rdalarm(struct timespec *tp, uint32_t alarmid)
DEBUGASSERT((RTC_ALARM0 <= alarmid) && DEBUGASSERT((RTC_ALARM0 <= alarmid) &&
(alarmid < RTC_ALARM_LAST)); (alarmid < RTC_ALARM_LAST));
flags = spin_lock_irqsave(NULL); flags = spin_lock_irqsave(&g_rtc_lock);
/* Get the alarm according to the alarmid */ /* 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 + tp->tv_nsec = ((rt_timer_time_us() + g_rtc_save->offset +
cbinfo->deadline_us) % USEC_PER_SEC) * NSEC_PER_USEC; cbinfo->deadline_us) % USEC_PER_SEC) * NSEC_PER_USEC;
spin_unlock_irqrestore(NULL, flags); spin_unlock_irqrestore(&g_rtc_lock, flags);
return OK; return OK;
} }

View file

@ -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 struct esp32s2_rtc_backup_s *g_rtc_save;
static bool g_rt_timer_enabled = false; static bool g_rt_timer_enabled = false;
static spinlock_t g_rtc_lock = SP_UNLOCKED;
/**************************************************************************** /****************************************************************************
* Private Function Prototypes * Private Function Prototypes
@ -2420,7 +2421,7 @@ time_t up_rtc_time(void)
uint64_t time_us; uint64_t time_us;
irqstate_t flags; 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 /* NOTE: RT-Timer starts to work after the board is initialized, and the
* RTC controller starts works after up_rtc_initialize is initialized. * 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(); 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); 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; uint64_t rtc_offset_us;
DEBUGASSERT(ts != NULL && ts->tv_nsec < NSEC_PER_SEC); 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 + now_us = ((uint64_t) ts->tv_sec) * USEC_PER_SEC +
ts->tv_nsec / NSEC_PER_USEC; ts->tv_nsec / NSEC_PER_USEC;
@ -2496,7 +2497,7 @@ int up_rtc_settime(const struct timespec *ts)
g_rtc_save->offset = 0; g_rtc_save->offset = 0;
esp32s2_rtc_set_boot_time(rtc_offset_us); esp32s2_rtc_set_boot_time(rtc_offset_us);
spin_unlock_irqrestore(NULL, flags); spin_unlock_irqrestore(&g_rtc_lock, flags);
return OK; return OK;
} }
@ -2562,7 +2563,7 @@ int up_rtc_gettime(struct timespec *tp)
irqstate_t flags; irqstate_t flags;
uint64_t time_us; uint64_t time_us;
flags = spin_lock_irqsave(NULL); flags = spin_lock_irqsave(&g_rtc_lock);
if (g_rt_timer_enabled == true) 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_sec = time_us / USEC_PER_SEC;
tp->tv_nsec = (time_us % USEC_PER_SEC) * NSEC_PER_USEC; 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; return OK;
} }
@ -2620,7 +2621,7 @@ int up_rtc_setalarm(struct alm_setalarm_s *alminfo)
{ {
/* Create the RT-Timer alarm */ /* Create the RT-Timer alarm */
flags = spin_lock_irqsave(NULL); flags = spin_lock_irqsave(&g_rtc_lock);
if (cbinfo->alarm_hdl == NULL) if (cbinfo->alarm_hdl == NULL)
{ {
@ -2631,7 +2632,7 @@ int up_rtc_setalarm(struct alm_setalarm_s *alminfo)
if (ret < 0) if (ret < 0)
{ {
rtcerr("ERROR: Failed to create rt_timer error=%d\n", ret); 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; return ret;
} }
} }
@ -2652,7 +2653,7 @@ int up_rtc_setalarm(struct alm_setalarm_s *alminfo)
ret = OK; ret = OK;
} }
spin_unlock_irqrestore(NULL, flags); spin_unlock_irqrestore(&g_rtc_lock, flags);
} }
return ret; return ret;
@ -2687,7 +2688,8 @@ int up_rtc_cancelalarm(enum alm_id_e alarmid)
if (cbinfo->ac_cb != NULL) if (cbinfo->ac_cb != NULL)
{ {
flags = spin_lock_irqsave(NULL); sched_lock();
flags = spin_lock_irqsave(&g_rtc_lock);
/* Stop and delete the alarm */ /* Stop and delete the alarm */
@ -2698,7 +2700,8 @@ int up_rtc_cancelalarm(enum alm_id_e alarmid)
cbinfo->deadline_us = 0; cbinfo->deadline_us = 0;
cbinfo->alarm_hdl = NULL; cbinfo->alarm_hdl = NULL;
spin_unlock_irqrestore(NULL, flags); spin_unlock_irqrestore(&g_rtc_lock, flags);
sched_unlock();
ret = OK; ret = OK;
} }
@ -2729,7 +2732,7 @@ int up_rtc_rdalarm(struct timespec *tp, uint32_t alarmid)
DEBUGASSERT((RTC_ALARM0 <= alarmid) && DEBUGASSERT((RTC_ALARM0 <= alarmid) &&
(alarmid < RTC_ALARM_LAST)); (alarmid < RTC_ALARM_LAST));
flags = spin_lock_irqsave(NULL); flags = spin_lock_irqsave(&g_rtc_lock);
/* Get the alarm according to the alarmid */ /* 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 + tp->tv_nsec = ((rt_timer_time_us() + g_rtc_save->offset +
cbinfo->deadline_us) % USEC_PER_SEC) * NSEC_PER_USEC; cbinfo->deadline_us) % USEC_PER_SEC) * NSEC_PER_USEC;
spin_unlock_irqrestore(NULL, flags); spin_unlock_irqrestore(&g_rtc_lock, flags);
return OK; return OK;
} }