test
Dependencies: SX1276Lib1 WakeUp mbed-src
Fork of SX1276PingPong by
main.cpp
00001 #include "mbed.h" 00002 #include "main.h" 00003 #include "sx1276-hal.h" 00004 #include "WakeUp.h" 00005 //#include "debug.h" 00006 00007 /* Set this flag to '1' to display debug messages on the console */ 00008 #define DEBUG_MESSAGE 1 00009 00010 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ 00011 #define USE_MODEM_LORA 1 00012 #define USE_MODEM_FSK !USE_MODEM_LORA 00013 00014 #define RF_FREQUENCY 471900000 // Hz 00015 #define TX_OUTPUT_POWER 20 // 14 dBm 00016 00017 #if USE_MODEM_LORA == 1 00018 00019 #define LORA_BANDWIDTH 1 // [0: 125 kHz, 00020 // 1: 250 kHz, 00021 // 2: 500 kHz, 00022 // 3: Reserved] 00023 #define LORA_SPREADING_FACTOR 12 // [SF7..SF12] 00024 #define LORA_CODINGRATE 1 // [1: 4/5, 00025 // 2: 4/6, 00026 // 3: 4/7, 00027 // 4: 4/8] 00028 #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx 00029 #define LORA_SYMBOL_TIMEOUT 5 // Symbols 00030 #define LORA_FIX_LENGTH_PAYLOAD_ON false 00031 #define LORA_FHSS_ENABLED false 00032 #define LORA_NB_SYMB_HOP 4 00033 #define LORA_IQ_INVERSION_ON false 00034 #define LORA_CRC_ENABLED true 00035 00036 #elif USE_MODEM_FSK == 1 00037 00038 #define FSK_FDEV 25000 // HzY 00039 #define FSK_DATARATE 19200 // bps 00040 #define FSK_BANDWIDTH 50000 // Hz 00041 #define FSK_AFC_BANDWIDTH 83333 // Hz 00042 #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx 00043 #define FSK_FIX_LENGTH_PAYLOAD_ON false 00044 #define FSK_CRC_ENABLED true 00045 00046 #else 00047 #error "Please define a modem in the compiler options." 00048 #endif 00049 00050 #define RX_TIMEOUT_VALUE 3000000 // in us 00051 #define BUFFER_SIZE 32 // Define the payload size here 00052 00053 #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) 00054 DigitalOut led(LED2); 00055 #else 00056 DigitalOut led(LED1); 00057 #endif 00058 DigitalOut TXIO(PA_1); 00059 /* 00060 * Global variables declarations 00061 */ 00062 typedef enum { 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 00076 } AppStates_t; 00077 00078 volatile AppStates_t State = LOWPOWER; 00079 00080 /*! 00081 * Radio events function pointer 00082 */ 00083 static RadioEvents_t RadioEvents; 00084 00085 /* 00086 * Global variables declarations 00087 */ 00088 SX1276MB1xAS Radio( NULL ); 00089 00090 const uint8_t PingMsg[] = "PING"; 00091 const uint8_t PongMsg[] = "PONG"; 00092 00093 uint16_t BufferSize = BUFFER_SIZE; 00094 uint8_t Buffer[BUFFER_SIZE]; 00095 int16_t RssiValue = 0.0; 00096 int8_t SnrValue = 0.0; 00097 Serial pc(PB_6, PB_7); 00098 void srprintf(uint8_t *load,uint8_t size) 00099 { 00100 pc.printf(":"); 00101 for(int i=0; i<size; i++) { 00102 pc.printf("%x ",load[i]); 00103 } 00104 // pc.printf("size:%drssi:%dsnr:%d\r\n",size,rssi,snr); 00105 } 00106 int main() 00107 { 00108 uint8_t i; 00109 bool isMaster = true; 00110 pc.printf("Hello World !\n"); 00111 //debug( "\n\n\r SX1276 Ping Pong Demo Application \n\n\r" ); 00112 // Initialize Radio driver 00113 RadioEvents.TxDone = OnTxDone; 00114 RadioEvents.RxDone = OnRxDone; 00115 RadioEvents.RxError = OnRxError; 00116 RadioEvents.TxTimeout = OnTxTimeout; 00117 RadioEvents.RxTimeout = OnRxTimeout; 00118 Radio.Init( &RadioEvents ); 00119 00120 // verify the connection with the board 00121 while( Radio.Read( REG_VERSION ) == 0x00 ) { 00122 // debug( "Radio could not be detected!\n\r", NULL ); 00123 pc.printf("."); 00124 wait( 1 ); 00125 } 00126 TXIO = 1; 00127 // debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ) , "\n\r > Board Type: SX1276MB1LAS < \n\r" ); 00128 // debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ) , "\n\r > Board Type: SX1276MB1MAS < \n\r" ); 00129 00130 Radio.SetChannel( RF_FREQUENCY ); 00131 pc.printf("VERSION:%x\r\n",Radio.Read( REG_FRFMSB )); 00132 pc.printf("VERSION:%x\r\n",Radio.Read( REG_FRFMID )); 00133 #if USE_MODEM_LORA == 1 00134 00135 // debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r"); 00136 // debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r"); 00137 00138 Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, 00139 LORA_SPREADING_FACTOR, LORA_CODINGRATE, 00140 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, 00141 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 00142 LORA_IQ_INVERSION_ON, 2000000 ); 00143 00144 Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, 00145 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, 00146 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, 00147 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 00148 LORA_IQ_INVERSION_ON, true ); 00149 00150 #elif USE_MODEM_FSK == 1 00151 00152 // debug("\n\n\r > FSK Mode < \n\n\r"); 00153 Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, 00154 FSK_DATARATE, 0, 00155 FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, 00156 FSK_CRC_ENABLED, 0, 0, 0, 2000000 ); 00157 00158 Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, 00159 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, 00160 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, 00161 0, 0, false, false ); 00162 00163 #else 00164 00165 #error "Please define a modem in the compiler options." 00166 00167 #endif 00168 00169 pc.printf("VERSION:%x\r\n",Radio.Read( REG_LR_MODEMCONFIG1 )); 00170 pc.printf("VERSION:%x\r\n",Radio.Read( REG_LR_MODEMCONFIG2 )); 00171 // debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" ); 00172 00173 led = 0; 00174 // WakeUp::calibrate(); 00175 Radio.Rx( RX_TIMEOUT_VALUE ); 00176 while( 0 ) { 00177 // WakeUp::set_ms(6000); 00178 // deepsleep(); 00179 switch( State ) { 00180 case RX: 00181 // debugs((char* )Buffer,sizeof(Buffer)); 00182 // debug( "rssi:%d dbm\r\n",Radio.GetRssi( MODEM_LORA )); 00183 // debug( "\r\n" ); 00184 if( isMaster == true ) { 00185 if( BufferSize > 0 ) { 00186 if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 ) { 00187 led = !led; 00188 // debug( "...Pong\r\n" ); 00189 pc.printf("...Pong...\r\n"); 00190 // Send the next PING frame 00191 strcpy( ( char* )Buffer, ( char* )PingMsg ); 00192 // We fill the buffer with numbers for the payload 00193 for( i = 4; i < BufferSize; i++ ) { 00194 Buffer[i] = i - 4; 00195 } 00196 wait_ms( 10 ); 00197 Radio.Send( Buffer, BufferSize ); 00198 } else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) { 00199 // A master already exists then become a slave 00200 // debug( "...Ping\r\n" ); 00201 pc.printf("...Ping...\r\n"); 00202 led = !led; 00203 isMaster = false; 00204 // Send th e next PONG frame 00205 strcpy( ( char* )Buffer, ( char* )PongMsg ); 00206 // We fill the buffer with numbers for the payload 00207 for( i = 4; i < BufferSize; i++ ) { 00208 Buffer[i] = i - 4; 00209 } 00210 wait_ms( 10 ); 00211 Radio.Send( Buffer, BufferSize ); 00212 } else { // valid reception but neither a PING or a PONG message 00213 // Set device as master ans start again 00214 isMaster = true; 00215 Radio.Rx( RX_TIMEOUT_VALUE ); 00216 } 00217 } 00218 } else { 00219 if( BufferSize > 0 ) { 00220 if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) { 00221 led = !led; 00222 //debug( "...Ping\r\n" ); 00223 pc.printf("...Ping...\r\n"); 00224 // Send the reply to the PING string 00225 strcpy( ( char* )Buffer, ( char* )PongMsg ); 00226 // We fill the buffer with numbers for the payload 00227 for( i = 4; i < BufferSize; i++ ) { 00228 Buffer[i] = i - 4; 00229 } 00230 wait_ms( 10 ); 00231 Radio.Send( Buffer, BufferSize ); 00232 } else { // valid reception but not a PING as expected 00233 // Set device as master and start again 00234 isMaster = true; 00235 Radio.Rx( RX_TIMEOUT_VALUE ); 00236 } 00237 } 00238 } 00239 State = LOWPOWER; 00240 break; 00241 case TX: 00242 led = !led; 00243 if( isMaster == true ) { 00244 // debug( "Ping...\r\n" ); 00245 pc.printf("Ping...\r\n"); 00246 } else { 00247 // debug( "Pong...\r\n" ); 00248 pc.printf("Pong...\r\n"); 00249 } 00250 Radio.Rx( RX_TIMEOUT_VALUE ); 00251 State = LOWPOWER; 00252 break; 00253 case RX_TIMEOUT: 00254 if( isMaster == true ) { 00255 // Send the next PING frame 00256 strcpy( ( char* )Buffer, ( char* )PingMsg ); 00257 for( i = 4; i < BufferSize; i++ ) { 00258 Buffer[i] = i - 4; 00259 } 00260 wait_ms( 10 ); 00261 Radio.Send( Buffer, BufferSize ); 00262 } else { 00263 Radio.Rx( RX_TIMEOUT_VALUE ); 00264 } 00265 State = LOWPOWER; 00266 break; 00267 case RX_ERROR: 00268 // We have received a Packet with a CRC error, send reply as if packet was correct 00269 if( isMaster == true ) { 00270 // Send the next PING frame 00271 strcpy( ( char* )Buffer, ( char* )PingMsg ); 00272 for( i = 4; i < BufferSize; i++ ) { 00273 Buffer[i] = i - 4; 00274 } 00275 wait_ms( 10 ); 00276 Radio.Send( Buffer, BufferSize ); 00277 } else { 00278 // Send the next PONG frame 00279 strcpy( ( char* )Buffer, ( char* )PongMsg ); 00280 for( i = 4; i < BufferSize; i++ ) { 00281 Buffer[i] = i - 4; 00282 } 00283 wait_ms( 10 ); 00284 Radio.Send( Buffer, BufferSize ); 00285 } 00286 State = LOWPOWER; 00287 break; 00288 case TX_TIMEOUT: 00289 Radio.Rx( RX_TIMEOUT_VALUE ); 00290 State = LOWPOWER; 00291 break; 00292 case LOWPOWER: 00293 break; 00294 default: 00295 State = LOWPOWER; 00296 break; 00297 } 00298 } 00299 // while(1); 00300 } 00301 00302 void OnTxDone( void ) 00303 { 00304 Radio.Sleep( ); 00305 State = TX; 00306 // debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" ); 00307 pc.printf("> OnTxDone\r\n"); 00308 00309 } 00310 00311 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) 00312 { 00313 Radio.Sleep( ); 00314 BufferSize = size; 00315 memcpy( Buffer, payload, BufferSize ); 00316 RssiValue = rssi; 00317 SnrValue = snr; 00318 State = RX; 00319 // debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" ); 00320 pc.printf("> OnRxDone\r\n"); 00321 pc.printf("size:%d rssi:%d snr:%d",size,rssi,snr); 00322 srprintf(payload,sizeof(payload)); 00323 Radio.Rx( RX_TIMEOUT_VALUE ); 00324 } 00325 00326 void OnTxTimeout( void ) 00327 { 00328 Radio.Sleep( ); 00329 State = TX_TIMEOUT; 00330 // debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" ); 00331 pc.printf("> OnTxTimeout\r\n"); 00332 Radio.Rx( RX_TIMEOUT_VALUE ); 00333 } 00334 00335 void OnRxTimeout( void ) 00336 { 00337 Radio.Sleep( ); 00338 Buffer[ BufferSize ] = 0; 00339 State = RX_TIMEOUT; 00340 // debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" ); 00341 pc.printf("> OnRxTimeout\r\n"); 00342 Radio.Rx( RX_TIMEOUT_VALUE ); 00343 } 00344 00345 void OnRxError( void ) 00346 { 00347 Radio.Sleep( ); 00348 State = RX_ERROR; 00349 // debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" ); 00350 pc.printf("> OnRxError\r\n"); 00351 Radio.Rx( RX_TIMEOUT_VALUE ); 00352 } 00353
Generated on Fri Jul 15 2022 05:07:30 by 1.7.2