Driver for the SX1276 RF Transceiver

Dependents:   LoRa_PIR LoRaWAN-lmic-app SX1276PingPong LoRaWAN-lmic-app ... more

Files at this revision

API Documentation at this revision

Comitter:
GregCr
Date:
Fri Sep 19 14:16:35 2014 +0000
Parent:
6:e7f02929cd3d
Child:
8:0fe3e0e8007b
Commit message:
Added support for CAD; Changed Rssi into an int16_t

Changed in this revision

debug/debug.h Show diff for this revision Revisions of this file
enums/enums.h Show annotated file Show diff for this revision Revisions of this file
radio/radio.cpp Show annotated file Show diff for this revision Revisions of this file
radio/radio.h Show annotated file Show diff for this revision Revisions of this file
sx1276/sx1276-hal.cpp Show annotated file Show diff for this revision Revisions of this file
sx1276/sx1276-hal.h Show annotated file Show diff for this revision Revisions of this file
sx1276/sx1276.cpp Show annotated file Show diff for this revision Revisions of this file
sx1276/sx1276.h Show annotated file Show diff for this revision Revisions of this file
--- a/debug/debug.h	Thu Sep 04 14:03:20 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,61 +0,0 @@
-/* Copyright (c) 2012 mbed.org, MIT License
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
- * and associated documentation files (the "Software"), to deal in the Software without restriction,
- * including without limitation the rights to use, copy, modify, merge, publish, distribute,
- * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or
- * substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
- * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
- 
-#ifndef DEBUG_H
-#define DEBUG_H
-
-/** @file debug.h */
-
-#ifndef NDEBUG
-
-#include <stdarg.h>
-#include <stdio.h>
-
-/** Output a debug message
- * 
- * @param format printf-style format string, followed by variables
- */
-static inline void debug(const char *format, ...) {
-    va_list args;
-    va_start(args, format);
-    vfprintf(stderr, format, args);
-    va_end(args);
-}
-
-/** Conditionally output a debug message
- * 
- * @param condition output only if condition is true
- * @param format printf-style format string, followed by variables
- */
-static inline void debug(bool condition, const char *format, ...) {
-    if(condition) {
-        va_list args;
-        va_start(args, format);
-        vfprintf(stderr, format, args);
-        va_end(args);
-    }
-}
-
-#else
-
-static inline void debug(const char *format, ...) {}
-static inline void debug(bool condition, const char *format, ...) {}
-
-#endif
-
-#endif
--- a/enums/enums.h	Thu Sep 04 14:03:20 2014 +0000
+++ b/enums/enums.h	Fri Sep 19 14:16:35 2014 +0000
@@ -19,7 +19,8 @@
  *	State of the radio:
  *	[IDLE,
  *	 RX_RUNNING, RX_TIMEOUT, RX_ERROR,
- * 	 TX_RUNNING, TX_TIMEOUT]
+ * 	 TX_RUNNING, TX_TIMEOUT,
+ 	 CAD]
  */
 enum RadioState
 {
@@ -31,7 +32,10 @@
 	RX_ERROR,
 	
 	TX,
-	TX_TIMEOUT
+	TX_TIMEOUT,
+    
+    CAD,
+    CAD_DONE
 };
 
 /*!
--- a/radio/radio.cpp	Thu Sep 04 14:03:20 2014 +0000
+++ b/radio/radio.cpp	Fri Sep 19 14:16:35 2014 +0000
@@ -14,8 +14,8 @@
 */
 #include "radio.h"
 
-Radio::Radio( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int8_t rssi, int8_t snr ), 
-			  void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ) )
+Radio::Radio( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ), 
+			  void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( ) )
 {
 	this->txDone = txDone;
 	this->txTimeout = txTimeout;
@@ -23,5 +23,6 @@
 	this->rxTimeout = rxTimeout;
 	this->rxError = rxError;
 	this->fhssChangeChannel = fhssChangeChannel;
+    this->cadDone = cadDone;
 }
 
--- a/radio/radio.h	Thu Sep 04 14:03:20 2014 +0000
+++ b/radio/radio.h	Fri Sep 19 14:16:35 2014 +0000
@@ -50,7 +50,7 @@
      *                     FSK : N/A ( set to 0 )
      *                     LoRa: SNR value in dB
      */
-	void ( *rxDone ) ( uint8_t *payload, uint16_t size, int8_t rssi, int8_t snr );
+	void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
 
     /*!
      * @brief  Rx Timeout callback prototype.
@@ -68,6 +68,11 @@
      * \param [IN] CurrentChannel   Index number of the current channel
      */
     void ( *fhssChangeChannel )( uint8_t CurrentChannel );
