Skip to content

Commit

Permalink
cleanup CS pin handling now we have SpiDevice
Browse files Browse the repository at this point in the history
  • Loading branch information
ryankurte committed Apr 18, 2024
1 parent 3fda64e commit 600f8fd
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 37 deletions.
8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,16 @@ default = [ "std", "util", "serde", "driver-pal/hal-cp2130", "driver-pal/hal-lin
[dependencies]
radio = { version = "0.11.0", default_features = false }
embedded-hal = "1.0.0"
defmt = {version = "0.3.5", optional = true }

driver-pal = { version = "0.8.0", default_features = false, optional=true }
embedded-hal-bus = "0.1.0"
driver-pal = { version = "0.9.0", default_features = false, optional=true }

defmt = {version = "0.3.5", optional = true }
bitflags = "2.4.0"
libc = "0.2.123"
log = { version = "0.4.17", default_features = false }
strum = { version = "0.26.2", default_features = false, features = [ "derive" ] }

crc16 = { version = "0.4.0", optional = true }
hex = { version = "0.4.2", optional = true }

humantime = { version = "2.0.1", optional = true }
clap = { version = "4.4.7", optional = true, features = [ "derive", "env" ] }
thiserror = { version = "1.0.30", optional = true }
Expand Down
29 changes: 7 additions & 22 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,26 +151,23 @@ where
/// Base interface for radio device
pub struct Base<
Spi: SpiDevice<u8>,
Cs: OutputPin,
Busy: InputPin,
Ready: InputPin,
Sdn: OutputPin,
Delay: DelayNs,
> {
pub spi: Spi,
pub cs: Cs,
pub busy: Busy,
pub ready: Ready,
pub sdn: Sdn,
pub delay: Delay,
}

impl<Spi, Cs, Busy, Ready, Sdn, PinError, Delay> Hal for Base<Spi, Cs, Busy, Ready, Sdn, Delay>
impl<Spi, Busy, Ready, Sdn, PinError, Delay> Hal for Base<Spi, Busy, Ready, Sdn, Delay>
where
Spi: SpiDevice<u8>,
<Spi as ErrorType>::Error: Debug + 'static,

Cs: OutputPin<Error = PinError>,
Busy: InputPin<Error = PinError>,
Ready: InputPin<Error = PinError>,
Sdn: OutputPin<Error = PinError>,
Expand Down Expand Up @@ -212,12 +209,12 @@ where

/// Delay for the specified time
fn delay_ms(&mut self, ms: u32) {
self.delay_us(ms * 1000);
self.delay.delay_ms(ms);
}

/// Delay for the specified time
fn delay_us(&mut self, us: u32) {
self.delay_ns(us * 1000);
self.delay.delay_us(us);
}

fn delay_ns(&mut self, ns: u32) {
Expand All @@ -230,16 +227,10 @@ where
prefix: &[u8],
data: &[u8],
) -> Result<(), Error<Self::CommsError, Self::PinError>> {
self.cs.set_low().map_err(Error::Pin)?;

let r = self
self
.spi
.transaction(&mut [Operation::Write(prefix), Operation::Write(data)])
.map_err(Error::Comms);

self.cs.set_high().map_err(Error::Pin)?;

r
.map_err(Error::Comms)
}

/// Read data with prefix, asserting CS as required
Expand All @@ -248,16 +239,10 @@ where
prefix: &[u8],
data: &mut [u8],
) -> Result<(), Error<Self::CommsError, Self::PinError>> {
self.cs.set_low().map_err(Error::Pin)?;

let r = self
self
.spi
.transaction(&mut [Operation::Write(prefix), Operation::Read(data)])
.map_err(Error::Comms);

self.cs.set_high().map_err(Error::Pin)?;

r
.map_err(Error::Comms)
}

/// Write the specified command and data
Expand Down
11 changes: 4 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,15 @@ pub enum Error<CommsError: Debug + 'static, PinError: Debug + 'static> {
NoComms,
}

pub type Sx128xSpi<Spi, CsPin, BusyPin, ReadyPin, SdnPin, DelayPin> =
Sx128x<Base<Spi, CsPin, BusyPin, ReadyPin, SdnPin, DelayPin>>;
pub type Sx128xSpi<Spi, BusyPin, ReadyPin, SdnPin, DelayPin> =
Sx128x<Base<Spi, BusyPin, ReadyPin, SdnPin, DelayPin>>;

impl<Spi, CsPin, BusyPin, ReadyPin, SdnPin, PinError, Delay>
Sx128x<Base<Spi, CsPin, BusyPin, ReadyPin, SdnPin, Delay>>
impl<Spi, BusyPin, ReadyPin, SdnPin, PinError, Delay>
Sx128x<Base<Spi, BusyPin, ReadyPin, SdnPin, Delay>>
where
Spi: SpiDevice,
<Spi as ErrorType>::Error: Debug,

CsPin: OutputPin<Error = PinError>,
BusyPin: InputPin<Error = PinError>,
ReadyPin: InputPin<Error = PinError>,
SdnPin: OutputPin<Error = PinError>,
Expand All @@ -164,7 +163,6 @@ where
/// Create an Sx128x with the provided `Spi` implementation and pins
pub fn spi(
spi: Spi,
cs: CsPin,
busy: BusyPin,
ready: ReadyPin,
sdn: SdnPin,
Expand All @@ -174,7 +172,6 @@ where
// Create SpiWrapper over spi/cs/busy
let hal = Base {
spi,
cs,
sdn,
busy,
ready,
Expand Down
1 change: 0 additions & 1 deletion src/util/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ fn main() {
info!("Initialising Radio");
let mut radio = Sx128x::spi(
spi,
pins.cs,
pins.busy,
pins.ready,
pins.reset,
Expand Down
3 changes: 1 addition & 2 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use driver_pal::hal::*;
use radio::{Receive, Transmit};
use radio_sx128x::{base::Base, prelude::*};

pub type SpiWrapper = Base<HalSpi, HalOutputPin, HalInputPin, HalInputPin, HalOutputPin, HalDelay>;
pub type SpiWrapper = Base<HalSpi, HalInputPin, HalInputPin, HalOutputPin, HalDelay>;

pub type Radio = Sx128x<SpiWrapper>;

Expand All @@ -31,7 +31,6 @@ fn load_radio(rf_config: &Config, device_config: &DeviceConfig) -> Radio {

let radio = Sx128x::spi(
spi,
pins.cs,
pins.busy,
pins.ready,
pins.reset,
Expand Down

0 comments on commit 600f8fd

Please sign in to comment.