Timer and Interrupt used together to leave sleep mode

Leonardo_evaldt
Thu Sep 14, 2017 6:35 am
Hi Guys.

Anyone here knows a site or link that show the code to put maple mini on SLEEP MODE? I want to put the board on sleep mode with two different ways to leave it: By a counter or by an interrupt pin.

I found the official documento about sleep modes on ST site, but… I am no an expert in register programmer. I was wondering if anyone knows something “less difficult” to non-professional user, like a library?

Thanks all.


victor_pv
Thu Sep 14, 2017 1:02 pm
There are multiple posts in the forum about that. Please do a search for sleep and standby.
If you don’t have a more specific question I’ll lock this thread since a search will answer it. If you have a more specific question, please update the first post and it’s title after you search for what’s available already.

Forum rules and procedures


Leonardo_evaldt
Thu Sep 14, 2017 6:04 pm
Dear Victor,

There is a lot of posts related. But none of then answering my question or give me the anwser I need.
As I said, I’m not a professional firmware engineer. I don’t understand 90% of they say. My question is if someone knows about a finished library that I can use. There is a lot of posts about this but to arduino. I tried to compile some but my processor not acceptd.
So I appreciate if you keep this post because I realy need this anwser.

Best regards.


victor_pv
Thu Sep 14, 2017 7:32 pm
Leonardo, I found a link to a library in less than 3 minutes. You are basically asking for someone else to do a search for you.

Leonardo_evaldt
Thu Sep 14, 2017 9:19 pm
Victor,

Again, all topics I found didnt anwser my question. I need a topic who provide me a library that give me the option of put maple mini in sleep mode with two ways to leave it: By a interrupt OR by a timer. BOTH AT THE SAME CODE.
Perhaps you are talking about this topic

http://www.stm32duino.com/viewtopic.php … eep#p29094

where the member “ag123” give a link to a code that (I think, but I do not have sure) create a timer clock attached to system clock and enable interrupts (again, I am not a professional firmware engineer, so I don’t understand all the code). Not helps me.

Or perheps you are talking about this topic

http://www.stm32duino.com/viewtopic.php … ilit=sleep

Where ag123 give a lot of other codes related sleep mode and timer clocks. But none of them solve my problem.
I’m not this lazy guy that you are suggesting. I have not treated anyone like this here in this forum, so I would be grateful if you did not treat me with so little respect.

After some hours of hunting up on internet I found this document in other forums website.

http://www.st.com/content/ccc/resource/ … 228163.pdf

This document explains, in a very didactic way, about energy management in topic 2.5.1. I’m still reading it. But I still ask you to do not delete or block this post because i don’t know if the document will really solve my problem.

Thanks for understanding.


victor_pv
Thu Sep 14, 2017 9:32 pm
Did you see this one?
http://www.stm32duino.com/viewtopic.php … ep+library

That topic is one of the first ones I get in a search for “sleep library”.

You also need to read the STM32F103 datasheet, because you need to understand how and what can wake up the CPU. You dont need any library to wake it up, just enable whatever interrupt or event you want to use to wake it up, then go to sleep with wfi or wfe. The libraries are about disabling certain peripherals or clocks before going to sleep.
Please update your thread title and first post to be more specific.
“MAPLE MINI – SLEEP MODE” is anything but specific and goes against the rules.


Leonardo_evaldt
Thu Sep 14, 2017 10:35 pm
Yes I did.
Acobo used this strange function “adc_disable_all();” to disable the ADC and this one setGPIOModeToAllPins(GPIO_INPUT_ANALOG) to disable the GPIO. I didn’t understand exactly how this functions works but, in my case, I cant use the first one. My interrupt uses the ADC to wake up.

After a lot of hardware discuss, acobo show a list of functions to disable all blocks inside the processor. But the ADC interrupt will be disabled too. It doesn’t solve my problem. I need to enable interrupt to wake up my board too. Just letting ADC enable (rcc_clk_enable(RCC_ADC1)) doesn’t mean that the interrupt attached to it will make my board wake up when an interrupt occurs because sleepAndWakeUp function just finish when the alarmDelay finish.
Adapting my original question to this situation, it would look like this: “How can I use this WFI function together with sleepAndWakeUp function so my board could be able to wake up after a predetermined time OR an interrupt”?
In my first post I ask for a library because I thought that someone could have a code that put together WFI and sleepAndWakeUp.

About the title of the topic, I am sorry about It. As I did not see any post with this title especific to maple mini, I understood that it would be a good title for other members understand that the topic gave an alternative of how to put the board in sleep mode. I changed it. Now does that according to the rules???


victor_pv
Thu Sep 14, 2017 11:00 pm
The title makes more sense now, “Maple and sleep” is just extremely wide.
I still not understanding exactly how you want to wake up. You say you want a pin interrupt to wake the MCU, to me that means a pin toggling from high to low or from low to high, that’s an EXTI interrupt, but you mention ADC. Are you wanting to wake up when an analog input gets to a certain analog level?

Leonardo_evaldt
Thu Sep 14, 2017 11:22 pm
[victor_pv – Thu Sep 14, 2017 11:00 pm] –
The title makes more sense now, “Maple and sleep” is just extremely wide.

Nice :D. Thank you for this help. I’m new here. I’m still learning.

[victor_pv – Thu Sep 14, 2017 11:00 pm] –
I still not understanding exactly how you want to wake up. You say you want a pin interrupt to wake the MCU, to me that means a pin toggling from high to low or from low to high, that’s an EXTI interrupt, but you mention ADC. Are you wanting to wake up when an analog input gets to a certain analog level?

Something like this. The interrupt is attached in a digital pin. But I use the ADC and some Transistors to allow the signal fire the digital pin interrupt. So, my ADC needs to be working.
I think that, using that “acobo’ disable list (but without rcc_clk_disable(RCC_ADC1) I can solve the problem about ADC. But the problem is, how to use sleepAndWakeUp function with interrupt enabled? Because, as I understood, sleepAndWakeUp just wake up the bord when timer finish and WFI only wake up the board when an iterrupt fire the digital pin. I need both wake up ways in the same function.


victor_pv
Fri Sep 15, 2017 12:21 am
Read the code of sleepAndWakeUp() because it shows what it does, and any enabled interrupt can wake the board.
I still do not understand what you are doing with the ADC…

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:

#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);
}


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
Fri Sep 15, 2017 4:43 am
[RogerClark – Fri Sep 15, 2017 3:40 am] –
Sleeping will disconnect USB.

I suspected. Thank you for confirming, Roger.

[RogerClark – 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:

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


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
Fri Sep 15, 2017 5:07 am
[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

Like This?

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


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
Fri Sep 15, 2017 5:41 am
[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

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?


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
Fri Sep 15, 2017 6:07 am
[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()

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.


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).


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

#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


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

//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();


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
Mon Sep 18, 2017 8:41 am
[victor_pv – 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?


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
Wed Sep 20, 2017 11:04 am
[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

#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


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
Fri Sep 22, 2017 8:32 am
[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.

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.


Leave a Reply

Your email address will not be published. Required fields are marked *