Skip to content

Commit a9a34a9

Browse files
committed
framesize
1 parent f1b674c commit a9a34a9

File tree

1 file changed

+58
-19
lines changed

1 file changed

+58
-19
lines changed

src/spi.rs

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ impl Ms for Master {
132132
}
133133

134134
#[derive(Debug)]
135-
pub struct Spi<SPI, PINS, const BIDI: bool = false, OPERATION = Master> {
135+
pub struct Spi<SPI, PINS, const BIDI: bool = false, FrameSize = u8, OPERATION = Master> {
136136
spi: SPI,
137137
pins: PINS,
138-
_operation: PhantomData<OPERATION>,
138+
_operation: PhantomData<(FrameSize, OPERATION)>,
139139
}
140140

141141
// Implemented by all SPI instances
@@ -149,8 +149,8 @@ pub trait Instance:
149149
// Implemented by all SPI instances
150150
macro_rules! spi {
151151
($SPI:ty: $Spi:ident) => {
152-
pub type $Spi<PINS, const BIDI: bool = false, OPERATION = Master> =
153-
Spi<$SPI, PINS, BIDI, OPERATION>;
152+
pub type $Spi<PINS, const BIDI: bool = false, FrameSize = u8, OPERATION = Master> =
153+
Spi<$SPI, PINS, BIDI, FrameSize, OPERATION>;
154154

155155
impl Instance for $SPI {
156156
fn ptr() -> *const spi1::RegisterBlock {
@@ -281,30 +281,58 @@ impl<SPI: Instance, PINS, const BIDI: bool, OPERATION: Ms> Spi<SPI, PINS, BIDI,
281281
}
282282
}
283283

284-
impl<SPI: Instance, PINS, OPERATION: Ms> Spi<SPI, PINS, false, OPERATION> {
284+
impl<SPI: Instance, PINS, FrameSize, OPERATION: Ms> Spi<SPI, PINS, false, OPERATION> {
285285
pub fn to_bidi_transfer_mode(self) -> Spi<SPI, PINS, true, OPERATION> {
286286
self.into_mode()
287287
}
288288
}
289289

290-
impl<SPI: Instance, PINS, OPERATION: Ms> Spi<SPI, PINS, true, OPERATION> {
290+
impl<SPI: Instance, PINS, FrameSize, OPERATION: Ms> Spi<SPI, PINS, true, OPERATION> {
291291
pub fn to_normal_transfer_mode(self) -> Spi<SPI, PINS, false, OPERATION> {
292292
self.into_mode()
293293
}
294294
}
295295

296-
impl<SPI: Instance, PINS, const BIDI: bool> Spi<SPI, PINS, BIDI, Master> {
296+
impl<SPI: Instance, PINS, const BIDI: bool, FrameSize> Spi<SPI, PINS, BIDI, FrameSize, Master> {
297297
pub fn to_slave_operation(self) -> Spi<SPI, PINS, BIDI, Slave> {
298298
self.into_mode()
299299
}
300300
}
301301

302-
impl<SPI: Instance, PINS, const BIDI: bool> Spi<SPI, PINS, BIDI, Slave> {
302+
impl<SPI: Instance, PINS, const BIDI: bool, FrameSize> Spi<SPI, PINS, BIDI, FrameSize, Slave> {
303303
pub fn to_master_operation(self) -> Spi<SPI, PINS, BIDI, Master> {
304304
self.into_mode()
305305
}
306306
}
307307

308+
impl<SPI, REMAP, PINS, const BIDI: bool, OPERATION> Spi<SPI, REMAP, PINS, u8, OPERATION>
309+
where
310+
SPI: Instance,
311+
{
312+
/// Converts from 8bit dataframe to 16bit.
313+
pub fn frame_size_16bit(self) -> Spi<SPI, REMAP, PINS, u16> {
314+
self.spi.cr1.modify(|_, w| w.spe().clear_bit());
315+
self.spi
316+
.cr1
317+
.modify(|_, w| w.dff().set_bit().spe().set_bit());
318+
Spi::_new(spi, pins)
319+
}
320+
}
321+
322+
impl<SPI, REMAP, PINS, const BIDI: bool, OPERATION> Spi<SPI, REMAP, PINS, u16, OPERATION>
323+
where
324+
SPI: Instance,
325+
{
326+
/// Converts from 16bit dataframe to 8bit.
327+
pub fn frame_size_8bit(self) -> Spi<SPI, REMAP, PINS, u8> {
328+
self.spi.cr1.modify(|_, w| w.spe().clear_bit());
329+
self.spi
330+
.cr1
331+
.modify(|_, w| w.dff().clear_bit().spe().set_bit());
332+
Spi::_new(spi, pins)
333+
}
334+
}
335+
308336
impl<SPI: Instance, SCK, MISO, MOSI> Spi<SPI, (SCK, MISO, MOSI), false, Master> {
309337
pub fn new(
310338
spi: SPI,
@@ -556,7 +584,7 @@ impl<SPI: Instance, PINS, const BIDI: bool, OPERATION> Spi<SPI, PINS, BIDI, OPER
556584
}
557585

558586
#[inline(always)]
559-
fn check_read(&mut self) -> nb::Result<u8, Error> {
587+
fn check_read(&mut self) -> nb::Result<FrameSize, Error> {
560588
let sr = self.spi.sr.read();
561589

562590
Err(if sr.ovr().bit_is_set() {
@@ -566,14 +594,14 @@ impl<SPI: Instance, PINS, const BIDI: bool, OPERATION> Spi<SPI, PINS, BIDI, OPER
566594
} else if sr.crcerr().bit_is_set() {
567595
Error::Crc.into()
568596
} else if sr.rxne().bit_is_set() {
569-
return Ok(self.read_u8());
597+
return Ok(self.read_data_reg());
570598
} else {
571599
nb::Error::WouldBlock
572600
})
573601
}
574602

575603
#[inline(always)]
576-
fn check_send(&mut self, byte: u8) -> nb::Result<(), Error> {
604+
fn check_send(&mut self, byte: FrameSize) -> nb::Result<(), Error> {
577605
let sr = self.spi.sr.read();
578606

579607
Err(if sr.ovr().bit_is_set() {
@@ -592,23 +620,34 @@ impl<SPI: Instance, PINS, const BIDI: bool, OPERATION> Spi<SPI, PINS, BIDI, OPER
592620
});
593621
Error::Crc.into()
594622
} else if sr.txe().bit_is_set() {
595-
self.send_u8(byte);
623+
self.write_data_reg(byte);
596624
return Ok(());
597625
} else {
598626
nb::Error::WouldBlock
599627
})
600628
}
629+
}
601630

602-
#[inline(always)]
603-
fn read_u8(&mut self) -> u8 {
604-
// NOTE(read_volatile) read only 1 byte (the svd2rust API only allows reading a half-word)
605-
unsafe { ptr::read_volatile(&self.spi.dr as *const _ as *const u8) }
631+
pub trait ReadWriteReg<T> {
632+
fn read_data_reg(&mut self) -> T;
633+
fn write_data_reg(&mut self, data: T);
634+
}
635+
636+
impl<SPI, REMAP, PINS, const BIDI: bool, OPERATION, FrameSize> SpiReadWrite<FrameSize>
637+
for Spi<SPI, REMAP, PINS, FrameSize, OPERATION>
638+
where
639+
SPI: Instance,
640+
FrameSize: Copy,
641+
{
642+
fn read_data_reg(&mut self) -> FrameSize {
643+
// NOTE(read_volatile) read only 1 byte (the svd2rust API only allows
644+
// reading a half-word)
645+
unsafe { ptr::read_volatile(&self.spi.dr as *const _ as *const FrameSize) }
606646
}
607647

608-
#[inline(always)]
609-
fn send_u8(&mut self, byte: u8) {
648+
fn write_data_reg(&mut self, data: FrameSize) {
610649
// NOTE(write_volatile) see note above
611-
unsafe { ptr::write_volatile(&self.spi.dr as *const _ as *mut u8, byte) }
650+
unsafe { ptr::write_volatile(&self.spi.dr as *const _ as *mut FrameSize, data) }
612651
}
613652
}
614653

0 commit comments

Comments
 (0)