Simple example Ping Pong from Semtech, updated new lib

Dependencies:   SX1276Lib mbed

Fork of SX1276PingPong by Semtech

i'm using stm32 nucleo l46rg and stm32 nucleo l073 with sx1276mb1mas.

Committer:
GregCr
Date:
Tue Aug 19 12:48:26 2014 +0000
Revision:
0:1ed39951ab7b
Child:
1:126d70d374f6
Simple Ping-Pong between two SX1276MB1xAS boards

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GregCr 0:1ed39951ab7b 1 #include "mbed.h"
GregCr 0:1ed39951ab7b 2 #include "sx1276-hal.h"
GregCr 0:1ed39951ab7b 3 #include "debug.h"
GregCr 0:1ed39951ab7b 4
GregCr 0:1ed39951ab7b 5 /* Set this flag to '1' to display debug messages on the console */
GregCr 0:1ed39951ab7b 6 #define DEBUG_MESSAGE 0
GregCr 0:1ed39951ab7b 7
GregCr 0:1ed39951ab7b 8
GregCr 0:1ed39951ab7b 9 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */
GregCr 0:1ed39951ab7b 10 #define USE_MODEM_LORA 0
GregCr 0:1ed39951ab7b 11 #define USE_MODEM_FSK !USE_MODEM_LORA
GregCr 0:1ed39951ab7b 12
GregCr 0:1ed39951ab7b 13 #define RF_FREQUENCY 869000000 // Hz
GregCr 0:1ed39951ab7b 14 #define TX_OUTPUT_POWER 14 // 14 dBm
GregCr 0:1ed39951ab7b 15
GregCr 0:1ed39951ab7b 16 #if USE_MODEM_LORA == 1
GregCr 0:1ed39951ab7b 17
GregCr 0:1ed39951ab7b 18 #define LORA_BANDWIDTH 0 // [0: 125 kHz,
GregCr 0:1ed39951ab7b 19 // 1: 250 kHz,
GregCr 0:1ed39951ab7b 20 // 2: 500 kHz,
GregCr 0:1ed39951ab7b 21 // 3: Reserved]
GregCr 0:1ed39951ab7b 22 #define LORA_SPREADING_FACTOR 7 // [SF7..SF12]
GregCr 0:1ed39951ab7b 23 #define LORA_CODINGRATE 1 // [1: 4/5,
GregCr 0:1ed39951ab7b 24 // 2: 4/6,
GregCr 0:1ed39951ab7b 25 // 3: 4/7,
GregCr 0:1ed39951ab7b 26 // 4: 4/8]
GregCr 0:1ed39951ab7b 27 #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
GregCr 0:1ed39951ab7b 28 #define LORA_SYMBOL_TIMEOUT 5 // Symbols
GregCr 0:1ed39951ab7b 29 #define LORA_FIX_LENGTH_PAYLOAD_ON false
GregCr 0:1ed39951ab7b 30 #define LORA_IQ_INVERSION_ON false
GregCr 0:1ed39951ab7b 31
GregCr 0:1ed39951ab7b 32 #elif USE_MODEM_FSK == 1
GregCr 0:1ed39951ab7b 33
GregCr 0:1ed39951ab7b 34 #define FSK_FDEV 25e3 // Hz
GregCr 0:1ed39951ab7b 35 #define FSK_DATARATE 50e3 // bps
GregCr 0:1ed39951ab7b 36 #define FSK_BANDWIDTH 50e3 // Hz
GregCr 0:1ed39951ab7b 37 #define FSK_AFC_BANDWIDTH 83.333e3 // Hz
GregCr 0:1ed39951ab7b 38 #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx
GregCr 0:1ed39951ab7b 39 #define FSK_FIX_LENGTH_PAYLOAD_ON false
GregCr 0:1ed39951ab7b 40
GregCr 0:1ed39951ab7b 41 #else
GregCr 0:1ed39951ab7b 42 #error "Please define a modem in the compiler options."
GregCr 0:1ed39951ab7b 43 #endif
GregCr 0:1ed39951ab7b 44
GregCr 0:1ed39951ab7b 45 #define RX_TIMEOUT_VALUE 3000000 // in us
GregCr 0:1ed39951ab7b 46 #define BUFFER_SIZE 32 // Define the payload size here
GregCr 0:1ed39951ab7b 47
GregCr 0:1ed39951ab7b 48 /*
GregCr 0:1ed39951ab7b 49 * Callback functions prototypes
GregCr 0:1ed39951ab7b 50 */
GregCr 0:1ed39951ab7b 51 /*!
GregCr 0:1ed39951ab7b 52 * @brief Function to be executed on Radio Tx Done event
GregCr 0:1ed39951ab7b 53 */
GregCr 0:1ed39951ab7b 54 void OnTxDone( void );
GregCr 0:1ed39951ab7b 55
GregCr 0:1ed39951ab7b 56 /*!
GregCr 0:1ed39951ab7b 57 * @brief Function to be executed on Radio Rx Done event
GregCr 0:1ed39951ab7b 58 */
GregCr 0:1ed39951ab7b 59 void OnRxDone( uint8_t *payload, uint16_t size, int8_t rssi, int8_t snr );
GregCr 0:1ed39951ab7b 60
GregCr 0:1ed39951ab7b 61 /*!
GregCr 0:1ed39951ab7b 62 * @brief Function executed on Radio Tx Timeout event
GregCr 0:1ed39951ab7b 63 */
GregCr 0:1ed39951ab7b 64 void OnTxTimeout( void );
GregCr 0:1ed39951ab7b 65
GregCr 0:1ed39951ab7b 66 /*!
GregCr 0:1ed39951ab7b 67 * @brief Function executed on Radio Rx Timeout event
GregCr 0:1ed39951ab7b 68 */
GregCr 0:1ed39951ab7b 69 void OnRxTimeout( void );
GregCr 0:1ed39951ab7b 70
GregCr 0:1ed39951ab7b 71 /*!
GregCr 0:1ed39951ab7b 72 * @brief Function executed on Radio Rx Error event
GregCr 0:1ed39951ab7b 73 */
GregCr 0:1ed39951ab7b 74 void OnRxError( void );
GregCr 0:1ed39951ab7b 75
GregCr 0:1ed39951ab7b 76 /*
GregCr 0:1ed39951ab7b 77 * Global variables declarations
GregCr 0:1ed39951ab7b 78 */
GregCr 0:1ed39951ab7b 79 typedef RadioState States_t;
GregCr 0:1ed39951ab7b 80
GregCr 0:1ed39951ab7b 81 /*
GregCr 0:1ed39951ab7b 82 * Global variables declarations
GregCr 0:1ed39951ab7b 83 */
GregCr 0:1ed39951ab7b 84 SX1276MB1xAS Radio( OnTxDone, OnTxTimeout, OnRxDone, OnRxTimeout, OnRxError );
GregCr 0:1ed39951ab7b 85
GregCr 0:1ed39951ab7b 86 const uint8_t PingMsg[] = "PING";
GregCr 0:1ed39951ab7b 87 const uint8_t PongMsg[] = "PONG";
GregCr 0:1ed39951ab7b 88
GregCr 0:1ed39951ab7b 89 uint16_t BufferSize = BUFFER_SIZE;
GregCr 0:1ed39951ab7b 90 uint8_t Buffer[BUFFER_SIZE];
GregCr 0:1ed39951ab7b 91
GregCr 0:1ed39951ab7b 92 volatile States_t State = LOWPOWER;
GregCr 0:1ed39951ab7b 93
GregCr 0:1ed39951ab7b 94 double RssiValue = 0.0;
GregCr 0:1ed39951ab7b 95 double SnrValue = 0.0;
GregCr 0:1ed39951ab7b 96
GregCr 0:1ed39951ab7b 97 DigitalOut Led( LED1 );
GregCr 0:1ed39951ab7b 98
GregCr 0:1ed39951ab7b 99 int main()
GregCr 0:1ed39951ab7b 100 {
GregCr 0:1ed39951ab7b 101 uint8_t i;
GregCr 0:1ed39951ab7b 102 bool isMaster = true;
GregCr 0:1ed39951ab7b 103
GregCr 0:1ed39951ab7b 104 debug("\n\r\n\r SX1276 Ping Pong Demo Application \n\r");
GregCr 0:1ed39951ab7b 105
GregCr 0:1ed39951ab7b 106 #if defined TARGET_NUCLEO_L152RE
GregCr 0:1ed39951ab7b 107 debug( DEBUG_MESSAGE, " > Nucleo-L152RE Platform <\r\n" );
GregCr 0:1ed39951ab7b 108 #elif defined TARGET_KL25Z
GregCr 0:1ed39951ab7b 109 debug( DEBUG_MESSAGE, " > KL25Z Platform <\r\n" );
GregCr 0:1ed39951ab7b 110 #elif defined TARGET_LPC11U6X
GregCr 0:1ed39951ab7b 111 debug( DEBUG_MESSAGE, " > LPC11U6X Platform <\r\n" );
GregCr 0:1ed39951ab7b 112 #else
GregCr 0:1ed39951ab7b 113 debug( DEBUG_MESSAGE, " > Untested Platform <\r\n" );
GregCr 0:1ed39951ab7b 114 #endif
GregCr 0:1ed39951ab7b 115
GregCr 0:1ed39951ab7b 116
GregCr 0:1ed39951ab7b 117 Radio.SetChannel( RF_FREQUENCY );
GregCr 0:1ed39951ab7b 118
GregCr 0:1ed39951ab7b 119 #if USE_MODEM_LORA == 1
GregCr 0:1ed39951ab7b 120
GregCr 0:1ed39951ab7b 121 debug("\n\r\n\r > LORA Mode < \n\r\n\r");
GregCr 0:1ed39951ab7b 122 Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
GregCr 0:1ed39951ab7b 123 LORA_SPREADING_FACTOR, LORA_CODINGRATE,
GregCr 0:1ed39951ab7b 124 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
GregCr 0:1ed39951ab7b 125 true, LORA_IQ_INVERSION_ON, 3000000 );
GregCr 0:1ed39951ab7b 126
GregCr 0:1ed39951ab7b 127 Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
GregCr 0:1ed39951ab7b 128 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
GregCr 0:1ed39951ab7b 129 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
GregCr 0:1ed39951ab7b 130 true, LORA_IQ_INVERSION_ON, true );
GregCr 0:1ed39951ab7b 131
GregCr 0:1ed39951ab7b 132 #elif USE_MODEM_FSK == 1
GregCr 0:1ed39951ab7b 133
GregCr 0:1ed39951ab7b 134 debug("\n\r\n\r > FSK Mode < \n\r\n\r");
GregCr 0:1ed39951ab7b 135 Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
GregCr 0:1ed39951ab7b 136 FSK_DATARATE, 0,
GregCr 0:1ed39951ab7b 137 FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
GregCr 0:1ed39951ab7b 138 true, 0, 3000000 );
GregCr 0:1ed39951ab7b 139
GregCr 0:1ed39951ab7b 140 Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
GregCr 0:1ed39951ab7b 141 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
GregCr 0:1ed39951ab7b 142 0, FSK_FIX_LENGTH_PAYLOAD_ON, true,
GregCr 0:1ed39951ab7b 143 false, true );
GregCr 0:1ed39951ab7b 144
GregCr 0:1ed39951ab7b 145 #else
GregCr 0:1ed39951ab7b 146
GregCr 0:1ed39951ab7b 147 #error "Please define a modem in the compiler options."
GregCr 0:1ed39951ab7b 148
GregCr 0:1ed39951ab7b 149 #endif
GregCr 0:1ed39951ab7b 150
GregCr 0:1ed39951ab7b 151 Radio.Rx( RX_TIMEOUT_VALUE );
GregCr 0:1ed39951ab7b 152
GregCr 0:1ed39951ab7b 153 while( 1 )
GregCr 0:1ed39951ab7b 154 {
GregCr 0:1ed39951ab7b 155 switch( State )
GregCr 0:1ed39951ab7b 156 {
GregCr 0:1ed39951ab7b 157 case RX:
GregCr 0:1ed39951ab7b 158 if( isMaster == true )
GregCr 0:1ed39951ab7b 159 {
GregCr 0:1ed39951ab7b 160 if( BufferSize > 0 )
GregCr 0:1ed39951ab7b 161 {
GregCr 0:1ed39951ab7b 162 if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 )
GregCr 0:1ed39951ab7b 163 {
GregCr 0:1ed39951ab7b 164 // Indicates on a LED that the received frame is a PONG
GregCr 0:1ed39951ab7b 165 Led = !Led;
GregCr 0:1ed39951ab7b 166 debug( "Pong...\r\n" );
GregCr 0:1ed39951ab7b 167 // Send the next PING frame
GregCr 0:1ed39951ab7b 168 Buffer[0] = 'P';
GregCr 0:1ed39951ab7b 169 Buffer[1] = 'I';
GregCr 0:1ed39951ab7b 170 Buffer[2] = 'N';
GregCr 0:1ed39951ab7b 171 Buffer[3] = 'G';
GregCr 0:1ed39951ab7b 172 // We fill the buffer with numbers for the payload
GregCr 0:1ed39951ab7b 173 for( i = 4; i < BufferSize; i++ )
GregCr 0:1ed39951ab7b 174 {
GregCr 0:1ed39951ab7b 175 Buffer[i] = i - 4;
GregCr 0:1ed39951ab7b 176 }
GregCr 0:1ed39951ab7b 177 wait_ms( 10 );
GregCr 0:1ed39951ab7b 178 Radio.Send( Buffer, BufferSize );
GregCr 0:1ed39951ab7b 179 }
GregCr 0:1ed39951ab7b 180 else if( strncmp( ( const char* )Buffer,
GregCr 0:1ed39951ab7b 181 ( const char* )PingMsg, 4 ) == 0 )
GregCr 0:1ed39951ab7b 182 { // A master already exists then become a slave
GregCr 0:1ed39951ab7b 183 isMaster = false;
GregCr 0:1ed39951ab7b 184 Radio.Rx( RX_TIMEOUT_VALUE );
GregCr 0:1ed39951ab7b 185 }
GregCr 0:1ed39951ab7b 186 }
GregCr 0:1ed39951ab7b 187 }
GregCr 0:1ed39951ab7b 188 else
GregCr 0:1ed39951ab7b 189 {
GregCr 0:1ed39951ab7b 190 if( BufferSize > 0 )
GregCr 0:1ed39951ab7b 191 {
GregCr 0:1ed39951ab7b 192 if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
GregCr 0:1ed39951ab7b 193 {
GregCr 0:1ed39951ab7b 194 // Indicates on a LED that the received frame is a PING
GregCr 0:1ed39951ab7b 195 Led = !Led;
GregCr 0:1ed39951ab7b 196 debug( "Ping...\r\n" );
GregCr 0:1ed39951ab7b 197 // Send the reply to the PONG string
GregCr 0:1ed39951ab7b 198 Buffer[0] = 'P';
GregCr 0:1ed39951ab7b 199 Buffer[1] = 'O';
GregCr 0:1ed39951ab7b 200 Buffer[2] = 'N';
GregCr 0:1ed39951ab7b 201 Buffer[3] = 'G';
GregCr 0:1ed39951ab7b 202 // We fill the buffer with numbers for the payload
GregCr 0:1ed39951ab7b 203 for( i = 4; i < BufferSize; i++ )
GregCr 0:1ed39951ab7b 204 {
GregCr 0:1ed39951ab7b 205 Buffer[i] = i - 4;
GregCr 0:1ed39951ab7b 206 }
GregCr 0:1ed39951ab7b 207 wait_ms( 10 );
GregCr 0:1ed39951ab7b 208 Radio.Send( Buffer, BufferSize );
GregCr 0:1ed39951ab7b 209 }
GregCr 0:1ed39951ab7b 210 }
GregCr 0:1ed39951ab7b 211 }
GregCr 0:1ed39951ab7b 212 State = LOWPOWER;
GregCr 0:1ed39951ab7b 213 break;
GregCr 0:1ed39951ab7b 214 case TX:
GregCr 0:1ed39951ab7b 215 // Indicates on a LED that we have sent a PING [Master]
GregCr 0:1ed39951ab7b 216 // Indicates on a LED that we have sent a PONG [Slave]
GregCr 0:1ed39951ab7b 217 Led = !Led;
GregCr 0:1ed39951ab7b 218 if ( isMaster )
GregCr 0:1ed39951ab7b 219 {
GregCr 0:1ed39951ab7b 220 debug("...Ping\r\n" );
GregCr 0:1ed39951ab7b 221 }
GregCr 0:1ed39951ab7b 222 else
GregCr 0:1ed39951ab7b 223 {
GregCr 0:1ed39951ab7b 224 debug("...Pong\r\n" );
GregCr 0:1ed39951ab7b 225 }
GregCr 0:1ed39951ab7b 226
GregCr 0:1ed39951ab7b 227 Radio.Rx( RX_TIMEOUT_VALUE );
GregCr 0:1ed39951ab7b 228 State = LOWPOWER;
GregCr 0:1ed39951ab7b 229 break;
GregCr 0:1ed39951ab7b 230 case RX_TIMEOUT:
GregCr 0:1ed39951ab7b 231 if( isMaster == true )
GregCr 0:1ed39951ab7b 232 {
GregCr 0:1ed39951ab7b 233 // Send the next PING frame
GregCr 0:1ed39951ab7b 234 Buffer[0] = 'P';
GregCr 0:1ed39951ab7b 235 Buffer[1] = 'I';
GregCr 0:1ed39951ab7b 236 Buffer[2] = 'N';
GregCr 0:1ed39951ab7b 237 Buffer[3] = 'G';
GregCr 0:1ed39951ab7b 238 for( i = 4; i < BufferSize; i++ )
GregCr 0:1ed39951ab7b 239 {
GregCr 0:1ed39951ab7b 240 Buffer[i] = i - 4;
GregCr 0:1ed39951ab7b 241 }
GregCr 0:1ed39951ab7b 242 wait_ms( 10 );
GregCr 0:1ed39951ab7b 243 Radio.Send( Buffer, BufferSize );
GregCr 0:1ed39951ab7b 244 }
GregCr 0:1ed39951ab7b 245 else
GregCr 0:1ed39951ab7b 246 {
GregCr 0:1ed39951ab7b 247 Radio.Rx( RX_TIMEOUT_VALUE );
GregCr 0:1ed39951ab7b 248 }
GregCr 0:1ed39951ab7b 249 State = LOWPOWER;
GregCr 0:1ed39951ab7b 250 break;
GregCr 0:1ed39951ab7b 251 case RX_ERROR:
GregCr 0:1ed39951ab7b 252 if( isMaster == true )
GregCr 0:1ed39951ab7b 253 {
GregCr 0:1ed39951ab7b 254 // Send the next PING frame
GregCr 0:1ed39951ab7b 255 Buffer[0] = 'P';
GregCr 0:1ed39951ab7b 256 Buffer[1] = 'I';
GregCr 0:1ed39951ab7b 257 Buffer[2] = 'N';
GregCr 0:1ed39951ab7b 258 Buffer[3] = 'G';
GregCr 0:1ed39951ab7b 259 for( i = 4; i < BufferSize; i++ )
GregCr 0:1ed39951ab7b 260 {
GregCr 0:1ed39951ab7b 261 Buffer[i] = i - 4;
GregCr 0:1ed39951ab7b 262 }
GregCr 0:1ed39951ab7b 263 wait_ms( 10 );
GregCr 0:1ed39951ab7b 264 Radio.Send( Buffer, BufferSize );
GregCr 0:1ed39951ab7b 265 }
GregCr 0:1ed39951ab7b 266 else
GregCr 0:1ed39951ab7b 267 {
GregCr 0:1ed39951ab7b 268 Radio.Rx( RX_TIMEOUT_VALUE );
GregCr 0:1ed39951ab7b 269 }
GregCr 0:1ed39951ab7b 270 State = LOWPOWER;
GregCr 0:1ed39951ab7b 271 break;
GregCr 0:1ed39951ab7b 272 case TX_TIMEOUT:
GregCr 0:1ed39951ab7b 273 Radio.Rx( RX_TIMEOUT_VALUE );
GregCr 0:1ed39951ab7b 274 State = LOWPOWER;
GregCr 0:1ed39951ab7b 275 break;
GregCr 0:1ed39951ab7b 276 case LOWPOWER:
GregCr 0:1ed39951ab7b 277 break;
GregCr 0:1ed39951ab7b 278 default:
GregCr 0:1ed39951ab7b 279 State = LOWPOWER;
GregCr 0:1ed39951ab7b 280 break;
GregCr 0:1ed39951ab7b 281 }
GregCr 0:1ed39951ab7b 282 }
GregCr 0:1ed39951ab7b 283 }
GregCr 0:1ed39951ab7b 284
GregCr 0:1ed39951ab7b 285 void OnTxDone( void )
GregCr 0:1ed39951ab7b 286 {
GregCr 0:1ed39951ab7b 287 debug( DEBUG_MESSAGE, ":OnTxDone\n\r" );
GregCr 0:1ed39951ab7b 288 State = TX;
GregCr 0:1ed39951ab7b 289 }
GregCr 0:1ed39951ab7b 290
GregCr 0:1ed39951ab7b 291 void OnRxDone( uint8_t *payload, uint16_t size, int8_t rssi, int8_t snr)
GregCr 0:1ed39951ab7b 292 {
GregCr 0:1ed39951ab7b 293 debug( DEBUG_MESSAGE, ":OnRxDone\n\r" );
GregCr 0:1ed39951ab7b 294 Radio.Sleep( );
GregCr 0:1ed39951ab7b 295 BufferSize = size;
GregCr 0:1ed39951ab7b 296 memcpy( Buffer, payload, BufferSize );
GregCr 0:1ed39951ab7b 297 RssiValue = rssi;
GregCr 0:1ed39951ab7b 298 SnrValue = snr;
GregCr 0:1ed39951ab7b 299 State = RX;
GregCr 0:1ed39951ab7b 300 }
GregCr 0:1ed39951ab7b 301
GregCr 0:1ed39951ab7b 302 void OnTxTimeout( void )
GregCr 0:1ed39951ab7b 303 {
GregCr 0:1ed39951ab7b 304 debug( DEBUG_MESSAGE, ":OnTxTimeout\n\r" );
GregCr 0:1ed39951ab7b 305 Radio.Sleep( );
GregCr 0:1ed39951ab7b 306 State = TX_TIMEOUT;
GregCr 0:1ed39951ab7b 307 }
GregCr 0:1ed39951ab7b 308
GregCr 0:1ed39951ab7b 309 void OnRxTimeout( void )
GregCr 0:1ed39951ab7b 310 {
GregCr 0:1ed39951ab7b 311 debug( DEBUG_MESSAGE, ":OnRxTimeout\n\r" );
GregCr 0:1ed39951ab7b 312 Radio.Sleep( );
GregCr 0:1ed39951ab7b 313 State = RX_TIMEOUT;
GregCr 0:1ed39951ab7b 314 Buffer[ BufferSize ] = 0;
GregCr 0:1ed39951ab7b 315 }
GregCr 0:1ed39951ab7b 316
GregCr 0:1ed39951ab7b 317 void OnRxError( void )
GregCr 0:1ed39951ab7b 318 {
GregCr 0:1ed39951ab7b 319 debug( DEBUG_MESSAGE, ":OnRxError\n\r" );
GregCr 0:1ed39951ab7b 320 Radio.Sleep( );
GregCr 0:1ed39951ab7b 321 State = RX_ERROR;
GregCr 0:1ed39951ab7b 322 }