RadioLib
Universal wireless communication library for Arduino
Module Class Reference

Implements all common low-level methods to control the wireless module. Every module class contains one private instance of this class. More...

#include <Module.h>

Inheritance diagram for Module:
STM32WLx_Module

Classes

struct  RfSwitchMode_t
 

Public Types

enum  OpMode_t { MODE_END_OF_TABLE = 0 , MODE_IDLE , MODE_RX , MODE_TX }
 
typedef int16_t(* SPIparseStatusCb_t) (uint8_t in)
 SPI status parsing callback typedef.
 

Public Member Functions

 Module (RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE gpio=RADIOLIB_NC)
 Default constructor. More...
 
 Module (const Module &mod)
 Copy constructor. More...
 
Moduleoperator= (const Module &mod)
 Overload for assignment operator. More...
 
void init ()
 Initialize low-level module control.
 
void term ()
 Terminate low-level module control.
 
int16_t SPIgetRegValue (uint8_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. More...
 
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)
 Overwrite-safe SPI write method with verification. This method is the preferred SPI write mechanism. More...
 
void SPIreadRegisterBurst (uint8_t reg, uint8_t numBytes, uint8_t *inBytes)
 SPI burst read method. More...
 
uint8_t SPIreadRegister (uint8_t reg)
 SPI basic read method. Use of this method is reserved for special cases, SPIgetRegValue should be used instead. More...
 
void SPIwriteRegisterBurst (uint8_t reg, uint8_t *data, uint8_t numBytes)
 SPI burst write method. More...
 
void SPIwriteRegister (uint8_t reg, uint8_t data)
 SPI basic write method. Use of this method is reserved for special cases, SPIsetRegValue should be used instead. More...
 
void SPItransfer (uint8_t cmd, uint8_t reg, uint8_t *dataOut, uint8_t *dataIn, uint8_t numBytes)
 SPI single transfer method. More...
 
int16_t SPItransferStream (uint8_t *cmd, uint8_t cmdLen, bool write, uint8_t *dataOut, uint8_t *dataIn, uint8_t numBytes, bool waitForGpio, uint32_t timeout)
 SPI single transfer method for modules with stream-type SPI interface (SX126x, SX128x etc.). More...
 
RADIOLIB_PIN_TYPE getCs () const
 Access method to get the pin number of SPI chip select. More...
 
RADIOLIB_PIN_TYPE getIrq () const
 Access method to get the pin number of interrupt/GPIO. More...
 
RADIOLIB_PIN_TYPE getRst () const
 Access method to get the pin number of hardware reset pin. More...
 
RADIOLIB_PIN_TYPE getGpio () const
 Access method to get the pin number of second interrupt/GPIO. More...
 
void setRfSwitchPins (RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn)
 Some modules contain external RF switch controlled by pins. This function gives RadioLib control over those pins to automatically switch between various modes: When idle both pins will be LOW, during TX the txEn pin will be HIGH, during RX the rxPin will be HIGH. More...
 
void setRfSwitchTable (const RADIOLIB_PIN_TYPE(&pins)[RFSWITCH_MAX_PINS], const RfSwitchMode_t table[])
 Some modules contain external RF switch controlled by pins. This function gives RadioLib control over those pins to automatically switch between various modes. More...
 
const RfSwitchMode_tfindRfSwitchMode (uint8_t mode) const
 Find a mode in the RfSwitchTable. More...
 
void setRfSwitchState (uint8_t mode)
 Set RF switch state. More...
 
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 timing mode, it is up to the user to set up the timing interrupt! More...
 
void pinMode (RADIOLIB_PIN_TYPE pin, RADIOLIB_PIN_MODE mode)
 Arduino core pinMode override that checks RADIOLIB_NC as alias for unused pin. More...
 
void digitalWrite (RADIOLIB_PIN_TYPE pin, RADIOLIB_PIN_STATUS value)
 Arduino core digitalWrite override that checks RADIOLIB_NC as alias for unused pin. More...
 
