Deep Sleep Retentive
Description
This example shows how to use the Deep Sleep Retentive mode in order to save power.
It sets up the wakeup source using pi_pmu_wakeup_control
as RTC.
The RTC is set to trigger after a few seconds.
The chips then goes to deep sleep retentive using pi_pmu_domain_state_change
.
To have more details about these functions you can refer to the
PMU API documentation.
When the RTC triggers, the chip wakes up and prints “Second boot”.
The memory banks are saved during the deep sleep retentive mode. The code needs to be reloaded when the chip wakes up. Hence it needs to be flashed.
To run this example, follow the Flashing guide.
For more information about low-power modes, please refer to the Using low power modes guide.
How to run
Configure CMake
cmake -B build
Flash the code on your board
cmake --build build -t flash
See more details in the Flashing guide.
Run the example
Put a jumper on the boot pins. Press the reset button of your board to execute the code flashed.
Logs will be prompt on the uart console.
Code
/**
* Copyright (c) GreenWaves Technologies 2022
* All rights reserved.
*/
/**
* Example deep sleep retentive mode
*
* The wake up is done by RTC
*/
#include "pmsis.h"
#define RTC_DIV 0x8000
#define RTC_COUNT 1
int main(void)
{
printf("\n*** Entering deep sleep example ***\n\n");
printf("First boot\n");
// We will wakup from RTC, so let's set it
struct pi_rtc_conf conf;
pi_device_t rtc;
pi_rtc_conf_init(&conf);
conf.mode = PI_RTC_MODE_TIMER;
conf.clk_div = RTC_DIV;
conf.counter = RTC_COUNT;
pi_open_from_conf(&rtc, &conf);
if (pi_rtc_open(&rtc))
{
return -1;
}
pi_rtc_timer_set(&rtc, RTC_COUNT);
pi_rtc_ioctl(&rtc, PI_RTC_TIMER_START, (void *)1);
// Set source for wakeup
pi_pmu_wakeup_control(PI_PMU_WAKEUP_RTC, 0);
// Set sleep mode and go to sleep
pi_pmu_domain_state_change(PI_PMU_DOMAIN_CHIP, PI_PMU_DOMAIN_STATE_DEEP_SLEEP_RETENTIVE, 0);
/**
* WAKING UP HERE, right after the function that 'put us' into sleep
*/
printf("Second boot\n");
printf("Exiting deep sleep retentive example\n");
return 0;
}
# Copyright (c) 2022 GreenWaves Technologies SAS
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of GreenWaves Technologies SAS nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
cmake_minimum_required(VERSION 3.19)
###############################################################################
# Panel Control
###############################################################################
set(TARGET_NAME "deep_sleep_retentive")
set(TARGET_SRCS deep_sleep_retentive.c)
###############################################################################
# CMake pre initialization
###############################################################################
set(CONFIG_GAP_SDK_HOME $ENV{GAP_SDK_HOME})
include($ENV{GAP_SDK_HOME}/utils/cmake/setup.cmake)
project(${TARGET_NAME} C ASM)
add_executable(${TARGET_NAME} ${TARGET_SRCS})
if(${TARGET_COMPILE_OPTIONS})
target_compile_options(${TARGET_NAME} PUBLIC ${TARGET_COMPILE_OPTIONS})
endif()
###############################################################################
# CMake post initialization
###############################################################################
setupos(${TARGET_NAME})