Page 1 of 2

Sleeping and waking up from interrupts

Posted: Sat Sep 17, 2011 10:35 am
by DrunkenDonkey
Hello again, after like 7 hours trying to make it work, I have faced a defeat. I have just received my Olimex STM32-P103 board and very happy about it, especially how easy the start with ChibiOS is, but got stuck with sleeping.
Deep sleep with waking up from RTC works great (although restarts the cpu on wakeup, I guess that is normal), adding

Code: Select all

/* Keep debugger connection during SLEEP (debugging) */
DBGMCU_Config(DBGMCU_SLEEP, ENABLE);
DBGMCU_Config(DBGMCU_STOP, ENABLE);
DBGMCU_Config(DBGMCU_STANDBY, ENABLE);
/* IWDG stopped when core is halted (debugging) */
DBGMCU_Config(DBGMCU_IWDG_STOP, ENABLE);

even makes the debugger (Olimex ARM-JTAG-EW) not crashing when the sleep is enabled.

On to the interrupts, I used the exti test app modified a bit

Code: Select all

static const EXTConfig extcfg = {
  {
   {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART, extcb1},
   {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART, extcb3},
   {EXT_CH_MODE_DISABLED, NULL},
   {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART, extcb3},
   {EXT_CH_MODE_DISABLED, NULL},
   {EXT_CH_MODE_DISABLED, NULL},
   {EXT_CH_MODE_DISABLED, NULL},
   {EXT_CH_MODE_DISABLED, NULL},
   {EXT_CH_MODE_DISABLED, NULL},
   {EXT_CH_MODE_DISABLED, NULL},
   {EXT_CH_MODE_DISABLED, NULL},
   {EXT_CH_MODE_DISABLED, NULL},
   {EXT_CH_MODE_RISING_EDGE | EXT_CH_MODE_AUTOSTART, extcb2},
   {EXT_CH_MODE_DISABLED, NULL},
   {EXT_CH_MODE_DISABLED, NULL},
   {EXT_CH_MODE_DISABLED, NULL},
  },
  EXT_MODE_EXTI(EXT_MODE_GPIOA,
               EXT_MODE_GPIOA,
                0,
               EXT_MODE_GPIOA,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                EXT_MODE_GPIOC,
                0,
                0,
                0)
};
extStart(&EXTD1, &extcfg);


The interrupts are being called, working great, up to the moment I try to do any sleep:

Code: Select all

__WFI();

crashes the debug into some vector tables code, nothing, even the RTC can wake it up from there

Code: Select all

//RTC driver test code
PWR->CR |= (PWR_CR_PDDS | PWR_CR_LPDS | PWR_CR_CSBF | PWR_CR_CWUF);
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
__WFI();

does not crash, button or data on the serial tx do nothing, RTC successfully wakes it up with restart

Code: Select all

PWR_EnterSTOPMode(PWR_Regulator_ON,PWR_STOPEntry_WFI);

crashes again

All these are tested with and without chSysLock(); in advance, and with interrupt priorities in the mcuconfig ranging from 0 to 6, did experiments with various codes around and I know I'm missing something very basic, but can't figure out what is it. Ideally, I'd like to go into sleep with most peripherals disabled and wake up on usart, but sleeping tries so far have resulted in nothing but crashes. Can anyone give a hint?

Re: Sleeping and waking up from interrupts

Posted: Sat Sep 17, 2011 10:45 am
by Giovanni
Both EXT and RTC are experimental drivers, I haven't played much with the sleep modes of the STM32 so far, may be Barthess will be able to give you an hint about the problem, he posted something about testing sleep modes in the development forum (moving this thread there).

Giovanni

Re: Sleeping and waking up from interrupts

Posted: Sat Sep 17, 2011 9:11 pm
by barthess
Hi, DrunkenDonkey
You want to quick wake up from low power mode when bit drops to UART. Am I right?

Re: Sleeping and waking up from interrupts

Posted: Sun Sep 18, 2011 12:33 am
by barthess
Hm... it works.
Look in testhal/EXT_WAKEUP.

or data on the serial tx do nothing

You must set EXT interrupt on rx pin, not tx. May be this is your problem.

Next moment from p.4.3.4 of RM0008.pdf:
When exiting Stop mode by issuing an interrupt or a wakeup event, the HSI RC oscillator is
selected as system clock.

You must reinit main clock.

Hint: in such experimental driver do not use library functions like "PWR_EnterSTOPMode(PWR_Regulator_ON,PWR_STOPEntry_WFI);" Much better manually write bytes to registers to understand what are you doing.

Also, try to not use:

Code: Select all

