Gregory Cristian
/
SX1276PingPongFHSS
SX1276 Ping Pong FHSS 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 #include "debug.h" 00005 00006 /* Set this flag to '1' to display debug messages on the console */ 00007 #define DEBUG_MESSAGE 0 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 915000000 // Hz 00014 #define TX_OUTPUT_POWER 14 // 14 dBm 00015 00016 #if USE_MODEM_LORA == 1 00017 00018 #define LORA_BANDWIDTH 1 // [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 8 // 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 true 00031 #define LORA_NB_SYMB_HOP 4 00032 #define LORA_IQ_INVERSION_ON false 00033 #define LORA_CRC_ENABLED true 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 true 00044 00045 #else 00046 #error "Please define a modem in the compiler options." 00047 #endif 00048 00049 #define RX_TIMEOUT_VALUE 3500000 // in us 00050 #define BUFFER_SIZE 32 // Define the payload size here [min:1 max:255] 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 RadioState States_t; 00062 volatile States_t State = LOWPOWER; 00063 00064 /* 00065 * Global variables declarations 00066 */ 00067 SX1276MB1xAS Radio( OnTxDone, OnTxTimeout, OnRxDone, OnRxTimeout, OnRxError, OnFhssChangeChannel, NULL ); 00068 00069 const uint8_t PingMsg[] = "PING"; 00070 const uint8_t PongMsg[] = "PONG"; 00071 00072 uint16_t BufferSize = BUFFER_SIZE; 00073 uint8_t Buffer[BUFFER_SIZE]; 00074 00075 int16_t RssiValue = 0.0; 00076 int8_t SnrValue = 0.0; 00077 00078 int main() 00079 { 00080 uint8_t i; 00081 bool isMaster = true; 00082 00083 debug( "\n\n\r SX1276 Ping Pong Demo Application \n\n\r" ); 00084 00085 // verify the connection with the board 00086 while( Radio.Read( REG_VERSION ) == 0x00 ) 00087 { 00088 debug( "Radio could not be detected!\n\r", NULL ); 00089 wait( 1 ); 00090 } 00091 00092 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ) , "\n\r > Board Type: SX1276MB1LAS < \n\r" ); 00093 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ) , "\n\r > Board Type: SX1276MB1MAS < \n\r" ); 00094 00095 Radio.SetChannel( HoppingFrequencies[0] ); 00096 00097 #if USE_MODEM_LORA == 1 00098 00099 debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r"); 00100 debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r"); 00101 00102 Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, 00103 LORA_SPREADING_FACTOR, LORA_CODINGRATE, 00104 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, 00105 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 00106 LORA_IQ_INVERSION_ON, 4000000 ); 00107 00108 Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, 00109 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, 00110 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, 00111 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 00112 LORA_IQ_INVERSION_ON, true ); 00113 00114 #elif USE_MODEM_FSK == 1 00115 00116 debug("\n\n\r > FSK Mode < \n\n\r"); 00117 Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, 00118 FSK_DATARATE, 0, 00119 FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, 00120 FSK_CRC_ENABLED, 0, 0, 0, 3000000 ); 00121 00122 Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, 00123 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, 00124 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, 00125 0, 0, false, true ); 00126 00127 #else 00128 00129 #error "Please define a modem in the compiler options." 00130 00131 #endif 00132 00133 debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" ); 00134 00135 led = 0; 00136 00137 Radio.Rx( RX_TIMEOUT_VALUE ); 00138 00139 while( 1 ) 00140 { 00141 switch( State ) 00142 { 00143 case RX: 00144 if( isMaster == true ) 00145 { 00146 if( BufferSize > 0 ) 00147 { 00148 if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 ) 00149 { 00150 led = !led; 00151 debug( "...Pong\r\n" ); 00152 // Send the next PING frame 00153 strcpy( ( char* )Buffer, ( char* )PingMsg ); 00154 // We fill the buffer with numbers for the payload 00155 for( i = 4; i < BufferSize; i++ ) 00156 { 00157 Buffer[i] = i - 4; 00158 } 00159 wait_ms( 10 ); 00160 Radio.Send( Buffer, BufferSize ); 00161 } 00162 else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) 00163 { // A master already exists then become a slave 00164 debug( "...Ping\r\n" ); 00165 led = !led; 00166 isMaster = false; 00167 // Send the next PONG frame 00168 strcpy( ( char* )Buffer, ( char* )PongMsg ); 00169 // We fill the buffer with numbers for the payload 00170 for( i = 4; i < BufferSize; i++ ) 00171 { 00172 Buffer[i] = i - 4; 00173 } 00174 wait_ms( 10 ); 00175 Radio.Send( Buffer, BufferSize ); 00176 } 00177 else // valid reception but neither a PING or a PONG message 00178 { // Set device as master ans start again 00179 isMaster = true; 00180 Radio.Rx( RX_TIMEOUT_VALUE ); 00181 } 00182 } 00183 } 00184 else 00185 { 00186 if( BufferSize > 0 ) 00187 { 00188 if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) 00189 { 00190 led = !led; 00191 debug( "...Ping\r\n" ); 00192 // Send the reply to the PING string 00193 strcpy( ( char* )Buffer, ( char* )PongMsg ); 00194 // We fill the buffer with numbers for the payload 00195 for( i = 4; i < BufferSize; i++ ) 00196 { 00197 Buffer[i] = i - 4; 00198 } 00199 wait_ms( 10 ); 00200 Radio.Send( Buffer, BufferSize ); 00201 } 00202 else // valid reception but not a PING as expected 00203 { // Set device as master and start again 00204 isMaster = true; 00205 Radio.Rx( RX_TIMEOUT_VALUE ); 00206 } 00207 } 00208 } 00209 State = LOWPOWER; 00210 break; 00211 case TX: 00212 led = !led; 00213 if( isMaster == true ) 00214 { 00215 debug( "Ping...\r\n" ); 00216 } 00217 else 00218 { 00219 debug( "Pong...\r\n" ); 00220 } 00221 Radio.Rx( RX_TIMEOUT_VALUE ); 00222 State = LOWPOWER; 00223 break; 00224 case RX_TIMEOUT: 00225 if( isMaster == true ) 00226 { 00227 // Send the next PING frame 00228 strcpy( ( char* )Buffer, ( char* )PingMsg ); 00229 for( i = 4; i < BufferSize; i++ ) 00230 { 00231 Buffer[i] = i - 4; 00232 } 00233 wait_ms( 10 ); 00234 Radio.Send( Buffer, BufferSize ); 00235 } 00236 else 00237 { 00238 Radio.Rx( RX_TIMEOUT_VALUE ); 00239 } 00240 State = LOWPOWER; 00241 break; 00242 case RX_ERROR: 00243 // We have received a Packet with a CRC error, send reply as if packet was correct 00244 if( isMaster == true ) 00245 { 00246 // Send the next PING frame 00247 strcpy( ( char* )Buffer, ( char* )PingMsg ); 00248 for( i = 4; i < BufferSize; i++ ) 00249 { 00250 Buffer[i] = i - 4; 00251 } 00252 wait_ms( 10 ); 00253 Radio.Send( Buffer, BufferSize ); 00254 } 00255 else 00256 { 00257 // Send the next PONG frame 00258 strcpy( ( char* )Buffer, ( char* )PongMsg ); 00259 for( i = 4; i < BufferSize; i++ ) 00260 { 00261 Buffer[i] = i - 4; 00262 } 00263 wait_ms( 10 ); 00264 Radio.Send( Buffer, BufferSize ); 00265 } 00266 State = LOWPOWER; 00267 break; 00268 case TX_TIMEOUT: 00269 Radio.Rx( RX_TIMEOUT_VALUE ); 00270 State = LOWPOWER; 00271 break; 00272 case LOWPOWER: 00273 break; 00274 default: 00275 State = LOWPOWER; 00276 break; 00277 } 00278 } 00279 } 00280 00281 void OnTxDone( void ) 00282 { 00283 Radio.SetChannel( HoppingFrequencies[0] ); 00284 Radio.Sleep( ); 00285 State = TX; 00286 debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" ); 00287 } 00288 00289 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) 00290 { 00291 Radio.SetChannel( HoppingFrequencies[0] ); 00292 Radio.Sleep( ); 00293 BufferSize = size; 00294 memcpy( Buffer, payload, BufferSize ); 00295 RssiValue = rssi; 00296 SnrValue = snr; 00297 State = RX; 00298 debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" ); 00299 } 00300 00301 void OnTxTimeout( void ) 00302 { 00303 Radio.SetChannel( HoppingFrequencies[0] ); 00304 Radio.Sleep( ); 00305 State = TX_TIMEOUT; 00306 debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" ); 00307 } 00308 00309 void OnRxTimeout( void ) 00310 { 00311 Radio.SetChannel( HoppingFrequencies[0] ); 00312 Radio.Sleep( ); 00313 Buffer[ BufferSize ] = 0; 00314 State = RX_TIMEOUT; 00315 debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" ); 00316 } 00317 00318 void OnRxError( void ) 00319 { 00320 Radio.SetChannel( HoppingFrequencies[0] ); 00321 Radio.Sleep( ); 00322 State = RX_ERROR; 00323 debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" ); 00324 } 00325 00326 void OnFhssChangeChannel( uint8_t channelIndex ) 00327 { 00328 Radio.SetChannel( HoppingFrequencies[channelIndex] ); 00329 debug_if( DEBUG_MESSAGE, "F%d-", channelIndex); 00330 }
Generated on Fri Aug 19 2022 16:52:46 by 1.7.2