rf24/radio/rf24/
status.rs1use embedded_hal::{delay::DelayNs, digital::OutputPin, spi::SpiDevice};
2
3use crate::{
4 radio::{prelude::EsbStatus, Nrf24Error, RF24},
5 types::StatusFlags,
6};
7
8use super::{commands, registers, Config};
9
10impl<SPI, DO, DELAY> EsbStatus for RF24<SPI, DO, DELAY>
11where
12 SPI: SpiDevice,
13 DO: OutputPin,
14 DELAY: DelayNs,
15{
16 type StatusErrorType = Nrf24Error<SPI::Error, DO::Error>;
17
18 fn set_status_flags(&mut self, flags: StatusFlags) -> Result<(), Self::StatusErrorType> {
19 self.spi_read(1, registers::CONFIG)?;
20 self._config_reg =
21 Config::from_bits(self._buf[1] & 0x0F | (!flags.into_bits() & StatusFlags::IRQ_MASK));
22 self.spi_write_byte(registers::CONFIG, self._config_reg.into_bits())
23 }
24
25 fn clear_status_flags(&mut self, flags: StatusFlags) -> Result<(), Self::StatusErrorType> {
26 self.spi_write_byte(registers::STATUS, flags.into_bits() & StatusFlags::IRQ_MASK)
27 }
28
29 fn update(&mut self) -> Result<(), Self::StatusErrorType> {
30 self.spi_read(0, commands::NOP)
31 }
32
33 fn get_status_flags(&self, flags: &mut StatusFlags) {
34 *flags = self._status;
35 }
36}
37
38#[cfg(test)]
41mod test {
42 extern crate std;
43 use crate::radio::prelude::EsbStatus;
44 use crate::radio::rf24::commands;
45 use crate::spi_test_expects;
46 use crate::types::StatusFlags;
47
48 use super::{registers, RF24};
49 use embedded_hal_mock::eh1::delay::NoopDelay;
50 use embedded_hal_mock::eh1::digital::Mock as PinMock;
51 use embedded_hal_mock::eh1::spi::{Mock as SpiMock, Transaction as SpiTransaction};
52 use std::vec;
53
54 #[test]
55 pub fn what_happened() {
56 let pin_expectations = [];
58 let mut pin_mock = PinMock::new(&pin_expectations);
59
60 let delay_mock = NoopDelay::new();
62
63 let spi_expectations = spi_test_expects![
64 (vec![commands::NOP], vec![0x70u8]),
66 ];
67 let mut spi_mock = SpiMock::new(&spi_expectations);
68 let mut radio = RF24::new(pin_mock.clone(), spi_mock.clone(), delay_mock);
69 radio.update().unwrap();
70 let mut flags = StatusFlags::default();
71 radio.get_status_flags(&mut flags);
72 assert!(flags.rx_dr());
73 assert!(flags.tx_ds());
74 assert!(flags.tx_df());
75 spi_mock.done();
76 pin_mock.done();
77 }
78
79 #[test]
80 pub fn set_status_flags() {
81 let pin_expectations = [];
83 let mut pin_mock = PinMock::new(&pin_expectations);
84
85 let delay_mock = NoopDelay::new();
87
88 let spi_expectations = spi_test_expects![
89 (vec![registers::CONFIG, 0u8], vec![0xEu8, 0xFu8]),
91 (
93 vec![registers::CONFIG | commands::W_REGISTER, 0x7Fu8],
94 vec![0xEu8, 0u8],
95 ),
96 ];
97 let mut spi_mock = SpiMock::new(&spi_expectations);
98 let mut radio = RF24::new(pin_mock.clone(), spi_mock.clone(), delay_mock);
99 radio.set_status_flags(StatusFlags::default()).unwrap();
100 spi_mock.done();
101 pin_mock.done();
102 }
103}