sx1262 0.3.0

A embedded-hal driver for the Semtech SX1261/2 sub-GHz radio transceiver
Documentation
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//! Status commands
//!
//! This module contains commands for monitoring device status and performance:
//! - Device operating mode and command status
//! - Signal strength measurements
//! - Packet reception status
//! - Error detection and handling
//! - Communication statistics
//!
//! These commands can be used to monitor device operation and
//! diagnose issues during development and operation.

use core::convert::Infallible;

use regiface::FromByteArray;

use crate::{Command, NoParameters};

/// Error type for invalid operating mode values
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OperatingModeError {
    /// The value does not correspond to a valid operating mode
    InvalidValue(u8),
}

/// Error type for invalid command status values
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CommandStatusError {
    /// The value does not correspond to a valid command status
    InvalidValue(u8),
}

/// Error type for status byte parsing
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum StatusError {
    /// Error extracting operating mode from status byte
    InvalidMode(OperatingModeError),
    /// Error extracting command status from status byte
    InvalidCommandStatus(CommandStatusError),
}

/// Operating mode of the device
///
/// Represents the current state of the radio's state machine.
/// Extracted from status byte bits 6:4.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OperatingMode {
    /// STDBY_RC mode: RC oscillator running
    StandbyRc = 0x2,
    /// STDBY_XOSC mode: Crystal oscillator running
    StandbyXosc = 0x3,
    /// FS mode: Frequency synthesizer running
    FrequencySynthesizer = 0x4,
    /// RX mode: Receiving packets
    Receive = 0x5,
    /// TX mode: Transmitting packets
    Transmit = 0x6,
}

impl TryFrom<u8> for OperatingMode {
    type Error = OperatingModeError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0x2 => Ok(Self::StandbyRc),
            0x3 => Ok(Self::StandbyXosc),
            0x4 => Ok(Self::FrequencySynthesizer),
            0x5 => Ok(Self::Receive),
            0x6 => Ok(Self::Transmit),
            invalid => Err(OperatingModeError::InvalidValue(invalid)),
        }
    }
}

/// Command processing status
///
/// Indicates the result of the last command execution.
/// Extracted from status byte bits 3:1.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CommandStatus {
    /// Data is available to be read from the radio
    DataAvailable = 0x2,
    /// Command timed out during execution
    Timeout = 0x3,
    /// Error occurred during command processing
    ProcessingError = 0x4,
    /// Command execution failed
    ExecutionFailure = 0x5,
    /// TX operation completed successfully
    TxDone = 0x6,
}

impl TryFrom<u8> for CommandStatus {
    type Error = CommandStatusError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0x2 => Ok(Self::DataAvailable),
            0x3 => Ok(Self::Timeout),
            0x4 => Ok(Self::ProcessingError),
            0x5 => Ok(Self::ExecutionFailure),
            0x6 => Ok(Self::TxDone),
            invalid => Err(CommandStatusError::InvalidValue(invalid)),
        }
    }
}

/// Device status information
///
/// Contains the current operating mode and command processing status.
/// Returned by GetStatus command.
///
/// # Status Byte Format
/// - Bits 7: Reserved
/// - Bits 6:4: Operating mode
/// - Bits 3:1: Command status
/// - Bits 0: Reserved
#[derive(Debug, Clone, Copy)]
pub struct Status {
    /// Current operating mode of the device
    pub mode: OperatingMode,
    /// Status of the last command execution
    pub cmd_status: CommandStatus,
}

impl FromByteArray for Status {
    type Error = StatusError;
    type Array = [u8; 1];

    fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
        let mode = (bytes[0] >> 4) & 0x7;
        let cmd = (bytes[0] >> 1) & 0x7;

        Ok(Self {
            mode: OperatingMode::try_from(mode).map_err(StatusError::InvalidMode)?,
            cmd_status: CommandStatus::try_from(cmd).map_err(StatusError::InvalidCommandStatus)?,
        })
    }
}

/// GetStatus command (0xC0)
///
/// Returns the current device status including operating mode and command status.
///
/// # Important Notes
/// - Operating mode indicates current radio state
/// - Command status shows result of last command
/// - Use to monitor device state and detect errors
/// - Helpful for debugging communication issues
#[derive(Debug, Clone)]
pub struct GetStatus;

impl Command for GetStatus {
    type IdType = u8;
    type CommandParameters = NoParameters;
    type ResponseParameters = Status;

    fn id() -> Self::IdType {
        0xC0
    }

    fn invoking_parameters(self) -> Self::CommandParameters {
        NoParameters::default()
    }
}

/// GetRssiInst response
///
/// Contains the device status and instantaneous RSSI value.
#[derive(Debug, Clone, Copy)]
pub struct GetRssiInstResponse {
    /// Device status from the first response byte
    pub status: Status,
    /// Instantaneous RSSI value
    /// Signal power in dBm = -value/2
    pub rssi: u8,
}

impl FromByteArray for GetRssiInstResponse {
    type Error = Infallible;
    type Array = [u8; 2]; // 1 status byte + 1 RSSI byte

    fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
        Ok(Self {
            status: Status::from_bytes([bytes[0]]).unwrap(),
            rssi: bytes[1],
        })
    }
}

/// GetRssiInst command (0x15)
///
/// Returns instantaneous RSSI value during reception.
///
/// # RSSI Calculation
/// Signal power in dBm = -value/2
///
/// # Important Notes
/// - Only valid in RX mode
/// - Updates continuously during reception
/// - Accuracy typically ±2dB
#[derive(Debug, Clone)]
pub struct GetRssiInst;

impl Command for GetRssiInst {
    type IdType = u8;
    type CommandParameters = NoParameters;
    type ResponseParameters = GetRssiInstResponse;

    fn id() -> Self::IdType {
        0x15
    }

    fn invoking_parameters(self) -> Self::CommandParameters {
        NoParameters::default()
    }
}

/// RX buffer status response
///
/// Contains information about received packet in buffer.
#[derive(Debug, Clone, Copy)]
pub struct RxBufferStatus {
    /// Length of received payload in bytes
    pub payload_length: u8,

    /// Buffer pointer to first byte of payload
    /// Offset from RxBaseAddress
    pub buffer_pointer: u8,
}

impl FromByteArray for RxBufferStatus {
    type Error = Infallible;
    type Array = [u8; 2];

    fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
        Ok(Self {
            payload_length: bytes[0],
            buffer_pointer: bytes[1],
        })
    }
}

/// GetRxBufferStatus response
///
/// Contains the device status and RX buffer information.
#[derive(Debug, Clone, Copy)]
pub struct GetRxBufferStatusResponse {
    /// Device status from the first response byte
    pub status: Status,
    /// RX buffer information
    pub buffer_status: RxBufferStatus,
}

impl FromByteArray for GetRxBufferStatusResponse {
    type Error = Infallible;
    type Array = [u8; 3]; // 1 status byte + 2 buffer bytes

    fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
        Ok(Self {
            status: Status::from_bytes([bytes[0]]).unwrap(),
            buffer_status: RxBufferStatus::from_bytes([bytes[1], bytes[2]]).unwrap(),
        })
    }
}

/// GetRxBufferStatus command (0x13)
///
/// Returns status of received packet in buffer.
///
/// # Important Notes
/// - Valid after RxDone interrupt
/// - Returns payload length and start address
/// - Data remains valid until next RX operation
/// - Use with ReadBuffer to retrieve payload
#[derive(Debug, Clone)]
pub struct GetRxBufferStatus;

impl Command for GetRxBufferStatus {
    type IdType = u8;
    type CommandParameters = NoParameters;
    type ResponseParameters = GetRxBufferStatusResponse;

    fn id() -> Self::IdType {
        0x13
    }

    fn invoking_parameters(self) -> Self::CommandParameters {
        NoParameters::default()
    }
}