RADIOLIB_PIN_STATUS digitalRead (RADIOLIB_PIN_TYPE pin)
 Arduino core digitalWrite override that checks RADIOLIB_NC as alias for unused pin. More...
 
void tone (RADIOLIB_PIN_TYPE pin, uint16_t value, uint32_t duration=0)
 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. More...
 
void noTone (RADIOLIB_PIN_TYPE pin)
 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. More...
 
void attachInterrupt (RADIOLIB_PIN_TYPE interruptNum, void(*userFunc)(void), RADIOLIB_INTERRUPT_STATUS mode)
 Arduino core attachInterrupt override. More...
 
void detachInterrupt (RADIOLIB_PIN_TYPE interruptNum)
 Arduino core detachInterrupt override. More...
 
void yield ()
 Arduino core yield override.
 
void delay (uint32_t ms)
 Arduino core delay override. More...
 
void delayMicroseconds (uint32_t us)
 Arduino core delayMicroseconds override. More...
 
uint32_t millis ()
 Arduino core millis override.
 
uint32_t micros ()
 Arduino core micros override.
 
uint32_t pulseIn (RADIOLIB_PIN_TYPE pin, RADIOLIB_PIN_STATUS state, uint32_t timeout)
 Arduino core pulseIn override.
 
void begin ()
 Arduino core SPI begin override.
 
void beginTransaction ()
 Arduino core SPI beginTransaction override.
 
uint8_t transfer (uint8_t b)
 Arduino core SPI transfer override.
 
void endTransaction ()
 Arduino core SPI endTransaction override.
 
void end ()
 Arduino core SPI end override.
 
virtual void SPIbeginTransaction ()
 
virtual uint8_t SPItransfer (uint8_t b)
 
virtual void SPIendTransaction ()
 
void regdump (uint8_t start, uint8_t len)
 Function to dump device registers as hex into the debug port. More...
 

Static Public Member Functions

static uint8_t flipBits (uint8_t b)
 Function to reflect bits within a byte.
 
static uint16_t flipBits16 (uint16_t i)
 Function to reflect bits within an integer.
 
static void hexdump (uint8_t *data, size_t len)
 Function to dump data as hex into the debug port. More...
 

Public Attributes

uint8_t SPIreadCommand = 0b00000000
 Basic SPI read command. Defaults to 0x00.
 
uint8_t SPIwriteCommand = 0b10000000
 Basic SPI write command. Defaults to 0x80.
 
bool SPIstreamType = false
 Whether the SPI interface is stream-type (e.g. SX126x) or register-type (e.g. SX127x). Defaults to register-type SPI interfaces.
 
SPIparseStatusCb_t SPIparseStatusCb = nullptr
 Callback to function that will parse the module-specific status codes to RadioLib status codes. Typically used for modules with SPI stream-type interface (e.g. SX126x/SX128x).
 

Static Public Attributes

static const size_t RFSWITCH_MAX_PINS = 3
 The maximum number of pins supported by the RF switch code. More...
 
static const RfSwitchMode_t END_OF_MODE_TABLE
 

Detailed Description

Implements all common low-level methods to control the wireless module. Every module class contains one private instance of this class.

Member Enumeration Documentation

◆ OpMode_t

Constants to use in a mode table set be setRfSwitchTable. These constants work for most radios, but some radios define their own constants to be used instead.

See setRfSwitchTable() for details.

Enumerator
MODE_END_OF_TABLE 

End of table marker, use END_OF_MODE_TABLE constant instead. Value is zero to ensure zero-initialized mode ends the table

MODE_IDLE 

Idle mode

MODE_RX 

Receive mode

MODE_TX 

Transmission mode

Constructor & Destructor Documentation

◆ Module() [1/2]

Module::Module ( RADIOLIB_PIN_TYPE  cs,
RADIOLIB_PIN_TYPE  irq,
RADIOLIB_PIN_TYPE  rst,
RADIOLIB_PIN_TYPE  gpio = RADIOLIB_NC 
)

Default constructor.

