Skip to content

Commit c9f9675

Browse files
authored
Merge pull request #1 from x37v/xnor/octospi
Xnor/octospi
2 parents 2da85df + aa20ceb commit c9f9675

File tree

1 file changed

+38
-56
lines changed

1 file changed

+38
-56
lines changed

src/xspi/mod.rs

Lines changed: 38 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -442,17 +442,23 @@ mod common {
442442
/// and the data phase.
443443
/// * `data` - true is there is a data phase, false for no data phase.
444444
fn setup_extended(&mut self, instruction: XspiWord, address: XspiWord,
445-
alternate_bytes: XspiWord, dummy_cycles: u8, data: bool) {
445+
alternate_bytes: XspiWord, dummy_cycles: u8, data: bool, read: bool) {
446446

447+
let fmode = if read { 0b01 } else { 0b00 };
447448
let mode = self.mode.reg_value();
448449
let imode = if instruction != XspiWord::None { mode } else { 0 };
449450
let admode = if address != XspiWord::None { mode } else { 0 };
450451
let abmode = if alternate_bytes != XspiWord::None { mode } else { 0 };
451452
let dmode = if data { mode } else { 0 };
452453

454+
//writing to ccr will trigger the start of a transaction if there is no address or
455+
//data rm0433 pg 894, so we do it all in one go
453456
self.rb.ccr.modify(|_, w| unsafe {
454457
#[cfg(any(feature = "rm0433", feature = "rm0399"))]
455-
let w = w.dcyc().bits(dummy_cycles);
458+
let w = {
459+
let ir = instruction.bits_u8().unwrap();
460+
w.dcyc().bits(dummy_cycles).instruction().bits(ir).fmode().bits(fmode)
461+
};
456462

457463
#[cfg(any(feature = "rm0455", feature = "rm0468"))]
458464
let w = w.isize().bits(instruction.size());
@@ -472,7 +478,29 @@ mod common {
472478
});
473479

474480
#[cfg(any(feature = "rm0455", feature = "rm0468"))]
475-
self.rb.tcr.write(|w| unsafe { w.dcyc().bits(dummy_cycles) });
481+
{
482+
self.rb.tcr.write(|w| unsafe { w.dcyc().bits(dummy_cycles) });
483+
self.rb.cr.modify(|_, w| unsafe { w.fmode().bits(fmode) });
484+
}
485+
486+
// Write alternate-bytes
487+
self.rb.abr.write(|w| unsafe {
488+
w.alternate().bits(alternate_bytes.bits())
489+
});
490+
491+
#[cfg(any(feature = "rm0455", feature = "rm0468"))]
492+
if instruction != XspiWord::None {
493+
self.rb.ir.write(|w| unsafe {
494+
w.instruction().bits(instruction.bits())
495+
});
496+
}
497+
498+
if address != XspiWord::None {
499+
// Write the address. The transaction starts on the next write
500+
// to DATA, unless there is no DATA phase configured, in which
501+
// case it starts here.
502+
self.rb.ar.write(|w| unsafe { w.address().bits(address.bits()) });
503+
}
476504
}
477505

478506
/// Begin a write over the XSPI interface. This is mostly useful for use with
@@ -578,9 +606,6 @@ mod common {
578606

579607
self.is_busy()?;
580608

581-
// Setup extended mode. Typically no dummy cycles in write mode
582-
self.setup_extended(instruction, address, alternate_bytes, 0, !data.is_empty());
583-
584609
// Clear the transfer complete flag.
585610
self.rb.fcr.write(|w| w.ctcf().set_bit());
586611

@@ -591,32 +616,11 @@ mod common {
591616
.write(|w| unsafe { w.dl().bits(data.len() as u32 - 1) });
592617
}
593618

594-
// Configure the mode to indirect write.
595-
fmode_reg!(self).modify(|_, w| unsafe { w.fmode().bits(0b00) });
596-
597-
// Write alternate-bytes
598-
self.rb.abr.write(|w| unsafe {
599-
w.alternate().bits(alternate_bytes.bits())
600-
});
601-
602-
// Write instruction. If there is no address or data phase, the
603-
// transaction starts here.
604-
#[cfg(any(feature = "rm0433", feature = "rm0399"))]
605-
{
606-
let ir = instruction.bits_u8()?;
607-
self.rb.ccr.modify(|_, w| unsafe { w.instruction().bits(ir) });
608-
}
609-
#[cfg(any(feature = "rm0455", feature = "rm0468"))]
610-
self.rb.ir.write(|w| unsafe {
611-
w.instruction().bits(instruction.bits())
612-
});
613-
614-
// Write the address. The transaction starts on the next write
615-
// to DATA, unless there is no DATA phase configured, in which
616-
// case it starts here.
617-
self.rb.ar.write(|w| unsafe { w.address().bits(address.bits()) });
619+
// Setup extended mode. Typically no dummy cycles in write mode
620+
self.setup_extended(instruction, address, alternate_bytes, 0, !data.is_empty(), false);
618621

619622
// Write data to the FIFO in a byte-wise manner.
623+
// Transaction starts here
620624
unsafe {
621625
for byte in data {
622626
ptr::write_volatile(&self.rb.dr as *const _ as *mut u8, *byte);
@@ -751,10 +755,6 @@ mod common {
751755

752756
self.is_busy()?;
753757

754-
// Setup extended mode. Read operations always have a data phase.
755-
self.setup_extended(instruction, address, alternate_bytes,
756-
dummy_cycles, true);
757-
758758
// Clear the transfer complete flag.
759759
self.rb.fcr.write(|w| w.ctcf().set_bit());
760760

@@ -763,28 +763,10 @@ mod common {
763763
.dlr
764764
.write(|w| unsafe { w.dl().bits(dest.len() as u32 - 1) });
765765

766-
// Configure the mode to indirect read.
767-
fmode_reg!(self).modify(|_, w| unsafe { w.fmode().bits(0b01) });
768-
769-
// Write alternate-bytes
770-
self.rb.abr.write(|w| unsafe {
771-
w.alternate().bits(alternate_bytes.bits())
772-
});
773-
774-
// Write instruction. If there is no address phase, the
775-
// transaction starts here.
776-
#[cfg(any(feature = "rm0433", feature = "rm0399"))]
777-
{
778-
let ir = instruction.bits_u8()?;
779-
self.rb.ccr.modify(|_, w| unsafe { w.instruction().bits(ir) });
780-
}
781-
#[cfg(any(feature = "rm0455", feature = "rm0468"))]
782-
self.rb.ir.write(|w| unsafe {
783-
w.instruction().bits(instruction.bits())
784-
});
785-
786-
// Write the address. Transaction starts here.
787-
self.rb.ar.write(|w| unsafe { w.address().bits(address.bits()) });
766+
// Setup extended mode. Read operations always have a data phase.
767+
// Transaction starts here
768+
self.setup_extended(instruction, address, alternate_bytes,
769+
dummy_cycles, true, true);
788770

789771
// Wait for the transaction to complete
790772
while self.rb.sr.read().tcf().bit_is_clear() {}

0 commit comments

Comments
 (0)