-
Notifications
You must be signed in to change notification settings - Fork 14
/
RH_Serial.cpp
240 lines (215 loc) · 4.78 KB
/
RH_Serial.cpp
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
// RH_Serial.cpp
//
// Copyright (C) 2014 Mike McCauley
// $Id: RH_Serial.cpp,v 1.13 2017/01/12 23:58:00 mikem Exp $
#include <RH_Serial.h>
#if (RH_PLATFORM == RH_PLATFORM_STM32F2)
#else
#include <HardwareSerial.h>
#endif
#include <RHCRC.h>
RH_Serial::RH_Serial(HardwareSerial& serial)
:
_serial(serial),
_rxState(RxStateInitialising)
{
}
HardwareSerial& RH_Serial::serial()
{
return _serial;
}
bool RH_Serial::init()
{
if (!RHGenericDriver::init())
return false;
_rxState = RxStateIdle;
return true;
}
// Call this often
bool RH_Serial::available()
{
while (!_rxBufValid &&_serial.available())
handleRx(_serial.read());
return _rxBufValid;
}
void RH_Serial::waitAvailable()
{
#if (RH_PLATFORM == RH_PLATFORM_UNIX)
// Unix version driver in RHutil/HardwareSerial knows how to wait without polling
while (!available())
_serial.waitAvailable();
#else
RHGenericDriver::waitAvailable();
#endif
}
bool RH_Serial::waitAvailableTimeout(uint16_t timeout)
{
#if (RH_PLATFORM == RH_PLATFORM_UNIX)
// Unix version driver in RHutil/HardwareSerial knows how to wait without polling
unsigned long starttime = millis();
while ((millis() - starttime) < timeout)
{
_serial.waitAvailableTimeout(timeout - (millis() - starttime));
if (available())
return true;
YIELD;
}
return false;
#else
return RHGenericDriver::waitAvailableTimeout(timeout);
#endif
}
void RH_Serial::handleRx(uint8_t ch)
{
// State machine for receiving chars
switch(_rxState)
{
case RxStateIdle:
{
if (ch == DLE)
_rxState = RxStateDLE;
}
break;
case RxStateDLE:
{
if (ch == STX)
{
clearRxBuf();
_rxState = RxStateData;
}
else
_rxState = RxStateIdle;
}
break;
case RxStateData:
{
if (ch == DLE)
_rxState = RxStateEscape;
else
appendRxBuf(ch);
}
break;
case RxStateEscape:
{
if (ch == ETX)
{
// add fcs for DLE, ETX
_rxFcs = RHcrc_ccitt_update(_rxFcs, DLE);
_rxFcs = RHcrc_ccitt_update(_rxFcs, ETX);
_rxState = RxStateWaitFCS1; // End frame
}
else if (ch == DLE)
{
appendRxBuf(ch);
_rxState = RxStateData;
}
else
_rxState = RxStateIdle; // Unexpected
}
break;
case RxStateWaitFCS1:
{
_rxRecdFcs = ch << 8;
_rxState = RxStateWaitFCS2;
}
break;
case RxStateWaitFCS2:
{
_rxRecdFcs |= ch;
_rxState = RxStateIdle;
validateRxBuf();
}
break;
default: // Else some compilers complain
break;
}
}
void RH_Serial::clearRxBuf()
{
_rxBufValid = false;
_rxFcs = 0xffff;
_rxBufLen = 0;
}
void RH_Serial::appendRxBuf(uint8_t ch)
{
if (_rxBufLen < RH_SERIAL_MAX_PAYLOAD_LEN)
{
// Normal data, save and add to FCS
_rxBuf[_rxBufLen++] = ch;
_rxFcs = RHcrc_ccitt_update(_rxFcs, ch);
}
// If the buffer overflows, we dont record the trailing data, and the FCS will be wrong,
// causing the message to be dropped when the FCS is received
}
// Check whether the latest received message is complete and uncorrupted
void RH_Serial::validateRxBuf()
{
if (_rxRecdFcs != _rxFcs)
{
_rxBad++;
return;
}
// Extract the 4 headers
_rxHeaderTo = _rxBuf[0];
_rxHeaderFrom = _rxBuf[1];
_rxHeaderId = _rxBuf[2];
_rxHeaderFlags = _rxBuf[3];
if (_promiscuous ||
_rxHeaderTo == _thisAddress ||
_rxHeaderTo == RH_BROADCAST_ADDRESS)
{
_rxGood++;
_rxBufValid = true;
}
}
bool RH_Serial::recv(uint8_t* buf, uint8_t* len)
{
if (!available())
return false;
if (buf && len)
{
// Skip the 4 headers that are at the beginning of the rxBuf
if (*len > _rxBufLen-RH_SERIAL_HEADER_LEN)
*len = _rxBufLen-RH_SERIAL_HEADER_LEN;
memcpy(buf, _rxBuf+RH_SERIAL_HEADER_LEN, *len);
}
clearRxBuf(); // This message accepted and cleared
return true;
}
// Caution: this may block
bool RH_Serial::send(const uint8_t* data, uint8_t len)
{
if (!waitCAD())
return false; // Check channel activity
_txFcs = 0xffff; // Initial value
_serial.write(DLE); // Not in FCS
_serial.write(STX); // Not in FCS
// First the 4 headers
txData(_txHeaderTo);
txData(_txHeaderFrom);
txData(_txHeaderId);
txData(_txHeaderFlags);
// Now the payload
while (len--)
txData(*data++);
// End of message
_serial.write(DLE);
_txFcs = RHcrc_ccitt_update(_txFcs, DLE);
_serial.write(ETX);
_txFcs = RHcrc_ccitt_update(_txFcs, ETX);
// Now send the calculated FCS for this message
_serial.write((_txFcs >> 8) & 0xff);
_serial.write(_txFcs & 0xff);
return true;
}
void RH_Serial::txData(uint8_t ch)
{
if (ch == DLE) // DLE stuffing required?
_serial.write(DLE); // Not in FCS
_serial.write(ch);
_txFcs = RHcrc_ccitt_update(_txFcs, ch);
}
uint8_t RH_Serial::maxMessageLength()
{
return RH_SERIAL_MAX_MESSAGE_LEN;
}