use rp2350_pac::POWMAN;
use crate::Steal;
pub const POWMAN_PASSWORD: u32 = 0x5AFE_0000;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(u32)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum PowerDomain {
SramBank1 = 0,
SramBank0 = 1,
XipCache = 2,
SwitchedCore = 3,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum WakeupMode {
Level,
Edge,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum WakeupDirection {
Low,
High,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(u32)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum VregTempThreshold {
Temp100C = 0,
Temp105C = 1,
Temp110C = 2,
Temp115C = 3,
Temp120C = 4,
Temp125C = 5,
Temp135C = 6,
Temp150C = 7,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Voltage {
V0_55 = 0b00000,
V0_60 = 0b00001,
V0_65 = 0b00010,
V0_70 = 0b00011,
V0_75 = 0b00100,
V0_80 = 0b00101,
V0_85 = 0b00110,
V0_90 = 0b00111,
V0_95 = 0b01000,
V1_00 = 0b01001,
V1_05 = 0b01010,
V1_10 = 0b01011,
V1_15 = 0b01100,
V1_20 = 0b01101,
V1_25 = 0b01110,
V1_30 = 0b01111,
V1_35 = 0b10000,
V1_40 = 0b10001,
V1_50 = 0b10010,
V1_60 = 0b10011,
V1_65 = 0b10100,
V1_70 = 0b10101,
V1_80 = 0b10110,
V1_90 = 0b10111,
V2_00 = 0b11000,
V2_35 = 0b11001,
V2_50 = 0b11010,
V2_65 = 0b11011,
V2_80 = 0b11100,
V3_00 = 0b11101,
V3_15 = 0b11110,
V3_30 = 0b11111,
}
impl Voltage {
pub const MIN: Self = Voltage::V0_85;
pub const DEFAULT: Self = Voltage::V1_10;
pub const MAX: Self = Voltage::V1_30;
}
pub type PowerState = u32;
pub struct Powman {
powman: POWMAN,
}
impl Powman {
#[inline]
pub fn init(powman: POWMAN) -> Self {
Self { powman }
}
#[inline]
pub unsafe fn steal() -> Steal<Self> {
Steal(Self::init(unsafe { POWMAN::steal() }))
}
#[inline]
pub unsafe fn regs(&self) -> &rp2350_pac::powman::RegisterBlock {
&self.powman
}
#[inline]
pub fn release(self) -> POWMAN {
self.powman
}
pub fn set_voltage(&self, voltage: Voltage) {
if voltage as u8 > Voltage::MAX as u8 {
assert!(
self.powman.vreg_ctrl.read_bits()
& rp2350_pac::powman::VregCtrl::DISABLE_VOLTAGE_LIMIT
!= 0,
"Voltage above 1.30 V requires calling set_voltage_limit_disabled(true) first"
);
}
self.powman
.vreg_ctrl
.write_bits(POWMAN_PASSWORD | rp2350_pac::powman::VregCtrl::UNLOCK);
crate::spin::spin_wait("prior VREG update", || {
self.powman.vreg.read_bits() & rp2350_pac::powman::Vreg::UPDATE_IN_PROGRESS == 0
});
let vsel = (voltage as u32) << rp2350_pac::powman::Vreg::VSEL_OFFSET;
self.powman.vreg.write_bits(POWMAN_PASSWORD | vsel);
crate::spin::spin_wait("VREG update", || {
self.powman.vreg.read_bits() & rp2350_pac::powman::Vreg::UPDATE_IN_PROGRESS == 0
});
}
#[inline]
pub fn voltage(&self) -> Voltage {
let raw = self.powman.vreg.vsel() as u8;
unsafe { core::mem::transmute(raw) }
}
#[inline]
pub fn is_vreg_ok(&self) -> bool {
self.powman.vreg_sts.vout_ok()
}
#[inline]
pub fn is_vreg_starting(&self) -> bool {
self.powman.vreg_sts.startup()
}
pub fn wait_vreg_stable(&self) {
crate::spin::spin_wait("VREG stable", || {
self.is_vreg_ok() && !self.is_vreg_starting()
});
}
pub fn set_vreg_temp_threshold(&self, threshold: VregTempThreshold) {
self.powman.vreg_ctrl.write_ht_th(threshold as u32);
}
pub fn set_voltage_limit_disabled(&self, disabled: bool) {
if disabled {
self.powman.vreg_ctrl.set_disable_voltage_limit();
} else {
self.powman.vreg_ctrl.clr_disable_voltage_limit();
}
}
#[inline]
pub fn enable_bod(&self) {
self.powman.bod.set_en();
}
#[inline]
pub fn disable_bod(&self) {
self.powman.bod.clr_en();
}
pub fn set_bod_threshold(&self, vsel: u8) {
assert!(vsel <= 31, "BOD threshold must be 0-31, got {}", vsel);
self.powman.bod.write_vsel(vsel as u32);
}
pub fn lposc_freq(&self) -> u32 {
32768
}
pub fn timer_set_1khz_tick_source_lposc(&self) {
self.timer_set_1khz_tick_source_lposc_with_hz(32768);
}
pub fn timer_set_1khz_tick_source_lposc_with_hz(&self, lposc_freq_hz: u32) {
let was_running = self.timer_is_running();
if was_running {
self.timer_stop();
}
let freq_khz = lposc_freq_hz / 1000;
let freq_khz_frac = (lposc_freq_hz % 1000) * 65536 / 1000;
self.powman
.lposc_freq_khz_int
.write_bits(POWMAN_PASSWORD | (freq_khz & 0xFFFF));
self.powman
.lposc_freq_khz_frac
.write_bits(POWMAN_PASSWORD | (freq_khz_frac & 0xFFFF));
const USE_LPOSC: u32 = 1 << 9; self.powman.timer.set_bits(POWMAN_PASSWORD | USE_LPOSC);
if was_running {
self.timer_start();
const USING_LPOSC: u32 = 1 << 5; crate::spin::spin_wait("LPOSC tick source active", || {
self.powman.timer.read_bits() & USING_LPOSC != 0
});
}
}
pub fn timer_set_1khz_tick_source_xosc(&self) {
self.timer_set_1khz_tick_source_xosc_with_hz(12_000_000);
}
pub fn timer_set_1khz_tick_source_xosc_with_hz(&self, xosc_freq_hz: u32) {
let was_running = self.timer_is_running();
if was_running {
self.timer_stop();
}
let freq_khz = xosc_freq_hz / 1000;
let freq_khz_frac = (xosc_freq_hz % 1000) * 65536 / 1000;
self.powman
.xosc_freq_khz_int
.write_bits(POWMAN_PASSWORD | (freq_khz & 0xFFFF));
self.powman
.xosc_freq_khz_frac
.write_bits(POWMAN_PASSWORD | (freq_khz_frac & 0xFFFF));
const USE_XOSC: u32 = 1 << 8; self.powman.timer.set_bits(POWMAN_PASSWORD | USE_XOSC);
if was_running {
self.timer_start();
const USING_XOSC: u32 = 1 << 4; crate::spin::spin_wait("XOSC tick source active", || {
self.powman.timer.read_bits() & USING_XOSC != 0
});
}
}
pub fn timer_set_1khz_tick_source_gpio(&self, gpio: u8) {
let source = match gpio {
12 => 0,
20 => 1,
14 => 2,
22 => 3,
other => panic!(
"invalid 1kHz GPIO source {} (must be 12, 14, 20, or 22)",
other
),
};
let was_running = self.timer_is_running();
if was_running {
self.timer_stop();
}
self.powman
.ext_time_ref
.write_bits(POWMAN_PASSWORD | source);
const USE_GPIO_1KHZ: u32 = 1 << 10; self.powman.timer.set_bits(POWMAN_PASSWORD | USE_GPIO_1KHZ);
if was_running {
self.timer_start();
}
}
#[inline]
pub fn timer_is_running(&self) -> bool {
(self.powman.timer.read_bits() & 1) != 0 }
pub fn timer_start(&self) {
const RUN: u32 = 1; self.powman.timer.set_bits(POWMAN_PASSWORD | RUN);
}
pub fn timer_stop(&self) {
const RUN: u32 = 1; self.powman.timer.clr_bits(POWMAN_PASSWORD | RUN);
}
pub fn timer_get_ms(&self) -> u64 {
let mut hi = self.powman.read_time_upper.read_bits();
loop {
let lo = self.powman.read_time_lower.read_bits();
let next_hi = self.powman.read_time_upper.read_bits();
if hi == next_hi {
return ((hi as u64) << 32) | (lo as u64);
}
hi = next_hi;
}
}
pub fn timer_set_ms(&self, time_ms: u64) {
let was_running = self.timer_is_running();
if was_running {
self.timer_stop();
}
self.powman
.set_time_15to0
.write_bits(POWMAN_PASSWORD | ((time_ms & 0xFFFF) as u32));
self.powman
.set_time_31to16
.write_bits(POWMAN_PASSWORD | (((time_ms >> 16) & 0xFFFF) as u32));
self.powman
.set_time_47to32
.write_bits(POWMAN_PASSWORD | (((time_ms >> 32) & 0xFFFF) as u32));
self.powman
.set_time_63to48
.write_bits(POWMAN_PASSWORD | (((time_ms >> 48) & 0xFFFF) as u32));
if was_running {
self.timer_start();
}
}
pub fn timer_enable_alarm_at_ms(&self, alarm_time_ms: u64) {
const TIMER_INT: u32 = 1 << 0; self.powman.inte.set_bits(POWMAN_PASSWORD | TIMER_INT);
const ALARM_ENAB: u32 = 1 << 2; self.powman.timer.clr_bits(POWMAN_PASSWORD | ALARM_ENAB);
self.powman
.alarm_time_15to0
.write_bits(POWMAN_PASSWORD | ((alarm_time_ms & 0xFFFF) as u32));
self.powman
.alarm_time_31to16
.write_bits(POWMAN_PASSWORD | (((alarm_time_ms >> 16) & 0xFFFF) as u32));
self.powman
.alarm_time_47to32
.write_bits(POWMAN_PASSWORD | (((alarm_time_ms >> 32) & 0xFFFF) as u32));
self.powman
.alarm_time_63to48
.write_bits(POWMAN_PASSWORD | (((alarm_time_ms >> 48) & 0xFFFF) as u32));
self.timer_clear_alarm();
self.powman.timer.set_bits(POWMAN_PASSWORD | ALARM_ENAB);
}
pub fn timer_disable_alarm(&self) {
const TIMER_INT: u32 = 1 << 0;
const ALARM_ENAB: u32 = 1 << 2;
self.powman.inte.clr_bits(POWMAN_PASSWORD | TIMER_INT);
self.powman.timer.clr_bits(POWMAN_PASSWORD | ALARM_ENAB);
}
pub fn timer_clear_alarm(&self) {
const ALARM: u32 = 1 << 1; self.powman.timer.clr_bits(POWMAN_PASSWORD | ALARM);
}
#[inline]
pub fn timer_alarm_fired(&self) -> bool {
(self.powman.timer.read_bits() & (1 << 1)) != 0
}
pub fn enable_alarm_wakeup_at_ms(&self, alarm_time_ms: u64) {
self.timer_enable_alarm_at_ms(alarm_time_ms);
const PWRUP_ON_ALARM: u32 = 1 << 3; self.powman.timer.set_bits(POWMAN_PASSWORD | PWRUP_ON_ALARM);
}
pub fn disable_alarm_wakeup(&self) {
self.timer_disable_alarm();
const PWRUP_ON_ALARM: u32 = 1 << 3;
self.powman.timer.clr_bits(POWMAN_PASSWORD | PWRUP_ON_ALARM);
}
pub fn enable_gpio_wakeup(
&self,
wakeup_num: u8,
gpio: u8,
mode: WakeupMode,
direction: WakeupDirection,
) {
assert!(wakeup_num < 4, "wakeup_num must be 0-3, got {}", wakeup_num);
const ENABLE: u32 = 1 << 6;
const DIRECTION: u32 = 1 << 7;
const MODE: u32 = 1 << 8;
const STATUS: u32 = 1 << 9;
let mode_bits = match mode {
WakeupMode::Level => 0,
WakeupMode::Edge => MODE,
};
let direction_bits = match direction {
WakeupDirection::Low => 0,
WakeupDirection::High => DIRECTION,
};
let value = mode_bits | direction_bits | ((gpio as u32) & 0x3F);
macro_rules! configure {
($reg:ident) => {{
self.powman.$reg.write_bits(POWMAN_PASSWORD | value);
self.powman.$reg.set_bits(POWMAN_PASSWORD | STATUS);
self.powman.$reg.set_bits(POWMAN_PASSWORD | ENABLE);
}};
}
match wakeup_num {
0 => configure!(pwrup0),
1 => configure!(pwrup1),
2 => configure!(pwrup2),
3 => configure!(pwrup3),
_ => {}
}
}
pub fn disable_gpio_wakeup(&self, wakeup_num: u8) {
assert!(wakeup_num < 4, "wakeup_num must be 0-3, got {}", wakeup_num);
const ENABLE: u32 = 1 << 6;
match wakeup_num {
0 => self.powman.pwrup0.clr_bits(POWMAN_PASSWORD | ENABLE),
1 => self.powman.pwrup1.clr_bits(POWMAN_PASSWORD | ENABLE),
2 => self.powman.pwrup2.clr_bits(POWMAN_PASSWORD | ENABLE),
3 => self.powman.pwrup3.clr_bits(POWMAN_PASSWORD | ENABLE),
_ => {}
}
}
pub fn disable_all_wakeups(&self) {
for i in 0..4 {
self.disable_gpio_wakeup(i);
}
self.disable_alarm_wakeup();
}
pub fn power_state(&self) -> PowerState {
(!self.powman.state.read_bits()) & 0xF
}
pub fn is_domain_on(&self, domain: PowerDomain) -> bool {
(self.power_state() & (1 << domain as u32)) != 0
}
pub fn state_with_domain_on(state: PowerState, domain: PowerDomain) -> PowerState {
state | (1 << domain as u32)
}
pub fn state_with_domain_off(state: PowerState, domain: PowerDomain) -> PowerState {
state & !(1 << domain as u32)
}
pub fn write_scratch(&self, index: u8, value: u32) {
assert!(index < 8, "scratch index must be 0-7, got {}", index);
match index {
0 => self.powman.scratch0.write_bits(value),
1 => self.powman.scratch1.write_bits(value),
2 => self.powman.scratch2.write_bits(value),
3 => self.powman.scratch3.write_bits(value),
4 => self.powman.scratch4.write_bits(value),
5 => self.powman.scratch5.write_bits(value),
6 => self.powman.scratch6.write_bits(value),
7 => self.powman.scratch7.write_bits(value),
_ => unreachable!(),
}
}
pub fn read_scratch(&self, index: u8) -> u32 {
assert!(index < 8, "scratch index must be 0-7, got {}", index);
match index {
0 => self.powman.scratch0.read_bits(),
1 => self.powman.scratch1.read_bits(),
2 => self.powman.scratch2.read_bits(),
3 => self.powman.scratch3.read_bits(),
4 => self.powman.scratch4.read_bits(),
5 => self.powman.scratch5.read_bits(),
6 => self.powman.scratch6.read_bits(),
7 => self.powman.scratch7.read_bits(),
_ => unreachable!(),
}
}
#[inline]
pub fn timer_irq_raw(&self) -> bool {
self.powman.intr.timer()
}
#[inline]
pub fn vreg_output_low_irq_raw(&self) -> bool {
self.powman.intr.vreg_output_low()
}
#[inline]
pub fn state_req_ignored_irq_raw(&self) -> bool {
self.powman.intr.state_req_ignored()
}
#[inline]
pub fn pwrup_while_waiting_irq_raw(&self) -> bool {
self.powman.intr.pwrup_while_waiting()
}
#[inline]
pub fn pwrup0_status(&self) -> bool {
self.powman.pwrup0.status()
}
#[inline]
pub fn pwrup1_status(&self) -> bool {
self.powman.pwrup1.status()
}
#[inline]
pub fn pwrup2_status(&self) -> bool {
self.powman.pwrup2.status()
}
#[inline]
pub fn pwrup3_status(&self) -> bool {
self.powman.pwrup3.status()
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Timespec {
pub tv_sec: i64,
pub tv_nsec: i64,
}
impl Timespec {
pub const fn new(secs: i64, nsecs: i64) -> Self {
Self {
tv_sec: secs,
tv_nsec: nsecs,
}
}
pub const fn from_secs(secs: i64) -> Self {
Self {
tv_sec: secs,
tv_nsec: 0,
}
}
pub const fn from_millis(millis: i64) -> Self {
Self {
tv_sec: millis / 1000,
tv_nsec: (millis % 1000) * 1_000_000,
}
}
pub const fn as_secs(&self) -> i64 {
self.tv_sec
}
pub const fn as_millis(&self) -> i64 {
self.tv_sec * 1000 + self.tv_nsec / 1_000_000
}
pub fn to_ms(&self) -> u64 {
let secs = self.tv_sec.max(0) as u64;
let nsecs = self.tv_nsec.max(0) as u64;
secs * 1000 + nsecs / 1_000_000
}
pub fn from_ms(ms: u64) -> Self {
Self {
tv_sec: (ms / 1000) as i64,
tv_nsec: ((ms % 1000) * 1_000_000) as i64,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum AonClockSource {
Lposc,
Xosc,
Gpio(u8),
}
pub struct AonTimer {
powman: Powman,
}
impl AonTimer {
#[inline]
pub fn init(powman: POWMAN) -> Self {
Self {
powman: Powman::init(powman),
}
}
#[inline]
pub unsafe fn steal() -> Steal<Self> {
Steal(Self::init(unsafe { POWMAN::steal() }))
}
pub fn init_with_lposc(powman: POWMAN) -> Self {
let timer = Self::init(powman);
timer.powman.timer_set_1khz_tick_source_lposc();
timer
}
pub fn init_with_lposc_hz(powman: POWMAN, lposc_freq_hz: u32) -> Self {
let timer = Self::init(powman);
timer
.powman
.timer_set_1khz_tick_source_lposc_with_hz(lposc_freq_hz);
timer
}
pub fn init_with_xosc(powman: POWMAN, xosc_freq_hz: u32) -> Self {
let timer = Self::init(powman);
timer
.powman
.timer_set_1khz_tick_source_xosc_with_hz(xosc_freq_hz);
timer
}
pub fn init_with_gpio(powman: POWMAN, gpio: u8) -> Self {
let timer = Self::init(powman);
timer.powman.timer_set_1khz_tick_source_gpio(gpio);
timer
}
#[inline]
pub fn release(self) -> POWMAN {
self.powman.release()
}
#[inline]
pub fn powman(&self) -> &Powman {
&self.powman
}
#[inline]
pub fn start(&self) {
self.powman.timer_start();
}
#[inline]
pub fn stop(&self) {
self.powman.timer_stop();
}
#[inline]
pub fn is_running(&self) -> bool {
self.powman.timer_is_running()
}
pub fn set_time(&self, ts: &Timespec) {
self.powman.timer_set_ms(ts.to_ms());
}
pub fn time(&self) -> Timespec {
Timespec::from_ms(self.powman.timer_get_ms())
}
#[inline]
pub fn time_ms(&self) -> u64 {
self.powman.timer_get_ms()
}
#[inline]
pub fn set_time_ms(&self, ms: u64) {
self.powman.timer_set_ms(ms);
}
pub fn enable_alarm(&self, ts: &Timespec, wakeup_from_low_power: bool) {
if wakeup_from_low_power {
self.powman.enable_alarm_wakeup_at_ms(ts.to_ms());
} else {
self.powman.timer_enable_alarm_at_ms(ts.to_ms());
}
}
pub fn enable_alarm_at_ms(&self, alarm_time_ms: u64, wakeup_from_low_power: bool) {
if wakeup_from_low_power {
self.powman.enable_alarm_wakeup_at_ms(alarm_time_ms);
} else {
self.powman.timer_enable_alarm_at_ms(alarm_time_ms);
}
}
pub fn disable_alarm(&self) {
self.powman.timer_disable_alarm();
}
#[inline]
pub fn alarm_fired(&self) -> bool {
self.powman.timer_alarm_fired()
}
#[inline]
pub fn clear_alarm(&self) {
self.powman.timer_clear_alarm();
}
pub const fn irq_num() -> crate::pac::Interrupt {
crate::pac::Interrupt::POWMAN_IRQ_TIMER
}
#[inline]
pub fn irq_pending(&self) -> bool {
self.powman.timer_irq_raw()
}
pub fn wait_until(&self, ts: &Timespec) {
let target_ms = ts.to_ms();
while self.time_ms() < target_ms {
core::hint::spin_loop();
}
}
pub fn delay_ms(&self, ms: u64) {
let target = self.time_ms() + ms;
while self.time_ms() < target {
core::hint::spin_loop();
}
}
}
pub mod calendar {
use super::Timespec;
#[derive(Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct DateTime {
pub year: u16,
pub month: u8,
pub day: u8,
pub hour: u8,
pub minute: u8,
pub second: u8,
pub day_of_week: u8,
}
impl DateTime {
pub const fn new(year: u16, month: u8, day: u8, hour: u8, minute: u8, second: u8) -> Self {
Self {
year,
month,
day,
hour,
minute,
second,
day_of_week: 0,
}
}
pub fn to_unix_secs(&self) -> i64 {
let mut days = 0i64;
for y in 1970..self.year {
days += if is_leap_year(y) { 366 } else { 365 };
}
let days_in_months = if is_leap_year(self.year) {
[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
} else {
[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
};
for &days_in_month in days_in_months
.iter()
.take(self.month.saturating_sub(1) as usize)
{
days += days_in_month as i64;
}
days += (self.day.saturating_sub(1)) as i64;
days * 86400
+ (self.hour as i64) * 3600
+ (self.minute as i64) * 60
+ self.second as i64
}
pub fn from_unix_secs(secs: i64) -> Self {
if secs < 0 {
return Self::default();
}
let mut remaining = secs as u64;
let second = (remaining % 60) as u8;
remaining /= 60;
let minute = (remaining % 60) as u8;
remaining /= 60;
let hour = (remaining % 24) as u8;
remaining /= 24;
let day_of_week = ((remaining + 4) % 7) as u8;
let mut year = 1970u16;
loop {
let days_in_year = if is_leap_year(year) { 366 } else { 365 };
if remaining < days_in_year {
break;
}
remaining -= days_in_year;
year += 1;
}
let days_in_months = if is_leap_year(year) {
[31u64, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
} else {
[31u64, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
};
let mut month = 1u8;
for days_in_month in days_in_months.iter() {
if remaining < *days_in_month {
break;
}
remaining -= *days_in_month;
month += 1;
}
let day = remaining as u8 + 1;
Self {
year,
month,
day,
hour,
minute,
second,
day_of_week,
}
}
pub fn to_timespec(&self) -> Timespec {
Timespec::from_secs(self.to_unix_secs())
}
pub fn from_timespec(ts: &Timespec) -> Self {
Self::from_unix_secs(ts.tv_sec)
}
}
pub const fn is_leap_year(year: u16) -> bool {
(year.is_multiple_of(4) && !year.is_multiple_of(100)) || year.is_multiple_of(400)
}
}