Power Management
TinyCLR exposes several power modes for trading off responsiveness against energy consumption — useful for battery-powered devices, deep-sleep IoT nodes, and anything that needs to nap until something interesting happens.
NuGet packages: GHIElectronics.TinyCLR.Native for the Power API, plus GHIElectronics.TinyCLR.Devices.Gpio and GHIElectronics.TinyCLR.Pins when wiring wake-up interrupts.
Idle
When the runtime has nothing to do — all threads sleeping or blocked on events — the system automatically enters idle. You don't call anything; this is the default state between active work.
Slow clock speed
The CPU can run at half speed, cutting power use by about 40%. Switching requires a soft reset, and the change can optionally persist across resets.
using GHIElectronics.TinyCLR.Native;
var persistClock = false;
if (Power.GetSystemClock() == SystemClock.High) {
Power.SetSystemClock(SystemClock.Low, persistClock);
Power.Reset();
}
Switch back to full speed (only works if persistClock was false when you switched down):
if (Power.GetSystemClock() == SystemClock.Low) {
Power.SetSystemClock(SystemClock.High, persist: false);
Power.Reset();
}
When not persisted, Power.Reset() keeps the current clock speed, but a hardware reset (reset pin) or a power cycle reverts to the default.
Sleep
Sleep disables most of the system to drop power consumption substantially. A GPIO edge or the RTC can wake the device and resume execution where it left off.
You must register an interrupt handler on the wake-up pin before calling Power.Sleep() — otherwise the system has no idea what edge to wake on.
using GHIElectronics.TinyCLR.Devices.Gpio;
using GHIElectronics.TinyCLR.Native;
using GHIElectronics.TinyCLR.Pins;
using System.Diagnostics;
var ldrButton = GpioController.GetDefault().OpenPin(SC20100.GpioPin.PE3);
ldrButton.SetDriveMode(GpioPinDriveMode.InputPullUp);
ldrButton.ValueChanged += LdrButton_ValueChanged;
Debug.WriteLine("Going to sleep...");
Power.Sleep();
Debug.WriteLine("Woken up");
// Handler can be empty — its existence is what arms the interrupt for wake-up.
void LdrButton_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e) { }
Sleep for a specific time using the RTC:
Power.Sleep(DateTime.Now.AddSeconds(90)); // Wake in 90 seconds.
Shutdown
The deepest power state. The system is completely off, internal pull-ups are disabled, and the only ways to wake it are:
- A reset
- A power cycle
- The RTC alarm firing
- A configured edge on the WKUP pin
Waking from Shutdown always resets the system. Your application starts over from scratch — it doesn't resume where it left off. Anything you need across the sleep has to live in battery-backed memory or external storage.
Internal pull-ups are off in Shutdown, so add an external pull-up or pull-down to the WKUP pin to give it a stable default state — otherwise spurious wake-ups are possible.
Shutdown until WKUP changes:
Power.Shutdown(true, DateTime.MaxValue);
By default the system wakes on a rising edge of WKUP. Switch to falling edge:
Power.WakeupEdge = WakeupEdge.Falling;
Shutdown for a fixed time (WKUP pin ignored):
Power.Shutdown(false, DateTime.Now.AddSeconds(90));
Shutdown for a fixed time or until WKUP changes (whichever comes first):
Power.Shutdown(true, DateTime.Now.AddSeconds(90));
Disabling on-board Ethernet PHY
Modules with an on-board Ethernet PHY draw power even when networking isn't in use. Pulling a specific GPIO pin low disables the PHY:
| Module | Disable pin |
|---|---|
| SCM20260E/D | PG3 |
| SCM20100E | PD8 |
Software reset
Reset the application from code without a hardware reset:
using GHIElectronics.TinyCLR.Native;
Power.Reset(); // Reset application.
Power.Reset(false); // Reset into GHI Bootloader mode.
The bootloader-mode reset is useful for triggering firmware updates from inside the application — for example, from an in-app "Check for updates" menu.
Identifying the previous reset cause
After boot, GetResetSource returns what triggered the last reset — useful for distinguishing a watchdog reset, a brown-out, a user reset button press, or a clean power-up.
var resetSource = Power.GetResetSource();
API reference
| Namespace | Description |
|---|---|
| GHIElectronics.TinyCLR.Native | Power mode and low-power sleep classes |