First attempt at LoRa USB Rx

Dependencies:   BufferedSerial SX1276GenericLib mbed

Fork of DISCO-L072CZ-LRWAN1_LoRa_PingPong by ST

Committer:
akashvibhute
Date:
Tue Jan 02 06:32:31 2018 +0000
Revision:
11:17b932171aee
Parent:
10:f84959c2e044
First attempt at LoRa USB Rx

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Helmut64 0:c43b6919ae15 1 /*
Helmut64 0:c43b6919ae15 2 * Copyright (c) 2017 Helmut Tschemernjak
Helmut64 0:c43b6919ae15 3 * 30826 Garbsen (Hannover) Germany
Helmut64 0:c43b6919ae15 4 * Licensed under the Apache License, Version 2.0);
Helmut64 0:c43b6919ae15 5 */
Helmut64 0:c43b6919ae15 6 #include "main.h"
Helmut64 0:c43b6919ae15 7
Helmut64 0:c43b6919ae15 8 DigitalOut myled(LED1);
Helmut64 0:c43b6919ae15 9 BufferedSerial *ser;
Helmut64 0:c43b6919ae15 10
akashvibhute 11:17b932171aee 11
akashvibhute 11:17b932171aee 12 #ifdef FEATURE_LORA
akashvibhute 11:17b932171aee 13
akashvibhute 11:17b932171aee 14 /* Set this flag to '1' to display debug messages on the console */
akashvibhute 11:17b932171aee 15 #define DEBUG_MESSAGE 0
akashvibhute 11:17b932171aee 16
akashvibhute 11:17b932171aee 17 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */
akashvibhute 11:17b932171aee 18 #define USE_MODEM_LORA 1
akashvibhute 11:17b932171aee 19 #define USE_MODEM_FSK !USE_MODEM_LORA
akashvibhute 11:17b932171aee 20 #define RF_FREQUENCY 915000000//RF_FREQUENCY_868_1 // Hz
akashvibhute 11:17b932171aee 21 #define TX_OUTPUT_POWER 20 // 14 dBm
akashvibhute 11:17b932171aee 22
akashvibhute 11:17b932171aee 23 #if USE_MODEM_LORA == 1
akashvibhute 11:17b932171aee 24
akashvibhute 11:17b932171aee 25 #define LORA_BANDWIDTH 125000 // LoRa default, details in SX1276::BandwidthMap
akashvibhute 11:17b932171aee 26 #define LORA_SPREADING_FACTOR LORA_SF12
akashvibhute 11:17b932171aee 27 #define LORA_CODINGRATE LORA_ERROR_CODING_RATE_4_5
akashvibhute 11:17b932171aee 28
akashvibhute 11:17b932171aee 29 #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
akashvibhute 11:17b932171aee 30 #define LORA_SYMBOL_TIMEOUT 5 // Symbols
akashvibhute 11:17b932171aee 31 #define LORA_FIX_LENGTH_PAYLOAD_ON false
akashvibhute 11:17b932171aee 32 #define LORA_FHSS_ENABLED false
akashvibhute 11:17b932171aee 33 #define LORA_NB_SYMB_HOP 4
akashvibhute 11:17b932171aee 34 #define LORA_IQ_INVERSION_ON false
akashvibhute 11:17b932171aee 35 #define LORA_CRC_ENABLED true
akashvibhute 11:17b932171aee 36
akashvibhute 11:17b932171aee 37 #elif USE_MODEM_FSK == 1
akashvibhute 11:17b932171aee 38
akashvibhute 11:17b932171aee 39 #define FSK_FDEV 25000 // Hz
akashvibhute 11:17b932171aee 40 #define FSK_DATARATE 19200 // bps
akashvibhute 11:17b932171aee 41 #define FSK_BANDWIDTH 50000 // Hz
akashvibhute 11:17b932171aee 42 #define FSK_AFC_BANDWIDTH 83333 // Hz
akashvibhute 11:17b932171aee 43 #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx
akashvibhute 11:17b932171aee 44 #define FSK_FIX_LENGTH_PAYLOAD_ON false
akashvibhute 11:17b932171aee 45 #define FSK_CRC_ENABLED true
akashvibhute 11:17b932171aee 46
akashvibhute 11:17b932171aee 47 #else
akashvibhute 11:17b932171aee 48 #error "Please define a modem in the compiler options."
akashvibhute 11:17b932171aee 49 #endif
akashvibhute 11:17b932171aee 50
akashvibhute 11:17b932171aee 51
akashvibhute 11:17b932171aee 52 #define RX_TIMEOUT_VALUE 8000//3500 // in ms
akashvibhute 11:17b932171aee 53
akashvibhute 11:17b932171aee 54 //#define BUFFER_SIZE 32 // Define the payload size here
akashvibhute 11:17b932171aee 55 #define BUFFER_SIZE 4//64 // Define the payload size here
akashvibhute 11:17b932171aee 56
akashvibhute 11:17b932171aee 57 /*
akashvibhute 11:17b932171aee 58 * Global variables declarations
akashvibhute 11:17b932171aee 59 */
akashvibhute 11:17b932171aee 60 typedef enum
akashvibhute 11:17b932171aee 61 {
akashvibhute 11:17b932171aee 62 LOWPOWER = 0,
akashvibhute 11:17b932171aee 63 IDLE,
akashvibhute 11:17b932171aee 64
akashvibhute 11:17b932171aee 65 RX,
akashvibhute 11:17b932171aee 66 RX_TIMEOUT,
akashvibhute 11:17b932171aee 67 RX_ERROR,
akashvibhute 11:17b932171aee 68
akashvibhute 11:17b932171aee 69 TX,
akashvibhute 11:17b932171aee 70 TX_TIMEOUT,
akashvibhute 11:17b932171aee 71
akashvibhute 11:17b932171aee 72 CAD,
akashvibhute 11:17b932171aee 73 CAD_DONE
akashvibhute 11:17b932171aee 74 } AppStates_t;
akashvibhute 11:17b932171aee 75
akashvibhute 11:17b932171aee 76 volatile AppStates_t State = LOWPOWER;
akashvibhute 11:17b932171aee 77
akashvibhute 11:17b932171aee 78 /*!
akashvibhute 11:17b932171aee 79 * Radio events function pointer
akashvibhute 11:17b932171aee 80 */
akashvibhute 11:17b932171aee 81 static RadioEvents_t RadioEvents;
akashvibhute 11:17b932171aee 82
akashvibhute 11:17b932171aee 83 /*
akashvibhute 11:17b932171aee 84 * Global variables declarations
akashvibhute 11:17b932171aee 85 */
akashvibhute 11:17b932171aee 86 SX1276Generic *Radio;
akashvibhute 11:17b932171aee 87
akashvibhute 11:17b932171aee 88
akashvibhute 11:17b932171aee 89 //const uint8_t PingMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'I', 'N', 'G'};// "PING";
akashvibhute 11:17b932171aee 90 const uint8_t PingMsg[] = { 'P', 'I', 'N', 'G'};// "PING";
akashvibhute 11:17b932171aee 91 //const uint8_t PongMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'O', 'N', 'G'};// "PONG";
akashvibhute 11:17b932171aee 92 const uint8_t PongMsg[] = { 'P', 'O', 'N', 'G'};// "PONG";
akashvibhute 11:17b932171aee 93
akashvibhute 11:17b932171aee 94 uint16_t BufferSize = BUFFER_SIZE;
akashvibhute 11:17b932171aee 95 uint8_t *Buffer;
akashvibhute 11:17b932171aee 96
akashvibhute 11:17b932171aee 97 DigitalOut *led3;
akashvibhute 11:17b932171aee 98 DigitalOut *led = new DigitalOut(LED1);
akashvibhute 11:17b932171aee 99
akashvibhute 11:17b932171aee 100 uint8_t i;
akashvibhute 11:17b932171aee 101 bool isMaster = false;
akashvibhute 11:17b932171aee 102
akashvibhute 11:17b932171aee 103 uint8_t SX1276DataTx[4];
akashvibhute 11:17b932171aee 104 uint8_t SX1276DataRx[4];
akashvibhute 11:17b932171aee 105 bool mode = false; // false is slave, true is master
akashvibhute 11:17b932171aee 106
akashvibhute 11:17b932171aee 107 uint8_t serialRxBuffer[4];
Helmut64 0:c43b6919ae15 108 int main() {
Helmut64 0:c43b6919ae15 109 SystemClock_Config();
Helmut64 0:c43b6919ae15 110 ser = new BufferedSerial(USBTX, USBRX);
akashvibhute 11:17b932171aee 111 ser->baud(115200);
Helmut64 0:c43b6919ae15 112 ser->format(8);
Helmut64 0:c43b6919ae15 113 ser->printf("Hello World\n\r");
Helmut64 0:c43b6919ae15 114 myled = 1;
Helmut64 0:c43b6919ae15 115
akashvibhute 11:17b932171aee 116 #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) )
akashvibhute 11:17b932171aee 117 DigitalOut *led = new DigitalOut(LED2);
akashvibhute 11:17b932171aee 118 #elif defined(TARGET_NUCLEO_L073RZ)
akashvibhute 11:17b932171aee 119 DigitalOut *led = new DigitalOut(LED4); // RX red
akashvibhute 11:17b932171aee 120 led3 = new DigitalOut(LED3); // TX blue
akashvibhute 11:17b932171aee 121 #else
akashvibhute 11:17b932171aee 122 DigitalOut *led = new DigitalOut(LED1);
akashvibhute 11:17b932171aee 123 led3 = led;
akashvibhute 11:17b932171aee 124 #endif
akashvibhute 11:17b932171aee 125
akashvibhute 11:17b932171aee 126 Buffer = new uint8_t[BUFFER_SIZE];
akashvibhute 11:17b932171aee 127 *led3 = 1;
akashvibhute 11:17b932171aee 128
akashvibhute 11:17b932171aee 129 #ifdef B_L072Z_LRWAN1_LORA
akashvibhute 11:17b932171aee 130 Radio = new SX1276Generic(NULL, MURATA_SX1276,
akashvibhute 11:17b932171aee 131 LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET,
akashvibhute 11:17b932171aee 132 LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5,
akashvibhute 11:17b932171aee 133 LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO);
akashvibhute 11:17b932171aee 134 #else // RFM95
akashvibhute 11:17b932171aee 135 Radio = new SX1276Generic(NULL, RFM95_SX1276,
akashvibhute 11:17b932171aee 136 LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET,
akashvibhute 11:17b932171aee 137 LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5);
akashvibhute 11:17b932171aee 138
akashvibhute 11:17b932171aee 139 #endif
akashvibhute 11:17b932171aee 140
akashvibhute 11:17b932171aee 141
akashvibhute 11:17b932171aee 142 isMaster = true;
akashvibhute 11:17b932171aee 143
akashvibhute 11:17b932171aee 144 dprintf("SX1276 Ping Pong Demo Application" );
akashvibhute 11:17b932171aee 145 dprintf("Freqency: %.1f", (double)RF_FREQUENCY/1000000.0);
akashvibhute 11:17b932171aee 146 dprintf("TXPower: %d dBm", TX_OUTPUT_POWER);
akashvibhute 11:17b932171aee 147 #if USE_MODEM_LORA == 1
akashvibhute 11:17b932171aee 148 dprintf("Bandwidth: %d Hz", LORA_BANDWIDTH);
akashvibhute 11:17b932171aee 149 dprintf("Spreading factor: SF%d", LORA_SPREADING_FACTOR);
akashvibhute 11:17b932171aee 150 #elif USE_MODEM_FSK == 1
akashvibhute 11:17b932171aee 151 dprintf("Bandwidth: %d kHz", FSK_BANDWIDTH);
akashvibhute 11:17b932171aee 152 dprintf("Baudrate: %d", FSK_DATARATE);
akashvibhute 11:17b932171aee 153 #endif
akashvibhute 11:17b932171aee 154 // Initialize Radio driver
akashvibhute 11:17b932171aee 155 RadioEvents.TxDone = OnTxDone;
akashvibhute 11:17b932171aee 156 RadioEvents.RxDone = OnRxDone;
akashvibhute 11:17b932171aee 157 RadioEvents.RxError = OnRxError;
akashvibhute 11:17b932171aee 158 RadioEvents.TxTimeout = OnTxTimeout;
akashvibhute 11:17b932171aee 159 RadioEvents.RxTimeout = OnRxTimeout;
akashvibhute 11:17b932171aee 160 if (Radio->Init( &RadioEvents ) == false) {
akashvibhute 11:17b932171aee 161 while(1) {
akashvibhute 11:17b932171aee 162 dprintf("Radio could not be detected!");
akashvibhute 11:17b932171aee 163 wait( 1 );
akashvibhute 11:17b932171aee 164 }
akashvibhute 11:17b932171aee 165 }
akashvibhute 11:17b932171aee 166
akashvibhute 11:17b932171aee 167
akashvibhute 11:17b932171aee 168 switch(Radio->DetectBoardType()) {
akashvibhute 11:17b932171aee 169 case SX1276MB1LAS:
akashvibhute 11:17b932171aee 170 if (DEBUG_MESSAGE)
akashvibhute 11:17b932171aee 171 dprintf(" > Board Type: SX1276MB1LAS <");
akashvibhute 11:17b932171aee 172 break;
akashvibhute 11:17b932171aee 173 case SX1276MB1MAS:
akashvibhute 11:17b932171aee 174 if (DEBUG_MESSAGE)
akashvibhute 11:17b932171aee 175 dprintf(" > Board Type: SX1276MB1LAS <");
akashvibhute 11:17b932171aee 176 case MURATA_SX1276:
akashvibhute 11:17b932171aee 177 if (DEBUG_MESSAGE)
akashvibhute 11:17b932171aee 178 dprintf(" > Board Type: MURATA_SX1276_STM32L0 <");
akashvibhute 11:17b932171aee 179 break;
akashvibhute 11:17b932171aee 180 case RFM95_SX1276:
akashvibhute 11:17b932171aee 181 if (DEBUG_MESSAGE)
akashvibhute 11:17b932171aee 182 dprintf(" > HopeRF RFM95xx <");
akashvibhute 11:17b932171aee 183 break;
akashvibhute 11:17b932171aee 184 default:
akashvibhute 11:17b932171aee 185 dprintf(" > Board Type: unknown <");
akashvibhute 11:17b932171aee 186 }
akashvibhute 11:17b932171aee 187
akashvibhute 11:17b932171aee 188 Radio->SetChannel(RF_FREQUENCY );
akashvibhute 11:17b932171aee 189
akashvibhute 11:17b932171aee 190 #if USE_MODEM_LORA == 1
akashvibhute 11:17b932171aee 191
akashvibhute 11:17b932171aee 192 if (LORA_FHSS_ENABLED)
akashvibhute 11:17b932171aee 193 dprintf(" > LORA FHSS Mode <");
akashvibhute 11:17b932171aee 194 if (!LORA_FHSS_ENABLED)
akashvibhute 11:17b932171aee 195 dprintf(" > LORA Mode <");
akashvibhute 11:17b932171aee 196
akashvibhute 11:17b932171aee 197 Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
akashvibhute 11:17b932171aee 198 LORA_SPREADING_FACTOR, LORA_CODINGRATE,
akashvibhute 11:17b932171aee 199 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
akashvibhute 11:17b932171aee 200 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
akashvibhute 11:17b932171aee 201 LORA_IQ_INVERSION_ON, 2000 );
akashvibhute 11:17b932171aee 202
akashvibhute 11:17b932171aee 203 Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
akashvibhute 11:17b932171aee 204 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
akashvibhute 11:17b932171aee 205 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
akashvibhute 11:17b932171aee 206 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
akashvibhute 11:17b932171aee 207 LORA_IQ_INVERSION_ON, true );
akashvibhute 11:17b932171aee 208
akashvibhute 11:17b932171aee 209 #elif USE_MODEM_FSK == 1
akashvibhute 11:17b932171aee 210
akashvibhute 11:17b932171aee 211 dprintf(" > FSK Mode <");
akashvibhute 11:17b932171aee 212 Radio->SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
akashvibhute 11:17b932171aee 213 FSK_DATARATE, 0,
akashvibhute 11:17b932171aee 214 FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
akashvibhute 11:17b932171aee 215 FSK_CRC_ENABLED, 0, 0, 0, 2000 );
akashvibhute 11:17b932171aee 216
akashvibhute 11:17b932171aee 217 Radio->SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
akashvibhute 11:17b932171aee 218 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
akashvibhute 11:17b932171aee 219 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED,
akashvibhute 11:17b932171aee 220 0, 0, false, true );
akashvibhute 11:17b932171aee 221
akashvibhute 11:17b932171aee 222 #else
akashvibhute 11:17b932171aee 223
akashvibhute 11:17b932171aee 224 #error "Please define a modem in the compiler options."
akashvibhute 11:17b932171aee 225
akashvibhute 11:17b932171aee 226 #endif
akashvibhute 11:17b932171aee 227
akashvibhute 11:17b932171aee 228 if (DEBUG_MESSAGE)
akashvibhute 11:17b932171aee 229 dprintf("Starting Rx loop");
akashvibhute 11:17b932171aee 230
akashvibhute 11:17b932171aee 231
akashvibhute 11:17b932171aee 232 Radio->Rx( RX_TIMEOUT_VALUE );
akashvibhute 11:17b932171aee 233 /*
akashvibhute 11:17b932171aee 234 SX1276DataTx[0] = 0x0A;
akashvibhute 11:17b932171aee 235 SX1276DataTx[1] = 0x0B;
akashvibhute 11:17b932171aee 236 SX1276DataTx[2] = 0x0C;
akashvibhute 11:17b932171aee 237 SX1276DataTx[3] = 0x0D;
akashvibhute 11:17b932171aee 238 */
akashvibhute 11:17b932171aee 239
akashvibhute 11:17b932171aee 240 while(1)
akashvibhute 11:17b932171aee 241 SX1276Comm(0, 1);
akashvibhute 11:17b932171aee 242 //SX1276PingPong();
akashvibhute 11:17b932171aee 243
akashvibhute 11:17b932171aee 244
Helmut64 0:c43b6919ae15 245 }
Helmut64 0:c43b6919ae15 246
Helmut64 0:c43b6919ae15 247
Helmut64 0:c43b6919ae15 248
Helmut64 0:c43b6919ae15 249
Helmut64 0:c43b6919ae15 250 void SystemClock_Config(void)
Helmut64 0:c43b6919ae15 251 {
Helmut64 0:c43b6919ae15 252 #ifdef B_L072Z_LRWAN1_LORA
Helmut64 0:c43b6919ae15 253 /*
Helmut64 0:c43b6919ae15 254 * The L072Z_LRWAN1_LORA clock setup is somewhat differnt from the Nucleo board.
Helmut64 0:c43b6919ae15 255 * It has no LSE.
Helmut64 0:c43b6919ae15 256 */
Helmut64 0:c43b6919ae15 257 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
Helmut64 0:c43b6919ae15 258 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
Helmut64 0:c43b6919ae15 259
Helmut64 0:c43b6919ae15 260 /* Enable HSE Oscillator and Activate PLL with HSE as source */
Helmut64 0:c43b6919ae15 261 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
Helmut64 0:c43b6919ae15 262 RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
Helmut64 0:c43b6919ae15 263 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
Helmut64 0:c43b6919ae15 264 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
Helmut64 0:c43b6919ae15 265 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
Helmut64 0:c43b6919ae15 266 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
Helmut64 0:c43b6919ae15 267 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_6;
Helmut64 0:c43b6919ae15 268 RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_3;
Helmut64 0:c43b6919ae15 269
Helmut64 0:c43b6919ae15 270 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Helmut64 0:c43b6919ae15 271 // Error_Handler();
Helmut64 0:c43b6919ae15 272 }
Helmut64 0:c43b6919ae15 273
Helmut64 0:c43b6919ae15 274 /* Set Voltage scale1 as MCU will run at 32MHz */
Helmut64 0:c43b6919ae15 275 __HAL_RCC_PWR_CLK_ENABLE();
Helmut64 0:c43b6919ae15 276 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
Helmut64 0:c43b6919ae15 277
Helmut64 0:c43b6919ae15 278 /* Poll VOSF bit of in PWR_CSR. Wait until it is reset to 0 */
Helmut64 0:c43b6919ae15 279 while (__HAL_PWR_GET_FLAG(PWR_FLAG_VOS) != RESET) {};
Helmut64 0:c43b6919ae15 280
Helmut64 0:c43b6919ae15 281 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
Helmut64 0:c43b6919ae15 282 clocks dividers */
Helmut64 0:c43b6919ae15 283 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
Helmut64 0:c43b6919ae15 284 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
Helmut64 0:c43b6919ae15 285 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
Helmut64 0:c43b6919ae15 286 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
Helmut64 0:c43b6919ae15 287 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
Helmut64 0:c43b6919ae15 288 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {
Helmut64 0:c43b6919ae15 289 // Error_Handler();
Helmut64 0:c43b6919ae15 290 }
Helmut64 0:c43b6919ae15 291 #endif
Helmut64 0:c43b6919ae15 292 }
Helmut64 0:c43b6919ae15 293
Helmut64 0:c43b6919ae15 294 void dump(const char *title, const void *data, int len, bool dwords)
Helmut64 0:c43b6919ae15 295 {
Helmut64 0:c43b6919ae15 296 dprintf("dump(\"%s\", 0x%x, %d bytes)", title, data, len);
Helmut64 0:c43b6919ae15 297
Helmut64 0:c43b6919ae15 298 int i, j, cnt;
Helmut64 0:c43b6919ae15 299 unsigned char *u;
Helmut64 0:c43b6919ae15 300 const int width = 16;
Helmut64 0:c43b6919ae15 301 const int seppos = 7;
Helmut64 0:c43b6919ae15 302
Helmut64 0:c43b6919ae15 303 cnt = 0;
Helmut64 0:c43b6919ae15 304 u = (unsigned char *)data;
Helmut64 0:c43b6919ae15 305 while (len > 0) {
Helmut64 0:c43b6919ae15 306 ser->printf("%08x: ", (unsigned int)data + cnt);
Helmut64 0:c43b6919ae15 307 if (dwords) {
Helmut64 0:c43b6919ae15 308 unsigned int *ip = ( unsigned int *)u;
Helmut64 0:c43b6919ae15 309 ser->printf(" 0x%08x\r\n", *ip);
Helmut64 0:c43b6919ae15 310 u+= 4;
Helmut64 0:c43b6919ae15 311 len -= 4;
Helmut64 0:c43b6919ae15 312 cnt += 4;
Helmut64 0:c43b6919ae15 313 continue;
Helmut64 0:c43b6919ae15 314 }
Helmut64 0:c43b6919ae15 315 cnt += width;
Helmut64 0:c43b6919ae15 316 j = len < width ? len : width;
Helmut64 0:c43b6919ae15 317 for (i = 0; i < j; i++) {
Helmut64 0:c43b6919ae15 318 ser->printf("%2.2x ", *(u + i));
Helmut64 0:c43b6919ae15 319 if (i == seppos)
Helmut64 0:c43b6919ae15 320 ser->putc(' ');
Helmut64 0:c43b6919ae15 321 }
Helmut64 0:c43b6919ae15 322 ser->putc(' ');
Helmut64 0:c43b6919ae15 323 if (j < width) {
Helmut64 0:c43b6919ae15 324 i = width - j;
Helmut64 0:c43b6919ae15 325 if (i > seppos + 1)
Helmut64 0:c43b6919ae15 326 ser->putc(' ');
Helmut64 0:c43b6919ae15 327 while (i--) {
Helmut64 0:c43b6919ae15 328 printf("%s", " ");
Helmut64 0:c43b6919ae15 329 }
Helmut64 0:c43b6919ae15 330 }
Helmut64 0:c43b6919ae15 331 for (i = 0; i < j; i++) {
Helmut64 0:c43b6919ae15 332 int c = *(u + i);
Helmut64 0:c43b6919ae15 333 if (c >= ' ' && c <= '~')
Helmut64 0:c43b6919ae15 334 ser->putc(c);
Helmut64 0:c43b6919ae15 335 else
Helmut64 0:c43b6919ae15 336 ser->putc('.');
Helmut64 0:c43b6919ae15 337 if (i == seppos)
Helmut64 0:c43b6919ae15 338 ser->putc(' ');
Helmut64 0:c43b6919ae15 339 }
Helmut64 0:c43b6919ae15 340 len -= width;
Helmut64 0:c43b6919ae15 341 u += width;
Helmut64 0:c43b6919ae15 342 ser->printf("\r\n");
Helmut64 0:c43b6919ae15 343 }
Helmut64 0:c43b6919ae15 344 ser->printf("--\r\n");
Helmut64 0:c43b6919ae15 345 }
akashvibhute 11:17b932171aee 346
akashvibhute 11:17b932171aee 347
akashvibhute 11:17b932171aee 348
akashvibhute 11:17b932171aee 349
akashvibhute 11:17b932171aee 350 int SX1276PingPong()
akashvibhute 11:17b932171aee 351 {
akashvibhute 11:17b932171aee 352
akashvibhute 11:17b932171aee 353
akashvibhute 11:17b932171aee 354 while( 1 )
akashvibhute 11:17b932171aee 355 {
akashvibhute 11:17b932171aee 356 #ifdef TARGET_STM32L4
akashvibhute 11:17b932171aee 357 WatchDogUpdate();
akashvibhute 11:17b932171aee 358 #endif
akashvibhute 11:17b932171aee 359
akashvibhute 11:17b932171aee 360 switch( State )
akashvibhute 11:17b932171aee 361 {
akashvibhute 11:17b932171aee 362 case RX:
akashvibhute 11:17b932171aee 363 *led3 = 0;
akashvibhute 11:17b932171aee 364 if( isMaster == true )
akashvibhute 11:17b932171aee 365 {
akashvibhute 11:17b932171aee 366 if( BufferSize > 0 )
akashvibhute 11:17b932171aee 367 {
akashvibhute 11:17b932171aee 368 if( memcmp(Buffer, PongMsg, sizeof(PongMsg)) == 0 )
akashvibhute 11:17b932171aee 369 {
akashvibhute 11:17b932171aee 370 *led = !*led;
akashvibhute 11:17b932171aee 371 dprintf( "...Pong" );
akashvibhute 11:17b932171aee 372 // Send the next PING frame
akashvibhute 11:17b932171aee 373 memcpy(Buffer, PingMsg, sizeof(PingMsg));
akashvibhute 11:17b932171aee 374 // We fill the buffer with numbers for the payload
akashvibhute 11:17b932171aee 375 for( i = sizeof(PingMsg); i < BufferSize; i++ )
akashvibhute 11:17b932171aee 376 {
akashvibhute 11:17b932171aee 377 Buffer[i] = i - sizeof(PingMsg);
akashvibhute 11:17b932171aee 378 }
akashvibhute 11:17b932171aee 379 //wait_ms( 10 );
akashvibhute 11:17b932171aee 380 Radio->Send( Buffer, BufferSize );
akashvibhute 11:17b932171aee 381 }
akashvibhute 11:17b932171aee 382 else if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 )
akashvibhute 11:17b932171aee 383 { // A master already exists then become a slave
akashvibhute 11:17b932171aee 384 dprintf( "...Ping" );
akashvibhute 11:17b932171aee 385 *led = !*led;
akashvibhute 11:17b932171aee 386 isMaster = false;
akashvibhute 11:17b932171aee 387 // Send the next PONG frame
akashvibhute 11:17b932171aee 388 memcpy(Buffer, PongMsg, sizeof(PongMsg));
akashvibhute 11:17b932171aee 389 // We fill the buffer with numbers for the payload
akashvibhute 11:17b932171aee 390 for( i = sizeof(PongMsg); i < BufferSize; i++ )
akashvibhute 11:17b932171aee 391 {
akashvibhute 11:17b932171aee 392 Buffer[i] = i - sizeof(PongMsg);
akashvibhute 11:17b932171aee 393 }
akashvibhute 11:17b932171aee 394 //wait_ms( 10 );
akashvibhute 11:17b932171aee 395 Radio->Send( Buffer, BufferSize );
akashvibhute 11:17b932171aee 396 }
akashvibhute 11:17b932171aee 397 else // valid reception but neither a PING or a PONG message
akashvibhute 11:17b932171aee 398 { // Set device as master ans start again
akashvibhute 11:17b932171aee 399 isMaster = true;
akashvibhute 11:17b932171aee 400 Radio->Rx( RX_TIMEOUT_VALUE );
akashvibhute 11:17b932171aee 401 }
akashvibhute 11:17b932171aee 402 }
akashvibhute 11:17b932171aee 403 }
akashvibhute 11:17b932171aee 404 else
akashvibhute 11:17b932171aee 405 {
akashvibhute 11:17b932171aee 406 if( BufferSize > 0 )
akashvibhute 11:17b932171aee 407 {
akashvibhute 11:17b932171aee 408 if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 )
akashvibhute 11:17b932171aee 409 {
akashvibhute 11:17b932171aee 410 *led = !*led;
akashvibhute 11:17b932171aee 411 dprintf( "...Ping" );
akashvibhute 11:17b932171aee 412 // Send the reply to the PING string
akashvibhute 11:17b932171aee 413 memcpy(Buffer, PongMsg, sizeof(PongMsg));
akashvibhute 11:17b932171aee 414 // We fill the buffer with numbers for the payload
akashvibhute 11:17b932171aee 415 for( i = sizeof(PongMsg); i < BufferSize; i++ )
akashvibhute 11:17b932171aee 416 {
akashvibhute 11:17b932171aee 417 Buffer[i] = i - sizeof(PongMsg);
akashvibhute 11:17b932171aee 418 }
akashvibhute 11:17b932171aee 419 //wait_ms( 10 );
akashvibhute 11:17b932171aee 420 Radio->Send( Buffer, BufferSize );
akashvibhute 11:17b932171aee 421 }
akashvibhute 11:17b932171aee 422 else // valid reception but not a PING as expected
akashvibhute 11:17b932171aee 423 { // Set device as master and start again
akashvibhute 11:17b932171aee 424 isMaster = true;
akashvibhute 11:17b932171aee 425 Radio->Rx( RX_TIMEOUT_VALUE );
akashvibhute 11:17b932171aee 426 }
akashvibhute 11:17b932171aee 427 }
akashvibhute 11:17b932171aee 428 }
akashvibhute 11:17b932171aee 429 State = LOWPOWER;
akashvibhute 11:17b932171aee 430 break;
akashvibhute 11:17b932171aee 431 case TX:
akashvibhute 11:17b932171aee 432 *led3 = 1;
akashvibhute 11:17b932171aee 433 if( isMaster == true )
akashvibhute 11:17b932171aee 434 {
akashvibhute 11:17b932171aee 435 dprintf("Ping..." );
akashvibhute 11:17b932171aee 436 }
akashvibhute 11:17b932171aee 437 else
akashvibhute 11:17b932171aee 438 {
akashvibhute 11:17b932171aee 439 dprintf("Pong..." );
akashvibhute 11:17b932171aee 440 }
akashvibhute 11:17b932171aee 441 Radio->Rx( RX_TIMEOUT_VALUE );
akashvibhute 11:17b932171aee 442 State = LOWPOWER;
akashvibhute 11:17b932171aee 443 break;
akashvibhute 11:17b932171aee 444 case RX_TIMEOUT:
akashvibhute 11:17b932171aee 445 if( isMaster == true )
akashvibhute 11:17b932171aee 446 {
akashvibhute 11:17b932171aee 447 // Send the next PING frame
akashvibhute 11:17b932171aee 448 memcpy(Buffer, PingMsg, sizeof(PingMsg));
akashvibhute 11:17b932171aee 449 for( i = sizeof(PingMsg); i < BufferSize; i++ )
akashvibhute 11:17b932171aee 450 {
akashvibhute 11:17b932171aee 451 Buffer[i] = i - sizeof(PingMsg);
akashvibhute 11:17b932171aee 452 }
akashvibhute 11:17b932171aee 453 //wait_ms( 10 );
akashvibhute 11:17b932171aee 454 Radio->Send( Buffer, BufferSize );
akashvibhute 11:17b932171aee 455 }
akashvibhute 11:17b932171aee 456 else
akashvibhute 11:17b932171aee 457 {
akashvibhute 11:17b932171aee 458 Radio->Rx( RX_TIMEOUT_VALUE );
akashvibhute 11:17b932171aee 459 }
akashvibhute 11:17b932171aee 460 State = LOWPOWER;
akashvibhute 11:17b932171aee 461 break;
akashvibhute 11:17b932171aee 462 case RX_ERROR:
akashvibhute 11:17b932171aee 463 // We have received a Packet with a CRC error, send reply as if packet was correct
akashvibhute 11:17b932171aee 464 if( isMaster == true )
akashvibhute 11:17b932171aee 465 {
akashvibhute 11:17b932171aee 466 // Send the next PING frame
akashvibhute 11:17b932171aee 467 memcpy(Buffer, PingMsg, sizeof(PingMsg));
akashvibhute 11:17b932171aee 468 for( i = 4; i < BufferSize; i++ )
akashvibhute 11:17b932171aee 469 {
akashvibhute 11:17b932171aee 470 Buffer[i] = i - 4;
akashvibhute 11:17b932171aee 471 }
akashvibhute 11:17b932171aee 472 //wait_ms( 10 );
akashvibhute 11:17b932171aee 473 Radio->Send( Buffer, BufferSize );
akashvibhute 11:17b932171aee 474 }
akashvibhute 11:17b932171aee 475 else
akashvibhute 11:17b932171aee 476 {
akashvibhute 11:17b932171aee 477 // Send the next PONG frame
akashvibhute 11:17b932171aee 478 memcpy(Buffer, PongMsg, sizeof(PongMsg));
akashvibhute 11:17b932171aee 479 for( i = sizeof(PongMsg); i < BufferSize; i++ )
akashvibhute 11:17b932171aee 480 {
akashvibhute 11:17b932171aee 481 Buffer[i] = i - sizeof(PongMsg);
akashvibhute 11:17b932171aee 482 }
akashvibhute 11:17b932171aee 483 //wait_ms( 10 );
akashvibhute 11:17b932171aee 484 Radio->Send( Buffer, BufferSize );
akashvibhute 11:17b932171aee 485 }
akashvibhute 11:17b932171aee 486 State = LOWPOWER;
akashvibhute 11:17b932171aee 487 break;
akashvibhute 11:17b932171aee 488 case TX_TIMEOUT:
akashvibhute 11:17b932171aee 489 Radio->Rx( RX_TIMEOUT_VALUE );
akashvibhute 11:17b932171aee 490 State = LOWPOWER;
akashvibhute 11:17b932171aee 491 break;
akashvibhute 11:17b932171aee 492 case LOWPOWER:
akashvibhute 11:17b932171aee 493 sleep();
akashvibhute 11:17b932171aee 494 break;
akashvibhute 11:17b932171aee 495 default:
akashvibhute 11:17b932171aee 496 State = LOWPOWER;
akashvibhute 11:17b932171aee 497 break;
akashvibhute 11:17b932171aee 498 }
akashvibhute 11:17b932171aee 499 }
akashvibhute 11:17b932171aee 500 }
akashvibhute 11:17b932171aee 501
akashvibhute 11:17b932171aee 502 void OnTxDone(void *radio)
akashvibhute 11:17b932171aee 503 {
akashvibhute 11:17b932171aee 504 Radio->Sleep( );
akashvibhute 11:17b932171aee 505 State = TX;
akashvibhute 11:17b932171aee 506 if (DEBUG_MESSAGE)
akashvibhute 11:17b932171aee 507 dprintf("> OnTxDone");
akashvibhute 11:17b932171aee 508 }
akashvibhute 11:17b932171aee 509
akashvibhute 11:17b932171aee 510 void OnRxDone(void *radio, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
akashvibhute 11:17b932171aee 511 {
akashvibhute 11:17b932171aee 512 Radio->Sleep( );
akashvibhute 11:17b932171aee 513 BufferSize = size;
akashvibhute 11:17b932171aee 514 memcpy( Buffer, payload, BufferSize );
akashvibhute 11:17b932171aee 515 State = RX;
akashvibhute 11:17b932171aee 516 if (DEBUG_MESSAGE)
akashvibhute 11:17b932171aee 517 dprintf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d", rssi, snr);
akashvibhute 11:17b932171aee 518 //dump("Data:", payload, size);
akashvibhute 11:17b932171aee 519 //ser->printf("Rx Data ~%x%x%x%x\n", payload[0], payload[1], payload[2], payload[3]);
akashvibhute 11:17b932171aee 520 ser->printf("~%02x%02x%02x%02x\n", Buffer[0], Buffer[1], Buffer[2], Buffer[3]);
akashvibhute 11:17b932171aee 521 }
akashvibhute 11:17b932171aee 522
akashvibhute 11:17b932171aee 523 void OnTxTimeout(void *radio)
akashvibhute 11:17b932171aee 524 {
akashvibhute 11:17b932171aee 525 *led3 = 0;
akashvibhute 11:17b932171aee 526 Radio->Sleep( );
akashvibhute 11:17b932171aee 527 State = TX_TIMEOUT;
akashvibhute 11:17b932171aee 528 if(DEBUG_MESSAGE)
akashvibhute 11:17b932171aee 529 dprintf("> OnTxTimeout");
akashvibhute 11:17b932171aee 530 }
akashvibhute 11:17b932171aee 531
akashvibhute 11:17b932171aee 532 void OnRxTimeout(void *radio)
akashvibhute 11:17b932171aee 533 {
akashvibhute 11:17b932171aee 534 *led3 = 0;
akashvibhute 11:17b932171aee 535 Radio->Sleep( );
akashvibhute 11:17b932171aee 536 Buffer[BufferSize-1] = 0;
akashvibhute 11:17b932171aee 537 State = RX_TIMEOUT;
akashvibhute 11:17b932171aee 538 if (DEBUG_MESSAGE)
akashvibhute 11:17b932171aee 539 dprintf("> OnRxTimeout");
akashvibhute 11:17b932171aee 540 }
akashvibhute 11:17b932171aee 541
akashvibhute 11:17b932171aee 542 void OnRxError(void *radio)
akashvibhute 11:17b932171aee 543 {
akashvibhute 11:17b932171aee 544 Radio->Sleep( );
akashvibhute 11:17b932171aee 545 State = RX_ERROR;
akashvibhute 11:17b932171aee 546 if (DEBUG_MESSAGE)
akashvibhute 11:17b932171aee 547 dprintf("> OnRxError");
akashvibhute 11:17b932171aee 548 }
akashvibhute 11:17b932171aee 549
akashvibhute 11:17b932171aee 550 #endif
akashvibhute 11:17b932171aee 551
akashvibhute 11:17b932171aee 552
akashvibhute 11:17b932171aee 553 int SX1276Comm(bool mode, bool ack)
akashvibhute 11:17b932171aee 554 {
akashvibhute 11:17b932171aee 555
akashvibhute 11:17b932171aee 556
akashvibhute 11:17b932171aee 557 Radio->Rx( RX_TIMEOUT_VALUE );
akashvibhute 11:17b932171aee 558 wait_ms(100);
akashvibhute 11:17b932171aee 559
akashvibhute 11:17b932171aee 560
akashvibhute 11:17b932171aee 561
akashvibhute 11:17b932171aee 562
akashvibhute 11:17b932171aee 563 /*switch( State ) {
akashvibhute 11:17b932171aee 564
akashvibhute 11:17b932171aee 565 case RX:
akashvibhute 11:17b932171aee 566 Radio->Rx( RX_TIMEOUT_VALUE );
akashvibhute 11:17b932171aee 567
akashvibhute 11:17b932171aee 568 //memcpy(Buffer, commData, sizeof(Buffer));
akashvibhute 11:17b932171aee 569 //ser->printf("Rx Data\n\r", Buffer[0], Buffer[1], Buffer[2], Buffer[3]);
akashvibhute 11:17b932171aee 570
akashvibhute 11:17b932171aee 571 //State = LOWPOWER;
akashvibhute 11:17b932171aee 572 break;
akashvibhute 11:17b932171aee 573 case TX:
akashvibhute 11:17b932171aee 574
akashvibhute 11:17b932171aee 575 //State = LOWPOWER;
akashvibhute 11:17b932171aee 576 break;
akashvibhute 11:17b932171aee 577 case RX_TIMEOUT:
akashvibhute 11:17b932171aee 578
akashvibhute 11:17b932171aee 579 Radio->Rx( RX_TIMEOUT_VALUE );
akashvibhute 11:17b932171aee 580 //State = LOWPOWER;
akashvibhute 11:17b932171aee 581 break;
akashvibhute 11:17b932171aee 582 case RX_ERROR:
akashvibhute 11:17b932171aee 583
akashvibhute 11:17b932171aee 584 //wait_ms(10);
akashvibhute 11:17b932171aee 585 //Radio->Rx( RX_TIMEOUT_VALUE );
akashvibhute 11:17b932171aee 586 State = LOWPOWER;
akashvibhute 11:17b932171aee 587 break;
akashvibhute 11:17b932171aee 588 case TX_TIMEOUT:
akashvibhute 11:17b932171aee 589
akashvibhute 11:17b932171aee 590 //State = LOWPOWER;
akashvibhute 11:17b932171aee 591 break;
akashvibhute 11:17b932171aee 592 case LOWPOWER:
akashvibhute 11:17b932171aee 593 sleep();
akashvibhute 11:17b932171aee 594 break;
akashvibhute 11:17b932171aee 595 default:
akashvibhute 11:17b932171aee 596 //State = LOWPOWER;
akashvibhute 11:17b932171aee 597 break;
akashvibhute 11:17b932171aee 598 }
akashvibhute 11:17b932171aee 599 */
akashvibhute 11:17b932171aee 600 }