|
| 1 | +//! Flash block configuration |
| 2 | +
|
| 3 | +use crate::target_device::EFC; |
| 4 | +use crate::pmc::PmcError; |
| 5 | + |
| 6 | +pub struct Efc { |
| 7 | + pub(crate) periph: EFC, |
| 8 | +} |
| 9 | + |
| 10 | +impl Efc { |
| 11 | + pub fn new(periph: EFC) -> Self { |
| 12 | + periph.eefc_wpmr.modify(|_r, w| { |
| 13 | + w.wpkey().passwd(); |
| 14 | + w.wpen().clear_bit(); |
| 15 | + w |
| 16 | + }); |
| 17 | + |
| 18 | + Self { periph } |
| 19 | + } |
| 20 | + |
| 21 | + pub fn set_wait_states(&mut self, fws: FlashWaitStates) { |
| 22 | + let fws_bits = fws as u8; |
| 23 | + |
| 24 | + self.periph |
| 25 | + .eefc_fmr |
| 26 | + .modify(|_r, w| unsafe { w.fws().bits(fws_bits) }); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/// The number of flash wait states for a read operation. |
| 31 | +/// |
| 32 | +/// Note: The number of cycles a read takes is 1 + FWS. |
| 33 | +#[derive(Debug, PartialEq, Copy, Clone)] |
| 34 | +#[repr(u8)] |
| 35 | +pub enum FlashWaitStates { |
| 36 | + Zero, |
| 37 | + One, |
| 38 | + Two, |
| 39 | + Three, |
| 40 | + Four, |
| 41 | + Five, |
| 42 | + Six, |
| 43 | +} |
| 44 | + |
| 45 | +impl FlashWaitStates { |
| 46 | + /// Calculate the lowest possible number of flash wait states from a given |
| 47 | + /// peripheral clock frequency in MHz. |
| 48 | + /// |
| 49 | + /// The max PCLK 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_pclk_mhz(freq: u8) -> Result<Self, PmcError> { |
| 54 | + // Reference: Table 58-51 Embedded Flash Wait States for Worst-Case Conditions |
| 55 | + let fws = match freq { |
| 56 | + 0..=23 => Self::Zero, |
| 57 | + 24..=46 => Self::One, |
| 58 | + 47..=69 => Self::Two, |
| 59 | + 70..=92 => Self::Three, |
| 60 | + 93..=115 => Self::Four, |
| 61 | + 116..=138 => Self::Five, |
| 62 | + 139..=150 => Self::Six, |
| 63 | + _ => return Err(PmcError::InvalidConfiguration), |
| 64 | + }; |
| 65 | + |
| 66 | + Ok(fws) |
| 67 | + } |
| 68 | +} |
0 commit comments