Page 1 of 1

Deep sleep within chibios

Posted: Thu Oct 15, 2015 3:31 pm
by flabbergast
I am trying to implement a USB device that would support USB suspend mechanism (e.g. when a PC goes to sleep, it sends "sleep" instruction to the device; when it wakes up it sends a "wakeup" packet). I've got most of this working - I mean I detect these packets OK. The remaining bit for me to figure out is how to decrease the power consumption during the sleep (the official requirement is something ridiculous like 0.5mA I think, which is hard to achieve, but anyway).

At the moment I'm running on kinetis devices, so the kernel uses tick-mode.

Here's what I was thinking that "could" work: upon entering the sleep mode, I would stop the kernel (chSysDisable or chSysSuspend?), disable SysTick, take care of enabled interrupts and subsystems, and enter one of the sleep modes on the MCU. The system would wake up through the USB interrupt on the "wakeup" packet - or every several ms using a timer and check for wakeup. On wakeup, I would re-enable systick and again take care of the subsystems and interrupts, and then enable the OS kernel back.

Is this scenario plausible, and/or what are the OS internals that can get messed up and/or need taking care of? Would this "confuse" the kernel too much? I mean to run this from a thread - should I explicitly "pause" the other thread(s), or would suspending the kernel be enough?
I'm sorry I don't understand the ChibiOS RT kernel internals well enough yet.
Thank you.

Re: Deep sleep within chibios

Posted: Thu Oct 15, 2015 3:33 pm
by Giovanni
That would work.

An alternate way would be to use the idle thread enter/leave macros and enter a sleep mode in the idle loop.

Giovanni

Re: Deep sleep within chibios

Posted: Thu Oct 15, 2015 3:53 pm
by flabbergast
Thanks!

With the idle hooks - wouldn't SysTick / kernel still fire up many times a second? I mean it would definitely decrease the power consumptions a lot, but...

Re: Deep sleep within chibios

Posted: Thu Oct 15, 2015 4:04 pm
by Giovanni
Not in tick-less mode. The timer is programmed to the next timer event, it is not periodic.

It will idle until the next timers expires OR some peripheral generates an interrupt that wakes up a thread.

Giovanni

Re: Deep sleep within chibios

Posted: Thu Oct 15, 2015 4:45 pm
by flabbergast
Sure, but I thought the tick-less mode is not available for Kinetis port yet. I'll have a second look. Thanks!

Re: Deep sleep within chibios

Posted: Thu Oct 15, 2015 10:05 pm
by alexblack
Hi.
flabbergast wrote:Thanks!

With the idle hooks - wouldn't SysTick / kernel still fire up many times a second? I mean it would definitely decrease the power consumptions a lot, but...

In most systems all clocks will stopped in deep sleep mode and thus SysTick will not fire at all in this case and will not work correctly. I used RTC periodic wakeups with period 10 ms on my STM32F303 and have ~20 uA of power consuption. I copied virtual timers core and renamed it to sleep_timer_t. This gave me two types of timers: regular (high resolution) - which work only in active mode, and "sleep" timers, which work always but with low resolution.
http://forum.chibios.org/phpbb/viewtopic.php?f=24&t=2858 - this is my code of IDLE hook.