nrf24l01_commands/
fields.rs

1//! Enums for certain nRF24L01+ register fields.
2
3/// A trait for certain multi-bit register fields that are represented as enums.
4#[const_trait]
5pub trait EnumField {
6    /// Convert the field to its bits representation.
7    fn into_bits(self) -> u8;
8    /// Convert bits to the field.
9    fn from_bits(bits: u8) -> Self;
10}
11
12/// CRC encoding scheme.
13#[derive(Copy, Clone, Debug, PartialEq, Eq)]
14#[repr(u8)]
15pub enum Crco {
16    /// 1 byte CRC
17    OneByte = 0,
18    /// 2 byte CRC
19    TwoByte = 1,
20}
21impl const EnumField for Crco {
22    fn into_bits(self) -> u8 {
23        self as _
24    }
25    fn from_bits(bits: u8) -> Self {
26        // SAFETY: The result is guaranteed to be in the range of 0-1
27        unsafe { core::mem::transmute(bits & 1) }
28    }
29}
30
31/// RX/TX address field width in bytes.
32/// LSByte is used if address width is below 5 bytes.
33#[derive(Copy, Clone, Debug, PartialEq, Eq)]
34#[repr(u8)]
35pub enum AddressWidth {
36    Illegal = 0,
37    ThreeByte = 0b01,
38    FourByte = 0b10,
39    FiveByte = 0b11,
40}
41impl const EnumField for AddressWidth {
42    fn into_bits(self) -> u8 {
43        self as _
44    }
45    fn from_bits(bits: u8) -> Self {
46        // SAFETY: The result is guaranteed to be in the range of 0-3
47        unsafe { core::mem::transmute(bits & 0b11) }
48    }
49}
50
51/// Auto retransmit delay.
52///
53/// `0000`: Wait 250µS
54///
55/// `0001`: Wait 500µS
56///
57/// `0010`: Wait 750µS
58///
59/// ……
60///
61/// `1111`: Wait 4000µS
62#[derive(Copy, Clone, Debug, PartialEq, Eq)]
63#[repr(u8)]
64pub enum AutoRetransmitDelay {
65    US250 = 0b0000,
66    US500 = 0b0001,
67    US750 = 0b0010,
68    US1000 = 0b0011,
69    US1250 = 0b0100,
70    US1500 = 0b0101,
71    US1750 = 0b0110,
72    US2000 = 0b0111,
73    US2250 = 0b1000,
74    US2500 = 0b1001,
75    US2750 = 0b1010,
76    US3000 = 0b1011,
77    US3250 = 0b1100,
78    US3500 = 0b1101,
79    US3750 = 0b1110,
80    US4000 = 0b1111,
81}
82impl const EnumField for AutoRetransmitDelay {
83    fn into_bits(self) -> u8 {
84        self as _
85    }
86    fn from_bits(bits: u8) -> Self {
87        // SAFETY: The result is guaranteed to be in the range of 0-15
88        unsafe { core::mem::transmute(bits & 0b1111) }
89    }
90}
91
92/// High speed data rate.
93#[derive(Copy, Clone, Debug, PartialEq, Eq)]
94#[repr(u8)]
95pub enum RfDrHigh {
96    Mbps1 = 0,
97    Mbps2 = 1,
98}
99impl const EnumField for RfDrHigh {
100    fn into_bits(self) -> u8 {
101        self as _
102    }
103    fn from_bits(bits: u8) -> Self {
104        // SAFETY: The result is guaranteed to be in the range of 0-1
105        unsafe { core::mem::transmute(bits & 1) }
106    }
107}
108
109/// Set RF output power in TX mode.
110#[derive(Copy, Clone, Debug, PartialEq, Eq)]
111#[repr(u8)]
112pub enum RfPower {
113    /// -18 dBm
114    Neg18Dbm = 0b00,
115    /// -12 dBm
116    Neg12Dbm = 0b01,
117    /// -6 dBm
118    Neg6Dbm = 0b10,
119    /// 0 dBm
120    Dbm0 = 0b11,
121}
122impl const EnumField for RfPower {
123    fn into_bits(self) -> u8 {
124        self as _
125    }
126    fn from_bits(bits: u8) -> Self {
127        // SAFETY: The result is guaranteed to be in the range of 0-3
128        unsafe { core::mem::transmute(bits & 0b11) }
129    }
130}
131
132/// Data pipe number for the payload available from reading RX FIFO.
133#[derive(Copy, Clone, Debug, PartialEq, Eq)]
134#[repr(u8)]
135pub enum RxPipeNo {
136    Pipe0 = 0,
137    Pipe1 = 1,
138    Pipe2 = 2,
139    Pipe3 = 3,
140    Pipe4 = 4,
141    Pipe5 = 5,
142    NotUsed = 0b110,
143    RxFifoEmpty = 0b111,
144}
145impl const EnumField for RxPipeNo {
146    fn into_bits(self) -> u8 {
147        self as _
148    }
149    fn from_bits(bits: u8) -> Self {
150        // SAFETY: The result is guaranteed to be in the range of 0-7
151        unsafe { core::mem::transmute(bits & 0b111) }
152    }
153}