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.

______ _
/ _____) _ | |
( (____ _____ ____ _| |_ _____ ____| |__
\____ \| ___ | (_ _) ___ |/ ___) _ \
_____) ) ____| | | || |_| ____( (___| | | |
(______/|_____)_|_|_| \__)_____)\____)_| |_|
(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_tlast 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
modeNew 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
payloadA pointer to the payload
sizeThe 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
payloadA pointer to a buffer into which the payload will be copied
sizeA pointer to the size of the payload received
maxSizeThe maximal size allowed to copy into the buffer

◆ SX126xSendPayload()

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

Sends a payload.

Parameters
payloadA pointer to the payload to send
sizeThe size of the payload to send
timeoutThe timeout for Tx operation

◆ SX126xSetSyncWord()

uint8_t SX126xSetSyncWord ( uint8_t *  syncWord)

Sets the Sync Word given by index used in GFSK.

Parameters
syncWordSyncWord 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
seedInitial LFSR value ( 2 bytes )

◆ SX126xSetCrcPolynomial()

void SX126xSetCrcPolynomial ( uint16_t  polynomial)

Sets the polynomial used for the CRC calculation.

Parameters
polynomialThe seed value

◆ SX126xSetWhiteningSeed()

void SX126xSetWhiteningSeed ( uint16_t  seed)

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

Parameters
seedInitial 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
randomValue32 bits random value

◆ SX126xSetSleep()

void SX126xSetSleep ( SleepParams_t  sleepConfig)

Sets the radio in sleep mode.

Parameters
sleepConfigThe sleep configuration describing data retention and RTC wake-up

◆ SX126xSetStandby()

void SX126xSetStandby ( RadioStandbyModes_t  mode)

Sets the radio in configuration mode.

Parameters
modeThe 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
timeoutStructure describing the transmission timeout value

◆ SX126xSetRx()

void SX126xSetRx ( uint32_t  timeout)

Sets the radio in reception mode.

Parameters
timeoutStructure describing the reception timeout value

◆ SX126xSetRxBoosted()

void SX126xSetRxBoosted ( uint32_t  timeout)

Sets the radio in reception mode with Boosted LNA gain.

Parameters
timeoutStructure describing the reception timeout value

◆ SX126xSetRxDutyCycle()

void SX126xSetRxDutyCycle ( uint32_t  rxTime,
uint32_t  sleepTime 
)

Sets the Rx duty cycle management parameters.

Parameters
rxTimeStructure describing reception timeout value
sleepTimeStructure 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
SymbNumnumber 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
calibParamThe description of blocks to be calibrated

◆ SX126xCalibrateImage()

void SX126xCalibrateImage ( uint32_t  freq)

Calibrates the Image rejection depending of the frequency.

Parameters
freqThe operating frequency

◆ SX126xSetPaConfig()

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

Sets the transmission parameters.

Parameters
paDutyCycleDuty Cycle for the PA
hpMax0 for sx1261, 7 for sx1262
deviceSel1 for sx1261, 0 for sx1262
paLut0 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
fallbackModeThe 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
irqMaskGeneral IRQ mask
dio1MaskDIO1 mask
dio2MaskDIO2 mask
dio3MaskDIO3 mask
Examples
Sensor-Gateway-Deepsleep\LoRa-Gateway\src\main.cpp.

◆ SX126xGetIrqStatus()

uint16_t SX126xGetIrqStatus ( void  )

Returns the current IRQ status.

Return values
irqStatusIRQ status

◆ SX126xSetDio2AsRfSwitchCtrl()

void SX126xSetDio2AsRfSwitchCtrl ( uint8_t  enable)

Indicates if DIO2 is used to control an RF Switch.

Parameters
enabletrue of false

◆ SX126xSetDio3AsTcxoCtrl()

void SX126xSetDio3AsTcxoCtrl ( RadioTcxoCtrlVoltage_t  tcxoVoltage,
uint32_t  timeout 
)

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

Parameters
tcxoVoltagevoltage used to control the TCXO
timeouttime given to the TCXO to go to 32MHz

◆ SX126xSetRfFrequency()

void SX126xSetRfFrequency ( uint32_t  frequency)

Sets the RF frequency.

Parameters
frequencyRF 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
powerRF output power [-18..13] dBm
rampTimeTransmission ramp up time

◆ SX126xSetModulationParams()

void SX126xSetModulationParams ( ModulationParams_t modParams)

Set the modulation parameters.

Parameters
modParamsA structure describing the modulation parameters

◆ SX126xSetPacketParams()

void SX126xSetPacketParams ( PacketParams_t packetParams)

Sets the packet parameters.

Parameters
packetParamsA 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
cadSymbolNumThe 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]
cadDetPeakLimit for detection of SNR peak used in the CAD
cadDetMinSet the minimum symbol recognition for CAD
cadExitModeOperation to be done at the end of CAD action [LORA_CAD_ONLY, LORA_CAD_RX, LORA_CAD_LBT]
cadTimeoutDefines 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
txBaseAddressTransmission base address
rxBaseAddressReception base address

◆ SX126xGetStatus()

RadioStatus_t SX126xGetStatus ( void  )

Gets the current radio status.

Return values
statusRadio status

◆ SX126xGetRssiInst()

int8_t SX126xGetRssiInst ( void  )

Returns the instantaneous RSSI value for the last packet received.

Return values
rssiInstInstantaneous RSSI

◆ SX126xGetRxBufferStatus()

void SX126xGetRxBufferStatus ( uint8_t *  payloadLength,
uint8_t *  rxStartBuffer 
)

Gets the last received packet buffer status.

Parameters
payloadLengthLast received packet payload length
rxStartBufferLast received packet buffer address pointer

◆ SX126xGetPacketStatus()

void SX126xGetPacketStatus ( PacketStatus_t pktStatus)

Gets the last received packet payload length.

Parameters
pktStatusA structure of packet status

◆ SX126xGetDeviceErrors()

RadioError_t SX126xGetDeviceErrors ( void  )

Returns the possible system errors.

Return values
sysErrorsValue 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
irqIRQ(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.