Semtech
/
SX1276PingPong
SX1276 Ping Pong Demo Application
Embed:
(wiki syntax)
Show/hide line numbers
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 3500 // in ms 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( void ) 00098 { 00099 uint8_t i; 00100 bool isMaster = true; 00101 00102 debug( "\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 debug( "Radio could not be detected!\n\r", NULL ); 00116 wait( 1 ); 00117 } 00118 00119 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ), "\n\r > Board Type: SX1276MB1LAS < \n\r" ); 00120 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ), "\n\r > Board Type: SX1276MB1MAS < \n\r" ); 00121 00122 Radio.SetChannel( RF_FREQUENCY ); 00123 00124 #if USE_MODEM_LORA == 1 00125 00126 debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r" ); 00127 debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r" ); 00128 00129 Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, 00130 LORA_SPREADING_FACTOR, LORA_CODINGRATE, 00131 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, 00132 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 00133 LORA_IQ_INVERSION_ON, 2000 ); 00134 00135 Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, 00136 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, 00137 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, 00138 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 00139 LORA_IQ_INVERSION_ON, true ); 00140 00141 #elif USE_MODEM_FSK == 1 00142 00143 debug("\n\n\r > FSK Mode < \n\n\r" ); 00144 Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, 00145 FSK_DATARATE, 0, 00146 FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, 00147 FSK_CRC_ENABLED, 0, 0, 0, 2000 ); 00148 00149 Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, 00150 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, 00151 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, 00152 0, 0, false, true ); 00153 00154 #else 00155 00156 #error "Please define a modem in the compiler options." 00157 00158 #endif 00159 00160 debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" ); 00161 00162 led = 0; 00163 00164 Radio.Rx( RX_TIMEOUT_VALUE ); 00165 00166 while( 1 ) 00167 { 00168 switch( State ) 00169 { 00170 case RX: 00171 if( isMaster == true ) 00172 { 00173 if( BufferSize > 0 ) 00174 { 00175 if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 ) 00176 { 00177 led = !led; 00178 debug( "...Pong\r\n" ); 00179 // Send the next PING frame 00180 strcpy( ( char* )Buffer, ( char* )PingMsg ); 00181 // We fill the buffer with numbers for the payload 00182 for( i = 4; i < BufferSize; i++ ) 00183 { 00184 Buffer[i] = i - 4; 00185 } 00186 wait_ms( 10 ); 00187 Radio.Send( Buffer, BufferSize ); 00188 } 00189 else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) 00190 { // A master already exists then become a slave 00191 debug( "...Ping\r\n" ); 00192 led = !led; 00193 isMaster = false; 00194 // Send the next PONG frame 00195 strcpy( ( char* )Buffer, ( char* )PongMsg ); 00196 // We fill the buffer with numbers for the payload 00197 for( i = 4; i < BufferSize; i++ ) 00198 { 00199 Buffer[i] = i - 4; 00200 } 00201 wait_ms( 10 ); 00202 Radio.Send( Buffer, BufferSize ); 00203 } 00204 else // valid reception but neither a PING or a PONG message 00205 { // Set device as master ans start again 00206 isMaster = true; 00207 Radio.Rx( RX_TIMEOUT_VALUE ); 00208 } 00209 } 00210 } 00211 else 00212 { 00213 if( BufferSize > 0 ) 00214 { 00215 if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) 00216 { 00217 led = !led; 00218 debug( "...Ping\r\n" ); 00219 // Send the reply to the PING string 00220 strcpy( ( char* )Buffer, ( char* )PongMsg ); 00221 // We fill the buffer with numbers for the payload 00222 for( i = 4; i < BufferSize; i++ ) 00223 { 00224 Buffer[i] = i - 4; 00225 } 00226 wait_ms( 10 ); 00227 Radio.Send( Buffer, BufferSize ); 00228 } 00229 else // valid reception but not a PING as expected 00230 { // Set device as master and start again 00231 isMaster = true; 00232 Radio.Rx( RX_TIMEOUT_VALUE ); 00233 } 00234 } 00235 } 00236 State = LOWPOWER; 00237 break; 00238 case TX: 00239 led = !led; 00240 if( isMaster == true ) 00241 { 00242 debug( "Ping...\r\n" ); 00243 } 00244 else 00245 { 00246 debug( "Pong...\r\n" ); 00247 } 00248 Radio.Rx( RX_TIMEOUT_VALUE ); 00249 State = LOWPOWER; 00250 break; 00251 case RX_TIMEOUT: 00252 if( isMaster == true ) 00253 { 00254 // Send the next PING frame 00255 strcpy( ( char* )Buffer, ( char* )PingMsg ); 00256 for( i = 4; i < BufferSize; i++ ) 00257 { 00258 Buffer[i] = i - 4; 00259 } 00260 wait_ms( 10 ); 00261 Radio.Send( Buffer, BufferSize ); 00262 } 00263 else 00264 { 00265 Radio.Rx( RX_TIMEOUT_VALUE ); 00266 } 00267 State = LOWPOWER; 00268 break; 00269 case RX_ERROR: 00270 // We have received a Packet with a CRC error, send reply as if packet was correct 00271 if( isMaster == true ) 00272 { 00273 // Send the next PING frame 00274 strcpy( ( char* )Buffer, ( char* )PingMsg ); 00275 for( i = 4; i < BufferSize; i++ ) 00276 { 00277 Buffer[i] = i - 4; 00278 } 00279 wait_ms( 10 ); 00280 Radio.Send( Buffer, BufferSize ); 00281 } 00282 else 00283 { 00284 // Send the next PONG frame 00285 strcpy( ( char* )Buffer, ( char* )PongMsg ); 00286 for( i = 4; i < BufferSize; i++ ) 00287 { 00288 Buffer[i] = i - 4; 00289 } 00290 wait_ms( 10 ); 00291 Radio.Send( Buffer, BufferSize ); 00292 } 00293 State = LOWPOWER; 00294 break; 00295 case TX_TIMEOUT: 00296 Radio.Rx( RX_TIMEOUT_VALUE ); 00297 State = LOWPOWER; 00298 break; 00299 case LOWPOWER: 00300 break; 00301 default: 00302 State = LOWPOWER; 00303 break; 00304 } 00305 } 00306 } 00307 00308 void OnTxDone( void ) 00309 { 00310 Radio.Sleep( ); 00311 State = TX; 00312 debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" ); 00313 } 00314 00315 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ) 00316 { 00317 Radio.Sleep( ); 00318 BufferSize = size; 00319 memcpy( Buffer, payload, BufferSize ); 00320 RssiValue = rssi; 00321 SnrValue = snr; 00322 State = RX; 00323 debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" ); 00324 } 00325 00326 void OnTxTimeout( void ) 00327 { 00328 Radio.Sleep( ); 00329 State = TX_TIMEOUT; 00330 debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" ); 00331 } 00332 00333 void OnRxTimeout( void ) 00334 { 00335 Radio.Sleep( ); 00336 Buffer[BufferSize] = 0; 00337 State = RX_TIMEOUT; 00338 debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" ); 00339 } 00340 00341 void OnRxError( void ) 00342 { 00343 Radio.Sleep( ); 00344 State = RX_ERROR; 00345 debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" ); 00346 }
Generated on Wed Jul 13 2022 10:07:57 by 1.7.2