|
| 1 | +/** |
| 2 | + ************************************************** |
| 3 | + * |
| 4 | + * @file Inkplate_6_Motion_Deep_Sleep.ino |
| 5 | + * @brief How to use the low-power deep sleep feature on Inkplate 6MOTION |
| 6 | + * This sketch demonstrates the full features of Deep Sleep with all details |
| 7 | + * For a RTC+DeepSleep example check the RTC examples folder |
| 8 | + * |
| 9 | + * For info on how to quickly get started with Inkplate 6MOTION visit docs.inkplate.com |
| 10 | + * |
| 11 | + * @authors Borna Biro and Robert Soric for soldered.com |
| 12 | + * @date January 2025 |
| 13 | + ***************************************************/ |
| 14 | + |
| 15 | +// Include Inkplate Motion Library |
| 16 | +#include <InkplateMotion.h> |
| 17 | + |
| 18 | +// Include Soldered font |
| 19 | +#include "solderedFont30pt7b.h" |
| 20 | + |
| 21 | +// Create Inkplate object |
| 22 | +Inkplate inkplate; |
| 23 | + |
| 24 | +// Variable to store backup RAM data |
| 25 | +// Explained below, this is how you can store data between sleeps |
| 26 | +int value = 0; |
| 27 | + |
| 28 | +void setup() |
| 29 | +{ |
| 30 | + // Initialize Serial communication @ 115200 bauds |
| 31 | + Serial.begin(115200); |
| 32 | + |
| 33 | + // Write debug message |
| 34 | + Serial.println("Inkplate started"); |
| 35 | + |
| 36 | + // Initialize Inkplate Motion library |
| 37 | + inkplate.begin(); |
| 38 | + |
| 39 | + // Initialize STM32 RTC (needed for Backup RAM) without reseting the whole RTC |
| 40 | + inkplate.rtc.begin(RTC_HOURFORMAT_24); |
| 41 | + |
| 42 | + // Check if is this first time the example is run, reset the variable |
| 43 | + if (!inkplate.rtc.isRTCSet()) |
| 44 | + { |
| 45 | + // Initialize RTC and reset it! Otherwise, backup RAM will return wrong value |
| 46 | + inkplate.rtc.begin(RTC_HOURFORMAT_24, true); |
| 47 | + |
| 48 | + // Force RTC Set variable to run this part of the code only once |
| 49 | + // The otherway to do this is to set the clock or date |
| 50 | + inkplate.rtc.rtcSetFlag(); |
| 51 | + |
| 52 | + // Set variable stored in backup RAM to zero. This RAM is available between |
| 53 | + // system resets, but also between power cycles (if RTC battery is provided) |
| 54 | + inkplate.rtc.writeToBackupRAM(0, &value, sizeof(value)); |
| 55 | + } |
| 56 | + |
| 57 | + // Set Soldered font |
| 58 | + inkplate.setFont(&solderedFont30pt7b); |
| 59 | + |
| 60 | + // Set text color to black with white background. |
| 61 | + inkplate.setTextColor(BLACK, WHITE); |
| 62 | + |
| 63 | + // Start printing text at X = 0, Y = 50 |
| 64 | + inkplate.setCursor(0, 50); |
| 65 | + |
| 66 | + // Print the reset cause |
| 67 | + printResetCause(); |
| 68 | + |
| 69 | + // Get the new value of the variable from the backup RAM |
| 70 | + inkplate.rtc.readFromBackupRAM(0, &value, sizeof(value)); |
| 71 | + |
| 72 | + // Print some text on screen |
| 73 | + inkplate.printf("Inkplate 6 MOTION deep sleep example.\nWoken up %d times.", value++); |
| 74 | + |
| 75 | + // Store the new value |
| 76 | + inkplate.rtc.writeToBackupRAM(0, &value, sizeof(value)); |
| 77 | + |
| 78 | + // Refresh the screen |
| 79 | + inkplate.display(); |
| 80 | + |
| 81 | + // Check if the wake up from sleep did reset the board. If so, clear the flags |
| 82 | + if (__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET) |
| 83 | + { |
| 84 | + // Clear Standby flag |
| 85 | + __HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB); |
| 86 | + |
| 87 | + // Wake-up button on Inkplate 6 Motion is PC13 = WAKEUP PIN 4. |
| 88 | + HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN4); |
| 89 | + } |
| 90 | + |
| 91 | + // Enable wake up button. By setting PC13 to low (button press), Inkplate will wake up |
| 92 | + HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN4_LOW); |
| 93 | + |
| 94 | + // Print last message before goint to sleep |
| 95 | + Serial.println("Going to sleep"); |
| 96 | + Serial.flush(); |
| 97 | + |
| 98 | + // Finally, enter in sleep mode (deep sleep, lowest current consumption, but data is not retained, after wake up, |
| 99 | + // code starts from begining) |
| 100 | + deepSleep(); // Code stops here! |
| 101 | +} |
| 102 | + |
| 103 | +void loop() |
| 104 | +{ |
| 105 | + // Nothing! Must be empty! |
| 106 | +} |
| 107 | + |
| 108 | +// This fucntion demonstrates the different reset causes |
| 109 | +// How to read them, and what they represent |
| 110 | +void printResetCause(void) |
| 111 | +{ |
| 112 | + uint32_t reset_cause = RCC->RSR; // Read the RSR register |
| 113 | + |
| 114 | + inkplate.print("Reset cause(s):\n"); |
| 115 | + |
| 116 | + if (reset_cause & RCC_RSR_CPURSTF) |
| 117 | + { |
| 118 | + inkplate.printf("-CPU Reset\n"); |
| 119 | + } |
| 120 | + |
| 121 | + if (reset_cause & RCC_RSR_D1RSTF) |
| 122 | + { |
| 123 | + inkplate.printf("-Deep sleep reset (D1)\n"); |
| 124 | + } |
| 125 | + |
| 126 | + if (reset_cause & RCC_RSR_D2RSTF) |
| 127 | + { |
| 128 | + inkplate.printf("-Deep sleep reset (D2)\n"); |
| 129 | + } |
| 130 | + |
| 131 | + if (reset_cause & RCC_RSR_BORRSTF) |
| 132 | + { |
| 133 | + inkplate.printf("-Brownout reset\n"); |
| 134 | + } |
| 135 | + |
| 136 | + if (reset_cause & RCC_RSR_PINRSTF) |
| 137 | + { |
| 138 | + inkplate.printf("-Pin reset\n"); |
| 139 | + } |
| 140 | + |
| 141 | + if (reset_cause & RCC_RSR_PORRSTF) |
| 142 | + { |
| 143 | + inkplate.printf("-Power-on reset\n"); |
| 144 | + } |
| 145 | + |
| 146 | + if (reset_cause & RCC_RSR_SFTRSTF) |
| 147 | + { |
| 148 | + inkplate.printf("-Software reset\n"); |
| 149 | + } |
| 150 | + |
| 151 | + if (reset_cause & RCC_RSR_IWDG1RSTF) |
| 152 | + { |
| 153 | + inkplate.printf("-Independent watchdog reset\n"); |
| 154 | + } |
| 155 | + |
| 156 | + if (reset_cause & RCC_RSR_WWDG1RSTF) |
| 157 | + { |
| 158 | + inkplate.printf("-Window watchdog reset\n"); |
| 159 | + } |
| 160 | + |
| 161 | + |
| 162 | + if (reset_cause & RCC_RSR_LPWRRSTF) |
| 163 | + { |
| 164 | + inkplate.printf("-Low-power reset\n"); |
| 165 | + } |
| 166 | + |
| 167 | + // Clear the reset flags |
| 168 | + __HAL_RCC_CLEAR_RESET_FLAGS(); |
| 169 | +} |
| 170 | + |
| 171 | +// Deep sleep function. Calling this will put STM32 in deep sleep mode. If all peripherals are disabled |
| 172 | +// deep sleep current will be around 20-30uA |
| 173 | +void deepSleep() |
| 174 | +{ |
| 175 | + // Disable all peripherals including WiFi, microSD card, SDRAM, sensors etc |
| 176 | + inkplate.peripheralState(INKPLATE_PERIPHERAL_ALL, false); |
| 177 | + |
| 178 | + // Disable USB voltage detection on USB OTG (This causes high current consumption in sleep!) |
| 179 | + HAL_PWREx_DisableUSBVoltageDetector(); |
| 180 | + |
| 181 | + // Use different voltage scale for internal voltage regulator (lower current consumption in sleep mode) |
| 182 | + HAL_PWREx_ControlStopModeVoltageScaling(PWR_REGULATOR_SVOS_SCALE5); |
| 183 | + |
| 184 | + // Put every domain into standby mode |
| 185 | + HAL_PWREx_EnterSTANDBYMode(PWR_D3_DOMAIN); |
| 186 | + HAL_PWREx_EnterSTANDBYMode(PWR_D2_DOMAIN); |
| 187 | + HAL_PWREx_EnterSTANDBYMode(PWR_D1_DOMAIN); |
| 188 | +} |
0 commit comments