SX1276 Ping Pong FHSS Demo Application

Dependencies:   SX1276Lib mbed

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                                    915000000 // Hz
00014 #define TX_OUTPUT_POWER                                 14        // 14 dBm
00015 
00016 #if USE_MODEM_LORA == 1
00017 
00018     #define LORA_BANDWIDTH                              1         // [0: 125 kHz,
00019                                                                   //  1: 250 kHz,
00020                                                                   //  2: 500 kHz,
00021                                                                   //  3: Reserved]
00022     #define LORA_SPREADING_FACTOR                       10         // [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                           true  
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                                3500      // in ms
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( void ) 
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     RadioEvents.FhssChangeChannel = OnFhssChangeChannel;
00112     Radio.Init( &RadioEvents );
00113 
00114     // verify the connection with the board
00115     while( Radio.Read( REG_VERSION ) == 0x00  )
00116     {
00117         debug( "Radio could not be detected!\n\r", NULL );
00118         wait( 1 );
00119     }
00120 
00121     debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ), "\n\r > Board Type: SX1276MB1LAS < \n\r" );
00122     debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ), "\n\r > Board Type: SX1276MB1MAS < \n\r" );
00123 
00124     Radio.SetChannel( HoppingFrequencies[0] ); 
00125 
00126 #if USE_MODEM_LORA == 1
00127 
00128     debug_if( LORA_FHSS_ENABLED, "\n\n\r             > LORA FHSS Mode < \n\n\r" );
00129     debug_if( !LORA_FHSS_ENABLED, "\n\n\r             > LORA Mode < \n\n\r" );
00130 
00131     Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
00132                          LORA_SPREADING_FACTOR, LORA_CODINGRATE,
00133                          LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
00134                          LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
00135                          LORA_IQ_INVERSION_ON, 4000 );
00136 
00137     Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
00138                          LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
00139                          LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
00140                          LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
00141                          LORA_IQ_INVERSION_ON, true );
00142 
00143 #elif USE_MODEM_FSK == 1
00144 
00145     debug("\n\n\r              > FSK Mode < \n\n\r" );
00146     Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
00147                          FSK_DATARATE, 0,
00148                          FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
00149                          FSK_CRC_ENABLED, 0, 0, 0, 3000 );
00150 
00151     Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
00152                          0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
00153                          0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED,
00154                          0, 0, false, true );
00155 
00156 #else
00157 
00158 #error "Please define a modem in the compiler options."
00159 
00160 #endif
00161 
00162     debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" );
00163 
00164     led = 0;
00165 
00166     Radio.Rx( RX_TIMEOUT_VALUE );
00167 
00168     while( 1 )
00169     {
00170         switch( State )
00171         {
00172         case RX:
00173             if( isMaster == true )
00174             {
00175                 if( BufferSize > 0 )
00176                 {
00177                     if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 )
00178                     {
00179                         led = !led;
00180                         debug( "...Pong\r\n" );
00181                         // Send the next PING frame
00182                         strcpy( ( char* )Buffer, ( char* )PingMsg );
00183                         // We fill the buffer with numbers for the payload
00184                         for( i = 4; i < BufferSize; i++ )
00185                         {
00186                             Buffer[i] = i - 4;
00187                         }
00188                         wait_ms( 10 );
00189                         Radio.Send( Buffer, BufferSize );
00190                     }
00191                     else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
00192                     { // A master already exists then become a slave
00193                         debug( "...Ping\r\n" );
00194                         led = !led;
00195                         isMaster = false;
00196                         // Send the next PONG frame
00197                         strcpy( ( char* )Buffer, ( char* )PongMsg );
00198                         // We fill the buffer with numbers for the payload
00199                         for( i = 4; i < BufferSize; i++ )
00200                         {
00201                             Buffer[i] = i - 4;
00202                         }
00203                         wait_ms( 10 );
00204                         Radio.Send( Buffer, BufferSize );
00205                     }
00206                     else // valid reception but neither a PING or a PONG message
00207                     {    // Set device as master ans start again
00208                         isMaster = true;
00209                         Radio.Rx( RX_TIMEOUT_VALUE );
00210                     }
00211                 }
00212             }
00213             else
00214             {
00215                 if( BufferSize > 0 )
00216                 {
00217                     if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
00218                     {
00219                         led = !led;
00220                         debug( "...Ping\r\n" );
00221                         // Send the reply to the PING string
00222                         strcpy( ( char* )Buffer, ( char* )PongMsg );
00223                         // We fill the buffer with numbers for the payload
00224                         for( i = 4; i < BufferSize; i++ )
00225                         {
00226                             Buffer[i] = i - 4;
00227                         }
00228                         wait_ms( 10 );
00229                         Radio.Send( Buffer, BufferSize );
00230                     }
00231                     else // valid reception but not a PING as expected
00232                     {    // Set device as master and start again
00233                         isMaster = true;
00234                         Radio.Rx( RX_TIMEOUT_VALUE );
00235                     }
00236                 }
00237             }
00238             State = LOWPOWER;
00239             break;
00240         case TX:
00241             led = !led;
00242             if( isMaster == true )  
00243             {
00244                 debug( "Ping...\r\n" );
00245             }
00246             else
00247             {
00248                 debug( "Pong...\r\n" );
00249             }
00250             Radio.Rx( RX_TIMEOUT_VALUE );
00251             State = LOWPOWER;
00252             break;
00253         case RX_TIMEOUT:
00254             if( isMaster == true )
00255             {
00256                 // Send the next PING frame
00257                 strcpy( ( char* )Buffer, ( char* )PingMsg );
00258                 for( i = 4; i < BufferSize; i++ )
00259                 {
00260                     Buffer[i] = i - 4;
00261                 }
00262                 wait_ms( 10 );
00263                 Radio.Send( Buffer, BufferSize );
00264             }
00265             else
00266             {
00267                 Radio.Rx( RX_TIMEOUT_VALUE );
00268             }
00269             State = LOWPOWER;
00270             break;
00271         case RX_ERROR:
00272             // We have received a Packet with a CRC error, send reply as if packet was correct
00273             if( isMaster == true )
00274             {
00275                 // Send the next PING frame
00276                 strcpy( ( char* )Buffer, ( char* )PingMsg );
00277                 for( i = 4; i < BufferSize; i++ )
00278                 {
00279                     Buffer[i] = i - 4;
00280                 }
00281                 wait_ms( 10 );
00282                 Radio.Send( Buffer, BufferSize );
00283             }
00284             else
00285             {
00286                 // Send the next PONG frame
00287                 strcpy( ( char* )Buffer, ( char* )PongMsg );
00288                 for( i = 4; i < BufferSize; i++ )
00289                 {
00290                     Buffer[i] = i - 4;
00291                 }
00292                 wait_ms( 10 );
00293                 Radio.Send( Buffer, BufferSize );
00294             }
00295             State = LOWPOWER;
00296             break;
00297         case TX_TIMEOUT:
00298             Radio.Rx( RX_TIMEOUT_VALUE );
00299             State = LOWPOWER;
00300             break;
00301         case LOWPOWER:
00302             break;
00303         default:
00304             State = LOWPOWER;
00305             break;
00306         }
00307     }
00308 }
00309 
00310 void OnTxDone( void )
00311 {
00312     Radio.SetChannel( HoppingFrequencies[0] );
00313     Radio.Sleep( );
00314     State = TX;
00315     debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" );
00316 }
00317 
00318 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
00319 {
00320     Radio.SetChannel( HoppingFrequencies[0] );
00321     Radio.Sleep( );
00322     BufferSize = size;
00323     memcpy( Buffer, payload, BufferSize );
00324     RssiValue = rssi;
00325     SnrValue = snr;
00326     State = RX;
00327     debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" );
00328 }
00329 
00330 void OnTxTimeout( void )
00331 {
00332     Radio.SetChannel( HoppingFrequencies[0] );
00333     Radio.Sleep( );
00334     State = TX_TIMEOUT;
00335     debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" );
00336 }
00337 
00338 void OnRxTimeout( void )
00339 {
00340     Radio.SetChannel( HoppingFrequencies[0] );
00341     Radio.Sleep( );
00342     Buffer[BufferSize] = 0;
00343     State = RX_TIMEOUT;
00344     debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" );
00345 }
00346 
00347 void OnRxError( void )
00348 {
00349     Radio.SetChannel( HoppingFrequencies[0] );
00350     Radio.Sleep( );
00351     State = RX_ERROR;
00352     debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" );
00353 }
00354 
00355 void OnFhssChangeChannel( uint8_t channelIndex )
00356 {
00357     Radio.SetChannel( HoppingFrequencies[channelIndex] );
00358     debug_if( DEBUG_MESSAGE, "F%d-", channelIndex );
00359 }