| SX126x-Arduino 2.0.1

Data Structures | Functions | Variables
sx126x.cpp File Reference

SX126x driver implementation. More...

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

## Data Structures
struct | RadioRegisters_t
| Radio registers definition. More...

## Functions
void | SX126xOnDioIrq (void)
| DIO 0 IRQ callback. More...
void | SX126xSetPollingMode (void)
| DIO 0 IRQ callback. More...
void | SX126xSetInterruptMode (void)
| DIO 0 IRQ callback. More...
void | SX126xProcessIrqs (void)
void | SX126xInit (DioIrqHandler dioIrq)
| Initializes the radio driver. More...
void | SX126xReInit (DioIrqHandler dioIrq)
| Re-Initializes the radio driver after CPU wakeup from deep sleep. More...
RadioOperatingModes_t | SX126xGetOperatingMode (void)
| Gets the current Operation Mode of the Radio. More...
void | SX126xSetOperatingMode (RadioOperatingModes_t mode)
| Sets/Updates the current Radio OperationMode variable. More...
void | SX126xCheckDeviceReady (void)
| Wakeup the radio if it is in Sleep mode and check that Busy is low. More...
void | SX126xSetPayload (uint8_t *payload, uint8_t size)
| Saves the payload to be send in the radio buffer. More...
uint8_t | SX126xGetPayload (uint8_t *buffer, uint8_t *size, uint8_t maxSize)
| Reads the payload received. If the received payload is longer than maxSize, then the method returns 1 and do not set size and payload. More...
void | SX126xSendPayload (uint8_t *payload, uint8_t size, uint32_t timeout)
| Sends a payload. More...
uint8_t | SX126xSetSyncWord (uint8_t *syncWord)
| Sets the Sync Word given by index used in GFSK. More...
void | SX126xSetCrcSeed (uint16_t seed)
| Sets the seed value for the LFSR used for the CRC calculation. More...
void | SX126xSetCrcPolynomial (uint16_t polynomial)
| Sets the polynomial used for the CRC calculation. More...
void | SX126xSetWhiteningSeed (uint16_t seed)
| Sets the Initial value of the LFSR used for the whitening in GFSK protocols. More...
uint32_t | SX126xGetRandom (void)
| Gets a 32 bits random value generated by the radio. More...
void | SX126xSetSleep (SleepParams_t sleepConfig)
| Sets the radio in sleep mode. More...
void | SX126xSetStandby (RadioStandbyModes_t standbyConfig)
| Sets the radio in configuration mode. More...
void | SX126xSetFs (void)
| Sets the radio in FS mode. More...
void | SX126xSetTx (uint32_t timeout)
| Sets the radio in transmission mode. More...
void | SX126xSetRx (uint32_t timeout)
| Sets the radio in reception mode. More...
void | SX126xSetRxBoosted (uint32_t timeout)
| Sets the radio in reception mode with Boosted LNA gain. More...
void | SX126xSetRxDutyCycle (uint32_t rxTime, uint32_t sleepTime)
| Sets the Rx duty cycle management parameters. More...
void | SX126xSetCad (void)
| Sets the radio in CAD mode. More...
void | SX126xSetTxContinuousWave (void)
| Sets the radio in continuous wave transmission mode. More...
void | SX126xSetTxInfinitePreamble (void)
| Sets the radio in continuous preamble transmission mode. More...
void | SX126xSetStopRxTimerOnPreambleDetect (bool enable)
| Decide which interrupt will stop the internal radio rx timer. More...
void | SX126xSetLoRaSymbNumTimeout (uint8_t SymbNum)
| Set the number of symbol the radio will wait to validate a reception. More...
void | SX126xSetRegulatorMode (RadioRegulatorMode_t mode)
| Sets the power regulators operating mode. More...
void | SX126xCalibrate (CalibrationParams_t calibParam)
| Calibrates the given radio block. More...
void | SX126xCalibrateImage (uint32_t freq)
| Calibrates the Image rejection depending of the frequency. More...
void | SX126xSetPaConfig (uint8_t paDutyCycle, uint8_t hpMax, uint8_t deviceSel, uint8_t paLut)
| Sets the transmission parameters. More...
void | SX126xSetRxTxFallbackMode (uint8_t fallbackMode)
| Defines into which mode the chip goes after a TX / RX done. More...
void | SX126xSetDioIrqParams (uint16_t irqMask, uint16_t dio1Mask, uint16_t dio2Mask, uint16_t dio3Mask)
| Sets the IRQ mask and DIO masks. More...
uint16_t | SX126xGetIrqStatus (void)
| Returns the current IRQ status. More...
void | SX126xSetDio2AsRfSwitchCtrl (uint8_t enable)
| Indicates if DIO2 is used to control an RF Switch. More...
void | SX126xSetDio3AsTcxoCtrl (RadioTcxoCtrlVoltage_t tcxoVoltage, uint32_t timeout)
| Indicates if the Radio main clock is supplied from a tcxo. More...
void | SX126xSetRfFrequency (uint32_t frequency)
| Sets the RF frequency. More...
void | SX126xSetPacketType (RadioPacketTypes_t packetType)
| Sets the radio for the given protocol. More...
RadioPacketTypes_t | SX126xGetPacketType (void)
| Gets the current radio protocol. More...
void | SX126xSetTxParams (int8_t power, RadioRampTimes_t rampTime)
| Sets the transmission parameters. More...
void | SX126xSetModulationParams (ModulationParams_t *modulationParams)
| Set the modulation parameters. More...
void | SX126xSetPacketParams (PacketParams_t *packetParams)
| Sets the packet parameters. More...
void | SX126xSetCadParams (RadioLoRaCadSymbols_t cadSymbolNum, uint8_t cadDetPeak, uint8_t cadDetMin, RadioCadExitModes_t cadExitMode, uint32_t cadTimeout)
| Sets the Channel Activity Detection (CAD) parameters. More...
void | SX126xSetBufferBaseAddress (uint8_t txBaseAddress, uint8_t rxBaseAddress)
| Sets the data buffer base address for transmission and reception. More...
RadioStatus_t | SX126xGetStatus (void)
| Gets the current radio status. More...
int8_t | SX126xGetRssiInst (void)
| Returns the instantaneous RSSI value for the last packet received. More...
void | SX126xGetRxBufferStatus (uint8_t *payloadLength, uint8_t *rxStartBufferPointer)
| Gets the last received packet buffer status. More...
void | SX126xGetPacketStatus (PacketStatus_t *pktStatus)
| Gets the last received packet payload length. More...
RadioError_t | SX126xGetDeviceErrors (void)
| Returns the possible system errors. More...
void | SX126xClearDeviceErrors (void)
| Clear all the errors in the device. More...
void | SX126xClearIrqStatus (uint16_t irq)
| Clears the IRQs. More...