Parameters
csPin to be used as chip select.
irqPin to be used as interrupt/GPIO.
rstPin to be used as hardware reset for the module.
gpioPin to be used as additional interrupt/GPIO.

◆ Module() [2/2]

Module::Module ( const Module mod)

Copy constructor.

Parameters
modModule instance to copy.

Member Function Documentation

◆ attachInterrupt()

void Module::attachInterrupt ( RADIOLIB_PIN_TYPE  interruptNum,
void(*)(void)  userFunc,
RADIOLIB_INTERRUPT_STATUS  mode 
)

Arduino core attachInterrupt override.

Parameters
interruptNumInterrupt number.
userFuncInterrupt service routine.
modePin hcange direction.

◆ delay()

void Module::delay ( uint32_t  ms)

Arduino core delay override.

Parameters
msDelay length in milliseconds.

◆ delayMicroseconds()

void Module::delayMicroseconds ( uint32_t  us)

Arduino core delayMicroseconds override.

Parameters
usDelay length in microseconds.

◆ detachInterrupt()

void Module::detachInterrupt ( RADIOLIB_PIN_TYPE  interruptNum)

Arduino core detachInterrupt override.

Parameters
interruptNumInterrupt number.

◆ digitalRead()

RADIOLIB_PIN_STATUS Module::digitalRead ( RADIOLIB_PIN_TYPE  pin)

Arduino core digitalWrite override that checks RADIOLIB_NC as alias for unused pin.

Parameters
pinPin to read from.
Returns
Pin value.

◆ digitalWrite()

void Module::digitalWrite ( RADIOLIB_PIN_TYPE  pin,
RADIOLIB_PIN_STATUS  value 
)

Arduino core digitalWrite override that checks RADIOLIB_NC as alias for unused pin.

Parameters
pinPin to write to.
valueWhether to set the pin high or low.

◆ findRfSwitchMode()

const Module::RfSwitchMode_t * Module::findRfSwitchMode ( uint8_t  mode) const

Find a mode in the RfSwitchTable.

Parameters
Themode to find.
Returns
A pointer to the RfSwitchMode_t struct in the table that matches the passed mode. Returns nullptr if no rfswitch pins are configured, or the passed mode is not listed in the table.

◆ getCs()

RADIOLIB_PIN_TYPE Module::getCs ( ) const
inline

Access method to get the pin number of SPI chip select.

Returns
Pin number of SPI chip select configured in the constructor.

◆ getGpio()

RADIOLIB_PIN_TYPE Module::getGpio ( ) const
inline

Access method to get the pin number of second interrupt/GPIO.

Returns
Pin number of second interrupt/GPIO configured in the constructor.

◆ getIrq()

RADIOLIB_PIN_TYPE Module::getIrq ( ) const
inline

Access method to get the pin number of interrupt/GPIO.

Returns
Pin number of interrupt/GPIO configured in the constructor.

◆ getRst()

RADIOLIB_PIN_TYPE Module::getRst ( ) const
inline

Access method to get the pin number of hardware reset pin.

Returns
Pin number of hardware reset pin configured in the constructor.

◆ hexdump()

void Module::hexdump ( uint8_t *  data,
size_t  len 
)
static

Function to dump data as hex into the debug port.

Parameters
dataData to dump.
lenNumber of bytes to dump.

◆ noTone()

void Module::noTone ( RADIOLIB_PIN_TYPE  pin)

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.

Parameters
pinPin to write to.

◆ operator=()

Module & Module::operator= ( const Module mod)

Overload for assignment operator.

Parameters
framervalue Module.

◆ pinMode()

void Module::pinMode ( RADIOLIB_PIN_TYPE  pin,
RADIOLIB_PIN_MODE  mode 
)

Arduino core pinMode override that checks RADIOLIB_NC as alias for unused pin.

Parameters
pinPin to change the mode of.
modeWhich mode to set.

◆ regdump()

void Module::regdump ( uint8_t  start,
uint8_t  len 
)

Function to dump device registers as hex into the debug port.

