Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion crates/rf24-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmt = {version = "1.0.1", optional = true}
embedded-hal = "1.0.0"

[dev-dependencies]
embedded-hal-mock = "0.11.1"
embedded-hal-mock = {git = "https://github.com/2bndy5/embedded-hal-mock.git", branch = "dev"}

[features]
defmt = ["dep:defmt"]
Expand Down
150 changes: 62 additions & 88 deletions crates/rf24-rs/src/radio/prelude.rs

Large diffs are not rendered by default.

16 changes: 7 additions & 9 deletions crates/rf24-rs/src/radio/rf24/auto_ack.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use embedded_hal::{delay::DelayNs, digital::OutputPin, spi::SpiDevice};

use crate::radio::{prelude::EsbAutoAck, Nrf24Error, RF24};
use crate::radio::{prelude::EsbAutoAck, RF24};

use super::{commands, registers, Feature};

Expand All @@ -10,9 +10,7 @@ where
DO: OutputPin,
DELAY: DelayNs,
{
type AutoAckErrorType = Nrf24Error<SPI::Error, DO::Error>;

fn set_ack_payloads(&mut self, enable: bool) -> Result<(), Self::AutoAckErrorType> {
fn set_ack_payloads(&mut self, enable: bool) -> Result<(), Self::Error> {
if self.feature.ack_payloads() != enable {
self.spi_read(1, registers::FEATURE)?;
self.feature =
Expand All @@ -36,7 +34,7 @@ where
self.feature.ack_payloads()
}

fn set_auto_ack(&mut self, enable: bool) -> Result<(), Self::AutoAckErrorType> {
fn set_auto_ack(&mut self, enable: bool) -> Result<(), Self::Error> {
self.spi_write_byte(registers::EN_AA, 0x3F * enable as u8)?;
// accommodate ACK payloads feature
if !enable && self.feature.ack_payloads() {
Expand All @@ -45,7 +43,7 @@ where
Ok(())
}

fn set_auto_ack_pipe(&mut self, enable: bool, pipe: u8) -> Result<(), Self::AutoAckErrorType> {
fn set_auto_ack_pipe(&mut self, enable: bool, pipe: u8) -> Result<(), Self::Error> {
if pipe > 5 {
return Ok(());
}
Expand All @@ -58,12 +56,12 @@ where
self.spi_write_byte(registers::EN_AA, reg_val & !mask | (mask * enable as u8))
}

fn allow_ask_no_ack(&mut self, enable: bool) -> Result<(), Self::AutoAckErrorType> {
fn allow_ask_no_ack(&mut self, enable: bool) -> Result<(), Self::Error> {
self.spi_read(1, registers::FEATURE)?;
self.spi_write_byte(registers::FEATURE, self.buf[1] & !1 | enable as u8)
}

fn write_ack_payload(&mut self, pipe: u8, buf: &[u8]) -> Result<bool, Self::AutoAckErrorType> {
fn write_ack_payload(&mut self, pipe: u8, buf: &[u8]) -> Result<bool, Self::Error> {
if self.feature.ack_payloads() && pipe <= 5 {
let len = buf.len().min(32);
self.spi_write_buf(commands::W_ACK_PAYLOAD | pipe, &buf[..len])?;
Expand All @@ -72,7 +70,7 @@ where
Ok(false)
}

fn set_auto_retries(&mut self, delay: u8, count: u8) -> Result<(), Self::AutoAckErrorType> {
fn set_auto_retries(&mut self, delay: u8, count: u8) -> Result<(), Self::Error> {
self.spi_write_byte(registers::SETUP_RETR, count.min(15) | (delay.min(15) << 4))
}
}
Expand Down
8 changes: 3 additions & 5 deletions crates/rf24-rs/src/radio/rf24/channel.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::registers;
use crate::radio::{prelude::EsbChannel, Nrf24Error, RF24};
use crate::radio::{prelude::EsbChannel, RF24};
use embedded_hal::{delay::DelayNs, digital::OutputPin, spi::SpiDevice};

impl<SPI, DO, DELAY> EsbChannel for RF24<SPI, DO, DELAY>
Expand All @@ -8,16 +8,14 @@ where
DO: OutputPin,
DELAY: DelayNs,
{
type ChannelErrorType = Nrf24Error<SPI::Error, DO::Error>;

/// The nRF24L01 support 126 channels. The specified `channel` is
/// clamped to the range [0, 125].
fn set_channel(&mut self, channel: u8) -> Result<(), Self::ChannelErrorType> {
fn set_channel(&mut self, channel: u8) -> Result<(), Self::Error> {
self.spi_write_byte(registers::RF_CH, channel.min(125))
}

/// See also [`RF24::set_channel()`].
fn get_channel(&mut self) -> Result<u8, Self::ChannelErrorType> {
fn get_channel(&mut self) -> Result<u8, Self::Error> {
self.spi_read(1, registers::RF_CH)?;
Ok(self.buf[1])
}
Expand Down
6 changes: 2 additions & 4 deletions crates/rf24-rs/src/radio/rf24/crc_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ where
DO: OutputPin,
DELAY: DelayNs,
{
type CrcLengthErrorType = Nrf24Error<SPI::Error, DO::Error>;

fn get_crc_length(&mut self) -> Result<CrcLength, Self::CrcLengthErrorType> {
fn get_crc_length(&mut self) -> Result<CrcLength, Self::Error> {
self.spi_read(1, registers::CONFIG)?;
if self.buf[1] & ConfigReg::CRC_MASK == 4 {
return Err(Nrf24Error::BinaryCorruption);
Expand All @@ -21,7 +19,7 @@ where
Ok(self.config_reg.crc_length())
}

fn set_crc_length(&mut self, crc_length: CrcLength) -> Result<(), Self::CrcLengthErrorType> {
fn set_crc_length(&mut self, crc_length: CrcLength) -> Result<(), Self::Error> {
self.spi_read(1, registers::CONFIG)?;
self.config_reg = self.config_reg.with_crc_length(crc_length);
self.spi_write_byte(registers::CONFIG, self.config_reg.into_bits())
Expand Down
6 changes: 2 additions & 4 deletions crates/rf24-rs/src/radio/rf24/data_rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ where
DO: OutputPin,
DELAY: DelayNs,
{
type DataRateErrorType = Nrf24Error<SPI::Error, DO::Error>;

fn get_data_rate(&mut self) -> Result<DataRate, Self::DataRateErrorType> {
fn get_data_rate(&mut self) -> Result<DataRate, Self::Error> {
self.spi_read(1, registers::RF_SETUP)?;
let da_bin = self.buf[1] & DataRate::MASK;
if da_bin == DataRate::MASK {
Expand All @@ -32,7 +30,7 @@ where
Ok(DataRate::from_bits(da_bin))
}

fn set_data_rate(&mut self, data_rate: DataRate) -> Result<(), Self::DataRateErrorType> {
fn set_data_rate(&mut self, data_rate: DataRate) -> Result<(), Self::Error> {
self.tx_delay = set_tx_delay(data_rate);
self.spi_read(1, registers::RF_SETUP)?;
let da_bin = data_rate.into_bits();
Expand Down
10 changes: 4 additions & 6 deletions crates/rf24-rs/src/radio/rf24/details.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Nrf24Error, RF24};
use super::RF24;
use crate::radio::prelude::EsbDetails;
use embedded_hal::{delay::DelayNs, digital::OutputPin, spi::SpiDevice};

Expand All @@ -18,11 +18,9 @@ where
DO: OutputPin,
DELAY: DelayNs,
{
type DetailsErrorType = Nrf24Error<SPI::Error, DO::Error>;

#[cfg(feature = "defmt")]
#[cfg(target_os = "none")]
fn print_details(&mut self) -> Result<(), Self::DetailsErrorType> {
fn print_details(&mut self) -> Result<(), Self::Error> {
defmt::println!("Is a plus variant_________{=bool}", self.is_plus_variant());

let channel = self.get_channel()?;
Expand Down Expand Up @@ -157,13 +155,13 @@ where
}

#[cfg(not(any(feature = "defmt", feature = "std")))]
fn print_details(&mut self) -> Result<(), Self::DetailsErrorType> {
fn print_details(&mut self) -> Result<(), Self::Error> {
Ok(())
}

#[cfg(not(target_os = "none"))]
#[cfg(feature = "std")]
fn print_details(&mut self) -> Result<(), Self::DetailsErrorType> {
fn print_details(&mut self) -> Result<(), Self::Error> {
use crate::radio::rf24::ConfigReg;

std::println!("Is a plus variant_________{}", self.is_plus_variant());
Expand Down
39 changes: 22 additions & 17 deletions crates/rf24-rs/src/radio/rf24/fifo.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
use embedded_hal::{delay::DelayNs, digital::OutputPin, spi::SpiDevice};

use crate::radio::{prelude::EsbFifo, Nrf24Error, RF24};
use crate::radio::{prelude::EsbFifo, RF24};
use crate::FifoState;

use super::{commands, registers};
use super::{commands, registers, Nrf24Error};

impl<SPI, DO, DELAY> EsbFifo for RF24<SPI, DO, DELAY>
where
SPI: SpiDevice,
DO: OutputPin,
DELAY: DelayNs,
{
type FifoErrorType = Nrf24Error<SPI::Error, DO::Error>;

fn available(&mut self) -> Result<bool, Self::FifoErrorType> {
fn available(&mut self) -> Result<bool, Self::Error> {
self.spi_read(1, registers::FIFO_STATUS)?;
Ok(self.buf[1] & 1 == 0)
}

fn available_pipe(&mut self, pipe: &mut u8) -> Result<bool, Self::FifoErrorType> {
fn available_pipe(&mut self, pipe: &mut u8) -> Result<bool, Self::Error> {
if self.available()? {
// RX FIFO is not empty
// get last used pipe
Expand All @@ -30,23 +28,24 @@ where
}

/// Use this to discard all 3 layers in the radio's RX FIFO.
fn flush_rx(&mut self) -> Result<(), Self::FifoErrorType> {
fn flush_rx(&mut self) -> Result<(), Self::Error> {
self.spi_read(0, commands::FLUSH_RX)
}

/// Use this to discard all 3 layers in the radio's TX FIFO.
fn flush_tx(&mut self) -> Result<(), Self::FifoErrorType> {
fn flush_tx(&mut self) -> Result<(), Self::Error> {
self.spi_read(0, commands::FLUSH_TX)
}

fn get_fifo_state(&mut self, about_tx: bool) -> Result<FifoState, Self::FifoErrorType> {
fn get_fifo_state(&mut self, about_tx: bool) -> Result<FifoState, Self::Error> {
self.spi_read(1, registers::FIFO_STATUS)?;
let offset = about_tx as u8 * 4;
let status = (self.buf[1] & (3 << offset)) >> offset;
match status {
0 => Ok(FifoState::Occupied),
1 => Ok(FifoState::Empty),
2 => Ok(FifoState::Full),
_ => Ok(FifoState::Occupied),
_ => Err(Nrf24Error::BinaryCorruption),
}
}
}
Expand All @@ -56,7 +55,7 @@ where
#[cfg(test)]
mod test {
extern crate std;
use super::{commands, registers, EsbFifo, FifoState};
use super::{commands, registers, EsbFifo, FifoState, Nrf24Error};
use crate::{spi_test_expects, test::mk_radio};
use embedded_hal_mock::eh1::spi::Transaction as SpiTransaction;
use std::vec;
Expand Down Expand Up @@ -102,17 +101,19 @@ mod test {
pub fn get_fifo_state() {
let spi_expectations = spi_test_expects![
// read FIFO register value with empty TX FIFO_STATUS
(vec![registers::FIFO_STATUS, 0u8], vec![0xEu8, 0x10u8]),
(vec![registers::FIFO_STATUS, 0], vec![0xEu8, 0x10]),
// read FIFO register value with full TX FIFO_STATUS
(vec![registers::FIFO_STATUS, 0x10u8], vec![0xEu8, 0x20u8]),
(vec![registers::FIFO_STATUS, 0x10], vec![0xEu8, 0x20]),
// read FIFO register value with occupied TX FIFO_STATUS
(vec![registers::FIFO_STATUS, 0x20u8], vec![0xEu8, 0u8]),
(vec![registers::FIFO_STATUS, 0x20], vec![0xEu8, 0]),
// read FIFO register value with empty RX FIFO_STATUS
(vec![registers::FIFO_STATUS, 0u8], vec![0xEu8, 1u8]),
(vec![registers::FIFO_STATUS, 0], vec![0xEu8, 1]),
// read FIFO register value with full RX FIFO_STATUS
(vec![registers::FIFO_STATUS, 1u8], vec![0xEu8, 2u8]),
(vec![registers::FIFO_STATUS, 1], vec![0xEu8, 2]),
// read FIFO register value with occupied RX FIFO_STATUS
(vec![registers::FIFO_STATUS, 2u8], vec![0xEu8, 0u8]),
(vec![registers::FIFO_STATUS, 2], vec![0xEu8, 0]),
// read FIFO register value with binary corruption
(vec![registers::FIFO_STATUS, 0], vec![0xEu8, 3]),
];
let mocks = mk_radio(&[], &spi_expectations);
let (mut radio, mut spi, mut ce_pin) = (mocks.0, mocks.1, mocks.2);
Expand All @@ -122,6 +123,10 @@ mod test {
assert_eq!(radio.get_fifo_state(false), Ok(FifoState::Empty));
assert_eq!(radio.get_fifo_state(false), Ok(FifoState::Full));
assert_eq!(radio.get_fifo_state(false), Ok(FifoState::Occupied));
assert_eq!(
radio.get_fifo_state(false),
Err(Nrf24Error::BinaryCorruption)
);
spi.done();
ce_pin.done();
}
Expand Down
6 changes: 2 additions & 4 deletions crates/rf24-rs/src/radio/rf24/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ where
DO: OutputPin,
DELAY: DelayNs,
{
type ConfigErrorType = Nrf24Error<SPI::Error, DO::Error>;

/// Initialize the radio's hardware using the [`SpiDevice`] and [`OutputPin`] given
/// to [`RF24::new()`].
fn init(&mut self) -> Result<(), Self::ConfigErrorType> {
fn init(&mut self) -> Result<(), Self::Error> {
// Must allow the radio time to settle else configuration bits will not necessarily stick.
// This is actually only required following power up but some settling time also appears to
// be required after resets too. For full coverage, we'll always assume the worst.
Expand Down Expand Up @@ -49,7 +47,7 @@ where
self.with_config(&RadioConfig::default())
}

fn with_config(&mut self, config: &RadioConfig) -> Result<(), Self::ConfigErrorType> {
fn with_config(&mut self, config: &RadioConfig) -> Result<(), Self::Error> {
self.clear_status_flags(StatusFlags::new())?;
self.power_down()?;

Expand Down
Loading
Loading