nrf24l01_commands/
lib.rs

1//! <div class="warning">
2//! <strong>Requires Rust Nightly</strong>
3//! </div>
4//!
5#![no_std]
6#![allow(incomplete_features)]
7#![feature(generic_const_exprs)]
8#![feature(const_trait_impl)]
9#![doc = include_str!("../README.md")]
10
11pub mod commands;
12pub mod fields;
13pub mod registers;
14
15#[cfg(test)]
16mod tests {
17    use super::{commands, fields, registers, registers::AddressRegister};
18
19    #[test]
20    fn test_reg_config() {
21        // Check default
22        let reg = registers::Config::new();
23        assert_eq!(reg.into_bits(), 0b0000_1000);
24        // Check fields
25        let reg = reg
26            .with_crco(fields::Crco::TwoByte)
27            .with_en_crc(false)
28            .with_mask_max_rt(true)
29            .with_mask_tx_ds(true)
30            .with_mask_rx_dr(true);
31        assert_eq!(reg.into_bits(), 0b0111_0100);
32        // Check read command
33        let read_reg = commands::RRegister::<registers::Config>::bytes();
34        assert_eq!(read_reg, [0 | 0x00, 0]);
35        // Check write command
36        let write_reg = commands::WRegister(reg).bytes();
37        assert_eq!(write_reg, [0b0010_0000 | 0x00, 0b0111_0100]);
38    }
39
40    #[test]
41    fn test_reg_rf_ch() {
42        // Check default
43        let reg = registers::RfCh::new();
44        assert_eq!(reg.into_bits(), 0b0000_0010);
45        // Check fields
46        let reg = reg.with_rf_ch(48);
47        assert_eq!(reg.into_bits(), 48);
48        // Check read command
49        let read_reg = commands::RRegister::<registers::RfCh>::bytes();
50        assert_eq!(read_reg, [0 | 0x05, 0]);
51        // Check write command
52        let write_reg = commands::WRegister(reg).bytes();
53        assert_eq!(write_reg, [0b0010_0000 | 0x05, 48]);
54    }
55
56    #[test]
57    fn test_reg_status() {
58        // Check default
59        let reg = registers::Status::new();
60        assert_eq!(reg.into_bits(), 0);
61        // Check fields
62        let mut reg = registers::Status::from_bits(0b0010_0110);
63        assert!(!reg.tx_full());
64        assert_eq!(reg.rx_p_no(), fields::RxPipeNo::Pipe3);
65        assert!(!reg.max_rt());
66        assert!(reg.tx_ds());
67        assert!(!reg.rx_dr());
68        // Set field
69        reg.set_max_rt(true);
70        assert_eq!(reg.into_bits(), 0b0011_0110);
71        // Check read command
72        let read_reg = commands::RRegister::<registers::Status>::bytes();
73        assert_eq!(read_reg, [0 | 0x07, 0]);
74        // Check write command
75        let write_reg = commands::WRegister(reg).bytes();
76        assert_eq!(write_reg, [0b0010_0000 | 0x07, 0b0011_0110]);
77    }
78
79    #[test]
80    fn test_reg_cd() {
81        // Check default
82        let reg = registers::Rpd::new();
83        assert_eq!(reg.into_bits(), 0);
84        // Check fields
85        let reg = registers::Rpd::from_bits(1);
86        assert_eq!(reg.into_bits(), 1);
87        // Check read command
88        let read_reg = commands::RRegister::<registers::Rpd>::bytes();
89        assert_eq!(read_reg, [0 | 0x09, 0]);
90    }
91
92    #[test]
93    fn test_reg_rx_addr_p0() {
94        // Check default
95        let reg = registers::RxAddrP0::<3>::new();
96        assert_eq!(reg.into_bits(), 0xE7E7E7E7E7);
97        // Check fields
98        let reg = registers::RxAddrP0::<4>::new().with_rx_addr_p0(0x17E73A6C58);
99        assert_eq!(reg.into_bits(), 0x17E73A6C58);
100        // Check read command
101        let read_reg = commands::RRegister::<registers::RxAddrP0<5>>::bytes();
102        assert_eq!(read_reg, [0 | 0x0A, 0, 0, 0, 0, 0]);
103        // Check write command
104        let write_reg = commands::WRegister(reg).bytes();
105        assert_eq!(write_reg, [0b0010_0000 | 0x0A, 0x58, 0x6C, 0x3A, 0xE7]);
106    }
107
108    #[test]
109    fn test_reg_tx_addr() {
110        // Check default
111        let reg = registers::TxAddr::<5>::new();
112        assert_eq!(reg.into_bits(), 0xE7E7E7E7E7);
113        // Check fields
114        let reg = registers::TxAddr::<5>::new().with_tx_addr(0xA2891FFF6A);
115        assert_eq!(reg.into_bits(), 0xA2891FFF6A);
116        // Check read command
117        let read_reg = commands::RRegister::<registers::TxAddr<4>>::bytes();
118        assert_eq!(read_reg, [0 | 0x10, 0, 0, 0, 0]);
119        // Check write command
120        let write_reg = commands::WRegister(reg).bytes();
121        assert_eq!(
122            write_reg,
123            [0b0010_0000 | 0x10, 0x6A, 0xFF, 0x1F, 0x89, 0xA2]
124        );
125    }
126
127    #[test]
128    fn test_reg_rx_pw_p0() {
129        // Check default
130        let reg = registers::RxPwP0::new();
131        assert_eq!(reg.into_bits(), 0);
132        // Check fields
133        let reg = reg.with_rx_pw_p0(32);
134        assert_eq!(reg.into_bits(), 32);
135        // Check read command
136        let read_reg = commands::RRegister::<registers::RxPwP0>::bytes();
137        assert_eq!(read_reg, [0 | 0x11, 0]);
138        // Check write command
139        let write_reg = commands::WRegister(reg).bytes();
140        assert_eq!(write_reg, [0b0010_0000 | 0x11, 32]);
141    }
142
143    #[test]
144    fn test_reg_fifo_status() {
145        // Check default
146        let reg = registers::FifoStatus::new();
147        assert_eq!(reg.into_bits(), 0);
148        // Check fields
149        let reg = registers::FifoStatus::from_bits(0b0100_0001);
150        assert!(reg.rx_empty());
151        assert!(!reg.rx_full());
152        assert!(!reg.tx_empty());
153        assert!(!reg.tx_full());
154        assert!(reg.tx_reuse());
155        // Check read command
156        let read_reg = commands::RRegister::<registers::FifoStatus>::bytes();
157        assert_eq!(read_reg, [0 | 0x17, 0]);
158    }
159
160    #[test]
161    fn test_reg_feature() {
162        // Check default
163        let reg = registers::Feature::new();
164        assert_eq!(reg.into_bits(), 0);
165        // Check fields
166        let reg = reg
167            .with_en_dyn_ack(true)
168            .with_en_ack_pay(true)
169            .with_en_dpl(false);
170        assert_eq!(reg.into_bits(), 0b0000_0011);
171        // Check read command
172        let read_reg = commands::RRegister::<registers::Feature>::bytes();
173        assert_eq!(read_reg, [0 | 0x1D, 0]);
174        // Check write command
175        let write_reg = commands::WRegister(reg).bytes();
176        assert_eq!(write_reg, [0b0010_0000 | 0x1D, 0b0000_0011]);
177    }
178
179    #[test]
180    fn test_read_rx_payload() {
181        let bytes = commands::RRxPayload::<32>::bytes();
182        let mut expected_bytes = [0; 33];
183        expected_bytes[0] = 0b0110_0001;
184        assert_eq!(bytes, expected_bytes);
185    }
186
187    #[test]
188    fn test_cmd_write_tx_payload_no_ack() {
189        let bytes = commands::WTxPayloadNoack([b'H', b'e', b'l', b'l', b'o']).bytes();
190        assert_eq!(bytes, [0b1011_0000, b'H', b'e', b'l', b'l', b'o']);
191    }
192}