nrf24l01_commands/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//! <div class="warning">
//! <strong>Requires Rust Nightly</strong>
//! </div>
//!
#![no_std]
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]
#![doc = include_str!("../README.md")]

pub mod commands;
pub mod registers;

#[cfg(test)]
mod tests {
    use super::{commands, registers};

    #[test]
    fn test_reg_config() {
        // Check default
        let reg = registers::Config::new();
        assert_eq!(reg.into_bits(), 0b0000_1000);
        // Check fields
        let reg = reg
            .with_crco(true)
            .with_en_crc(false)
            .with_mask_max_rt(true)
            .with_mask_tx_ds(true)
            .with_mask_rx_dr(true);
        assert_eq!(reg.into_bits(), 0b0111_0100);
        // Check read command
        let read_reg = commands::RRegister::<registers::Config>::bytes();
        assert_eq!(read_reg, [0 | 0x00, 0]);
        // Check write command
        let write_reg = commands::WRegister(reg).bytes();
        assert_eq!(write_reg, [0b0010_0000 | 0x00, 0b0111_0100]);
    }

    #[test]
    fn test_reg_rf_ch() {
        // Check default
        let reg = registers::RfCh::new();
        assert_eq!(reg.into_bits(), 0b0000_0010);
        // Check fields
        let reg = reg.with_rf_ch(48);
        assert_eq!(reg.into_bits(), 48);
        // Check read command
        let read_reg = commands::RRegister::<registers::RfCh>::bytes();
        assert_eq!(read_reg, [0 | 0x05, 0]);
        // Check write command
        let write_reg = commands::WRegister(reg).bytes();
        assert_eq!(write_reg, [0b0010_0000 | 0x05, 48]);
    }

    #[test]
    fn test_reg_status() {
        // Check default
        let reg = registers::Status::new();
        assert_eq!(reg.into_bits(), 0);
        // Check fields
        let mut reg = registers::Status::from_bits(0b0010_0110);
        assert!(!reg.tx_full());
        assert_eq!(reg.rx_p_no(), 0b011);
        assert!(!reg.max_rt());
        assert!(reg.tx_ds());
        assert!(!reg.rx_dr());
        // Set field
        reg.set_max_rt(true);
        assert_eq!(reg.into_bits(), 0b0011_0110);
        // Check read command
        let read_reg = commands::RRegister::<registers::Status>::bytes();
        assert_eq!(read_reg, [0 | 0x07, 0]);
        // Check write command
        let write_reg = commands::WRegister(reg).bytes();
        assert_eq!(write_reg, [0b0010_0000 | 0x07, 0b0011_0110]);
    }

    #[test]
    fn test_reg_cd() {
        // Check default
        let reg = registers::Cd::new();
        assert_eq!(reg.into_bits(), 0);
        // Check fields
        let reg = registers::Cd::from_bits(1);
        assert_eq!(reg.into_bits(), 1);
        // Check read command
        let read_reg = commands::RRegister::<registers::Cd>::bytes();
        assert_eq!(read_reg, [0 | 0x09, 0]);
    }

    #[test]
    fn test_reg_rx_addr_p0() {
        // Check default
        let reg = registers::RxAddrP0::<3>::new();
        assert_eq!(reg.into_bits(), 0xE7E7E7E7E7);
        // Check fields
        let reg = registers::RxAddrP0::<4>::new().with_rx_addr_p0(0x17E73A6C58);
        assert_eq!(reg.into_bits(), 0x17E73A6C58);
        // Check read command
        let read_reg = commands::RRegister::<registers::RxAddrP0<5>>::bytes();
        assert_eq!(read_reg, [0 | 0x0A, 0, 0, 0, 0, 0]);
        // Check write command
        let write_reg = commands::WRegister(reg).bytes();
        assert_eq!(write_reg, [0b0010_0000 | 0x0A, 0x58, 0x6C, 0x3A, 0xE7]);
    }

    #[test]
    fn test_reg_tx_addr() {
        // Check default
        let reg = registers::TxAddr::<5>::new();
        assert_eq!(reg.into_bits(), 0xE7E7E7E7E7);
        // Check fields
        let reg = registers::TxAddr::<5>::new().with_tx_addr(0xA2891FFF6A);
        assert_eq!(reg.into_bits(), 0xA2891FFF6A);
        // Check read command
        let read_reg = commands::RRegister::<registers::TxAddr<4>>::bytes();
        assert_eq!(read_reg, [0 | 0x10, 0, 0, 0, 0]);
        // Check write command
        let write_reg = commands::WRegister(reg).bytes();
        assert_eq!(
            write_reg,
            [0b0010_0000 | 0x10, 0x6A, 0xFF, 0x1F, 0x89, 0xA2]
        );
    }

    #[test]
    fn test_reg_rx_pw_p0() {
        // Check default
        let reg = registers::RxPwP0::new();
        assert_eq!(reg.into_bits(), 0);
        // Check fields
        let reg = reg.with_rx_pw_p0(32);
        assert_eq!(reg.into_bits(), 32);
        // Check read command
        let read_reg = commands::RRegister::<registers::RxPwP0>::bytes();
        assert_eq!(read_reg, [0 | 0x11, 0]);
        // Check write command
        let write_reg = commands::WRegister(reg).bytes();
        assert_eq!(write_reg, [0b0010_0000 | 0x11, 32]);
    }

    #[test]
    fn test_reg_fifo_status() {
        // Check default
        let reg = registers::FifoStatus::new();
        assert_eq!(reg.into_bits(), 0);
        // Check fields
        let reg = registers::FifoStatus::from_bits(0b0100_0001);
        assert!(reg.rx_empty());
        assert!(!reg.rx_full());
        assert!(!reg.tx_empty());
        assert!(!reg.tx_full());
        assert!(reg.tx_reuse());
        // Check read command
        let read_reg = commands::RRegister::<registers::FifoStatus>::bytes();
        assert_eq!(read_reg, [0 | 0x17, 0]);
    }

    #[test]
    fn test_reg_feature() {
        // Check default
        let reg = registers::Feature::new();
        assert_eq!(reg.into_bits(), 0);
        // Check fields
        let reg = reg
            .with_en_dyn_ack(true)
            .with_en_ack_pay(true)
            .with_en_dpl(false);
        assert_eq!(reg.into_bits(), 0b0000_0011);
        // Check read command
        let read_reg = commands::RRegister::<registers::Feature>::bytes();
        assert_eq!(read_reg, [0 | 0x1D, 0]);
        // Check write command
        let write_reg = commands::WRegister(reg).bytes();
        assert_eq!(write_reg, [0b0010_0000 | 0x1D, 0b0000_0011]);
    }

    #[test]
    fn test_read_rx_payload() {
        let bytes = commands::RRxPayload::<32>::bytes();
        let mut expected_bytes = [0; 33];
        expected_bytes[0] = 0b0110_0001;
        assert_eq!(bytes, expected_bytes);
    }

    #[test]
    fn test_cmd_write_tx_payload_no_ack() {
        let bytes = commands::WTxPayloadNoack([b'H', b'e', b'l', b'l', b'o']).bytes();
        assert_eq!(bytes, [0b1011_0000, b'o', b'l', b'l', b'e', b'H']);
    }
}