Skip to content

Commit 25b8490

Browse files
kyp44Dan Whitman
authored andcommitted
feat(nvm): Updates the NVM to use the clock::v2 API.
* `nvm::Nvm::new` now requires the `AhbClk<NvmCtrl>` and `ApbClk<NvmCtrl>`. * Adds the `nvm::Nvm::free` method that returns the PAC controller and bus clocks. * `nvm::Nvm::smart_eeprom` now requires the `AhbClk<NvmCtrlSmeeProm>`. * Adds the `nvm::smart_eeprom::SmartEeprom::free` method that returns the bus clock. * Breaks some Tier 1 BSP examples, which cannot be repaired at the moment due to requiring other peripheral migrations to the `clock::v2` API.
1 parent 2d890f0 commit 25b8490

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

hal/src/peripherals/nvm/mod.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222

2323
pub mod smart_eeprom;
2424

25+
use crate::clock::v2::{
26+
ahb::AhbClk,
27+
apb::ApbClk,
28+
types::{NvmCtrl, NvmCtrlSmeeProm},
29+
};
2530
use crate::pac::Nvmctrl;
2631
pub use crate::pac::nvmctrl::ctrla::Prmselect;
2732
use crate::pac::nvmctrl::ctrlb::Cmdselect;
@@ -75,6 +80,10 @@ pub const QUADWORDSIZE: u32 = 16;
7580
pub struct Nvm {
7681
/// PAC peripheral
7782
nvm: Nvmctrl,
83+
/// AHB bus clock
84+
ahb_clk: AhbClk<NvmCtrl>,
85+
/// APB bus clock
86+
apb_clk: ApbClk<NvmCtrl>,
7887
}
7988

8089
/// Errors generated by the NVM peripheral
@@ -172,8 +181,18 @@ impl Nvm {
172181

173182
/// Create a new NVM controller or handle failure from DSU
174183
#[inline]
175-
pub fn new(nvm: Nvmctrl) -> Self {
176-
Self { nvm }
184+
pub fn new(nvm: Nvmctrl, ahb_clk: AhbClk<NvmCtrl>, apb_clk: ApbClk<NvmCtrl>) -> Self {
185+
Self {
186+
nvm,
187+
ahb_clk,
188+
apb_clk,
189+
}
190+
}
191+
192+
/// Destroy the NVM controller and returns the underlying resources
193+
#[inline]
194+
pub fn free(self) -> (Nvmctrl, AhbClk<NvmCtrl>, ApbClk<NvmCtrl>) {
195+
(self.nvm, self.ahb_clk, self.apb_clk)
177196
}
178197

179198
/// Raw access to the registers.
@@ -727,8 +746,8 @@ impl Nvm {
727746

728747
/// Retrieve SmartEEPROM
729748
#[inline]
730-
pub fn smart_eeprom(&mut self) -> smart_eeprom::Result<'_> {
731-
smart_eeprom::SmartEepromMode::retrieve(self)
749+
pub fn smart_eeprom(&mut self, ahb_clk: AhbClk<NvmCtrlSmeeProm>) -> smart_eeprom::Result<'_> {
750+
smart_eeprom::SmartEepromMode::retrieve(self, ahb_clk)
732751
}
733752
}
734753

hal/src/peripherals/nvm/smart_eeprom.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use core::marker::PhantomData;
3333

3434
use super::Nvm;
35+
use crate::clock::v2::{ahb::AhbClk, types::NvmCtrlSmeeProm};
3536
use crate::pac::{Nvmctrl, nvmctrl::ctrlb::Cmdselect};
3637
use crate::typelevel::Sealed;
3738

@@ -42,6 +43,7 @@ use crate::typelevel::Sealed;
4243
/// - current state ([`Locked`]/[`Unlocked`])
4344
pub struct SmartEeprom<'a, T: SmartEepromState> {
4445
nvm: &'a mut Nvm,
46+
ahb_clk: AhbClk<NvmCtrlSmeeProm>,
4547
virtual_size: usize,
4648
__: PhantomData<T>,
4749
}
@@ -103,7 +105,7 @@ fn wait_if_busy() {
103105
impl<'a> SmartEepromMode<'a> {
104106
/// Retrieve [`SmartEeprom`] instance using information found in relevant HW
105107
/// registers.
106-
pub(super) fn retrieve(nvm: &'a mut Nvm) -> Result<'a> {
108+
pub(super) fn retrieve(nvm: &'a mut Nvm, ahb_clk: AhbClk<NvmCtrlSmeeProm>) -> Result<'a> {
107109
use SmartEepromMode as Mode;
108110
use SmartEepromRetrievalFailure::*;
109111
if nvm.nvm.seecfg().read().aprdis().bit_is_set() {
@@ -125,12 +127,14 @@ impl<'a> SmartEepromMode<'a> {
125127
if nvm.nvm.seestat().read().lock().bit_is_set() {
126128
Ok(Mode::Locked(SmartEeprom {
127129
nvm,
130+
ahb_clk,
128131
virtual_size,
129132
__: PhantomData::<Locked>,
130133
}))
131134
} else {
132135
Ok(Mode::Unlocked(SmartEeprom {
133136
nvm,
137+
ahb_clk,
134138
virtual_size,
135139
__: PhantomData::<Unlocked>,
136140
}))
@@ -209,6 +213,12 @@ impl SmartEepromPointableSize for u8 {}
209213
impl SmartEepromPointableSize for u16 {}
210214
impl SmartEepromPointableSize for u32 {}
211215

216+
impl<'a, T: SmartEepromState> SmartEeprom<'a, T> {
217+
/// Destroy the SmartEEPROM instance and return the bus clock
218+
pub fn free(self) -> AhbClk<NvmCtrlSmeeProm> {
219+
self.ahb_clk
220+
}
221+
}
212222
impl<'a> SmartEeprom<'a, Unlocked> {
213223
/// Returns a mutable slice to SmartEEPROM mapped address space.
214224
///
@@ -257,10 +267,14 @@ impl<'a> SmartEeprom<'a, Unlocked> {
257267
.command_sync(Cmdselect::Lsee)
258268
.expect("SmartEEPROM locking failed");
259269
let Self {
260-
nvm, virtual_size, ..
270+
nvm,
271+
ahb_clk,
272+
virtual_size,
273+
..
261274
} = self;
262275
SmartEeprom {
263276
nvm,
277+
ahb_clk,
264278
virtual_size,
265279
__: PhantomData,
266280
}
@@ -275,10 +289,14 @@ impl<'a> SmartEeprom<'a, Locked> {
275289
.command_sync(Cmdselect::Usee)
276290
.expect("SmartEEPROM unlocking failed");
277291
let Self {
278-
nvm, virtual_size, ..
292+
nvm,
293+
ahb_clk,
294+
virtual_size,
295+
..
279296
} = self;
280297
SmartEeprom {
281298
nvm,
299+
ahb_clk,
282300
virtual_size,
283301
__: PhantomData,
284302
}

0 commit comments

Comments
 (0)