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