SX1261 and sx1262 common library

Dependents:   SX126xDevKit SX1262PingPong SX126X_TXonly SX126X_PingPong_Demo ... more

Fork of SX126xLib by Gregory Cristian

Revision:
3:7e3595a9ebe0
Parent:
2:4ff11ea92fbe
Child:
4:c6ef863d0b07
--- a/sx126x.h	Fri Sep 23 09:38:34 2016 +0000
+++ b/sx126x.h	Wed Oct 12 08:49:58 2016 +0000
@@ -50,6 +50,26 @@
 #define AUTO_RX_TX_OFFSET                           2
 
 /*!
+ * \brief LFSR initial value to compute IBM type CRC
+ */
+#define CRC_IBM_SEED                                0xFFFF
+
+/*!
+ * \brief LFSR initial value to compute CCIT type CRC
+ */
+#define CRC_CCITT_SEED                              0x1D0F
+
+/*!
+ * \brief Polynomial used to compute IBM CRC
+ */
+#define CRC_POLYNOMIAL_IBM                          0x8005
+
+/*!
+ * \brief Polynomial used to compute CCIT CRC
+ */
+#define CRC_POLYNOMIAL_CCITT                        0x1021
+
+/*!
  * \brief The address of the register holding the first byte defining the CRC seed
  *
  */
@@ -63,56 +83,10 @@
 
 /*!
  * \brief The address of the register holding the first byte defining the whitening seed
- *
- * \remark Only used for packet types GFSK, FloRa and BLE
  */
 #define REG_LR_WHITSEEDBASEADDR                     0x06B6
 
 /*!
- * \brief The address of the register holding the ranging id check length
- *
- * \remark Only used for packet type Ranging
- */
-#define REG_LR_RANGINGIDCHECKLENGTH                 0x072C
-
-/*!
- * \brief The address of the register holding the device ranging id
- *
- * \remark Only used for packet type Ranging
- */
-#define REG_LR_DEVICERANGINGADDR                    0x0711
-
-/*!
- * \brief The address of the register holding the device ranging id
- *
- * \remark Only used for packet type Ranging
- */
-#define REG_LR_REQUESTRANGINGADDR                   0x070D
-
-
-
-/*!
- * \brief The address of the register holding the first byte of ranging results
- * Only used for packet type Ranging
- */
-#define REG_LR_RANGINGRESULTRAWBASEADDR             0x0775
-
-#define REG_LR_RANGINGRESULTAVGBASEADDR             0x0778
-
-#define REG_LR_RANGINGRESULTDEBBASEADDR             0x077B
-
-#define REG_LR_RANGINGRESULTFINBASEADDR             0x077E
-
-
-
-/*!
- * \brief The address of the register holding the first byte of ranging calibration
- *
- * \remark Only used for packet type Ranging
- */
-#define REG_LR_RANGINGRERXTXDELAYCAL                0x0727
-
-/*!
  * \brief The address of the instruction RAM and its size
  */
 #define IRAM_START_ADDRESS                          0x8000
@@ -160,6 +134,17 @@
     STDBY_RC                                = 0,
     STDBY_XOSC                              = 1,
 };