/* Keep debugger connection during SLEEP (debugging) */
DBGMCU_Config(DBGMCU_SLEEP, ENABLE);
DBGMCU_Config(DBGMCU_STOP, ENABLE);
DBGMCU_Config(DBGMCU_STANDBY, ENABLE);
/* IWDG stopped when core is halted (debugging) */
DBGMCU_Config(DBGMCU_IWDG_STOP, ENABLE);

Re: Sleeping and waking up from interrupts

Posted: Sun Sep 18, 2011 7:10 am
by DrunkenDonkey
You are the man :D It really works, I moved it to usart2, as it is the only one wired on my board. Now's the time to see why it works in yours and not in my project and why I crash into some vectors the moment I use WFI. Thanks a lot, it is a great example not only about sleeping and waking up ;)

Re: Sleeping and waking up from interrupts

Posted: Sun Sep 18, 2011 11:34 pm
by barthess
DrunkenDonkey,
Do not forget to tell us what was your problem, it is interesting.

I have play with my board and detect that MCU wakes up fast enough to not drop first byte on the 38400 baudrate (higher not tested).
Preconditions:
- main clock source must be HSI _without_ PLL.
- internal LDO in normal power mode.
Example changed to meet that preconditions (rev.3343).

Go ahead and experiment.

Re: Sleeping and waking up from interrupts

Posted: Mon Sep 19, 2011 9:55 pm
by DrunkenDonkey
Hey barthess,

I think the problem was too much testing code and leftover values in the registers that are not reset after reloading the program(?) After powering down and up the board, and using your code I don't crash anymore in vectors even if using simple WFI. Also, when it goes to deep sleep, it does not crash the debugger even without DBGMCU_Config functions. Totally weird, but I like it when it is working :D
My tests, based on simple thread doing:

Code: Select all

while (TRUE)
{
u8 b=sdGet(&SD2);
sdPut(&SD2,b);
}


Showed 7 bytes lost upon wakeup on 115200, in reality, I'll want 57600 so I was just going to test it (can afford 1 lost byte, as it is sync one, can try to afford another, as its value is hardcoded, just won't know where the packet starts), updated the trunk to see your changes to the example and now none of my projects work, they all go endless loop in halInit, the RTC part:

Code: Select all

  while (!(RTC->CRL & RTC_CRL_RSF))

located in rtc_lld.c line 122

I'll try to check what has changed, but won't be long, have to get up early in the morning.

All in all, it is great fun to play with embedded development, haven't faced so many new stuff in years ;)

Re: Sleeping and waking up from interrupts

Posted: Mon Sep 19, 2011 10:30 pm
by DrunkenDonkey
Got it, few changes in the mcuconfig resulted in this... but man... I don't know what you did or more importantly how you did it (I suspect no longer reinitting clocks in my situation), but it is freaking awesome now - even on 115200 I don't lose a single byte!! It manages to wake up during the start bit of the first byte, thats just hard to believe... I think I can happily go sleep now ;)

Re: Sleeping and waking up from interrupts

Posted: Tue Sep 20, 2011 8:55 am
by barthess
RTC driver was broken, fixed in rev.3357
Also I have try to put internal LDO in low power mode when MCU core stopped -- on 115200 wakes without problems.
DrunkenDonkey wrote:I suspect no longer reinitting clocks in my situation

It is better to call stm32_clock_init() (even if use only HSI clock) just to be safe.

Can you measure power consumption in stop and normal mode on your board? My home brewed device have too much unconnectable parts to perform objective measurements.

Wake up pin on your board connected to CTS line of RS-232 port, you can use it for waking up too.

Re: Sleeping and waking up from interrupts

Posted: Tue Sep 20, 2011 1:52 pm
by barthess
Finally I have found timing characteristics for stm32 in datasheet "Medium-density performance line ARM-based 32-bit MCU".

Wakeup from Sleep mode -- 1.8 μs
Wakeup from Stop mode (regulator in run mode) -- 3.6 μs
Wakeup from Stop mode (regulator in low power mode) -- 5.4 μs
Wakeup from Standby mode -- 50 μs

In 115200 bauds you have about 8.7 uS on start bit. Now look in wikipedia:
All operations of the UART hardware are controlled by a clock signal which runs at a multiple (say, 16) of the data rate - each data bit is as long as 16 clock pulses. The receiver tests the state of the incoming signal on each clock pulse, looking for the beginning of the start bit. If the apparent start bit lasts at least one-half of the bit time, it is valid and signals the start of a new character. If not, the spurious pulse is ignored.

5.4 > 8.7 / 2
so it is better to slow down port speed if you plan to put regulator in low power mode. It works in my case, but may cause unpredictable errors.