Parameters
startFirst address to dump.
lenNumber of bytes to dump.

◆ setRfSwitchPins()

void Module::setRfSwitchPins ( RADIOLIB_PIN_TYPE  rxEn,
RADIOLIB_PIN_TYPE  txEn 
)

Some modules contain external RF switch controlled by pins. This function gives RadioLib control over those pins to automatically switch between various modes: When idle both pins will be LOW, during TX the txEn pin will be HIGH, during RX the rxPin will be HIGH.

Radiolib will automatically set the pin mode and value of these pins, so do not control them from the sketch.

When more than two pins or more control over the output values are needed, use the setRfSwitchTable() function.

Parameters
rxEnRX enable pin.
txEnTX enable pin.

◆ setRfSwitchState()

void Module::setRfSwitchState ( uint8_t  mode)

Set RF switch state.

Parameters
modeThe mode to set. This must be one of the MODE_ constants, or a radio-specific constant.

◆ setRfSwitchTable()

void Module::setRfSwitchTable ( const RADIOLIB_PIN_TYPE(&)  pins[RFSWITCH_MAX_PINS],
const RfSwitchMode_t  table[] 
)

Some modules contain external RF switch controlled by pins. This function gives RadioLib control over those pins to automatically switch between various modes.

Radiolib will automatically set the pin mode and value of these pins, so do not control them from the sketch.

Parameters
pinsA reference to an array of pins to control. This should always be an array of 3 elements. If you need less pins, use RADIOLIB_NC for the unused elements.
tableA reference to an array of pin values to use for each supported mode. Each element is an RfSwitchMode_T struct that lists the mode for which it applies and the values for each of the pins passed in the pins argument respectively.

The pins array will be copied into the Module object, so the original array can be deallocated after this call. However, a reference to the table array will be stored, so that array must remain valid as long RadioLib is being used.

The mode field in each table row should normally use any of the MODE_* constants from the Module::OpMode_t enum. However, some radios support additional modes and will define their own OpMode_t enum.

The length of the table is variable (to support radios that add additional modes), so the table must always be terminated with the special END_OF_MODE_TABLE value.

Normally all modes should be listed in the table, but for some radios, modes can be omitted to indicate they are not supported (e.g. when a radio has a high power and low power TX mode but external circuitry only supports low power). If applicable, this is documented in the radio class itself.

Example

For example, on a board that has an RF switch with an enable pin connected to PA0 and a TX/RX select pin connected to PA1:

// In global scope, define the pin array and mode table
static const RADIOLIB_PIN_TYPE rfswitch_pins[] =
{PA0, PA1, RADIOLIB_NC};
static const Module::RfSwitchMode_t rfswitch_table[] = {
{Module::MODE_IDLE, {LOW, LOW}},
{Module::MODE_RX, {HIGH, LOW}},
{Module::MODE_TX, {HIGH, HIGH}},
};
void setup() {
...
// Then somewhere in setup, pass them to radiolib
radio.setRfSwitchTable(rfswitch_pins, rfswitch_table);
...
}
static const RfSwitchMode_t END_OF_MODE_TABLE
Definition: Module.h:65
@ MODE_TX
Definition: Module.h:56
@ MODE_IDLE
Definition: Module.h:52
@ MODE_RX
Definition: Module.h:54
Definition: Module.h:34

◆ SPIgetRegValue()