/// Packet status response
///
/// Contains status information about received packet.
/// Interpretation depends on packet type (LoRa/FSK).
#[derive(Debug, Clone, Copy)]
pub struct PacketStatus {
    /// Status bytes array:
    /// FSK Mode:
    /// - status[0]: RxStatus
    ///   - bit 7: Preamble error
    ///   - bit 6: Sync error
    ///   - bit 5: Addr error
    ///   - bit 4: CRC error
    ///   - bit 3: Length error
    ///   - bit 2: Abort error
    ///   - bit 1: Packet received
    ///   - bit 0: Packet sent
    /// - status[1]: RssiSync (-value/2 dBm)
    /// - status[2]: RssiAvg (-value/2 dBm)
    ///
    /// LoRa Mode:
    /// - status[0]: RssiPkt (-value/2 dBm)
    /// - status[1]: SnrPkt (value/4 dB)
    /// - status[2]: SignalRssiPkt (-value/2 dBm)
    pub status: [u8; 3],
}

impl FromByteArray for PacketStatus {
    type Error = Infallible;
    type Array = [u8; 3];

    fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
        Ok(Self { status: bytes })
    }
}

/// GetPacketStatus response
///
/// Contains the device status and packet status information.
#[derive(Debug, Clone, Copy)]
pub struct GetPacketStatusResponse {
    /// Device status from the first response byte
    pub status: Status,
    /// Packet status information
    pub packet_status: PacketStatus,
}

impl FromByteArray for GetPacketStatusResponse {
    type Error = Infallible;
    type Array = [u8; 4]; // 1 status byte + 3 packet status bytes

    fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
        Ok(Self {
            status: Status::from_bytes([bytes[0]]).unwrap(),
            packet_status: PacketStatus::from_bytes([bytes[1], bytes[2], bytes[3]]).unwrap(),
        })
    }
}

/// GetPacketStatus command (0x14)
///
/// Returns detailed status of received packet.
///
/// # Important Notes
/// - Valid after RxDone interrupt
/// - Status interpretation depends on packet type
/// - RSSI/SNR values latched at different times
/// - FSK: RssiSync at sync word, RssiAvg over payload
/// - LoRa: RssiPkt average over header+payload
#[derive(Debug, Clone)]
pub struct GetPacketStatus;

impl Command for GetPacketStatus {
    type IdType = u8;
    type CommandParameters = NoParameters;
    type ResponseParameters = GetPacketStatusResponse;

    fn id() -> Self::IdType {
        0x14
    }

    fn invoking_parameters(self) -> Self::CommandParameters {
        NoParameters::default()
    }
}

/// Device errors response
///
/// Contains flags for various error conditions.
#[derive(Debug, Clone, Copy)]
pub struct DeviceErrors {
    /// RC64k calibration error
    pub rc64k_calib_err: bool,
    /// RC13M calibration error
    pub rc13m_calib_err: bool,
    /// PLL calibration error
    pub pll_calib_err: bool,
    /// ADC calibration error
    pub adc_calib_err: bool,
    /// Image calibration error
    pub img_calib_err: bool,
    /// XOSC startup error
    /// Normal with TCXO at startup
    pub xosc_start_err: bool,
    /// PLL lock error
    pub pll_lock_err: bool,
    /// PA ramping error
    pub pa_ramp_err: bool,
}

impl FromByteArray for DeviceErrors {
    type Error = Infallible;
    type Array = [u8; 2];

    fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
        Ok(Self {
            rc64k_calib_err: bytes[1] & 0b1 != 0,
            rc13m_calib_err: bytes[1] & 0b10 != 0,
            pll_calib_err: bytes[1] & 0b100 != 0,
            adc_calib_err: bytes[1] & 0b1000 != 0,
            img_calib_err: bytes[1] & 0b1_0000 != 0,
            xosc_start_err: bytes[1] & 0b10_0000 != 0,
            pll_lock_err: bytes[1] & 0b100_0000 != 0,
            pa_ramp_err: bytes[0] & 0b1 != 0,
        })
    }
}

/// GetDeviceErrors response
///
/// Contains the device status and error flags.
#[derive(Debug, Clone, Copy)]
pub struct GetDeviceErrorsResponse {
    /// Device status from the first response byte
    pub status: Status,
    /// Device error flags
    pub errors: DeviceErrors,
}

impl FromByteArray for GetDeviceErrorsResponse {
    type Error = Infallible;
    type Array = [u8; 3]; // 1 status byte + 2 error bytes

    fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
        Ok(Self {
            status: Status::from_bytes([bytes[0]]).unwrap(),
            errors: DeviceErrors::from_bytes([bytes[1], bytes[2]]).unwrap(),
        })
    }
}

/// GetDeviceErrors command (0x17)
///
/// Returns error flags for various conditions.
///
/// # Important Notes
/// - Errors persist until explicitly cleared
/// - XOSC_START_ERR normal with TCXO at startup
/// - Multiple errors may be set simultaneously
/// - Use ClearDeviceErrors to clear flags
#[derive(Debug, Clone)]
pub struct GetDeviceErrors;

impl Command for GetDeviceErrors {
    type IdType = u8;
    type CommandParameters = NoParameters;
    type ResponseParameters = GetDeviceErrorsResponse;

    fn id() -> Self::IdType {
        0x17
    }

    fn invoking_parameters(self) -> Self::CommandParameters {
        NoParameters::default()
    }
}

/// ClearDeviceErrors command (0x07)
///
/// Clears all device error flags.
///
/// # Important Notes
/// - Clears all errors simultaneously
/// - Cannot clear errors individually
/// - Should be called after handling errors
#[derive(Debug, Clone)]
pub struct ClearDeviceErrors;

impl Command for ClearDeviceErrors {
    type IdType = u8;
    type CommandParameters = NoParameters;
    type ResponseParameters = NoParameters;

    fn id() -> Self::IdType {
        0x07
    }

    fn invoking_parameters(self) -> Self::CommandParameters {
        NoParameters::default()
    }
}

/// Statistics response
///
/// Contains packet reception statistics.
#[derive(Debug, Clone, Copy)]
pub struct Stats {
    /// Number of packets received
    /// Increments for all received packets
    pub packets_received: u16,

    /// Number of packets with CRC error
    /// Increments when CRC check fails
    pub packets_crc_error: u16,

    /// Number of packets with header error
    /// LoRa: Header CRC error
    /// FSK: Invalid length field
    pub packets_header_error: u16,
}

impl FromByteArray for Stats {
    type Error = Infallible;
    type Array = [u8; 6];

    fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
        Ok(Self {
            packets_received: u16::from_be_bytes(bytes[0..2].try_into().unwrap()),
            packets_crc_error: u16::from_be_bytes(bytes[2..4].try_into().unwrap()),
            packets_header_error: u16::from_be_bytes(bytes[4..6].try_into().unwrap()),
        })
    }
}

/// GetStats response
///
/// Contains the device status and packet statistics.
#[derive(Debug, Clone, Copy)]
pub struct GetStatsResponse {
    /// Device status from the first response byte
    pub status: Status,
    /// Packet reception statistics
    pub stats: Stats,
}

impl FromByteArray for GetStatsResponse {
    type Error = Infallible;
    type Array = [u8; 7]; // 1 status byte + 6 stats bytes

    fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
        Ok(Self {
            status: Status::from_bytes([bytes[0]]).unwrap(),
            stats: Stats::from_bytes([bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6]])
                .unwrap(),
        })
    }
}

/// GetStats command (0x10)
///
/// Returns packet reception statistics.
///
/// # Important Notes
/// - Stats persist through sleep mode
/// - Reset with ResetStats command
/// - Useful for monitoring link quality
/// - CRC/header error rates indicate issues
#[derive(Debug, Clone)]
pub struct GetStats;

impl Command for GetStats {
    type IdType = u8;
    type CommandParameters = NoParameters;
    type ResponseParameters = GetStatsResponse;

    fn id() -> Self::IdType {
        0x10
    }

    fn invoking_parameters(self) -> Self::CommandParameters {
        NoParameters::default()
    }
}

/// ResetStats command (0x00)
///
/// Resets all packet reception statistics to zero.
///
/// # Important Notes
/// - Resets all counters simultaneously
/// - Cannot reset counters individually
/// - Use before starting new test/monitoring
#[derive(Debug, Clone)]
pub struct ResetStats;

impl Command for ResetStats {
    type IdType = u8;
    type CommandParameters = NoParameters;
    type ResponseParameters = NoParameters;

    fn id() -> Self::IdType {
        0x00
    }

    fn invoking_parameters(self) -> Self::CommandParameters {
        NoParameters::default()
    }
}