| SX126x-Arduino 2.0.1

Data Structures | Functions | Variables
radio.cpp File Reference

Radio driver API definition. More...

`#include <math.h>`
`#include <string.h>`
`#include "boards/mcu/board.h"`
`#include "radio/radio.h"`
`#include "sx126x.h"`
`#include "boards/sx126x/sx126x-board.h"`
`#include "boards/mcu/timer.h"`

## Data Structures
struct | FskBandwidth_t
struct | RadioPublicNetwork_t

## Functions
void | RadioInit (RadioEvents_t *events)
| Initializes the radio. More...
void | RadioReInit (RadioEvents_t *events)
| Initializes the radio after deep sleep of the CPU. More...
RadioState_t | RadioGetStatus (void)
void | RadioSetModem (RadioModems_t modem)
| Configures the radio with the given modem. More...
void | RadioSetChannel (uint32_t freq)
| Sets the channel frequency. More...
bool | RadioIsChannelFree (RadioModems_t modem, uint32_t freq, int16_t rssiThresh, uint32_t maxCarrierSenseTime)
| Checks if the channel is free for the given time. More...
uint32_t | RadioRandom (void)
| Generates a 32 bits random value based on the RSSI readings. More...
void | RadioSetRxConfig (RadioModems_t modem, uint32_t bandwidth, uint32_t datarate, uint8_t coderate, uint32_t bandwidthAfc, uint16_t preambleLen, uint16_t symbTimeout, bool fixLen, uint8_t payloadLen, bool crcOn, bool FreqHopOn, uint8_t HopPeriod, bool iqInverted, bool rxContinuous)
| Sets the reception parameters. More...
void | RadioSetTxConfig (RadioModems_t modem, int8_t power, uint32_t fdev, uint32_t bandwidth, uint32_t datarate, uint8_t coderate, uint16_t preambleLen, bool fixLen, bool crcOn, bool FreqHopOn, uint8_t HopPeriod, bool iqInverted, uint32_t timeout)
| Sets the transmission parameters. More...
bool | RadioCheckRfFrequency (uint32_t frequency)
| Checks if the given RF frequency is supported by the hardware. More...
uint32_t | RadioTimeOnAir (RadioModems_t modem, uint8_t pktLen)
| Computes the packet time on air in ms for the given payload. More...
void | RadioSend (uint8_t *buffer, uint8_t size)
| Sends the buffer of size. Prepares the packet to be sent and sets the radio in transmission. More...
void | RadioSleep (void)
| Sets the radio in sleep mode. More...
void | RadioStandby (void)
| Sets the radio in standby mode. More...
void | RadioRx (uint32_t timeout)
| Sets the radio in reception mode for the given time. More...
void | RadioSetCadParams (uint8_t cadSymbolNum, uint8_t cadDetPeak, uint8_t cadDetMin, uint8_t cadExitMode, uint32_t cadTimeout)
| Set Channel Activity Detection parameters. More...
void | RadioStartCad (void)
| Start a Channel Activity Detection. More...
void | RadioSetTxContinuousWave (uint32_t freq, int8_t power, uint16_t time)
| Sets the radio in continuous wave transmission mode. More...
int16_t | RadioRssi (RadioModems_t modem)
| Reads the current RSSI value. More...
void | RadioWrite (uint16_t addr, uint8_t data)
| Writes the radio register at the specified address. More...
uint8_t | RadioRead (uint16_t addr)
| Reads the radio register at the specified address. More...
void | RadioWriteBuffer (uint16_t addr, uint8_t *buffer, uint8_t size)
| Writes multiple radio registers starting at address. More...
void | RadioReadBuffer (uint16_t addr, uint8_t *buffer, uint8_t size)
| Reads multiple radio registers starting at address. More...
void | RadioSetMaxPayloadLength (RadioModems_t modem, uint8_t max)
| Sets the maximum payload length. More...
void | RadioSetPublicNetwork (bool enable)
| Sets the network to public or private. Updates the sync byte. More...
uint32_t | RadioGetWakeupTime (void)
| Gets the time required for the board plus radio to get out of sleep.[ms]. More...
void | RadioBgIrqProcess (void)
| Process radio irq in background task (nRF52 & ESP32) More...
void | RadioIrqProcess (void)
| Process radio irq. More...
void | RadioIrqProcessAfterDeepSleep (void)
| Process radio irq after deep sleep of the CPU. More...
void | RadioRxBoosted (uint32_t timeout)
| Sets the radio in reception mode with Max LNA gain for the given time. More...
void | RadioSetRxDutyCycle (uint32_t rxTime, uint32_t sleepTime)
| Sets the Rx duty cycle management parameters. More...
void | RadioOnDioIrq (void)
| DIO 0 IRQ callback. More...
void | RadioOnTxTimeoutIrq (void)
| Tx timeout timer callback. More...
void | RadioOnRxTimeoutIrq (void)
| Rx timeout timer callback. More...
static uint8_t | RadioGetFskBandwidthRegValue (uint32_t bandwidth)
void | RadioTx (uint32_t timeout)
void | RadioWriteFifo (uint8_t *buffer, uint8_t size)
void | RadioReadFifo (uint8_t *buffer, uint8_t size)

