# 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, {} }
// default timeout for SPI transfers
#define RADIOLIB_MODULE_SPI_TIMEOUT (1000)
class Module {
public:
static const size_t RFSWITCH_MAX_PINS = 3;
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;
uint8_t SPIreadCommand = 0b00000000;
uint8_t SPIwriteCommand = 0b10000000;
uint8_t SPInopCommand = 0x00;
uint8_t SPIstatusCommand = 0x00;
uint8_t SPIaddrWidth = 8;
bool SPIstreamType = false;
int16_t SPIstreamError = RADIOLIB_ERR_UNKNOWN;
typedef int16_t (*SPIparseStatusCb_t)(uint8_t in);
SPIparseStatusCb_t SPIparseStatusCb = nullptr;
#if defined(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(uint16_t reg, uint8_t msb = 7, uint8_t lsb = 0);
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);
void SPIreadRegisterBurst(uint16_t reg, size_t numBytes, uint8_t* inBytes);
uint8_t SPIreadRegister(uint16_t reg);
void SPIwriteRegisterBurst(uint16_t reg, uint8_t* data, size_t numBytes);
void SPIwriteRegister(uint16_t reg, uint8_t data);
void SPItransfer(uint8_t cmd, uint16_t reg, uint8_t* dataOut, uint8_t* dataIn, size_t numBytes);
int16_t SPIcheckStream();
int16_t SPIreadStream(uint8_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(uint8_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(uint8_t* cmd, uint8_t cmdLen, bool write, uint8_t* dataOut, uint8_t* dataIn, size_t numBytes, bool waitForGpio, uint32_t timeout);
// 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(uint32_t start, uint32_t len);
static uint32_t reflect(uint32_t in, uint8_t bits);
static void hexdump(uint8_t* data, size_t len, uint32_t offset = 0, uint8_t width = 1, bool be = false);
void regdump(uint16_t start, size_t len);
#if defined(RADIOLIB_DEBUG) and defined(RADIOLIB_BUILD_ARDUINO)
static size_t serialPrintf(const char* format, ...);
#endif
#if !defined(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 };
const RfSwitchMode_t *rfSwitchTable = nullptr;
#if defined(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: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:54
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: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: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:132
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:49
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:376
Module::reflect
static uint32_t reflect(uint32_t in, uint8_t bits)
Function to reflect bits within a byte.
Definition: Module.cpp:394
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:509
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:236
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:219
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:486
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:103
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:112
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:202
Module::regdump
void regdump(uint16_t start, size_t len)
Function to dump device registers as hex into the debug port.
Definition: Module.cpp:447
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:502
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:255
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:402
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:123
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:519
RadioLibHal
Hardware abstraction library base interface.
Definition: Hal.h:35
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