This is the quick start guide for the Sleep Driver, with step-by-step instructions on how to configure and use the driver for a specific use case.
The section described below can be copied into, e.g. the main application loop or any other function that will need to control and execute different sleep modes on the device.
Basic usage of the sleep driver
This use case will prepare the device to enter the Power Down sleep mode and then enter the sleep mode. After waking up it will disable sleep.
Workflow
Example code
Add to, e.g., the main loop in the application C-file:
Workflow
- Set what sleep mode to use, the different sleep modes can be found in the device header file under the enum definition SLEEP_SMODE_enum:
- Enable that the device are allowed to go to sleep:
- Note
- This function has to be called in order for the device to go to sleep. This is a safety feature to stop the device to go to sleep unintentionally, even though it is possible to have this enabled at all times it is recommended to enable sleep mode only when you intend to go to sleep within a few clock cycles.
- Enter sleep mode:
- Attention
- Make sure to enable global interrupt and the interrupt you plan to use as wake-up source for your device, do also pay special attention to what wake-up sources are available for the different sleep modes. Failing to enable interrupts may result in indefinite sleep until power is cycled!
- When the device is woken from sleep it will execute the interrupt handler related to the wakeup-source (interrupt source) and continue on the next line of code after the sleep_enter() call. Make sure to disable sleep when waking up.
Sleep Modes
Possible sleep modes depend on the device that is used. Please refer to the device datasheet and header file to find these definitions.
As an example the ATxmega32A4U device has the following sleep modes:
- Idle sleep: SLEEP_SMODE_IDLE_gc
- Power Down: SLEEP_SMODE_PDOWN_gc
- Power Save: SLEEP_SMODE_PSAVE_gc
- Standby: SLEEP_SMODE_STDBY_gc
- Extended standby: SLEEP_SMODE_ESTDBY_gc