diff --git a/src/rp2_common/hardware_clocks/clocks.c b/src/rp2_common/hardware_clocks/clocks.c index 8870a947..8243b6e7 100644 --- a/src/rp2_common/hardware_clocks/clocks.c +++ b/src/rp2_common/hardware_clocks/clocks.c @@ -443,14 +443,14 @@ bool check_sys_clock_khz(uint32_t freq_khz, uint *vco_out, uint *postdiv1_out, u } -void clock_get_sleep_en_gate(clock_dest_set_t *dests) { +void clock_get_sleep_en_gate(clock_dest_bitset_t *dests) { static_assert(CLOCKS_SLEEP_EN1_OFFSET == CLOCKS_SLEEP_EN0_OFFSET + 4, ""); for(uint i=0;i < bitset_word_size(&dests->bitset); i++) { bitset_write_word(&dests->bitset, i, clocks_hw->sleep_en[i]); } } -void clock_gate_sleep_en(const clock_dest_set_t *dests) { +void clock_gate_sleep_en(const clock_dest_bitset_t *dests) { static_assert(CLOCKS_SLEEP_EN1_OFFSET == CLOCKS_SLEEP_EN0_OFFSET + 4, ""); for(uint i=0;i < bitset_word_size(&dests->bitset); i++) { clocks_hw->sleep_en[i] = bitset_read_word(&dests->bitset, i); diff --git a/src/rp2_common/hardware_clocks/include/hardware/clocks.h b/src/rp2_common/hardware_clocks/include/hardware/clocks.h index 5e93e11d..115268dd 100644 --- a/src/rp2_common/hardware_clocks/include/hardware/clocks.h +++ b/src/rp2_common/hardware_clocks/include/hardware/clocks.h @@ -579,32 +579,32 @@ static inline bool set_sys_clock_khz(uint32_t freq_khz, bool required) { #include "pico/util/bitset.h" -typedef bitset_type_t(NUM_CLOCK_DESTINATIONS) clock_dest_set_t; -#define clock_dest_set_none() bitset_with_value(clock_dest_set_t, NUM_CLOCK_DESTINATIONS, 0) -#define clock_dest_set_all() bitset_with_value(clock_dest_set_t, NUM_CLOCK_DESTINATIONS, 1) +typedef bitset_type_t(NUM_CLOCK_DESTINATIONS) clock_dest_bitset_t; +#define clock_dest_bitset_none() bitset_with_value(clock_dest_bitset_t, NUM_CLOCK_DESTINATIONS, 0) +#define clock_dest_bitset_all() bitset_with_value(clock_dest_bitset_t, NUM_CLOCK_DESTINATIONS, 1) -static inline clock_dest_set_t *clock_dest_set_clear(clock_dest_set_t *dests) { +static inline clock_dest_bitset_t *clock_dest_bitset_clear(clock_dest_bitset_t *dests) { bitset_clear(&dests->bitset); return dests; } -static inline clock_dest_set_t *clock_dest_set_add_all(clock_dest_set_t *dests) { +static inline clock_dest_bitset_t *clock_dest_bitset_add_all(clock_dest_bitset_t *dests) { bitset_set_all(&dests->bitset); return dests; } -static inline clock_dest_set_t *clock_dest_set_add(clock_dest_set_t *dests, clock_dest_num_t dest) { +static inline clock_dest_bitset_t *clock_dest_bitset_add(clock_dest_bitset_t *dests, clock_dest_num_t dest) { bitset_set_bit(&dests->bitset, dest); return dests; } -static inline clock_dest_set_t *clock_dest_set_remove(clock_dest_set_t *dests, clock_dest_num_t dest) { +static inline clock_dest_bitset_t *clock_dest_bitset_remove(clock_dest_bitset_t *dests, clock_dest_num_t dest) { bitset_clear_bit(&dests->bitset, dest); return dests; } -void clock_get_sleep_en_gate(clock_dest_set_t *clocks); -void clock_gate_sleep_en(const clock_dest_set_t *clocks); +void clock_get_sleep_en_gate(clock_dest_bitset_t *clocks); +void clock_gate_sleep_en(const clock_dest_bitset_t *clocks); #define GPIO_TO_GPOUT_CLOCK_HANDLE_RP2040(gpio, default_clk_handle) \ ((gpio) == 21 ? clk_gpout0 : \ diff --git a/src/rp2_common/pico_low_power/include/pico/low_power.h b/src/rp2_common/pico_low_power/include/pico/low_power.h index 35c2ed91..d26c9ab8 100644 --- a/src/rp2_common/pico_low_power/include/pico/low_power.h +++ b/src/rp2_common/pico_low_power/include/pico/low_power.h @@ -87,7 +87,7 @@ typedef void (*low_power_pstate_resume_func)(pstate_bitset_t *pstate); * \param keep_enabled The clocks to keep enabled during sleep. * \return 0 on success, non-zero on error. */ -int low_power_sleep_until_irq(const clock_dest_set_t *keep_enabled); +int low_power_sleep_until_irq(const clock_dest_bitset_t *keep_enabled); /*! \brief Sleep until time using timer * \ingroup pico_low_power @@ -100,7 +100,7 @@ int low_power_sleep_until_irq(const clock_dest_set_t *keep_enabled); * \param exclusive Whether to only listen for the timer interrupt, or other interrupts. * \return 0 on success, non-zero on error. */ -int low_power_sleep_until_timer(timer_hw_t *timer, absolute_time_t until, const clock_dest_set_t *keep_enabled, bool exclusive); +int low_power_sleep_until_timer(timer_hw_t *timer, absolute_time_t until, const clock_dest_bitset_t *keep_enabled, bool exclusive); /*! \brief Sleep until time using default timer * \ingroup pico_low_power @@ -111,7 +111,7 @@ int low_power_sleep_until_timer(timer_hw_t *timer, absolute_time_t until, const * \param exclusive Whether to only listen for the timer interrupt, or other interrupts. * \return 0 on success, non-zero on error. */ -static inline int low_power_sleep_until_default_timer(absolute_time_t until, const clock_dest_set_t *keep_enabled, bool exclusive) { +static inline int low_power_sleep_until_default_timer(absolute_time_t until, const clock_dest_bitset_t *keep_enabled, bool exclusive) { return low_power_sleep_until_timer(PICO_DEFAULT_TIMER_INSTANCE(), until, keep_enabled, exclusive); } @@ -128,7 +128,7 @@ static inline int low_power_sleep_until_default_timer(absolute_time_t until, con * \param exclusive Whether to only listen for the GPIO interrupt, or other interrupts. * \return 0 on success, non-zero on error. */ -int low_power_sleep_until_pin_state(uint gpio_pin, bool edge, bool high, const clock_dest_set_t *keep_enabled, bool exclusive); +int low_power_sleep_until_pin_state(uint gpio_pin, bool edge, bool high, const clock_dest_bitset_t *keep_enabled, bool exclusive); /*! \brief Go dormant until time using AON timer * \ingroup pico_low_power @@ -147,7 +147,7 @@ int low_power_sleep_until_pin_state(uint gpio_pin, bool edge, bool high, const c * \param keep_enabled The clocks to keep enabled during dormant. * \return 0 on success, non-zero on error. */ -int low_power_dormant_until_aon_timer(absolute_time_t until, dormant_clock_source_t dormant_clock_source, uint src_hz, uint gpio_pin, const clock_dest_set_t *keep_enabled); +int low_power_dormant_until_aon_timer(absolute_time_t until, dormant_clock_source_t dormant_clock_source, uint src_hz, uint gpio_pin, const clock_dest_bitset_t *keep_enabled); /*! \brief Go dormant until pin state changes * \ingroup pico_low_power @@ -166,7 +166,7 @@ int low_power_dormant_until_aon_timer(absolute_time_t until, dormant_clock_sourc * \param keep_enabled The clocks to keep enabled during dormant. * \return 0 on success, non-zero on error. */ -int low_power_dormant_until_pin_state(uint gpio_pin, bool edge, bool high, dormant_clock_source_t dormant_clock_source, const clock_dest_set_t *keep_enabled); +int low_power_dormant_until_pin_state(uint gpio_pin, bool edge, bool high, dormant_clock_source_t dormant_clock_source, const clock_dest_bitset_t *keep_enabled); #if HAS_POWMAN_TIMER /*! \brief Go to Pstate until time using AON timer diff --git a/src/rp2_common/pico_low_power/low_power.c b/src/rp2_common/pico_low_power/low_power.c index 35bb857c..7736eead 100644 --- a/src/rp2_common/pico_low_power/low_power.c +++ b/src/rp2_common/pico_low_power/low_power.c @@ -54,7 +54,7 @@ static void prepare_for_clock_gating(void) { static void post_clock_gating(void) { // restore all clocks in sleep mode, to prevent other __wfi from causing issues - clock_dest_set_t all = clock_dest_set_all(); + clock_dest_bitset_t all = clock_dest_bitset_all(); clock_gate_sleep_en(&all); } @@ -150,25 +150,25 @@ static void low_power_wakeup_gpio(__unused uint gpio, __unused uint32_t event_ma low_power_wakeup(); } -static void replace_null_enable_values(const clock_dest_set_t *keep_enabled, - clock_dest_set_t *local_keep_enabled) { +static void replace_null_enable_values(const clock_dest_bitset_t *keep_enabled, + clock_dest_bitset_t *local_keep_enabled) { if (keep_enabled) { *local_keep_enabled = *keep_enabled; } else { // default to keep nothing on - *local_keep_enabled = clock_dest_set_none(); + *local_keep_enabled = clock_dest_bitset_none(); } } -static void add_library_clocks(clock_dest_set_t *local_keep_enabled) { +static void add_library_clocks(clock_dest_bitset_t *local_keep_enabled) { #if LIB_PICO_STDIO_USB || LIB_TINYUSB_HOST || LIB_TINYUSB_DEVICE // this is necessary to prevent dropping the connection #if PICO_RP2040 - clock_dest_set_add(local_keep_enabled, CLK_DEST_SYS_USBCTRL); - clock_dest_set_add(local_keep_enabled, CLK_DEST_USB_USBCTRL); + clock_dest_bitset_add(local_keep_enabled, CLK_DEST_SYS_USBCTRL); + clock_dest_bitset_add(local_keep_enabled, CLK_DEST_USB_USBCTRL); #elif PICO_RP2350 - clock_dest_set_add(local_keep_enabled, CLK_DEST_SYS_USBCTRL); - clock_dest_set_add(local_keep_enabled, CLK_DEST_USB); + clock_dest_bitset_add(local_keep_enabled, CLK_DEST_SYS_USBCTRL); + clock_dest_bitset_add(local_keep_enabled, CLK_DEST_USB); #else #error Unknown processor #endif @@ -176,8 +176,8 @@ static void add_library_clocks(clock_dest_set_t *local_keep_enabled) { #if LIB_PICO_STDIO_UART // this is only needed to prevent losing stdin while sleeping - clock_dest_set_add(local_keep_enabled, PICO_DEFAULT_UART ? CLK_DEST_PERI_UART1 : CLK_DEST_PERI_UART0); - clock_dest_set_add(local_keep_enabled, PICO_DEFAULT_UART ? CLK_DEST_SYS_UART1 : CLK_DEST_SYS_UART0); + clock_dest_bitset_add(local_keep_enabled, PICO_DEFAULT_UART ? CLK_DEST_PERI_UART1 : CLK_DEST_PERI_UART0); + clock_dest_bitset_add(local_keep_enabled, PICO_DEFAULT_UART ? CLK_DEST_SYS_UART1 : CLK_DEST_SYS_UART0); #endif } @@ -202,8 +202,8 @@ static void restore_other_interrupts(void) { } } -int low_power_sleep_until_irq(const clock_dest_set_t *keep_enabled) { - clock_dest_set_t local_keep_enabled; +int low_power_sleep_until_irq(const clock_dest_bitset_t *keep_enabled) { + clock_dest_bitset_t local_keep_enabled; replace_null_enable_values(keep_enabled, &local_keep_enabled); add_library_clocks(&local_keep_enabled); @@ -224,7 +224,7 @@ int low_power_sleep_until_irq(const clock_dest_set_t *keep_enabled) { // only the deep_sleep variant of this, as DORMANT cannot wake from TIMER int low_power_sleep_until_timer(timer_hw_t *timer, absolute_time_t until, - const clock_dest_set_t *keep_enabled, bool exclusive) { + const clock_dest_bitset_t *keep_enabled, bool exclusive) { int alarm_num = timer_hardware_alarm_claim_unused(timer, false); if (alarm_num < 0) return PICO_ERROR_INSUFFICIENT_RESOURCES; @@ -236,13 +236,13 @@ int low_power_sleep_until_timer(timer_hw_t *timer, absolute_time_t until, return 0; } - clock_dest_set_t local_keep_enabled; + clock_dest_bitset_t local_keep_enabled; replace_null_enable_values(keep_enabled, &local_keep_enabled); #if PICO_RP2040 - clock_dest_set_add(&local_keep_enabled, CLK_DEST_SYS_TIMER); + clock_dest_bitset_add(&local_keep_enabled, CLK_DEST_SYS_TIMER); #elif PICO_RP2350 - clock_dest_set_add(&local_keep_enabled, timer_get_index(timer) ? CLK_DEST_SYS_TIMER1 : CLK_DEST_SYS_TIMER0); - clock_dest_set_add(&local_keep_enabled, CLK_DEST_REF_TICKS); + clock_dest_bitset_add(&local_keep_enabled, timer_get_index(timer) ? CLK_DEST_SYS_TIMER1 : CLK_DEST_SYS_TIMER0); + clock_dest_bitset_add(&local_keep_enabled, CLK_DEST_REF_TICKS); #else #error Unknown processor #endif @@ -277,11 +277,11 @@ int low_power_sleep_until_timer(timer_hw_t *timer, absolute_time_t until, } int low_power_sleep_until_pin_state(uint gpio_pin, bool edge, bool high, - const clock_dest_set_t *keep_enabled, bool exclusive) { + const clock_dest_bitset_t *keep_enabled, bool exclusive) { event_happened = false; - clock_dest_set_t local_keep_enabled; + clock_dest_bitset_t local_keep_enabled; replace_null_enable_values(keep_enabled, &local_keep_enabled); add_library_clocks(&local_keep_enabled); @@ -444,16 +444,16 @@ void low_power_go_dormant(dormant_clock_source_t dormant_clock_source) { int low_power_dormant_until_aon_timer(absolute_time_t until, dormant_clock_source_t dormant_clock_source, uint src_hz, uint gpio_pin, - const clock_dest_set_t *keep_enabled) { + const clock_dest_bitset_t *keep_enabled) { low_power_setup_clocks_for_dormant(dormant_clock_source); - clock_dest_set_t local_keep_enabled; + clock_dest_bitset_t local_keep_enabled; replace_null_enable_values(keep_enabled, &local_keep_enabled); #if PICO_RP2040 // The RTC must be run from an external source, since the dormant source will be inactive rtc_run_from_external_source(src_hz, gpio_pin); - clock_dest_set_add(&local_keep_enabled, CLK_DEST_RTC_RTC); + clock_dest_bitset_add(&local_keep_enabled, CLK_DEST_RTC_RTC); #elif PICO_RP2350 ((void)src_hz); ((void)gpio_pin); @@ -462,7 +462,7 @@ int low_power_dormant_until_aon_timer(absolute_time_t until, else return PICO_ERROR_INVALID_ARG; - clock_dest_set_add(&local_keep_enabled, CLK_DEST_REF_POWMAN); + clock_dest_bitset_add(&local_keep_enabled, CLK_DEST_REF_POWMAN); #else #error Unknown processor #endif @@ -491,11 +491,11 @@ int low_power_dormant_until_aon_timer(absolute_time_t until, int low_power_dormant_until_pin_state(uint gpio_pin, bool edge, bool high, dormant_clock_source_t dormant_clock_source, - const clock_dest_set_t *keep_enabled) { + const clock_dest_bitset_t *keep_enabled) { low_power_setup_clocks_for_dormant(dormant_clock_source); - clock_dest_set_t local_keep_enabled; + clock_dest_bitset_t local_keep_enabled; replace_null_enable_values(keep_enabled, &local_keep_enabled); // Configure the appropriate IRQ at IO bank 0 diff --git a/test/hello_sleep/hello_sleep_gpio.c b/test/hello_sleep/hello_sleep_gpio.c index f2789f61..335199b1 100644 --- a/test/hello_sleep/hello_sleep_gpio.c +++ b/test/hello_sleep/hello_sleep_gpio.c @@ -94,12 +94,12 @@ int main() { printf("Going to non-exclusive sleep until GPIO wakeup\n"); // need to keep the timer running - clock_dest_set_t keep_enabled = clock_dest_set_none(); + clock_dest_bitset_t keep_enabled = clock_dest_bitset_none(); #if PICO_RP2040 - clock_dest_set_add(&keep_enabled, CLK_DEST_SYS_TIMER); + clock_dest_bitset_add(&keep_enabled, CLK_DEST_SYS_TIMER); #else - clock_dest_set_add(&keep_enabled, CLK_DEST_SYS_TIMER0); - clock_dest_set_add(&keep_enabled, CLK_DEST_REF_TICKS); + clock_dest_bitset_add(&keep_enabled, CLK_DEST_SYS_TIMER0); + clock_dest_bitset_add(&keep_enabled, CLK_DEST_REF_TICKS); #endif low_power_sleep_until_pin_state(PICO_DEFAULT_UART_RX_PIN, true, false, &keep_enabled, false);