From 7eb19e6024af7f05cf94c66fb843439a3509147e Mon Sep 17 00:00:00 2001
From: QuakeGod <quakegod@sina.com>
Date: 星期一, 02 九月 2024 15:30:40 +0800
Subject: [PATCH] 2024-09-02

---
 Radio_LLCC68/Radio/src/radio.c |  113 +++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 94 insertions(+), 19 deletions(-)

diff --git a/Radio_LLCC68/Radio/src/radio.c b/Radio_LLCC68/Radio/src/radio.c
index bbe4ebd..76a1f2c 100644
--- a/Radio_LLCC68/Radio/src/radio.c
+++ b/Radio_LLCC68/Radio/src/radio.c
@@ -192,6 +192,18 @@
 uint32_t RadioTimeOnAir( RadioModems_t modem, uint8_t pktLen );
 
 /*!
+ * \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 (us) for the given packet payload length
+ */
+uint32_t RadioTimeOnAiruS( RadioModems_t modem, uint8_t pktLen );
+
+/*!
  * \brief Sends the buffer of size. Prepares the packet to be sent and sets
  *        the radio in transmission
  *
@@ -330,7 +342,7 @@
     RadioSetRxConfig,
     RadioSetTxConfig,
     RadioCheckRfFrequency,
-    RadioTimeOnAir,
+    RadioTimeOnAiruS,
     RadioSend,
     RadioSleep,
     RadioStandby,
@@ -395,12 +407,19 @@
 };
 
 const RadioLoRaBandwidths_t Bandwidths[] = { LORA_BW_125, LORA_BW_250, LORA_BW_500 };
-
+/*
 //                                          SF12    SF11    SF10    SF9    SF8    SF7    SF6    SF5
-static double RadioLoRaSymbTime[3][8] = {{ 32.768, 16.384, 8.192, 4.096, 2.048, 1.024,	0.512,	0.256},  // 125 KHz
+const double RadioLoRaSymbTime[3][8] = {{ 32.768, 16.384, 8.192, 4.096, 2.048, 1.024,	0.512,	0.256},  // 125 KHz
                                          { 16.384, 8.192,  4.096, 2.048, 1.024, 0.512,	0.256,	0.128},  // 250 KHz
                                          { 8.192,  4.096,  2.048, 1.024, 0.512, 0.256,	0.128,	0.064}}; // 500 KHz
 
+// */						
+/*
+static uint16_t RadioLoRaSymbTimeUs[3][8] = {{ 32768, 16384,  8192, 4096, 2048, 1024,	512,	256},  // 125 KHz
+                                         {   16384,  8192,  4096, 2048, 1024, 512,	256,	128},  // 250 KHz
+                                         {    8192,  4096,  2048, 1024,  512, 256,	128,	64}}; // 500 KHz
+
+*/
 uint8_t MaxPayloadLength = 0xFF;
 
 uint32_t TxTimeout = 0;
@@ -410,7 +429,7 @@
 
 
 PacketStatus_t RadioPktStatus;
-uint8_t RadioRxPayload[32];
+uint8_t RadioRxPayload[256];
 
 bool IrqFired = false;
 
@@ -500,7 +519,7 @@
     RadioEvents = events;
     
     SX126xInit( RadioOnDioIrq );
-    SX126xSetStandby( STDBY_RC );
+    SX126xSetStandby( STDBY_XOSC );		//STDBY_RC
     SX126xSetRegulatorMode( USE_DCDC );
     
     SX126xSetBufferBaseAddress( 0x00, 0x00 );
@@ -560,8 +579,8 @@
 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;
+    int16_t rssi = 0;
+    uint32_t carrierSenseTime = 0;
 
     RadioSetModem( modem );
 
@@ -577,15 +596,15 @@
      //Perform carrier sense for maxCarrierSenseTime
 //    while( TimerGetElapsedTime( carrierSenseTime ) < maxCarrierSenseTime )
 //    {
-//        rssi = RadioRssi( modem );
+        rssi = RadioRssi( modem );
 //
-//        if( rssi > rssiThresh )
-//        {
-//            status = false;
+        if( rssi > rssiThresh )
+        {
+            status = false;
 //            break;
-//        }
+        }
 //    }
-    RadioSleep( );
+//    RadioSleep( );
     return status;
 }
 
@@ -669,7 +688,7 @@
             SX126xSetSyncWord( ( uint8_t[] ){ 0xC1, 0x94, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x00 } );
             SX126xSetWhiteningSeed( 0x01FF );
 
-            RxTimeout = ( uint32_t )( symbTimeout * ( ( 1.0 / ( double )datarate ) * 8.0 ) * 1000 );
+            RxTimeout = ( uint32_t )( symbTimeout * 1000 * 8 / datarate );			//( symbTimeout * ( ( 1.0 / ( double )datarate ) * 8.0 ) * 1000 );
             break;
 
         case MODEM_LORA:
@@ -695,9 +714,9 @@
             if( ( SX126x.ModulationParams.Params.LoRa.SpreadingFactor == LORA_SF5 ) ||
                 ( SX126x.ModulationParams.Params.LoRa.SpreadingFactor == LORA_SF6 ) )
             {
-                if( preambleLen < 12 )
+                if( preambleLen < 8 )
                 {
-                    SX126x.PacketParams.Params.LoRa.PreambleLength = 12;
+                    SX126x.PacketParams.Params.LoRa.PreambleLength = 8;
                 }
                 else
                 {
@@ -823,6 +842,8 @@
     return true;
 }
 
+/*
+
 uint32_t RadioTimeOnAir( RadioModems_t modem, uint8_t pktLen )
 {
     uint32_t airTime = 0;
@@ -842,21 +863,75 @@
     case MODEM_LORA:
         {
             double ts = RadioLoRaSymbTime[SX126x.ModulationParams.Params.LoRa.Bandwidth - 4][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;
+						double tOnAir = tPreamble + tPayload;
             // return milli seconds
             airTime = floor( tOnAir + 0.999 );
+        }
+        break;
+    }
+    return airTime;
+}
+// */
+
+uint32_t RadioTimeOnAiruS( RadioModems_t modem, uint8_t pktLen )
+{
+    uint32_t airTime = 0;
+
+    switch( modem )
+    {
+    case MODEM_FSK:
+        {
+           airTime =  (1000 *  8 * ( SX126x.PacketParams.Params.Gfsk.PreambleLength +
+                                     ( SX126x.PacketParams.Params.Gfsk.SyncWordLength >> 3 ) +
+                                     ( ( SX126x.PacketParams.Params.Gfsk.HeaderType == RADIO_PACKET_FIXED_LENGTH ) ? 0 : 1 ) +
+                                     pktLen +
+                                     ( ( SX126x.PacketParams.Params.Gfsk.CrcLength == RADIO_CRC_2_BYTES ) ? 2 : 0 ) ) /
+                                     SX126x.ModulationParams.Params.Gfsk.BitRate ) ;
+        }
+        break;
+    case MODEM_LORA:
+        {
+						uint32_t ts = (1 << (7 + SX126x.ModulationParams.Params.LoRa.SpreadingFactor - SX126x.ModulationParams.Params.LoRa.Bandwidth));///1000.0;
+					
+            // time of preamble
+            uint32_t tPreamble = ( SX126x.PacketParams.Params.LoRa.PreambleLength + 4 ) * ts + (ts>>2);
+						if (SX126x.ModulationParams.Params.LoRa.SpreadingFactor == 5 || SX126x.ModulationParams.Params.LoRa.SpreadingFactor == 6)
+						{
+							tPreamble = ( SX126x.PacketParams.Params.LoRa.PreambleLength + 6 ) * ts + (ts>>2);
+						}
+            // Symbol length of payload and time
+						uint32_t tmp32 = ( ( 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 ) ) /
+                                 ( 4 * ( SX126x.ModulationParams.Params.LoRa.SpreadingFactor -
+                                 ( ( SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize > 0 ) ? 2 : 0 )  ) )  +1 ) *
+					
+                                 ( ( SX126x.ModulationParams.Params.LoRa.CodingRate % 4 ) + 4 );
+																 
+					 uint32_t nPayload = 8 + ( ( tmp32 > 0 ) ? tmp32 : 0 );
+					 uint32_t tPayload = nPayload * ts;
+            // Time on air
+ 						uint32_t tOnAir = tPreamble + tPayload;
+            // return micro seconds
+						airTime = tOnAir; //tPreamble;// tOnAir;
         }
         break;
     }
@@ -1067,7 +1142,7 @@
         IrqFired = false;
 
         uint16_t irqRegs = SX126xGetIrqStatus( );
-        SX126xClearIrqStatus( IRQ_RADIO_ALL );
+        SX126xClearIrqStatus( irqRegs);			//IRQ_RADIO_ALL );
         
         if( ( irqRegs & IRQ_TX_DONE ) == IRQ_TX_DONE )
         {
@@ -1090,7 +1165,7 @@
         {
             uint8_t size;
 
-            SX126xGetPayload( RadioRxPayload, &size , 255 );
+            SX126xGetPayload( RadioRxPayload, &size , 120 );
             SX126xGetPacketStatus( &RadioPktStatus );
             if( ( RadioEvents != NULL ) && ( RadioEvents->RxDone != NULL ) )
             {

--
Gitblit v1.9.1