## Variables
TimerEvent_t | TxTimeoutTimer
TimerEvent_t | RxTimeoutTimer
const struct Radio_s | Radio
| Radio driver. More...
const FskBandwidth_t | FskBandwidths []
const RadioLoRaBandwidths_t | Bandwidths [] = {LORA_BW_125, LORA_BW_250, LORA_BW_500}
static double | RadioLoRaSymbTime [3][6]
uint8_t | MaxPayloadLength = 0xFF
uint32_t | TxTimeout = 0
uint32_t | RxTimeout = 0
bool | RxContinuous = false
PacketStatus_t | RadioPktStatus
uint8_t | RadioRxPayload [255]
bool | IrqFired = false
bool | TimerRxTimeout = false
bool | TimerTxTimeout = false
RadioModems_t | _modem
static RadioPublicNetwork_t | RadioPublicNetwork = {false}
static RadioEvents_t * | RadioEvents
SX126x_t | SX126x
| Radio hardware and global parameters. More...

## Detailed Description

Radio driver API definition.

Copyright
  Revised BSD License, see file LICENSE.

______ _
/ _____) _ | |
( (____ _____ ____ _| |_ _____ ____| |__
\____ \| ___ | (_ _) ___ |/ ___) _ \
_____) ) ____| | | || |_| ____( (___| | | |
(______/|_____)_|_|_| \__)_____)\____)_| |_|
(C)2013-2017 Semtech

Author
  Miguel Luis ( Semtech )
  Gregory Cristian ( Semtech )

## Function Documentation

## ◆ RadioInit()

void RadioInit | ( | RadioEvents_t * | events | ) |

Initializes the radio.

Parameters

  events | Structure containing the driver callback functions

## ◆ RadioReInit()

void RadioReInit | ( | RadioEvents_t * | events | ) |

Initializes the radio after deep sleep of the CPU.

Parameters

  events | Structure containing the driver callback functions

## ◆ RadioGetStatus()

RadioState_t RadioGetStatus | ( | void |  | ) |

Return current radio status

Return values

  Radio | status.[RF_IDLE, RF_RX_RUNNING, RF_TX_RUNNING]

## ◆ RadioSetModem()

void RadioSetModem | ( | RadioModems_t | modem | ) |

Configures the radio with the given modem.

Parameters

  modem | Modem to be used [0: FSK, 1: LoRa]

## ◆ RadioSetChannel()

void RadioSetChannel | ( | uint32_t | freq | ) |

Sets the channel frequency.

Parameters

  freq | Channel RF frequency

## ◆ RadioIsChannelFree()

bool RadioIsChannelFree | ( | RadioModems_t | modem,
|  | uint32_t | freq,
|  | int16_t | rssiThresh,
|  | uint32_t | maxCarrierSenseTime
| ) |  |

