Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of SX1272PingPong by
Diff: main.cpp
- Revision:
- 14:ab6eecd44b7a
- Parent:
- 13:edb9b443c1dd
- Child:
- 15:f48a416da6a8
--- a/main.cpp Wed Feb 17 09:17:46 2016 +0000
+++ b/main.cpp Mon Apr 24 09:29:13 2017 +0000
@@ -31,7 +31,7 @@
#define LORA_NB_SYMB_HOP 4
#define LORA_IQ_INVERSION_ON false
#define LORA_CRC_ENABLED true
-
+
#elif USE_MODEM_FSK == 1
#define FSK_FDEV 25000 // Hz
@@ -41,18 +41,18 @@
#define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx
#define FSK_FIX_LENGTH_PAYLOAD_ON false
#define FSK_CRC_ENABLED true
-
+
#else
#error "Please define a modem in the compiler options."
#endif
-#define RX_TIMEOUT_VALUE 3500000 // in us
+#define RX_TIMEOUT_VALUE 3500 // in ms
#define BUFFER_SIZE 32 // Define the payload size here
#if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) )
-DigitalOut led(LED2);
+DigitalOut led( LED2 );
#else
-DigitalOut led(LED1);
+DigitalOut led( LED1 );
#endif
/*
@@ -62,14 +62,14 @@
{
LOWPOWER = 0,
IDLE,
-
+
RX,
RX_TIMEOUT,
RX_ERROR,
-
+
TX,
TX_TIMEOUT,
-
+
CAD,
CAD_DONE
}AppStates_t;
@@ -95,11 +95,11 @@
int16_t RssiValue = 0.0;
int8_t SnrValue = 0.0;
-int main()
+int main( void )
{
uint8_t i;
bool isMaster = true;
-
+
debug( "\n\n\r SX1272 Ping Pong Demo Application \n\n\r" );
// Initialize Radio driver
@@ -109,60 +109,60 @@
RadioEvents.TxTimeout = OnTxTimeout;
RadioEvents.RxTimeout = OnRxTimeout;
Radio.Init( &RadioEvents );
-
+
// verify the connection with the board
while( Radio.Read( REG_VERSION ) == 0x00 )
{
debug( "Radio could not be detected!\n\r", NULL );
wait( 1 );
}
-
- debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ) , "\n\r > Board Type: SX1272MB2xAS < \n\r" );
-
+
+ debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ), "\n\r > Board Type: SX1272MB2xAS < \n\r" );
+
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, "\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,
- LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
- LORA_IQ_INVERSION_ON, 2000000 );
-
+ LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
+ LORA_IQ_INVERSION_ON, 2000 );
+
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,
+ LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
LORA_IQ_INVERSION_ON, true );
-
+
#elif USE_MODEM_FSK == 1
- debug("\n\n\r > FSK Mode < \n\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,
- FSK_CRC_ENABLED, 0, 0, 0, 2000000 );
-
+ FSK_CRC_ENABLED, 0, 0, 0, 2000 );
+
Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED,
0, 0, false, true );
-
+
#else
#error "Please define a modem in the compiler options."
#endif
-
- debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" );
-
+
+ debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" );
+
led = 0;
-
+
Radio.Rx( RX_TIMEOUT_VALUE );
-
+
while( 1 )
{
switch( State )
@@ -176,14 +176,14 @@
{
led = !led;
debug( "...Pong\r\n" );
- // Send the next PING frame
+ // Send the next PING frame
strcpy( ( char* )Buffer, ( char* )PingMsg );
- // We fill the buffer with numbers for the payload
+ // We fill the buffer with numbers for the payload
for( i = 4; i < BufferSize; i++ )
{
Buffer[i] = i - 4;
}
- wait_ms( 10 );
+ wait_ms( 10 );
Radio.Send( Buffer, BufferSize );
}
else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
@@ -191,21 +191,21 @@
debug( "...Ping\r\n" );
led = !led;
isMaster = false;
- // Send the next PONG frame
+ // Send the next PONG frame
strcpy( ( char* )Buffer, ( char* )PongMsg );
- // We fill the buffer with numbers for the payload
+ // We fill the buffer with numbers for the payload
for( i = 4; i < BufferSize; i++ )
{
Buffer[i] = i - 4;
}
- wait_ms( 10 );
+ wait_ms( 10 );
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 );
- }
+ }
}
}
else
@@ -218,25 +218,25 @@
debug( "...Ping\r\n" );
// Send the reply to the PING string
strcpy( ( char* )Buffer, ( char* )PongMsg );
- // We fill the buffer with numbers for the payload
+ // We fill the buffer with numbers for the payload
for( i = 4; i < BufferSize; i++ )
{
Buffer[i] = i - 4;
}
- wait_ms( 10 );
+ wait_ms( 10 );
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 );
- }
+ }
}
}
State = LOWPOWER;
break;
- case TX:
- led = !led;
+ case TX:
+ led = !led;
if( isMaster == true )
{
debug( "Ping...\r\n" );
@@ -257,13 +257,13 @@
{
Buffer[i] = i - 4;
}
- wait_ms( 10 );
+ wait_ms( 10 );
Radio.Send( Buffer, BufferSize );
}
else
{
- Radio.Rx( RX_TIMEOUT_VALUE );
- }
+ Radio.Rx( RX_TIMEOUT_VALUE );
+ }
State = LOWPOWER;
break;
case RX_ERROR:
@@ -276,7 +276,7 @@
{
Buffer[i] = i - 4;
}
- wait_ms( 10 );
+ wait_ms( 10 );
Radio.Send( Buffer, BufferSize );
}
else
@@ -287,7 +287,7 @@
{
Buffer[i] = i - 4;
}
- wait_ms( 10 );
+ wait_ms( 10 );
Radio.Send( Buffer, BufferSize );
}
State = LOWPOWER;
@@ -301,7 +301,7 @@
default:
State = LOWPOWER;
break;
- }
+ }
}
}
@@ -312,7 +312,7 @@
debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" );
}
-void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
+void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
{
Radio.Sleep( );
BufferSize = size;
@@ -333,7 +333,7 @@
void OnRxTimeout( void )
{
Radio.Sleep( );
- Buffer[ BufferSize ] = 0;
+ Buffer[BufferSize] = 0;
State = RX_TIMEOUT;
debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" );
}
@@ -344,4 +344,3 @@
State = RX_ERROR;
debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" );
}
-
