# RadioLib: src/Module.h Source File

Module.h
#if !defined(_RADIOLIB_MODULE_H)
#define _RADIOLIB_MODULE_H
#include "TypeDef.h"
#include "Hal.h"
#if defined(RADIOLIB_BUILD_ARDUINO)
#include <SPI.h>
#endif
#if defined(STM32WLxx)
#include <SubGhz.h>
#endif
#define END_OF_MODE_TABLE { Module::MODE_END_OF_TABLE, {} }
#define RADIOLIB_MODULE_SPI_COMMAND_READ (0)
#define RADIOLIB_MODULE_SPI_COMMAND_WRITE (1)
#define RADIOLIB_MODULE_SPI_COMMAND_NOP (2)
#define RADIOLIB_MODULE_SPI_COMMAND_STATUS (3)
#define RADIOLIB_MODULE_SPI_WIDTH_ADDR (0)
#define RADIOLIB_MODULE_SPI_WIDTH_CMD (1)
#define RADIOLIB_MODULE_SPI_WIDTH_STATUS (2)
class Module {
public:
static const size_t RFSWITCH_MAX_PINS = 5;
struct RfSwitchMode_t {
uint8_t mode;
uint32_t values[RFSWITCH_MAX_PINS];
};
enum OpMode_t {
MODE_END_OF_TABLE = 0,
MODE_IDLE,
MODE_RX,
MODE_TX,
};
#if defined(RADIOLIB_BUILD_ARDUINO)
Module(uint32_t cs, uint32_t irq, uint32_t rst, uint32_t gpio = RADIOLIB_NC);
Module(uint32_t cs, uint32_t irq, uint32_t rst, uint32_t gpio, SPIClass& spi, SPISettings spiSettings = RADIOLIB_DEFAULT_SPI_SETTINGS);
#endif
Module(RadioLibHal *hal, uint32_t cs, uint32_t irq, uint32_t rst, uint32_t gpio = RADIOLIB_NC);
Module(const Module& mod);
Module& operator=(const Module& mod);
// public member variables
RadioLibHal* hal = NULL;
typedef int16_t (*SPIparseStatusCb_t)(uint8_t in);
typedef int16_t (*SPIcheckStatusCb_t)(Module* mod);
enum BitWidth_t {
BITS_0 = 0,
BITS_8 = 8,
BITS_16 = 16,
BITS_32 = 32,
};
struct SPIConfig_t {
bool stream;
int16_t err;
uint16_t cmds[4];
BitWidth_t widths[3];
uint8_t statusPos;
SPIparseStatusCb_t parseStatusCb;
SPIcheckStatusCb_t checkStatusCb;
RadioLibTime_t timeout;
};
SPIConfig_t spiConfig = {
.stream = false,
.err = RADIOLIB_ERR_UNKNOWN,
.cmds = { 0x00, 0x80, 0x00, 0x00 },
.widths = { Module::BITS_8, Module::BITS_0, Module::BITS_8 },
.statusPos = 0,
.parseStatusCb = nullptr,
.checkStatusCb = nullptr,
.timeout = 1000,
};
#if RADIOLIB_INTERRUPT_TIMING
typedef void (*TimerSetupCb_t)(uint32_t len);
TimerSetupCb_t TimerSetupCb = nullptr;
volatile bool TimerFlag = false;
#endif
// basic methods
void init();
void term();
// SPI methods
int16_t SPIgetRegValue(uint32_t reg, uint8_t msb = 7, uint8_t lsb = 0);
int16_t SPIsetRegValue(uint32_t reg, uint8_t value, uint8_t msb = 7, uint8_t lsb = 0, uint8_t checkInterval = 2, uint8_t checkMask = 0xFF);
void SPIreadRegisterBurst(uint32_t reg, size_t numBytes, uint8_t* inBytes);
uint8_t SPIreadRegister(uint32_t reg);
void SPIwriteRegisterBurst(uint32_t reg, uint8_t* data, size_t numBytes);
void SPIwriteRegister(uint32_t reg, uint8_t data);
void SPItransfer(uint16_t cmd, uint32_t reg, uint8_t* dataOut, uint8_t* dataIn, size_t numBytes);
int16_t SPIcheckStream();
int16_t SPIreadStream(uint16_t cmd, uint8_t* data, size_t numBytes, bool waitForGpio = true, bool verify = true);
int16_t SPIreadStream(uint8_t* cmd, uint8_t cmdLen, uint8_t* data, size_t numBytes, bool waitForGpio = true, bool verify = true);
int16_t SPIwriteStream(uint16_t cmd, uint8_t* data, size_t numBytes, bool waitForGpio = true, bool verify = true);
int16_t SPIwriteStream(uint8_t* cmd, uint8_t cmdLen, uint8_t* data, size_t numBytes, bool waitForGpio = true, bool verify = true);
int16_t SPItransferStream(const uint8_t* cmd, uint8_t cmdLen, bool write, uint8_t* dataOut, uint8_t* dataIn, size_t numBytes, bool waitForGpio);
// pin number access methods
uint32_t getCs() const { return(csPin); }
uint32_t getIrq() const { return(irqPin); }
uint32_t getRst() const { return(rstPin); }
uint32_t getGpio() const { return(gpioPin); }
void setRfSwitchPins(uint32_t rxEn, uint32_t txEn);
void setRfSwitchTable(const uint32_t (&pins)[RFSWITCH_MAX_PINS], const RfSwitchMode_t table[]);
const RfSwitchMode_t *findRfSwitchMode(uint8_t mode) const;
void setRfSwitchState(uint8_t mode);
void waitForMicroseconds(RadioLibTime_t start, RadioLibTime_t len);
static uint32_t reflect(uint32_t in, uint8_t bits);
#if RADIOLIB_DEBUG
static void hexdump(const char* level, uint8_t* data, size_t len, uint32_t offset = 0, uint8_t width = 1, bool be = false);
void regdump(const char* level, uint16_t start, size_t len);
#endif
#if RADIOLIB_DEBUG and defined(RADIOLIB_BUILD_ARDUINO)
static size_t serialPrintf(const char* format, ...);
#endif
#if !RADIOLIB_GODMODE
private:
#endif
uint32_t csPin = RADIOLIB_NC;
uint32_t irqPin = RADIOLIB_NC;
uint32_t rstPin = RADIOLIB_NC;
uint32_t gpioPin = RADIOLIB_NC;
// RF switch pins and table
uint32_t rfSwitchPins[RFSWITCH_MAX_PINS] = { RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC };
const RfSwitchMode_t *rfSwitchTable = nullptr;
#if RADIOLIB_INTERRUPT_TIMING
uint32_t prevTimingLen = 0;
#endif
};
#endif
Module
Implements all common low-level methods to control the wireless module. Every module class contains o...
Definition: Module.h:65
Module::RFSWITCH_MAX_PINS
static const size_t RFSWITCH_MAX_PINS
The maximum number of pins supported by the RF switch code. Note: It is not recommended to use this c...
Definition: Module.h:74
Module::SPIcheckStatusCb_t
int16_t(* SPIcheckStatusCb_t)(Module *mod)
Callback for validation SPI status.
Definition: Module.h:166
Module::SPIwriteRegisterBurst
void SPIwriteRegisterBurst(uint32_t reg, uint8_t *data, size_t numBytes)
SPI burst write method.
Definition: Module.cpp:143
Module::SPIgetRegValue
int16_t SPIgetRegValue(uint32_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:55
Module::spiConfig
SPIConfig_t spiConfig
SPI configuration structure. The default configuration corresponds to register-access modules,...
Definition: Module.h:206
Module::SPItransferStream
int16_t SPItransferStream(const uint8_t *cmd, uint8_t cmdLen, bool write, uint8_t *dataOut, uint8_t *dataIn, size_t numBytes, bool waitForGpio)
SPI single transfer method for modules with stream-type SPI interface (SX126x, SX128x etc....
Definition: Module.cpp:316
Module::hal
RadioLibHal * hal
Hardware abstraction layer to be used.
Definition: Module.h:160
Module::term
void term()
Terminate low-level module control.
Definition: Module.cpp:50
Module::reflect
static uint32_t reflect(uint32_t in, uint8_t bits)
Function to reflect bits within a byte.
Definition: Module.cpp:462
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:400
Module::findRfSwitchMode
const RfSwitchMode_t * findRfSwitchMode(uint8_t mode) const
Find a mode in the RfSwitchTable.
Definition: Module.cpp:588
Module::OpMode_t
OpMode_t
Constants to use in a mode table set be setRfSwitchTable. These constants work for most radios,...
Definition: Module.h:97
Module::MODE_TX
@ MODE_TX
Transmission mode.
Definition: Module.h:111
Module::MODE_IDLE
@ MODE_IDLE
Idle mode.
Definition: Module.h:105
Module::MODE_RX
@ MODE_RX
Receive mode.
Definition: Module.h:108
Module::MODE_END_OF_TABLE
@ MODE_END_OF_TABLE
End of table marker, use END_OF_MODE_TABLE constant instead. Value is zero to ensure zero-initialized...
Definition: Module.h:102
Module::SPIcheckStream
int16_t SPIcheckStream()
Method to check the result of last SPI stream transfer.
Definition: Module.cpp:293
Module::SPIwriteRegister
void SPIwriteRegister(uint32_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:159
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:565
Module::waitForMicroseconds
void waitForMicroseconds(RadioLibTime_t start, RadioLibTime_t len)
Wait for time to elapse, either using the microsecond timer, or the TimerFlag. Note that in interrupt...
Definition: Module.cpp:444
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::SPIparseStatusCb_t
int16_t(* SPIparseStatusCb_t)(uint8_t in)
Callback for parsing SPI status.
Definition: Module.h:163
Module::getCs
uint32_t getCs() const
Access method to get the pin number of SPI chip select.
Definition: Module.h:382
Module::SPItransfer
void SPItransfer(uint16_t cmd, uint32_t reg, uint8_t *dataOut, uint8_t *dataIn, size_t numBytes)
SPI single transfer method.
Definition: Module.cpp:175
Module::SPIreadRegister
uint8_t SPIreadRegister(uint32_t reg)
SPI basic read method. Use of this method is reserved for special cases, SPIgetRegValue should be use...
Definition: Module.cpp:125
Module::getIrq
uint32_t getIrq() const
Access method to get the pin number of interrupt/GPIO.
Definition: Module.h:388
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:581
Module::SPIreadRegisterBurst
void SPIreadRegisterBurst(uint32_t reg, size_t numBytes, uint8_t *inBytes)
SPI burst read method.
Definition: Module.cpp:109
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:394
Module::SPIreadStream
int16_t SPIreadStream(uint16_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:237
Module::SPIsetRegValue
int16_t SPIsetRegValue(uint32_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:65
Module::SPIwriteStream
int16_t SPIwriteStream(uint16_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:265
Module::setRfSwitchState
void setRfSwitchState(uint8_t mode)
Set RF switch state.
Definition: Module.cpp:598
RadioLibHal
Hardware abstraction library base interface.
Definition: Hal.h:13
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
RadioLibTime_t
unsigned long RadioLibTime_t
Type used for durations in RadioLib.
Definition: TypeDef.h:621
Module::RfSwitchMode_t
Description of RF switch pin states for a single mode. See setRfSwitchTable for details.
Definition: Module.h:81
Module::RfSwitchMode_t::mode
uint8_t mode
RF switching mode, one of OpMode_t or a custom radio-defined value.
Definition: Module.h:83
Module::RfSwitchMode_t::values
uint32_t values[RFSWITCH_MAX_PINS]
Output pin values.
Definition: Module.h:86
Module::SPIConfig_t
SPI configuration structure.
Definition: Module.h:179
Module::SPIConfig_t::stream
bool stream
Whether the SPI module is stream-type (SX126x/8x) or registrer access type (SX127x,...
Definition: Module.h:181
Module::SPIConfig_t::timeout
RadioLibTime_t timeout
Timeout in ms when waiting for GPIO signals.
Definition: Module.h:202
Module::SPIConfig_t::cmds
uint16_t cmds[4]
SPI commands.
Definition: Module.h:187
Module::SPIConfig_t::statusPos
uint8_t statusPos
Byte position of status command in SPI stream.
Definition: Module.h:193
Module::SPIConfig_t::widths
BitWidth_t widths[3]
Bit widths of SPI addresses, commands and status bytes.
Definition: Module.h:190
Module::SPIConfig_t::parseStatusCb
SPIparseStatusCb_t parseStatusCb
Callback for parsing SPI status.
Definition: Module.h:196
Module::SPIConfig_t::checkStatusCb
SPIcheckStatusCb_t checkStatusCb
Callback for validation SPI status.
Definition: Module.h:199
Module::SPIConfig_t::err
int16_t err
Last recorded SPI error - only updated for modules that return status during SPI transfers.
Definition: Module.h:184