## Variables
static RadioOperatingModes_t | OperatingMode
| Holds the internal operating mode of the radio. More...
static RadioPacketTypes_t | PacketType
| Stores the current packet type set in the radio. More...
volatile uint32_t | FrequencyError = 0
| Stores the last frequency error measured on LoRa received packet. More...
static bool | ImageCalibrated = false
| Hold the status of the Image calibration. More...

## Detailed Description

SX126x driver implementation.

Copyright
  Revised BSD License, see file LICENSE.

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

Author
  Miguel Luis ( Semtech )
  Gregory Cristian ( Semtech )

## Function Documentation

## ◆ SX126xOnDioIrq()

void SX126xOnDioIrq | ( | void |  | ) |

DIO 0 IRQ callback.

## ◆ SX126xSetPollingMode()

void SX126xSetPollingMode | ( | void |  | ) |

DIO 0 IRQ callback.

## ◆ SX126xSetInterruptMode()

void SX126xSetInterruptMode | ( | void |  | ) |

DIO 0 IRQ callback.

## ◆ SX126xProcessIrqs()

void SX126xProcessIrqs | ( | void |  | ) |

## ◆ SX126xInit()

void SX126xInit | ( | DioIrqHandler | dioIrq | ) |

Initializes the radio driver.

# Public functions prototypes

## ◆ SX126xReInit()

void SX126xReInit | ( | DioIrqHandler | dioIrq | ) |

Re-Initializes the radio driver after CPU wakeup from deep sleep.

## ◆ SX126xGetOperatingMode()

RadioOperatingModes_t SX126xGetOperatingMode | ( | void |  | ) |

