Electronic – Detect which momentary switch caused a STM32 cpu to wakeup


Edit: This question is wrong. The stm325105 has only one wakeup pin. But other ST-parts has more than one wakeup pin, so the given answer is valid for those.

I have a stm32f105 that has two momentary buttons connected to the wakeup inputs. The processor is put in STANDBY MODE. When any of the buttons are pressed, or the RTC timer fires, the cpu wakes up.

The problem is that I want the cpu to do different things depending on which wakeup input was triggered. According to 5.3.5 from ST stm32f105xx reference manual, no registers are preserved except for a status register indicating that we've been woken up (but not by who) and 42 backup registers.

The Standby mode allows to achieve the lowest power consumption. It is based on the
Cortex ® -M3 deepsleep mode, with the voltage regulator disabled. The 1.8 V domain is
consequently powered off. The PLL, the HSI oscillator and the HSE oscillator are also
switched off. SRAM and register contents are lost except for registers in the Backup domain
and Standby circuitry.

After waking up from Standby mode, program execution restarts in the same way as after a
Reset (boot pins sampling, vector reset is fetched, etc.). The SBF status flag in the Power
control/status register (PWR_CSR) indicates that the MCU was in Standby mode.

This ST forum post, How to determine the origin of Wakeup from Standby?, suggests that I can't detect which wakeup triggered in software. I found no other posts there that gave any more enlightment.

How can I use software or hardware for determining, after waking up, which wakeup input was triggered?

Best Answer

I don't know the details of this ST part, so I'll assume your description of it is correct.

The processor should wake up very soon after either button is pressed. This should be especially true if it has a internal RC oscillator. Even if you need to run from a crystal eventually for accuracy reasons, perhaps you can have the part start from a internal RC, then switch to the crystal later. There are micros that can do such things, although I don't know if yours is one of them.

In any case, you read both inputs as soon as possible after wakeup. Unless there is something unusual about this ST micro, that should be from a ms to a few 10s of ms from the button press.

To guarantee the line is still low, use a capacitor to keep the line low for up to 100 ms after the button is released.

For example, let's say the guaranteed logic low input level is 20% of the supply voltage. The line is pulled up with a resistor, and has a capacitor to ground. The button shorts the line to ground. The line therefore floats high, and is actively forced to ground when the button is pressed. When the button is released, the voltage exponentially decays towards the supply.

Decaying to 20% of the final value happens in 0.22 time constants. Lets say you want to guarantee the line looks low for 100 ms after a button press. That means the RC time constant must be 450 ms. With 100 kΩ pullup, the capacitance would need to be 4.5 µF. So a 4.7 µF 10 V cap would do nicely.

In summary, here is the circuit: