pING

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   1
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                                    904500000 // Hz
00014 #define TX_OUTPUT_POWER                                 10       // units: dBm
00015 
00016 #if USE_MODEM_LORA == 1
00017 
00018     #define LORA_BANDWIDTH                              0         // [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                        16         // 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                            false
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                             false
00044     
00045 #else
00046     #error "Please define a modem in the compiler options."
00047 #endif
00048 
00049 #define RX_TIMEOUT_VALUE                                500000   // in us
00050 #define BUFFER_SIZE                                     8        // 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[] = "BBBBBBBB";
00090 const uint8_t PongMsg[] = "AAAAAAAA";
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         strcpy( ( char* )Buffer, ( char* )PingMsg );
00170         Radio.Send( Buffer, BufferSize );
00171     }
00172 }
00173 
00174 void OnTxDone( void )
00175 {
00176     Radio.Sleep( );
00177     State = TX;
00178     debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" );
00179 }
00180 
00181 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
00182 {
00183     Radio.Sleep( );
00184     BufferSize = size;
00185     memcpy(Buffer, payload, BufferSize);
00186     //printf("Payload length = %d \n\r",BufferSize);
00187     //printf("First 8 bytes -- %d %d %d %d %d %d %d %d\n\r", payload[0], payload[1], payload[2], payload[3], payload[4], payload[5], payload[6], payload[7]);
00188     RssiValue = rssi;
00189     SnrValue = snr;
00190     State = RX;
00191     debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" );
00192     printf("SINR Value = %d \n\r",SnrValue);
00193     printf("RSSI Value = %d \n\r",RssiValue);
00194     
00195 }
00196 
00197 void OnTxTimeout( void )
00198 {
00199     Radio.Sleep( );
00200     State = TX_TIMEOUT;
00201     debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" );
00202 }
00203 
00204 void OnRxTimeout( void )
00205 {
00206     Radio.Sleep( );
00207     Buffer[ BufferSize ] = 0;
00208     State = RX_TIMEOUT;
00209     debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" );
00210 
00211 }
00212 
00213 void OnRxError( void )
00214 {
00215     Radio.Sleep( );
00216     State = RX_ERROR;
00217     debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" );
00218 }