Checks if the channel is free for the given time.

Parameters

  modem | Radio modem to be used [0: FSK, 1: LoRa]
  freq | Channel RF frequency
  rssiThresh | RSSI threshold
  maxCarrierSenseTime | Max time while the RSSI is measured

Return values

  isFree | [true: Channel is free, false: Channel is not free]

## ◆ RadioRandom()

uint32_t RadioRandom | ( | void |  | ) |

Generates a 32 bits random value based on the RSSI readings.

Remarks
  This function sets the radio in LoRa modem mode and disables all interrupts. After calling this function either Radio.SetRxConfig or Radio.SetTxConfig functions must be called.

Return values

  randomValue | 32 bits random value

## ◆ RadioSetRxConfig()

void RadioSetRxConfig | ( | RadioModems_t | modem,
|  | uint32_t | bandwidth,
|  | uint32_t | datarate,
|  | uint8_t | coderate,
|  | uint32_t | bandwidthAfc,
|  | uint16_t | preambleLen,
|  | uint16_t | symbTimeout,
|  | bool | fixLen,
|  | uint8_t | payloadLen,
|  | bool | crcOn,
|  | bool | FreqHopOn,
|  | uint8_t | HopPeriod,
|  | bool | iqInverted,
|  | bool | rxContinuous
| ) |  |

Sets the reception parameters.

Parameters

  modem | Radio modem to be used [0: FSK, 1: LoRa]
  bandwidth | Sets the bandwidth FSK : >= 2600 and <= 250000 Hz LoRa: [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved]
  datarate | Sets the Datarate FSK : 600..300000 bits/s LoRa: [6: 64, 7: 128, 8: 256, 9: 512, 10: 1024, 11: 2048, 12: 4096 chips]
  coderate | Sets the coding rate (LoRa only) FSK : N/A ( set to 0 ) LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
  bandwidthAfc | Sets the AFC Bandwidth (FSK only) FSK : >= 2600 and <= 250000 Hz LoRa: N/A ( set to 0 )
  preambleLen | Sets the Preamble length FSK : Number of bytes LoRa: Length in symbols (the hardware adds 4 more symbols)
  symbTimeout | Sets the RxSingle timeout value FSK : timeout in number of bytes LoRa: timeout in symbols
  fixLen | Fixed length packets [0: variable, 1: fixed]
  payloadLen | Sets payload length when fixed length is used
  crcOn | Enables/Disables the CRC [0: OFF, 1: ON]
  FreqHopOn | Enables disables the intra-packet frequency hopping FSK : N/A ( set to 0 ) LoRa: [0: OFF, 1: ON]
  HopPeriod | Number of symbols between each hop FSK : N/A ( set to 0 ) LoRa: Number of symbols
  iqInverted | Inverts IQ signals (LoRa only) FSK : N/A ( set to 0 ) LoRa: [0: not inverted, 1: inverted]
  rxContinuous | Sets the reception in continuous mode [false: single mode, true: continuous mode]

## ◆ RadioSetTxConfig()

void RadioSetTxConfig | ( | RadioModems_t | modem,
|  | int8_t | power,
|  | uint32_t | fdev,
|  | uint32_t | bandwidth,
|  | uint32_t | datarate,
|  | uint8_t | coderate,
|  | uint16_t | preambleLen,
|  | bool | fixLen,
|  | bool | crcOn,
|  | bool | FreqHopOn,
|  | uint8_t | HopPeriod,
|  | bool | iqInverted,
|  | uint32_t | timeout
| ) |  |

Sets the transmission parameters.

