Updated Nov. 18th.

Dependencies:   SX1276Lib_modtronix

Fork of SX1276PingPong by Semtech

Committer:
AMNoll
Date:
Sun Nov 19 18:22:07 2017 +0000
Revision:
16:4051b7c16410
Parent:
15:8b35dd7c1436
Updated Nov. 18th

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AMNoll 16:4051b7c16410 1 #include "mbed.h"
AMNoll 16:4051b7c16410 2 #include "sx1276Regs-LoRa.h"
AMNoll 16:4051b7c16410 3 #include "sx1276-inAir.h"
AMNoll 16:4051b7c16410 4
AMNoll 16:4051b7c16410 5 #define DEBUG 1
AMNoll 16:4051b7c16410 6
AMNoll 16:4051b7c16410 7 #define RF_FREQUENCY 915000000 // 915MHz
AMNoll 16:4051b7c16410 8 #define TX_OUTPUT_POWER 14 // 14 dBm for inAir9
AMNoll 16:4051b7c16410 9 #define LORA_BANDWIDTH 7 // 0: 7.8 kHz, 1: 10.4 kHz, 2: 15.6kHz, 3: 20.8kHz,
AMNoll 16:4051b7c16410 10 // 4: 31.25kHz, 5: 41.7 kHz, 6: 62.5 kHz,
AMNoll 16:4051b7c16410 11 // 7: 125 kHz, 8: 250 kHz, 9: 500 kHz
AMNoll 16:4051b7c16410 12 #define LORA_SPREADING_FACTOR 12 // SF7..SF12
AMNoll 16:4051b7c16410 13 #define LORA_CODINGRATE 1 // 1=4/5, 2=4/6, 3=4/7, 4=4/8
AMNoll 16:4051b7c16410 14 #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
AMNoll 16:4051b7c16410 15 #define LORA_SYMBOL_TIMEOUT 5 // Symbols
AMNoll 16:4051b7c16410 16 #define LORA_FIX_LENGTH_PAYLOAD_ON false
AMNoll 16:4051b7c16410 17 #define LORA_FHSS_ENABLED false
AMNoll 16:4051b7c16410 18 #define LORA_NB_SYMB_HOP 4
AMNoll 16:4051b7c16410 19 #define LORA_IQ_INVERSION_ON false
AMNoll 16:4051b7c16410 20 #define LORA_CRC_ENABLED true
AMNoll 16:4051b7c16410 21
AMNoll 16:4051b7c16410 22 #define TX_TIMEOUT_VALUE 2000000
AMNoll 16:4051b7c16410 23 #define RX_TIMEOUT_VALUE 3500000 // in us
AMNoll 16:4051b7c16410 24 #define BUFFER_SIZE 32 // Define the payload size here
AMNoll 16:4051b7c16410 25
AMNoll 16:4051b7c16410 26
AMNoll 16:4051b7c16410 27 void OnTxDone(void);
AMNoll 16:4051b7c16410 28 void OnTxTimeout(void);
AMNoll 16:4051b7c16410 29 void OnRxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr);
AMNoll 16:4051b7c16410 30 void OnRxTimeout(void);
AMNoll 16:4051b7c16410 31 void OnRxError(void);
AMNoll 16:4051b7c16410 32
AMNoll 16:4051b7c16410 33 Serial pc(USBTX, USBRX); //Use default TX and RX. Available via USB Com port when using PGM-NUCLEO programmer
AMNoll 16:4051b7c16410 34 SX1276inAir radio(OnTxDone, OnTxTimeout, OnRxDone, OnRxTimeout, OnRxError, NULL, NULL);
AMNoll 16:4051b7c16410 35 DigitalOut led(LED1);
AMNoll 16:4051b7c16410 36
AMNoll 16:4051b7c16410 37 uint8_t Buffer[BUFFER_SIZE];
AMNoll 16:4051b7c16410 38 uint16_t BufferSize = BUFFER_SIZE;
AMNoll 16:4051b7c16410 39 int16_t LoRaRssi;
AMNoll 16:4051b7c16410 40 int8_t LoRaSNR;
AMNoll 16:4051b7c16410 41 volatile RadioState State = LOWPOWER;
AMNoll 16:4051b7c16410 42
AMNoll 16:4051b7c16410 43 const uint8_t PingMsg[] = "PING";
AMNoll 16:4051b7c16410 44 const uint8_t PongMsg[] = "PONG";
AMNoll 16:4051b7c16410 45
AMNoll 16:4051b7c16410 46 int main() {
AMNoll 16:4051b7c16410 47 wait_ms(500); // start delay
AMNoll 16:4051b7c16410 48
AMNoll 16:4051b7c16410 49 // configure uart port
AMNoll 16:4051b7c16410 50 pc.baud(9600);
AMNoll 16:4051b7c16410 51 pc.format(8, SerialBase::None, 1);
AMNoll 16:4051b7c16410 52 pc.printf("PC printing enabled\n\r");
AMNoll 16:4051b7c16410 53 wait(2);
AMNoll 16:4051b7c16410 54 // configure radio
AMNoll 16:4051b7c16410 55 radio.SetBoardType(BOARD_INAIR9); // the correct hardware for our own board
AMNoll 16:4051b7c16410 56 pc.printf("Board type set\n\r");
AMNoll 16:4051b7c16410 57
AMNoll 16:4051b7c16410 58 /*
AMNoll 16:4051b7c16410 59 while (true) {
AMNoll 16:4051b7c16410 60 led = !led;
AMNoll 16:4051b7c16410 61 pc.printf("loop reached\n\r");
AMNoll 16:4051b7c16410 62 wait(2);
AMNoll 16:4051b7c16410 63 }*/
AMNoll 16:4051b7c16410 64
AMNoll 16:4051b7c16410 65 led = 0;
AMNoll 16:4051b7c16410 66 while (radio.Read(REG_VERSION) == 0x00)
AMNoll 16:4051b7c16410 67 {
AMNoll 16:4051b7c16410 68 pc.printf("Trying to connect to radio device\r\n");
AMNoll 16:4051b7c16410 69 wait_ms(200);
AMNoll 16:4051b7c16410 70 }
AMNoll 16:4051b7c16410 71 led = 1;
AMNoll 16:4051b7c16410 72
AMNoll 16:4051b7c16410 73 pc.printf("%u",radio.Read(REG_VERSION));
AMNoll 16:4051b7c16410 74 pc.printf("Radio is initialized\r\n");
AMNoll 16:4051b7c16410 75
AMNoll 16:4051b7c16410 76 // set radio frequency
AMNoll 16:4051b7c16410 77 radio.SetChannel(RF_FREQUENCY);
AMNoll 16:4051b7c16410 78
AMNoll 16:4051b7c16410 79 pc.printf("Freq set to 915 MHz\r\n");
AMNoll 16:4051b7c16410 80
AMNoll 16:4051b7c16410 81 // setup the modern
AMNoll 16:4051b7c16410 82 radio.SetTxConfig(
AMNoll 16:4051b7c16410 83 MODEM_LORA,
AMNoll 16:4051b7c16410 84 TX_OUTPUT_POWER,
AMNoll 16:4051b7c16410 85 0,
AMNoll 16:4051b7c16410 86 LORA_BANDWIDTH,
AMNoll 16:4051b7c16410 87 LORA_SPREADING_FACTOR,
AMNoll 16:4051b7c16410 88 LORA_CODINGRATE,
AMNoll 16:4051b7c16410 89 LORA_PREAMBLE_LENGTH,
AMNoll 16:4051b7c16410 90 LORA_FIX_LENGTH_PAYLOAD_ON,
AMNoll 16:4051b7c16410 91 LORA_CRC_ENABLED,
AMNoll 16:4051b7c16410 92 LORA_FHSS_ENABLED,
AMNoll 16:4051b7c16410 93 LORA_NB_SYMB_HOP,
AMNoll 16:4051b7c16410 94 LORA_IQ_INVERSION_ON,
AMNoll 16:4051b7c16410 95 TX_TIMEOUT_VALUE
AMNoll 16:4051b7c16410 96 );
AMNoll 16:4051b7c16410 97 radio.SetRxConfig(
AMNoll 16:4051b7c16410 98 MODEM_LORA,
AMNoll 16:4051b7c16410 99 LORA_BANDWIDTH,
AMNoll 16:4051b7c16410 100 LORA_SPREADING_FACTOR,
AMNoll 16:4051b7c16410 101 LORA_CODINGRATE,
AMNoll 16:4051b7c16410 102 0,
AMNoll 16:4051b7c16410 103 LORA_PREAMBLE_LENGTH,
AMNoll 16:4051b7c16410 104 LORA_SYMBOL_TIMEOUT,
AMNoll 16:4051b7c16410 105 LORA_FIX_LENGTH_PAYLOAD_ON,
AMNoll 16:4051b7c16410 106 0,
AMNoll 16:4051b7c16410 107 LORA_CRC_ENABLED,
AMNoll 16:4051b7c16410 108 LORA_FHSS_ENABLED,
AMNoll 16:4051b7c16410 109 LORA_NB_SYMB_HOP,
AMNoll 16:4051b7c16410 110 LORA_IQ_INVERSION_ON,
AMNoll 16:4051b7c16410 111 true
AMNoll 16:4051b7c16410 112 );
AMNoll 16:4051b7c16410 113 pc.printf("TX and RX configured\r\n");
AMNoll 16:4051b7c16410 114
AMNoll 16:4051b7c16410 115 uint8_t i;
AMNoll 16:4051b7c16410 116 bool isMaster = true;
AMNoll 16:4051b7c16410 117
AMNoll 16:4051b7c16410 118 pc.printf("before .Rx\r\n");
AMNoll 16:4051b7c16410 119 radio.Rx(RX_TIMEOUT_VALUE);
AMNoll 16:4051b7c16410 120 pc.printf("RX_TIMEOUT_VALUE RX set\r\n");
AMNoll 16:4051b7c16410 121
AMNoll 16:4051b7c16410 122 State = RX_TIMEOUT;
AMNoll 16:4051b7c16410 123
AMNoll 16:4051b7c16410 124 while (1)
AMNoll 16:4051b7c16410 125 {
AMNoll 16:4051b7c16410 126 // Check for connection to radio module
AMNoll 16:4051b7c16410 127 while (radio.Read(REG_VERSION) == 0x00)
AMNoll 16:4051b7c16410 128 {
AMNoll 16:4051b7c16410 129 led = !led;
AMNoll 16:4051b7c16410 130 pc.printf("Reconnecting...\r\n");
AMNoll 16:4051b7c16410 131 wait_ms(200);
AMNoll 16:4051b7c16410 132 }
AMNoll 16:4051b7c16410 133 led = 1;
AMNoll 16:4051b7c16410 134
AMNoll 16:4051b7c16410 135 pc.printf("%s\r\n",State );
AMNoll 16:4051b7c16410 136 //pc.printf("About to check State value\r\n");
AMNoll 16:4051b7c16410 137
AMNoll 16:4051b7c16410 138 switch(State)
AMNoll 16:4051b7c16410 139 {
AMNoll 16:4051b7c16410 140 case RX_DONE:
AMNoll 16:4051b7c16410 141 if (isMaster)
AMNoll 16:4051b7c16410 142 {
AMNoll 16:4051b7c16410 143 if (BufferSize > 0)
AMNoll 16:4051b7c16410 144 {
AMNoll 16:4051b7c16410 145 if (strncmp((const char *)Buffer, (const char *)PongMsg, 4) == 0)
AMNoll 16:4051b7c16410 146 {
AMNoll 16:4051b7c16410 147 pc.printf("...Pong\r\n");
AMNoll 16:4051b7c16410 148 // send next ping frame
AMNoll 16:4051b7c16410 149 strcpy((char *)Buffer, (char *)PingMsg);
AMNoll 16:4051b7c16410 150 // fill the buffer with numbers for the payload
AMNoll 16:4051b7c16410 151 for( i = 4; i < BufferSize; i++ )
AMNoll 16:4051b7c16410 152 {
AMNoll 16:4051b7c16410 153 Buffer[i] = i - 4;
AMNoll 16:4051b7c16410 154 }
AMNoll 16:4051b7c16410 155 wait_ms( 10 );
AMNoll 16:4051b7c16410 156 radio.Send( Buffer, BufferSize );
AMNoll 16:4051b7c16410 157 }
AMNoll 16:4051b7c16410 158 else if (strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
AMNoll 16:4051b7c16410 159 {
AMNoll 16:4051b7c16410 160 // A master already exists then become a slave
AMNoll 16:4051b7c16410 161 pc.printf("...Ping\r\n");
AMNoll 16:4051b7c16410 162 isMaster = false;
AMNoll 16:4051b7c16410 163 // send the next pong frame
AMNoll 16:4051b7c16410 164 strcpy( ( char* )Buffer, ( char* )PongMsg );
AMNoll 16:4051b7c16410 165 // We fill the buffer with numbers for the payload
AMNoll 16:4051b7c16410 166 for( i = 4; i < BufferSize; i++ )
AMNoll 16:4051b7c16410 167 {
AMNoll 16:4051b7c16410 168 Buffer[i] = i - 4;
AMNoll 16:4051b7c16410 169 }
AMNoll 16:4051b7c16410 170 wait_ms( 10 );
AMNoll 16:4051b7c16410 171 radio.Send( Buffer, BufferSize );
AMNoll 16:4051b7c16410 172 }
AMNoll 16:4051b7c16410 173 else
AMNoll 16:4051b7c16410 174 {
AMNoll 16:4051b7c16410 175 isMaster = true;
AMNoll 16:4051b7c16410 176 radio.Rx(RX_TIMEOUT_VALUE);
AMNoll 16:4051b7c16410 177 }
AMNoll 16:4051b7c16410 178 }
AMNoll 16:4051b7c16410 179 }
AMNoll 16:4051b7c16410 180 else
AMNoll 16:4051b7c16410 181 {
AMNoll 16:4051b7c16410 182 if (BufferSize > 0)
AMNoll 16:4051b7c16410 183 {
AMNoll 16:4051b7c16410 184 if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
AMNoll 16:4051b7c16410 185 {
AMNoll 16:4051b7c16410 186 pc.printf( "...Ping\r\n");
AMNoll 16:4051b7c16410 187 // Send the reply to the PING string
AMNoll 16:4051b7c16410 188 strcpy( ( char* )Buffer, ( char* )PongMsg );
AMNoll 16:4051b7c16410 189 // We fill the buffer with numbers for the payload
AMNoll 16:4051b7c16410 190 for( i = 4; i < BufferSize; i++ )
AMNoll 16:4051b7c16410 191 {
AMNoll 16:4051b7c16410 192 Buffer[i] = i - 4;
AMNoll 16:4051b7c16410 193 }
AMNoll 16:4051b7c16410 194 wait_ms( 10 );
AMNoll 16:4051b7c16410 195 radio.Send( Buffer, BufferSize );
AMNoll 16:4051b7c16410 196 }
AMNoll 16:4051b7c16410 197 else // valid reception but not a PING as expected
AMNoll 16:4051b7c16410 198 { // Set device as master and start again
AMNoll 16:4051b7c16410 199 isMaster = true;
AMNoll 16:4051b7c16410 200 radio.Rx( RX_TIMEOUT_VALUE );
AMNoll 16:4051b7c16410 201 }
AMNoll 16:4051b7c16410 202 }
AMNoll 16:4051b7c16410 203 }
AMNoll 16:4051b7c16410 204 State = LOWPOWER;
AMNoll 16:4051b7c16410 205 pc.printf( "\r\n");
AMNoll 16:4051b7c16410 206 break;
AMNoll 16:4051b7c16410 207
AMNoll 16:4051b7c16410 208 case TX_DONE:
AMNoll 16:4051b7c16410 209 if (isMaster)
AMNoll 16:4051b7c16410 210 {
AMNoll 16:4051b7c16410 211 pc.printf("Ping...\r\n");
AMNoll 16:4051b7c16410 212 }
AMNoll 16:4051b7c16410 213 else
AMNoll 16:4051b7c16410 214 {
AMNoll 16:4051b7c16410 215 pc.printf("Pong...\r\n");
AMNoll 16:4051b7c16410 216 }
AMNoll 16:4051b7c16410 217 radio.Rx(RX_TIMEOUT_VALUE);
AMNoll 16:4051b7c16410 218 State = LOWPOWER;
AMNoll 16:4051b7c16410 219 break;
AMNoll 16:4051b7c16410 220
AMNoll 16:4051b7c16410 221 case RX_TIMEOUT:
AMNoll 16:4051b7c16410 222 if( isMaster == true )
AMNoll 16:4051b7c16410 223 {
AMNoll 16:4051b7c16410 224 pc.printf("trying to send ping\r\n");
AMNoll 16:4051b7c16410 225 // Send the next PING frame
AMNoll 16:4051b7c16410 226 strcpy( ( char* )Buffer, ( char* )PingMsg );
AMNoll 16:4051b7c16410 227 for( i = 4; i < BufferSize; i++ )
AMNoll 16:4051b7c16410 228 {
AMNoll 16:4051b7c16410 229 Buffer[i] = i - 4;
AMNoll 16:4051b7c16410 230 }
AMNoll 16:4051b7c16410 231 wait_ms( 10 );
AMNoll 16:4051b7c16410 232 radio.Send( Buffer, BufferSize );
AMNoll 16:4051b7c16410 233 }
AMNoll 16:4051b7c16410 234 else
AMNoll 16:4051b7c16410 235 {
AMNoll 16:4051b7c16410 236 radio.Rx( RX_TIMEOUT_VALUE );
AMNoll 16:4051b7c16410 237 }
AMNoll 16:4051b7c16410 238 State = LOWPOWER;
AMNoll 16:4051b7c16410 239 break;
AMNoll 16:4051b7c16410 240
AMNoll 16:4051b7c16410 241 case TX_TIMEOUT:
AMNoll 16:4051b7c16410 242 radio.Rx( RX_TIMEOUT_VALUE );
AMNoll 16:4051b7c16410 243 State = LOWPOWER;
AMNoll 16:4051b7c16410 244 break;
AMNoll 16:4051b7c16410 245
AMNoll 16:4051b7c16410 246 case RX_ERROR:
AMNoll 16:4051b7c16410 247 // We have received a Packet with a CRC error, send reply as if packet was correct
AMNoll 16:4051b7c16410 248 if( isMaster == true )
AMNoll 16:4051b7c16410 249 {
AMNoll 16:4051b7c16410 250 // Send the next PING frame
AMNoll 16:4051b7c16410 251 strcpy( ( char* )Buffer, ( char* )PingMsg );
AMNoll 16:4051b7c16410 252 for( i = 4; i < BufferSize; i++ )
AMNoll 16:4051b7c16410 253 {
AMNoll 16:4051b7c16410 254 Buffer[i] = i - 4;
AMNoll 16:4051b7c16410 255 }
AMNoll 16:4051b7c16410 256 wait_ms( 10 );
AMNoll 16:4051b7c16410 257 radio.Send( Buffer, BufferSize );
AMNoll 16:4051b7c16410 258 }
AMNoll 16:4051b7c16410 259 else
AMNoll 16:4051b7c16410 260 {
AMNoll 16:4051b7c16410 261 // Send the next PONG frame
AMNoll 16:4051b7c16410 262 strcpy( ( char* )Buffer, ( char* )PongMsg );
AMNoll 16:4051b7c16410 263 for( i = 4; i < BufferSize; i++ )
AMNoll 16:4051b7c16410 264 {
AMNoll 16:4051b7c16410 265 Buffer[i] = i - 4;
AMNoll 16:4051b7c16410 266 }
AMNoll 16:4051b7c16410 267 wait_ms( 10 );
AMNoll 16:4051b7c16410 268 radio.Send( Buffer, BufferSize );
AMNoll 16:4051b7c16410 269 }
AMNoll 16:4051b7c16410 270 State = LOWPOWER;
AMNoll 16:4051b7c16410 271 break;
AMNoll 16:4051b7c16410 272
AMNoll 16:4051b7c16410 273 case LOWPOWER:
AMNoll 16:4051b7c16410 274 break;
AMNoll 16:4051b7c16410 275
AMNoll 16:4051b7c16410 276 default:
AMNoll 16:4051b7c16410 277 State = LOWPOWER;
AMNoll 16:4051b7c16410 278 break;
AMNoll 16:4051b7c16410 279 }
AMNoll 16:4051b7c16410 280 }
AMNoll 16:4051b7c16410 281 }
AMNoll 16:4051b7c16410 282
AMNoll 16:4051b7c16410 283
AMNoll 16:4051b7c16410 284 void OnTxDone(void)
AMNoll 16:4051b7c16410 285 {
AMNoll 16:4051b7c16410 286 radio.Sleep();
AMNoll 16:4051b7c16410 287 State = TX_DONE;
AMNoll 16:4051b7c16410 288
AMNoll 16:4051b7c16410 289 #if DEBUG == 1
AMNoll 16:4051b7c16410 290 pc.printf("OnTxDone\r\n");
AMNoll 16:4051b7c16410 291 #endif
AMNoll 16:4051b7c16410 292 }
AMNoll 16:4051b7c16410 293
AMNoll 16:4051b7c16410 294 void OnTxTimeout(void)
AMNoll 16:4051b7c16410 295 {
AMNoll 16:4051b7c16410 296 radio.Sleep();
AMNoll 16:4051b7c16410 297 State = TX_TIMEOUT;
AMNoll 16:4051b7c16410 298
AMNoll 16:4051b7c16410 299 #if DEBUG == 1
AMNoll 16:4051b7c16410 300 pc.printf("OnTxTimeout\r\n");
AMNoll 16:4051b7c16410 301 #endif
AMNoll 16:4051b7c16410 302 }
AMNoll 16:4051b7c16410 303
AMNoll 16:4051b7c16410 304 void OnRxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
AMNoll 16:4051b7c16410 305 {
AMNoll 16:4051b7c16410 306 radio.Sleep();
AMNoll 16:4051b7c16410 307 BufferSize = size;
AMNoll 16:4051b7c16410 308 memcpy(Buffer, payload, BufferSize);
AMNoll 16:4051b7c16410 309 LoRaRssi = rssi;
AMNoll 16:4051b7c16410 310 LoRaSNR = snr;
AMNoll 16:4051b7c16410 311 State = RX_DONE;
AMNoll 16:4051b7c16410 312
AMNoll 16:4051b7c16410 313 #if DEBUG == 1
AMNoll 16:4051b7c16410 314 pc.printf("OnRxDone\r\n");
AMNoll 16:4051b7c16410 315 #endif
AMNoll 16:4051b7c16410 316 }
AMNoll 16:4051b7c16410 317
AMNoll 16:4051b7c16410 318 void OnRxTimeout(void)
AMNoll 16:4051b7c16410 319 {
AMNoll 16:4051b7c16410 320 radio.Sleep();
AMNoll 16:4051b7c16410 321 Buffer[BufferSize] = 0;
AMNoll 16:4051b7c16410 322 State = RX_TIMEOUT;
AMNoll 16:4051b7c16410 323
AMNoll 16:4051b7c16410 324 #if DEBUG == 1
AMNoll 16:4051b7c16410 325 pc.printf("OnRxTimeout\r\n");
AMNoll 16:4051b7c16410 326 #endif
AMNoll 16:4051b7c16410 327 }
AMNoll 16:4051b7c16410 328
AMNoll 16:4051b7c16410 329 void OnRxError(void)
AMNoll 16:4051b7c16410 330 {
AMNoll 16:4051b7c16410 331 radio.Sleep();
AMNoll 16:4051b7c16410 332 State = RX_ERROR;
AMNoll 16:4051b7c16410 333
AMNoll 16:4051b7c16410 334 #if DEBUG == 1
AMNoll 16:4051b7c16410 335 pc.printf("OnRxError\r\n");
AMNoll 16:4051b7c16410 336 #endif
AMNoll 16:4051b7c16410 337 }