Move parameters to a config struct

This commit is contained in:
Thales Fragoso 2021-05-14 23:34:16 -03:00
parent a5d473be0e
commit 1e5f25aa41

View File

@ -135,23 +135,28 @@ fn clk_div(ker_ck: Hertz, sdmmc_ck: u32) -> Result<(u16, Hertz), Error> {
} }
} }
#[non_exhaustive]
pub struct Config {
/// AHB clock
pub hclk: Hertz,
/// SDMMC kernel clock
pub kernel_clk: Hertz,
/// The timeout to be set for data transfers, in card bus clock periods
pub data_transfer_timeout: u32,
}
/// Sdmmc device /// Sdmmc device
pub struct Sdmmc<'d, T: Instance, P: Pins<T>> { pub struct Sdmmc<'d, T: Instance, P: Pins<T>> {
sdmmc: PhantomData<&'d mut T>, sdmmc: PhantomData<&'d mut T>,
pins: P, pins: P,
irq: T::Interrupt, irq: T::Interrupt,
/// SDMMC kernel clock config: Config,
ker_ck: Hertz,
/// AHB clock
hclk: Hertz,
/// Current clock to card /// Current clock to card
clock: Hertz, clock: Hertz,
/// Current signalling scheme to card /// Current signalling scheme to card
signalling: Signalling, signalling: Signalling,
/// Card /// Card
card: Option<Card>, card: Option<Card>,
/// The timeout to be set for data transfers, in card bus clock periods
data_transfer_timeout: u32,
} }
impl<'d, T: Instance, P: Pins<T>> Sdmmc<'d, T, P> { impl<'d, T: Instance, P: Pins<T>> Sdmmc<'d, T, P> {
@ -163,15 +168,13 @@ impl<'d, T: Instance, P: Pins<T>> Sdmmc<'d, T, P> {
_peripheral: impl Unborrow<Target = T> + 'd, _peripheral: impl Unborrow<Target = T> + 'd,
pins: impl Unborrow<Target = P> + 'd, pins: impl Unborrow<Target = P> + 'd,
irq: impl Unborrow<Target = T::Interrupt>, irq: impl Unborrow<Target = T::Interrupt>,
hclk: Hertz, config: Config,
kernel_clk: Hertz,
data_transfer_timeout: u32,
) -> Self { ) -> Self {
unborrow!(irq, pins); unborrow!(irq, pins);
pins.configure(); pins.configure();
let inner = T::inner(); let inner = T::inner();
let clock = inner.new_inner(kernel_clk); let clock = inner.new_inner(config.kernel_clk);
irq.set_handler(Self::on_interrupt); irq.set_handler(Self::on_interrupt);
irq.unpend(); irq.unpend();
@ -181,12 +184,10 @@ impl<'d, T: Instance, P: Pins<T>> Sdmmc<'d, T, P> {
sdmmc: PhantomData, sdmmc: PhantomData,
pins, pins,
irq, irq,
ker_ck: kernel_clk, config,
hclk,
clock, clock,
signalling: Default::default(), signalling: Default::default(),
card: None, card: None,
data_transfer_timeout,
} }
} }
@ -201,11 +202,11 @@ impl<'d, T: Instance, P: Pins<T>> Sdmmc<'d, T, P> {
P::BUSWIDTH, P::BUSWIDTH,
&mut self.card, &mut self.card,
&mut self.signalling, &mut self.signalling,
self.hclk, self.config.hclk,
self.ker_ck, self.config.kernel_clk,
&mut self.clock, &mut self.clock,
T::state(), T::state(),
self.data_transfer_timeout, self.config.data_transfer_timeout,
) )
.await .await
} }
@ -228,7 +229,7 @@ impl<'d, T: Instance, P: Pins<T>> Sdmmc<'d, T, P> {
buf, buf,
card_capacity, card_capacity,
state, state,
self.data_transfer_timeout, self.config.data_transfer_timeout,
) )
.await .await
} }
@ -241,7 +242,13 @@ impl<'d, T: Instance, P: Pins<T>> Sdmmc<'d, T, P> {
// NOTE(unsafe) DataBlock uses align 4 // NOTE(unsafe) DataBlock uses align 4
let buf = unsafe { &*((&buffer.0) as *const [u8; 512] as *const [u32; 128]) }; let buf = unsafe { &*((&buffer.0) as *const [u8; 512] as *const [u32; 128]) };
inner inner
.write_block(block_idx, buf, card, state, self.data_transfer_timeout) .write_block(
block_idx,
buf,
card,
state,
self.config.data_transfer_timeout,
)
.await .await
} }
@ -1507,7 +1514,7 @@ mod sdmmc_rs {
buf, buf,
card_capacity, card_capacity,
state, state,
self.data_transfer_timeout, self.config.data_transfer_timeout,
) )
.await?; .await?;
address += 1; address += 1;
@ -1533,7 +1540,7 @@ mod sdmmc_rs {
// NOTE(unsafe) DataBlock uses align 4 // NOTE(unsafe) DataBlock uses align 4
let buf = unsafe { &*(block as *const [u8; 512] as *const [u32; 128]) }; let buf = unsafe { &*(block as *const [u8; 512] as *const [u32; 128]) };
inner inner
.write_block(address, buf, card, state, self.data_transfer_timeout) .write_block(address, buf, card, state, self.config.data_transfer_timeout)
.await?; .await?;
address += 1; address += 1;
} }