Timothy Mulrooney
/
SX1272PingPong
ping pong
Fork of SX1276PingPong by
Diff: main.cpp
- Revision:
- 13:1f1f356631e1
- Parent:
- 10:7af820d1e1df
- Child:
- 14:e6b2c1612a03
--- a/main.cpp Thu Nov 26 17:00:50 2015 +0000 +++ b/main.cpp Tue Feb 09 02:06:08 2016 +0000 @@ -1,16 +1,17 @@ #include "mbed.h" #include "main.h" -#include "sx1276-hal.h" +#include "sx1272-hal.h" #include "debug.h" /* Set this flag to '1' to display debug messages on the console */ -#define DEBUG_MESSAGE 0 +#define DEBUG_MESSAGE 1 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ #define USE_MODEM_LORA 1 #define USE_MODEM_FSK !USE_MODEM_LORA -#define RF_FREQUENCY 868000000 // Hz +//#define RF_FREQUENCY 868000000 // Hz +#define RF_FREQUENCY 915000000 // Hz US #define TX_OUTPUT_POWER 14 // 14 dBm #if USE_MODEM_LORA == 1 @@ -49,11 +50,16 @@ #define RX_TIMEOUT_VALUE 3500000 // in us #define BUFFER_SIZE 32 // Define the payload size here -#if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) -DigitalOut led(LED2); -#else -DigitalOut led(LED1); -#endif +//#if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) +//DigitalOut led(LED2); +//#else +//DigitalOut led(LED1); +//#endif +DigitalOut led(PTA1); +DigitalOut led_green(PTA2); +//DigitalOut led_blue(PTD5); + +Serial pc(PTE0, PTE1); /* * Global variables declarations @@ -84,7 +90,8 @@ /* * Global variables declarations */ -SX1276MB1xAS Radio( NULL ); +//SX1272MB1xAS Radio( NULL ); +SX1272MB1xAS *Radio; const uint8_t PingMsg[] = "PING"; const uint8_t PongMsg[] = "PONG"; @@ -97,10 +104,13 @@ int main() { + led_green = 0; uint8_t i; bool isMaster = true; - debug( "\n\n\r SX1276 Ping Pong Demo Application \n\n\r" ); + pc.baud(115200); + printf( "\r\nSX1272 Ping Pong Demo Application TJM\r\n" ); + Radio = new SX1272MB1xAS(NULL); // Initialize Radio driver RadioEvents.TxDone = OnTxDone; @@ -108,32 +118,33 @@ RadioEvents.RxError = OnRxError; RadioEvents.TxTimeout = OnTxTimeout; RadioEvents.RxTimeout = OnRxTimeout; - Radio.Init( &RadioEvents ); - + Radio->Init( &RadioEvents ); + // verify the connection with the board - while( Radio.Read( REG_VERSION ) == 0x00 ) + while( Radio->Read( REG_VERSION ) == 0x00 ) { - debug( "Radio could not be detected!\n\r", NULL ); + printf( "Radio could not be detected!\r\n", NULL ); wait( 1 ); } - - 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" ); + printf( "Radio detected!\r\n", NULL ); + + debug_if( ( DEBUG_MESSAGE & ( Radio->DetectBoardType( ) == SX1276MB1LAS ) ) , "> Board Type: SX1272MB1LAS < \r\n" ); + debug_if( ( DEBUG_MESSAGE & ( Radio->DetectBoardType( ) == SX1276MB1MAS ) ) , "> Board Type: SX1272MB1MAS < \r\n" ); + debug_if( ( DEBUG_MESSAGE & ( Radio->DetectBoardType( ) == SX1272RF1 ) ) , "> Board Type: SX1272RF1 < \r\n" ); - Radio.SetChannel( RF_FREQUENCY ); - + Radio->SetChannel( RF_FREQUENCY ); #if USE_MODEM_LORA == 1 - 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"); + debug_if( LORA_FHSS_ENABLED, "> LORA FHSS Mode <\r\n"); + debug_if( !LORA_FHSS_ENABLED, "> LORA Mode <\r\n"); - Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, + Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, LORA_CODINGRATE, LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, LORA_IQ_INVERSION_ON, 2000000 ); - Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, + Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, @@ -141,7 +152,7 @@ #elif USE_MODEM_FSK == 1 - debug("\n\n\r > FSK Mode < \n\n\r"); + debug("> FSK Mode < \r\n\n"); Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, FSK_DATARATE, 0, FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, @@ -161,22 +172,23 @@ debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" ); led = 0; - - Radio.Rx( RX_TIMEOUT_VALUE ); - + Radio->Rx( RX_TIMEOUT_VALUE ); + debug("State: %d\r\n",State); + while( 1 ) { switch( State ) { case RX: - if( isMaster == true ) + debug("State: RX\r\n"); + if( isMaster == true ) { if( BufferSize > 0 ) { if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 ) { led = !led; - debug( "...Pong\r\n" ); + printf( "...Pong\r\n" ); // Send the next PING frame strcpy( ( char* )Buffer, ( char* )PingMsg ); // We fill the buffer with numbers for the payload @@ -185,11 +197,11 @@ Buffer[i] = i - 4; } wait_ms( 10 ); - Radio.Send( Buffer, BufferSize ); + Radio->Send( Buffer, BufferSize ); } else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) { // A master already exists then become a slave - debug( "...Ping\r\n" ); + printf( "...Ping\r\n" ); led = !led; isMaster = false; // Send the next PONG frame @@ -200,12 +212,12 @@ Buffer[i] = i - 4; } wait_ms( 10 ); - Radio.Send( Buffer, BufferSize ); + Radio->Send( Buffer, BufferSize ); } else // valid reception but neither a PING or a PONG message { // Set device as master ans start again isMaster = true; - Radio.Rx( RX_TIMEOUT_VALUE ); + Radio->Rx( RX_TIMEOUT_VALUE ); } } } @@ -216,7 +228,7 @@ if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) { led = !led; - debug( "...Ping\r\n" ); + printf( "...Ping\r\n" ); // Send the reply to the PING string strcpy( ( char* )Buffer, ( char* )PongMsg ); // We fill the buffer with numbers for the payload @@ -225,32 +237,34 @@ Buffer[i] = i - 4; } wait_ms( 10 ); - Radio.Send( Buffer, BufferSize ); + Radio->Send( Buffer, BufferSize ); } else // valid reception but not a PING as expected { // Set device as master and start again isMaster = true; - Radio.Rx( RX_TIMEOUT_VALUE ); + Radio->Rx( RX_TIMEOUT_VALUE ); } } } State = LOWPOWER; break; case TX: - led = !led; + debug("State: TX\r\n"); + led = !led; if( isMaster == true ) { - debug( "Ping...\r\n" ); + printf( "Ping...\r\n" ); } else { - debug( "Pong...\r\n" ); + printf( "Pong...\r\n" ); } - Radio.Rx( RX_TIMEOUT_VALUE ); + Radio->Rx( RX_TIMEOUT_VALUE ); State = LOWPOWER; break; case RX_TIMEOUT: - if( isMaster == true ) + debug("State: RX_TIMEOUT\r\n"); + if( isMaster == true ) { // Send the next PING frame strcpy( ( char* )Buffer, ( char* )PingMsg ); @@ -259,16 +273,17 @@ Buffer[i] = i - 4; } wait_ms( 10 ); - Radio.Send( Buffer, BufferSize ); + Radio->Send( Buffer, BufferSize ); } else { - Radio.Rx( RX_TIMEOUT_VALUE ); + Radio->Rx( RX_TIMEOUT_VALUE ); } State = LOWPOWER; break; case RX_ERROR: - // We have received a Packet with a CRC error, send reply as if packet was correct + debug("State: RX_ERROR\r\n"); + // We have received a Packet with a CRC error, send reply as if packet was correct if( isMaster == true ) { // Send the next PING frame @@ -278,7 +293,7 @@ Buffer[i] = i - 4; } wait_ms( 10 ); - Radio.Send( Buffer, BufferSize ); + Radio->Send( Buffer, BufferSize ); } else { @@ -289,12 +304,13 @@ Buffer[i] = i - 4; } wait_ms( 10 ); - Radio.Send( Buffer, BufferSize ); + Radio->Send( Buffer, BufferSize ); } State = LOWPOWER; break; case TX_TIMEOUT: - Radio.Rx( RX_TIMEOUT_VALUE ); + debug("State: TX_TIMEOUT\r\n"); + Radio->Rx( RX_TIMEOUT_VALUE ); State = LOWPOWER; break; case LOWPOWER: @@ -304,45 +320,45 @@ break; } } -} + } void OnTxDone( void ) { - Radio.Sleep( ); + Radio->Sleep( ); State = TX; - debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" ); + debug_if( DEBUG_MESSAGE, "> OnTxDone\r\n" ); } void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) { - Radio.Sleep( ); + Radio->Sleep( ); BufferSize = size; memcpy( Buffer, payload, BufferSize ); RssiValue = rssi; SnrValue = snr; State = RX; - debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" ); + debug_if( DEBUG_MESSAGE, "> OnRxDone\r\n" ); } void OnTxTimeout( void ) { - Radio.Sleep( ); + Radio->Sleep( ); State = TX_TIMEOUT; - debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" ); + debug_if( DEBUG_MESSAGE, "> OnTxTimeout\r\n" ); } void OnRxTimeout( void ) { - Radio.Sleep( ); + Radio->Sleep( ); Buffer[ BufferSize ] = 0; State = RX_TIMEOUT; - debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" ); + debug_if( DEBUG_MESSAGE, "> OnRxTimeout\r\n" ); } void OnRxError( void ) { - Radio.Sleep( ); + Radio->Sleep( ); State = RX_ERROR; - debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" ); + debug_if( DEBUG_MESSAGE, "> OnRxError\r\n" ); }