-
Notifications
You must be signed in to change notification settings - Fork 0
/
RH_E32.cpp
372 lines (317 loc) · 8.87 KB
/
RH_E32.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
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
// RH_E32.cpp
//
// Copyright (C) 2017 Mike McCauley
// $Id: RH_E32.cpp,v 1.4 2018/11/15 01:10:48 mikem Exp $
#include <RadioHead.h>
#ifdef RH_HAVE_SERIAL // No serial
#include <RH_E32.h>
#include <Stream.h>
RH_E32::RH_E32(Stream *s, uint8_t m0_pin, uint8_t m1_pin, uint8_t aux_pin)
:
_s(s),
_m0_pin(m0_pin),
_m1_pin(m1_pin),
_aux_pin(aux_pin)
{
// Prevent glitches a startup
pinMode(_aux_pin, INPUT);
digitalWrite(_m0_pin, true);
digitalWrite(_m1_pin, true);
pinMode(_m0_pin, OUTPUT);
pinMode(_m1_pin, OUTPUT);
}
bool RH_E32::init()
{
// When a message is available, Aux will go low 5 msec before the first character is output
// So if we ever wait more than this period of time after Aux low, can conclude there will be no data
_s->setTimeout(10);
// Wait until the module is connected
waitAuxHigh();
if (!getVersion())
return false; // Could not communicate with module or wrong type of module
setMode(RHModeRx);
clearRxBuf();
if (!setDataRate(DataRate5kbps))
return false;
if (!setPower(Power21dBm))
return false;
// if (!setBaudRate(BaudRate9600, Parity8N1))
// return false;
if (!setFrequency(433))
return false;
return true;
}
bool RH_E32::set_tx_header( uint8_t rx_ADDH, uint8_t rx_ADDL, uint8_t rx_freq, uint8_t tx_ADDH, uint8_t tx_ADDL, uint8_t tx_freq) {
_rx_ADDH=rx_ADDH;
_rx_ADDL=rx_ADDL;
_rx_freq=rx_freq;
_tx_ADDH=tx_ADDH;
_tx_ADDL=tx_ADDL;
_tx_freq=tx_freq;
//verify
if (_rx_ADDH==rx_ADDH && _rx_ADDL==rx_ADDL && _rx_freq==rx_freq && _tx_ADDH==tx_ADDH && _tx_ADDL==tx_ADDL && _tx_freq==tx_freq) {
return 1;
}
else {
return 0;
}
}
bool RH_E32::print_tx_header () {
// add spirintft in the end
Serial.print (_rx_ADDH, DEC);
Serial.print (_rx_ADDL, DEC);
Serial.print (_rx_freq, DEC);
Serial.print (_tx_ADDH, DEC);
Serial.print (_tx_ADDL, DEC);
Serial.print (_tx_freq, DEC);
return 1;
}
bool RH_E32::reset()
{
setOperatingMode(ModeSleep);
uint8_t resetCommand[] = { RH_E32_COMMAND_RESET, RH_E32_COMMAND_RESET, RH_E32_COMMAND_RESET };
size_t result = _s->write(resetCommand, sizeof(resetCommand));
setOperatingMode(ModeNormal);
return (result == sizeof(resetCommand));
}
bool RH_E32::readParameters(Parameters& params)
{
setOperatingMode(ModeSleep);
uint8_t readParamsCommand[] = { RH_E32_COMMAND_READ_PARAMS, RH_E32_COMMAND_READ_PARAMS, RH_E32_COMMAND_READ_PARAMS };
_s->write(readParamsCommand, sizeof(readParamsCommand));
size_t result = _s->readBytes((char*)¶ms, sizeof(params)); // default 1 sec timeout
setOperatingMode(ModeNormal);
return (result == sizeof(Parameters));
}
bool RH_E32::writeParameters(Parameters& params, bool save)
{
setOperatingMode(ModeSleep);
params.head = save ? RH_E32_COMMAND_WRITE_PARAMS_SAVE : RH_E32_COMMAND_WRITE_PARAMS_NOSAVE;
// printBuffer("writing now", (uint8_t*)¶ms, sizeof(params));
size_t result = _s->write((uint8_t*)¶ms, sizeof(params));
if (result != sizeof(params))
return false;
// Now we expect to get the same data back
result = _s->readBytes((char*)¶ms, sizeof(params));
if (result != sizeof(params))
return false;
// printBuffer("additional read", (uint8_t*)¶ms, sizeof(params));
// Without a little delay here, writing params often fails
delay(20);
setOperatingMode(ModeNormal);
return result == sizeof(params);
}
void RH_E32::setOperatingMode(OperatingMode mode)
{
waitAuxHigh();
switch (mode)
{
case ModeNormal:
digitalWrite(_m0_pin, false);
digitalWrite(_m1_pin, false);
break;
case ModeWakeUp:
digitalWrite(_m0_pin, true);
digitalWrite(_m1_pin, false);
break;
case ModePowerSaving:
digitalWrite(_m0_pin, false);
digitalWrite(_m1_pin, true);
break;
case ModeSleep:
digitalWrite(_m0_pin, true);
digitalWrite(_m1_pin, true);
break;
}
delay(10); // Takes a little while to start its response
waitAuxHigh();
}
bool RH_E32::getVersion()
{
setOperatingMode(ModeSleep);
uint8_t readVersionCommand[] = { RH_E32_COMMAND_READ_VERSION, RH_E32_COMMAND_READ_VERSION, RH_E32_COMMAND_READ_VERSION };
_s->write(readVersionCommand, sizeof(readVersionCommand));
uint8_t version[4];
size_t result = _s->readBytes((char *)version, sizeof(version)); // default 1 sec timeout
setOperatingMode(ModeNormal);
if (result == 4)
{
// Successful read
printBuffer("read version", version, sizeof(version));
if (version[0] != 0xc3 || version [1] != 0x32)
{
// Not an E32
return false;
}
else
{
// REVISIT: do something with it?
}
}
else
{
// Read failed: no module? Wrong baud?
return false;
}
return true;
}
void RH_E32::waitAuxHigh()
{
// REVISIT: timeout needed?
while (digitalRead(_aux_pin) == false)
;
}
void RH_E32::waitAuxLow()
{
while (digitalRead(_aux_pin) == true)
;
}
// Check whether the latest received message is complete and uncorrupted
void RH_E32::validateRxBuf()
{
if (_bufLen < RH_E32_RECV_HEADER_LEN)
return; // Too short to be a real message
if (_bufLen != _buf[0]) {
return; // Do we have all the message?
} else {
_rxGood++;
_rxBufValid = true;
}
}
void RH_E32::clearRxBuf()
{
_rxBufValid = false;
_bufLen = 0;
}
bool RH_E32::available()
{
// Caution: long packets could be sent in several bursts
if (!_rxBufValid)
{
if (_mode == RHModeTx)
return false;
if (!_s->available())
return false;
// Suck up all the characters we can
uint8_t data;
while (_s->readBytes((char *)&data, 1) == 1) // Not read timeout
{
_buf[_bufLen++] = data;
}
// Now assess what we have
if (_bufLen < RH_E32_RECV_HEADER_LEN)
{
Serial.println("Incomplete header");
return false;
}
else if (_bufLen < _buf[0])
{
Serial.println("Incomplete message");
return false;
}
else if ( _bufLen > _buf[0]
|| _bufLen > RH_E32_MAX_PAYLOAD_LEN)
{
Serial.println("Overrun");
clearRxBuf();
_rxBad++;
return false;
}
// Else it a partial or complete message, test it
// printBuffer("read success", _buf, _bufLen);
validateRxBuf();
}
return _rxBufValid;
}
bool RH_E32::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 > _bufLen - RH_E32_RECV_HEADER_LEN)
*len = _bufLen - RH_E32_RECV_HEADER_LEN;
memcpy(buf, _buf + RH_E32_RECV_HEADER_LEN, *len);
}
clearRxBuf(); // This message accepted and cleared
return true;
}
bool RH_E32::send(const uint8_t* data, uint8_t len)
{
if (len > RH_E32_MAX_MESSAGE_LEN)
return false;
waitPacketSent(); // Make sure we dont collide with previous message
//E32 TTL messsage addresing frame
_buf[0] = _rx_ADDH;
_buf[1] = _rx_ADDL;
_buf[2] = _rx_freq;
//verification buf_length
//visible message data
_buf[3] = len + RH_E32_RECV_HEADER_LEN;
_buf[4] = _tx_ADDH;
_buf[5] = _tx_ADDL;
_buf[6] = _tx_freq;
// REVISIT: do we really have to do this? perhaps just write it after writing the header?
memcpy(_buf+RH_E32_SEND_HEADER_LEN, data, len);
_s->write(_buf, len + RH_E32_SEND_HEADER_LEN);
setMode(RHModeTx);
_txGood++;
// Aux will return high when the TX buffer is empty
return true;
}
uint8_t RH_E32::maxMessageLength()
{
return RH_E32_MAX_MESSAGE_LEN;
}
bool RH_E32::waitPacketSent()
{
if (_mode == RHModeTx)
waitAuxHigh();
setMode(RHModeRx);
return true;
}
bool RH_E32::setDataRate(DataRate rate)
{
Parameters params;
if (!readParameters(params))
return false;
// The DataRate enums are the same values as the register bitmasks
params.sped &= ~RH_E32_PARAM_SPED_DATARATE_MASK;
params.sped |= (rate & RH_E32_PARAM_SPED_DATARATE_MASK);
return writeParameters(params);
}
bool RH_E32::setPower(PowerLevel level)
{
Parameters params;
if (!readParameters(params))
return false;
// The DataRate enums are the same values as the register bitmasks
params.option &= ~RH_E32_PARAM_OPTION_POWER_MASK;
params.option |= (level & RH_E32_PARAM_OPTION_POWER_MASK);
return writeParameters(params);
}
bool RH_E32::setBaudRate(BaudRate rate, Parity parity)
{
Parameters params;
if (!readParameters(params))
return false;
// The DataRate enums are the same values as the register bitmasks
params.sped &= ~RH_E32_PARAM_SPED_UART_BAUD_MASK;
params.sped |= (rate & RH_E32_PARAM_SPED_UART_BAUD_MASK);
// Also set the parity
params.sped &= ~RH_E32_PARAM_SPED_UART_MODE_MASK;
params.sped |= (parity & RH_E32_PARAM_SPED_UART_MODE_MASK);
return writeParameters(params);
}
bool RH_E32::setFrequency(uint16_t frequency)
{
if (frequency < 410 || frequency > 441)
return false;
Parameters params;
if (!readParameters(params))
return false;
params.chan = frequency - 410;
return writeParameters(params);
}
#endif // RH_PLATFORM_ATTINY