Andrew Noll
/
SX1272PingPong_forNRF5182_updatedpins_119
SX1272 Ping Pong example for NRF51822
Fork of SX1272PingPong by
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "main.h" 00003 #include "sx1272-hal.h" 00004 #include "debug.h" 00005 00006 /* Set this flag to '1' to display debug messages on the console */ 00007 #define DEBUG_MESSAGE 1 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 868000000 // Hz 00014 #define TX_OUTPUT_POWER 14 // 14 dBm 00015 00016 #if USE_MODEM_LORA == 1 00017 00018 #define LORA_BANDWIDTH 2 // [0: 125 kHz, 00019 // 1: 250 kHz, 00020 // 2: 500 kHz, 00021 // 3: Reserved] 00022 #define LORA_SPREADING_FACTOR 7 // [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 false 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 3500 // in ms 00050 #define BUFFER_SIZE 32 // Define the payload size here 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 enum 00062 { 00063 LOWPOWER = 0, 00064 IDLE, 00065 00066 RX, 00067 RX_TIMEOUT, 00068 RX_ERROR, 00069 00070 TX, 00071 TX_TIMEOUT, 00072 00073 CAD, 00074 CAD_DONE 00075 }AppStates_t; 00076 00077 volatile AppStates_t State = LOWPOWER; 00078 00079 /*! 00080 * Radio events function pointer 00081 */ 00082 static RadioEvents_t RadioEvents; 00083 00084 /* 00085 * Global variables declarations 00086 */ 00087 SX1272MB2xAS Radio( NULL ); 00088 00089 const uint8_t PingMsg[] = "PING"; 00090 const uint8_t PongMsg[] = "PONG"; 00091 00092 uint16_t BufferSize = BUFFER_SIZE; 00093 uint8_t Buffer[BUFFER_SIZE]; 00094 00095 int16_t RssiValue = 0.0; 00096 int8_t SnrValue = 0.0; 00097 00098 int main( void ) 00099 { 00100 uint8_t i; 00101 bool isMaster = true; 00102 00103 debug( "\n\n\r SX1272 Ping Pong Demo Application \n\n\r" ); 00104 00105 // Initialize Radio driver 00106 RadioEvents.TxDone = OnTxDone; 00107 RadioEvents.RxDone = OnRxDone; 00108 RadioEvents.RxError = OnRxError; 00109 RadioEvents.TxTimeout = OnTxTimeout; 00110 RadioEvents.RxTimeout = OnRxTimeout; 00111 Radio.Init( &RadioEvents ); 00112 00113 // verify the connection with the board 00114 while( Radio.Read( REG_VERSION ) == 0x00 ) 00115 { 00116 debug( "Radio could not be detected!\n\r", NULL ); 00117 wait( 1 ); 00118 } 00119 00120 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ), "\n\r > Board Type: SX1272MB2xAS < \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 Sat Jul 23 2022 22:59:28 by 1.7.2