Timer and Interrupt used together to leave sleep mode

Maple Mini, Maple Rev3, Maple Rev 5 and Maple Ret 6, iTead Maple etc
Leonardo_evaldt
Posts: 19
Joined: Wed Jul 19, 2017 3:30 pm

Re: Timer and Interrupt used together to leave sleep mode

Post by Leonardo_evaldt » Fri Sep 15, 2017 1:45 am

Dont worry. The ADC is not important now. I can test without it.
I'm reading the function sleepAndWakeUp. It uses asm(" wfi"). So, seems to me that this function also uses interrupts to leave sleep mode.
So I decided to test the "stm32_periodic_stop.ino" exemple. I added a blink test. the problem is that the led is not blinking.
This is the code:

Code: Select all

#include <STM32Sleep.h>
#include <RTClock.h>

RTClock rt(RTCSEL_LSE);
long int alarmDelay = 5;

void setup() {
  // Disable ADC to save power
  adc_disable_all();
  pinMode(LED_BUILTIN, OUTPUT);

  // Set all GPIO pins to Analog input to save power (this disables pretty
  // much all I/O incl. Serial)
  setGPIOModeToAllPins(GPIO_INPUT_ANALOG);

  sleepAndWakeUp(STOP, &rt, alarmDelay);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000); 
  sleepAndWakeUp(STOP, &rt, alarmDelay);
}
it compiles and writes to the flash memory. But no blink at all.
If I remove sleepAndWakeUp(STOP, &rt, alarmDelay) from setup(){} the led blinks once, then stops. After stop I can hear that sound when you disconnect the USB port from you PC.So It seems that the board enter in loop(){}, then blink once then enter in sleep mode, then disable the peripherals including UART (that is the reason why i hear the disconnect USB sound).
I tried to run my own code with the sleepAndWakeUp function but happens the same. It runs fine but when it reaches in the sleepAndWakeUp function, it stops and I hear the USB disconnect sound. Seems that it isn't leaving the standby mode.
I don't understand why it isn't leaving the sleep mode?

User avatar
RogerClark
Posts: 7147
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Timer and Interrupt used together to leave sleep mode

Post by RogerClark » Fri Sep 15, 2017 3:40 am

After stop I can hear that sound when you disconnect the USB port from you PC
USB operation requires the MCU to constantly handle ISR requests from the host PC.

Sleeping will disconnect USB.

You could try calling Serial.begin() again when waking from sleep but there is no guarantee that this works, because when powered via USB its not normal to sleep the MCU as its pointless as power consumption would not be an issue

Leonardo_evaldt
Posts: 19
Joined: Wed Jul 19, 2017 3:30 pm

Re: Timer and Interrupt used together to leave sleep mode

Post by Leonardo_evaldt » Fri Sep 15, 2017 4:43 am

RogerClark wrote:
Fri Sep 15, 2017 3:40 am
Sleeping will disconnect USB.
I suspected. Thank you for confirming, Roger.
RogerClark wrote:
Fri Sep 15, 2017 3:40 am
You could try calling Serial.begin() again when waking from sleep but there is no guarantee that this works, because when powered via USB its not normal to sleep the MCU as its pointless as power consumption would not be an issue
Sorry, I dont understand. I'm not using a terminal. I am programming the flash. How I supposed to calling Serial.begin()? Can I put Serial.begin() inside loop(){...} Like this:

Code: Select all

loop(){
...
sleepAndWakeUp(STOP, &rt, alarmDelay);
Serial.begin();
...
}
???

User avatar
RogerClark
Posts: 7147
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Timer and Interrupt used together to leave sleep mode

Post by RogerClark » Fri Sep 15, 2017 4:57 am

Looking at the existing code ...

You would need to call Serial.end() and then call Serial.begin()

just after you code wakes from sleep

And... This is only likely to work on the Maple Mini board, which has the correct USB reset hardware

It can probably be made to work on the BluePill etc, but you'd need to take a look at the hack we use to get the BluePill to fool the USB host into re-enumerating its bus.

Leonardo_evaldt
Posts: 19
Joined: Wed Jul 19, 2017 3:30 pm

Re: Timer and Interrupt used together to leave sleep mode

Post by Leonardo_evaldt » Fri Sep 15, 2017 5:07 am

