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