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 7:14 am

Thanks Pito. It clarifies a lot of things. So Roger is trying to help me to re-enumerate the serial after I lost connection. Something in the core could do it. I just need to find out how.
It's understood now.
But the problem of serial disconnection is not my biggest problem. I still trying to figure out why my board doesn't leave the sleep mode.
I try running an example from here

https://github.com/chacal/stm32sleep

In exemple stm32_periodic_standby

Code: Select all

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

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

void setup() {
  // We have just started or woken up from sleep! System clock is set to 72MHz HSE.
  delay(1000);
  sleepAndWakeUp(STANDBY, &rt, alarmDelay);  
}

void loop() { }  // This is never run


The question is, why is the sleepAndWakeUp function inside the setup()? Should not this be inside the loop()?

In the second exemplo (stm32_periodic_stop) i made a little modification (I putted the builtin led to blink) like this

Code: Select all

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

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

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  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(STANDBY, &rt, alarmDelay);
}
But neither of them ran. Only when I removed sleepAndWakeUp(STOP, &rt, alarmDelay) from setup() they ran.
The second exemple ran correct. The led blink. After a second it turned off. After a second the board locked and the led did not light anymore.
So it obvious that the code is getting stuck in the sleepAndWakeUp function. The question is, why?
Solving this problem I think that my problem is solve too.
Do you have any idea why the code is getting stuck on sleepAndWakeUp function?
Has someone get stuck with the same problem before?

User avatar
RogerClark
Posts: 6894
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 10:32 am

This won't help with waking from sleep but looking in the code..

This is what gets called in the pre-initialisation - before setup() is called in the Arduino sketch

Code: Select all

//Reset the USB interface on generic boards - developed by Victor PV
gpio_set_mode(PIN_MAP[PA12].gpio_device, PIN_MAP[PA12].gpio_bit, GPIO_OUTPUT_PP);
gpio_write_bit(PIN_MAP[PA12].gpio_device, PIN_MAP[PA12].gpio_bit,0);

for(volatile unsigned int i=0;i<512;i++);// Only small delay seems to be needed, and USB pins will get configured in Serial.begin
gpio_set_mode(PIN_MAP[PA12].gpio_device, PIN_MAP[PA12].gpio_bit, GPIO_INPUT_FLOATING);

Serial.begin();

So if you use this code with it "may" restart the USB --- I've not tested it

Note. You must have previously called Serial.end(), becuase Serial.begin() has a check in it, to prevent the USB initialisation being called more than once.

i.e calling

Serial.begin();
Serial.begin();
Serial.begin();

Only sets up USB once.

