@@ -33,6 +33,35 @@ SPI1 PA5 PA6 PA7 PA4 PB0 NA
3333
3434*/
3535
36+ volatile uint8_t _wokeUpByInterrupt =
37+ INVALID_INTERRUPT_NUM; // Interrupt number that woke the mcu.
38+ volatile uint8_t _wakeUp1Interrupt =
39+ INVALID_INTERRUPT_NUM; // Interrupt number for wakeUp1-callback.
40+ volatile uint8_t _wakeUp2Interrupt =
41+ INVALID_INTERRUPT_NUM; // Interrupt number for wakeUp2-callback.
42+
43+ static uint32_t sleepRemainingMs = 0ul ;
44+
45+ void wakeUp1 (void )
46+ {
47+ // First interrupt occurred will be reported only
48+ if (INVALID_INTERRUPT_NUM == _wokeUpByInterrupt) {
49+ _wokeUpByInterrupt = _wakeUp1Interrupt;
50+ }
51+ }
52+ void wakeUp2 (void )
53+ {
54+ // First interrupt occurred will be reported only
55+ if (INVALID_INTERRUPT_NUM == _wokeUpByInterrupt) {
56+ _wokeUpByInterrupt = _wakeUp2Interrupt;
57+ }
58+ }
59+
60+ inline bool interruptWakeUp (void )
61+ {
62+ return _wokeUpByInterrupt != INVALID_INTERRUPT_NUM;
63+ }
64+
3665bool hwInit (void )
3766{
3867#if !defined(MY_DISABLED_SERIAL)
@@ -41,7 +70,7 @@ bool hwInit(void)
4170 while (!MY_SERIALDEVICE) {}
4271#endif
4372#endif
44-
73+ LowPower. begin ();
4574 return true ;
4675}
4776
@@ -93,31 +122,56 @@ void hwWriteConfig(const int addr, uint8_t value)
93122
94123int8_t hwSleep (uint32_t ms)
95124{
96- // TODO: Not supported!
97- (void )ms;
98- return MY_SLEEP_NOT_POSSIBLE;
125+ // Return what woke the mcu.
126+ // Default: no interrupt triggered, timer wake up
127+ int8_t ret = MY_WAKE_UP_BY_TIMER;
128+
129+ if (ms > 0u ) {
130+ // sleep for defined time
131+ LowPower.deepSleep (ms);
132+ } else {
133+ // sleep until ext interrupt triggered
134+ LowPower.deepSleep ();
135+ }
136+ if (interruptWakeUp ()) {
137+ ret = static_cast <int8_t >(_wokeUpByInterrupt);
138+ }
139+ // Clear woke-up-by-interrupt flag, so next sleeps won't return immediately.
140+ _wokeUpByInterrupt = INVALID_INTERRUPT_NUM;
141+
142+ return ret;
99143}
100144
101145int8_t hwSleep (const uint8_t interrupt, const uint8_t mode, uint32_t ms)
102146{
103- // TODO: Not supported!
104- (void )interrupt;
105- (void )mode;
106- (void )ms;
107- return MY_SLEEP_NOT_POSSIBLE;
147+ return hwSleep (interrupt, mode, INVALID_INTERRUPT_NUM, 0u , ms);
108148}
109149
110150int8_t hwSleep (const uint8_t interrupt1, const uint8_t mode1, const uint8_t interrupt2,
111151 const uint8_t mode2,
112152 uint32_t ms)
113153{
114- // TODO: Not supported!
115- (void )interrupt1;
116- (void )mode1;
117- (void )interrupt2;
118- (void )mode2;
119- (void )ms;
120- return MY_SLEEP_NOT_POSSIBLE;
154+ // According to STM32LowPower API following modes to wake from sleep are supported: HIGH, LOW, RISING, FALLING or CHANGE
155+ // Ref: https://github.com/stm32duino/STM32LowPower
156+
157+ // attach interrupts
158+ _wakeUp1Interrupt = interrupt1;
159+ _wakeUp2Interrupt = interrupt2;
160+
161+ if (interrupt1 != INVALID_INTERRUPT_NUM) {
162+ LowPower.attachInterruptWakeup (interrupt1, wakeUp1, mode1, DEEP_SLEEP_MODE);
163+ }
164+ if (interrupt2 != INVALID_INTERRUPT_NUM) {
165+ LowPower.attachInterruptWakeup (interrupt2, wakeUp2, mode2, DEEP_SLEEP_MODE);
166+ }
167+
168+ if (ms > 0u ) {
169+ // sleep for defined time
170+ return hwSleep (ms);
171+ } else {
172+ // sleep until ext interrupt triggered
173+ return hwSleep (0 );
174+ }
121175}
122176
123177
0 commit comments