Skip to content

Commit 4f97069

Browse files
committed
Updates and cleanups to match the changes in the PAC
1 parent 4b0f161 commit 4f97069

File tree

2 files changed

+56
-43
lines changed

2 files changed

+56
-43
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- Update the sdio driver to match the changes in the PAC
13+
1014
## [v0.9.0] - 2021-04-04
1115

1216
### Changed

src/sdio.rs

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,6 @@ pub enum Buswidth {
120120
Buswidth4 = 1,
121121
}
122122

123-
enum PowerCtrl {
124-
Off = 0b00,
125-
On = 0b11,
126-
}
127-
128123
/// Clock frequency of a SDIO bus.
129124
pub enum ClockFreq {
130125
F24Mhz = 0,
@@ -185,21 +180,21 @@ impl Sdio {
185180
}
186181

187182
// Configure clock
188-
sdio.clkcr.write(|w| unsafe {
183+
sdio.clkcr.write(|w| {
189184
w.widbus()
190-
.bits(Buswidth::Buswidth1 as u8)
185+
.bus_width1()
191186
.clken()
192-
.set_bit()
187+
.enabled()
193188
.clkdiv()
194189
.bits(ClockFreq::F400Khz as u8)
195190
.pwrsav()
196-
.clear_bit()
191+
.disabled()
197192
.bypass()
198-
.clear_bit()
193+
.disabled()
199194
.negedge()
200-
.clear_bit()
195+
.rising()
201196
.hwfc_en()
202-
.set_bit()
197+
.enabled()
203198
});
204199

205200
let mut host = Sdio {
@@ -210,17 +205,17 @@ impl Sdio {
210205
};
211206

212207
// Make sure card is powered off
213-
host.set_power(PowerCtrl::Off);
208+
host.power_card(false);
214209
host
215210
}
216211

217212
/// Initializes card (if present) and sets the bus at the specified frequency.
218213
pub fn init_card(&mut self, freq: ClockFreq) -> Result<(), Error> {
219214
// Enable power to card
220-
self.set_power(PowerCtrl::On);
215+
self.power_card(true);
221216

222217
// Enable clock
223-
self.sdio.clkcr.modify(|_, w| w.clken().set_bit());
218+
self.sdio.clkcr.modify(|_, w| w.clken().enabled());
224219
// Send card to idle state
225220
self.cmd(cmd::idle())?;
226221

@@ -307,10 +302,16 @@ impl Sdio {
307302
Ok(())
308303
}
309304

310-
fn set_power(&mut self, pwr: PowerCtrl) {
311-
self.sdio
312-
.power
313-
.modify(|_, w| unsafe { w.pwrctrl().bits(pwr as u8) });
305+
fn power_card(&mut self, on: bool) {
306+
use crate::stm32::sdio::power::PWRCTRL_A;
307+
308+
self.sdio.power.modify(|_, w| {
309+
w.pwrctrl().variant(if on {
310+
PWRCTRL_A::POWERON
311+
} else {
312+
PWRCTRL_A::POWEROFF
313+
})
314+
});
314315

315316
// Wait for 2 ms after changing power settings
316317
cortex_m::asm::delay(2 * (self.clocks.sysclk().0 / 1000));
@@ -407,7 +408,9 @@ impl Sdio {
407408
Ok(())
408409
}
409410

410-
fn start_datapath_transfer(&self, length_bytes: u32, block_size: u8, dtdir: bool) {
411+
fn start_datapath_transfer(&self, length_bytes: u32, block_size: u8, card_to_controller: bool) {
412+
use crate::stm32::sdio::dctrl::DTDIR_A;
413+
411414
// Block Size up to 2^14 bytes
412415
assert!(block_size <= 14);
413416

@@ -417,22 +420,24 @@ impl Sdio {
417420
|| self.sdio.sta.read().txact().bit_is_set()
418421
{}
419422

423+
let dtdir = if card_to_controller {
424+
DTDIR_A::CARDTOCONTROLLER
425+
} else {
426+
DTDIR_A::CONTROLLERTOCARD
427+
};
428+
420429
// Data timeout, in bus cycles
421-
self.sdio
422-
.dtimer
423-
.write(|w| unsafe { w.datatime().bits(0xFFFF_FFFF) });
430+
self.sdio.dtimer.write(|w| w.datatime().bits(0xFFFF_FFFF));
424431
// Data length, in bytes
425-
self.sdio
426-
.dlen
427-
.write(|w| unsafe { w.datalength().bits(length_bytes) });
432+
self.sdio.dlen.write(|w| w.datalength().bits(length_bytes));
428433
// Transfer
429-
self.sdio.dctrl.write(|w| unsafe {
434+
self.sdio.dctrl.write(|w| {
430435
w.dblocksize()
431436
.bits(block_size) // 2^n bytes block size
432437
.dtdir()
433-
.bit(dtdir)
438+
.variant(dtdir)
434439
.dten()
435-
.set_bit() // Enable transfer
440+
.enabled() // Enable transfer
436441
});
437442
}
438443

@@ -522,22 +527,24 @@ impl Sdio {
522527

523528
/// Set bus width and clock frequency
524529
fn set_bus(&self, width: Buswidth, freq: ClockFreq) -> Result<(), Error> {
530+
use crate::stm32::sdio::clkcr::WIDBUS_A;
531+
525532
let card_widebus = self.card()?.supports_widebus();
526533

527534
let width = match width {
528-
Buswidth::Buswidth4 if card_widebus => Buswidth::Buswidth4,
529-
_ => Buswidth::Buswidth1,
535+
Buswidth::Buswidth4 if card_widebus => WIDBUS_A::BUSWIDTH4,
536+
_ => WIDBUS_A::BUSWIDTH1,
530537
};
531538

532-
self.app_cmd(cmd::set_bus_width(width == Buswidth::Buswidth4))?;
539+
self.app_cmd(cmd::set_bus_width(width == WIDBUS_A::BUSWIDTH4))?;
533540

534-
self.sdio.clkcr.modify(|_, w| unsafe {
541+
self.sdio.clkcr.modify(|_, w| {
535542
w.clkdiv()
536543
.bits(freq as u8)
537544
.widbus()
538-
.bits(width as u8)
545+
.variant(width)
539546
.clken()
540-
.set_bit()
547+
.enabled()
541548
});
542549
Ok(())
543550
}
@@ -550,32 +557,34 @@ impl Sdio {
550557

551558
/// Send command to card
552559
fn cmd<R: cmd::Resp>(&self, cmd: Cmd<R>) -> Result<(), Error> {
560+
use crate::stm32::sdio::cmd::WAITRESP_A;
561+
553562
// Command state machines must be idle
554563
while self.sdio.sta.read().cmdact().bit_is_set() {}
555564

556565
// Clear the interrupts before we start
557566
clear_all_interrupts(&self.sdio.icr);
558567

559568
// Command arg
560-
self.sdio.arg.write(|w| unsafe { w.cmdarg().bits(cmd.arg) });
569+
self.sdio.arg.write(|w| w.cmdarg().bits(cmd.arg));
561570

562571
// Determine what kind of response the CPSM should wait for
563572
let waitresp = match cmd.response_len() {
564-
ResponseLen::Zero => 0b00,
565-
ResponseLen::R48 => 0b01,
566-
ResponseLen::R136 => 0b11,
573+
ResponseLen::Zero => WAITRESP_A::NORESPONSE,
574+
ResponseLen::R48 => WAITRESP_A::SHORTRESPONSE,
575+
ResponseLen::R136 => WAITRESP_A::LONGRESPONSE,
567576
};
568577

569578
// Send the command
570-
self.sdio.cmd.write(|w| unsafe {
579+
self.sdio.cmd.write(|w| {
571580
w.waitresp()
572-
.bits(waitresp)
581+
.variant(waitresp)
573582
.cmdindex()
574583
.bits(cmd.cmd)
575584
.waitint()
576-
.clear_bit()
585+
.disabled()
577586
.cpsmen()
578-
.set_bit()
587+
.enabled()
579588
});
580589

581590
let mut timeout: u32 = 0xFFFF_FFFF;

0 commit comments

Comments
 (0)