1use crate::register_acces::Register;
18use crate::MAX_PAYLOAD_SIZE;
19
20const MAX_CHANNEL: u8 = 125;
21
22#[derive(Copy, Clone)]
55#[cfg_attr(feature = "defmt", derive(defmt::Format))]
56pub struct NrfConfig {
57 pub(crate) payload_size: PayloadSize,
58 pub(crate) channel: u8,
59 pub(crate) addr_width: AddressWidth,
60 pub(crate) data_rate: DataRate,
61 pub(crate) pa_level: PALevel,
62 pub(crate) crc_encoding_scheme: EncodingScheme,
63 pub(crate) ack_payloads_enabled: bool,
64 pub(crate) auto_retry: AutoRetransmission,
65}
66
67impl NrfConfig {
68 pub fn payload_size<T: Into<PayloadSize>>(mut self, payload_size: T) -> Self {
72 self.payload_size = payload_size.into();
73 self
74 }
75 pub fn channel(mut self, channel: u8) -> Self {
78 self.channel = core::cmp::min(channel, MAX_CHANNEL);
79 self
80 }
81 pub fn addr_width<T: Into<AddressWidth>>(mut self, addr_width: T) -> Self {
84 self.addr_width = addr_width.into();
85 self
86 }
87 pub fn data_rate(mut self, data_rate: DataRate) -> Self {
89 self.data_rate = data_rate;
90 self
91 }
92 pub fn pa_level(mut self, pa_level: PALevel) -> Self {
94 self.pa_level = pa_level;
95 self
96 }
97 pub fn crc_encoding_scheme(mut self, crc_encoding_scheme: EncodingScheme) -> Self {
99 self.crc_encoding_scheme = crc_encoding_scheme;
100 self
101 }
102 pub fn ack_payloads_enabled(mut self, ack_payloads_enabled: bool) -> Self {
104 self.ack_payloads_enabled = ack_payloads_enabled;
105 self
106 }
107 pub fn auto_retry<T: Into<AutoRetransmission>>(mut self, auto_retry: T) -> Self {
109 self.auto_retry = auto_retry.into();
110 self
111 }
112}
113
114impl Default for NrfConfig {
115 fn default() -> Self {
116 Self {
117 channel: 76,
118 payload_size: PayloadSize::default(),
119 addr_width: AddressWidth::default(),
120 crc_encoding_scheme: EncodingScheme::default(),
121 pa_level: PALevel::default(),
122 data_rate: DataRate::default(),
123 ack_payloads_enabled: false,
124 auto_retry: AutoRetransmission::default(),
125 }
126 }
127}
128
129#[derive(Copy, Clone)]
134pub enum PALevel {
135 Min = 0b0000_0000,
137 Low = 0b0000_0010,
139 High = 0b0000_0100,
141 Max = 0b0000_0110,
143}
144
145impl PALevel {
146 pub(crate) fn bitmask() -> u8 {
147 0b0000_0110
148 }
149 pub(crate) fn level(&self) -> u8 {
150 *self as u8
151 }
152}
153
154impl Default for PALevel {
155 fn default() -> Self {
156 PALevel::Min
157 }
158}
159
160impl From<u8> for PALevel {
161 fn from(t: u8) -> Self {
162 match t & Self::bitmask() {
163 0b0000_0000 => Self::Min,
164 0b0000_0010 => Self::Low,
165 0b0000_0100 => Self::High,
166 0b0000_0110 => Self::Max,
167 _ => unreachable!(),
168 }
169 }
170}
171
172#[cfg(feature = "defmt")]
173impl defmt::Format for PALevel {
174 fn format(&self, fmt: defmt::Formatter) {
175 match *self {
176 PALevel::Min => defmt::write!(fmt, "min (-18 dBm)"),
177 PALevel::Low => defmt::write!(fmt, "low (-12 dBm)"),
178 PALevel::High => defmt::write!(fmt, "high (-6 dBm)"),
179 PALevel::Max => defmt::write!(fmt, "max (0 dBm)"),
180 }
181 }
182}
183
184#[derive(PartialEq, Eq, Copy, Clone)]
186#[cfg_attr(feature = "defmt", derive(defmt::Format))]
187pub enum PayloadSize {
188 Dynamic,
190 Static(u8),
192}
193
194impl PayloadSize {
195 pub(crate) fn truncate(self) -> Self {
197 match self {
198 Self::Dynamic => Self::Dynamic,
199 Self::Static(n) => Self::Static(core::cmp::min(n, MAX_PAYLOAD_SIZE)),
200 }
201 }
202}
203
204impl Default for PayloadSize {
205 fn default() -> Self {
206 Self::Static(MAX_PAYLOAD_SIZE)
207 }
208}
209
210impl From<u8> for PayloadSize {
211 fn from(size: u8) -> Self {
212 match size {
213 0 => Self::Dynamic,
214 n => Self::Static(core::cmp::min(n, MAX_PAYLOAD_SIZE)),
215 }
216 }
217}
218
219#[derive(Copy, Clone)]
223#[cfg_attr(feature = "defmt", derive(defmt::Format))]
224pub enum DataRate {
225 R1Mbps = 0b0000_0000,
227 R2Mbps = 0b0000_0001,
229}
230
231impl DataRate {
232 pub(crate) fn bitmask() -> u8 {
233 0b0000_1000
234 }
235 pub(crate) fn rate(&self) -> u8 {
236 *self as u8
237 }
238}
239
240impl Default for DataRate {
241 fn default() -> Self {
242 DataRate::R1Mbps
243 }
244}
245
246impl From<u8> for DataRate {
247 fn from(t: u8) -> Self {
248 match t & Self::bitmask() {
249 0b0000_0000 => Self::R1Mbps,
250 0b0000_1000 => Self::R2Mbps,
251 _ => unreachable!(),
252 }
253 }
254}
255
256#[derive(Copy, Clone)]
258#[cfg_attr(feature = "defmt", derive(defmt::Format))]
259pub enum EncodingScheme {
260 NoRedundancyCheck = 0b0000_0000,
262 R1Byte = 0b0000_1000,
264 R2Bytes = 0b0000_1100,
266}
267
268impl Default for EncodingScheme {
269 fn default() -> Self {
270 Self::R2Bytes
271 }
272}
273
274impl EncodingScheme {
275 pub(crate) fn bitmask() -> u8 {
276 0b0000_1100
277 }
278
279 pub(crate) fn scheme(&self) -> u8 {
280 *self as u8
281 }
282}
283
284impl From<u8> for EncodingScheme {
285 fn from(t: u8) -> Self {
286 match t & Self::bitmask() {
287 0b0000_0000 => Self::NoRedundancyCheck,
288 0b0000_1000 => Self::R1Byte,
289 0b0000_1100 => Self::R2Bytes,
290 _ => unreachable!(),
291 }
292 }
293}
294#[derive(Copy, Clone)]
297pub enum AddressWidth {
298 R3Bytes = 1,
300 R4Bytes = 2,
302 R5Bytes = 3,
304}
305
306impl AddressWidth {
307 pub(crate) fn value(&self) -> u8 {
308 *self as u8
309 }
310 pub(crate) fn from_register(t: u8) -> Self {
311 match t & 0b11 {
312 0b01 => Self::R3Bytes,
313 0b10 => Self::R4Bytes,
314 0b11 => Self::R5Bytes,
315 _ => unreachable!(),
316 }
317 }
318}
319impl Default for AddressWidth {
320 fn default() -> Self {
321 Self::R5Bytes
322 }
323}
324
325impl From<u8> for AddressWidth {
326 fn from(t: u8) -> Self {
328 match t {
329 0..=3 => Self::R3Bytes,
330 4 => Self::R4Bytes,
331 5..=u8::MAX => Self::R5Bytes,
332 }
333 }
334}
335
336#[cfg(feature = "defmt")]
337impl defmt::Format for AddressWidth {
338 fn format(&self, fmt: defmt::Formatter) {
339 match *self {
340 Self::R3Bytes => defmt::write!(fmt, "3 bytes"),
341 Self::R4Bytes => defmt::write!(fmt, "4 bytes"),
342 Self::R5Bytes => defmt::write!(fmt, "5 bytes"),
343 }
344 }
345}
346
347#[derive(Copy, Clone)]
358pub struct AutoRetransmission {
359 delay: u8,
360 count: u8,
361}
362
363impl Default for AutoRetransmission {
364 fn default() -> Self {
365 Self {
366 delay: 5,
367 count: 15,
368 }
369 }
370}
371
372impl AutoRetransmission {
373 pub(crate) fn from_register(reg: u8) -> Self {
374 Self {
375 delay: reg >> 4,
376 count: reg & 0b0000_1111,
377 }
378 }
379 pub fn raw_delay(&self) -> u8 {
384 self.delay
385 }
386
387 pub fn delay(&self) -> u32 {
389 ((self.delay as u32 + 1) * 250) + 86
390 }
391 pub fn count(&self) -> u8 {
394 self.count
395 }
396}
397
398impl From<(u8, u8)> for AutoRetransmission {
399 fn from((d, c): (u8, u8)) -> Self {
400 Self {
401 delay: core::cmp::min(d, 15),
402 count: core::cmp::min(c, 15),
403 }
404 }
405}
406
407#[cfg(feature = "defmt")]
408impl defmt::Format for AutoRetransmission {
409 fn format(&self, fmt: defmt::Formatter) {
410 defmt::write!(
411 fmt,
412 "AutoRetransmission {{ raw_delay: {=u8}, delay_µs: {=u32}, count: {=u8} }}",
413 &self.raw_delay(),
414 &self.delay(),
415 &self.count(),
416 )
417 }
418}
419
420#[derive(Copy, Clone)]
439#[repr(u8)]
440#[cfg_attr(feature = "defmt", derive(defmt::Format))]
441pub enum DataPipe {
442 DP0 = 0,
447 DP1 = 1,
449 DP2 = 2,
451 DP3 = 3,
453 DP4 = 4,
455 DP5 = 5,
457}
458
459impl DataPipe {
460 pub(crate) fn pipe(&self) -> u8 {
461 *self as u8
462 }
463}
464
465impl Default for DataPipe {
466 fn default() -> Self {
467 DataPipe::DP0
468 }
469}
470
471impl From<u8> for DataPipe {
472 fn from(t: u8) -> Self {
473 match t {
474 0 => DataPipe::DP0,
475 1 => DataPipe::DP1,
476 2 => DataPipe::DP2,
477 3 => DataPipe::DP3,
478 4 => DataPipe::DP4,
479 5 => DataPipe::DP5,
480 _ => DataPipe::DP0,
481 }
482 }
483}
484
485impl Into<Register> for DataPipe {
486 fn into(self) -> Register {
487 match self {
488 DataPipe::DP0 => Register::RX_ADDR_P0,
489 DataPipe::DP1 => Register::RX_ADDR_P1,
490 DataPipe::DP2 => Register::RX_ADDR_P2,
491 DataPipe::DP3 => Register::RX_ADDR_P3,
492 DataPipe::DP4 => Register::RX_ADDR_P4,
493 DataPipe::DP5 => Register::RX_ADDR_P5,
494 }
495 }
496}