Skip to content

Commit

Permalink
spi: implement setting bit order
Browse files Browse the repository at this point in the history
  • Loading branch information
liebman committed May 5, 2024
1 parent 209a82b commit 605577e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
54 changes: 54 additions & 0 deletions esp-hal/src/spi/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ use super::{
HalfDuplexMode,
IsFullDuplex,
IsHalfDuplex,
SpiBitOrder,
SpiDataMode,
SpiMode,
};
Expand Down Expand Up @@ -481,6 +482,14 @@ where
self
}

/// Set the bit order for the SPI instance.
///
/// The default is MSB first for both read and write.
pub fn with_bit_order(mut self, read_order: SpiBitOrder, write_order: SpiBitOrder) -> Self {
self.spi.set_bit_order(read_order, write_order);
self
}

/// Setup pins for this SPI instance.
///
/// All pins are optional. Pass [crate::gpio::NO_PIN] if you don't need the
Expand Down Expand Up @@ -718,6 +727,14 @@ where
pub fn change_bus_frequency(&mut self, frequency: HertzU32, clocks: &Clocks) {
self.spi.ch_bus_freq(frequency, clocks);
}

/// Set the bit order for the SPI instance.
///
/// The default is MSB first for both read and write.
pub fn with_bit_order(mut self, read_order: SpiBitOrder, write_order: SpiBitOrder) -> Self {
self.spi.set_bit_order(read_order, write_order);
self
}
}

impl<T, M> HalfDuplexReadWrite for Spi<'_, T, M>
Expand Down Expand Up @@ -2630,6 +2647,43 @@ pub trait Instance: crate::private::Sealed {
});
}

#[cfg(not(any(esp32, esp32s2)))]
fn set_bit_order(&mut self, read_order: SpiBitOrder, write_order: SpiBitOrder) {
let reg_block = self.register_block();

let read_value = match read_order {
SpiBitOrder::MSBFirst => 0,
SpiBitOrder::LSBFirst => 1,
};
let write_value = match write_order {
SpiBitOrder::MSBFirst => 0,
SpiBitOrder::LSBFirst => 1,
};
reg_block.ctrl().modify(|_, w| unsafe {
w.rd_bit_order().bits(read_value);
w.wr_bit_order().bits(write_value);
w
});
}
#[cfg(any(esp32, esp32s2))]
fn set_bit_order(&mut self, read_order: SpiBitOrder, write_order: SpiBitOrder) {
let reg_block = self.register_block();

let read_value = match read_order {
SpiBitOrder::MSBFirst => false,
SpiBitOrder::LSBFirst => true,
};
let write_value = match write_order {
SpiBitOrder::MSBFirst => false,
SpiBitOrder::LSBFirst => true,
};
reg_block.ctrl().modify(|_, w| unsafe {
w.rd_bit_order().bit(read_value);
w.wr_bit_order().bit(write_value);
w
});
}

fn read_byte(&mut self) -> nb::Result<u8, Error> {
if self.busy() {
return Err(nb::Error::WouldBlock);
Expand Down
8 changes: 8 additions & 0 deletions esp-hal/src/spi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ pub enum SpiMode {
Mode3,
}

/// SPI Bit Order
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum SpiBitOrder {
MSBFirst,
LSBFirst,
}

pub trait DuplexMode {}
pub trait IsFullDuplex: DuplexMode {}
pub trait IsHalfDuplex: DuplexMode {}
Expand Down

0 comments on commit 605577e

Please sign in to comment.