12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181 |
- /*!
- * \file radio.c
- *
- * \brief Radio driver API definition
- *
- * \copyright Revised BSD License, see section \ref LICENSE.
- *
- * \code
- * ______ _
- * / _____) _ | |
- * ( (____ _____ ____ _| |_ _____ ____| |__
- * \____ \| ___ | (_ _) ___ |/ ___) _ \
- * _____) ) ____| | | || |_| ____( (___| | | |
- * (______/|_____)_|_|_| \__)_____)\____)_| |_|
- * (C)2013-2017 Semtech
- *
- * \endcode
- *
- * \author Miguel Luis ( Semtech )
- *
- * \author Gregory Cristian ( Semtech )
- */
- #include <math.h>
- #include <string.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include "board.h"
- #include "radio.h"
- #include "sx126x.h"
- #include "sx126x-board.h"
- /*!
- * \brief Initializes the radio
- *
- * \param [IN] events Structure containing the driver callback functions
- */
- void RadioInit( RadioEvents_t *events );
- /*!
- * Return current radio status
- *
- * \param status Radio status.[RF_IDLE, RF_RX_RUNNING, RF_TX_RUNNING]
- */
- RadioState_t RadioGetStatus( void );
- /*!
- * \brief Configures the radio with the given modem
- *
- * \param [IN] modem Modem to be used [0: FSK, 1: LoRa]
- */
- void RadioSetModem( RadioModems_t modem );
- /*!
- * \brief Sets the channel frequency
- *
- * \param [IN] freq Channel RF frequency
- */
- void RadioSetChannel( uint32_t freq );
- /*!
- * \brief Checks if the channel is free for the given time
- *
- * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
- * \param [IN] freq Channel RF frequency
- * \param [IN] rssiThresh RSSI threshold
- * \param [IN] maxCarrierSenseTime Max time while the RSSI is measured
- *
- * \retval isFree [true: Channel is free, false: Channel is not free]
- */
- bool RadioIsChannelFree( RadioModems_t modem, uint32_t freq, int16_t rssiThresh, uint32_t maxCarrierSenseTime );
- /*!
- * \brief Generates a 32 bits random value based on the RSSI readings
- *
- * \remark 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.
- *
- * \retval randomValue 32 bits random value
- */
- uint32_t RadioRandom( void );
- /*!
- * \brief Sets the reception parameters
- *
- * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
- * \param [IN] bandwidth Sets the bandwidth
- * FSK : >= 2600 and <= 250000 Hz
- * LoRa: [0: 125 kHz, 1: 250 kHz,
- * 2: 500 kHz, 3: Reserved]
- * \param [IN] 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]
- * \param [IN] 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]
- * \param [IN] bandwidthAfc Sets the AFC Bandwidth (FSK only)
- * FSK : >= 2600 and <= 250000 Hz
- * LoRa: N/A ( set to 0 )
- * \param [IN] preambleLen Sets the Preamble length
- * FSK : Number of bytes
- * LoRa: Length in symbols (the hardware adds 4 more symbols)
- * \param [IN] symbTimeout Sets the RxSingle timeout value
- * FSK : timeout in number of bytes
- * LoRa: timeout in symbols
- * \param [IN] fixLen Fixed length packets [0: variable, 1: fixed]
- * \param [IN] payloadLen Sets payload length when fixed length is used
- * \param [IN] crcOn Enables/Disables the CRC [0: OFF, 1: ON]
- * \param [IN] FreqHopOn Enables disables the intra-packet frequency hopping
- * FSK : N/A ( set to 0 )
- * LoRa: [0: OFF, 1: ON]
- * \param [IN] HopPeriod Number of symbols between each hop
- * FSK : N/A ( set to 0 )
- * LoRa: Number of symbols
- * \param [IN] iqInverted Inverts IQ signals (LoRa only)
- * FSK : N/A ( set to 0 )
- * LoRa: [0: not inverted, 1: inverted]
- * \param [IN] rxContinuous Sets the reception in continuous mode
- * [false: single mode, true: continuous mode]
- */
- 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 );
- /*!
- * \brief Sets the transmission parameters
- *
- * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
- * \param [IN] power Sets the output power [dBm]
- * \param [IN] fdev Sets the frequency deviation (FSK only)
- * FSK : [Hz]
- * LoRa: 0
- * \param [IN] bandwidth Sets the bandwidth (LoRa only)
- * FSK : 0
- * LoRa: [0: 125 kHz, 1: 250 kHz,
- * 2: 500 kHz, 3: Reserved]
- * \param [IN] 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]
- * \param [IN] 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]
- * \param [IN] preambleLen Sets the preamble length
- * FSK : Number of bytes
- * LoRa: Length in symbols (the hardware adds 4 more symbols)
- * \param [IN] fixLen Fixed length packets [0: variable, 1: fixed]
- * \param [IN] crcOn Enables disables the CRC [0: OFF, 1: ON]
- * \param [IN] FreqHopOn Enables disables the intra-packet frequency hopping
- * FSK : N/A ( set to 0 )
- * LoRa: [0: OFF, 1: ON]
- * \param [IN] HopPeriod Number of symbols between each hop
- * FSK : N/A ( set to 0 )
- * LoRa: Number of symbols
- * \param [IN] iqInverted Inverts IQ signals (LoRa only)
- * FSK : N/A ( set to 0 )
- * LoRa: [0: not inverted, 1: inverted]
- * \param [IN] timeout Transmission timeout [ms]
- */
- 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 );
- /*!
- * \brief Checks if the given RF frequency is supported by the hardware
- *
- * \param [IN] frequency RF frequency to be checked
- * \retval isSupported [true: supported, false: unsupported]
- */
- bool RadioCheckRfFrequency( uint32_t frequency );
- /*!
- * \brief Computes the packet time on air in ms for the given payload
- *
- * \Remark Can only be called once SetRxConfig or SetTxConfig have been called
- *
- * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
- * \param [IN] pktLen Packet payload length
- *
- * \retval airTime Computed airTime (ms) for the given packet payload length
- */
- uint32_t RadioTimeOnAir( RadioModems_t modem, uint8_t pktLen );
- /*!
- * \brief Sends the buffer of size. Prepares the packet to be sent and sets
- * the radio in transmission
- *
- * \param [IN]: buffer Buffer pointer
- * \param [IN]: size Buffer size
- */
- void RadioSend( uint8_t *buffer, uint8_t size );
- /*!
- * \brief Sets the radio in sleep mode
- */
- void RadioSleep( void );
- /*!
- * \brief Sets the radio in standby mode
- */
- void RadioStandby( void );
- /*!
- * \brief Sets the radio in reception mode for the given time
- * \param [IN] timeout Reception timeout [ms]
- * [0: continuous, others timeout]
- */
- void RadioRx( uint32_t timeout );
- /*!
- * \brief Start a Channel Activity Detection
- */
- void RadioStartCad( void );
- /*!
- * \brief Sets the radio in continuous wave transmission mode
- *
- * \param [IN]: freq Channel RF frequency
- * \param [IN]: power Sets the output power [dBm]
- * \param [IN]: time Transmission mode timeout [s]
- */
- void RadioSetTxContinuousWave( uint32_t freq, int8_t power, uint16_t time );
- /*!
- * \brief Reads the current RSSI value
- *
- * \retval rssiValue Current RSSI value in [dBm]
- */
- int16_t RadioRssi( RadioModems_t modem );
- /*!
- * \brief Writes the radio register at the specified address
- *
- * \param [IN]: addr Register address
- * \param [IN]: data New register value
- */
- void RadioWrite( uint16_t addr, uint8_t data );
- /*!
- * \brief Reads the radio register at the specified address
- *
- * \param [IN]: addr Register address
- * \retval data Register value
- */
- uint8_t RadioRead( uint16_t addr );
- /*!
- * \brief Writes multiple radio registers starting at address
- *
- * \param [IN] addr First Radio register address
- * \param [IN] buffer Buffer containing the new register's values
- * \param [IN] size Number of registers to be written
- */
- void RadioWriteBuffer( uint16_t addr, uint8_t *buffer, uint8_t size );
- /*!
- * \brief Reads multiple radio registers starting at address
- *
- * \param [IN] addr First Radio register address
- * \param [OUT] buffer Buffer where to copy the registers data
- * \param [IN] size Number of registers to be read
- */
- void RadioReadBuffer( uint16_t addr, uint8_t *buffer, uint8_t size );
- /*!
- * \brief Sets the maximum payload length.
- *
- * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
- * \param [IN] max Maximum payload length in bytes
- */
- void RadioSetMaxPayloadLength( RadioModems_t modem, uint8_t max );
- /*!
- * \brief Sets the network to public or private. Updates the sync byte.
- *
- * \remark Applies to LoRa modem only
- *
- * \param [IN] enable if true, it enables a public network
- */
- void RadioSetPublicNetwork( bool enable );
- /*!
- * \brief Gets the time required for the board plus radio to get out of sleep.[ms]
- *
- * \retval time Radio plus board wakeup time in ms.
- */
- uint32_t RadioGetWakeupTime( void );
- /*!
- * \brief Process radio irq
- */
- void RadioIrqProcess( void );
- /*!
- * \brief Sets the radio in reception mode with Max LNA gain for the given time
- * \param [IN] timeout Reception timeout [ms]
- * [0: continuous, others timeout]
- */
- void RadioRxBoosted( uint32_t timeout );
- /*!
- * \brief Sets the Rx duty cycle management parameters
- *
- * \param [in] rxTime Structure describing reception timeout value
- * \param [in] sleepTime Structure describing sleep timeout value
- */
- void RadioSetRxDutyCycle( uint32_t rxTime, uint32_t sleepTime );
- /*!
- * Radio driver structure initialization
- */
- const struct Radio_s Radio =
- {
- RadioInit,
- RadioGetStatus,
- RadioSetModem,
- RadioSetChannel,
- RadioIsChannelFree,
- RadioRandom,
- RadioSetRxConfig,
- RadioSetTxConfig,
- RadioCheckRfFrequency,
- RadioTimeOnAir,
- RadioSend,
- RadioSleep,
- RadioStandby,
- RadioRx,
- RadioStartCad,
- RadioSetTxContinuousWave,
- RadioRssi,
- RadioWrite,
- RadioRead,
- RadioWriteBuffer,
- RadioReadBuffer,
- RadioSetMaxPayloadLength,
- RadioSetPublicNetwork,
- RadioGetWakeupTime,
- RadioIrqProcess,
- // Available on SX126x only
- RadioRxBoosted,
- RadioSetRxDutyCycle
- };
- uint8_t regulatorMode = USE_DCDC;
- /*
- * Local types definition
- */
- /*!
- * FSK bandwidth definition
- */
- typedef struct
- {
- uint32_t bandwidth;
- uint8_t RegValue;
- }FskBandwidth_t;
- /*!
- * Precomputed FSK bandwidth registers values
- */
- const FskBandwidth_t FskBandwidths[] =
- {
- { 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 }, // Invalid Bandwidth
- };
- const RadioLoRaBandwidths_t Bandwidths[] = {LORA_BW_062, LORA_BW_125, LORA_BW_250, LORA_BW_500 }; //add @LORA_BW_062,by vollgo dropLin,2022年3月29日
- // SF12 SF11 SF10 SF9 SF8 SF7
- static double RadioLoRaSymbTime[4][6] = {
- { 65.536, 32.768, 16.384, 8.192, 4.096, 2.048 }, // 62.5 KHz //add @62.5 KHz,by vollgo dropLin, 2022年3月29日
- { 32.768, 16.384, 8.192, 4.096, 2.048, 1.024 }, // 125 KHz
- { 16.384, 8.192, 4.096, 2.048, 1.024, 0.512 }, // 250 KHz
- { 8.192, 4.096, 2.048, 1.024, 0.512, 0.256 }}; // 500 KHz
- 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;
- uint8_t syncWord[] = { 0xC1, 0x94, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x00 };
- /*
- * SX126x DIO IRQ callback functions prototype
- */
- /*!
- * \brief DIO 0 IRQ callback
- */
- void RadioOnDioIrq( void );
- /*!
- * \brief Tx timeout timer callback
- */
- void RadioOnTxTimeoutIrq( void );
- /*!
- * \brief Rx timeout timer callback
- */
- void RadioOnRxTimeoutIrq( void );
- /*
- * Private global variables
- */
- /*!
- * Holds the current network type for the radio
- */
- typedef struct
- {
- bool Previous;
- bool Current;
- }RadioPublicNetwork_t;
- static RadioPublicNetwork_t RadioPublicNetwork = { false };
- /*!
- * Radio callbacks variable
- */
- static RadioEvents_t* RadioEvents;
- /*
- * Public global variables
- */
- /*!
- * Radio hardware and global parameters
- */
- volatile SX126x_t SX126x;
- /*!
- * Tx and Rx timers
- */
- //TimerEvent_t TxTimeoutTimer;
- //TimerEvent_t RxTimeoutTimer;
- /*!
- * Returns the known FSK bandwidth registers value
- *
- * \param [IN] bandwidth Bandwidth value in Hz
- * \retval regValue Bandwidth register value.
- */
- static uint8_t RadioGetFskBandwidthRegValue( uint32_t bandwidth )
- {
- uint8_t i;
- if( bandwidth == 0 )
- {
- return( 0x1F );
- }
- for( i = 0; i < ( sizeof( FskBandwidths ) / sizeof( FskBandwidth_t ) ) - 1; i++ )
- {
- if( ( bandwidth >= FskBandwidths[i].bandwidth ) && ( bandwidth < FskBandwidths[i + 1].bandwidth ) )
- {
- return FskBandwidths[i+1].RegValue;
- }
- }
- // ERROR: Value not found
- while( 1 );
- }
- void RadioSetregulatorMode(uint8_t mode)
- {
- regulatorMode = mode;
- }
- void RadioInit( RadioEvents_t *events )
- {
- RadioEvents = events;
-
- SX126xInit( 0 );
- SX126xSetStandby( STDBY_RC );
- SX126xSetRegulatorMode( regulatorMode );
-
- SX126xSetBufferBaseAddress( 0x00, 0x00 );
- SX126xSetTxParams( 0, RADIO_RAMP_200_US );
- SX126xSetDioIrqParams( IRQ_RADIO_ALL, IRQ_RADIO_ALL, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
-
- //Initialize driver timeout timers
- //TimerInit( &TxTimeoutTimer, RadioOnTxTimeoutIrq );
- //TimerInit( &RxTimeoutTimer, RadioOnRxTimeoutIrq );
-
- IrqFired = false;
- }
- RadioState_t RadioGetStatus( void )
- {
- switch( SX126xGetOperatingMode( ) )
- {
- case MODE_TX:
- return RF_TX_RUNNING;
- case MODE_RX:
- return RF_RX_RUNNING;
- case RF_CAD:
- return RF_CAD;
- default:
- return RF_IDLE;
- }
- }
- void RadioSetModem( RadioModems_t modem )
- {
- switch( modem )
- {
- default:
- case MODEM_FSK:
- SX126xSetPacketType( PACKET_TYPE_GFSK );
- // When switching to GFSK mode the LoRa SyncWord register value is reset
- // Thus, we also reset the RadioPublicNetwork variable
- RadioPublicNetwork.Current = false;
- break;
- case MODEM_LORA:
- SX126xSetPacketType( PACKET_TYPE_LORA );
- // Public/Private network register is reset when switching modems
- if( RadioPublicNetwork.Current != RadioPublicNetwork.Previous )
- {
- RadioPublicNetwork.Current = RadioPublicNetwork.Previous;
- RadioSetPublicNetwork( RadioPublicNetwork.Current );
- }
- break;
- }
- }
- void RadioSetChannel( uint32_t freq )
- {
- SX126xSetRfFrequency( freq );
- }
- bool RadioIsChannelFree( RadioModems_t modem, uint32_t freq, int16_t rssiThresh, uint32_t maxCarrierSenseTime )
- {
- bool status = true;
- int16_t rssi = 0;
- uint32_t carrierSenseTime = 0;
- RadioSetModem( modem );
- RadioSetChannel( freq );
- RadioRx( 0 );
- HAL_Delay_nMS( 1 );
- // carrierSenseTime = TimerGetCurrentTime( );
- // Perform carrier sense for maxCarrierSenseTime
- // while( TimerGetElapsedTime( carrierSenseTime ) < maxCarrierSenseTime )
- // {
- // rssi = RadioRssi( modem );
- //
- // if( rssi > rssiThresh )
- // {
- // status = false;
- // break;
- // }
- // }
- RadioSleep( );
- return status;
- }
- uint32_t RadioRandom( void )
- {
- uint8_t i;
- uint32_t rnd = 0;
- /*
- * Radio setup for random number generation
- */
- // Set LoRa modem ON
- RadioSetModem( MODEM_LORA );
- // Set radio in continuous reception
- SX126xSetRx( 0 );
- for( i = 0; i < 32; i++ )
- {
- HAL_Delay_nMS( 1 );
- // Unfiltered RSSI value reading. Only takes the LSB value
- rnd |= ( ( uint32_t )SX126xGetRssiInst( ) & 0x01 ) << i;
- }
- RadioSleep( );
- return rnd;
- }
- 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 )
- {
- RxContinuous = rxContinuous;
- if( fixLen == true )
- {
- MaxPayloadLength = payloadLen;
- }
- else
- {
- MaxPayloadLength = 0xFF;
- }
- switch( modem )
- {
- case MODEM_FSK:
- SX126xSetStopRxTimerOnPreambleDetect( false );
- SX126x.ModulationParams.PacketType = PACKET_TYPE_GFSK;
- SX126x.ModulationParams.Params.Gfsk.BitRate = datarate;
- SX126x.ModulationParams.Params.Gfsk.ModulationShaping = MOD_SHAPING_G_BT_1;
- SX126x.ModulationParams.Params.Gfsk.Bandwidth = RadioGetFskBandwidthRegValue( bandwidth << 1 );
- SX126x.PacketParams.PacketType = PACKET_TYPE_GFSK;
- SX126x.PacketParams.Params.Gfsk.PreambleLength = ( preambleLen << 3 ); // convert byte into bit
- SX126x.PacketParams.Params.Gfsk.PreambleMinDetect = RADIO_PREAMBLE_DETECTOR_16_BITS;
- SX126x.PacketParams.Params.Gfsk.SyncWordLength = 3 << 3; // convert byte into bit
- SX126x.PacketParams.Params.Gfsk.AddrComp = RADIO_ADDRESSCOMP_FILT_OFF;
- SX126x.PacketParams.Params.Gfsk.HeaderType = ( fixLen == true ) ? RADIO_PACKET_FIXED_LENGTH : RADIO_PACKET_VARIABLE_LENGTH;
- SX126x.PacketParams.Params.Gfsk.PayloadLength = MaxPayloadLength;
- if( crcOn == true )
- {
- SX126x.PacketParams.Params.Gfsk.CrcLength = RADIO_CRC_2_BYTES_CCIT;
- }
- else
- {
- SX126x.PacketParams.Params.Gfsk.CrcLength = RADIO_CRC_OFF;
- }
- SX126x.PacketParams.Params.Gfsk.DcFree = RADIO_DC_FREE_OFF;
- RadioStandby( );
- RadioSetModem( ( SX126x.ModulationParams.PacketType == PACKET_TYPE_GFSK ) ? MODEM_FSK : MODEM_LORA );
- SX126xSetModulationParams( &SX126x.ModulationParams );
- SX126xSetPacketParams( &SX126x.PacketParams );
- SX126xSetSyncWord( syncWord );
- SX126xSetWhiteningSeed( 0x01FF );
- RxTimeout = ( uint32_t )( symbTimeout * ( ( 1.0 / ( double )datarate ) * 8.0 ) * 1000 );
- break;
- case MODEM_LORA:
- SX126xSetStopRxTimerOnPreambleDetect( false );
- SX126xSetLoRaSymbNumTimeout( symbTimeout );
- SX126x.ModulationParams.PacketType = PACKET_TYPE_LORA;
- SX126x.ModulationParams.Params.LoRa.SpreadingFactor = ( RadioLoRaSpreadingFactors_t )datarate;
- SX126x.ModulationParams.Params.LoRa.Bandwidth = Bandwidths[bandwidth];
- SX126x.ModulationParams.Params.LoRa.CodingRate = ( RadioLoRaCodingRates_t )coderate;
- if( ( ( bandwidth == 0 ) && ( ( datarate == 11 ) || ( datarate == 12 ) ) ) ||
- ( ( bandwidth == 1 ) && ( datarate == 12 ) ) )
- {
- SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize = 0x01;
- }
- else
- {
- SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize = 0x00;
- }
- SX126x.PacketParams.PacketType = PACKET_TYPE_LORA;
- if( ( SX126x.ModulationParams.Params.LoRa.SpreadingFactor == LORA_SF5 ) ||
- ( SX126x.ModulationParams.Params.LoRa.SpreadingFactor == LORA_SF6 ) )
- {
- if( preambleLen < 12 )
- {
- SX126x.PacketParams.Params.LoRa.PreambleLength = 12;
- }
- else
- {
- SX126x.PacketParams.Params.LoRa.PreambleLength = preambleLen;
- }
- }
- else
- {
- SX126x.PacketParams.Params.LoRa.PreambleLength = preambleLen;
- }
- SX126x.PacketParams.Params.LoRa.HeaderType = ( RadioLoRaPacketLengthsMode_t )fixLen;
- SX126x.PacketParams.Params.LoRa.PayloadLength = MaxPayloadLength;
- SX126x.PacketParams.Params.LoRa.CrcMode = ( RadioLoRaCrcModes_t )crcOn;
- SX126x.PacketParams.Params.LoRa.InvertIQ = ( RadioLoRaIQModes_t )iqInverted;
- RadioSetModem( ( SX126x.ModulationParams.PacketType == PACKET_TYPE_GFSK ) ? MODEM_FSK : MODEM_LORA );
- SX126xSetModulationParams( &SX126x.ModulationParams );
- SX126xSetPacketParams( &SX126x.PacketParams );
- // Timeout Max, Timeout handled directly in SetRx function
- RxTimeout = 0xFFFF;
- break;
- }
- }
- 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 )
- {
- switch( modem )
- {
- case MODEM_FSK:
- SX126x.ModulationParams.PacketType = PACKET_TYPE_GFSK;
- SX126x.ModulationParams.Params.Gfsk.BitRate = datarate;
- SX126x.ModulationParams.Params.Gfsk.ModulationShaping = MOD_SHAPING_G_BT_1;
- SX126x.ModulationParams.Params.Gfsk.Bandwidth = RadioGetFskBandwidthRegValue( bandwidth << 1);
- SX126x.ModulationParams.Params.Gfsk.Fdev = fdev;
- SX126x.PacketParams.PacketType = PACKET_TYPE_GFSK;
- SX126x.PacketParams.Params.Gfsk.PreambleLength = ( preambleLen << 3 ); // convert byte into bit
- SX126x.PacketParams.Params.Gfsk.PreambleMinDetect = RADIO_PREAMBLE_DETECTOR_08_BITS;
- SX126x.PacketParams.Params.Gfsk.SyncWordLength = 3 << 3 ; // convert byte into bit
- SX126x.PacketParams.Params.Gfsk.AddrComp = RADIO_ADDRESSCOMP_FILT_OFF;
- SX126x.PacketParams.Params.Gfsk.HeaderType = ( fixLen == true ) ? RADIO_PACKET_FIXED_LENGTH : RADIO_PACKET_VARIABLE_LENGTH;
- if( crcOn == true )
- {
- SX126x.PacketParams.Params.Gfsk.CrcLength = RADIO_CRC_2_BYTES_CCIT;
- }
- else
- {
- SX126x.PacketParams.Params.Gfsk.CrcLength = RADIO_CRC_OFF;
- }
- SX126x.PacketParams.Params.Gfsk.DcFree = RADIO_DC_FREEWHITENING;
- RadioStandby( );
- RadioSetModem( ( SX126x.ModulationParams.PacketType == PACKET_TYPE_GFSK ) ? MODEM_FSK : MODEM_LORA );
- SX126xSetModulationParams( &SX126x.ModulationParams );
- SX126xSetPacketParams( &SX126x.PacketParams );
- SX126xSetSyncWord( syncWord );
- SX126xSetWhiteningSeed( 0x01FF );
- break;
- case MODEM_LORA:
- SX126x.ModulationParams.PacketType = PACKET_TYPE_LORA;
- SX126x.ModulationParams.Params.LoRa.SpreadingFactor = ( RadioLoRaSpreadingFactors_t ) datarate;
- SX126x.ModulationParams.Params.LoRa.Bandwidth = Bandwidths[bandwidth];
- SX126x.ModulationParams.Params.LoRa.CodingRate= ( RadioLoRaCodingRates_t )coderate;
- if( ( ( bandwidth == 0 ) && ( ( datarate == 11 ) || ( datarate == 12 ) ) ) ||
- ( ( bandwidth == 1 ) && ( datarate == 12 ) ) )
- {
- SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize = 0x01;
- }
- else
- {
- SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize = 0x00;
- }
- SX126x.PacketParams.PacketType = PACKET_TYPE_LORA;
- if( ( SX126x.ModulationParams.Params.LoRa.SpreadingFactor == LORA_SF5 ) ||
- ( SX126x.ModulationParams.Params.LoRa.SpreadingFactor == LORA_SF6 ) )
- {
- if( preambleLen < 12 )
- {
- SX126x.PacketParams.Params.LoRa.PreambleLength = 12;
- }
- else
- {
- SX126x.PacketParams.Params.LoRa.PreambleLength = preambleLen;
- }
- }
- else
- {
- SX126x.PacketParams.Params.LoRa.PreambleLength = preambleLen;
- }
- SX126x.PacketParams.Params.LoRa.HeaderType = ( RadioLoRaPacketLengthsMode_t )fixLen;
- SX126x.PacketParams.Params.LoRa.PayloadLength = MaxPayloadLength;
- SX126x.PacketParams.Params.LoRa.CrcMode = ( RadioLoRaCrcModes_t )crcOn;
- SX126x.PacketParams.Params.LoRa.InvertIQ = ( RadioLoRaIQModes_t )iqInverted;
- RadioStandby( );
- RadioSetModem( ( SX126x.ModulationParams.PacketType == PACKET_TYPE_GFSK ) ? MODEM_FSK : MODEM_LORA );
- SX126xSetModulationParams( &SX126x.ModulationParams );
- SX126xSetPacketParams( &SX126x.PacketParams );
- break;
- }
- SX126xSetRfTxPower( power );
- TxTimeout = timeout;
- }
- bool RadioCheckRfFrequency( uint32_t frequency )
- {
- return true;
- }
- uint32_t RadioTimeOnAir( RadioModems_t modem, uint8_t pktLen )
- {
- uint32_t airTime = 0;
- switch( modem )
- {
- case MODEM_FSK:
- {
- airTime = rint( ( 8 * ( SX126x.PacketParams.Params.Gfsk.PreambleLength +
- ( SX126x.PacketParams.Params.Gfsk.SyncWordLength >> 3 ) +
- ( ( SX126x.PacketParams.Params.Gfsk.HeaderType == RADIO_PACKET_FIXED_LENGTH ) ? 0.0 : 1.0 ) +
- pktLen +
- ( ( SX126x.PacketParams.Params.Gfsk.CrcLength == RADIO_CRC_2_BYTES ) ? 2.0 : 0 ) ) /
- SX126x.ModulationParams.Params.Gfsk.BitRate ) * 1e3 );
- }
- break;
- case MODEM_LORA:
- {
- double ts = RadioLoRaSymbTime[SX126x.ModulationParams.Params.LoRa.Bandwidth - 3][12 - SX126x.ModulationParams.Params.LoRa.SpreadingFactor];
- // time of preamble
- double tPreamble = ( SX126x.PacketParams.Params.LoRa.PreambleLength + 4.25 ) * ts;
- // Symbol length of payload and time
- double tmp = ceil( ( 8 * pktLen - 4 * SX126x.ModulationParams.Params.LoRa.SpreadingFactor +
- 28 + 16 * SX126x.PacketParams.Params.LoRa.CrcMode -
- ( ( SX126x.PacketParams.Params.LoRa.HeaderType == LORA_PACKET_FIXED_LENGTH ) ? 20 : 0 ) ) /
- ( double )( 4 * ( SX126x.ModulationParams.Params.LoRa.SpreadingFactor -
- ( ( SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize > 0 ) ? 2 : 0 ) ) ) ) *
- ( ( SX126x.ModulationParams.Params.LoRa.CodingRate % 4 ) + 4 );
- double nPayload = 8 + ( ( tmp > 0 ) ? tmp : 0 );
- double tPayload = nPayload * ts;
- // Time on air
- double tOnAir = tPreamble + tPayload;
- // return milli seconds
- airTime = floor( tOnAir + 0.999 );
- }
- break;
- }
- return airTime;
- }
- void RadioSend( uint8_t *buffer, uint8_t size )
- {
- SX126xSetDioIrqParams( IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT,
- IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT,
- IRQ_RADIO_NONE,
- IRQ_RADIO_NONE );
- if( SX126xGetPacketType( ) == PACKET_TYPE_LORA )
- {
- SX126x.PacketParams.Params.LoRa.PayloadLength = size;
- }
- else
- {
- SX126x.PacketParams.Params.Gfsk.PayloadLength = size;
- }
- SX126xSetPacketParams( &SX126x.PacketParams );
- SX126xSendPayload( buffer, size, 0 );
- // TimerSetValue( &TxTimeoutTimer, TxTimeout );
- // TimerStart( &TxTimeoutTimer );
- }
- void RadioSleep( void )
- {
- SleepParams_t params = { 0 };
- params.Fields.WarmStart = 1;
- SX126xSetSleep( params );
- HAL_Delay_nMS( 2 );
- }
- void RadioStandby( void )
- {
- SX126xSetStandby( STDBY_RC );
- }
- void RadioRx( uint32_t timeout )
- {
- // SX126xSetDioIrqParams( IRQ_RADIO_ALL, //IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT,
- // IRQ_RADIO_ALL, //IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT,
- // IRQ_RADIO_NONE,
- // IRQ_RADIO_NONE );
-
- SX126xSetDioIrqParams( IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT | IRQ_CRC_ERROR,
- IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT | IRQ_CRC_ERROR,
- IRQ_RADIO_NONE,
- IRQ_RADIO_NONE );
-
- // SX126xSetDioIrqParams( IRQ_SYNCWORD_VALID,
- // IRQ_SYNCWORD_VALID,
- // IRQ_RADIO_NONE,
- // IRQ_RADIO_NONE );
- if( timeout != 0 )
- {
- // TimerSetValue( &RxTimeoutTimer, timeout );
- // TimerStart( &RxTimeoutTimer );
- }
- if( RxContinuous == true )
- {
- SX126xSetRx( 0xFFFFFF ); // Rx Continuous
- }
- else
- {
- SX126xSetRx( timeout << 6 );
- }
- }
- void RadioRxBoosted( uint32_t timeout )
- {
- SX126xSetDioIrqParams( IRQ_RADIO_ALL, //IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT,
- IRQ_RADIO_ALL, //IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT,
- IRQ_RADIO_NONE,
- IRQ_RADIO_NONE );
- if( timeout != 0 )
- {
- // TimerSetValue( &RxTimeoutTimer, timeout );
- // TimerStart( &RxTimeoutTimer );
- }
- if( RxContinuous == true )
- {
- SX126xSetRxBoosted( 0xFFFFFF ); // Rx Continuous
- }
- else
- {
- SX126xSetRxBoosted( timeout << 6 );
- }
- }
- void RadioSetRxDutyCycle( uint32_t rxTime, uint32_t sleepTime )
- {
- SX126xSetRxDutyCycle( rxTime, sleepTime );
- }
- void RadioStartCad( void )
- {
- SX126xSetCad( );
- }
- void RadioTx( uint32_t timeout )
- {
- SX126xSetTx( timeout << 6 );
- }
- void RadioSetTxContinuousWave( uint32_t freq, int8_t power, uint16_t time )
- {
- SX126xSetRfFrequency( freq );
- SX126xSetRfTxPower( power );
- SX126xSetTxContinuousWave( );
- // TimerSetValue( &RxTimeoutTimer, time * 1e3 );
- // TimerStart( &RxTimeoutTimer );
- }
- int16_t RadioRssi( RadioModems_t modem )
- {
- return SX126xGetRssiInst( );
- }
- void RadioWrite( uint16_t addr, uint8_t data )
- {
- SX126xWriteRegister( addr, data );
- }
- uint8_t RadioRead( uint16_t addr )
- {
- return SX126xReadRegister( addr );
- }
- void RadioWriteBuffer( uint16_t addr, uint8_t *buffer, uint8_t size )
- {
- SX126xWriteRegisters( addr, buffer, size );
- }
- void RadioReadBuffer( uint16_t addr, uint8_t *buffer, uint8_t size )
- {
- SX126xReadRegisters( addr, buffer, size );
- }
- void RadioWriteFifo( uint8_t *buffer, uint8_t size )
- {
- SX126xWriteBuffer( 0, buffer, size );
- }
- void RadioReadFifo( uint8_t *buffer, uint8_t size )
- {
- SX126xReadBuffer( 0, buffer, size );
- }
- void RadioSetMaxPayloadLength( RadioModems_t modem, uint8_t max )
- {
- if( modem == MODEM_LORA )
- {
- SX126x.PacketParams.Params.LoRa.PayloadLength = MaxPayloadLength = max;
- SX126xSetPacketParams( &SX126x.PacketParams );
- }
- else
- {
- if( SX126x.PacketParams.Params.Gfsk.HeaderType == RADIO_PACKET_VARIABLE_LENGTH )
- {
- SX126x.PacketParams.Params.Gfsk.PayloadLength = MaxPayloadLength = max;
- SX126xSetPacketParams( &SX126x.PacketParams );
- }
- }
- }
- void RadioSetPublicNetwork( bool enable )
- {
- RadioPublicNetwork.Current = RadioPublicNetwork.Previous = enable;
- RadioSetModem( MODEM_LORA );
- if( enable == true )
- {
- // Change LoRa modem SyncWord
- SX126xWriteRegister( REG_LR_SYNCWORD, ( LORA_MAC_PUBLIC_SYNCWORD >> 8 ) & 0xFF );
- SX126xWriteRegister( REG_LR_SYNCWORD + 1, LORA_MAC_PUBLIC_SYNCWORD & 0xFF );
- }
- else
- {
- // Change LoRa modem SyncWord
- SX126xWriteRegister( REG_LR_SYNCWORD, ( LORA_MAC_PRIVATE_SYNCWORD >> 8 ) & 0xFF );
- SX126xWriteRegister( REG_LR_SYNCWORD + 1, LORA_MAC_PRIVATE_SYNCWORD & 0xFF );
- }
- }
- uint32_t RadioGetWakeupTime( void )
- {
- return( RADIO_TCXO_SETUP_TIME + RADIO_WAKEUP_TIME );
- }
- void RadioOnTxTimeoutIrq( void )
- {
- if( ( RadioEvents != NULL ) && ( RadioEvents->TxTimeout != NULL ) )
- {
- RadioEvents->TxTimeout( );
- }
- }
- void RadioOnRxTimeoutIrq( void )
- {
- if( ( RadioEvents != NULL ) && ( RadioEvents->RxTimeout != NULL ) )
- {
- RadioEvents->RxTimeout( );
- }
- }
- void RadioOnDioIrq( void )
- {
- IrqFired = true;
- }
- void RadioIrqProcess( void )
- {
- uint16_t irqRegs;
- if( IrqFired == true )
- // if( READ_RF_SX126x_IO1())
- {
- //BoardDisableIrq( );
- IrqFired = false;
- // BoardEnableIrq( );
- irqRegs = SX126xGetIrqStatus( );
- SX126xClearIrqStatus( IRQ_RADIO_ALL );
-
- if( ( irqRegs & IRQ_TX_DONE ) == IRQ_TX_DONE )
- {
- //TimerStop( &TxTimeoutTimer );
- if( ( RadioEvents != NULL ) && ( RadioEvents->TxDone != NULL ) )
- {
- RadioEvents->TxDone( );
- }
- }
- if( ( irqRegs & IRQ_RX_DONE ) == IRQ_RX_DONE )
- {
- uint8_t size;
- // TimerStop( &RxTimeoutTimer );
- SX126xGetPayload( RadioRxPayload, &size , 255 );
- SX126xGetPacketStatus( &RadioPktStatus );
- if( ( RadioEvents != NULL ) && ( RadioEvents->RxDone != NULL ) )
- {
- // #ifdef USE_MODEM_LORA
- RadioEvents->RxDone( RadioRxPayload, size, RadioPktStatus.Params.LoRa.RssiPkt+RadioPktStatus.Params.LoRa.SnrPkt, RadioPktStatus.Params.LoRa.SnrPkt );
- // #else
- // RadioEvents->RxDone( RadioRxPayload, size, RadioPktStatus.Params.Gfsk.RssiSync, RadioPktStatus.Params.Gfsk.RssiAvg );
- // #endif
- }
- }
- if( ( irqRegs & IRQ_CRC_ERROR ) == IRQ_CRC_ERROR )
- {
- if( ( RadioEvents != NULL ) && ( RadioEvents->RxError ) )
- {
- RadioEvents->RxError( );
- }
- }
- if( ( irqRegs & IRQ_CAD_DONE ) == IRQ_CAD_DONE )
- {
- if( ( RadioEvents != NULL ) && ( RadioEvents->CadDone != NULL ) )
- {
- RadioEvents->CadDone( ( ( irqRegs & IRQ_CAD_ACTIVITY_DETECTED ) == IRQ_CAD_ACTIVITY_DETECTED ) );
- }
- }
- if( ( irqRegs & IRQ_RX_TX_TIMEOUT ) == IRQ_RX_TX_TIMEOUT )
- {
- if( SX126xGetOperatingMode( ) == MODE_TX )
- {
- // TimerStop( &TxTimeoutTimer );
- if( ( RadioEvents != NULL ) && ( RadioEvents->TxTimeout != NULL ) )
- {
- RadioEvents->TxTimeout( );
- }
- }
- else if( SX126xGetOperatingMode( ) == MODE_RX )
- {
- // TimerStop( &RxTimeoutTimer );
- if( ( RadioEvents != NULL ) && ( RadioEvents->RxTimeout != NULL ) )
- {
- RadioEvents->RxTimeout( );
- }
- }
- }
- if( ( irqRegs & IRQ_PREAMBLE_DETECTED ) == IRQ_PREAMBLE_DETECTED )
- {
- //__NOP( );
- }
- if( ( irqRegs & IRQ_SYNCWORD_VALID ) == IRQ_SYNCWORD_VALID )
- {
- //__NOP( );
- }
- if( ( irqRegs & IRQ_HEADER_VALID ) == IRQ_HEADER_VALID )
- {
- //__NOP( );
- }
- if( ( irqRegs & IRQ_HEADER_ERROR ) == IRQ_HEADER_ERROR )
- {
- // TimerStop( &RxTimeoutTimer );
- if( ( RadioEvents != NULL ) && ( RadioEvents->RxTimeout != NULL ) )
- {
- RadioEvents->RxTimeout( );
- }
- }
- }
- }
|