SX1261 and sx1262 common library

Dependents:   SX126xDevKit SX1262PingPong SX126X_TXonly SX126X_PingPong_Demo ... more

Fork of SX126xLib by Gregory Cristian

Revision:
5:e488e6f185f3
Parent:
4:c6ef863d0b07
Child:
6:1e2345700991
--- a/sx126x.cpp	Mon Sep 04 15:16:44 2017 +0000
+++ b/sx126x.cpp	Fri Mar 09 13:19:45 2018 +0000
@@ -31,8 +31,16 @@
  */
 volatile uint32_t FrequencyError = 0;
 
+/*!
+ * \brief Hold the status of the Image calibration
+ */
+static bool ImageCalibrated = false;
+
+
 void SX126x::Init( void )
 {
+    CalibrationParams_t calibParam;
+    
     /*!
      * \brief pin OPT is used to detect if the board has a TCXO or a XTAL
      *
@@ -49,15 +57,18 @@
 
     if( OPT == 0 )
     {
-        SetDio3AsTcxoCtrl( TCXO_CTRL_1_7V, 6400 ); //100 ms
-        WriteReg( REG_XTA_TRIM, 0x2F );
+        SetDio3AsTcxoCtrl( TCXO_CTRL_1_7V, 320 ); //5 ms
+        calibParam.Value = 0x7F;
+        Calibrate( calibParam );
     }
 
     SetPollingMode( );
-    
+
     AntSwOn( );
     SetDio2AsRfSwitchCtrl( true );
-
+    
+    OperatingMode = MODE_STDBY_RC;
+    
     SetPacketType( PACKET_TYPE_LORA );
 
 #ifdef USE_CONFIG_PUBLIC_NETOWRK
@@ -277,8 +288,6 @@
 {
     uint8_t buf[6];
 
-    SetLongPreamble( true );
-
     buf[0] = ( uint8_t )( ( rxTime >> 16 ) & 0xFF );
     buf[1] = ( uint8_t )( ( rxTime >> 8 ) & 0xFF );
     buf[2] = ( uint8_t )( rxTime & 0xFF );
@@ -334,9 +343,36 @@
     WriteCommand( RADIO_CALIBRATE, &calibParam.Value, 1 );
 }
 
-void SX126x::SetLongPreamble( uint8_t enable )
+void SX126x::CalibrateImage( uint32_t freq )
 {
-    WriteCommand( RADIO_SET_LONGPREAMBLE, &enable, 1 );
+    uint8_t calFreq[2];
+
+    if( freq > 900000000 )
+    {
+        calFreq[0] = 0xE1;
+        calFreq[1] = 0xE9;
+    }
+    else if( freq > 850000000 )
+    {
+        calFreq[0] = 0xD7;
+        calFreq[1] = 0xD8;
+    }
+    else if( freq > 770000000 )
+    {
+        calFreq[0] = 0xC1;
+        calFreq[1] = 0xC5;
+    }
+    else if( freq > 460000000 )
+    {
+        calFreq[0] = 0x75;
+        calFreq[1] = 0x81;
+    }
+    else if( freq > 425000000 )
+    {
+        calFreq[0] = 0x6B;
+        calFreq[1] = 0x6F;
+    }
+    WriteCommand( RADIO_CALIBRATEIMAGE, calFreq, 2 );
 }
 
 void SX126x::SetPaConfig( uint8_t paDutyCycle, uint8_t HpMax, uint8_t deviceSel, uint8_t paLUT )
@@ -397,8 +433,8 @@
 void SX126x::SetDio3AsTcxoCtrl( RadioTcxoCtrlVoltage_t tcxoVoltage, uint32_t timeout )
 {
     uint8_t buf[4];
-    
-    buf[0] = tcxoVoltage & 0x08;
+
+    buf[0] = tcxoVoltage & 0x07;
     buf[1] = ( uint8_t )( ( timeout >> 16 ) & 0xFF );
     buf[2] = ( uint8_t )( ( timeout >> 8 ) & 0xFF );
     buf[3] = ( uint8_t )( timeout & 0xFF );
@@ -415,6 +451,12 @@
     printf("SetRfFrequency ");
 #endif
 
+    if( ImageCalibrated == false )
+    {
+        CalibrateImage( frequency );
+        ImageCalibrated = true;
+    }
+
     freq = ( uint32_t )( ( double )frequency / ( double )FREQ_STEP );
     buf[0] = ( uint8_t )( ( freq >> 24 ) & 0xFF );
     buf[1] = ( uint8_t )( ( freq >> 16 ) & 0xFF );
@@ -431,12 +473,6 @@
 
     // Save packet type internally to avoid questioning the radio
     this->PacketType = packetType;
-
-    if( packetType == PACKET_TYPE_GFSK )
-    {
-        WriteReg( REG_BIT_SYNC, 0x00 );
-    }
-
     WriteCommand( RADIO_SET_PACKETTYPE, ( uint8_t* )&packetType, 1 );
 }
 
@@ -458,20 +494,21 @@
     {
         if( power == 15 )
         {
-            SetPaConfig( 0x06, 0x00, 0x01, 0x00 );
+            SetPaConfig( 0x06, 0x00, 0x01, 0x01 );
         }
         else
         {
-            SetPaConfig( 0x04, 0x00, 0x01, 0x00 );  
+            SetPaConfig( 0x04, 0x00, 0x01, 0x01 );  
         }
         if( power >= 14 )
         {
             power = 14;
-        } 
+        }
         else if( power < -3 )
         {
             power = -3;
         }
+        WriteReg( REG_OCP, 0x18 ); // current max is 80 mA for the whole device
     }
     else // sx1262
     {
@@ -479,11 +516,12 @@
         if( power > 22 )
         {
             power = 22;
-        } 
+        }
         else if( power < -3 )
         {
             power = -3;
         }
+        WriteReg( REG_OCP, 0x38 ); // current max 160mA for the whole device
     }
     buf[0] = power;
     if( OPT == 0 )
@@ -682,7 +720,7 @@
     buf[4] = ( uint8_t )( ( cadTimeout >> 16 ) & 0xFF );
     buf[5] = ( uint8_t )( ( cadTimeout >> 8 ) & 0xFF );
     buf[6] = ( uint8_t )( cadTimeout & 0xFF );
-    WriteCommand( RADIO_SET_CADPARAMS, buf, 5 );
+    WriteCommand( RADIO_SET_CADPARAMS, buf, 7 );
     OperatingMode = MODE_CAD;
 }
 
@@ -711,7 +749,7 @@
 
 int8_t SX126x::GetRssiInst( void )
 {
-    int8_t rssi;
+    uint8_t rssi;
 
     ReadCommand( RADIO_GET_RSSIINST, ( uint8_t* )&rssi, 1 );
     return( -( rssi / 2 ) );
@@ -774,7 +812,7 @@
     RadioError_t error;
 
     ReadCommand( RADIO_GET_ERROR, ( uint8_t * )&error, 2 );
-    return error; 
+    return error;
 }
 
 void SX126x::ClearIrqStatus( uint16_t irq )
@@ -841,16 +879,9 @@
     if( ( irqRegs & IRQ_HEADER_VALID ) == IRQ_HEADER_VALID )
     {
         // LoRa Only
-        FrequencyError = 0x000000 | ( ( 0x0F & ReadReg( 0x076A ) ) << 16 );
-        FrequencyError = FrequencyError | ( ReadReg( 0x076B ) << 8 );
-        FrequencyError = FrequencyError | ( ReadReg( 0x076c ) );
-    }
-
-    if( ( irqRegs & IRQ_SYNCWORD_VALID ) == IRQ_SYNCWORD_VALID )
-    {
-        // GFSK Only
-        FrequencyError = 0x000000 | ( ( 0x0F & ReadReg( 0x06B0 ) ) << 8 );
-        FrequencyError = FrequencyError | ( ReadReg( 0x06B1 ) );
+        FrequencyError = 0x000000 | ( ( 0x0F & ReadReg( REG_FREQUENCY_ERRORBASEADDR ) ) << 16 );
+        FrequencyError = FrequencyError | ( ReadReg( REG_FREQUENCY_ERRORBASEADDR + 1 ) << 8 );
+        FrequencyError = FrequencyError | ( ReadReg( REG_FREQUENCY_ERRORBASEADDR + 2 ) );
     }
 
     if( ( irqRegs & IRQ_TX_DONE ) == IRQ_TX_DONE )