From bffb9a310d6e427a5251e9c4d7096cfb69fc1d21 Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Sun, 22 Oct 2023 13:50:31 +0200 Subject: [PATCH] Reconfigure Spi new functions for better type inference --- rp2040-hal/src/spi.rs | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/rp2040-hal/src/spi.rs b/rp2040-hal/src/spi.rs index e188d1598..a41d70762 100644 --- a/rp2040-hal/src/spi.rs +++ b/rp2040-hal/src/spi.rs @@ -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); //! ``` @@ -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, const DS: u8 = 8u8> { +pub struct Spi, const DS: u8> { device: D, pins: P, state: PhantomData, @@ -251,10 +252,12 @@ impl, const DS: u8> Spi, const DS: u8> Spi { - /// Create new not initialized Spi bus. Initialize it with [`.init`][Self::init] +impl> Spi { + /// 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 { + pub fn new(device: D, pins: P) -> Spi { Spi { device, pins, @@ -262,6 +265,20 @@ impl, const DS: u8> Spi { } } + /// 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(device: D, pins: P) -> Spi { + Spi { + device, + pins, + state: PhantomData, + } + } +} + +impl, const DS: u8> Spi { /// Set format and datasize fn set_format(&mut self, data_bits: u8, frame_format: FrameFormat) { self.device.sspcr0.modify(|_, w| unsafe {