+/*!
+ * \brief Declares the power regulation used to power the device
+ *
+ * This command allows the user to specify if DC-DC or LDO is used for power regulation.
+ * Using only LDO implies that the Rx or Tx current is doubled
+ */
+enum RadioRegulatorMode_t
+{
+    USE_LDO                               = 0x00, // default
+    USE_DCDC                              = 0x01,
+};
 
 /*!
  * \brief Represents the possible packet type (i.e. modem) used
@@ -168,10 +153,7 @@
 {
     PACKET_TYPE_GFSK                        = 0,
     PACKET_TYPE_LORA                        = 1,
-    PACKET_TYPE_BPSK                        = 2,
-    PACKET_TYPE_GMSK                        = 3,
-    PACKET_TYPE_RANGING                     = 4,
-    PACKET_TYPE_NONE                        = 0x0F,
+    PACKET_TYPE_RESERVED                    = 0x0F,
 };
 
 /*!
@@ -211,36 +193,66 @@
  */
 enum RadioModShapings_t
 {
-    MOD_SHAPING_G_BT_03                      = 0x08,
-    MOD_SHAPING_G_BT_05                      = 0x09,
-    MOD_SHAPING_G_BT_07                      = 0x0A,
-    MOD_SHAPING_G_BT_1                       = 0x0B,
+    MOD_SHAPING_G_BT_03                     = 0x08,
+    MOD_SHAPING_G_BT_05                     = 0x09,
+    MOD_SHAPING_G_BT_07                     = 0x0A,
+    MOD_SHAPING_G_BT_1                      = 0x0B,
+    
+    MOD_SHAPING_G_BT_03_D                   = 0x0C,
+    MOD_SHAPING_G_BT_05_D                   = 0x0D,
+    MOD_SHAPING_G_BT_07_D                   = 0x0E,
+    MOD_SHAPING_G_BT_1_D                    = 0x0F,
     
-    MOD_SHAPING_G_BT_03_D                    = 0x0C,
-    MOD_SHAPING_G_BT_05_D                    = 0x0D,
-    MOD_SHAPING_G_BT_07_D                    = 0x0E,
-    MOD_SHAPING_G_BT_1_D                     = 0x0F,
+    MOD_SHAPING_RC_BT_03                    = 0x10,
+    MOD_SHAPING_RC_BT_05                    = 0x11,
+    MOD_SHAPING_RC_BT_07                    = 0x12,
+    MOD_SHAPING_RC_BT_1                     = 0x13,
     
-    MOD_SHAPING_RC_BT_03                     = 0x10,
-    MOD_SHAPING_RC_BT_05                     = 0x11,
-    MOD_SHAPING_RC_BT_07                     = 0x12,
-    MOD_SHAPING_RC_BT_1                      = 0x13,
+    MOD_SHAPING_RC_BT_03_D                  = 0x14,
+    MOD_SHAPING_RC_BT_05_D                  = 0x15,
+    MOD_SHAPING_RC_BT_07_D                  = 0x16,
+    MOD_SHAPING_RC_BT_1_D                   = 0x17,
+    
+    MOD_SHAPING_RRC_BT_03                   = 0x18,
+    MOD_SHAPING_RRC_BT_05                   = 0x19,
+    MOD_SHAPING_RRC_BT_07                   = 0x1A,
+    MOD_SHAPING_RRC_BT_1                    = 0x1B,
     
-    MOD_SHAPING_RC_BT_03_D                   = 0x14,
-    MOD_SHAPING_RC_BT_05_D                   = 0x15,
-    MOD_SHAPING_RC_BT_07_D                   = 0x16,
-    MOD_SHAPING_RC_BT_1_D                    = 0x17,
-    
-    MOD_SHAPING_RRC_BT_03                    = 0x18,
-    MOD_SHAPING_RRC_BT_05                    = 0x19,
-    MOD_SHAPING_RRC_BT_07                    = 0x1A,
-    MOD_SHAPING_RRC_BT_1                     = 0x1B,
-    
-    MOD_SHAPING_RRC_BT_03_D                  = 0x1C,
-    MOD_SHAPING_RRC_BT_05_D                  = 0x1D,
-    MOD_SHAPING_RRC_BT_07_D                  = 0x1E,
-    MOD_SHAPING_RRC_BT_1_D                   = 0x1F,
-      
+    MOD_SHAPING_RRC_BT_03_D                 = 0x1C,
+    MOD_SHAPING_RRC_BT_05_D                 = 0x1D,
+    MOD_SHAPING_RRC_BT_07_D                 = 0x1E,
+    MOD_SHAPING_RRC_BT_1_D                  = 0x1F,
+};
+
+/*!
+ * \brief Represents the modulation shaping parameter
+ */
+enum RadioRxBandwidth_t
+{
+    RX_BW_4800                              = 0x1F,
+    RX_BW_5800                              = 0x17,
+    RX_BW_7300                              = 0x0F,
+    RX_BW_9700                              = 0x1E,
+    RX_BW_11700                             = 0x16,
+    RX_BW_14600                             = 0x0E,
+    RX_BW_19500                             = 0x1D,
+    RX_BW_23400                             = 0x15,
+    RX_BW_29300                             = 0x0D,
+    RX_BW_39000                             = 0x1C,
+    RX_BW_46900                             = 0x14,
+    RX_BW_58600                             = 0x0C,
+    RX_BW_78200                             = 0x1B,
+    RX_BW_93800                             = 0x13,
+    RX_BW_117300                            = 0x0B,
+    RX_BW_156200                            = 0x1A,
+    RX_BW_187200                            = 0x12,
+    RX_BW_234300                            = 0x0A,
+    RX_BW_312000                            = 0x18,
+    RX_BW_373600                            = 0x11,
+    RX_BW_467000                            = 0x1F,
+    RX_BW_619300                            = 0x18,
+    RX_BW_742400                            = 0x10,
+    RX_BW_929000                            = 0x08,
 };
 
 /*!
@@ -263,16 +275,16 @@
  */
 enum RadioLoRaBandwidths_t
 {
-    LORA_BW_500                            = 6,
-    LORA_BW_250                            = 5,
-    LORA_BW_125                            = 4,
-    LORA_BW_062                            = 3,
-    LORA_BW_041                            = 10,
-    LORA_BW_031                            = 2,
-    LORA_BW_020                            = 9,
-    LORA_BW_015                            = 1,
-    LORA_BW_010                            = 8,
-    LORA_BW_007                            = 0,  
+    LORA_BW_500                             = 6,
+    LORA_BW_250                             = 5,
+    LORA_BW_125                             = 4,
+    LORA_BW_062                             = 3,
+    LORA_BW_041                             = 10,
+    LORA_BW_031                             = 2,
+    LORA_BW_020                             = 9,
+    LORA_BW_015                             = 1,
+    LORA_BW_010                             = 8,
+    LORA_BW_007                             = 0,  
 };
 
 /*!
@@ -295,16 +307,26 @@
 enum RadioPreambleLengths_t
 {
     RADIO_PREAMBLE_LENGTH_04_BITS           = 0x00,         //!< Preamble length: 04 bits
-    RADIO_PREAMBLE_LENGTH_08_BITS           = 0x10,         //!< Preamble length: 08 bits
-    RADIO_PREAMBLE_LENGTH_12_BITS           = 0x20,         //!< Preamble length: 12 bits
-    RADIO_PREAMBLE_LENGTH_16_BITS           = 0x30,         //!< Preamble length: 16 bits
-    RADIO_PREAMBLE_LENGTH_20_BITS           = 0x40,         //!< Preamble length: 20 bits
-    RADIO_PREAMBLE_LENGTH_24_BITS           = 0x50,         //!< Preamble length: 24 bits
-    RADIO_PREAMBLE_LENGTH_28_BITS           = 0x60,         //!< Preamble length: 28 bits
-    RADIO_PREAMBLE_LENGTH_32_BITS           = 0x70,         //!< Preamble length: 32 bits
+    RADIO_PREAMBLE_LENGTH_08_BITS           = 0x01,         //!< Preamble length: 08 bits
+    RADIO_PREAMBLE_LENGTH_12_BITS           = 0x02,         //!< Preamble length: 12 bits
+    RADIO_PREAMBLE_LENGTH_16_BITS           = 0x03,         //!< Preamble length: 16 bits
+    RADIO_PREAMBLE_LENGTH_20_BITS           = 0x04,         //!< Preamble length: 20 bits
+    RADIO_PREAMBLE_LENGTH_24_BITS           = 0x05,         //!< Preamble length: 24 bits
+    RADIO_PREAMBLE_LENGTH_28_BITS           = 0x06,         //!< Preamble length: 28 bits
+    RADIO_PREAMBLE_LENGTH_32_BITS           = 0x07,         //!< Preamble length: 32 bits
 };
 
-
+/*!
+ * \brief Represents the preamble length used to detect the packet on Rx side
+ */
+enum RadioPreambleDetection_t
+{
+    RADIO_PREAMBLE_DETECTOR_OFF             = 0x00,         //!< Preamble detection length off
+    RADIO_PREAMBLE_DETECTOR_08_BITS         = 0x04,         //!< Preamble detection length 8 bits
+    RADIO_PREAMBLE_DETECTOR_12_BITS         = 0x05,         //!< Preamble detection length 12 bits
+    RADIO_PREAMBLE_DETECTOR_16_BITS         = 0x06,         //!< Preamble detection length 16 bits
+    RADIO_PREAMBLE_DETECTOR_20_BITS         = 0x07,         //!< Preamble detection length 20 bits
+};
 
 /*!
  * \brief The length of sync words 
@@ -393,30 +415,6 @@
 };
 
 /*!
- * \brief Represents the length of the ID to check in ranging operation
- */
-enum RadioRangingIdCheckLen_t
-{
-    RANGING_IDCHECK_LENGTH_08_BITS = 0,
-    RANGING_IDCHECK_LENGTH_16_BITS = 1,
-    RANGING_IDCHECK_LENGTH_24_BITS = 2,
-    RANGING_IDCHECK_LENGTH_32_BITS = 3,
-};
-
-/*!
- * \brief Represents the result type to be used in ranging operation
- */
-enum RadioRangingResultType_t
-{
-    RANGING_RESULT_RAW                      = 0x00,
-    RANGING_RESULT_AVERAGED                 = 0x03,
-    RANGING_RESULT_DEBIASED                 = 0x06,
-    RANGING_RESULT_FILTERED                 = 0x09,
-};
-
-
-
-/*!
  * \brief Represents the interruption masks available for the radio
  *
  * \remark Note that not all these interruptions are available for all packet types
@@ -430,8 +428,7 @@
     IRQ_SYNCWORD_VALID                      = 0x0008,
     IRQ_HEADER_VALID                        = 0x0010,
     IRQ_HEADER_ERROR                        = 0x0020,      
-    IRQ_GFSK_ERROR                          = 0x0040,
-    IRQ_LORA_CRC_ERROR                      = 0x0040,       //shared with IRQ_GFSK_ERROR
+    IRQ_CRC_ERROR                           = 0x0040,
     IRQ_CAD_DONE                            = 0x0080,
     IRQ_CAD_ACTIVITY_DETECTED               = 0x0100,
     IRQ_RX_TX_TIMEOUT                       = 0x0200,
@@ -490,10 +487,18 @@
     RADIO_GET_ERROR                         = 0x17,
     RADIO_SET_LONGPREAMBLE                  = 0x90,
     RADIO_SET_TXFALLBACKMODE                = 0x93,
-    
 };
 
-
+/*!
+* \brief RX_TX_CONTINUOUS and RX_TX_SINGLE are two particular values for TickTime.
+* The former keep the radio in Rx or Tx mode, even after successfull reception
+* or transmission. It should never generate Timeout interrupt.
+* The later let the radio enought time to make one reception or transmission.
+* No Timeout interrupt is generated, and the radio fall in StandBy mode after
+* reception or transmission.
+*/
+#define RX_TX_CONTINUOUS ( TickTime_t ){ RADIO_TICK_SIZE_0015_US, 0xFFFF }
+#define RX_TX_SINGLE     ( TickTime_t ){ RADIO_TICK_SIZE_0015_US, 0 }
 
 /*!
  * \brief The type describing the modulation parameters for every packet types
@@ -504,39 +509,19 @@
     union
     {
         uint8_t Buffer[8];
-        
-        struct
-        {
-            uint8_t             BitRate[3];
-            //uint32_t          BitRate:24;
-            RadioModShapings_t  PulseShape:8;
-            uint8_t             BW;
-            uint8_t             Fdev[3];
-        }Gfsk;
-        
         struct
         {
-            uint8_t             BitRate[3];
-            RadioModShapings_t  PulseShape:8;
-            uint8_t             BW;
-            uint8_t             Fdev[3];
-        }Gmsk;
-        
-        struct
-        {
-            uint8_t             BitRate[3];
-            RadioModShapings_t  PulseShape:8;
-        }Bpsk;
-        
- 
+            uint32_t                     BitRate;
+            uint32_t                     Fdev;
+            RadioModShapings_t           PulseShape:8;
+            uint8_t                      BW;
+        }Gfsk;
         struct
         {
             RadioLoRaSpreadingFactors_t  SpreadingFactor;   //!< Spreading Factor for the LoRa modulation
             RadioLoRaBandwidths_t        Bandwidth;         //!< Bandwidth for the LoRa modulation
             RadioLoRaCodingRates_t       CodingRate;        //!< Coding rate for the LoRa modulation
         }LoRa;
-
-      
     }Params;                                                //!< Holds the modulation parameters structure
 }ModulationParams_t;
 
@@ -642,10 +627,10 @@
     uint8_t Value;
     struct
     {
-        uint8_t Reserved                : 5;
+        uint8_t WakeUpRTC               : 1;                    //!< Get out of sleep mode if wakeup signal received from RTC
+        uint8_t Reset                   : 1;
         uint8_t WarmStart               : 1;
-        uint8_t Reset                   : 1;
-        uint8_t WakeUpRTC               : 1;                    //!< Get out of sleep mode if wakeup signal received from RTC
+        uint8_t Reserved                : 5;
     } Fields;
 
 }SleepParams_t;
@@ -672,15 +657,15 @@
      *
      */
     SX126x( void ( *txDone )( ), void ( *rxDone )( ), void ( *rxPblSyncWordHeader )(IrqPblSyncHeaderCode_t val ),
-            void ( *rxTxTimeout )( ), void ( *rxError )( IrqErrorCode_t errorCode ), void ( *rangingDone )( IrqRangingCode_t val ),
-            void ( *cadDone )( bool channelActivityDetected ),  void ( *onDioIrq )( ) ): Radio( txDone, rxDone, rxPblSyncWordHeader, rxTxTimeout, rxError, rangingDone, cadDone )
+            void ( *rxTxTimeout )( IrqTimeoutCode_t timeoutCode ), void ( *rxError )( IrqErrorCode_t errorCode ),
+            void ( *cadDone )( bool channelActivityDetected ),  void ( *onDioIrq )( ) ): Radio( txDone, rxDone, rxPblSyncWordHeader, rxTxTimeout, rxError, cadDone )
     {
 
         this->dioIrq = &SX126x::OnDioIrq;
       
         this->onCustomDioIrq = onDioIrq;
   
-        this->PacketType = PACKET_TYPE_NONE;
+        this->PacketType = PACKET_TYPE_RESERVED;
     }
 
     virtual ~SX126x( )