int16_t Module::SPIgetRegValue ( uint8_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.

Parameters
regAddress of SPI register to read.
msbMost significant bit of the register variable. Bits above this one will be masked out.
lsbLeast significant bit of the register variable. Bits below this one will be masked out.
Returns
Masked register value or status code.

◆ SPIreadRegister()

uint8_t Module::SPIreadRegister ( uint8_t  reg)

SPI basic read method. Use of this method is reserved for special cases, SPIgetRegValue should be used instead.

Parameters
regAddress of SPI register to read.
Returns
Value that was read from register.

◆ SPIreadRegisterBurst()

void Module::SPIreadRegisterBurst ( uint8_t  reg,
uint8_t  numBytes,
uint8_t *  inBytes 
)

SPI burst read method.

Parameters
regAddress of SPI register to read.
numBytesNumber of bytes that will be read.
inBytesPointer to array that will hold the read data.

◆ SPIsetRegValue()

int16_t Module::SPIsetRegValue ( uint8_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.

Parameters
regAddress of SPI register to write.
valueSingle byte value that will be written to the SPI register.
msbMost significant bit of the register variable. Bits above this one will not be affected by the write operation.
lsbLeast significant bit of the register variable. Bits below this one will not be affected by the write operation.
checkIntervalNumber of milliseconds between register writing and verification reading. Some registers need up to 10ms to process the change.
checkMaskMask of bits to check, only bits set to 1 will be verified.
Returns
Status Codes

◆ SPItransfer()

void Module::SPItransfer ( uint8_t  cmd,
uint8_t  reg,
uint8_t *  dataOut,
uint8_t *  dataIn,
uint8_t  numBytes 
)

SPI single transfer method.

Parameters
cmdSPI access command (read/write/burst/...).
regAddress of SPI register to transfer to/from.
dataOutData that will be transfered from master to slave.
dataInData that was transfered from slave to master.
numBytesNumber of bytes to transfer.

◆ SPItransferStream()

int16_t Module::SPItransferStream ( uint8_t *  cmd,
uint8_t  cmdLen,
bool  write,
uint8_t *  dataOut,
uint8_t *  dataIn,
uint8_t  numBytes,
bool  waitForGpio,
uint32_t  timeout 
)

SPI single transfer method for modules with stream-type SPI interface (SX126x, SX128x etc.).

Parameters
cmdSPI operation command.
cmdSPI command length in bytes.
writeSet to true for write commands, false for read commands.
dataOutData that will be transfered from master to slave.
dataInData that was transfered from slave to master.
numBytesNumber of bytes to transfer.
waitForGpioWhether to wait for some GPIO at the end of transfer (e.g. BUSY line on SX126x/SX128x).
timeoutGPIO wait period timeout in milliseconds.
Returns
Status Codes

◆ SPIwriteRegister()

void Module::SPIwriteRegister ( uint8_t  reg,
uint8_t  data 
)

SPI basic write method. Use of this method is reserved for special cases, SPIsetRegValue should be used instead.

Parameters
regAddress of SPI register to write.
dataValue that will be written to the register.

◆ SPIwriteRegisterBurst()

void Module::SPIwriteRegisterBurst ( uint8_t  reg,
uint8_t *  data,
uint8_t  numBytes 
)

SPI burst write method.

Parameters
regAddress of SPI register to write.
dataPointer to array that holds the data that will be written.
numBytesNumber of bytes that will be written.

◆ tone()

void Module::tone ( RADIOLIB_PIN_TYPE  pin,
uint16_t  value,
uint32_t  duration = 0 
)

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.

Parameters
pinPin to write to.
valueFrequency to output.

◆ waitForMicroseconds()

void Module::waitForMicroseconds ( uint32_t  start,
uint32_t  len 
)

Wait for time to elapse, either using the microsecond timer, or the TimerFlag. Note that in interrupt timing mode, it is up to the user to set up the timing interrupt!

Parameters
startWaiting start timestamp, in microseconds.
lenWaiting duration, in microseconds;

Member Data Documentation

◆ END_OF_MODE_TABLE

const RfSwitchMode_t Module::END_OF_MODE_TABLE
static

Value to use as the last element in a mode table to indicate the end of the table.

See setRfSwitchTable() for details.

◆ RFSWITCH_MAX_PINS

const size_t Module::RFSWITCH_MAX_PINS = 3
static

The maximum number of pins supported by the RF switch code.

Note: It is not recommended to use this constant in your sketch when defining a rfswitch pins array, to prevent issues when this value is ever increased and such an array gets extra zero elements (that will be interpreted as pin 0).


The documentation for this class was generated from the following files: