Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

SpiBus -> SpiDevice #35

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 2 additions & 38 deletions src/interface.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use defmt::trace;
use embedded_hal_async::spi::SpiBus;
use embedded_hal_async::spi::SpiDevice;

use crate::mod_params::RadioError;
CBJamo marked this conversation as resolved.
Show resolved Hide resolved
use crate::mod_params::RadioError::*;
Expand All @@ -12,7 +12,7 @@ pub(crate) struct SpiInterface<SPI, IV> {

impl<SPI, IV> SpiInterface<SPI, IV>
where
SPI: SpiBus<u8>,
SPI: SpiDevice<u8>,
IV: InterfaceVariant,
{
pub fn new(spi: SPI, iv: IV) -> Self {
Expand All @@ -21,19 +21,12 @@ where

// Write one or more buffers to the radio.
pub async fn write(&mut self, write_buffers: &[&[u8]], is_sleep_command: bool) -> Result<(), RadioError> {
self.iv.set_nss_low().await?;
for buffer in write_buffers {
let write_result = self.spi.write(buffer).await.map_err(|_| SPI);
let flush_result = self.spi.flush().await.map_err(|_| SPI);
if write_result != Ok(()) {
let _err = self.iv.set_nss_high().await;
write_result?;
} else if flush_result != Ok(()) {
let _err = self.iv.set_nss_high().await;
flush_result?;
}
}
self.iv.set_nss_high().await?;

if !is_sleep_command {
self.iv.wait_on_busy().await?;
Expand Down Expand Up @@ -63,16 +56,10 @@ where
) -> Result<(), RadioError> {
let mut input = [0u8];

self.iv.set_nss_low().await?;
for buffer in write_buffers {
let write_result = self.spi.write(buffer).await.map_err(|_| SPI);
let flush_result = self.spi.flush().await.map_err(|_| SPI);
if write_result != Ok(()) {
let _err = self.iv.set_nss_high().await;
write_result?;
} else if flush_result != Ok(()) {
let _err = self.iv.set_nss_high().await;
flush_result?;
}
}

Expand All @@ -84,17 +71,11 @@ where
#[allow(clippy::needless_range_loop)]
for i in 0..number_to_read {
let transfer_result = self.spi.transfer(&mut input, &[0x00]).await.map_err(|_| SPI);
let flush_result = self.spi.flush().await.map_err(|_| SPI);
if transfer_result != Ok(()) {
let _err = self.iv.set_nss_high().await;
transfer_result?;
} else if flush_result != Ok(()) {
let _err = self.iv.set_nss_high().await;
flush_result?;
}
read_buffer[i] = input[0];
}
self.iv.set_nss_high().await?;

self.iv.wait_on_busy().await?;

Expand Down Expand Up @@ -123,43 +104,26 @@ where
let mut status = [0u8];
let mut input = [0u8];

self.iv.set_nss_low().await?;
for buffer in write_buffers {
let write_result = self.spi.write(buffer).await.map_err(|_| SPI);
let flush_result = self.spi.flush().await.map_err(|_| SPI);
if write_result != Ok(()) {
let _err = self.iv.set_nss_high().await;
write_result?;
} else if flush_result != Ok(()) {
let _err = self.iv.set_nss_high().await;
flush_result?;
}
}

let transfer_result = self.spi.transfer(&mut status, &[0x00]).await.map_err(|_| SPI);
let flush_result = self.spi.flush().await.map_err(|_| SPI);
if transfer_result != Ok(()) {
let _err = self.iv.set_nss_high().await;
transfer_result?;
} else if flush_result != Ok(()) {
let _err = self.iv.set_nss_high().await;
flush_result?;
}

#[allow(clippy::needless_range_loop)]
for i in 0..read_buffer.len() {
let transfer_result = self.spi.transfer(&mut input, &[0x00]).await.map_err(|_| SPI);
let flush_result = self.spi.flush().await.map_err(|_| SPI);
if transfer_result != Ok(()) {
let _err = self.iv.set_nss_high().await;
transfer_result?;
} else if flush_result != Ok(()) {
let _err = self.iv.set_nss_high().await;
flush_result?;
}
read_buffer[i] = input[0];
}
self.iv.set_nss_high().await?;

self.iv.wait_on_busy().await?;

Expand Down
4 changes: 0 additions & 4 deletions src/mod_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ use crate::mod_params::*;
pub trait InterfaceVariant {
/// Set the LoRa board type
fn set_board_type(&mut self, board_type: BoardType);
/// Select the LoRa chip for an operation
async fn set_nss_low(&mut self) -> Result<(), RadioError>;
/// De-select the LoRa chip after an operation
async fn set_nss_high(&mut self) -> Result<(), RadioError>;
/// Reset the LoRa chip
async fn reset(&mut self, delay: &mut impl DelayUs) -> Result<(), RadioError>;
/// Wait for the LoRa chip to become available for an operation
Expand Down
6 changes: 3 additions & 3 deletions src/sx1261_2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct SX1261_2<SPI, IV> {

impl<SPI, IV> SX1261_2<SPI, IV>
where
SPI: SpiBus<u8>,
SPI: SpiDevice<u8>,
IV: InterfaceVariant,
{
/// Create an instance of the RadioKind implementation for the LoRa chip kind and board type
Expand Down Expand Up @@ -154,7 +154,7 @@ where

impl<SPI, IV> RadioKind for SX1261_2<SPI, IV>
where
SPI: SpiBus<u8>,
SPI: SpiDevice<u8>,
IV: InterfaceVariant,
{
fn get_board_type(&self) -> BoardType {
Expand Down Expand Up @@ -965,7 +965,7 @@ where

impl<SPI, IV> crate::RngRadio for SX1261_2<SPI, IV>
where
SPI: SpiBus<u8>,
SPI: SpiDevice<u8>,
IV: InterfaceVariant,
{
/// Generate a 32 bit random value based on the RSSI readings, after disabling all interrupts.
Expand Down
4 changes: 2 additions & 2 deletions src/sx1276_7_8_9/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct SX1276_7_8_9<SPI, IV> {

impl<SPI, IV> SX1276_7_8_9<SPI, IV>
where
SPI: SpiBus<u8>,
SPI: SpiDevice<u8>,
IV: InterfaceVariant,
{
/// Create an instance of the RadioKind implementation for the LoRa chip kind and board type
Expand Down Expand Up @@ -77,7 +77,7 @@ where

impl<SPI, IV> RadioKind for SX1276_7_8_9<SPI, IV>
where
SPI: SpiBus<u8>,
SPI: SpiDevice<u8>,
IV: InterfaceVariant,
{
fn get_board_type(&self) -> BoardType {
Expand Down