1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177 |
- #include <math.h>
- #include <string.h>
- #include <stdbool.h>
- #include "board.h"
- #include "radio.h"
- #include "sx126x.h"
- #include "sx126x-board.h"
- void RadioInit( RadioEvents_t *events );
- RadioState_t RadioGetStatus( void );
- void RadioSetModem( RadioModems_t modem );
- void RadioSetChannel( uint32_t freq );
- bool RadioIsChannelFree( RadioModems_t modem, uint32_t freq, int16_t rssiThresh, uint32_t maxCarrierSenseTime );
- uint32_t RadioRandom( void );
- 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 );
- 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 );
- bool RadioCheckRfFrequency( uint32_t frequency );
- uint32_t RadioTimeOnAir( RadioModems_t modem, uint8_t pktLen );
- void RadioSend( uint8_t *buffer, uint8_t size );
- void RadioSleep( void );
- void RadioStandby( void );
- void RadioRx( uint32_t timeout );
- void RadioStartCad( void );
- void RadioSetTxContinuousWave( uint32_t freq, int8_t power, uint16_t time );
- int16_t RadioRssi( RadioModems_t modem );
- void RadioWrite( uint16_t addr, uint8_t data );
- uint8_t RadioRead( uint16_t addr );
- void RadioWriteBuffer( uint16_t addr, uint8_t *buffer, uint8_t size );
- void RadioReadBuffer( uint16_t addr, uint8_t *buffer, uint8_t size );
- void RadioSetMaxPayloadLength( RadioModems_t modem, uint8_t max );
- void RadioSetPublicNetwork( bool enable );
- uint32_t RadioGetWakeupTime( void );
- void RadioIrqProcess( void );
- void RadioRxBoosted( uint32_t timeout );
- void RadioSetRxDutyCycle( uint32_t rxTime, uint32_t sleepTime );
- 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,
-
- RadioRxBoosted,
- RadioSetRxDutyCycle
- };
-
- typedef struct
- {
- uint32_t bandwidth;
- uint8_t RegValue;
- }FskBandwidth_t;
- 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 },
- };
- const RadioLoRaBandwidths_t Bandwidths[] = {LORA_BW_062, LORA_BW_125, LORA_BW_250, LORA_BW_500 };
- static double RadioLoRaSymbTime[4][6] = {
- { 65.536, 32.768, 16.384, 8.192, 4.096, 2.048 },
- { 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 }};
- 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 };
- void RadioOnDioIrq( void );
- void RadioOnTxTimeoutIrq( void );
- void RadioOnRxTimeoutIrq( void );
- typedef struct
- {
- bool Previous;
- bool Current;
- }RadioPublicNetwork_t;
- static RadioPublicNetwork_t RadioPublicNetwork = { false };
- static RadioEvents_t* RadioEvents;
- SX126x_t SX126x;
- 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;
- }
- }
-
- while( 1 );
- }
- void RadioInit( RadioEvents_t *events )
- {
- RadioEvents = events;
-
- SX126xInit( 0 );
- SX126xSetStandby( STDBY_RC );
- SX126xSetRegulatorMode( USE_DCDC );
-
- SX126xSetBufferBaseAddress( 0x00, 0x00 );
- SX126xSetTxParams( 0, RADIO_RAMP_200_US );
- SX126xSetDioIrqParams( IRQ_RADIO_ALL, IRQ_RADIO_ALL, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
-
-
-
-
-
- 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 );
-
-
- RadioPublicNetwork.Current = false;
- break;
- case MODEM_LORA:
- SX126xSetPacketType( PACKET_TYPE_LORA );
-
- 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 );
-
-
- RadioSleep( );
- return status;
- }
- uint32_t RadioRandom( void )
- {
- uint8_t i;
- uint32_t rnd = 0;
-
-
- RadioSetModem( MODEM_LORA );
-
- SX126xSetRx( 0 );
- for( i = 0; i < 32; i++ )
- {
- HAL_Delay_nMS( 1 );
-
- 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 );
- SX126x.PacketParams.Params.Gfsk.PreambleMinDetect = RADIO_PREAMBLE_DETECTOR_16_BITS;
- SX126x.PacketParams.Params.Gfsk.SyncWordLength = 3 << 3;
- 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 );
-
- 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 );
- SX126x.PacketParams.Params.Gfsk.PreambleMinDetect = RADIO_PREAMBLE_DETECTOR_08_BITS;
- SX126x.PacketParams.Params.Gfsk.SyncWordLength = 3 << 3 ;
- 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 - 4][12 - SX126x.ModulationParams.Params.LoRa.SpreadingFactor];
-
- double tPreamble = ( SX126x.PacketParams.Params.LoRa.PreambleLength + 4.25 ) * ts;
-
- 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;
-
- double tOnAir = tPreamble + tPayload;
-
- 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 );
- }
- 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_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 );
-
- if( timeout != 0 )
- {
- }
- if( RxContinuous == true )
- {
- SX126xSetRx( 0xFFFFFF );
- }
- else
- {
- SX126xSetRx( timeout << 6 );
- }
- }
- void RadioRxBoosted( uint32_t timeout )
- {
- SX126xSetDioIrqParams( IRQ_RADIO_ALL,
- IRQ_RADIO_ALL,
- IRQ_RADIO_NONE,
- IRQ_RADIO_NONE );
- if( timeout != 0 )
- {
- }
- if( RxContinuous == true )
- {
- SX126xSetRxBoosted( 0xFFFFFF );
- }
- 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( );
- }
- 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 )
- {
-
- SX126xWriteRegister( REG_LR_SYNCWORD, ( LORA_MAC_PUBLIC_SYNCWORD >> 8 ) & 0xFF );
- SX126xWriteRegister( REG_LR_SYNCWORD + 1, LORA_MAC_PUBLIC_SYNCWORD & 0xFF );
- }
- else
- {
-
- 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 )
-
- {
-
- IrqFired = false;
-
- irqRegs = SX126xGetIrqStatus( );
- SX126xClearIrqStatus( IRQ_RADIO_ALL );
-
- if( ( irqRegs & IRQ_TX_DONE ) == IRQ_TX_DONE )
- {
-
- if( ( RadioEvents != NULL ) && ( RadioEvents->TxDone != NULL ) )
- {
- RadioEvents->TxDone( );
- }
- }
- if( ( irqRegs & IRQ_RX_DONE ) == IRQ_RX_DONE )
- {
- uint8_t size;
-
- SX126xGetPayload( RadioRxPayload, &size , 255 );
- SX126xGetPacketStatus( &RadioPktStatus );
- if( ( RadioEvents != NULL ) && ( RadioEvents->RxDone != NULL ) )
- {
-
- RadioEvents->RxDone( RadioRxPayload, size, RadioPktStatus.Params.LoRa.RssiPkt+RadioPktStatus.Params.LoRa.SnrPkt, RadioPktStatus.Params.LoRa.SnrPkt );
-
-
-
- }
- }
- 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 )
- {
-
- if( ( RadioEvents != NULL ) && ( RadioEvents->TxTimeout != NULL ) )
- {
- RadioEvents->TxTimeout( );
- }
- }
- else if( SX126xGetOperatingMode( ) == MODE_RX )
- {
-
- if( ( RadioEvents != NULL ) && ( RadioEvents->RxTimeout != NULL ) )
- {
- RadioEvents->RxTimeout( );
- }
- }
- }
- if( ( irqRegs & IRQ_PREAMBLE_DETECTED ) == IRQ_PREAMBLE_DETECTED )
- {
-
- }
- if( ( irqRegs & IRQ_SYNCWORD_VALID ) == IRQ_SYNCWORD_VALID )
- {
-
- }
- if( ( irqRegs & IRQ_HEADER_VALID ) == IRQ_HEADER_VALID )
- {
-
- }
- if( ( irqRegs & IRQ_HEADER_ERROR ) == IRQ_HEADER_ERROR )
- {
-
- if( ( RadioEvents != NULL ) && ( RadioEvents->RxTimeout != NULL ) )
- {
- RadioEvents->RxTimeout( );
- }
- }
- }
- }
|