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

  1. Configure CMake

cmake -B build
  1. Flash the code on your board

cmake --build build -t flash

See more details in the Flashing guide.

  1. 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;
}