use crate::{analog::adc::Attenuation, peripherals::EFUSE};
#[cfg_attr(not(feature = "unstable"), allow(dead_code))]
mod fields;
#[instability::unstable]
pub use fields::*;
#[instability::unstable]
pub enum AdcCalibUnit {
ADC1,
}
#[instability::unstable]
pub fn flash_encryption() -> bool {
!super::read_field_le::<u8>(SPI_BOOT_CRYPT_CNT)
.count_ones()
.is_multiple_of(2)
}
#[instability::unstable]
pub fn rwdt_multiplier() -> u8 {
super::read_field_le::<u8>(WDT_DELAY_SEL)
}
#[instability::unstable]
pub fn block_version() -> (u8, u8) {
(
super::read_field_le::<u8>(BLK_VERSION_MAJOR),
super::read_field_le::<u8>(BLK_VERSION_MINOR),
)
}
#[instability::unstable]
pub fn rtc_calib_version() -> u8 {
let (major, _minor) = block_version();
if major == 0 { 1 } else { 0 }
}
#[instability::unstable]
pub fn rtc_calib_init_code(_unit: AdcCalibUnit, atten: Attenuation) -> Option<u16> {
let version = rtc_calib_version();
if version != 1 {
return None;
}
let diff_code0: u16 = super::read_field_le(ADC1_INIT_CODE_ATTEN0);
let code0 = if diff_code0 & (1 << 7) != 0 {
2160 - (diff_code0 & 0x7f)
} else {
2160 + diff_code0
};
if matches!(atten, Attenuation::_0dB) {
return Some(code0);
}
let diff_code11: u16 = super::read_field_le(ADC1_INIT_CODE_ATTEN3);
let code11 = code0 + diff_code11;
Some(code11)
}
#[instability::unstable]
pub fn rtc_calib_cal_mv(_unit: AdcCalibUnit, atten: Attenuation) -> u16 {
match atten {
Attenuation::_0dB => 400,
Attenuation::_11dB => 1370,
_ => panic!("Unsupported attenuation"),
}
}
#[instability::unstable]
pub fn rtc_calib_cal_code(_unit: AdcCalibUnit, atten: Attenuation) -> Option<u16> {
let version = rtc_calib_version();
if version != 1 {
return None;
}
let diff_code0: u16 = super::read_field_le(ADC1_CAL_VOL_ATTEN0);
let code0 = if diff_code0 & (1 << 7) != 0 {
1540 - (diff_code0 & 0x7f)
} else {
1540 + diff_code0
};
if matches!(atten, Attenuation::_0dB) {
return Some(code0);
}
let diff_code11: u16 = super::read_field_le(ADC1_CAL_VOL_ATTEN3);
let code11 = if diff_code0 & (1 << 5) != 0 {
code0 - (diff_code11 & 0x1f)
} else {
code0 + diff_code11
} - 123;
Some(code11)
}
#[instability::unstable]
pub fn major_chip_version() -> u8 {
super::read_field_le(WAFER_VERSION_MAJOR)
}
#[instability::unstable]
pub fn minor_chip_version() -> u8 {
super::read_field_le(WAFER_VERSION_MINOR)
}
#[derive(Debug, Clone, Copy, strum::FromRepr)]
#[repr(u32)]
pub(crate) enum EfuseBlock {
Block0,
Block1,
Block2,
Block3,
}
impl EfuseBlock {
pub(crate) fn address(self) -> *const u32 {
let efuse = EFUSE::regs();
match self {
Self::Block0 => efuse.rd_wr_dis().as_ptr(),
Self::Block1 => efuse.rd_blk1_data0().as_ptr(),
Self::Block2 => efuse.rd_blk2_data0().as_ptr(),
Self::Block3 => efuse.rd_blk3_data0().as_ptr(),
}
}
}