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:
Wed Aug 20 06:29:39 2014 +0000
Revision:
1:126d70d374f6
Parent:
0:1ed39951ab7b
Child:
2:59e108728d71
Release Candidate

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 1:126d70d374f6 10 #define USE_MODEM_LORA 1
GregCr 0:1ed39951ab7b 11 #define USE_MODEM_FSK !USE_MODEM_LORA
GregCr 0:1ed39951ab7b 12
GregCr 1:126d70d374f6 13 #define RF_FREQUENCY 868000000 // 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 1:126d70d374f6 18 #define LORA_BANDWIDTH 2 // [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 1:126d70d374f6 35 #define FSK_DATARATE 9600 // 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 int main()
GregCr 0:1ed39951ab7b 98 {
GregCr 0:1ed39951ab7b 99 uint8_t i;
GregCr 0:1ed39951ab7b 100 bool isMaster = true;
GregCr 0:1ed39951ab7b 101
GregCr 1:126d70d374f6 102 debug( "\n\r\n\r SX1276 Ping Pong Demo Application \n\r" );
GregCr 0:1ed39951ab7b 103
GregCr 0:1ed39951ab7b 104 #if defined TARGET_NUCLEO_L152RE
GregCr 0:1ed39951ab7b 105 debug( DEBUG_MESSAGE, " > Nucleo-L152RE Platform <\r\n" );
GregCr 0:1ed39951ab7b 106 #elif defined TARGET_KL25Z
GregCr 0:1ed39951ab7b 107 debug( DEBUG_MESSAGE, " > KL25Z Platform <\r\n" );
GregCr 0:1ed39951ab7b 108 #elif defined TARGET_LPC11U6X
GregCr 0:1ed39951ab7b 109 debug( DEBUG_MESSAGE, " > LPC11U6X Platform <\r\n" );
GregCr 0:1ed39951ab7b 110 #else
GregCr 0:1ed39951ab7b 111 debug( DEBUG_MESSAGE, " > Untested Platform <\r\n" );
GregCr 0:1ed39951ab7b 112 #endif
GregCr 0:1ed39951ab7b 113
GregCr 1:126d70d374f6 114 debug( DEBUG_MESSAGE, "SX1276 Chipset Version = 0x%x \n\r", Radio.Read( REG_VERSION ) );
GregCr 1:126d70d374f6 115
GregCr 0:1ed39951ab7b 116 Radio.SetChannel( RF_FREQUENCY );
GregCr 0:1ed39951ab7b 117
GregCr 0:1ed39951ab7b 118 #if USE_MODEM_LORA == 1
GregCr 0:1ed39951ab7b 119
GregCr 0:1ed39951ab7b 120 debug("\n\r\n\r > LORA Mode < \n\r\n\r");
GregCr 0:1ed39951ab7b 121 Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
GregCr 0:1ed39951ab7b 122 LORA_SPREADING_FACTOR, LORA_CODINGRATE,
GregCr 0:1ed39951ab7b 123 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
GregCr 0:1ed39951ab7b 124 true, LORA_IQ_INVERSION_ON, 3000000 );
GregCr 0:1ed39951ab7b 125
GregCr 0:1ed39951ab7b 126 Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
GregCr 0:1ed39951ab7b 127 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
GregCr 0:1ed39951ab7b 128 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
GregCr 0:1ed39951ab7b 129 true, LORA_IQ_INVERSION_ON, true );
GregCr 0:1ed39951ab7b 130
GregCr 0:1ed39951ab7b 131 #elif USE_MODEM_FSK == 1
GregCr 0:1ed39951ab7b 132
GregCr 0:1ed39951ab7b 133 debug("\n\r\n\r > FSK Mode < \n\r\n\r");
GregCr 0:1ed39951ab7b 134 Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
GregCr 0:1ed39951ab7b 135 FSK_DATARATE, 0,
GregCr 0:1ed39951ab7b 136 FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
GregCr 0:1ed39951ab7b 137 true, 0, 3000000 );
GregCr 0:1ed39951ab7b 138
GregCr 0:1ed39951ab7b 139 Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
GregCr 0:1ed39951ab7b 140 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
GregCr 0:1ed39951ab7b 141 0, FSK_FIX_LENGTH_PAYLOAD_ON, true,
GregCr 0:1ed39951ab7b 142 false, true );
GregCr 0:1ed39951ab7b 143
GregCr 0:1ed39951ab7b 144 #else
GregCr 0:1ed39951ab7b 145
GregCr 0:1ed39951ab7b 146 #error "Please define a modem in the compiler options."
GregCr 0:1ed39951ab7b 147
GregCr 0:1ed39951ab7b 148 #endif
GregCr 1:126d70d374f6 149
GregCr 1:126d70d374f6 150 debug( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" );
GregCr 0:1ed39951ab7b 151
GregCr 0:1ed39951ab7b 152 Radio.Rx( RX_TIMEOUT_VALUE );
GregCr 0:1ed39951ab7b 153
GregCr 0:1ed39951ab7b 154 while( 1 )
GregCr 0:1ed39951ab7b 155 {
GregCr 0:1ed39951ab7b 156 switch( State )
GregCr 0:1ed39951ab7b 157 {
GregCr 0:1ed39951ab7b 158 case RX:
GregCr 0:1ed39951ab7b 159 if( isMaster == true )
GregCr 0:1ed39951ab7b 160 {
GregCr 0:1ed39951ab7b 161 if( BufferSize > 0 )
GregCr 0:1ed39951ab7b 162 {
GregCr 0:1ed39951ab7b 163 if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 )
GregCr 0:1ed39951ab7b 164 {
GregCr 0:1ed39951ab7b 165 debug( "Pong...\r\n" );
GregCr 0:1ed39951ab7b 166 // Send the next PING frame
GregCr 0:1ed39951ab7b 167 Buffer[0] = 'P';
GregCr 0:1ed39951ab7b 168 Buffer[1] = 'I';
GregCr 0:1ed39951ab7b 169 Buffer[2] = 'N';
GregCr 0:1ed39951ab7b 170 Buffer[3] = 'G';
GregCr 0:1ed39951ab7b 171 // We fill the buffer with numbers for the payload
GregCr 0:1ed39951ab7b 172 for( i = 4; i < BufferSize; i++ )
GregCr 0:1ed39951ab7b 173 {
GregCr 0:1ed39951ab7b 174 Buffer[i] = i - 4;
GregCr 0:1ed39951ab7b 175 }
GregCr 0:1ed39951ab7b 176 wait_ms( 10 );
GregCr 0:1ed39951ab7b 177 Radio.Send( Buffer, BufferSize );
GregCr 0:1ed39951ab7b 178 }
GregCr 0:1ed39951ab7b 179 else if( strncmp( ( const char* )Buffer,
GregCr 0:1ed39951ab7b 180 ( const char* )PingMsg, 4 ) == 0 )
GregCr 0:1ed39951ab7b 181 { // A master already exists then become a slave
GregCr 0:1ed39951ab7b 182 isMaster = false;
GregCr 0:1ed39951ab7b 183 Radio.Rx( RX_TIMEOUT_VALUE );
GregCr 0:1ed39951ab7b 184 }
GregCr 0:1ed39951ab7b 185 }
GregCr 0:1ed39951ab7b 186 }
GregCr 0:1ed39951ab7b 187 else
GregCr 0:1ed39951ab7b 188 {
GregCr 0:1ed39951ab7b 189 if( BufferSize > 0 )
GregCr 0:1ed39951ab7b 190 {
GregCr 0:1ed39951ab7b 191 if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
GregCr 0:1ed39951ab7b 192 {
GregCr 0:1ed39951ab7b 193 debug( "Ping...\r\n" );
GregCr 0:1ed39951ab7b 194 // Send the reply to the PONG string
GregCr 0:1ed39951ab7b 195 Buffer[0] = 'P';
GregCr 0:1ed39951ab7b 196 Buffer[1] = 'O';
GregCr 0:1ed39951ab7b 197 Buffer[2] = 'N';
GregCr 0:1ed39951ab7b 198 Buffer[3] = 'G';
GregCr 0:1ed39951ab7b 199 // We fill the buffer with numbers for the payload
GregCr 0:1ed39951ab7b 200 for( i = 4; i < BufferSize; i++ )
GregCr 0:1ed39951ab7b 201 {
GregCr 0:1ed39951ab7b 202 Buffer[i] = i - 4;
GregCr 0:1ed39951ab7b 203 }
GregCr 0:1ed39951ab7b 204 wait_ms( 10 );
GregCr 0:1ed39951ab7b 205 Radio.Send( Buffer, BufferSize );
GregCr 0:1ed39951ab7b 206 }
GregCr 0:1ed39951ab7b 207 }
GregCr 0:1ed39951ab7b 208 }
GregCr 0:1ed39951ab7b 209 State = LOWPOWER;
GregCr 0:1ed39951ab7b 210 break;
GregCr 0:1ed39951ab7b 211 case TX:
GregCr 0:1ed39951ab7b 212 if ( isMaster )
GregCr 0:1ed39951ab7b 213 {
GregCr 0:1ed39951ab7b 214 debug("...Ping\r\n" );
GregCr 0:1ed39951ab7b 215 }
GregCr 0:1ed39951ab7b 216 else
GregCr 0:1ed39951ab7b 217 {
GregCr 0:1ed39951ab7b 218 debug("...Pong\r\n" );
GregCr 0:1ed39951ab7b 219 }
GregCr 0:1ed39951ab7b 220
GregCr 0:1ed39951ab7b 221 Radio.Rx( RX_TIMEOUT_VALUE );
GregCr 0:1ed39951ab7b 222 State = LOWPOWER;
GregCr 0:1ed39951ab7b 223 break;
GregCr 0:1ed39951ab7b 224 case RX_TIMEOUT:
GregCr 0:1ed39951ab7b 225 if( isMaster == true )
GregCr 0:1ed39951ab7b 226 {
GregCr 0:1ed39951ab7b 227 // Send the next PING frame
GregCr 0:1ed39951ab7b 228 Buffer[0] = 'P';
GregCr 0:1ed39951ab7b 229 Buffer[1] = 'I';
GregCr 0:1ed39951ab7b 230 Buffer[2] = 'N';
GregCr 0:1ed39951ab7b 231 Buffer[3] = 'G';
GregCr 0:1ed39951ab7b 232 for( i = 4; i < BufferSize; i++ )
GregCr 0:1ed39951ab7b 233 {
GregCr 0:1ed39951ab7b 234 Buffer[i] = i - 4;
GregCr 0:1ed39951ab7b 235 }
GregCr 0:1ed39951ab7b 236 wait_ms( 10 );
GregCr 0:1ed39951ab7b 237 Radio.Send( Buffer, BufferSize );
GregCr 0:1ed39951ab7b 238 }
GregCr 0:1ed39951ab7b 239 else
GregCr 0:1ed39951ab7b 240 {
GregCr 0:1ed39951ab7b 241 Radio.Rx( RX_TIMEOUT_VALUE );
GregCr 0:1ed39951ab7b 242 }
GregCr 0:1ed39951ab7b 243 State = LOWPOWER;
GregCr 0:1ed39951ab7b 244 break;
GregCr 0:1ed39951ab7b 245 case RX_ERROR:
GregCr 0:1ed39951ab7b 246 if( isMaster == true )
GregCr 0:1ed39951ab7b 247 {
GregCr 0:1ed39951ab7b 248 // Send the next PING frame
GregCr 0:1ed39951ab7b 249 Buffer[0] = 'P';
GregCr 0:1ed39951ab7b 250 Buffer[1] = 'I';
GregCr 0:1ed39951ab7b 251 Buffer[2] = 'N';
GregCr 0:1ed39951ab7b 252 Buffer[3] = 'G';
GregCr 0:1ed39951ab7b 253 for( i = 4; i < BufferSize; i++ )
GregCr 0:1ed39951ab7b 254 {
GregCr 0:1ed39951ab7b 255 Buffer[i] = i - 4;
GregCr 0:1ed39951ab7b 256 }
GregCr 0:1ed39951ab7b 257 wait_ms( 10 );
GregCr 0:1ed39951ab7b 258 Radio.Send( Buffer, BufferSize );
GregCr 0:1ed39951ab7b 259 }
GregCr 0:1ed39951ab7b 260 else
GregCr 0:1ed39951ab7b 261 {
GregCr 0:1ed39951ab7b 262 Radio.Rx( RX_TIMEOUT_VALUE );
GregCr 0:1ed39951ab7b 263 }
GregCr 0:1ed39951ab7b 264 State = LOWPOWER;
GregCr 0:1ed39951ab7b 265 break;
GregCr 0:1ed39951ab7b 266 case TX_TIMEOUT:
GregCr 0:1ed39951ab7b 267 Radio.Rx( RX_TIMEOUT_VALUE );
GregCr 0:1ed39951ab7b 268 State = LOWPOWER;
GregCr 0:1ed39951ab7b 269 break;
GregCr 0:1ed39951ab7b 270 case LOWPOWER:
GregCr 0:1ed39951ab7b 271 break;
GregCr 0:1ed39951ab7b 272 default:
GregCr 0:1ed39951ab7b 273 State = LOWPOWER;
GregCr 0:1ed39951ab7b 274 break;
GregCr 0:1ed39951ab7b 275 }
GregCr 0:1ed39951ab7b 276 }
GregCr 0:1ed39951ab7b 277 }
GregCr 0:1ed39951ab7b 278
GregCr 0:1ed39951ab7b 279 void OnTxDone( void )
GregCr 0:1ed39951ab7b 280 {
GregCr 0:1ed39951ab7b 281 debug( DEBUG_MESSAGE, ":OnTxDone\n\r" );
GregCr 0:1ed39951ab7b 282 State = TX;
GregCr 0:1ed39951ab7b 283 }
GregCr 0:1ed39951ab7b 284
GregCr 0:1ed39951ab7b 285 void OnRxDone( uint8_t *payload, uint16_t size, int8_t rssi, int8_t snr)
GregCr 0:1ed39951ab7b 286 {
GregCr 0:1ed39951ab7b 287 debug( DEBUG_MESSAGE, ":OnRxDone\n\r" );
GregCr 0:1ed39951ab7b 288 Radio.Sleep( );
GregCr 0:1ed39951ab7b 289 BufferSize = size;
GregCr 0:1ed39951ab7b 290 memcpy( Buffer, payload, BufferSize );
GregCr 0:1ed39951ab7b 291 RssiValue = rssi;
GregCr 0:1ed39951ab7b 292 SnrValue = snr;
GregCr 0:1ed39951ab7b 293 State = RX;
GregCr 0:1ed39951ab7b 294 }
GregCr 0:1ed39951ab7b 295
GregCr 0:1ed39951ab7b 296 void OnTxTimeout( void )
GregCr 0:1ed39951ab7b 297 {
GregCr 0:1ed39951ab7b 298 debug( DEBUG_MESSAGE, ":OnTxTimeout\n\r" );
GregCr 0:1ed39951ab7b 299 Radio.Sleep( );
GregCr 0:1ed39951ab7b 300 State = TX_TIMEOUT;
GregCr 0:1ed39951ab7b 301 }
GregCr 0:1ed39951ab7b 302
GregCr 0:1ed39951ab7b 303 void OnRxTimeout( void )
GregCr 0:1ed39951ab7b 304 {
GregCr 0:1ed39951ab7b 305 debug( DEBUG_MESSAGE, ":OnRxTimeout\n\r" );
GregCr 0:1ed39951ab7b 306 Radio.Sleep( );
GregCr 1:126d70d374f6 307 Buffer[ BufferSize ] = 0;
GregCr 0:1ed39951ab7b 308 State = RX_TIMEOUT;
GregCr 0:1ed39951ab7b 309 }
GregCr 0:1ed39951ab7b 310
GregCr 0:1ed39951ab7b 311 void OnRxError( void )
GregCr 0:1ed39951ab7b 312 {
GregCr 0:1ed39951ab7b 313 debug( DEBUG_MESSAGE, ":OnRxError\n\r" );
GregCr 0:1ed39951ab7b 314 Radio.Sleep( );
GregCr 0:1ed39951ab7b 315 State = RX_ERROR;
GregCr 0:1ed39951ab7b 316 }