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
28 class Module {
29 public:
39 static const size_t RFSWITCH_MAX_PINS = 3;
40
46 struct RfSwitchMode_t {
47 uint8_t mode;
48 uint32_t values[RFSWITCH_MAX_PINS];
49 };
50
58 enum OpMode_t {
62 MODE_END_OF_TABLE = 0,
64 MODE_IDLE,
66 MODE_RX,
68 MODE_TX,
69 };
70
71 #if defined(RADIOLIB_BUILD_ARDUINO)
79 Module(uint32_t cs, uint32_t irq, uint32_t rst, uint32_t gpio = RADIOLIB_NC);
80
90 Module(uint32_t cs, uint32_t irq, uint32_t rst, uint32_t gpio, SPIClass& spi, SPISettings spiSettings = RADIOLIB_DEFAULT_SPI_SETTINGS);
91 #endif
92
101 Module(RadioLibHal *hal, uint32_t cs, uint32_t irq, uint32_t rst, uint32_t gpio = RADIOLIB_NC);
102
107 Module(const Module& mod);
108
113 Module& operator=(const Module& mod);
114
115 // public member variables
119 RadioLibHal* hal = NULL;
120
124 uint8_t SPIreadCommand = 0b00000000;
125
129 uint8_t SPIwriteCommand = 0b10000000;
130
134 uint8_t SPInopCommand = 0x00;
135
139 uint8_t SPIstatusCommand = 0x00;
140
144 uint8_t SPIaddrWidth = 8;
145
150 bool SPIstreamType = false;
151
155 int16_t SPIstreamError = RADIOLIB_ERR_UNKNOWN;
156
160 typedef int16_t (*SPIparseStatusCb_t)(uint8_t in);
161
166 SPIparseStatusCb_t SPIparseStatusCb = nullptr;
167
168 #if defined(RADIOLIB_INTERRUPT_TIMING)
169
173 typedef void (*TimerSetupCb_t)(uint32_t len);
174
178 TimerSetupCb_t TimerSetupCb = nullptr;
179
183 volatile bool TimerFlag = false;
184
185 #endif
186
187 // basic methods
188
192 void init();
193
197 void term();
198
199 // SPI methods
200
208 int16_t SPIgetRegValue(uint16_t reg, uint8_t msb = 7, uint8_t lsb = 0);
209
220 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);
221
228 void SPIreadRegisterBurst(uint16_t reg, size_t numBytes, uint8_t* inBytes);
229
235 uint8_t SPIreadRegister(uint16_t reg);
236
243 void SPIwriteRegisterBurst(uint16_t reg, uint8_t* data, size_t numBytes);
244
250 void SPIwriteRegister(uint16_t reg, uint8_t data);
251
260 void SPItransfer(uint8_t cmd, uint16_t reg, uint8_t* dataOut, uint8_t* dataIn, size_t numBytes);
261
266 int16_t SPIcheckStream();
267
277 int16_t SPIreadStream(uint8_t cmd, uint8_t* data, size_t numBytes, bool waitForGpio = true, bool verify = true);
278
289 int16_t SPIreadStream(uint8_t* cmd, uint8_t cmdLen, uint8_t* data, size_t numBytes, bool waitForGpio = true, bool verify = true);
290
300 int16_t SPIwriteStream(uint8_t cmd, uint8_t* data, size_t numBytes, bool waitForGpio = true, bool verify = true);
301
312 int16_t SPIwriteStream(uint8_t* cmd, uint8_t cmdLen, uint8_t* data, size_t numBytes, bool waitForGpio = true, bool verify = true);
313
326 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);
327
328 // pin number access methods
329
334 uint32_t getCs() const { return(csPin); }
335
340 uint32_t getIrq() const { return(irqPin); }
341
346 uint32_t getRst() const { return(rstPin); }
347
352 uint32_t getGpio() const { return(gpioPin); }
353
370 void setRfSwitchPins(uint32_t rxEn, uint32_t txEn);
371
434 void setRfSwitchTable(const uint32_t (&pins)[RFSWITCH_MAX_PINS], const RfSwitchMode_t table[]);
435
443 const RfSwitchMode_t *findRfSwitchMode(uint8_t mode) const;
444
449 void setRfSwitchState(uint8_t mode);
450
458 void waitForMicroseconds(uint32_t start, uint32_t len);
459
463 static uint8_t flipBits(uint8_t b);
464
468 static uint16_t flipBits16(uint16_t i);
469
477 static void hexdump(uint8_t* data, size_t len, uint32_t offset = 0, uint8_t width = 1, bool be = false);
478
484 void regdump(uint16_t start, size_t len);
485
486 #if defined(RADIOLIB_DEBUG) and defined(RADIOLIB_BUILD_ARDUINO)
487 static size_t serialPrintf(const char* format, ...);
488 #endif
489
490 #if !defined(RADIOLIB_GODMODE)
491 private:
492 #endif
493 uint32_t csPin = RADIOLIB_NC;
494 uint32_t irqPin = RADIOLIB_NC;
495 uint32_t rstPin = RADIOLIB_NC;
496 uint32_t gpioPin = RADIOLIB_NC;
497
498 // RF switch pins and table
499 uint32_t rfSwitchPins[RFSWITCH_MAX_PINS] = { RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC };
500 const RfSwitchMode_t *rfSwitchTable = nullptr;
501
502 #if defined(RADIOLIB_INTERRUPT_TIMING)
503 uint32_t prevTimingLen = 0;
504 #endif
505 };
506
507 #endif
Module
Implements all common low-level methods to control the wireless module. Every module class contains o...
Definition: Module.h:28
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:54
Module::SPIstatusCommand
uint8_t SPIstatusCommand
Basic SPI status read command. Defaults to 0x00.
Definition: Module.h:139
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:64
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:39
Module::flipBits16
static uint16_t flipBits16(uint16_t i)
Function to reflect bits within an integer.
Definition: Module.cpp:382
Module::SPIstreamType
bool SPIstreamType
Whether the SPI interface is stream-type (e.g. SX126x) or register-type (e.g. SX127x)....
Definition: Module.h:150
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:132
Module::hal
RadioLibHal * hal
Hardware abstraction layer to be used.
Definition: Module.h:119
Module::term
void term()
Terminate low-level module control.
Definition: Module.cpp:49
Module::SPInopCommand
uint8_t SPInopCommand
Basic SPI no-operation command. Defaults to 0x00.
Definition: Module.h:134
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:357
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:352
Module::findRfSwitchMode
const RfSwitchMode_t * findRfSwitchMode(uint8_t mode) const
Find a mode in the RfSwitchTable.
Definition: Module.cpp:497
Module::OpMode_t
OpMode_t
Definition: Module.h:58
Module::MODE_TX
@ MODE_TX
Definition: Module.h:68
Module::MODE_IDLE
@ MODE_IDLE
Definition: Module.h:64
Module::MODE_RX
@ MODE_RX
Definition: Module.h:66
Module::MODE_END_OF_TABLE
@ MODE_END_OF_TABLE
Definition: Module.h:62
Module::SPIcheckStream
int16_t SPIcheckStream()
Method to check the result of last SPI stream transfer.
Definition: Module.cpp:224
Module::SPIstreamError
int16_t SPIstreamError
The last recorded SPI stream error.
Definition: Module.h:155
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:207
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:141
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:474
Module::flipBits
static uint8_t flipBits(uint8_t b)
Function to reflect bits within a byte.
Definition: Module.cpp:375
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:124
Module::SPIreadRegisterBurst
void SPIreadRegisterBurst(uint16_t reg, size_t numBytes, uint8_t *inBytes)
SPI burst read method.
Definition: Module.cpp:103
Module::SPIparseStatusCb_t
int16_t(* SPIparseStatusCb_t)(uint8_t in)
SPI status parsing callback typedef.
Definition: Module.h:160
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:112
Module::getCs
uint32_t getCs() const
Access method to get the pin number of SPI chip select.
Definition: Module.h:334
Module::getIrq
uint32_t getIrq() const
Access method to get the pin number of interrupt/GPIO.
Definition: Module.h:340
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:190
Module::regdump
void regdump(uint16_t start, size_t len)
Function to dump device registers as hex into the debug port.
Definition: Module.cpp:435
Module::SPIaddrWidth
uint8_t SPIaddrWidth
SPI address width. Defaults to 8, currently only supports 8 and 16-bit addresses.
Definition: Module.h:144
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:490
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:243
Module::hexdump
static void hexdump(uint8_t *data, size_t len, uint32_t offset=0, uint8_t width=1, bool be=false)
Function to dump data as hex into the debug port.
Definition: Module.cpp:390
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:346
Module::SPIwriteCommand
uint8_t SPIwriteCommand
Basic SPI write command. Defaults to 0x80.
Definition: Module.h:129
Module::SPIwriteRegisterBurst
void SPIwriteRegisterBurst(uint16_t reg, uint8_t *data, size_t numBytes)
SPI burst write method.
Definition: Module.cpp:123
Module::SPIparseStatusCb
SPIparseStatusCb_t SPIparseStatusCb
Callback to function that will parse the module-specific status codes to RadioLib status codes....
Definition: Module.h:166
Module::setRfSwitchState
void setRfSwitchState(uint8_t mode)
Set RF switch state.
Definition: Module.cpp:507
RadioLibHal
Definition: Hal.h:11
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:46