Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8609

General • Re: Trouble using SDK powman to enter low power

$
0
0
I used the pico-example file powman_example.c and ran it in VS Code with the Pi Debug Probe. This code runs properly under the debugger, but fails when I put it in flash. I change the stdio configuration in cmakelists.txt to output to the USB for the flash version but that is the only difference. I get this output when running in the debugger:
Wake up, test run: 52
Awake for 5000ms
Powering off for 5000ms
Wake up, test run: 53
Awake for 5000ms
Powering off for 5000ms
Wake up, test run: 54
Awake for 5000ms
Powering off for 5000ms

and I get only this output when running in flash:
Powering off for 5000ms
Then it stops, or at least the output stops.

(because it takes me a while to restart the putty session after plugging in the USB)

Code:

#include <stdio.h>#include <inttypes.h>#include "pico/stdio.h"#include "pico/sync.h"#include "hardware/gpio.h"#include "hardware/powman.h"#include "powman_example.h"static powman_power_state off_state;static powman_power_state on_state;// Initialise everythingvoid powman_example_init(uint64_t abs_time_ms) {    // start powman and set the time    powman_timer_start();    powman_timer_set_ms(abs_time_ms);    powman_timer_set_1khz_tick_source_lposc();  // use LPOSC so timer survives low power    // Allow power down when debugger connected    //powman_set_debug_power_request_ignored(true);    // Power states    powman_power_state P1_7 = POWMAN_POWER_STATE_NONE;    powman_power_state P0_3 = POWMAN_POWER_STATE_NONE;    P0_3 = powman_power_state_with_domain_on(P0_3, POWMAN_POWER_DOMAIN_SWITCHED_CORE);    P0_3 = powman_power_state_with_domain_on(P0_3, POWMAN_POWER_DOMAIN_XIP_CACHE);    P0_3 = powman_power_state_with_domain_on(P0_3, POWMAN_POWER_DOMAIN_SRAM_BANK0);    P0_3 = powman_power_state_with_domain_on(P0_3, POWMAN_POWER_DOMAIN_SRAM_BANK1);    off_state = P1_7;    on_state = P0_3;}// Initiate power offstatic int powman_example_off(void) {    // Get ready to power off    stdio_flush();    stdio_deinit_all(); // turn off USB before sleep    // Set power states    bool valid_state = powman_configure_wakeup_state(off_state, on_state);    if (!valid_state) {        return PICO_ERROR_INVALID_STATE;    }    // reboot to main    powman_hw->boot[0] = 0;    powman_hw->boot[1] = 0;    powman_hw->boot[2] = 0;    powman_hw->boot[3] = 0;    // Switch to required power state    int rc = powman_set_power_state(off_state);    if (rc != PICO_OK) {        return rc;    }    // Power down    while (true) __wfi();}// Power off until a gpio goes highint powman_example_off_until_gpio_high(int gpio) {    gpio_init(gpio);    gpio_set_dir(gpio, false);    if (gpio_get(gpio)) {        printf("Waiting for gpio %d to go low\n", gpio);        while(gpio_get(gpio)) {            sleep_ms(100);        }    }    printf("Powering off until GPIO %d goes high\n", gpio);    powman_enable_gpio_wakeup(1, gpio, false, true);    return powman_example_off();}// Power off until a gpio goes lowint powman_example_off_until_gpio_low(int gpio) {    gpio_init(gpio);    gpio_set_dir(gpio, false);    if (!gpio_get(gpio)) {        printf("Waiting for gpio %d to go high\n", gpio);        while(!gpio_get(gpio)) {            sleep_ms(100);        }    }    printf("Powering off until GPIO %d goes low\n", gpio);    powman_enable_gpio_wakeup(1, gpio, false, false);    return powman_example_off();}// Power off until an absolute timeint powman_example_off_until_time(uint64_t abs_time_ms) {    // Start powman timer and turn off    printf("Powering off for %"PRIu64"ms\n", abs_time_ms - powman_timer_get_ms());    powman_enable_alarm_wakeup_at_ms(abs_time_ms);    return powman_example_off();}// Power off for a number of millisecondsint powman_example_off_for_ms(uint64_t duration_ms) {    uint64_t ms = powman_timer_get_ms();    return powman_example_off_until_time(ms + duration_ms);}#ifndef WAKEUP_GPIO#define WAKEUP_GPIO 15#endif// How long to wait#define PAUSE_TIME_MS 5000// Got to sleep and wakeup when a GPIO goes high// The example will repeatedly wait until GPIO 15 is low then switch off until GPIO 15 goes high// To test this you can connect the GPIO to ground and then 3V3(OUT)// The debugger will appear to be unresponsive while the device is offint main() {    stdio_init_all();    sleep_ms(2000); // Allow USB CDC to settle    // Initialise the example    powman_example_init(1704067200000);    // Scratch register survives power down    printf("Wake up, test run: %u\n", powman_hw->scratch[0]++);    // Stay awake for a few seconds    printf("Awake for %dms\n", PAUSE_TIME_MS);    sleep_ms(PAUSE_TIME_MS);    // power off    //int rc = powman_example_off_until_gpio_high(WAKEUP_GPIO);    int rc = powman_example_off_for_ms(5000);    hard_assert(rc == PICO_OK);    hard_assert(false); // should never get here!    return 0;}

Statistics: Posted by jderickson — Fri Sep 05, 2025 5:43 am



Viewing all articles
Browse latest Browse all 8609

Trending Articles