Gets the current Operation Mode of the Radio.

Return values

  RadioOperatingModes_t | last operating mode

## ◆ SX126xSetOperatingMode()

void SX126xSetOperatingMode | ( | RadioOperatingModes_t | mode | ) |

Sets/Updates the current Radio OperationMode variable.

Remarks
  WARNING: This function is only required to reflect the current radio operating mode when processing interrupts.

Parameters

  mode | New operating mode

## ◆ SX126xCheckDeviceReady()

void SX126xCheckDeviceReady | ( | void |  | ) |

Wakeup the radio if it is in Sleep mode and check that Busy is low.

## ◆ SX126xSetPayload()

void SX126xSetPayload | ( | uint8_t * | payload,
|  | uint8_t | size
| ) |  |

Saves the payload to be send in the radio buffer.

Parameters

  payload | A pointer to the payload
  size | The size of the payload

## ◆ SX126xGetPayload()

uint8_t SX126xGetPayload | ( | uint8_t * | payload,
|  | uint8_t * | size,
|  | uint8_t | maxSize
| ) |  |

Reads the payload received. If the received payload is longer than maxSize, then the method returns 1 and do not set size and payload.

Parameters

  payload | A pointer to a buffer into which the payload will be copied
  size | A pointer to the size of the payload received
  maxSize | The maximal size allowed to copy into the buffer

## ◆ SX126xSendPayload()

void SX126xSendPayload | ( | uint8_t * | payload,
|  | uint8_t | size,
|  | uint32_t | timeout
| ) |  |

Sends a payload.

Parameters

  payload | A pointer to the payload to send
  size | The size of the payload to send
  timeout | The timeout for Tx operation

## ◆ SX126xSetSyncWord()

uint8_t SX126xSetSyncWord | ( | uint8_t * | syncWord | ) |

Sets the Sync Word given by index used in GFSK.

Parameters

  syncWord | SyncWord bytes ( 8 bytes )

Return values

  status | [0: OK, 1: NOK]

## ◆ SX126xSetCrcSeed()

void SX126xSetCrcSeed | ( | uint16_t | seed | ) |

Sets the seed value for the LFSR used for the CRC calculation.

Parameters

  seed | Initial LFSR value ( 2 bytes )

## ◆ SX126xSetCrcPolynomial()

void SX126xSetCrcPolynomial | ( | uint16_t | polynomial | ) |

Sets the polynomial used for the CRC calculation.

Parameters

  polynomial | The seed value

## ◆ SX126xSetWhiteningSeed()

void SX126xSetWhiteningSeed | ( | uint16_t | seed | ) |

Sets the Initial value of the LFSR used for the whitening in GFSK protocols.

Parameters

  seed | Initial LFSR value

## ◆ SX126xGetRandom()

uint32_t SX126xGetRandom | ( | void |  | ) |

Gets a 32 bits random value generated by the radio.

Remarks
  The radio must be in reception mode before executing this function

Return values

  randomValue | 32 bits random value

## ◆ SX126xSetSleep()

void SX126xSetSleep | ( | SleepParams_t | sleepConfig | ) |

Sets the radio in sleep mode.

Parameters

  sleepConfig | The sleep configuration describing data retention and RTC wake-up

## ◆ SX126xSetStandby()

void SX126xSetStandby | ( | RadioStandbyModes_t | mode | ) |

Sets the radio in configuration mode.

Parameters

  mode | The standby mode to put the radio into

## ◆ SX126xSetFs()

void SX126xSetFs | ( | void |  | ) |

Sets the radio in FS mode.

## ◆ SX126xSetTx()

void SX126xSetTx | ( | uint32_t | timeout | ) |

Sets the radio in transmission mode.

Parameters

  timeout | Structure describing the transmission timeout value

## ◆ SX126xSetRx()

void SX126xSetRx | ( | uint32_t | timeout | ) |

Sets the radio in reception mode.

Parameters

  timeout | Structure describing the reception timeout value

## ◆ SX126xSetRxBoosted()

void SX126xSetRxBoosted | ( | uint32_t | timeout | ) |

Sets the radio in reception mode with Boosted LNA gain.

Parameters

  timeout | Structure describing the reception timeout value

