SX1276 Ping Pong FHSS Demo Application
Ping-Pong demo application using FHSS (Frequency Hoping Spread Spectrum) between two SX1276MB1xAs demo board.
Application demonstrating simple Tx/Rx between two boards. By default, each board starts as a "master" and will transmit a "Ping" message, and then wait for an answer. The first board receiving a "Ping" message will become a slave and answer the "master" with a "Pong". The Ping-Pong is then started...
Diff: main.cpp
- Revision:
- 2:b03bdfedfafa
- Parent:
- 1:41b10c760ff2
- Child:
- 3:99bd9ae6906f
--- a/main.cpp Tue Sep 23 14:25:25 2014 +0000 +++ b/main.cpp Mon Oct 13 07:45:39 2014 +0000 @@ -1,11 +1,10 @@ #include "mbed.h" +#include "main.h" #include "sx1276-hal.h" -#include "main.h" -#include "mbed_debug.h" +#include "debug.h" /* Set this flag to '1' to display debug messages on the console */ -#define DEBUG_MESSAGE 1 - +#define DEBUG_MESSAGE 0 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ #define USE_MODEM_LORA 1 @@ -47,10 +46,10 @@ #error "Please define a modem in the compiler options." #endif -#define RX_TIMEOUT_VALUE 4000000 // in us -#define BUFFER_SIZE 32 // Define the payload size here +#define RX_TIMEOUT_VALUE 3500000 // in us +#define BUFFER_SIZE 32 // Define the payload size here [min:1 max:255] -#if( defined ( TARGET_KL25Z ) ) +#if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) DigitalOut led(LED2); #else DigitalOut led(LED1); @@ -60,6 +59,7 @@ * Global variables declarations */ typedef RadioState States_t; +volatile States_t State = LOWPOWER; /* * Global variables declarations @@ -72,8 +72,6 @@ uint16_t BufferSize = BUFFER_SIZE; uint8_t Buffer[BUFFER_SIZE]; -volatile States_t State = LOWPOWER; - int16_t RssiValue = 0.0; int8_t SnrValue = 0.0; @@ -82,41 +80,25 @@ uint8_t i; bool isMaster = true; - debug( "\n\r\n\r SX1276 Ping Pong Demo Application \n\r" ); - -#if defined TARGET_NUCLEO_L152RE - debug_if( DEBUG_MESSAGE, " > Nucleo-L152RE Platform <\r\n", NULL ); -#elif defined TARGET_KL25Z - debug_if( DEBUG_MESSAGE, " > KL25Z Platform <\r\n", NULL ); -#elif defined TARGET_LPC11U6X - debug_if( DEBUG_MESSAGE, " > LPC11U6X Platform <\r\n", NULL ); -#else - debug_if( DEBUG_MESSAGE, " > Untested Platform <\r\n", NULL ); -#endif + debug( "\n\n\r SX1276 Ping Pong Demo Application \n\n\r" ); - if( Radio.DetectBoardType( ) == SX1276MB1LAS ) + // verify the connection with the board + while( Radio.Read( REG_VERSION ) == 0x00 ) { - debug_if( DEBUG_MESSAGE, "\n\r > Board Type: SX1276MB1LAS < \n\r", NULL ); + debug( "Radio could not be detected!\n\r", NULL ); + wait( 1 ); } - else - { - debug_if( DEBUG_MESSAGE, "\n\r > Board Type: SX1276MB1MAS < \n\r", NULL ); - } - - debug_if( DEBUG_MESSAGE, " > Chipset Version = 0x%x < \n\r", Radio.Read( REG_VERSION ) ); + + debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ) , "\n\r > Board Type: SX1276MB1LAS < \n\r" ); + debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ) , "\n\r > Board Type: SX1276MB1MAS < \n\r" ); Radio.SetChannel( HoppingFrequencies[0] ); #if USE_MODEM_LORA == 1 - if( LORA_FHSS_ENABLED == true ) - { - debug("\n\r\n\r > LORA FHSS Mode < \n\r\n\r"); - } - else - { - debug("\n\r\n\r > LORA Mode < \n\r\n\r"); - } + debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r"); + debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r"); + Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, LORA_CODINGRATE, LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, @@ -131,7 +113,7 @@ #elif USE_MODEM_FSK == 1 - debug("\n\r\n\r > FSK Mode < \n\r\n\r"); + debug("\n\n\r > FSK Mode < \n\n\r"); Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, FSK_DATARATE, 0, FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, @@ -148,7 +130,7 @@ #endif - debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n", NULL ); + debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" ); led = 0; @@ -168,10 +150,7 @@ led = !led; debug( "...Pong\r\n" ); // Send the next PING frame - Buffer[0] = 'P'; - Buffer[1] = 'I'; - Buffer[2] = 'N'; - Buffer[3] = 'G'; + strcpy( ( char* )Buffer, ( char* )PingMsg ); // We fill the buffer with numbers for the payload for( i = 4; i < BufferSize; i++ ) { @@ -185,11 +164,8 @@ debug( "...Ping\r\n" ); led = !led; isMaster = false; - // Send the next PING frame - Buffer[0] = 'P'; - Buffer[1] = 'O'; - Buffer[2] = 'N'; - Buffer[3] = 'G'; + // Send the next PONG frame + strcpy( ( char* )Buffer, ( char* )PongMsg ); // We fill the buffer with numbers for the payload for( i = 4; i < BufferSize; i++ ) { @@ -214,10 +190,7 @@ led = !led; debug( "...Ping\r\n" ); // Send the reply to the PING string - Buffer[0] = 'P'; - Buffer[1] = 'O'; - Buffer[2] = 'N'; - Buffer[3] = 'G'; + strcpy( ( char* )Buffer, ( char* )PongMsg ); // We fill the buffer with numbers for the payload for( i = 4; i < BufferSize; i++ ) { @@ -252,10 +225,7 @@ if( isMaster == true ) { // Send the next PING frame - Buffer[0] = 'P'; - Buffer[1] = 'I'; - Buffer[2] = 'N'; - Buffer[3] = 'G'; + strcpy( ( char* )Buffer, ( char* )PingMsg ); for( i = 4; i < BufferSize; i++ ) { Buffer[i] = i - 4; @@ -270,13 +240,11 @@ State = LOWPOWER; break; case RX_ERROR: + // We have received a Packet with a CRC error, send reply as if packet was correct if( isMaster == true ) { // Send the next PING frame - Buffer[0] = 'P'; - Buffer[1] = 'I'; - Buffer[2] = 'N'; - Buffer[3] = 'G'; + strcpy( ( char* )Buffer, ( char* )PingMsg ); for( i = 4; i < BufferSize; i++ ) { Buffer[i] = i - 4; @@ -287,10 +255,7 @@ else { // Send the next PONG frame - Buffer[0] = 'P'; - Buffer[1] = 'O'; - Buffer[2] = 'N'; - Buffer[3] = 'G'; + strcpy( ( char* )Buffer, ( char* )PongMsg ); for( i = 4; i < BufferSize; i++ ) { Buffer[i] = i - 4; @@ -318,7 +283,7 @@ Radio.SetChannel( HoppingFrequencies[0] ); Radio.Sleep( ); State = TX; - debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r", NULL ); + debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" ); } void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) @@ -330,7 +295,7 @@ RssiValue = rssi; SnrValue = snr; State = RX; - debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r", NULL ); + debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" ); } void OnTxTimeout( void ) @@ -338,7 +303,7 @@ Radio.SetChannel( HoppingFrequencies[0] ); Radio.Sleep( ); State = TX_TIMEOUT; - debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r", NULL ); + debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" ); } void OnRxTimeout( void ) @@ -347,7 +312,7 @@ Radio.Sleep( ); Buffer[ BufferSize ] = 0; State = RX_TIMEOUT; - debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r", NULL ); + debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" ); } void OnRxError( void ) @@ -355,7 +320,7 @@ Radio.SetChannel( HoppingFrequencies[0] ); Radio.Sleep( ); State = RX_ERROR; - debug_if( DEBUG_MESSAGE, "> OnRxError\n\r", NULL ); + debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" ); } void OnFhssChangeChannel( uint8_t channelIndex )