ENEL400 / Mbed 2 deprecated SX1276PingPong

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