And in fact, Serial.begin() is really called before setup() is run, so you don't need to call it at all in setup() if the bootloader upload method is selected.
(It has to do this otherwise the bootloader method would not work if the code in setup() didnt include Serial.begin()


Anyway.

Call Serial.end() just prior to sleeping, and then run code posted above when your code wakes up.



Oh. While I think about it...

Are you sure your 32khz RTC clock circuit on your board is working OK?

victor_pv
Posts: 1648
Joined: Mon Apr 27, 2015 12:12 pm

Re: Timer and Interrupt used together to leave sleep mode

Post by victor_pv » Fri Sep 15, 2017 1:35 pm

Leonardo, that code uses the rtc to wake after a certain time, do you have any crystal connected to the rtc oscillator pins? if you dont know what this is, the you dont have it.
The mcu will not wake up with an RTC alarm if RTC is not working.

There is a bunch of threads about RTC and many examples, so no point explaining it here.
If you just want to test that code, you can try instead using an external pin, there is examples on the forum about getting the mcu to sleep and waking it up with the onboard button.

This thread is now 3 pages long and has gone from "Maple and sleep" to how to wake up the MCU, to why the RTC interrupt is not working, next is going to "What is the RTC Chrystal"...
In my first post I linked you to the forum rules, which you had not read otherwise you would have known that posting a vague title is against the rule.
In my second I advised you to read the Reference Manual, which you have not read either otherwise you would have known how wake up from interrupt works, what's the rtc, what do you need for the RTC to work etc.

In your next post, please explain what possible RTC clock sources you can use, which one are you using, if a crystal where did you get it, what capacitors are you using with it, to which pins you connected the crystal. If using a different clock source how you configured it and how did you confirm it is working. If you dont have answers to that, DO NOT POST and go to the Reference Manual as I advised, and once you know what you need to do, feel free to POST again on HOW to do it. The forum is not meant to be a tutorial, there are plenty of tutorials online, and the Reference Manual is the best source of what the MCU can do and what do you need for that.

Leonardo I am not blocking you, so you can still post, but this is a warning that YOU need to try and someway understand the MCU from reading existing documentation before you try to get people scrambling their head to figure out why the RTC you never set up is not working.

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 » Mon Sep 18, 2017 8:41 am

victor_pv wrote:
Fri Sep 15, 2017 1:35 pm
If you just want to test that code, you can try instead using an external pin, there is examples on the forum about getting the mcu to sleep and waking it up with the onboard button.
Thanks, I will try it asap.

Victor, my initial problem was about putting my maple mini on Sleep mode with a pre-existing library. After getting information in the thread, I was learning about the process and my doubts were being healed and others were emerging naturally. But the only thing I want is some help to solve my problem. The last thing I want is to create problems in the forum. But then, if you think the topic violates the forum rules, please delete it. I realy sorry for posting that.

I am not a Firmware engineer expert. I am just a hardware development engineer that is trying to understand a little bit about firmware development to put my board to work. My hardware do not have any crystal. I'm do not using any decoupling capacitor on maple mini or any additional hardware to help maple mini with clocks. I'm just using the clock from maple mini. I have a GPS and a GPRS modules, a protection circuit, (in the electric system of a car there are many of load dump transient), a step-down switching regulator, some AmpOp and many transistors (figures atacheed in this post). In fact, to test the library, I'm using just the maple mini (I have detached all my hardware). I'm using delay() all the time, so I think the maple mini's clock is working well. In my first post I was looking for a library that uses the sleep mode. I got the library already. Now I'm just trying to understand why the library, that supposedly should work, does not work on my board.
I already read all ST document related to this firmware. But not being a firmware engineer, its hard to understand without any help.
My last post I said that the library (exactly as available on the link https://github.com/chacal/stm32sleep) is not working. I exposed all the behaviors of my board at runtime and asked if anyone knows why the library is behaving this way. If you want I can delete this thread and create a new one with just this question and with appropriate title. Do you think its better?
Attachments
Esquemático 3.png
Esquemático 3.png (78.8 KiB) Viewed 143 times
Esquemático 2.png
Esquemático 2.png (70.58 KiB) Viewed 143 times
Esquemático1.png
Esquemático1.png (46.65 KiB) Viewed 143 times

victor_pv
Posts: 1648
Joined: Mon Apr 27, 2015 12:12 pm

Re: Timer and Interrupt used together to leave sleep mode

Post by victor_pv » Mon Sep 18, 2017 1:54 pm

Leonardo, you don't need to read firmware documents to understand the hardware capabilities of the MCU.
Two people already adivsed you to check about RTC, and I specifically asked you several questions about RTC, please go back to me previous post and read about RTC in the referecne manual. Has nothing to do with the main clock in the MCU working or not working.

edogaldo
Posts: 263
Joined: Fri Jun 03, 2016 8:19 am

Re: Timer and Interrupt used together to leave sleep mode

Post by edogaldo » Wed Sep 20, 2017 11:04 am

Leonardo_evaldt wrote:
Fri Sep 15, 2017 7:14 am
Thanks Pito. It clarifies a lot of things. So Roger is trying to help me to re-enumerate the serial after I lost connection. Something in the core could do it. I just need to find out how.
It's understood now.
But the problem of serial disconnection is not my biggest problem. I still trying to figure out why my board doesn't leave the sleep mode.
I try running an example from here

https://github.com/chacal/stm32sleep

In exemple stm32_periodic_standby

Code: Select all

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

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

void setup() {
  // We have just started or woken up from sleep! System clock is set to 72MHz HSE.
  delay(1000);
  sleepAndWakeUp(STANDBY, &rt, alarmDelay);  
}

void loop() { }  // This is never run
Leonardo, the Maple mini board does not have LSE so, unless you added it, following statement is for sure wrong:

Code: Select all

RTClock rt(RTCSEL_LSE);
You could try with:

Code: Select all

RTClock rt(RTCSEL_LSI);
or

Code: Select all

RTClock rt(); // uses HSE with prescaler
(ref. <hardware_dir>\Arduino_STM32\STM32F1\libraries\RTClock\src\RTClock.cpp)
The question is, why is the sleepAndWakeUp function inside the setup()? Should not this be inside the loop()?
Because, as per specs, resume from the standby mode resets the board (and the program too) thus the reset drives the loop.
In the second exemplo (stm32_periodic_stop) i made a little modification (I putted the builtin led to blink) like this

Code: Select all

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

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

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  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(STANDBY, &rt, alarmDelay);
}
But neither of them ran. Only when I removed sleepAndWakeUp(STOP, &rt, alarmDelay) from setup() they ran.
The second exemple ran correct. The led blink. After a second it turned off. After a second the board locked and the led did not light anymore.
So it obvious that the code is getting stuck in the sleepAndWakeUp function. The question is, why?
Solving this problem I think that my problem is solve too.
Do you have any idea why the code is getting stuck on sleepAndWakeUp function?
Has someone get stuck with the same problem before?
In the second sketch I see 2 errors:
  • the use of LSE like in the first sketch
  • the use of

    Code: Select all

    sleepAndWakeUp(STANDBY, &rt, alarmDelay);
    in the loop; it should be:

    Code: Select all

    sleepAndWakeUp(STOP, &rt, alarmDelay);
Best regards, E.
Last edited by edogaldo on Fri Sep 22, 2017 8:19 am, edited 1 time in total.

User avatar
RogerClark
Posts: 6894
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 » Wed Sep 20, 2017 8:58 pm

Thanks E.

It did cross my mind that the OP may have been using the MM and I didnt think it had the external 32 khz crystal.

Using the LSI is an interesting work around, but I have not tried it, and of course the timing is going to me inaccurate unless you apply some calibration in the code
e.g. I know the Nordic Semi nRF5 SDK calibrates its sleep values by comparing the current freq of its LSI with its main clock freq.
It also briefly wakes up every few secs to recalibrate, to ensure accurate timekeeping.

edogaldo
Posts: 263
Joined: Fri Jun 03, 2016 8:19 am

Re: Timer and Interrupt used together to leave sleep mode

Post by edogaldo » Fri Sep 22, 2017 8:32 am

RogerClark wrote:
Wed Sep 20, 2017 8:58 pm
Thanks E.

It did cross my mind that the OP may have been using the MM and I didnt think it had the external 32 khz crystal.

Using the LSI is an interesting work around, but I have not tried it, and of course the timing is going to me inaccurate unless you apply some calibration in the code
e.g. I know the Nordic Semi nRF5 SDK calibrates its sleep values by comparing the current freq of its LSI with its main clock freq.
It also briefly wakes up every few secs to recalibrate, to ensure accurate timekeeping.
Hi Roger, probably, if precision is needed, for a project like this a Blue Pill could be a better choice respect to the Maple mini as it features also the LSE.

Cheers, E.

Post Reply