Deep Sleep
Requirements
In order to run this example, you must have an GAP9_EVK board.
Description
This example shows how to use the Deep Sleep 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 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 blinks the LED for 10 times. It then sets up the deep sleep and RTC wakeup again.
The memory banks are not saved during the deep sleep 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
#include "pmsis.h"
#define PAD_GPIO_LED2 (PI_PAD_086)
#define BLINK_DELAY_US (500 * 1000)
#define BLINK_ITERATIONS (10)
#define TIMER_COUNTER_SECONDS (5)
static int8_t blink_led()
{
#if defined(BLINK_LED)
/* set pad to gpio mode */
pi_pad_function_set(PAD_GPIO_LED2, PI_PAD_FUNC1);
/* configure gpio output */
pi_gpio_flags_e flags = PI_GPIO_OUTPUT;
pi_gpio_pin_configure(PAD_GPIO_LED2, flags);
/* blink the LED */
for (uint8_t i = 0; i < BLINK_ITERATIONS; i++)
{
pi_gpio_pin_write(PAD_GPIO_LED2, 1);
pi_time_wait_us(BLINK_DELAY_US);
pi_gpio_pin_write(PAD_GPIO_LED2, 0);
pi_time_wait_us(BLINK_DELAY_US);
}
/* Due to an EVK known issue, the LED2 must be 1 */
pi_gpio_pin_write(PAD_GPIO_LED2, 1);
#endif
return 0;
}
static void go_to_sleep(void)
{
/* force outputs during sleep to avoid random communications */
pi_pad_sleep_cfg_force(1);
/* set wakeup source */
pi_pmu_wakeup_control(PI_PMU_WAKEUP_RTC, 0);
/* configure RTC */
{
struct pi_rtc_conf conf;
pi_device_t rtc;
pi_rtc_conf_init(&conf);
conf.mode = PI_RTC_MODE_TIMER;
conf.counter = TIMER_COUNTER_SECONDS;
pi_open_from_conf(&rtc, &conf);
if (pi_rtc_open(&rtc))
{
pmsis_exit(-1);
}
uint32_t repeat = 1;
pi_rtc_ioctl(&rtc, PI_RTC_TIMER_START, (void*) repeat);
}
/* go to deep sleep */
pi_pmu_domain_state_change(PI_PMU_DOMAIN_CHIP, PI_PMU_DOMAIN_STATE_DEEP_SLEEP, 0);
}
int main(void)
{
/* Release the outputs that we forced in case we come back from deep sleep */
pi_pad_sleep_cfg_force(0);
/* Get boot source */
switch(pi_pmu_boot_state_get())
{
case PI_PMU_DOMAIN_STATE_DEEP_SLEEP:
{
blink_led();
}
break;
default:
break;
}
/* enter the deep sleep mode */
go_to_sleep();
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")
set(TARGET_SRCS deep_sleep.c)
###############################################################################
# CMake pre initialization
###############################################################################
include($ENV{GAP_SDK_HOME}/utils/cmake/setup.cmake)
project(${TARGET_NAME} C ASM)
add_executable(${TARGET_NAME} ${TARGET_SRCS})
if (NOT CONFIG_PLATFORM_GVSOC AND NOT CONFIG_CI)
list(APPEND TARGET_COMPILE_OPTIONS "-DBLINK_LED")
endif()
if(${TARGET_COMPILE_OPTIONS})
target_compile_options(${TARGET_NAME} PUBLIC ${TARGET_COMPILE_OPTIONS})
endif()
###############################################################################
# CMake post initialization
###############################################################################
setupos(${TARGET_NAME})