Skip to content

Commit d7e0f57

Browse files
committed
efc: find FWS via TryFrom impls
1 parent 1c8fcd2 commit d7e0f57

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

hal/Cargo.toml

+7-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,13 @@ atsamv71q21b = { version = "0.21.0", path = "../pac/atsamv71q21b", optional = tr
9595
[features]
9696
# Internal-only feature flags; do not set directly.
9797
# Refer to §2 in the data sheet.
98-
v71 = []
99-
v70 = []
98+
## Different electrical characteristics for different VDDIO values
99+
## Refer to Tables 58-3 and 58-9
100+
vddio-3v = [] # Minimum value of 3.3V
101+
vddio-1v = [] # Minimum value of 1.7V
102+
## Refer to §2.
103+
v71 = ["vddio-3v"]
104+
v70 = ["vddio-3v"]
100105
e70 = []
101106
s70 = []
102107
pins-64 = [] # J variants

hal/src/efc.rs

+39-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
//! Flash block configuration
1+
//! Flash controller configuration
22
3-
use crate::target_device::EFC;
43
use crate::pmc::PmcError;
4+
use crate::target_device::EFC;
55

66
pub struct Efc {
77
pub(crate) periph: EFC,
@@ -42,27 +42,50 @@ pub enum FlashWaitStates {
4242
Six,
4343
}
4444

45-
impl FlashWaitStates {
46-
/// Calculate the lowest possible number of flash wait states from a given
47-
/// master clock frequency in MHz.
48-
///
49-
/// The max mck frequency supported is 150MHz. This is *not* the CPU frequency,
50-
/// which may go up to 300MHz.
51-
///
52-
/// Note: This is probably only valid at VDDIO = 3.0V
53-
pub fn from_mck_mhz(freq: u8) -> Result<Self, PmcError> {
54-
// Reference: Table 58-51 Embedded Flash Wait States for Worst-Case Conditions
55-
let fws = match freq {
45+
impl TryFrom<u8> for FlashWaitStates {
46+
type Error = PmcError;
47+
48+
#[cfg(feature = "vddio-3v")]
49+
fn try_from(freq: u8) -> Result<Self, Error> {
50+
// References:
51+
// - Table 58-50 (p. 1804) Embedded Flash Wait States for Worst-Case Conditions (V70/V71)
52+
// - Table 59-50 (p. 1850) Embedded Flash Wait States for Worst-Case Conditions (E70/S70; VDDIO = 3.0V)
53+
match freq {
5654
0..=23 => Self::Zero,
5755
24..=46 => Self::One,
5856
47..=69 => Self::Two,
5957
70..=92 => Self::Three,
6058
93..=115 => Self::Four,
6159
116..=138 => Self::Five,
6260
139..=150 => Self::Six,
63-
_ => return Err(PmcError::InvalidConfiguration),
64-
};
61+
_ => Err(PmcError::InvalidConfiguration),
62+
}
63+
}
64+
65+
#[cfg(feature = "vddio-1v")]
66+
fn try_from(freq: u8) -> Result<Self, Error> {
67+
// References:
68+
// - Table 59-50 (p. 1850) Embedded Flash Wait States for Worst-Case Conditions (E70/S70; VDDIO = 1.7V)
69+
match freq {
70+
0..=21 => Self::Zero,
71+
22..=42 => Self::One,
72+
43..=63 => Self::Two,
73+
64..=84 => Self::Three,
74+
85..=106 => Self::Four,
75+
107..=125 => Self::Five,
76+
126..=137 => Self::Six,
77+
_ => Err(PmcError::InvalidConfiguration),
78+
}
79+
}
80+
}
6581

66-
Ok(fws)
82+
impl FlashWaitStates {
83+
/// Calculate the lowest possible number of flash wait states from a given
84+
/// master clock frequency in MHz.
85+
///
86+
/// The max mck frequency supported is 150MHz. This is *not* the CPU frequency,
87+
/// which may go up to 300MHz.
88+
pub fn from_mck_mhz(freq: u8) -> Result<Self, PmcError> {
89+
freq.try_into()?
6790
}
6891
}

0 commit comments

Comments
 (0)