Parameters

  modem | Radio modem to be used [0: FSK, 1: LoRa]
  power | Sets the output power [dBm]
  fdev | Sets the frequency deviation (FSK only) FSK : [Hz] LoRa: 0
  bandwidth | Sets the bandwidth (LoRa only) FSK : 0 LoRa: [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved]
  datarate | Sets the Datarate FSK : 600..300000 bits/s LoRa: [6: 64, 7: 128, 8: 256, 9: 512, 10: 1024, 11: 2048, 12: 4096 chips]
  coderate | Sets the coding rate (LoRa only) FSK : N/A ( set to 0 ) LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
  preambleLen | Sets the preamble length FSK : Number of bytes LoRa: Length in symbols (the hardware adds 4 more symbols)
  fixLen | Fixed length packets [0: variable, 1: fixed]
  crcOn | Enables disables the CRC [0: OFF, 1: ON]
  FreqHopOn | Enables disables the intra-packet frequency hopping FSK : N/A ( set to 0 ) LoRa: [0: OFF, 1: ON]
  HopPeriod | Number of symbols between each hop FSK : N/A ( set to 0 ) LoRa: Number of symbols
  iqInverted | Inverts IQ signals (LoRa only) FSK : N/A ( set to 0 ) LoRa: [0: not inverted, 1: inverted]
  timeout | Transmission timeout [ms]

## ◆ RadioCheckRfFrequency()

bool RadioCheckRfFrequency | ( | uint32_t | frequency | ) |

Checks if the given RF frequency is supported by the hardware.

Parameters

  frequency | RF frequency to be checked

Return values

  isSupported | [true: supported, false: unsupported]

## ◆ RadioTimeOnAir()

uint32_t RadioTimeOnAir | ( | RadioModems_t | modem,
|  | uint8_t | pktLen
| ) |  |

Computes the packet time on air in ms for the given payload.

Remarks
  Can only be called once SetRxConfig or SetTxConfig have been called

Parameters

  modem | Radio modem to be used [0: FSK, 1: LoRa]
  pktLen | Packet payload length

Return values

  airTime | Computed airTime (ms) for the given packet payload length

## ◆ RadioSend()

void RadioSend | ( | uint8_t * | buffer,
|  | uint8_t | size
| ) |  |

Sends the buffer of size. Prepares the packet to be sent and sets the radio in transmission.

Parameters

  | buffer Buffer pointer
  | size Buffer size
  buffer | Buffer pointer
  size | Buffer size

## ◆ RadioSleep()

void RadioSleep | ( | void |  | ) |

Sets the radio in sleep mode.

## ◆ RadioStandby()

void RadioStandby | ( | void |  | ) |

Sets the radio in standby mode.

## ◆ RadioRx()

void RadioRx | ( | uint32_t | timeout | ) |

Sets the radio in reception mode for the given time.

Parameters

  timeout | Reception timeout [ms] [0: continuous, others timeout]

## ◆ RadioSetCadParams()

void RadioSetCadParams | ( | uint8_t | cadSymbolNum,
|  | uint8_t | cadDetPeak,
|  | uint8_t | cadDetMin,
|  | uint8_t | cadExitMode,
|  | uint32_t | cadTimeout
| ) |  |

Set Channel Activity Detection parameters.

Parameters

  cadSymbolNum | The number of symbol to use for CAD operations [LORA_CAD_01_SYMBOL, LORA_CAD_02_SYMBOL, LORA_CAD_04_SYMBOL, LORA_CAD_08_SYMBOL, LORA_CAD_16_SYMBOL]
  cadDetPeak | Limit for detection of SNR peak used in the CAD
  cadDetMin | Set the minimum symbol recognition for CAD
  cadExitMode | Operation to be done at the end of CAD action [LORA_CAD_ONLY, LORA_CAD_RX, LORA_CAD_LBT]
  cadTimeout | Defines the timeout value to abort the CAD activity

## ◆ RadioStartCad()

void RadioStartCad | ( | void |  | ) |

Start a Channel Activity Detection.

Remarks
  Before calling this function CAD parameters need to be set first! \ Use RadioSetCadParams to setup the parameters or directly in the SX126x low level functions RadioSetCadParams(uint8_t cadSymbolNum, uint8_t cadDetPeak, uint8_t cadDetMin, uint8_t cadExitMode, uint32_t cadTimeout); IRQ_CAD_DONE | IRQ_CAD_ACTIVITY_DETECTED, \ IRQ_RADIO_NONE, IRQ_RADIO_NONE); \