@@ -894,18 +879,14 @@
     /*!
      * \brief Sets the radio for the given protocol
      *
-     * \param [in]  packetType    [PACKET_TYPE_GFSK, PACKET_TYPE_LORA,
-     *                             PACKET_TYPE_RANGING, PACKET_TYPE_FLORA,
-     *                             PACKET_TYPE_BLE]
+     * \param [in]  packetType    [PACKET_TYPE_GFSK, PACKET_TYPE_LORA]
      */
     void SetPacketType( RadioPacketType_t packetType );
 
     /*!
      * \brief Gets the current radio protocol
      *
-     * \retval      packetType    [PACKET_TYPE_GFSK, PACKET_TYPE_LORA,
-     *                             PACKET_TYPE_RANGING, PACKET_TYPE_FLORA,
-     *                             PACKET_TYPE_BLE, PACKET_TYPE_NONE]
+     * \retval      packetType    [PACKET_TYPE_GFSK, PACKET_TYPE_LORA]
      */
     RadioPacketType_t GetPacketType( void );
 
@@ -980,8 +961,7 @@
     void GetStats( RxCounter_t *rxCounter );
 
     void ResetStats();
-    
-    
+
     uint8_t GetError();
 
     /*!
@@ -1062,7 +1042,7 @@
      * \param [out] size          A pointer to the size of the payload received
      * \param [in]  maxSize       The maximal size allowed to copy into the buffer
      */
