|
| 1 | +/** |
| 2 | +****************************************************************************** |
| 3 | +* @file STM32LowPower.cpp |
| 4 | +* @author WI6LABS |
| 5 | +* @version V1.0.0 |
| 6 | +* @date 11-December-2017 |
| 7 | +* @brief Provides a STM32 Low Power interface with Arduino |
| 8 | +* |
| 9 | +****************************************************************************** |
| 10 | +* @attention |
| 11 | +* |
| 12 | +* <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> |
| 13 | +* |
| 14 | +* Redistribution and use in source and binary forms, with or without modification, |
| 15 | +* are permitted provided that the following conditions are met: |
| 16 | +* 1. Redistributions of source code must retain the above copyright notice, |
| 17 | +* this list of conditions and the following disclaimer. |
| 18 | +* 2. Redistributions in binary form must reproduce the above copyright notice, |
| 19 | +* this list of conditions and the following disclaimer in the documentation |
| 20 | +* and/or other materials provided with the distribution. |
| 21 | +* 3. Neither the name of STMicroelectronics nor the names of its contributors |
| 22 | +* may be used to endorse or promote products derived from this software |
| 23 | +* without specific prior written permission. |
| 24 | +* |
| 25 | +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 26 | +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 27 | +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 28 | +* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 29 | +* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 30 | +* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 31 | +* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 32 | +* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 33 | +* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 34 | +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | +* |
| 36 | +****************************************************************************** |
| 37 | +*/ |
| 38 | + |
| 39 | +#include "STM32LowPower.h" |
| 40 | + |
| 41 | +STM32LowPower LowPower; |
| 42 | + |
| 43 | + |
| 44 | +STM32LowPower::STM32LowPower() |
| 45 | +{ |
| 46 | + _configured = false; |
| 47 | + _serial = NULL; |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * @brief Initializes the low power mode |
| 52 | + * @param None |
| 53 | + * @retval None |
| 54 | + */ |
| 55 | +void STM32LowPower::begin(void) |
| 56 | +{ |
| 57 | + LowPower_init(); |
| 58 | + _configured = true; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * @brief Enable the idle low power mode (STM32 sleep). Exit this mode on |
| 63 | + * interrupt or in n milliseconds. |
| 64 | + * @param millis: optional delay before leave the idle mode (default: 0). |
| 65 | + * @retval None |
| 66 | + */ |
| 67 | +void STM32LowPower::idle(uint32_t millis) |
| 68 | +{ |
| 69 | + if(millis > 0) { |
| 70 | + programRtcWakeUp(millis); |
| 71 | + } |
| 72 | + LowPower_sleep(PWR_MAINREGULATOR_ON); |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * @brief Enable the sleep low power mode (STM32 sleep). Exit this mode on |
| 77 | + * interrupt or in n milliseconds. |
| 78 | + * @param millis: optional delay before leave the sleep mode (default: 0). |
| 79 | + * @retval None |
| 80 | + */ |
| 81 | +void STM32LowPower::sleep(uint32_t millis) |
| 82 | +{ |
| 83 | + if(millis > 0) { |
| 84 | + programRtcWakeUp(millis); |
| 85 | + } |
| 86 | + LowPower_sleep(PWR_LOWPOWERREGULATOR_ON); |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * @brief Enable the deepsleep low power mode (STM32 stop). Exit this mode on |
| 91 | + * interrupt or in n milliseconds. |
| 92 | + * @param millis: optional delay before leave the deepSleep mode (default: 0). |
| 93 | + * @retval None |
| 94 | + */ |
| 95 | +void STM32LowPower::deepSleep(uint32_t millis) |
| 96 | +{ |
| 97 | + if(millis > 0) { |
| 98 | + programRtcWakeUp(millis); |
| 99 | + } |
| 100 | + LowPower_stop(_serial); |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * @brief Enable the shutdown low power mode (STM32 shutdown or standby mode). |
| 105 | + * Exit this mode on interrupt or in n milliseconds. |
| 106 | + * @param millis: optional delay before leave the shutdown mode (default: 0). |
| 107 | + * @retval None |
| 108 | + */ |
| 109 | +void STM32LowPower::shutdown(uint32_t millis) |
| 110 | +{ |
| 111 | + if(millis > 0) { |
| 112 | + programRtcWakeUp(millis); |
| 113 | + } |
| 114 | + LowPower_shutdown(); |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * @brief Enable GPIO pin in interrupt mode. If the pin is a wakeup pin, it is |
| 119 | + * configured as wakeup source. |
| 120 | + * @param pin: pin number |
| 121 | + * @param callback: pointer to callback function. |
| 122 | + * @param mode: pin interrupt mode (HIGH, LOW, RISING, FALLING or CHANGE) |
| 123 | + * @retval None |
| 124 | + */ |
| 125 | +void STM32LowPower::attachInterruptWakeup(uint32_t pin, voidFuncPtrVoid callback, uint32_t mode) |
| 126 | +{ |
| 127 | + // all GPIO for idle (smt32 sleep) and sleep (stm32 stop) |
| 128 | + attachInterrupt(pin, callback, mode); |
| 129 | + |
| 130 | + // If Gpio is a Wake up pin activate it for deepSleep (standby stm32) and shutdown |
| 131 | + LowPower_EnableWakeUpPin(pin, mode); |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * @brief Enable a serial interface as a wakeup source. |
| 136 | + * @param serial: pointer to a HardwareSerial |
| 137 | + * @param callback: pointer to callback function called when leave the low power |
| 138 | + * mode. |
| 139 | + * @retval None |
| 140 | + */ |
| 141 | +void STM32LowPower::enableWakeupFrom(HardwareSerial *serial, voidFuncPtrVoid callback) |
| 142 | +{ |
| 143 | + if(serial != NULL) { |
| 144 | + _serial = &(serial->_serial); |
| 145 | + // Reconfigure serial for low power mode (using HSI as clock source) |
| 146 | + serial->configForLowPower(); |
| 147 | + LowPower_EnableWakeUpUart(_serial, callback); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +/** |
| 152 | + * @brief Attach a callback to a RTC alarm. |
| 153 | + * @param rtc: pointer to a STM32RTC |
| 154 | + * @param callback: pointer to callback function called when leave the low power |
| 155 | + * mode. |
| 156 | + * @retval None |
| 157 | + */ |
| 158 | +void STM32LowPower::enableWakeupFrom(STM32RTC *rtc, voidFuncPtr callback) |
| 159 | +{ |
| 160 | + rtc->attachInterrupt(callback); |
| 161 | +} |
| 162 | + |
| 163 | +/** |
| 164 | + * @brief Configure the RTC alarm |
| 165 | + * @param millis: time of the alarm in milliseconds. At least 1000ms. |
| 166 | + * @retval None |
| 167 | + */ |
| 168 | +void STM32LowPower::programRtcWakeUp(uint32_t millis) |
| 169 | +{ |
| 170 | + int epoc; |
| 171 | + uint32_t sec; |
| 172 | + STM32RTC& rtc = STM32RTC::getInstance(); |
| 173 | + |
| 174 | + if(millis > 0) { |
| 175 | + if (!rtc.isConfigured()){ |
| 176 | + // LSE must be selected as clock source to wakeup the device from shutdown mode |
| 177 | + rtc.setClockSource(STM32RTC::RTC_LSE_CLOCK); |
| 178 | + //Enable RTC |
| 179 | + rtc.begin(); |
| 180 | + } else { |
| 181 | + if (rtc.getClockSource() != STM32RTC::RTC_LSE_CLOCK) { |
| 182 | + // Save current config |
| 183 | + STM32RTC::RTC_AM_PM period; |
| 184 | + uint32_t subSeconds; |
| 185 | + uint8_t seconds, minutes, hours, weekDay, day, month, years; |
| 186 | + rtc.getDate(&weekDay, &day, &month, &years); |
| 187 | + rtc.getTime(&seconds, &minutes, &hours, &subSeconds, &period); |
| 188 | + rtc.end(); |
| 189 | + // LSE must be selected as clock source to wakeup the device from shutdown mode |
| 190 | + rtc.setClockSource(STM32RTC::RTC_LSE_CLOCK); |
| 191 | + //Enable RTC |
| 192 | + rtc.begin(period); |
| 193 | + // Restore config |
| 194 | + rtc.setTime(seconds, minutes, hours, subSeconds, period); |
| 195 | + rtc.setDate(weekDay, day, month, years); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + // convert millisecond to second |
| 200 | + sec = millis / 1000; |
| 201 | + // Minimum is 1 second |
| 202 | + if (sec == 0){ |
| 203 | + sec = 1; |
| 204 | + } |
| 205 | + |
| 206 | + epoc = rtc.getEpoch(); |
| 207 | + rtc.setAlarmEpoch( epoc + sec ); |
| 208 | + } |
| 209 | + |
| 210 | +} |
0 commit comments