Light Sleep

Description

This example shows how to use the Light 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 light 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 print “Second boot”.

The memory banks are saved during the light sleep mode. The code needs to be reloaded when the chip wakes up. Hence it needs to be flashed.

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 light sleep 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 light 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);

    /* Uncomment the function below to keep clocks as they are during sleep.
     * Otherwise, the slow clock will be the only clock on, to save power.
     */
    //pi_pmu_sleep_clocks_keep();

    // 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_LIGHT_SLEEP, 0);

    /**
     * WAKING UP HERE, right after the function that 'put us' into sleep
     */

    printf("Second boot\n");

    printf("Exiting light sleep example\n");
    return 0;
}