Skip to content

Commit

Permalink
Reconfigure Spi new functions for better type inference
Browse files Browse the repository at this point in the history
  • Loading branch information
fu5ha committed Oct 22, 2023
1 parent 661b98a commit bffb9a3
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions rp2040-hal/src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//! let spi_device = peripherals.SPI0;
//! let spi_pin_layout = (mosi, sclk);
//!
//! let spi = Spi::<_, _, _, 8>::new(spi_device, spi_pin_layout)
//! let spi = Spi::new(spi_device, spi_pin_layout)
//! .init(&mut peripherals.RESETS, 125_000_000u32.Hz(), 16_000_000u32.Hz(), MODE_0);
//! ```

Expand Down Expand Up @@ -174,11 +174,12 @@ impl Sealed for u16 {}
/// - `DS`: The "data size", i.e. the number of bits transferred per data frame. Defaults to 8.
///
/// In most cases you won't have to specify these types manually and can let the compiler infer
/// them for you based on the values you pass in to `new`. If you want to select a different
/// data frame size, you'll need to do that by specifying the `DS` parameter manually.
/// them for you based on the values you pass in to [`new`][Self::new], which assumes the default
/// data frame size of 8 bits. If you want to select a different data frame size, you'll need to
/// use [`new_with_data_size`][Self::new_with_data_size].
///
/// See [the module level docs][self] for an example.
pub struct Spi<S: State, D: SpiDevice, P: ValidSpiPinout<D>, const DS: u8 = 8u8> {
pub struct Spi<S: State, D: SpiDevice, P: ValidSpiPinout<D>, const DS: u8> {
device: D,
pins: P,
state: PhantomData<S>,
Expand Down Expand Up @@ -251,17 +252,33 @@ impl<S: State, D: SpiDevice, P: ValidSpiPinout<D>, const DS: u8> Spi<S, D, P, DS
}
}

impl<D: SpiDevice, P: ValidSpiPinout<D>, const DS: u8> Spi<Disabled, D, P, DS> {
/// Create new not initialized Spi bus. Initialize it with [`.init`][Self::init]
impl<D: SpiDevice, P: ValidSpiPinout<D>> Spi<Disabled, D, P, 8> {
/// Create new (not initialized) Spi bus with the default data size (8 bits).
///
/// Initialize it with [`.init`][Self::init]
/// or [`.init_slave`][Self::init_slave].
pub fn new(device: D, pins: P) -> Spi<Disabled, D, P, DS> {
pub fn new(device: D, pins: P) -> Spi<Disabled, D, P, 8> {
Spi {
device,
pins,
state: PhantomData,
}
}

/// Create new (not initialized) Spi bus with a non-default data size.
///
/// Initialize it with [`.init`][Self::init]
/// or [`.init_slave`][Self::init_slave].
pub fn new_with_data_size<const DS: u8>(device: D, pins: P) -> Spi<Disabled, D, P, DS> {
Spi {
device,
pins,
state: PhantomData,
}
}
}

impl<D: SpiDevice, P: ValidSpiPinout<D>, const DS: u8> Spi<Disabled, D, P, DS> {
/// Set format and datasize
fn set_format(&mut self, data_bits: u8, frame_format: FrameFormat) {
self.device.sspcr0.modify(|_, w| unsafe {
Expand Down

0 comments on commit bffb9a3

Please sign in to comment.