RogerClark wrote:
Fri Sep 15, 2017 4:57 am
Looking at the existing code ...

You would need to call Serial.end() and then call Serial.begin()

just after you code wakes from sleep
Like This?

Code: Select all

loop(){
...
Serial.end();
sleepAndWakeUp(STOP, &rt, alarmDelay);
Serial.begin();
...
}

User avatar
RogerClark
Posts: 7147
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Timer and Interrupt used together to leave sleep mode

Post by RogerClark » Fri Sep 15, 2017 5:29 am

No

More like

You'd need to call Serial.end() before sleeping, and call Serial.begin() when you wake again.

But... Its not likely to work on a BluePill

Leonardo_evaldt
Posts: 19
Joined: Wed Jul 19, 2017 3:30 pm

Re: Timer and Interrupt used together to leave sleep mode

Post by Leonardo_evaldt » Fri Sep 15, 2017 5:41 am

RogerClark wrote:
Fri Sep 15, 2017 5:29 am
No

More like

You'd need to call Serial.end() before sleeping, and call Serial.begin() when you wake again.

But... Its not likely to work on a BluePill
Roger, I realy sorry. I'm not so used to programing like you. I'm many steps behind you.
This is what I understant about your previous post:


Setup(){...}

loop(){
...
Serial.end();
sleepAndWakeUp(STOP, &rt, alarmDelay);
Serial.begin();
...
}

I tried but doesn't work. The code locked after sleepAndWakeUp(STOP, &rt, alarmDelay).
I also tried this

Setup(){...}

loop(){
...
Serial.end();
//sleepAndWakeUp(STOP, &rt, alarmDelay);
Serial.begin();
...
}

the code run correctly (the led in my board light after this part of code) but the serial do not obey Serial.begin(). It remains off.
What am I doing Wrong?

User avatar
RogerClark
Posts: 7147
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Timer and Interrupt used together to leave sleep mode

Post by RogerClark » Fri Sep 15, 2017 5:53 am

The core does not have code which supports re-enumeration of USB on boards except the Maple Mini.
And even on the Maple mini I've never tried to do it.

I'm sure it would be possible to modify the code, but you'd need to write code to for re-enumeration just before calling Serial.begin()

Leonardo_evaldt
Posts: 19
Joined: Wed Jul 19, 2017 3:30 pm

Re: Timer and Interrupt used together to leave sleep mode

Post by Leonardo_evaldt » Fri Sep 15, 2017 6:07 am

RogerClark wrote:
Fri Sep 15, 2017 5:53 am
The core does not have code which supports re-enumeration of USB on boards except the Maple Mini.
And even on the Maple mini I've never tried to do it.

I'm sure it would be possible to modify the code, but you'd need to write code to for re-enumeration just before calling Serial.begin()
I'm not understanding even a bit that you are talking Roger. Core? Re-enumeration???
I'm feeling stupid. :cry:
I tried to run the exemplo code from
https://github.com/chacal/stm32sleep

but even 'stm32_periodic_stop" exemple isn't running in my PC. It stay locked on sleepAndWakeUp(STOP, &rt, alarmDelay) funtion.
I'm certainly doing something very wrong.

User avatar
Pito
Posts: 1593
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Timer and Interrupt used together to leave sleep mode

Post by Pito » Fri Sep 15, 2017 6:29 am

"Core" - it is a "base" you get when compiling/building the binary with stm32duino (ie Roger's Libmaple or Daniel's Generic or other set of libraries used - also called here "cores"). The "core" includes basic routines which work in your code in background - like Serial, SerialUSB, Systick (millis(), micros() ), interrupts handling, and other stuff (SPI, I2C, timers..) which are compiled into your code when required..

Re-enumeration - the USB when connects a Host to a device runs a complex process, where the device (BluePill or MapleMini or other board) identifies itself it is ready to cooperate on the USB bus with the Host.
MapleMini board has got a special circuit, which tells Host (ie your PC) it is ready to cooperate, and it does it automatically. Other boards do not have this feature, so you have to press reset button, or plug/unplug USB, for example. You may add the circuit to BluePill as well (2 resistors and a pnp/pmosfet transistor).
Pukao Hats Cleaning Services Ltd.

Post Reply