Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HAL: Add PMC, EFC configuration APIs #16

Closed
wants to merge 45 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
4addd5d
manifest: increase internal feature flag granularity
tmplt Apr 22, 2022
c552e47
add pmc, efc modules from @jamesmunns's work
jamesmunns Apr 22, 2022
1c8fcd2
move feature checking to a build.rs instead
tmplt Apr 25, 2022
d7e0f57
efc: find FWS via TryFrom impls
tmplt Apr 25, 2022
7c5d8b6
hal: apply fmt
tmplt Apr 25, 2022
ecb51b5
features: separate device-selected master feature from MCU info
tmplt Apr 25, 2022
d77f43e
Add Voltage Level Checks To build.rs
martinmortsell Apr 26, 2022
83de611
Fix Return Types
martinmortsell Apr 26, 2022
3f1f25f
Add get_ Methods For All The Clocks (Except SLCK)
martinmortsell Apr 26, 2022
762e919
efc: set VDDIO level via ctor instead of feature
tmplt Apr 27, 2022
be19501
build: ensure a chip feature is enabled
tmplt Apr 27, 2022
e42837e
pmc: Add Methods For Configuring Plla And Mck
martinmortsell Apr 27, 2022
5d2dd11
Merge branch 'feat/pmc' of https://github.com/GrepitAB/atsams70-rust …
martinmortsell Apr 27, 2022
d8edfe1
pmc: Add Non-functional External Oscillator Support
martinmortsell Apr 27, 2022
daa8a2c
pmc: Fix Main Clock Selection
martinmortsell May 3, 2022
9faca53
pmc: Add PCK Configuration
martinmortsell May 5, 2022
14660e7
Update Documentation
martinmortsell May 6, 2022
4ce66d6
pmc: Change Clock Source From Enum To Trait
martinmortsell May 9, 2022
745c86a
Add get_slck() Method
martinmortsell May 13, 2022
3d8af21
pmc: apply formatting
tmplt May 24, 2022
41c83aa
boards/atsame70_xpro: init example skeleton
tmplt May 24, 2022
6a614a8
boards/atsame70_xpro: doc how to erase fauly firmware
tmplt May 24, 2022
465f7d7
pmc: improve get_mainck
tmplt May 24, 2022
c08d50f
hal/pmc: refer to PMC as pmc, instead of periph
tmplt May 24, 2022
672c168
hal/pmc: add main crystal osc in normal mode back
tmplt May 25, 2022
d213282
hal/pmc: get_pllack: deconstruct PllaConfig
tmplt May 25, 2022
fef7e49
hal/pmc: refactor get_hclk
tmplt May 25, 2022
0b5694b
hal/pmc: record clock freq for MAINCK, PLLACK, and HCLK
tmplt May 25, 2022
c47096b
hal/pmc: get_hclk: set EFC wait states before switching clocks
tmplt May 25, 2022
451e3c1
hal/pmc: correctly configure UPLLCK and record its freq
tmplt May 25, 2022
22c9964
hal/pmc: implement get_upllckdiv
tmplt May 30, 2022
e035fae
hal/pmc: use Hertz instead of Megahertz when logical
tmplt May 30, 2022
2d4389c
hal/pmc: record SLCK freq
tmplt May 30, 2022
5adcf9f
hal/pmc: remove unnecessary Results
tmplt May 30, 2022
dbe39b1
hal/pmc: handle unused_variables/dead_code warnings
tmplt May 30, 2022
b88ffd6
atsame70_xpro: apply example clock hierarchy configuration
tmplt May 30, 2022
27f5bc0
hal/pmc: improve struct/enum docs
tmplt May 30, 2022
0e6a9c5
hal/pmc: remove unused enum
tmplt May 30, 2022
dd1a9dc
hal/pmc: get_slck: remove impl details from docs
tmplt May 30, 2022
009d478
hal/pmc: improve top-level docs
tmplt May 30, 2022
f74507d
atsame70_xpro: expose UPLLCKDIV on PCK2, @ 2.4MHz
tmplt May 30, 2022
82a87de
hal/pmc: globalize magic values
tmplt May 31, 2022
828299f
hal/pmc: apply formatting
tmplt May 31, 2022
b89177d
hal/pmc: refactor SCLK configuration to match MAINCK
tmplt May 31, 2022
d249979
hal/pmc: remove outdated comment
tmplt May 31, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 84 additions & 66 deletions hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,136 +93,154 @@ atsamv71q21 = { version = "0.21.0", path = "../pac/atsamv71q21", optional = tru
atsamv71q21b = { version = "0.21.0", path = "../pac/atsamv71q21b", optional = true }

[features]
# Internal-only feature flags; do not set directly.
# Refer to §2 in the data sheet.
v71 = []
v70 = []
e70 = []
s70 = []
pins-64 = [] # J variants
pins-100 = [] # N variants
pins-144 = [] # Q variants
flash-2M = [] # "21"-suffix
flash-1M = [] # "20"-suffix
flash-512K = [] # "19"-suffix
rev-a = [] # No suffix after flash memory density number
rev-b = ["device-selected"] # "b"-suffix
device-selected = []
same70j19 = ["atsame70j19", "device-selected"]

same70j19 = ["atsame70j19", "e70", "pins-64", "flash-512K", "rev-a"]
same70j19-rt = ["same70j19", "atsame70j19/rt"]
same70j19b = ["atsame70j19b", "device-selected"]
same70j19b = ["atsame70j19b", "e70", "pins-64", "flash-512K", "rev-b"]
same70j19b-rt = ["same70j19b", "atsame70j19b/rt"]
same70j20 = ["atsame70j20", "device-selected"]
same70j20 = ["atsame70j20", "e70", "pins-64", "flash-1M", "rev-a"]
same70j20-rt = ["same70j20", "atsame70j20/rt"]
same70j20b = ["atsame70j20b", "device-selected"]
same70j20b = ["atsame70j20b", "e70", "pins-64", "flash-1M", "rev-b"]
same70j20b-rt = ["same70j20b", "atsame70j20b/rt"]
same70j21 = ["atsame70j21", "device-selected"]
same70j21 = ["atsame70j21", "e70", "pins-64", "flash-2M", "rev-a"]
same70j21-rt = ["same70j21", "atsame70j21/rt"]
same70j21b = ["atsame70j21b", "device-selected"]
same70j21b = ["atsame70j21b", "e70", "pins-64", "flash-2M", "rev-b"]
same70j21b-rt = ["same70j21b", "atsame70j21b/rt"]
same70n19 = ["atsame70n19", "device-selected"]
same70n19 = ["atsame70n19", "e70", "pins-100", "flash-512K", "rev-a"]
same70n19-rt = ["same70n19", "atsame70n19/rt"]
same70n19b = ["atsame70n19b", "device-selected"]
same70n19b = ["atsame70n19b", "e70", "pins-100", "flash-512K", "rev-b"]
same70n19b-rt = ["same70n19b", "atsame70n19b/rt"]
same70n20 = ["atsame70n20", "device-selected"]
same70n20 = ["atsame70n20", "e70", "pins-100", "flash-1M", "rev-a"]
same70n20-rt = ["same70n20", "atsame70n20/rt"]
same70n20b = ["atsame70n20b", "device-selected"]
same70n20b = ["atsame70n20b", "e70", "pins-100", "flash-1M", "rev-b"]
same70n20b-rt = ["same70n20b", "atsame70n20b/rt"]
same70n21 = ["atsame70n21", "device-selected"]
same70n21 = ["atsame70n21", "e70", "pins-100", "flash-2M", "rev-a"]
same70n21-rt = ["same70n21", "atsame70n21/rt"]
same70n21b = ["atsame70n21b", "device-selected"]
same70n21b = ["atsame70n21b", "e70", "pins-100", "flash-2M", "rev-b"]
same70n21b-rt = ["same70n21b", "atsame70n21b/rt"]
same70q19 = ["atsame70q19", "device-selected"]
same70q19 = ["atsame70q19", "e70", "pins-144", "flash-512K", "rev-a"]
same70q19-rt = ["same70q19", "atsame70q19/rt"]
same70q19b = ["atsame70q19b", "device-selected"]
same70q19b = ["atsame70q19b", "e70", "pins-144", "flash-512K", "rev-b"]
same70q19b-rt = ["same70q19b", "atsame70q19b/rt"]
same70q20 = ["atsame70q20", "device-selected"]
same70q20 = ["atsame70q20", "e70", "pins-144", "flash-1M", "rev-a"]
same70q20-rt = ["same70q20", "atsame70q20/rt"]
same70q20b = ["atsame70q20b", "device-selected"]
same70q20b = ["atsame70q20b", "e70", "pins-144", "flash-1M", "rev-b"]
same70q20b-rt = ["same70q20b", "atsame70q20b/rt"]
same70q21 = ["atsame70q21", "device-selected"]
same70q21 = ["atsame70q21", "e70", "pins-144", "flash-2M", "rev-a"]
same70q21-rt = ["same70q21", "atsame70q21/rt"]
same70q21b = ["atsame70q21b", "device-selected"]
same70q21b = ["atsame70q21b", "e70", "pins-144", "flash-2M", "rev-b"]
same70q21b-rt = ["same70q21b", "atsame70q21b/rt"]
sams70j19 = ["atsams70j19", "device-selected"]

sams70j19 = ["atsams70j19", "s70", "pins-64", "flash-512K", "rev-a"]
sams70j19-rt = ["sams70j19", "atsams70j19/rt"]
sams70j19b = ["atsams70j19b", "device-selected"]
sams70j19b = ["atsams70j19b", "s70", "pins-64", "flash-512K", "rev-b"]
sams70j19b-rt = ["sams70j19b", "atsams70j19b/rt"]
sams70j20 = ["atsams70j20", "device-selected"]
sams70j20 = ["atsams70j20", "s70", "pins-64", "flash-1M", "rev-a"]
sams70j20-rt = ["sams70j20", "atsams70j20/rt"]
sams70j20b = ["atsams70j20b", "device-selected"]
sams70j20b = ["atsams70j20b", "s70", "pins-64", "flash-1M", "rev-b"]
sams70j20b-rt = ["sams70j20b", "atsams70j20b/rt"]
sams70j21 = ["atsams70j21", "device-selected"]
sams70j21 = ["atsams70j21", "s70", "pins-64", "flash-2M", "rev-a"]
sams70j21-rt = ["sams70j21", "atsams70j21/rt"]
sams70j21b = ["atsams70j21b", "device-selected"]
sams70j21b = ["atsams70j21b", "s70", "pins-64", "flash-2M", "rev-b"]
sams70j21b-rt = ["sams70j21b", "atsams70j21b/rt"]
sams70n19 = ["atsams70n19", "device-selected"]
sams70n19 = ["atsams70n19", "s70", "pins-100", "flash-512K", "rev-a"]
sams70n19-rt = ["sams70n19", "atsams70n19/rt"]
sams70n19b = ["atsams70n19b", "device-selected"]
sams70n19b = ["atsams70n19b", "s70", "pins-100", "flash-512K", "rev-a"]
sams70n19b-rt = ["sams70n19b", "atsams70n19b/rt"]
sams70n20 = ["atsams70n20", "device-selected"]
sams70n20 = ["atsams70n20", "s70", "pins-100", "flash-1M", "rev-a"]
sams70n20-rt = ["sams70n20", "atsams70n20/rt"]
sams70n20b = ["atsams70n20b", "device-selected"]
sams70n20b = ["atsams70n20b", "s70", "pins-100", "flash-1M", "rev-b"]
sams70n20b-rt = ["sams70n20b", "atsams70n20b/rt"]
sams70n21 = ["atsams70n21", "device-selected"]
sams70n21 = ["atsams70n21", "s70", "pins-100", "flash-2M", "rev-a"]
sams70n21-rt = ["sams70n21", "atsams70n21/rt"]
sams70n21b = ["atsams70n21b", "device-selected"]
sams70n21b = ["atsams70n21b", "s70", "pins-100", "flash-2M", "rev-b"]
sams70n21b-rt = ["sams70n21b", "atsams70n21b/rt"]
sams70q19 = ["atsams70q19", "device-selected"]
sams70q19 = ["atsams70q19", "s70", "pins-144", "flash-512K", "rev-a"]
sams70q19-rt = ["sams70q19", "atsams70q19/rt"]
sams70q19b = ["atsams70q19b", "device-selected"]
sams70q19b = ["atsams70q19b", "s70", "pins-144", "flash-512K", "rev-b"]
sams70q19b-rt = ["sams70q19b", "atsams70q19b/rt"]
sams70q20 = ["atsams70q20", "device-selected"]
sams70q20 = ["atsams70q20", "s70", "pins-144", "flash-1M", "rev-a"]
sams70q20-rt = ["sams70q20", "atsams70q20/rt"]
sams70q20b = ["atsams70q20b", "device-selected"]
sams70q20b = ["atsams70q20b", "s70", "pins-144", "flash-1M", "rev-b"]
sams70q20b-rt = ["sams70q20b", "atsams70q20b/rt"]
sams70q21 = ["atsams70q21", "device-selected"]
sams70q21 = ["atsams70q21", "s70", "pins-144", "flash-2M", "rev-a"]
sams70q21-rt = ["sams70q21", "atsams70q21/rt"]
sams70q21b = ["atsams70q21b", "device-selected"]
sams70q21b = ["atsams70q21b", "s70", "pins-144", "flash-2M", "rev-b"]
sams70q21b-rt = ["sams70q21b", "atsams70q21b/rt"]
samv70j19 = ["atsamv70j19", "device-selected"]

samv70j19 = ["atsamv70j19", "v70", "pins-64", "flash-512K", "rev-a"]
samv70j19-rt = ["samv70j19", "atsamv70j19/rt"]
samv70j19b = ["atsamv70j19b", "device-selected"]
samv70j19b = ["atsamv70j19b", "v70", "pins-64", "flash-512K", "rev-b"]
samv70j19b-rt = ["samv70j19b", "atsamv70j19b/rt"]
samv70j20 = ["atsamv70j20", "device-selected"]
samv70j20 = ["atsamv70j20", "v70", "pins-64", "flash-1M", "rev-a"]
samv70j20-rt = ["samv70j20", "atsamv70j20/rt"]
samv70j20b = ["atsamv70j20b", "device-selected"]
samv70j20b = ["atsamv70j20b", "v70", "pins-64", "flash-1M", "rev-b"]
samv70j20b-rt = ["samv70j20b", "atsamv70j20b/rt"]
samv70n19 = ["atsamv70n19", "device-selected"]
samv70n19 = ["atsamv70n19", "v70", "pins-100", "flash-512K", "rev-a"]
samv70n19-rt = ["samv70n19", "atsamv70n19/rt"]
samv70n19b = ["atsamv70n19b", "device-selected"]
samv70n19b = ["atsamv70n19b", "v70", "pins-100", "flash-512K", "rev-b"]
samv70n19b-rt = ["samv70n19b", "atsamv70n19b/rt"]
samv70n20 = ["atsamv70n20", "device-selected"]
samv70n20 = ["atsamv70n20", "v70", "pins-100", "flash-1M", "rev-a"]
samv70n20-rt = ["samv70n20", "atsamv70n20/rt"]
samv70n20b = ["atsamv70n20b", "device-selected"]
samv70n20b = ["atsamv70n20b", "v70", "pins-100", "flash-1M", "rev-b"]
samv70n20b-rt = ["samv70n20b", "atsamv70n20b/rt"]
samv70q19 = ["atsamv70q19", "device-selected"]
samv70q19 = ["atsamv70q19", "v70", "pins-144", "flash-512K", "rev-a"]
samv70q19-rt = ["samv70q19", "atsamv70q19/rt"]
samv70q19b = ["atsamv70q19b", "device-selected"]
samv70q19b = ["atsamv70q19b", "v70", "pins-144", "flash-512K", "rev-b"]
samv70q19b-rt = ["samv70q19b", "atsamv70q19b/rt"]
samv70q20 = ["atsamv70q20", "device-selected"]
samv70q20 = ["atsamv70q20", "v70", "pins-144", "flash-1M", "rev-a"]
samv70q20-rt = ["samv70q20", "atsamv70q20/rt"]
samv70q20b = ["atsamv70q20b", "device-selected"]
samv70q20b = ["atsamv70q20b", "v70", "pins-144", "flash-1M", "rev-b"]
samv70q20b-rt = ["samv70q20b", "atsamv70q20b/rt"]
samv71j19 = ["atsamv71j19", "device-selected"]

samv71j19 = ["atsamv71j19", "v71", "pins-64", "flash-512K", "rev-a"]
samv71j19-rt = ["samv71j19", "atsamv71j19/rt"]
samv71j19b = ["atsamv71j19b", "device-selected"]
samv71j19b = ["atsamv71j19b", "v71", "pins-64", "flash-512K", "rev-b"]
samv71j19b-rt = ["samv71j19b", "atsamv71j19b/rt"]
samv71j20 = ["atsamv71j20", "device-selected"]
samv71j20 = ["atsamv71j20", "v71", "pins-64", "flash-1M", "rev-a"]
samv71j20-rt = ["samv71j20", "atsamv71j20/rt"]
samv71j20b = ["atsamv71j20b", "device-selected"]
samv71j20b = ["atsamv71j20b", "v71", "pins-64", "flash-1M", "rev-b"]
samv71j20b-rt = ["samv71j20b", "atsamv71j20b/rt"]
samv71j21 = ["atsamv71j21", "device-selected"]
samv71j21 = ["atsamv71j21", "v71", "pins-64", "flash-2M", "rev-a"]
samv71j21-rt = ["samv71j21", "atsamv71j21/rt"]
samv71j21b = ["atsamv71j21b", "device-selected"]
samv71j21b = ["atsamv71j21b", "v71", "pins-64", "flash-2M", "rev-b"]
samv71j21b-rt = ["samv71j21b", "atsamv71j21b/rt"]
samv71n19 = ["atsamv71n19", "device-selected"]
samv71n19 = ["atsamv71n19", "v71", "pins-100", "flash-512K", "rev-a"]
samv71n19-rt = ["samv71n19", "atsamv71n19/rt"]
samv71n19b = ["atsamv71n19b", "device-selected"]
samv71n19b = ["atsamv71n19b", "v71", "pins-100", "flash-512K", "rev-b"]
samv71n19b-rt = ["samv71n19b", "atsamv71n19b/rt"]
samv71n20 = ["atsamv71n20", "device-selected"]
samv71n20 = ["atsamv71n20", "v71", "pins-100", "flash-1M", "rev-a"]
samv71n20-rt = ["samv71n20", "atsamv71n20/rt"]
samv71n20b = ["atsamv71n20b", "device-selected"]
samv71n20b = ["atsamv71n20b", "v71", "pins-100", "flash-1M", "rev-b"]
samv71n20b-rt = ["samv71n20b", "atsamv71n20b/rt"]
samv71n21 = ["atsamv71n21", "device-selected"]
samv71n21 = ["atsamv71n21", "v71", "pins-100", "flash-2M", "rev-a"]
samv71n21-rt = ["samv71n21", "atsamv71n21/rt"]
samv71n21b = ["atsamv71n21b", "device-selected"]
samv71n21b = ["atsamv71n21b", "v71", "pins-100", "flash-2M", "rev-b"]
samv71n21b-rt = ["samv71n21b", "atsamv71n21b/rt"]
samv71q19 = ["atsamv71q19", "device-selected"]
samv71q19 = ["atsamv71q19", "v71", "pins-144", "flash-512K", "rev-a"]
samv71q19-rt = ["samv71q19", "atsamv71q19/rt"]
samv71q19b = ["atsamv71q19b", "device-selected"]
samv71q19b = ["atsamv71q19b", "v71", "pins-144", "flash-512K", "rev-b"]
samv71q19b-rt = ["samv71q19b", "atsamv71q19b/rt"]
samv71q20 = ["atsamv71q20", "device-selected"]
samv71q20 = ["atsamv71q20", "v71", "pins-144", "flash-1M", "rev-a"]
samv71q20-rt = ["samv71q20", "atsamv71q20/rt"]
samv71q20b = ["atsamv71q20b", "device-selected"]
samv71q20b = ["atsamv71q20b", "v71", "pins-144", "flash-1M", "rev-b"]
samv71q20b-rt = ["samv71q20b", "atsamv71q20b/rt"]
samv71q21 = ["atsamv71q21", "device-selected"]
samv71q21 = ["atsamv71q21", "v71", "pins-144", "flash-2M", "rev-a"]
samv71q21-rt = ["samv71q21", "atsamv71q21/rt"]
samv71q21b = ["atsamv71q21b", "device-selected"]
samv71q21b = ["atsamv71q21b", "v71", "pins-144", "flash-2M", "rev-b"]
samv71q21b-rt = ["samv71q21b", "atsamv71q21b/rt"]
68 changes: 68 additions & 0 deletions hal/src/efc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//! Flash block configuration

use crate::target_device::EFC;
use crate::pmc::PmcError;

pub struct Efc {
pub(crate) periph: EFC,
}

impl Efc {
pub fn new(periph: EFC) -> Self {
periph.eefc_wpmr.modify(|_r, w| {
w.wpkey().passwd();
w.wpen().clear_bit();
w
});

Self { periph }
}

pub fn set_wait_states(&mut self, fws: FlashWaitStates) {
let fws_bits = fws as u8;

self.periph
.eefc_fmr
.modify(|_r, w| unsafe { w.fws().bits(fws_bits) });
}
}

/// The number of flash wait states for a read operation.
///
/// Note: The number of cycles a read takes is 1 + FWS.
#[derive(Debug, PartialEq, Copy, Clone)]
#[repr(u8)]
pub enum FlashWaitStates {
Zero,
One,
Two,
Three,
Four,
Five,
Six,
}

impl FlashWaitStates {
/// Calculate the lowest possible number of flash wait states from a given
/// master clock frequency in MHz.
///
/// The max mck frequency supported is 150MHz. This is *not* the CPU frequency,
/// which may go up to 300MHz.
///
/// Note: This is probably only valid at VDDIO = 3.0V
pub fn from_mck_mhz(freq: u8) -> Result<Self, PmcError> {
// Reference: Table 58-51 Embedded Flash Wait States for Worst-Case Conditions
let fws = match freq {
0..=23 => Self::Zero,
24..=46 => Self::One,
47..=69 => Self::Two,
70..=92 => Self::Three,
93..=115 => Self::Four,
116..=138 => Self::Five,
139..=150 => Self::Six,
_ => return Err(PmcError::InvalidConfiguration),
};

Ok(fws)
}
}
8 changes: 6 additions & 2 deletions hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ pub use atsamv71q21 as target_device;
#[cfg(feature = "samv71q21b")]
pub use atsamv71q21b as target_device;

#[cfg(feature = "device-selected")]
#[cfg(feature = "rev-b")]
pub mod serial;
#[cfg(feature = "device-selected")]
#[cfg(feature = "rev-b")]
pub mod watchdog;
#[cfg(feature = "rev-b")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you haven't seen the cfg-if crate, it's pretty handy for avoiding wordy cfgs like this.

pub mod pmc;
#[cfg(feature = "rev-b")]
pub mod efc;
Loading