## ◆ RadioSetTxContinuousWave()

void RadioSetTxContinuousWave | ( | uint32_t | freq,
|  | int8_t | power,
|  | uint16_t | time
| ) |  |

Sets the radio in continuous wave transmission mode.

Parameters

  | freq Channel RF frequency
  | power Sets the output power [dBm]
  | time Transmission mode timeout [s]
  freq | Channel RF frequency
  power | Sets the output power [dBm]
  time | Transmission mode timeout [s]

## ◆ RadioRssi()

int16_t RadioRssi | ( | RadioModems_t | modem | ) |

Reads the current RSSI value.

Return values

  rssiValue | Current RSSI value in [dBm]

## ◆ RadioWrite()

void RadioWrite | ( | uint16_t | addr,
|  | uint8_t | data
| ) |  |

Writes the radio register at the specified address.

Parameters

  | addr Register address
  | data New register value
  addr | Register address
  data | New register value

## ◆ RadioRead()

uint8_t RadioRead | ( | uint16_t | addr | ) |

Reads the radio register at the specified address.

Parameters

  | addr Register address

Return values

  data | Register value

Parameters

  addr | Register address

## ◆ RadioWriteBuffer()

void RadioWriteBuffer | ( | uint16_t | addr,
|  | uint8_t * | buffer,
|  | uint8_t | size
| ) |  |

Writes multiple radio registers starting at address.

Parameters

  addr | First Radio register address
  buffer | Buffer containing the new register's values
  size | Number of registers to be written

## ◆ RadioReadBuffer()

void RadioReadBuffer | ( | uint16_t | addr,
|  | uint8_t * | buffer,
|  | uint8_t | size
| ) |  |

Reads multiple radio registers starting at address.

Parameters

  addr | First Radio register address
  buffer | Buffer where to copy the registers data
  size | Number of registers to be read

## ◆ RadioSetMaxPayloadLength()

void RadioSetMaxPayloadLength | ( | RadioModems_t | modem,
|  | uint8_t | max
| ) |  |

Sets the maximum payload length.

Parameters

  modem | Radio modem to be used [0: FSK, 1: LoRa]
  max | Maximum payload length in bytes

## ◆ RadioSetPublicNetwork()

void RadioSetPublicNetwork | ( | bool | enable | ) |

Sets the network to public or private. Updates the sync byte.

Remarks
  Applies to LoRa modem only

Parameters

  enable | if true, it enables a public network

## ◆ RadioGetWakeupTime()

uint32_t RadioGetWakeupTime | ( | void |  | ) |

Gets the time required for the board plus radio to get out of sleep.[ms].

Return values

  time | Radio plus board wakeup time in ms.

## ◆ RadioBgIrqProcess()

void RadioBgIrqProcess | ( | void |  | ) |

Process radio irq in background task (nRF52 & ESP32)

< Update operating mode state to a value lower than MODE_STDBY_XOSC

< Update operating mode state to a value lower than MODE_STDBY_XOSC

< Update operating mode state to a value lower than MODE_STDBY_XOSC

< Update operating mode state to a value lower than MODE_STDBY_XOSC

< Update operating mode state to a value lower than MODE_STDBY_XOSC

< Update operating mode state to a value lower than MODE_STDBY_XOSC

## ◆ RadioIrqProcess()

void RadioIrqProcess | ( | void |  | ) |

Process radio irq.

## ◆ RadioIrqProcessAfterDeepSleep()

void RadioIrqProcessAfterDeepSleep | ( | void |  | ) |

Process radio irq after deep sleep of the CPU.

## ◆ RadioRxBoosted()

void RadioRxBoosted | ( | uint32_t | timeout | ) |

Sets the radio in reception mode with Max LNA gain for the given time.

Parameters

  timeout | Reception timeout [ms] [0: continuous, others timeout]

## ◆ RadioSetRxDutyCycle()