-    uint8_t GetPayload( uint8_t *buffer, uint8_t *size,  uint8_t maxSize );
+    uint8_t GetPayload( uint8_t *buffer, uint8_t *size, uint8_t maxSize );
 
     /*!
      * \brief Sends a payload
@@ -1085,10 +1065,10 @@
     /*!
      * \brief Sets the Initial value for the LFSR used for the CRC calculation
      *
-     * \param [in]  seed          Initial LFSR value ( 4 bytes )
+     * \param [in]  seed          Initial LFSR value ( 2 bytes )
      *
      */
-    void SetCrcSeed( uint8_t *seed );
+    void SetCrcSeed( uint16_t seed );
 
     /*!
      * \brief Sets the seed used for the CRC calculation
@@ -1096,7 +1076,7 @@
      * \param [in]  seed          The seed value
      *
      */
-    void SetCrcPolynomial( uint8_t *seed );
+    void SetCrcPolynomial( uint16_t seed );
 
     /*!
      * \brief Sets the Initial value of the LFSR used for the whitening in GFSK, FloRa and BLE protocols
@@ -1106,70 +1086,6 @@
     void SetWhiteningSeed( uint8_t seed );
 
     /*!
-     * \brief Sets the number of bits used to check that ranging request match ranging ID
-     *
-     * \param [in]  length        [0: 8 bits, 1: 16 bits,
-     *                             2: 24 bits, 3: 32 bits]
-     */
-    void SetRangingIdLength( RadioRangingIdCheckLen_t length );
-
-    /*!
-     * \brief Sets ranging device id
-     *
-     * \param [in]  address       Device address
-     */
-    void SetDeviceRangingAddress( uint32_t address );
-
-    /*!
-     * \brief Sets the device id to ping in a ranging request
-     *
-     * \param [in]  address       Address of the device to ping
-     */
-    void SetRangingRequestAddress( uint32_t address );
-
-    /*!
-     * \brief Return the ranging result value
-     *
-     * \param [in]  resultType    Specifies the type of result.
-     *                            [0: RAW, 1: Averaged,
-     *                             2: De-biased, 3:Filtered]
-     *
-     * \retval      ranging       The ranging measure filtered according to resultType
-     */
-    int32_t GetRangingResult( RadioRangingResultType_t resultType );
-
-    /*!
-     * \brief Sets the standard processing delay between Master and Slave
-     *
-     * \param [in]  cal           RxTx delay offset for correcting ranging bias.
-     *
-     * The calibration value reflects the group delay of the radio front end and
-     * must be re-performed for each new SX1280 PCB design. The value is obtained
-     * empirically by either conducted measurement in a known electrical length
-     * coaxial RF cable (where the design is connectorised) or by radiated
-     * measurement, at a known distance, where an antenna is present.
-     * The result of the calibration process is that the SX1280 ranging result
-     * accurately reflects the physical range, the calibration procedure therefore
-     * removes the average timing error from the time-of-flight measurement for a
-     * given design.
-     *
-     * The values are Spreading Factor dependents, and depend also of the board
-     * design. Some typical values are provided in the next table.
-     *
-     * Spreading Factor | Calibration Value
-     * ---------------- | -----------------
-     *   SF5            |  12200
-     *   SF6            |  12200
-     *   SF7            |  12400
-     *   SF8            |  12650
-     *   SF9            |  12940
-     *   SF10           |  13000
-     *   SF11           |  13060
-     *   SF12           |  13120
-     */
-    void SetRangingCalibration( uint16_t cal );
-
-    /*!
      * \brief DIOs interrupt callback
      *
      * \remark Called to handle all 3 DIOs pins