RadioLib Universal wireless communication library for Arduino

Module.h
1 #if !defined(_RADIOLIB_MODULE_H)
2 #define _RADIOLIB_MODULE_H
3
4 #include "TypeDef.h"
5 #include "Hal.h"
6
7 #if defined(RADIOLIB_BUILD_ARDUINO)
8 #include <SPI.h>
9 #endif
10
11 #if defined(STM32WLxx)
12 #include <SubGhz.h>
13 #endif
14
21 #define END_OF_MODE_TABLE { Module::MODE_END_OF_TABLE, {} }
22
23 // default timeout for SPI transfers
24 #define RADIOLIB_MODULE_SPI_TIMEOUT (1000)
25
31 class Module {
32 public:
42 static const size_t RFSWITCH_MAX_PINS = 3;
43
49 struct RfSwitchMode_t {
50 uint8_t mode;
51 uint32_t values[RFSWITCH_MAX_PINS];
52 };
53
61 enum OpMode_t {
65 MODE_END_OF_TABLE = 0,
67 MODE_IDLE,
69 MODE_RX,
71 MODE_TX,
72 };
73
74 #if defined(RADIOLIB_BUILD_ARDUINO)
82 Module(uint32_t cs, uint32_t irq, uint32_t rst, uint32_t gpio = RADIOLIB_NC);
83
93 Module(uint32_t cs, uint32_t irq, uint32_t rst, uint32_t gpio, SPIClass& spi, SPISettings spiSettings = RADIOLIB_DEFAULT_SPI_SETTINGS);
94 #endif
95
104 Module(RadioLibHal *hal, uint32_t cs, uint32_t irq, uint32_t rst, uint32_t gpio = RADIOLIB_NC);
105
110 Module(const Module& mod);
111
116 Module& operator=(const Module& mod);
117
118 // public member variables
122 RadioLibHal* hal = NULL;
123
127 uint8_t SPIreadCommand = 0b00000000;
128
132 uint8_t SPIwriteCommand = 0b10000000;
133
137 uint8_t SPInopCommand = 0x00;
138
142 uint8_t SPIstatusCommand = 0x00;
143
147 uint8_t SPIaddrWidth = 8;
148
153 bool SPIstreamType = false;
154
158 int16_t SPIstreamError = RADIOLIB_ERR_UNKNOWN;
159
163 typedef int16_t (*SPIparseStatusCb_t)(uint8_t in);
164
169 SPIparseStatusCb_t SPIparseStatusCb = nullptr;
170
171 #if defined(RADIOLIB_INTERRUPT_TIMING)
172
176 typedef void (*TimerSetupCb_t)(uint32_t len);
177
181 TimerSetupCb_t TimerSetupCb = nullptr;
182
186 volatile bool TimerFlag = false;
187
188 #endif
189
190 // basic methods
191
195 void init();
196
200 void term();
201
202 // SPI methods
203
211 int16_t SPIgetRegValue(uint16_t reg, uint8_t msb = 7, uint8_t lsb = 0);
212
223 int16_t SPIsetRegValue(uint16_t reg, uint8_t value, uint8_t msb = 7, uint8_t lsb = 0, uint8_t checkInterval = 2, uint8_t checkMask = 0xFF);
224
231 void SPIreadRegisterBurst(uint16_t reg, size_t numBytes, uint8_t* inBytes);
232
238 uint8_t SPIreadRegister(uint16_t reg);
239
246 void SPIwriteRegisterBurst(uint16_t reg, uint8_t* data, size_t numBytes);
247
253 void SPIwriteRegister(uint16_t reg, uint8_t data);
254
263 void SPItransfer(uint8_t cmd, uint16_t reg, uint8_t* dataOut, uint8_t* dataIn, size_t numBytes);
264
269 int16_t SPIcheckStream();
270
280 int16_t SPIreadStream(uint8_t cmd, uint8_t* data, size_t numBytes, bool waitForGpio = true, bool verify = true);
281
292 int16_t SPIreadStream(uint8_t* cmd, uint8_t cmdLen, uint8_t* data, size_t numBytes, bool waitForGpio = true, bool verify = true);
293
303 int16_t SPIwriteStream(uint8_t cmd, uint8_t* data, size_t numBytes, bool waitForGpio = true, bool verify = true);
304
315 int16_t SPIwriteStream(uint8_t* cmd, uint8_t cmdLen, uint8_t* data, size_t numBytes, bool waitForGpio = true, bool verify = true);
316
329 int16_t SPItransferStream(uint8_t* cmd, uint8_t cmdLen, bool write, uint8_t* dataOut, uint8_t* dataIn, size_t numBytes, bool waitForGpio, uint32_t timeout);
330
331 // pin number access methods
332
337 uint32_t getCs() const { return(csPin); }
338
343 uint32_t getIrq() const { return(irqPin); }
344
349 uint32_t getRst() const { return(rstPin); }
350
355 uint32_t getGpio() const { return(gpioPin); }
356
373 void setRfSwitchPins(uint32_t rxEn, uint32_t txEn);
374
437 void setRfSwitchTable(const uint32_t (&pins)[RFSWITCH_MAX_PINS], const RfSwitchMode_t table[]);
438
446 const RfSwitchMode_t *findRfSwitchMode(uint8_t mode) const;
447
452 void setRfSwitchState(uint8_t mode);
453
461 void waitForMicroseconds(uint32_t start, uint32_t len);
462
469 static uint32_t reflect(uint32_t in, uint8_t bits);
470
471 #if defined(RADIOLIB_DEBUG)
479 static void hexdump(uint8_t* data, size_t len, uint32_t offset = 0, uint8_t width = 1, bool be = false);
480
486 void regdump(uint16_t start, size_t len);
487 #endif
488
489 #if defined(RADIOLIB_DEBUG) and defined(RADIOLIB_BUILD_ARDUINO)
490 static size_t serialPrintf(const char* format, ...);
491 #endif
492
493 #if !defined(RADIOLIB_GODMODE)
494 private:
495 #endif
496 uint32_t csPin = RADIOLIB_NC;
497 uint32_t irqPin = RADIOLIB_NC;
498 uint32_t rstPin = RADIOLIB_NC;
499 uint32_t gpioPin = RADIOLIB_NC;
500
501 // RF switch pins and table
502 uint32_t rfSwitchPins[RFSWITCH_MAX_PINS] = { RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC };
503 const RfSwitchMode_t *rfSwitchTable = nullptr;
504
505 #if defined(RADIOLIB_INTERRUPT_TIMING)
506 uint32_t prevTimingLen = 0;
507 #endif
508 };
509
510 #endif
Module
Implements all common low-level methods to control the wireless module. Every module class contains o...
Definition: Module.h:31
Module::SPIgetRegValue
int16_t SPIgetRegValue(uint16_t reg, uint8_t msb=7, uint8_t lsb=0)
SPI read method that automatically masks unused bits. This method is the preferred SPI read mechanism...
Definition: Module.cpp:58
Module::SPIstatusCommand
uint8_t SPIstatusCommand
Basic SPI status read command. Defaults to 0x00.
Definition: Module.h:142
Module::SPIsetRegValue
int16_t SPIsetRegValue(uint16_t reg, uint8_t value, uint8_t msb=7, uint8_t lsb=0, uint8_t checkInterval=2, uint8_t checkMask=0xFF)
Overwrite-safe SPI write method with verification. This method is the preferred SPI write mechanism.
Definition: Module.cpp:68
Module::RFSWITCH_MAX_PINS
static const size_t RFSWITCH_MAX_PINS
The maximum number of pins supported by the RF switch code.
Definition: Module.h:42
Module::SPIstreamType
bool SPIstreamType
Whether the SPI interface is stream-type (e.g. SX126x) or register-type (e.g. SX127x)....
Definition: Module.h:153
Module::SPIwriteRegister
void SPIwriteRegister(uint16_t reg, uint8_t data)
SPI basic write method. Use of this method is reserved for special cases, SPIsetRegValue should be us...
Definition: Module.cpp:136
Module::hal
RadioLibHal * hal
Hardware abstraction layer to be used.
Definition: Module.h:122
Module::term
void term()
Terminate low-level module control.
Definition: Module.cpp:53
Module::SPInopCommand
uint8_t SPInopCommand
Basic SPI no-operation command. Defaults to 0x00.
Definition: Module.h:137
Module::waitForMicroseconds
void waitForMicroseconds(uint32_t start, uint32_t len)
Wait for time to elapse, either using the microsecond timer, or the TimerFlag. Note that in interrupt...
Definition: Module.cpp:380
Module::reflect
static uint32_t reflect(uint32_t in, uint8_t bits)
Function to reflect bits within a byte.
Definition: Module.cpp:398
Module::operator=
Module & operator=(const Module &mod)
Overload for assignment operator.
Definition: Module.cpp:33
Module::getGpio
uint32_t getGpio() const
Access method to get the pin number of second interrupt/GPIO.
Definition: Module.h:355
Module::findRfSwitchMode
const RfSwitchMode_t * findRfSwitchMode(uint8_t mode) const
Find a mode in the RfSwitchTable.
Definition: Module.cpp:515
Module::OpMode_t
OpMode_t
Definition: Module.h:61
Module::MODE_TX
@ MODE_TX
Definition: Module.h:71
Module::MODE_IDLE
@ MODE_IDLE
Definition: Module.h:67
Module::MODE_RX
@ MODE_RX
Definition: Module.h:69
Module::MODE_END_OF_TABLE
@ MODE_END_OF_TABLE
Definition: Module.h:65
Module::SPIcheckStream
int16_t SPIcheckStream()
Method to check the result of last SPI stream transfer.
Definition: Module.cpp:240
Module::SPIstreamError
int16_t SPIstreamError
The last recorded SPI stream error.
Definition: Module.h:158
Module::SPIwriteStream
int16_t SPIwriteStream(uint8_t cmd, uint8_t *data, size_t numBytes, bool waitForGpio=true, bool verify=true)
Method to perform a write transaction with SPI stream.
Definition: Module.cpp:223
Module::SPItransfer
void SPItransfer(uint8_t cmd, uint16_t reg, uint8_t *dataOut, uint8_t *dataIn, size_t numBytes)
SPI single transfer method.
Definition: Module.cpp:145
Module::setRfSwitchPins
void setRfSwitchPins(uint32_t rxEn, uint32_t txEn)
Some modules contain external RF switch controlled by pins. This function gives RadioLib control over...
Definition: Module.cpp:492
Module::Module
Module(RadioLibHal *hal, uint32_t cs, uint32_t irq, uint32_t rst, uint32_t gpio=RADIOLIB_NC)
Module constructor.
Definition: Module.cpp:25
Module::SPIreadCommand
uint8_t SPIreadCommand
Basic SPI read command. Defaults to 0x00.
Definition: Module.h:127
Module::SPIreadRegisterBurst
void SPIreadRegisterBurst(uint16_t reg, size_t numBytes, uint8_t *inBytes)
SPI burst read method.
Definition: Module.cpp:107
Module::SPIparseStatusCb_t
int16_t(* SPIparseStatusCb_t)(uint8_t in)
SPI status parsing callback typedef.
Definition: Module.h:163
Module::SPIreadRegister
uint8_t SPIreadRegister(uint16_t reg)
SPI basic read method. Use of this method is reserved for special cases, SPIgetRegValue should be use...
Definition: Module.cpp:116
Module::getCs
uint32_t getCs() const
Access method to get the pin number of SPI chip select.
Definition: Module.h:337
Module::getIrq
uint32_t getIrq() const
Access method to get the pin number of interrupt/GPIO.
Definition: Module.h:343
Module::SPIreadStream
int16_t SPIreadStream(uint8_t cmd, uint8_t *data, size_t numBytes, bool waitForGpio=true, bool verify=true)
Method to perform a read transaction with SPI stream.
Definition: Module.cpp:206
Module::SPIaddrWidth
uint8_t SPIaddrWidth
SPI address width. Defaults to 8, currently only supports 8 and 16-bit addresses.
Definition: Module.h:147
Module::setRfSwitchTable
void setRfSwitchTable(const uint32_t(&pins)[RFSWITCH_MAX_PINS], const RfSwitchMode_t table[])
Some modules contain external RF switch controlled by pins. This function gives RadioLib control over...
Definition: Module.cpp:508
Module::SPItransferStream
int16_t SPItransferStream(uint8_t *cmd, uint8_t cmdLen, bool write, uint8_t *dataOut, uint8_t *dataIn, size_t numBytes, bool waitForGpio, uint32_t timeout)
SPI single transfer method for modules with stream-type SPI interface (SX126x, SX128x etc....
Definition: Module.cpp:259
Module::init
void init()
Initialize low-level module control.
Definition: Module.cpp:43
Module::getRst
uint32_t getRst() const
Access method to get the pin number of hardware reset pin.
Definition: Module.h:349
Module::SPIwriteCommand
uint8_t SPIwriteCommand
Basic SPI write command. Defaults to 0x80.
Definition: Module.h:132
Module::SPIwriteRegisterBurst
void SPIwriteRegisterBurst(uint16_t reg, uint8_t *data, size_t numBytes)
SPI burst write method.
Definition: Module.cpp:127
Module::SPIparseStatusCb
SPIparseStatusCb_t SPIparseStatusCb
Callback to function that will parse the module-specific status codes to RadioLib status codes....
Definition: Module.h:169
Module::setRfSwitchState
void setRfSwitchState(uint8_t mode)
Set RF switch state.
Definition: Module.cpp:525
RadioLibHal
Hardware abstraction library base interface.
Definition: Hal.h:57
RADIOLIB_ERR_UNKNOWN
#define RADIOLIB_ERR_UNKNOWN
There was an unexpected, unknown error. If you see this, something went incredibly wrong....
Definition: TypeDef.h:110
Module::RfSwitchMode_t
Definition: Module.h:49