STM32 - detachInterrupt and deepSleep

Post here first, or if you can't find a relevant section!
Post Reply
Beuzekom
Posts: 15
Joined: Thu Jul 30, 2020 1:32 pm
Answers: 1

STM32 - detachInterrupt and deepSleep

Post by Beuzekom »

Hi,

Working on STM32 project in which I have an external sensor with interrupt (PA11).
When in deepsleep I am looking at 2 different ways to wake up.
  • (1) Sensor wakes up the MCU thru an interrupt
  • (2) MCU wakes up "itself" due to timer that runs out (e.g. lp.deepsleep(60000))
    In this case the MCU should never wake up from the external sensor interrupt (PA11)
The 1st scenario works fine and MCU wakes up from deepsleep due to interrupt from sensors.

For the second scenario I had the idea to use detachInterrupt() and go to sleep and wait for the timer to expire.
However the following is happening:
  • After powerup it goes to sleep and wakes up after 1 minutes, goes to sleep, wakes up etc etc -> no problem.
    But when triggering the sensor (motion) the MCU wakes up and not able to sleep again
Code snapshots below

Code: Select all

STM32LowPower lp;
lp.attachInterruptWakeup(PA11, wakeUpNow, LOW, DEEP_SLEEP_MODE);

Code: Select all

void sleepLight2(bool useIntFromSensor)
{
  if (useIntFromSensor == true)
  {
    lp.attachInterruptWakeup(PA11, wakeUpNow, LOW, DEEP_SLEEP_MODE);
    interruptFlagMotion = false;
  } 
  else 
  { // Disable the interrupt from sensor
    detachInterrupt(PA11);
    __DSB();
    __ISB();
  }

  // ***** Deepsleep
  interruptFlagMotion = false;  
  lp.deepSleep( 60000 );                                              // deepsleep 1 minute
} // end of sleepLight2
Any suggestions why this is happening and what I am doing wrong?

Thanks!
User avatar
fpiSTM
Posts: 1746
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: STM32 - detachInterrupt and deepSleep

Post by fpiSTM »

Maybe you have a pending interrupt.
You probably needs to clear it.

You can have a look on this:
https://github.com/stm32duino/Arduino_C ... /pull/1258

How do you manage the call of:

Code: Select all

sleepLight2(bool useIntFromSensor)
Beuzekom
Posts: 15
Joined: Thu Jul 30, 2020 1:32 pm
Answers: 1

Re: STM32 - detachInterrupt and deepSleep

Post by Beuzekom »

Thanks for the feedback.

Clearing potential pending interrupt might be the cause. Will have a look at it.
For the 328p processor I cleared the INTs by setting the following (but of course obsoleted for the STM32):

Code: Select all

  
//  Atmega 328P (obsoleted)
//  EIFR = bit (INTF0);                                               // clear flag for interrupt 0 (D2)
//  EIFR = bit (INTF1);                                               // clear flag for interrupt 1 (D3)
For the current stmduino version it seems it is still in development(?). Will give it a try and implement it. Post results later on.

The function

Code: Select all

sleepLight2(bool useIntFromSensor)
is called from the mail loop. It should remain there till the sleep is finished (timeout or thru a sensor int)
Beuzekom
Posts: 15
Joined: Thu Jul 30, 2020 1:32 pm
Answers: 1

Re: STM32 - detachInterrupt and deepSleep

Post by Beuzekom »

Implemented the clearPendingInterrupt() (see below). It is going to sleep now but the interrupt is not detached (as in deepSleep the MCU wakes up from the sensor INT).

Code: Select all

void sleepLight2(bool useIntFromSensor)
{
  #ifdef DEBUG
    Serial.print(F("sleepLight:     Int on (1) or off (0):  ")); 
    Serial.println( useIntFromSensor ); 
    Serial.flush();
  #endif


  if (useIntFromSensor == true)
  {
    lp.attachInterruptWakeup(PA11, wakeUpNow, LOW, DEEP_SLEEP_MODE);
    interruptFlagMotion = false;
  } 
  else 
  { // Disable the interrupt from sensor
    clearPendingInterrupt(PA11);
    detachInterrupt(PA11);
    __DSB();
    __ISB();
  }

  // ***** Deepsleep
  interruptFlagMotion = false;  
  lp.deepSleep( 60000 );                                              // deepsleep 1 minute

} // end of sleepLight2
Any ideas?
Beuzekom
Posts: 15
Joined: Thu Jul 30, 2020 1:32 pm
Answers: 1

Re: STM32 - detachInterrupt and deepSleep

Post by Beuzekom »

Was not able to solve it and decided to switch off (power down thru MCU) the sensor instead of using the detachInterrupt().
Still looking for the solution and detach the Interrupt.
mlundin
Posts: 94
Joined: Wed Nov 04, 2020 1:20 pm
Answers: 6
Location: Sweden

Re: STM32 - detachInterrupt and deepSleep

Post by mlundin »

Looking at the source code in the Low Power library:
https://github.com/stm32duino/STM32LowPower

Code: Select all

void STM32LowPower::attachInterruptWakeup(uint32_t pin, voidFuncPtrVoid callback, uint32_t mode, LP_Mode LowPowerMode)
this function does two things, it attaches the pin interrupt and it enables the pin wakeup function

Code: Select all

LowPower_EnableWakeUpPin(uint32_t pin, uint32_t mode);
This one in turn calls the HAL function

Code: Select all

HAL_PWR_EnableWakeUpPin(wkup_pin);
So when the interrupt is detached it seems the pin wakeup functionality is still active, but there is no interrupt callback.
The solution might be to call the

Code: Select all

HAL_PWR_DisableWakeUpPin(wkup_pin);
This is just from reading the code, I have not done any tests
Beuzekom
Posts: 15
Joined: Thu Jul 30, 2020 1:32 pm
Answers: 1

Re: STM32 - detachInterrupt and deepSleep

Post by Beuzekom »

Thanks for the suggestion / going to test it tonight and share the results.
Jacobjsdhfg
Posts: 1
Joined: Thu Nov 23, 2023 6:56 am

Re: STM32 - detachInterrupt and deepSleep

Post by Jacobjsdhfg »

Beuzekom wrote: Tue Feb 16, 2021 7:26 am Thanks for the suggestion / going to test it tonight and share the results.Drive Mad 2
I look forward to hearing about your test results.
TerrellDarrough
Posts: 2
Joined: Tue Feb 06, 2024 4:33 am
Contact:

Re: STM32 - detachInterrupt and deepSleep

Post by TerrellDarrough »

Jacobjsdhfg wrote: Thu Nov 23, 2023 7:02 am
Beuzekom wrote: Tue Feb 16, 2021 7:26 am Thanks for the suggestion / going to test it tonight and share the results.
I look forward to hearing about your test results.
Publish the results and have a look
Post Reply

Return to “General discussion”