2929#include "sx1272.h"
3030#include "sx1272-board.h"
3131
32+ /*!
33+ * \brief Internal frequency of the radio
34+ */
35+ #define SX1272_XTAL_FREQ 32000000UL
36+
37+ /*!
38+ * \brief Scaling factor used to perform fixed-point operations
39+ */
40+ #define SX1272_PLL_STEP_SHIFT_AMOUNT ( 8 )
41+
42+ /*!
43+ * \brief PLL step - scaled with SX1276_PLL_STEP_SHIFT_AMOUNT
44+ */
45+ #define SX1272_PLL_STEP_SCALED ( SX1272_XTAL_FREQ >> ( 19 - SX1272_PLL_STEP_SHIFT_AMOUNT ) )
46+
47+ /*!
48+ * \brief Radio buffer size
49+ */
50+ #define RX_TX_BUFFER_SIZE 256
51+
3252/*
3353 * Local types definition
3454 */
@@ -87,25 +107,43 @@ static void SX1272ReadFifo( uint8_t *buffer, uint8_t size );
87107 */
88108static void SX1272SetOpMode ( uint8_t opMode );
89109
90- /**
91- * @brief Get the parameter corresponding to a FSK Rx bandwith immediately above the minimum requested one.
110+ /*!
111+ * \brief Get frequency in Hertz for a given number of PLL steps
112+ *
113+ * \param [in] pllSteps Number of PLL steps
114+ *
115+ * \returns Frequency in Hertz
116+ */
117+ static uint32_t SX1272ConvertPllStepToFreqInHz ( uint32_t pllSteps );
118+
119+ /*!
120+ * \brief Get the number of PLL steps for a given frequency in Hertz
92121 *
93- * @ param [in] bw Minimum required bandwith in Hz
122+ * \ param [in] freqInHz Frequency in Hertz
94123 *
95- * @returns parameter
124+ * \returns Number of PLL steps
125+ */
126+ static uint32_t SX1272ConvertFreqInHzToPllStep ( uint32_t freqInHz );
127+
128+ /*!
129+ * \brief Get the parameter corresponding to a FSK Rx bandwith immediately above the minimum requested one.
130+ *
131+ * \param [in] bw Minimum required bandwith in Hz
132+ *
133+ * \returns parameter
96134 */
97135static uint8_t GetFskBandwidthRegValue ( uint32_t bw );
98136
99- /**
100- * Get the actual value in Hertz of a given LoRa bandwidth
137+ /*!
138+ * \brief Get the actual value in Hertz of a given LoRa bandwidth
101139 *
102140 * \param [in] bw LoRa bandwidth parameter
103141 *
104142 * \returns Actual LoRa bandwidth in Hertz
105143 */
106144static uint32_t SX1272GetLoRaBandwidthInHz ( uint32_t bw );
107145
108- /**
146+ /*!
109147 * Compute the numerator for GFSK time-on-air computation.
110148 *
111149 * \remark To get the actual time-on-air in second, this value has to be divided by the GFSK bitrate in bits per
@@ -121,7 +159,7 @@ static uint32_t SX1272GetLoRaBandwidthInHz( uint32_t bw );
121159static uint32_t SX1272GetGfskTimeOnAirNumerator ( uint16_t preambleLen , bool fixLen ,
122160 uint8_t payloadLen , bool crcOn );
123161
124- /**
162+ /*!
125163 * Compute the numerator for LoRa time-on-air computation.
126164 *
127165 * \remark To get the actual time-on-air in second, this value has to be divided by the LoRa bandwidth in Hertz.
@@ -232,7 +270,7 @@ static RadioEvents_t *RadioEvents;
232270/*!
233271 * Reception buffer
234272 */
235- static uint8_t RxTxBuffer [RX_BUFFER_SIZE ];
273+ static uint8_t RxTxBuffer [RX_TX_BUFFER_SIZE ];
236274
237275/*
238276 * Public global variables
@@ -296,11 +334,13 @@ RadioState_t SX1272GetStatus( void )
296334
297335void SX1272SetChannel ( uint32_t freq )
298336{
337+ uint32_t freqInPllSteps = SX1272ConvertFreqInHzToPllStep ( freq );
338+
299339 SX1272 .Settings .Channel = freq ;
300- freq = ( uint32_t )( ( double ) freq / ( double ) FREQ_STEP );
301- SX1272Write ( REG_FRFMSB , ( uint8_t )( ( freq >> 16 ) & 0xFF ) );
302- SX1272Write ( REG_FRFMID , ( uint8_t )( ( freq >> 8 ) & 0xFF ) );
303- SX1272Write ( REG_FRFLSB , ( uint8_t )( freq & 0xFF ) );
340+
341+ SX1272Write ( REG_FRFMSB , ( uint8_t )( ( freqInPllSteps >> 16 ) & 0xFF ) );
342+ SX1272Write ( REG_FRFMID , ( uint8_t )( ( freqInPllSteps >> 8 ) & 0xFF ) );
343+ SX1272Write ( REG_FRFLSB , ( uint8_t )( freqInPllSteps & 0xFF ) );
304344}
305345
306346bool SX1272IsChannelFree ( uint32_t freq , uint32_t rxBandwidth , int16_t rssiThresh , uint32_t maxCarrierSenseTime )
@@ -398,11 +438,11 @@ void SX1272SetRxConfig( RadioModems_t modem, uint32_t bandwidth,
398438 SX1272 .Settings .Fsk .IqInverted = iqInverted ;
399439 SX1272 .Settings .Fsk .RxContinuous = rxContinuous ;
400440 SX1272 .Settings .Fsk .PreambleLen = preambleLen ;
401- SX1272 .Settings .Fsk .RxSingleTimeout = ( uint32_t )( symbTimeout * ( ( 1.0 / ( double ) datarate ) * 8.0 ) * 1000 ) ;
441+ SX1272 .Settings .Fsk .RxSingleTimeout = ( uint32_t )symbTimeout * 8000UL / datarate ;
402442
403- datarate = ( uint16_t )( ( double ) XTAL_FREQ / ( double ) datarate );
404- SX1272Write ( REG_BITRATEMSB , ( uint8_t )( datarate >> 8 ) );
405- SX1272Write ( REG_BITRATELSB , ( uint8_t )( datarate & 0xFF ) );
443+ uint32_t bitRate = ( uint32_t )( SX1272_XTAL_FREQ / datarate );
444+ SX1272Write ( REG_BITRATEMSB , ( uint8_t )( bitRate >> 8 ) );
445+ SX1272Write ( REG_BITRATELSB , ( uint8_t )( bitRate & 0xFF ) );
406446
407447 SX1272Write ( REG_RXBW , GetFskBandwidthRegValue ( bandwidth ) );
408448 SX1272Write ( REG_AFCBW , GetFskBandwidthRegValue ( bandwidthAfc ) );
@@ -542,13 +582,13 @@ void SX1272SetTxConfig( RadioModems_t modem, int8_t power, uint32_t fdev,
542582 SX1272 .Settings .Fsk .IqInverted = iqInverted ;
543583 SX1272 .Settings .Fsk .TxTimeout = timeout ;
544584
545- fdev = ( uint16_t )( ( double ) fdev / ( double ) FREQ_STEP );
546- SX1272Write ( REG_FDEVMSB , ( uint8_t )( fdev >> 8 ) );
547- SX1272Write ( REG_FDEVLSB , ( uint8_t )( fdev & 0xFF ) );
585+ uint32_t fdevInPllSteps = SX1272ConvertFreqInHzToPllStep ( fdev );
586+ SX1272Write ( REG_FDEVMSB , ( uint8_t )( fdevInPllSteps >> 8 ) );
587+ SX1272Write ( REG_FDEVLSB , ( uint8_t )( fdevInPllSteps & 0xFF ) );
548588
549- datarate = ( uint16_t )( ( double ) XTAL_FREQ / ( double ) datarate );
550- SX1272Write ( REG_BITRATEMSB , ( uint8_t )( datarate >> 8 ) );
551- SX1272Write ( REG_BITRATELSB , ( uint8_t )( datarate & 0xFF ) );
589+ uint32_t bitRate = ( uint32_t )( SX1272_XTAL_FREQ / datarate );
590+ SX1272Write ( REG_BITRATEMSB , ( uint8_t )( bitRate >> 8 ) );
591+ SX1272Write ( REG_BITRATELSB , ( uint8_t )( bitRate & 0xFF ) );
552592
553593 SX1272Write ( REG_PREAMBLEMSB , ( preambleLen >> 8 ) & 0x00FF );
554594 SX1272Write ( REG_PREAMBLELSB , preambleLen & 0xFF );
@@ -857,7 +897,7 @@ void SX1272SetRx( uint32_t timeout )
857897 break ;
858898 }
859899
860- memset ( RxTxBuffer , 0 , ( size_t )RX_BUFFER_SIZE );
900+ memset ( RxTxBuffer , 0 , ( size_t )RX_TX_BUFFER_SIZE );
861901
862902 SX1272 .Settings .State = RF_RX_RUNNING ;
863903 if ( timeout != 0 )
@@ -1192,6 +1232,37 @@ uint32_t SX1272GetWakeupTime( void )
11921232 return SX1272GetBoardTcxoWakeupTime ( ) + RADIO_WAKEUP_TIME ;
11931233}
11941234
1235+ static uint32_t SX1272ConvertPllStepToFreqInHz ( uint32_t pllSteps )
1236+ {
1237+ uint32_t freqInHzInt ;
1238+ uint32_t freqInHzFrac ;
1239+
1240+ // freqInHz = pllSteps * ( SX1272_XTAL_FREQ / 2^19 )
1241+ // Get integer and fractional parts of the frequency computed with a PLL step scaled value
1242+ freqInHzInt = pllSteps >> SX1272_PLL_STEP_SHIFT_AMOUNT ;
1243+ freqInHzFrac = pllSteps - ( freqInHzInt << SX1272_PLL_STEP_SHIFT_AMOUNT );
1244+
1245+ // Apply the scaling factor to retrieve a frequency in Hz (+ ceiling)
1246+ return freqInHzInt * SX1272_PLL_STEP_SCALED +
1247+ ( ( freqInHzFrac * SX1272_PLL_STEP_SCALED + ( 128 ) ) >> SX1272_PLL_STEP_SHIFT_AMOUNT );
1248+ }
1249+
1250+ static uint32_t SX1272ConvertFreqInHzToPllStep ( uint32_t freqInHz )
1251+ {
1252+ uint32_t stepsInt ;
1253+ uint32_t stepsFrac ;
1254+
1255+ // pllSteps = freqInHz / (SX1272_XTAL_FREQ / 2^19 )
1256+ // Get integer and fractional parts of the frequency computed with a PLL step scaled value
1257+ stepsInt = freqInHz / SX1272_PLL_STEP_SCALED ;
1258+ stepsFrac = freqInHz - ( stepsInt * SX1272_PLL_STEP_SCALED );
1259+
1260+ // Apply the scaling factor to retrieve a frequency in Hz (+ ceiling)
1261+ return ( stepsInt << SX1272_PLL_STEP_SHIFT_AMOUNT ) +
1262+ ( ( ( stepsFrac << SX1272_PLL_STEP_SHIFT_AMOUNT ) + ( SX1272_PLL_STEP_SCALED >> 1 ) ) /
1263+ SX1272_PLL_STEP_SCALED );
1264+ }
1265+
11951266static uint8_t GetFskBandwidthRegValue ( uint32_t bw )
11961267{
11971268 uint8_t i ;
@@ -1692,9 +1763,8 @@ static void SX1272OnDio2Irq( void* context )
16921763
16931764 SX1272 .Settings .FskPacketHandler .RssiValue = - ( SX1272Read ( REG_RSSIVALUE ) >> 1 );
16941765
1695- SX1272 .Settings .FskPacketHandler .AfcValue = ( int32_t )( ( double )( ( ( uint16_t )SX1272Read ( REG_AFCMSB ) << 8 ) |
1696- ( uint16_t )SX1272Read ( REG_AFCLSB ) ) *
1697- ( double )FREQ_STEP );
1766+ SX1272 .Settings .FskPacketHandler .AfcValue = ( int32_t )SX1272ConvertPllStepToFreqInHz ( ( ( uint16_t )SX1272Read ( REG_AFCMSB ) << 8 ) |
1767+ ( uint16_t )SX1272Read ( REG_AFCLSB ) );
16981768 SX1272 .Settings .FskPacketHandler .RxGain = ( SX1272Read ( REG_LNA ) >> 5 ) & 0x07 ;
16991769 }
17001770 break ;
0 commit comments