Support sleep on idle.

This commit is contained in:
NIIBE Yutaka
2017-11-15 10:22:30 +09:00
parent 035057db24
commit f161928b0b
4 changed files with 41 additions and 7 deletions

View File

@@ -1,5 +1,9 @@
2017-11-15 NIIBE Yutaka <gniibe@fsij.org> 2017-11-15 NIIBE Yutaka <gniibe@fsij.org>
* chopstx.c (chx_init): Initialize sleep lock.
(chopstx_conf_idle): New.
* chopstx-cortex-m.c (idle): Support sleep.
* mcu/usb-stm32f103.c (usb_lld_event_handler): Fix suspend/resume. * mcu/usb-stm32f103.c (usb_lld_event_handler): Fix suspend/resume.
2017-11-14 NIIBE Yutaka <gniibe@fsij.org> 2017-11-14 NIIBE Yutaka <gniibe@fsij.org>

View File

@@ -232,12 +232,14 @@ chx_cpu_sched_unlock (void)
static void __attribute__((naked, used)) static void __attribute__((naked, used))
idle (void) idle (void)
{ {
#if defined(USE_WFI_FOR_IDLE) int sleep_enabled;
for (;;) for (;;)
asm volatile ("wfi" : : : "memory"); {
#else asm ("ldr %0, %1" : "=r" (sleep_enabled): "m" (chx_allow_sleep));
for (;;); if (sleep_enabled)
#endif asm volatile ("wfi" : : : "memory");
}
} }

View File

@@ -79,7 +79,11 @@ chx_fatal (uint32_t err_code)
#include "chopstx-cortex-m.h" #include "chopstx-cortex-m.h"
#endif #endif
/* RUNNING: the current thread. */ /* ALLOW_SLEEP for idle */
int chx_allow_sleep;
static struct chx_spinlock chx_enable_sleep_lock;
/* RUNNING: the current thread. */
struct chx_thread *running; struct chx_thread *running;
struct chx_queue { struct chx_queue {
@@ -441,6 +445,7 @@ chx_init (struct chx_thread *tp)
{ {
chx_prio_init (); chx_prio_init ();
chx_init_arch (tp); chx_init_arch (tp);
chx_spin_init (&chx_enable_sleep_lock);
q_ready.q.next = q_ready.q.prev = (struct chx_pq *)&q_ready.q; q_ready.q.next = q_ready.q.prev = (struct chx_pq *)&q_ready.q;
chx_spin_init (&q_ready.lock); chx_spin_init (&q_ready.lock);
@@ -1470,3 +1475,24 @@ chopstx_setpriority (chopstx_prio_t prio_new)
return prio_orig; return prio_orig;
} }
/**
* chopstx_conf_idle - Configure IDLE thread
* @enable_sleep: Enable sleep on idle or not
*
* If @enable_sleep is true, allow sleep for the idle thread.
* Return previous value of @enable_sleep.
*/
int
chopstx_conf_idle (int enable_sleep)
{
int r;
chx_spin_lock (&chx_enable_sleep_lock);
r = chx_allow_sleep;
chx_allow_sleep = enable_sleep;
chx_spin_unlock (&chx_enable_sleep_lock);
return r;
}

View File

@@ -1,7 +1,7 @@
/* /*
* chopstx.h - Threads and only threads. * chopstx.h - Threads and only threads.
* *
* Copyright (C) 2013, 2016 Flying Stone Technology * Copyright (C) 2013, 2016, 2017 Flying Stone Technology
* Author: NIIBE Yutaka <gniibe@fsij.org> * Author: NIIBE Yutaka <gniibe@fsij.org>
* *
* This file is a part of Chopstx, a thread library for embedded. * This file is a part of Chopstx, a thread library for embedded.
@@ -160,4 +160,6 @@ void chopstx_intr_wait (chopstx_intr_t *intr); /* DEPRECATED */
int chopstx_poll (uint32_t *usec_p, int n, struct chx_poll_head *pd_array[]); int chopstx_poll (uint32_t *usec_p, int n, struct chx_poll_head *pd_array[]);
int chopstx_conf_idle (int enable_sleep);
#define CHOPSTX_THREAD_SIZE 64 #define CHOPSTX_THREAD_SIZE 64