Repository navigation
Expand file tree
/
Copy pathxbee_spi.cpp
More file actions
181 lines (153 loc) · 5.48 KB
/
Copy pathxbee_spi.cpp
File metadata and controls
181 lines (153 loc) · 5.48 KB
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
#include "xbee_spi.h"
#include <Arduino.h>
#include <SPI.h>
#include "pin_assignment.h"
#include "xbee_frame.h"
namespace {
// Matches thePunderWoman/Amidala's proven-working XBee3 SPI config (same
// module family) — see its xbee_spi.cpp.
const SPISettings kXbeeSpiSettings(3000000, MSBFIRST, SPI_MODE0);
uint8_t xbeeTransfer() { return SPI.transfer(0xFF); }
void xbeeDrain(uint16_t n) {
while (n--) SPI.transfer(0xFF);
}
} // namespace
void XbeeSpi::begin() {
pinMode(PinAssignment::kXbeeSpiCs, OUTPUT);
digitalWrite(PinAssignment::kXbeeSpiCs, HIGH);
pinMode(PinAssignment::kXbeeSpiAttn, INPUT);
SPI.begin(PinAssignment::kXbeeSpiSck, PinAssignment::kXbeeSpiMiso,
PinAssignment::kXbeeSpiMosi, PinAssignment::kXbeeSpiCs);
}
bool XbeeSpi::frameAvailable() {
return digitalRead(PinAssignment::kXbeeSpiAttn) == LOW;
}
int32_t XbeeSpi::readFrame(uint8_t *buf, uint16_t maxLen) {
SPI.beginTransaction(kXbeeSpiSettings);
digitalWrite(PinAssignment::kXbeeSpiCs, LOW);
// Skip idle 0xFF bytes (the module pads before the start delimiter).
uint8_t b = 0xFF;
for (int i = 0; i < 32 && b != 0x7E; i++) {
b = xbeeTransfer();
}
if (b != 0x7E) {
digitalWrite(PinAssignment::kXbeeSpiCs, HIGH);
SPI.endTransaction();
return -1;
}
const uint16_t length =
(static_cast<uint16_t>(xbeeTransfer()) << 8) | xbeeTransfer();
if (length == 0 || length > maxLen) {
xbeeDrain(length + 1); // drain data + checksum so the stream stays in sync
digitalWrite(PinAssignment::kXbeeSpiCs, HIGH);
SPI.endTransaction();
return 0;
}
for (uint16_t i = 0; i < length; i++) {
buf[i] = xbeeTransfer();
}
const uint8_t checksum = xbeeTransfer();
digitalWrite(PinAssignment::kXbeeSpiCs, HIGH);
SPI.endTransaction();
if (!XbeeFrame::checksumValid(buf, length, checksum)) {
return 0;
}
return static_cast<int32_t>(length);
}
void XbeeSpi::writeFrame(const uint8_t *frameData, uint16_t length) {
const uint8_t checksum = XbeeFrame::computeChecksum(frameData, length);
SPI.beginTransaction(kXbeeSpiSettings);
digitalWrite(PinAssignment::kXbeeSpiCs, LOW);
SPI.transfer(0x7E);
SPI.transfer(static_cast<uint8_t>(length >> 8));
SPI.transfer(static_cast<uint8_t>(length & 0xFF));
for (uint16_t i = 0; i < length; i++) {
SPI.transfer(frameData[i]);
}
SPI.transfer(checksum);
digitalWrite(PinAssignment::kXbeeSpiCs, HIGH);
SPI.endTransaction();
}
bool XbeeSpi::sendAtCommand(const char *atCmd, const uint8_t *value,
uint8_t valueLength, uint8_t *outValue,
uint8_t outValueCapacity,
uint8_t *outValueLength,
unsigned long timeoutMs) {
constexpr uint8_t kFrameId = 0x01;
uint8_t requestFrame[64];
const uint16_t requestLength = XbeeFrame::buildAtCommandFrame(
requestFrame, sizeof(requestFrame), kFrameId, atCmd, value,
valueLength);
if (requestLength == 0) {
return false;
}
writeFrame(requestFrame, requestLength);
const unsigned long start = millis();
uint8_t responseFrame[64];
while (millis() - start < timeoutMs) {
if (!frameAvailable()) {
yield(); // let the ESP32's background tasks/watchdog run
continue;
}
const int32_t length = readFrame(responseFrame, sizeof(responseFrame));
if (length <= 0) {
continue; // no delimiter yet, or a bad frame — keep waiting
}
XbeeFrame::AtCommandResponse response;
if (!XbeeFrame::parseAtCommandResponse(
responseFrame, static_cast<uint16_t>(length), &response)) {
continue; // some other frame type was queued — not our response
}
if (response.frameId != kFrameId) {
continue;
}
if (response.status != XbeeFrame::AtCommandStatus::kOk) {
return false;
}
const uint8_t copyLength = response.valueLength < outValueCapacity
? response.valueLength
: outValueCapacity;
for (uint8_t i = 0; i < copyLength; i++) {
outValue[i] = response.value[i];
}
if (outValueLength != nullptr) {
*outValueLength = copyLength;
}
return true;
}
return false; // timed out
}
void XbeeSpi::sendPacket(const uint8_t *payload, uint16_t payloadLength) {
constexpr uint8_t kFrameId = 0x02; // distinct from sendAtCommand's 0x01
uint8_t frame[32];
const uint16_t length = XbeeFrame::buildTransmitRequestFrame(
frame, sizeof(frame), kFrameId, payload, payloadLength);
if (length == 0) {
return; // payload too large for the frame buffer — drop it
}
writeFrame(frame, length);
}
bool XbeeSpi::pollForPacket(uint8_t *outPayload, uint16_t outPayloadCapacity,
uint16_t *outPayloadLength) {
if (!frameAvailable()) {
return false;
}
uint8_t frame[64];
const int32_t length = readFrame(frame, sizeof(frame));
if (length <= 0) {
return false; // no delimiter yet, or a bad frame was drained
}
XbeeFrame::ReceivePacket packet;
if (!XbeeFrame::parseReceivePacket(frame, static_cast<uint16_t>(length),
&packet)) {
return false; // some other frame type — not what we're looking for
}
const uint16_t copyLength = packet.payloadLength < outPayloadCapacity
? packet.payloadLength
: outPayloadCapacity;
for (uint16_t i = 0; i < copyLength; i++) {
outPayload[i] = packet.payload[i];
}
*outPayloadLength = copyLength;
return true;
}