esp32_rt_timer.c: Group static variables into a struct and fix naming

standard

Signed-off-by: Abdelatif Guettouche <abdelatif.guettouche@espressif.com>
This commit is contained in:
Abdelatif Guettouche 2021-09-10 10:51:54 +02:00 committed by Xiang Xiao
parent 0dff3f2762
commit 9e1d2ca95e

View File

@ -60,19 +60,28 @@
#define ESP32_TIMER_PRESCALER (APB_CLK_FREQ / (1000 * 1000)) #define ESP32_TIMER_PRESCALER (APB_CLK_FREQ / (1000 * 1000))
#define ESP32_RT_TIMER 0 /* Timer 0 */ #define ESP32_RT_TIMER 0 /* Timer 0 */
/****************************************************************************
* Private Types
****************************************************************************/
struct esp32_rt_priv_s
{
int pid;
sem_t toutsem;
struct list_node runlist;
struct list_node toutlist;
spinlock_t lock;
struct esp32_tim_dev_s *timer;
};
/**************************************************************************** /****************************************************************************
* Private Data * Private Data
****************************************************************************/ ****************************************************************************/
static int s_pid; static struct esp32_rt_priv_s g_rt_priv;
static sem_t s_toutsem;
static struct list_node s_runlist;
static struct list_node s_toutlist;
static spinlock_t g_lock;
static struct esp32_tim_dev_s *s_esp32_tim_dev;
/**************************************************************************** /****************************************************************************
* Private Function Prototypes * Private Function Prototypes
@ -102,7 +111,7 @@ static void start_rt_timer(struct rt_timer_s *timer,
struct rt_timer_s *p; struct rt_timer_s *p;
bool inserted = false; bool inserted = false;
uint64_t counter; uint64_t counter;
struct esp32_tim_dev_s *tim = s_esp32_tim_dev; struct esp32_rt_priv_s *priv = &g_rt_priv;
/* Only idle timer can be started */ /* Only idle timer can be started */
@ -110,7 +119,7 @@ static void start_rt_timer(struct rt_timer_s *timer,
{ {
/* Calculate the timer's alarm value */ /* Calculate the timer's alarm value */
ESP32_TIM_GETCTR(tim, &counter); ESP32_TIM_GETCTR(priv->timer, &counter);
timer->timeout = timeout; timer->timeout = timeout;
timer->alarm = timer->timeout + counter; timer->alarm = timer->timeout + counter;
@ -127,7 +136,7 @@ static void start_rt_timer(struct rt_timer_s *timer,
* node of timer whose alarm value is larger than new one * node of timer whose alarm value is larger than new one
*/ */
list_for_every_entry(&s_runlist, p, struct rt_timer_s, list) list_for_every_entry(&priv->runlist, p, struct rt_timer_s, list)
{ {
if (p->alarm > timer->alarm) if (p->alarm > timer->alarm)
{ {
@ -143,19 +152,19 @@ static void start_rt_timer(struct rt_timer_s *timer,
if (!inserted) if (!inserted)
{ {
list_add_tail(&s_runlist, &timer->list); list_add_tail(&priv->runlist, &timer->list);
} }
timer->state = RT_TIMER_READY; timer->state = RT_TIMER_READY;
/* If this timer is at the head of the list */ /* If this timer is at the head of the list */
if (timer == container_of(s_runlist.next, struct rt_timer_s, list)) if (timer == container_of(priv->runlist.next, struct rt_timer_s, list))
{ {
/* Reset the hardware timer alarm */ /* Reset the hardware timer alarm */
ESP32_TIM_SETALRVL(tim, timer->alarm); ESP32_TIM_SETALRVL(priv->timer, timer->alarm);
ESP32_TIM_SETALRM(tim, true); ESP32_TIM_SETALRM(priv->timer, true);
} }
} }
} }
@ -180,7 +189,7 @@ static void stop_rt_timer(struct rt_timer_s *timer)
bool ishead; bool ishead;
struct rt_timer_s *next_timer; struct rt_timer_s *next_timer;
uint64_t alarm; uint64_t alarm;
struct esp32_tim_dev_s *tim = s_esp32_tim_dev; struct esp32_rt_priv_s *priv = &g_rt_priv;
/* "start" function can set the timer's repeat flag, and function "stop" /* "start" function can set the timer's repeat flag, and function "stop"
* should remove this flag. * should remove this flag.
@ -194,7 +203,7 @@ static void stop_rt_timer(struct rt_timer_s *timer)
{ {
/* Check if the timer is at the head of the list */ /* Check if the timer is at the head of the list */
if (timer == container_of(s_runlist.next, struct rt_timer_s, list)) if (timer == container_of(priv->runlist.next, struct rt_timer_s, list))
{ {
ishead = true; ishead = true;
} }
@ -210,19 +219,19 @@ static void stop_rt_timer(struct rt_timer_s *timer)
if (ishead) if (ishead)
{ {
if (!list_is_empty(&s_runlist)) if (!list_is_empty(&priv->runlist))
{ {
/* Set the value from the next timer as the new hardware timer /* Set the value from the next timer as the new hardware timer
* alarm value * alarm value
*/ */
next_timer = container_of(s_runlist.next, next_timer = container_of(priv->runlist.next,
struct rt_timer_s, struct rt_timer_s,
list); list);
alarm = next_timer->alarm; alarm = next_timer->alarm;
ESP32_TIM_SETALRVL(tim, alarm); ESP32_TIM_SETALRVL(priv->timer, alarm);
ESP32_TIM_SETALRM(tim, true); ESP32_TIM_SETALRM(priv->timer, true);
} }
} }
} }
@ -247,8 +256,9 @@ static void stop_rt_timer(struct rt_timer_s *timer)
static void delete_rt_timer(struct rt_timer_s *timer) static void delete_rt_timer(struct rt_timer_s *timer)
{ {
irqstate_t flags; irqstate_t flags;
struct esp32_rt_priv_s *priv = &g_rt_priv;
flags = spin_lock_irqsave(&g_lock); flags = spin_lock_irqsave(&priv->lock);
if (timer->state == RT_TIMER_READY) if (timer->state == RT_TIMER_READY)
{ {
@ -263,11 +273,11 @@ static void delete_rt_timer(struct rt_timer_s *timer)
goto exit; goto exit;
} }
list_add_after(&s_toutlist, &timer->list); list_add_after(&priv->toutlist, &timer->list);
timer->state = RT_TIMER_DELETE; timer->state = RT_TIMER_DELETE;
exit: exit:
spin_unlock_irqrestore(&g_lock, flags); spin_unlock_irqrestore(&priv->lock, flags);
} }
/**************************************************************************** /****************************************************************************
@ -292,27 +302,28 @@ static int rt_timer_thread(int argc, char *argv[])
irqstate_t flags; irqstate_t flags;
struct rt_timer_s *timer; struct rt_timer_s *timer;
enum rt_timer_state_e raw_state; enum rt_timer_state_e raw_state;
struct esp32_rt_priv_s *priv = &g_rt_priv;
while (1) while (1)
{ {
/* Waiting for all the timers to time out */ /* Waiting for all the timers to time out */
ret = nxsem_wait(&s_toutsem); ret = nxsem_wait(&priv->toutsem);
if (ret) if (ret)
{ {
tmrerr("ERROR: Wait s_toutsem error=%d\n", ret); tmrerr("ERROR: Wait priv->toutsem error=%d\n", ret);
assert(0); assert(0);
} }
flags = spin_lock_irqsave(&g_lock); flags = spin_lock_irqsave(&priv->lock);
/* Process all the timers in the list */ /* Process all the timers in the list */
while (!list_is_empty(&s_toutlist)) while (!list_is_empty(&priv->toutlist))
{ {
/* Get the first timer in the list */ /* Get the first timer in the list */
timer = container_of(s_toutlist.next, struct rt_timer_s, list); timer = container_of(priv->toutlist.next, struct rt_timer_s, list);
/* Cache the raw state to decide how to deal with this timer */ /* Cache the raw state to decide how to deal with this timer */
@ -326,7 +337,7 @@ static int rt_timer_thread(int argc, char *argv[])
timer->state = RT_TIMER_IDLE; timer->state = RT_TIMER_IDLE;
spin_unlock_irqrestore(&g_lock, flags); spin_unlock_irqrestore(&priv->lock, flags);
if (raw_state == RT_TIMER_TIMEOUT) if (raw_state == RT_TIMER_TIMEOUT)
{ {
@ -339,7 +350,7 @@ static int rt_timer_thread(int argc, char *argv[])
/* Enter critical section for next scanning list */ /* Enter critical section for next scanning list */
flags = spin_lock_irqsave(&g_lock); flags = spin_lock_irqsave(&priv->lock);
if (raw_state == RT_TIMER_TIMEOUT) if (raw_state == RT_TIMER_TIMEOUT)
{ {
@ -352,7 +363,7 @@ static int rt_timer_thread(int argc, char *argv[])
} }
} }
spin_unlock_irqrestore(&g_lock, flags); spin_unlock_irqrestore(&priv->lock, flags);
} }
return 0; return 0;
@ -380,21 +391,21 @@ static int rt_timer_isr(int irq, void *context, void *arg)
struct rt_timer_s *timer; struct rt_timer_s *timer;
uint64_t alarm; uint64_t alarm;
uint64_t counter; uint64_t counter;
struct esp32_tim_dev_s *tim = s_esp32_tim_dev; struct esp32_rt_priv_s *priv = &g_rt_priv;
/* Clear interrupt register status */ /* Clear interrupt register status */
ESP32_TIM_ACKINT(tim); ESP32_TIM_ACKINT(priv->timer);
/* Wake up the thread to process timed-out timers */ /* Wake up the thread to process timed-out timers */
nxsem_post(&s_toutsem); nxsem_post(&priv->toutsem);
flags = spin_lock_irqsave(&g_lock); flags = spin_lock_irqsave(&priv->lock);
/* Check if there is a timer running */ /* Check if there is a timer running */
if (!list_is_empty(&s_runlist)) if (!list_is_empty(&priv->runlist))
{ {
/** /**
* When stop/delete timer, in the same time the hardware timer * When stop/delete timer, in the same time the hardware timer
@ -402,8 +413,8 @@ static int rt_timer_isr(int irq, void *context, void *arg)
* from running list, so the 1st timer is not which triggers. * from running list, so the 1st timer is not which triggers.
*/ */
timer = container_of(s_runlist.next, struct rt_timer_s, list); timer = container_of(priv->runlist.next, struct rt_timer_s, list);
ESP32_TIM_GETCTR(tim, &counter); ESP32_TIM_GETCTR(priv->timer, &counter);
if (timer->alarm <= counter) if (timer->alarm <= counter)
{ {
/* Remove the first timer in the running list and add it to /* Remove the first timer in the running list and add it to
@ -415,24 +426,25 @@ static int rt_timer_isr(int irq, void *context, void *arg)
list_delete(&timer->list); list_delete(&timer->list);
timer->state = RT_TIMER_TIMEOUT; timer->state = RT_TIMER_TIMEOUT;
list_add_after(&s_toutlist, &timer->list); list_add_after(&priv->toutlist, &timer->list);
/* Check if there is a timer running */ /* Check if there is a timer running */
if (!list_is_empty(&s_runlist)) if (!list_is_empty(&priv->runlist))
{ {
/* Reset hardware timer alarm with next timer's alarm value */ /* Reset hardware timer alarm with next timer's alarm value */
timer = container_of(s_runlist.next, struct rt_timer_s, list); timer = container_of(priv->runlist.next,
struct rt_timer_s, list);
alarm = timer->alarm; alarm = timer->alarm;
ESP32_TIM_SETALRVL(tim, alarm); ESP32_TIM_SETALRVL(priv->timer, alarm);
ESP32_TIM_SETALRM(tim, true); ESP32_TIM_SETALRM(priv->timer, true);
} }
} }
} }
spin_unlock_irqrestore(&g_lock, flags); spin_unlock_irqrestore(&priv->lock, flags);
return 0; return 0;
} }
@ -500,13 +512,14 @@ void rt_timer_start(struct rt_timer_s *timer,
bool repeat) bool repeat)
{ {
irqstate_t flags; irqstate_t flags;
struct esp32_rt_priv_s *priv = &g_rt_priv;
flags = spin_lock_irqsave(&g_lock); flags = spin_lock_irqsave(&priv->lock);
stop_rt_timer(timer); stop_rt_timer(timer);
start_rt_timer(timer, timeout, repeat); start_rt_timer(timer, timeout, repeat);
spin_unlock_irqrestore(&g_lock, flags); spin_unlock_irqrestore(&priv->lock, flags);
} }
/**************************************************************************** /****************************************************************************
@ -526,10 +539,11 @@ void rt_timer_start(struct rt_timer_s *timer,
void rt_timer_stop(struct rt_timer_s *timer) void rt_timer_stop(struct rt_timer_s *timer)
{ {
irqstate_t flags; irqstate_t flags;
struct esp32_rt_priv_s *priv = &g_rt_priv;
flags = spin_lock_irqsave(&g_lock); flags = spin_lock_irqsave(&priv->lock);
stop_rt_timer(timer); stop_rt_timer(timer);
spin_unlock_irqrestore(&g_lock, flags); spin_unlock_irqrestore(&priv->lock, flags);
} }
/**************************************************************************** /****************************************************************************
@ -568,9 +582,9 @@ void rt_timer_delete(struct rt_timer_s *timer)
uint64_t IRAM_ATTR rt_timer_time_us(void) uint64_t IRAM_ATTR rt_timer_time_us(void)
{ {
uint64_t counter; uint64_t counter;
struct esp32_tim_dev_s *tim = s_esp32_tim_dev; struct esp32_rt_priv_s *priv = &g_rt_priv;
ESP32_TIM_GETCTR(tim, &counter); ESP32_TIM_GETCTR(priv->timer, &counter);
return counter; return counter;
} }
@ -593,13 +607,13 @@ uint64_t IRAM_ATTR rt_timer_get_alarm(void)
{ {
irqstate_t flags; irqstate_t flags;
uint64_t counter; uint64_t counter;
struct esp32_tim_dev_s *tim = s_esp32_tim_dev; struct esp32_rt_priv_s *priv = &g_rt_priv;
uint64_t alarm_value = 0; uint64_t alarm_value = 0;
flags = spin_lock_irqsave(&g_lock); flags = spin_lock_irqsave(&priv->lock);
ESP32_TIM_GETCTR(tim, &counter); ESP32_TIM_GETCTR(priv->timer, &counter);
ESP32_TIM_GETALRVL(tim, &alarm_value); ESP32_TIM_GETALRVL(priv->timer, &alarm_value);
if (alarm_value <= counter) if (alarm_value <= counter)
{ {
@ -610,7 +624,7 @@ uint64_t IRAM_ATTR rt_timer_get_alarm(void)
alarm_value -= counter; alarm_value -= counter;
} }
spin_unlock_irqrestore(&g_lock, flags); spin_unlock_irqrestore(&priv->lock, flags);
return alarm_value; return alarm_value;
} }
@ -632,17 +646,17 @@ uint64_t IRAM_ATTR rt_timer_get_alarm(void)
void IRAM_ATTR rt_timer_calibration(uint64_t time_us) void IRAM_ATTR rt_timer_calibration(uint64_t time_us)
{ {
uint64_t counter; uint64_t counter;
struct esp32_tim_dev_s *tim = s_esp32_tim_dev; struct esp32_rt_priv_s *priv = &g_rt_priv;
irqstate_t flags; irqstate_t flags;
flags = spin_lock_irqsave(&g_lock); flags = spin_lock_irqsave(&priv->lock);
ESP32_TIM_GETCTR(tim, &counter); ESP32_TIM_GETCTR(priv->timer, &counter);
counter += time_us; counter += time_us;
ESP32_TIM_SETCTR(tim, counter); ESP32_TIM_SETCTR(priv->timer, counter);
ESP32_TIM_RLD_NOW(tim); ESP32_TIM_RLD_NOW(priv->timer);
spin_unlock_irqrestore(&g_lock, flags); spin_unlock_irqrestore(&priv->lock, flags);
} }
/**************************************************************************** /****************************************************************************
@ -664,6 +678,7 @@ int esp32_rt_timer_init(void)
int pid; int pid;
irqstate_t flags; irqstate_t flags;
struct esp32_tim_dev_s *tim; struct esp32_tim_dev_s *tim;
struct esp32_rt_priv_s *priv = &g_rt_priv;
tim = esp32_tim_init(ESP32_RT_TIMER); tim = esp32_tim_init(ESP32_RT_TIMER);
if (!tim) if (!tim)
@ -672,7 +687,7 @@ int esp32_rt_timer_init(void)
return -EINVAL; return -EINVAL;
} }
nxsem_init(&s_toutsem, 0, 0); nxsem_init(&priv->toutsem, 0, 0);
pid = kthread_create(RT_TIMER_TASK_NAME, pid = kthread_create(RT_TIMER_TASK_NAME,
RT_TIMER_TASK_PRIORITY, RT_TIMER_TASK_PRIORITY,
@ -686,13 +701,13 @@ int esp32_rt_timer_init(void)
return pid; return pid;
} }
list_initialize(&s_runlist); list_initialize(&priv->runlist);
list_initialize(&s_toutlist); list_initialize(&priv->toutlist);
s_esp32_tim_dev = tim; priv->timer = tim;
s_pid = pid; priv->pid = pid;
flags = spin_lock_irqsave(&g_lock); flags = spin_lock_irqsave(&priv->lock);
/* ESP32 hardware timer configuration: /* ESP32 hardware timer configuration:
* - 1 counter = 1us * - 1 counter = 1us
@ -710,7 +725,7 @@ int esp32_rt_timer_init(void)
ESP32_TIM_START(tim); ESP32_TIM_START(tim);
spin_unlock_irqrestore(&g_lock, flags); spin_unlock_irqrestore(&priv->lock, flags);
return 0; return 0;
} }
@ -732,16 +747,17 @@ int esp32_rt_timer_init(void)
void esp32_rt_timer_deinit(void) void esp32_rt_timer_deinit(void)
{ {
irqstate_t flags; irqstate_t flags;
struct esp32_rt_priv_s *priv = &g_rt_priv;
flags = spin_lock_irqsave(&g_lock); flags = spin_lock_irqsave(&priv->lock);
ESP32_TIM_STOP(s_esp32_tim_dev); ESP32_TIM_STOP(priv->timer);
esp32_tim_deinit(s_esp32_tim_dev); esp32_tim_deinit(priv->timer);
s_esp32_tim_dev = NULL; priv->timer = NULL;
spin_unlock_irqrestore(&g_lock, flags); spin_unlock_irqrestore(&priv->lock, flags);
kthread_delete(s_pid); kthread_delete(priv->pid);
nxsem_destroy(&s_toutsem); nxsem_destroy(&priv->toutsem);
} }