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

Commit

Permalink
renamed DesiredIrqState to TargetIrqState, removed unused variant
Browse files Browse the repository at this point in the history
  • Loading branch information
ilya-epifanov committed Oct 11, 2023
1 parent 4ce8128 commit 78307d7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 35 deletions.
31 changes: 19 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,20 +212,26 @@ where
.process_irq(
self.radio_mode,
self.rx_continuous,
DesiredIrqState::Done,
TargetIrqState::Done,
&mut self.delay,
None,
None,
)
.await
{
Ok(()) => Ok(()),
Ok(TargetIrqState::Done) => {
self.radio_mode = RadioMode::Standby;
// self.radio_kind.ensure_ready(self.radio_mode).await?;
// self.radio_kind.set_standby().await?;
Ok(())
}
Err(err) => {
self.radio_kind.ensure_ready(self.radio_mode).await?;
self.radio_kind.set_standby().await?;
self.radio_mode = RadioMode::Standby;
Err(err)
}
Ok(_) => unreachable!(),
}
}

Expand Down Expand Up @@ -297,37 +303,37 @@ where
rx_pkt_params: &PacketParams,
receiving_buffer: &mut [u8],
) -> Result<(u8, PacketStatus), RadioError> {
let IrqState::Done(len, status) = self.rx_until_state(rx_pkt_params, receiving_buffer, DesiredIrqState::Done).await? else {
let IrqState::RxDone(len, status) = self.rx_until_state(rx_pkt_params, receiving_buffer, TargetIrqState::Done).await? else {
unreachable!();
};
Ok((len as u8, status))
Ok((len, status))
}

/// Obtain the results of a read operation
pub async fn rx_until_state(
&mut self,
rx_pkt_params: &PacketParams,
receiving_buffer: &mut [u8],
desired_rx_state: DesiredIrqState,
target_rx_state: TargetIrqState,
) -> Result<IrqState, RadioError> {
match self
.radio_kind
.process_irq(
self.radio_mode,
self.rx_continuous,
desired_rx_state,
target_rx_state,
&mut self.delay,
self.polling_timeout_in_ms,
None,
)
.await
{
Ok(()) => match desired_rx_state {
DesiredIrqState::PreambleReceived => Ok(IrqState::PreambleReceived),
DesiredIrqState::Done => {
Ok(actual_state) => match actual_state {
TargetIrqState::PreambleReceived => Ok(IrqState::PreambleReceived),
TargetIrqState::Done => {
let received_len = self.radio_kind.get_rx_payload(rx_pkt_params, receiving_buffer).await?;
let rx_pkt_status = self.radio_kind.get_rx_packet_status().await?;
Ok(IrqState::Done(received_len, rx_pkt_status))
Ok(IrqState::RxDone(received_len, rx_pkt_status))
}
},
Err(err) => {
Expand Down Expand Up @@ -376,20 +382,21 @@ where
.process_irq(
self.radio_mode,
self.rx_continuous,
DesiredIrqState::Done,
TargetIrqState::Done,
&mut self.delay,
None,
Some(&mut cad_activity_detected),
)
.await
{
Ok(()) => Ok(cad_activity_detected),
Ok(TargetIrqState::Done) => Ok(cad_activity_detected),
Err(err) => {
self.radio_kind.ensure_ready(self.radio_mode).await?;
self.radio_kind.set_standby().await?;
self.radio_mode = RadioMode::Standby;
Err(err)
}
Ok(_) => unreachable!(),
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/mod_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,22 @@ pub trait InterfaceVariant {
async fn disable_rf_switch(&mut self) -> Result<(), RadioError>;
}

/// Specifies an IRQ processing state to run the loop to
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum DesiredIrqState {
pub enum TargetIrqState {
/// Runs the loop until after the preamble has been received
PreambleReceived,
/// Runs the loop until the operation is fully complete
Done,
}

/// An actual operation state, including some details where necessary
#[derive(Clone, Copy)]
pub enum IrqState {
Waiting,
/// Preamble has been received
PreambleReceived,
Done(u8, PacketStatus),
/// The RX operation is complete
RxDone(u8, PacketStatus),
}

/// Functions implemented for a specific kind of LoRa chip, called internally by the outward facing
Expand Down Expand Up @@ -135,11 +140,11 @@ pub trait RadioKind {
&mut self,
radio_mode: RadioMode,
rx_continuous: bool,
desired_rx_state: DesiredIrqState,
target_rx_state: TargetIrqState,
delay: &mut impl DelayUs,
polling_timeout_in_ms: Option<u32>,
cad_activity_detected: Option<&mut bool>,
) -> Result<(), RadioError>;
) -> Result<TargetIrqState, RadioError>;
}

/// Internal trait for specifying that a [`RadioKind`] object has RNG capability.
Expand Down
16 changes: 8 additions & 8 deletions src/sx1261_2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use embedded_hal_async::spi::*;
use radio_kind_params::*;

use crate::mod_params::*;
use crate::mod_traits::DesiredIrqState;
use crate::mod_traits::TargetIrqState;
use crate::{InterfaceVariant, RadioKind, SpiInterface};

// Syncwords for public and private networks
Expand Down Expand Up @@ -823,11 +823,11 @@ where
&mut self,
radio_mode: RadioMode,
rx_continuous: bool,
desired_rx_state: DesiredIrqState,
target_rx_state: TargetIrqState,
delay: &mut impl DelayUs,
polling_timeout_in_ms: Option<u32>,
cad_activity_detected: Option<&mut bool>,
) -> Result<(), RadioError> {
) -> Result<TargetIrqState, RadioError> {
let mut iteration_guard: u32 = 0;
if polling_timeout_in_ms.is_some() {
iteration_guard = polling_timeout_in_ms.unwrap();
Expand Down Expand Up @@ -882,7 +882,7 @@ where
if radio_mode == RadioMode::Transmit {
if (irq_flags & IrqMask::TxDone.value()) == IrqMask::TxDone.value() {
debug!("TxDone in radio mode {}", radio_mode);
return Ok(());
return Ok(TargetIrqState::Done);
}
if (irq_flags & IrqMask::RxTxTimeout.value()) == IrqMask::RxTxTimeout.value() {
debug!("RxTxTimeout in radio mode {}", radio_mode);
Expand Down Expand Up @@ -929,12 +929,12 @@ where
];
self.intf.write(&[&register_and_evt_clear], false).await?;
}
return Ok(());
return Ok(TargetIrqState::Done);
}
if desired_rx_state == DesiredIrqState::PreambleReceived
if target_rx_state == TargetIrqState::PreambleReceived
&& (IrqMask::PreambleDetected.is_set_in(irq_flags) || IrqMask::HeaderValid.is_set_in(irq_flags))
{
return Ok(());
return Ok(TargetIrqState::PreambleReceived);
}
if (irq_flags & IrqMask::RxTxTimeout.value()) == IrqMask::RxTxTimeout.value() {
debug!("RxTxTimeout in radio mode {}", radio_mode);
Expand All @@ -948,7 +948,7 @@ where
*(cad_activity_detected.unwrap()) =
(irq_flags & IrqMask::CADActivityDetected.value()) == IrqMask::CADActivityDetected.value();
}
return Ok(());
return Ok(TargetIrqState::Done);
}

// if an interrupt occurred for other than an error or operation completion, loop to wait again
Expand Down
18 changes: 8 additions & 10 deletions src/sx1276_7_8_9/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use embedded_hal_async::spi::*;
use radio_kind_params::*;

use crate::mod_params::*;
use crate::mod_traits::DesiredIrqState;
use crate::mod_traits::TargetIrqState;
use crate::{InterfaceVariant, RadioKind, SpiInterface};

// Syncwords for public and private networks
Expand Down Expand Up @@ -539,11 +539,11 @@ where
&mut self,
radio_mode: RadioMode,
_rx_continuous: bool,
desired_rx_state: DesiredIrqState,
target_rx_state: TargetIrqState,
delay: &mut impl DelayUs,
polling_timeout_in_ms: Option<u32>,
cad_activity_detected: Option<&mut bool>,
) -> Result<(), RadioError> {
) -> Result<TargetIrqState, RadioError> {
let mut iteration_guard: u32 = 0;
if polling_timeout_in_ms.is_some() {
iteration_guard = polling_timeout_in_ms.unwrap();
Expand Down Expand Up @@ -580,21 +580,19 @@ where
if radio_mode == RadioMode::Transmit {
if (irq_flags & IrqMask::TxDone.value()) == IrqMask::TxDone.value() {
debug!("TxDone in radio mode {}", radio_mode);
return Ok(());
return Ok(TargetIrqState::Done);
}
} else if radio_mode == RadioMode::Receive {
if (irq_flags & IrqMask::CRCError.value()) == IrqMask::CRCError.value() {
debug!("CRCError in radio mode {}", radio_mode);
}
if (irq_flags & IrqMask::RxDone.value()) == IrqMask::RxDone.value() {
debug!("RxDone in radio mode {}", radio_mode);
return Ok(());
return Ok(TargetIrqState::Done);
}
if desired_rx_state == DesiredIrqState::PreambleReceived
&& (irq_flags & IrqMask::HeaderValid.value()) == IrqMask::HeaderValid.value()
{
if target_rx_state == TargetIrqState::PreambleReceived && IrqMask::HeaderValid.is_set_in(irq_flags) {
debug!("HeaderValid in radio mode {}", radio_mode);
return Ok(());
return Ok(TargetIrqState::PreambleReceived);
}
if (irq_flags & IrqMask::RxTimeout.value()) == IrqMask::RxTimeout.value() {
debug!("RxTimeout in radio mode {}", radio_mode);
Expand All @@ -608,7 +606,7 @@ where
*(cad_activity_detected.unwrap()) =
(irq_flags & IrqMask::CADActivityDetected.value()) == IrqMask::CADActivityDetected.value();
}
return Ok(());
return Ok(TargetIrqState::Done);
}

// if an interrupt occurred for other than an error or operation completion, loop to wait again
Expand Down

0 comments on commit 78307d7

Please sign in to comment.