void RadioSetRxDutyCycle | ( | uint32_t | rxTime,
|  | uint32_t | sleepTime
| ) |  |

Sets the Rx duty cycle management parameters.

Parameters

  rxTime | Structure describing reception timeout value
  sleepTime | Structure describing sleep timeout value

## ◆ RadioOnDioIrq()

void RadioOnDioIrq | ( | void |  | ) |

DIO 0 IRQ callback.

## ◆ RadioOnTxTimeoutIrq()

void RadioOnTxTimeoutIrq | ( | void |  | ) |

Tx timeout timer callback.

## ◆ RadioOnRxTimeoutIrq()

void RadioOnRxTimeoutIrq | ( | void |  | ) |

Rx timeout timer callback.

## ◆ RadioGetFskBandwidthRegValue()

static uint8_t RadioGetFskBandwidthRegValue | ( | uint32_t | bandwidth | ) |
| static

Returns the known FSK bandwidth registers value

Parameters

  bandwidth | Bandwidth value in Hz

Return values

  regValue | Bandwidth register value.

## ◆ RadioTx()

void RadioTx | ( | uint32_t | timeout | ) |

## ◆ RadioWriteFifo()

void RadioWriteFifo | ( | uint8_t * | buffer,
|  | uint8_t | size
| ) |  |

## ◆ RadioReadFifo()

void RadioReadFifo | ( | uint8_t * | buffer,
|  | uint8_t | size
| ) |  |

## Variable Documentation

## ◆ TxTimeoutTimer

TimerEvent_t TxTimeoutTimer

## ◆ RxTimeoutTimer

TimerEvent_t RxTimeoutTimer

## ◆ Radio

const struct Radio_s Radio

Radio driver.

Radio driver structure initialization

## ◆ FskBandwidths

const FskBandwidth_t FskBandwidths[]

Initial value:
=
{
{4800, 0x1F},
{5800, 0x17},
{7300, 0x0F},
{9700, 0x1E},
{11700, 0x16},
{14600, 0x0E},
{19500, 0x1D},
{23400, 0x15},
{29300, 0x0D},
{39000, 0x1C},
{46900, 0x14},
{58600, 0x0C},
{78200, 0x1B},
{93800, 0x13},
{117300, 0x0B},
{156200, 0x1A},
{187200, 0x12},
{234300, 0x0A},
{312000, 0x19},
{373600, 0x11},
{467000, 0x09},
{500000, 0x00},
}

Precomputed FSK bandwidth registers values

## ◆ Bandwidths

const RadioLoRaBandwidths_t Bandwidths[] = {LORA_BW_125, LORA_BW_250, LORA_BW_500}

## ◆ RadioLoRaSymbTime

double RadioLoRaSymbTime[3][6]
| static

Initial value:
= {{32.768, 16.384, 8.192, 4.096, 2.048, 1.024},
{16.384, 8.192, 4.096, 2.048, 1.024, 0.512},
{8.192, 4.096, 2.048, 1.024, 0.512, 0.256}}

## ◆ MaxPayloadLength

uint8_t MaxPayloadLength = 0xFF

## ◆ TxTimeout

uint32_t TxTimeout = 0

## ◆ RxTimeout

uint32_t RxTimeout = 0

## ◆ RxContinuous

bool RxContinuous = false

## ◆ RadioPktStatus

PacketStatus_t RadioPktStatus

## ◆ RadioRxPayload

uint8_t RadioRxPayload[255]

## ◆ IrqFired

bool IrqFired = false

## ◆ TimerRxTimeout

bool TimerRxTimeout = false

## ◆ TimerTxTimeout

bool TimerTxTimeout = false

## ◆ _modem

RadioModems_t _modem

## ◆ RadioPublicNetwork

RadioPublicNetwork_t RadioPublicNetwork = {false}
| static

## ◆ RadioEvents

RadioEvents_t* RadioEvents
| static

Radio callbacks variable

## ◆ SX126x

SX126x_t SX126x

Radio hardware and global parameters.

Radio hardware and global parameters