-
Notifications
You must be signed in to change notification settings - Fork 409
/
Module.h
455 lines (322 loc) · 13.3 KB
/
Module.h
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
#if !defined(_RADIOLIB_MODULE_H)
#define _RADIOLIB_MODULE_H
#include "TypeDef.h"
#if defined(RADIOLIB_BUILD_ARDUINO)
#include <SPI.h>
#endif
/*!
\class Module
\brief Implements all common low-level methods to control the wireless module.
Every module class contains one private instance of this class.
*/
class Module {
public:
#if defined(RADIOLIB_BUILD_ARDUINO)
/*!
\brief Arduino Module constructor. Will use the default SPI interface and automatically initialize it
\param cs Arduino pin to be used as chip select.
\param irq Arduino pin to be used as interrupt/GPIO.
\param rst Arduino pin to be used as hardware reset for the module.
\param gpio Arduino pin to be used as additional interrupt/GPIO.
*/
Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE gpio = RADIOLIB_NC);
/*!
\brief Arduino Module constructor. Will not attempt SPI interface initialization.
\param cs Arduino pin to be used as chip select.
\param irq Arduino pin to be used as interrupt/GPIO.
\param rst Arduino pin to be used as hardware reset for the module.
\param gpio Arduino pin to be used as additional interrupt/GPIO.
\param spi SPI interface to be used, can also use software SPI implementations.
\param spiSettings SPI interface settings.
*/
Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE gpio, SPIClass& spi, SPISettings spiSettings = RADIOLIB_DEFAULT_SPI_SETTINGS);
#else
/*!
\brief Default constructor.
\param cs Pin to be used as chip select.
\param irq Pin to be used as interrupt/GPIO.
\param rst Pin to be used as hardware reset for the module.
\param gpio Pin to be used as additional interrupt/GPIO.
*/
Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE gpio = RADIOLIB_NC);
#endif
/*!
\brief Copy constructor.
\param mod Module instance to copy.
*/
Module(const Module& mod);
/*!
\brief Overload for assignment operator.
\param frame rvalue Module.
*/
Module& operator=(const Module& mod);
// public member variables
/*!
\brief Basic SPI read command. Defaults to 0x00.
*/
uint8_t SPIreadCommand = 0b00000000;
/*!
\brief Basic SPI write command. Defaults to 0x80.
*/
uint8_t SPIwriteCommand = 0b10000000;
// basic methods
/*!
\brief Initialize low-level module control.
*/
void init();
/*!
\brief Terminate low-level module control.
*/
void term();
// SPI methods
/*!
\brief SPI read method that automatically masks unused bits. This method is the preferred SPI read mechanism.
\param reg Address of SPI register to read.
\param msb Most significant bit of the register variable. Bits above this one will be masked out.
\param lsb Least significant bit of the register variable. Bits below this one will be masked out.
\returns Masked register value or status code.
*/
int16_t SPIgetRegValue(uint8_t reg, uint8_t msb = 7, uint8_t lsb = 0);
/*!
\brief Overwrite-safe SPI write method with verification. This method is the preferred SPI write mechanism.
\param reg Address of SPI register to write.
\param value Single byte value that will be written to the SPI register.
\param msb Most significant bit of the register variable. Bits above this one will not be affected by the write operation.
\param lsb Least significant bit of the register variable. Bits below this one will not be affected by the write operation.
\param checkInterval Number of milliseconds between register writing and verification reading. Some registers need up to 10ms to process the change.
\param checkMask Mask of bits to check, only bits set to 1 will be verified.
\returns \ref status_codes
*/
int16_t SPIsetRegValue(uint8_t reg, uint8_t value, uint8_t msb = 7, uint8_t lsb = 0, uint8_t checkInterval = 2, uint8_t checkMask = 0xFF);
/*!
\brief SPI burst read method.
\param reg Address of SPI register to read.
\param numBytes Number of bytes that will be read.
\param inBytes Pointer to array that will hold the read data.
*/
void SPIreadRegisterBurst(uint8_t reg, uint8_t numBytes, uint8_t* inBytes);
/*!
\brief SPI basic read method. Use of this method is reserved for special cases, SPIgetRegValue should be used instead.
\param reg Address of SPI register to read.
\returns Value that was read from register.
*/
uint8_t SPIreadRegister(uint8_t reg);
/*!
\brief SPI burst write method.
\param reg Address of SPI register to write.
\param data Pointer to array that holds the data that will be written.
\param numBytes Number of bytes that will be written.
*/
void SPIwriteRegisterBurst(uint8_t reg, uint8_t* data, uint8_t numBytes);
/*!
\brief SPI basic write method. Use of this method is reserved for special cases, SPIsetRegValue should be used instead.
\param reg Address of SPI register to write.
\param data Value that will be written to the register.
*/
void SPIwriteRegister(uint8_t reg, uint8_t data);
/*!
\brief SPI single transfer method.
\param cmd SPI access command (read/write/burst/...).
\param reg Address of SPI register to transfer to/from.
\param dataOut Data that will be transfered from master to slave.
\param dataIn Data that was transfered from slave to master.
\param numBytes Number of bytes to transfer.
*/
void SPItransfer(uint8_t cmd, uint8_t reg, uint8_t* dataOut, uint8_t* dataIn, uint8_t numBytes);
// pin number access methods
/*!
\brief Access method to get the pin number of SPI chip select.
\returns Pin number of SPI chip select configured in the constructor.
*/
RADIOLIB_PIN_TYPE getCs() const { return(_cs); }
/*!
\brief Access method to get the pin number of interrupt/GPIO.
\returns Pin number of interrupt/GPIO configured in the constructor.
*/
RADIOLIB_PIN_TYPE getIrq() const { return(_irq); }
/*!
\brief Access method to get the pin number of hardware reset pin.
\returns Pin number of hardware reset pin configured in the constructor.
*/
RADIOLIB_PIN_TYPE getRst() const { return(_rst); }
/*!
\brief Access method to get the pin number of second interrupt/GPIO.
\returns Pin number of second interrupt/GPIO configured in the constructor.
*/
RADIOLIB_PIN_TYPE getGpio() const { return(_gpio); }
/*!
\brief Some modules contain external RF switch controlled by two pins. This function gives RadioLib control over those two pins to automatically switch Rx and Tx state.
When using automatic RF switch control, DO NOT change the pin mode of rxEn or txEn from Arduino sketch!
\param rxEn RX enable pin.
\param txEn TX enable pin.
*/
void setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn);
/*!
\brief Set RF switch state.
\param rxPinState Pin state to set on Tx enable pin (usually high to transmit).
\param txPinState Pin state to set on Rx enable pin (usually high to receive).
*/
void setRfSwitchState(RADIOLIB_PIN_STATUS rxPinState, RADIOLIB_PIN_STATUS txPinState);
// Arduino core overrides
/*!
\brief Arduino core pinMode override that checks RADIOLIB_NC as alias for unused pin.
\param pin Pin to change the mode of.
\param mode Which mode to set.
*/
void pinMode(RADIOLIB_PIN_TYPE pin, RADIOLIB_PIN_MODE mode);
/*!
\brief Arduino core digitalWrite override that checks RADIOLIB_NC as alias for unused pin.
\param pin Pin to write to.
\param value Whether to set the pin high or low.
*/
void digitalWrite(RADIOLIB_PIN_TYPE pin, RADIOLIB_PIN_STATUS value);
/*!
\brief Arduino core digitalWrite override that checks RADIOLIB_NC as alias for unused pin.
\param pin Pin to read from.
\returns Pin value.
*/
RADIOLIB_PIN_STATUS digitalRead(RADIOLIB_PIN_TYPE pin);
/*!
\brief Arduino core tone override that checks RADIOLIB_NC as alias for unused pin and RADIOLIB_TONE_UNSUPPORTED to make sure the platform does support tone.
\param pin Pin to write to.
\param value Frequency to output.
*/
void tone(RADIOLIB_PIN_TYPE pin, uint16_t value, uint32_t duration = 0);
/*!
\brief Arduino core noTone override that checks RADIOLIB_NC as alias for unused pin and RADIOLIB_TONE_UNSUPPORTED to make sure the platform does support tone.
\param pin Pin to write to.
*/
void noTone(RADIOLIB_PIN_TYPE pin);
/*!
\brief Arduino core attachInterrupt override.
\param interruptNum Interrupt number.
\param userFunc Interrupt service routine.
\param mode Pin hcange direction.
*/
void attachInterrupt(RADIOLIB_PIN_TYPE interruptNum, void (*userFunc)(void), RADIOLIB_INTERRUPT_STATUS mode);
/*!
\brief Arduino core detachInterrupt override.
\param interruptNum Interrupt number.
*/
void detachInterrupt(RADIOLIB_PIN_TYPE interruptNum);
/*!
\brief Arduino core yield override.
*/
void yield();
/*!
\brief Arduino core delay override.
\param ms Delay length in milliseconds.
*/
void delay(uint32_t ms);
/*!
\brief Arduino core delayMicroseconds override.
\param us Delay length in microseconds.
*/
void delayMicroseconds(uint32_t us);
/*!
\brief Arduino core millis override.
*/
uint32_t millis();
/*!
\brief Arduino core micros override.
*/
uint32_t micros();
/*!
\brief Arduino core pulseIn override.
*/
uint32_t pulseIn(RADIOLIB_PIN_TYPE pin, RADIOLIB_PIN_STATUS state, uint32_t timeout);
/*!
\brief Arduino core SPI begin override.
*/
void begin();
/*!
\brief Arduino core SPI beginTransaction override.
*/
void beginTransaction();
/*!
\brief Arduino core SPI transfer override.
*/
uint8_t transfer(uint8_t b);
/*!
\brief Arduino core SPI endTransaction override.
*/
void endTransaction();
/*!
\brief Arduino core SPI end override.
*/
void end();
// helper functions to set up SPI overrides on Arduino
#if defined(RADIOLIB_BUILD_ARDUINO)
void SPIbegin();
void SPIend();
#endif
virtual void SPIbeginTransaction();
virtual uint8_t SPItransfer(uint8_t b);
virtual void SPIendTransaction();
/*!
\brief Function to reflect bits within a byte.
*/
static uint8_t flipBits(uint8_t b);
/*!
\brief Function to reflect bits within an integer.
*/
static uint16_t flipBits16(uint16_t i);
/*!
\brief Function to dump data as hex into the debug port.
\param data Data to dump.
\param len Number of bytes to dump.
*/
static void hexdump(uint8_t* data, size_t len);
/*!
\brief Function to dump device registers as hex into the debug port.
\param start First address to dump.
\param len Number of bytes to dump.
*/
void regdump(uint8_t start, uint8_t len);
// hardware abstraction layer callbacks
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_PIN_MODE);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_DIGITAL_WRITE);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_DIGITAL_READ);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_TONE);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_NO_TONE);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_ATTACH_INTERRUPT);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_DETACH_INTERRUPT);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_YIELD);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_DELAY);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_DELAY_MICROSECONDS);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_MILLIS);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_MICROS);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_PULSE_IN);
#if defined(RADIOLIB_BUILD_ARDUINO)
RADIOLIB_GENERATE_CALLBACK_SPI(RADIOLIB_CB_ARGS_SPI_BEGIN);
RADIOLIB_GENERATE_CALLBACK_SPI(RADIOLIB_CB_ARGS_SPI_BEGIN_TRANSACTION);
RADIOLIB_GENERATE_CALLBACK_SPI(RADIOLIB_CB_ARGS_SPI_TRANSFER);
RADIOLIB_GENERATE_CALLBACK_SPI(RADIOLIB_CB_ARGS_SPI_END_TRANSACTION);
RADIOLIB_GENERATE_CALLBACK_SPI(RADIOLIB_CB_ARGS_SPI_END);
#else
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_SPI_BEGIN);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_SPI_BEGIN_TRANSACTION);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_SPI_TRANSFER);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_SPI_END_TRANSACTION);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_SPI_END);
#endif
#if !defined(RADIOLIB_GODMODE)
private:
#endif
// pins
RADIOLIB_PIN_TYPE _cs = RADIOLIB_NC;
RADIOLIB_PIN_TYPE _irq = RADIOLIB_NC;
RADIOLIB_PIN_TYPE _rst = RADIOLIB_NC;
RADIOLIB_PIN_TYPE _gpio = RADIOLIB_NC;
// SPI interface (Arduino only)
#if defined(RADIOLIB_BUILD_ARDUINO)
SPIClass* _spi = NULL;
SPISettings _spiSettings = RADIOLIB_DEFAULT_SPI_SETTINGS;
bool _initInterface = false;
#endif
// RF switch presence and pins
bool _useRfSwitch = false;
RADIOLIB_PIN_TYPE _rxEn = RADIOLIB_NC;
RADIOLIB_PIN_TYPE _txEn = RADIOLIB_NC;
};
#endif