## ◆ SX126xSetRxDutyCycle()

void SX126xSetRxDutyCycle | ( | 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

## ◆ SX126xSetCad()

void SX126xSetCad | ( | void |  | ) |

Sets the radio in CAD mode.

## ◆ SX126xSetTxContinuousWave()

void SX126xSetTxContinuousWave | ( | void |  | ) |

Sets the radio in continuous wave transmission mode.

## ◆ SX126xSetTxInfinitePreamble()

void SX126xSetTxInfinitePreamble | ( | void |  | ) |

Sets the radio in continuous preamble transmission mode.

## ◆ SX126xSetStopRxTimerOnPreambleDetect()

void SX126xSetStopRxTimerOnPreambleDetect | ( | bool | enable | ) |

Decide which interrupt will stop the internal radio rx timer.

Parameters

  enable | [0: Timer stop after header/syncword detection 1: Timer stop after preamble detection]

## ◆ SX126xSetLoRaSymbNumTimeout()

void SX126xSetLoRaSymbNumTimeout | ( | uint8_t | SymbNum | ) |

Set the number of symbol the radio will wait to validate a reception.

Parameters

  SymbNum | number of LoRa symbols

## ◆ SX126xSetRegulatorMode()

void SX126xSetRegulatorMode | ( | RadioRegulatorMode_t | mode | ) |

Sets the power regulators operating mode.

Parameters

  mode | [0: LDO, 1:DC_DC]

## ◆ SX126xCalibrate()

void SX126xCalibrate | ( | CalibrationParams_t | calibParam | ) |

Calibrates the given radio block.

Parameters

  calibParam | The description of blocks to be calibrated

## ◆ SX126xCalibrateImage()

void SX126xCalibrateImage | ( | uint32_t | freq | ) |

Calibrates the Image rejection depending of the frequency.

Parameters

  freq | The operating frequency

## ◆ SX126xSetPaConfig()

void SX126xSetPaConfig | ( | uint8_t | paDutyCycle,
|  | uint8_t | hpMax,
|  | uint8_t | deviceSel,
|  | uint8_t | paLut
| ) |  |

Sets the transmission parameters.

Parameters

  paDutyCycle | Duty Cycle for the PA
  hpMax | 0 for sx1261, 7 for sx1262
  deviceSel | 1 for sx1261, 0 for sx1262
  paLut | 0 for 14dBm LUT, 1 for 22dBm LUT

## ◆ SX126xSetRxTxFallbackMode()

void SX126xSetRxTxFallbackMode | ( | uint8_t | fallbackMode | ) |

Defines into which mode the chip goes after a TX / RX done.

Parameters

  fallbackMode | The mode in which the radio goes

## ◆ SX126xSetDioIrqParams()

void SX126xSetDioIrqParams | ( | uint16_t | irqMask,
|  | uint16_t | dio1Mask,
|  | uint16_t | dio2Mask,
|  | uint16_t | dio3Mask
| ) |  |

Sets the IRQ mask and DIO masks.

Parameters

  irqMask | General IRQ mask
  dio1Mask | DIO1 mask
  dio2Mask | DIO2 mask
  dio3Mask | DIO3 mask

Examples
  Sensor-Gateway-Deepsleep\LoRa-Gateway\src\main.cpp.

## ◆ SX126xGetIrqStatus()

uint16_t SX126xGetIrqStatus | ( | void |  | ) |

Returns the current IRQ status.

Return values

  irqStatus | IRQ status

## ◆ SX126xSetDio2AsRfSwitchCtrl()

void SX126xSetDio2AsRfSwitchCtrl | ( | uint8_t | enable | ) |

Indicates if DIO2 is used to control an RF Switch.

Parameters

  enable | true of false

## ◆ SX126xSetDio3AsTcxoCtrl()

void SX126xSetDio3AsTcxoCtrl | ( | RadioTcxoCtrlVoltage_t | tcxoVoltage,
|  | uint32_t | timeout
| ) |  |

Indicates if the Radio main clock is supplied from a tcxo.

Parameters

  tcxoVoltage | voltage used to control the TCXO
  timeout | time given to the TCXO to go to 32MHz

## ◆ SX126xSetRfFrequency()

void SX126xSetRfFrequency | ( | uint32_t | frequency | ) |

Sets the RF frequency.

Parameters

  frequency | RF frequency [Hz]

## ◆ SX126xSetPacketType()

void SX126xSetPacketType | ( | RadioPacketTypes_t | packetType | ) |

Sets the radio for the given protocol.

Parameters

  packetType | [PACKET_TYPE_GFSK, PACKET_TYPE_LORA]

Remarks
  This method has to be called before SetRfFrequency, SetModulationParams and SetPacketParams

## ◆ SX126xGetPacketType()

RadioPacketTypes_t SX126xGetPacketType | ( | void |  | ) |

Gets the current radio protocol.

Return values

  packetType | [PACKET_TYPE_GFSK, PACKET_TYPE_LORA]

## ◆ SX126xSetTxParams()

void SX126xSetTxParams | ( | int8_t | power,
|  | RadioRampTimes_t | rampTime
| ) |  |

Sets the transmission parameters.

Parameters

  power | RF output power [-18..13] dBm
  rampTime | Transmission ramp up time

## ◆ SX126xSetModulationParams()

void SX126xSetModulationParams | ( | ModulationParams_t * | modParams | ) |

Set the modulation parameters.

Parameters

  modParams | A structure describing the modulation parameters

## ◆ SX126xSetPacketParams()

void SX126xSetPacketParams | ( | PacketParams_t * | packetParams | ) |

Sets the packet parameters.

Parameters

  packetParams | A structure describing the packet parameters

## ◆ SX126xSetCadParams()

void SX126xSetCadParams | ( | RadioLoRaCadSymbols_t | cadSymbolNum,
|  | uint8_t | cadDetPeak,
|  | uint8_t | cadDetMin,
|  | RadioCadExitModes_t | cadExitMode,
|  | uint32_t | cadTimeout
| ) |  |

Sets the Channel Activity Detection (CAD) 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

## ◆ SX126xSetBufferBaseAddress()

void SX126xSetBufferBaseAddress | ( | uint8_t | txBaseAddress,
|  | uint8_t | rxBaseAddress
| ) |  |

Sets the data buffer base address for transmission and reception.

Parameters

  txBaseAddress | Transmission base address
  rxBaseAddress | Reception base address

## ◆ SX126xGetStatus()

RadioStatus_t SX126xGetStatus | ( | void |  | ) |

Gets the current radio status.

Return values

  status | Radio status

## ◆ SX126xGetRssiInst()

int8_t SX126xGetRssiInst | ( | void |  | ) |

Returns the instantaneous RSSI value for the last packet received.

Return values

  rssiInst | Instantaneous RSSI

## ◆ SX126xGetRxBufferStatus()

void SX126xGetRxBufferStatus | ( | uint8_t * | payloadLength,
|  | uint8_t * | rxStartBuffer
| ) |  |

Gets the last received packet buffer status.

Parameters

  payloadLength | Last received packet payload length
  rxStartBuffer | Last received packet buffer address pointer

## ◆ SX126xGetPacketStatus()

void SX126xGetPacketStatus | ( | PacketStatus_t * | pktStatus | ) |

Gets the last received packet payload length.

Parameters

  pktStatus | A structure of packet status

## ◆ SX126xGetDeviceErrors()

RadioError_t SX126xGetDeviceErrors | ( | void |  | ) |

Returns the possible system errors.

Return values

  sysErrors | Value representing the possible sys failures

## ◆ SX126xClearDeviceErrors()

void SX126xClearDeviceErrors | ( | void |  | ) |

Clear all the errors in the device.

## ◆ SX126xClearIrqStatus()

void SX126xClearIrqStatus | ( | uint16_t | irq | ) |

Clears the IRQs.

Parameters

  irq | IRQ(s) to be cleared

## Variable Documentation

## ◆ OperatingMode

RadioOperatingModes_t OperatingMode
| static

Holds the internal operating mode of the radio.

## ◆ PacketType

RadioPacketTypes_t PacketType
| static

Stores the current packet type set in the radio.

## ◆ FrequencyError

volatile uint32_t FrequencyError = 0

Stores the last frequency error measured on LoRa received packet.

## ◆ ImageCalibrated

bool ImageCalibrated = false
| static

Hold the status of the Image calibration.