STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
sx1262.cpp
Go to the documentation of this file.
1#include "sx1262.h"
2
3#include <string.h>
4
5namespace radio_link {
6namespace {
7
8// Flags we clear once we have dealt with a packet (good or bad).
10
11// The status byte the chip returns in byte 1 of every "get" reply
12// (datasheet Rev 1.2, Table 13-76, page 95): bits 6:4 are the chip mode
13// (2 STBY_RC .. 6 TX), bits 3:1 the command status (3 timeout, 4 processing
14// error, 5 failure to execute). A missing chip or a stuck MISO line reads
15// 0x00 or 0xFF, which decode as modes 0 and 7 and are rejected here.
16bool status_ok(uint8_t status) {
17 const uint8_t mode = (status >> 4) & 0x07;
18 const uint8_t result = (status >> 1) & 0x07;
19 if (mode < 0x2 || mode > 0x6) {
20 return false;
21 }
22 return result != 0x3 && result != 0x4 && result != 0x5;
23}
24
25} // namespace
26
27bool sx1262_status_ok(uint8_t status) {
28 return status_ok(status);
29}
30
31bool Sx1262::command(const uint8_t* tx, uint8_t* rx, uint16_t n) {
32 if (!bus_.wait_busy()) {
33 return false;
34 }
35 bus_.transfer(tx, rx, n);
36 return true;
37}
38
40 const uint8_t tx[4] = {kOpSetRx, 0xFF, 0xFF, 0xFF};
41 uint8_t rx[4];
42 return command(tx, rx, sizeof tx);
43}
44
45bool Sx1262::clear_irq(uint16_t mask) {
46 const uint8_t tx[3] = {kOpClearIrqStatus, static_cast<uint8_t>(mask >> 8),
47 static_cast<uint8_t>(mask & 0xFF)};
48 uint8_t rx[3];
49 return command(tx, rx, sizeof tx);
50}
51
52RadioStatus Sx1262::receive(uint8_t* buf, uint8_t max_len, uint8_t& len) {
53 // 1. What happened? Reply bytes: junk, status, IRQ high, IRQ low.
54 const uint8_t irq_tx[4] = {kOpGetIrqStatus, 0, 0, 0};
55 uint8_t irq_rx[4];
56 if (!command(irq_tx, irq_rx, sizeof irq_tx) || !status_ok(irq_rx[1])) {
58 }
59 const uint16_t irq = static_cast<uint16_t>((irq_rx[2] << 8) | irq_rx[3]);
60
61 // A packet the radio itself flagged as damaged: throw it away.
62 if ((irq & (kIrqHeaderErr | kIrqCrcErr)) != 0) {
64 return clear_irq(kPacketFlags) ? RadioStatus::NoData
66 }
67 if ((irq & kIrqRxDone) == 0) {
69 }
70
71 // 2. How long is the packet, and where is it?
72 // Reply bytes: junk, status, length, start position.
73 const uint8_t buf_tx[4] = {kOpGetRxBufferStatus, 0, 0, 0};
74 uint8_t buf_rx[4];
75 if (!command(buf_tx, buf_rx, sizeof buf_tx) || !status_ok(buf_rx[1])) {
77 }
78 const uint8_t packet_len = buf_rx[2];
79 const uint8_t start = buf_rx[3];
80
81 // Nothing useful, or too big for the caller's box: throw it away.
82 if (packet_len == 0 || packet_len > max_len) {
83 if (!clear_irq(kPacketFlags)) {
85 }
86 return packet_len == 0 ? RadioStatus::NoData : RadioStatus::ErrTooLong;
87 }
88
89 // 3. Read the packet. We send the opcode, the start position and one
90 // NOP, then one dummy byte for each packet byte we want back. Reply
91 // bytes: junk, status, status, then the packet.
92 const uint16_t total = static_cast<uint16_t>(3 + packet_len);
94 read_tx_[1] = start;
95 memset(read_tx_ + 2, 0, total - 2); // NOP, then one dummy byte each
96 if (!command(read_tx_, read_rx_, total) || !status_ok(read_rx_[1])) {
98 }
99 memcpy(buf, read_rx_ + 3, packet_len);
100 len = packet_len;
101
102 // 4. Tell the chip we have taken the packet (this turns DIO1 off).
103 if (!clear_irq(kPacketFlags)) {
105 }
106 return RadioStatus::Ok;
107}
108
110 // Reply bytes: junk, status, RssiPkt, SnrPkt, SignalRssiPkt.
111 const uint8_t tx[5] = {kOpGetPacketStatus, 0, 0, 0, 0};
112 uint8_t rx[5];
113 if (!command(tx, rx, sizeof tx) || !status_ok(rx[1])) {
114 return false;
115 }
116 out.rssi_x2 = static_cast<int16_t>(-static_cast<int16_t>(rx[2]));
117 out.snr_x4 = static_cast<int8_t>(rx[3]); // two's complement
118 out.signal_rssi_x2 = static_cast<int16_t>(-static_cast<int16_t>(rx[4]));
119 return true;
120}
121
122} // namespace radio_link
static void start(gnss_frame_parser_t *p, uint8_t byte)
Definition gnss_frame.c:45
json_pointer result
Definition json.hpp:14064
*brief user defined string literal for JSON values *sa std::size_t n
Definition json.hpp:24474