SX1276 Ping Pong Demo Application

Dependencies:   SX1276Lib mbed

Fork of SX1276PingPong by Semtech

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "main.h"
00003 #include "sx1276-hal.h"
00004 #include "debug.h"
00005 
00006 /* Set this flag to '1' to display debug messages on the console */
00007 #define DEBUG_MESSAGE   0
00008 
00009 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */
00010 #define USE_MODEM_LORA  1
00011 #define USE_MODEM_FSK   !USE_MODEM_LORA
00012 
00013 #define RF_FREQUENCY                                    868000000 // Hz
00014 #define TX_OUTPUT_POWER                                 14        // 14 dBm
00015 
00016 #if USE_MODEM_LORA == 1
00017 
00018     #define LORA_BANDWIDTH                              2         // [0: 125 kHz,
00019                                                                   //  1: 250 kHz,
00020                                                                   //  2: 500 kHz,
00021                                                                   //  3: Reserved]
00022     #define LORA_SPREADING_FACTOR                       7         // [SF7..SF12]
00023     #define LORA_CODINGRATE                             1         // [1: 4/5,
00024                                                                   //  2: 4/6,
00025                                                                   //  3: 4/7,
00026                                                                   //  4: 4/8]
00027     #define LORA_PREAMBLE_LENGTH                        8         // Same for Tx and Rx
00028     #define LORA_SYMBOL_TIMEOUT                         5         // Symbols
00029     #define LORA_FIX_LENGTH_PAYLOAD_ON                  false
00030     #define LORA_FHSS_ENABLED                           false  
00031     #define LORA_NB_SYMB_HOP                            4     
00032     #define LORA_IQ_INVERSION_ON                        false
00033     #define LORA_CRC_ENABLED                            true
00034     
00035 #elif USE_MODEM_FSK == 1
00036 
00037     #define FSK_FDEV                                    25000     // Hz
00038     #define FSK_DATARATE                                19200     // bps
00039     #define FSK_BANDWIDTH                               50000     // Hz
00040     #define FSK_AFC_BANDWIDTH                           83333     // Hz
00041     #define FSK_PREAMBLE_LENGTH                         5         // Same for Tx and Rx
00042     #define FSK_FIX_LENGTH_PAYLOAD_ON                   false
00043     #define FSK_CRC_ENABLED                             true
00044     
00045 #else
00046     #error "Please define a modem in the compiler options."
00047 #endif
00048 
00049 #define RX_TIMEOUT_VALUE                                3500000   // in us
00050 #define BUFFER_SIZE                                     32        // Define the payload size here
00051 
00052 #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) )
00053 DigitalOut led(LED2);
00054 #else
00055 DigitalOut led(LED1);
00056 #endif
00057 
00058 /*
00059  *  Global variables declarations
00060  */
00061 typedef enum
00062 {
00063     LOWPOWER = 0,
00064     IDLE,
00065     
00066     RX,
00067     RX_TIMEOUT,
00068     RX_ERROR,
00069     
00070     TX,
00071     TX_TIMEOUT,
00072     
00073     CAD,
00074     CAD_DONE
00075 }AppStates_t;
00076 
00077 volatile AppStates_t State = LOWPOWER;
00078 
00079 /*!
00080  * Radio events function pointer
00081  */
00082 static RadioEvents_t RadioEvents;
00083 
00084 /*
00085  *  Global variables declarations
00086  */
00087 SX1276MB1xAS Radio( NULL );
00088 
00089 const uint8_t PingMsg[] = "PING";
00090 const uint8_t PongMsg[] = "PONG";
00091 
00092 uint16_t BufferSize = BUFFER_SIZE;
00093 uint8_t Buffer[BUFFER_SIZE];
00094 
00095 int16_t RssiValue = 0.0;
00096 int8_t SnrValue = 0.0;
00097 
00098 int main() 
00099 {
00100     uint8_t i;
00101     bool isMaster = true;
00102     
00103     debug( "\n\n\r     SX1276 Ping Pong Demo Application \n\n\r" );
00104 
00105     // Initialize Radio driver
00106     RadioEvents.TxDone = OnTxDone;
00107     RadioEvents.RxDone = OnRxDone;
00108     RadioEvents.RxError = OnRxError;
00109     RadioEvents.TxTimeout = OnTxTimeout;
00110     RadioEvents.RxTimeout = OnRxTimeout;
00111     Radio.Init( &RadioEvents );
00112     
00113     // verify the connection with the board
00114     while( Radio.Read( REG_VERSION ) == 0x00  )
00115     {
00116         debug( "Radio could not be detected!\n\r", NULL );
00117         wait( 1 );
00118     }
00119             
00120     debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ) , "\n\r > Board Type: SX1276MB1LAS < \n\r" );
00121     debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ) , "\n\r > Board Type: SX1276MB1MAS < \n\r" );
00122     
00123     Radio.SetChannel( RF_FREQUENCY ); 
00124 
00125 #if USE_MODEM_LORA == 1
00126     
00127     debug_if( LORA_FHSS_ENABLED, "\n\n\r             > LORA FHSS Mode < \n\n\r");
00128     debug_if( !LORA_FHSS_ENABLED, "\n\n\r             > LORA Mode < \n\n\r");
00129 
00130     Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
00131                          LORA_SPREADING_FACTOR, LORA_CODINGRATE,
00132                          LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
00133                          LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 
00134                          LORA_IQ_INVERSION_ON, 2000000 );
00135     
00136     Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
00137                          LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
00138                          LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
00139                          LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 
00140                          LORA_IQ_INVERSION_ON, true );
00141                          
00142 #elif USE_MODEM_FSK == 1
00143 
00144     debug("\n\n\r              > FSK Mode < \n\n\r");
00145     Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
00146                          FSK_DATARATE, 0,
00147                          FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
00148                          FSK_CRC_ENABLED, 0, 0, 0, 2000000 );
00149     
00150     Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
00151                          0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
00152                          0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED,
00153                          0, 0, false, true );
00154                          
00155 #else
00156 
00157 #error "Please define a modem in the compiler options."
00158 
00159 #endif
00160      
00161     debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" ); 
00162         
00163     led = 0;
00164         
00165     Radio.Rx( RX_TIMEOUT_VALUE );
00166     
00167     while( 1 )
00168     {
00169         switch( State )
00170         {
00171         case RX:
00172             if( isMaster == true )
00173             {
00174                 if( BufferSize > 0 )
00175                 {
00176                     if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 )
00177                     {
00178                         led = !led;
00179                         debug( "...Pong\r\n" );
00180                         // Send the next PING frame            
00181                         strcpy( ( char* )Buffer, ( char* )PingMsg );
00182                         // We fill the buffer with numbers for the payload 
00183                         for( i = 4; i < BufferSize; i++ )
00184                         {
00185                             Buffer[i] = i - 4;
00186                         }
00187                         wait_ms( 10 ); 
00188                         Radio.Send( Buffer, BufferSize );
00189                     }
00190                     else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
00191                     { // A master already exists then become a slave
00192                         debug( "...Ping\r\n" );
00193                         led = !led;
00194                         isMaster = false;
00195                         // Send the next PONG frame            
00196                         strcpy( ( char* )Buffer, ( char* )PongMsg );
00197                         // We fill the buffer with numbers for the payload 
00198                         for( i = 4; i < BufferSize; i++ )
00199                         {
00200                             Buffer[i] = i - 4;
00201                         }
00202                         wait_ms( 10 ); 
00203                         Radio.Send( Buffer, BufferSize );
00204                     }
00205                     else // valid reception but neither a PING or a PONG message
00206                     {    // Set device as master ans start again
00207                         isMaster = true;
00208                         Radio.Rx( RX_TIMEOUT_VALUE );
00209                     }    
00210                 }
00211             }
00212             else
00213             {
00214                 if( BufferSize > 0 )
00215                 {
00216                     if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
00217                     {
00218                         led = !led;
00219                         debug( "...Ping\r\n" );
00220                         // Send the reply to the PING string
00221                         strcpy( ( char* )Buffer, ( char* )PongMsg );
00222                         // We fill the buffer with numbers for the payload 
00223                         for( i = 4; i < BufferSize; i++ )
00224                         {
00225                             Buffer[i] = i - 4;
00226                         }
00227                         wait_ms( 10 );  
00228                         Radio.Send( Buffer, BufferSize );
00229                     }
00230                     else // valid reception but not a PING as expected
00231                     {    // Set device as master and start again
00232                         isMaster = true;
00233                         Radio.Rx( RX_TIMEOUT_VALUE );
00234                     }    
00235                 }
00236             }
00237             State = LOWPOWER;
00238             break;
00239         case TX:    
00240             led = !led; 
00241             if( isMaster == true )  
00242             {
00243                 debug( "Ping...\r\n" );
00244             }
00245             else
00246             {
00247                 debug( "Pong...\r\n" );
00248             }
00249             Radio.Rx( RX_TIMEOUT_VALUE );
00250             State = LOWPOWER;
00251             break;
00252         case RX_TIMEOUT:
00253             if( isMaster == true )
00254             {
00255                 // Send the next PING frame
00256                 strcpy( ( char* )Buffer, ( char* )PingMsg );
00257                 for( i = 4; i < BufferSize; i++ )
00258                 {
00259                     Buffer[i] = i - 4;
00260                 }
00261                 wait_ms( 10 ); 
00262                 Radio.Send( Buffer, BufferSize );
00263             }
00264             else
00265             {
00266                 Radio.Rx( RX_TIMEOUT_VALUE );  
00267             }             
00268             State = LOWPOWER;
00269             break;
00270         case RX_ERROR:
00271             // We have received a Packet with a CRC error, send reply as if packet was correct
00272             if( isMaster == true )
00273             {
00274                 // Send the next PING frame
00275                 strcpy( ( char* )Buffer, ( char* )PingMsg );
00276                 for( i = 4; i < BufferSize; i++ )
00277                 {
00278                     Buffer[i] = i - 4;
00279                 }
00280                 wait_ms( 10 );  
00281                 Radio.Send( Buffer, BufferSize );
00282             }
00283             else
00284             {
00285                 // Send the next PONG frame
00286                 strcpy( ( char* )Buffer, ( char* )PongMsg );
00287                 for( i = 4; i < BufferSize; i++ )
00288                 {
00289                     Buffer[i] = i - 4;
00290                 }
00291                 wait_ms( 10 );  
00292                 Radio.Send( Buffer, BufferSize );
00293             }
00294             State = LOWPOWER;
00295             break;
00296         case TX_TIMEOUT:
00297             Radio.Rx( RX_TIMEOUT_VALUE );
00298             State = LOWPOWER;
00299             break;
00300         case LOWPOWER:
00301             break;
00302         default:
00303             State = LOWPOWER;
00304             break;
00305         }    
00306     }
00307 }
00308 
00309 void OnTxDone( void )
00310 {
00311     Radio.Sleep( );
00312     State = TX;
00313     debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" );
00314 }
00315 
00316 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
00317 {
00318     Radio.Sleep( );
00319     BufferSize = size;
00320     memcpy( Buffer, payload, BufferSize );
00321     RssiValue = rssi;
00322     SnrValue = snr;
00323     State = RX;
00324     debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" );
00325 }
00326 
00327 void OnTxTimeout( void )
00328 {
00329     Radio.Sleep( );
00330     State = TX_TIMEOUT;
00331     debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" );
00332 }
00333 
00334 void OnRxTimeout( void )
00335 {
00336     Radio.Sleep( );
00337     Buffer[ BufferSize ] = 0;
00338     State = RX_TIMEOUT;
00339     debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" );
00340 }
00341 
00342 void OnRxError( void )
00343 {
00344     Radio.Sleep( );
00345     State = RX_ERROR;
00346     debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" );
00347 }
00348