+
+    /*!
+     * @brief CAD Done callback prototype.
+     */
+    void ( *cadDone ) ( );
 	
 public:
 	//-------------------------------------------------------------------------
@@ -81,8 +86,8 @@
 	 * @param [IN]	rxTimeout
 	 * @param [IN]	rxError
 	 */
-	Radio( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int8_t rssi, int8_t snr ), 
-		   void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ) );
+	Radio( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ), 
+		   void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( ) );
 	virtual ~Radio( ) {};
 
 	//-------------------------------------------------------------------------
@@ -239,7 +244,12 @@
 	/*!
      * @brief Sets the radio in standby mode
      */
-    virtual void Standby( void )= 0;
+    virtual void Standby( void ) = 0;
+    
+	/*!
+     * @brief Sets the radio in CAD mode
+     */
+    virtual void StartCad( void ) = 0;
     
 	/*!
      * @brief Sets the radio in reception mode for the given time
@@ -260,7 +270,7 @@
      *
      * @retval rssiValue Current RSSI value in [dBm]
      */
-    virtual int8_t GetRssi ( ModemType modem ) = 0;
+    virtual int16_t GetRssi ( ModemType modem ) = 0;
     
 	/*!
      * @brief Writes the radio register at the specified address
--- a/sx1276/sx1276-hal.cpp	Thu Sep 04 14:03:20 2014 +0000
+++ b/sx1276/sx1276-hal.cpp	Fri Sep 19 14:16:35 2014 +0000
@@ -33,12 +33,12 @@
     { MODEM_LORA, REG_LR_PAYLOADMAXLENGTH, 0x40 },  
 };
 
-SX1276MB1xAS::SX1276MB1xAS( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int8_t rssi, int8_t snr ), 
-                            void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ),
+SX1276MB1xAS::SX1276MB1xAS( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ), 
+                            void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( ),
                             PinName mosi, PinName miso, PinName sclk, PinName nss, PinName reset,
                             PinName dio0, PinName dio1, PinName dio2, PinName dio3, PinName dio4, PinName dio5,
                             PinName antSwitch )
-                            : SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, mosi, miso, sclk, nss, reset, dio0, dio1, dio2, dio3, dio4, dio5),
+                            : SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone, mosi, miso, sclk, nss, reset, dio0, dio1, dio2, dio3, dio4, dio5),
                             antSwitch( antSwitch ),
                         #if( defined ( TARGET_KL25Z ) ||  defined ( TARGET_LPC11U6X ) )
                             fake( A3 )
@@ -65,14 +65,14 @@
     this->settings.State = IDLE ;
 }
 
-SX1276MB1xAS::SX1276MB1xAS( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int8_t rssi, int8_t snr ), 
-                            void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ) ) 
+SX1276MB1xAS::SX1276MB1xAS( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ), 
+                            void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( ) ) 
                         #if( defined ( TARGET_KL25Z ) ||  defined ( TARGET_LPC11U6X ) )
-                        :   SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9 ),
+                        :   SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone, D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9 ),
                             antSwitch( A4 ), 
                             fake( A3 )
                         #elif defined ( TARGET_NUCLEO_L152RE )
-                        :   SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, D11, D12, D13, D10, A0, D2, D3, D4, D5, A3, D9 ), // For NUCLEO L152RE dio4 is on port A3
+                        :   SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone, D11, D12, D13, D10, A0, D2, D3, D4, D5, A3, D9 ), // For NUCLEO L152RE dio4 is on port A3
                             antSwitch( A4 ),
                             fake( D8 )
                         #else
--- a/sx1276/sx1276-hal.h	Thu Sep 04 14:03:20 2014 +0000
+++ b/sx1276/sx1276-hal.h	Fri Sep 19 14:16:35 2014 +0000
@@ -33,12 +33,13 @@
     static const RadioRegisters_t RadioRegsInit[];
     
 public:
-    SX1276MB1xAS( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int8_t rssi, int8_t snr ), 
-                  void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ),
+    SX1276MB1xAS( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ), 
+                  void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( ),
             PinName mosi, PinName miso, PinName sclk, PinName nss, PinName reset,
             PinName dio0, PinName dio1, PinName dio2, PinName dio3, PinName dio4, PinName dio5,
             PinName antSwitch ); 
-            SX1276MB1xAS( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int8_t rssi, int8_t snr ), void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ) );
+            SX1276MB1xAS( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ),
+                          void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( ) );
     virtual ~SX1276MB1xAS( ) { };
     
     protected:
--- a/sx1276/sx1276.cpp	Thu Sep 04 14:03:20 2014 +0000
+++ b/sx1276/sx1276.cpp	Fri Sep 19 14:16:35 2014 +0000
@@ -40,11 +40,11 @@
 };
 
 
-SX1276::SX1276( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int8_t rssi, int8_t snr ), 
-				void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ),
+SX1276::SX1276( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ), 
+				void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( ),
 			    PinName mosi, PinName miso, PinName sclk, PinName nss, PinName reset,
                 PinName dio0, PinName dio1, PinName dio2, PinName dio3, PinName dio4, PinName dio5 )
-			:   Radio( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel ),
+			:   Radio( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone ),
 				spi( mosi, miso, sclk ),
 				nss( nss ),
 				reset( reset ),
@@ -124,7 +124,7 @@
 
 bool SX1276::IsChannelFree( ModemType modem, uint32_t freq, int8_t rssiThresh )
 {
-    int8_t rssi = 0;
+    int16_t rssi = 0;
     
     SetModem( modem );
 
@@ -138,7 +138,7 @@
     
     Sleep( );
     
-    if( rssi > rssiThresh )
+    if( rssi > ( int16_t )rssiThresh )
     {
         return false;
     }
@@ -871,9 +871,41 @@
     SetOpMode( RF_OPMODE_TRANSMITTER );
 }
 
-int8_t SX1276::GetRssi( ModemType modem )
+void SX1276::StartCad( void )
 {
-    int8_t rssi = 0;
+    switch( this->settings.Modem )
+    {
+    case MODEM_FSK:
+        {
+           
+        }
+        break;
+    case MODEM_LORA:
+        {
+            Write( REG_LR_IRQFLAGSMASK, RFLR_IRQFLAGS_RXTIMEOUT |
+                                        RFLR_IRQFLAGS_RXDONE |
+                                        RFLR_IRQFLAGS_PAYLOADCRCERROR |
+                                        RFLR_IRQFLAGS_VALIDHEADER |
+                                        RFLR_IRQFLAGS_TXDONE |
+                                        //RFLR_IRQFLAGS_CADDONE |
+                                        RFLR_IRQFLAGS_FHSSCHANGEDCHANNEL |
+                                        RFLR_IRQFLAGS_CADDETECTED );
+                                          
+            // DIO3=CADDone
+            Write( REG_DIOMAPPING1, ( Read( REG_DIOMAPPING1 ) & RFLR_DIOMAPPING1_DIO0_MASK ) | RFLR_DIOMAPPING1_DIO0_00 );
+            
+            this->settings.State = CAD;
+            SetOpMode( RFLR_OPMODE_CAD );
+        }
+        break;
+    default:
+        break;
+    }
+}
+
+int16_t SX1276::GetRssi( ModemType modem )
+{
+    int16_t rssi = 0;
 
     switch( modem )
     {
@@ -1126,7 +1158,7 @@
                         snr = ( this->settings.LoRaPacketHandler.SnrValue & 0xFF ) >> 2;
                     }
 
-                    int8_t rssi = Read( REG_LR_PKTRSSIVALUE );
+                    int16_t rssi = Read( REG_LR_PKTRSSIVALUE );
                     if( this->settings.LoRaPacketHandler.SnrValue < 0 )
                     {
                         if( this->settings.Channel > RF_MID_BAND_THRESH )
@@ -1340,6 +1372,10 @@
     case MODEM_FSK:
         break;
     case MODEM_LORA:
+        if( ( cadDone != NULL ) )
+        {
+            cadDone( );
+        }
         break;
     default:
         break;
--- a/sx1276/sx1276.h	Thu Sep 04 14:03:20 2014 +0000
+++ b/sx1276/sx1276.h	Fri Sep 19 14:16:35 2014 +0000
@@ -105,11 +105,12 @@
 	void RxChainCalibration( void );
 
 public:
-	SX1276( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int8_t rssi, int8_t snr ), 
-			void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ),
+	SX1276( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ), 
+			void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( ),
 			PinName mosi, PinName miso, PinName sclk, PinName nss, PinName reset,
             PinName dio0, PinName dio1, PinName dio2, PinName dio3, PinName dio4, PinName dio5 ); 
-    SX1276( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int8_t rssi, int8_t snr ), void ( *rxTimeout ) ( ), void ( *rxError ) ( ) );
+    SX1276( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ), 
+            void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( ) );
 	virtual ~SX1276( );
 	
 	//-------------------------------------------------------------------------
@@ -272,13 +273,18 @@
      *                     [0: continuous, others timeout]
      */
     virtual void Tx( uint32_t timeout );
+    
+	/*!
+     * @brief Start a Channel Activity Detection
+     */
+    virtual void StartCad( void );	
 	
 	/*!
      * @brief Reads the current RSSI value
      *
      * @retval rssiValue Current RSSI value in [dBm]
      */
-    virtual int8_t GetRssi ( ModemType modem );
+    virtual int16_t GetRssi ( ModemType modem );
     
 	/*!
      * @brief Writes the radio register at the specified address