RX
Dependencies: mbed BufferedSerial SX1276GenericLib X_NUCLEO_IKS01A2
main.cpp@0:674f1e460248, 2019-06-05 (annotated)
- Committer:
- TMRL123
- Date:
- Wed Jun 05 00:23:37 2019 +0000
- Revision:
- 0:674f1e460248
Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TMRL123 | 0:674f1e460248 | 1 | #/* Includes */ |
TMRL123 | 0:674f1e460248 | 2 | #include "mbed.h" /* Mbed include */ |
TMRL123 | 0:674f1e460248 | 3 | |
TMRL123 | 0:674f1e460248 | 4 | /* Lora includes */ |
TMRL123 | 0:674f1e460248 | 5 | #include "PinMap.h" |
TMRL123 | 0:674f1e460248 | 6 | #include "sx1276-mbed-hal.h" |
TMRL123 | 0:674f1e460248 | 7 | |
TMRL123 | 0:674f1e460248 | 8 | /* Serial communication include */ |
TMRL123 | 0:674f1e460248 | 9 | #include "BufferedSerial.h" |
TMRL123 | 0:674f1e460248 | 10 | |
TMRL123 | 0:674f1e460248 | 11 | /* SD card includes */ |
TMRL123 | 0:674f1e460248 | 12 | #include "SDCard_Y.hh" |
TMRL123 | 0:674f1e460248 | 13 | |
TMRL123 | 0:674f1e460248 | 14 | /* Set this flag to display debug messages on the console */ |
TMRL123 | 0:674f1e460248 | 15 | #define DEBUG_MESSAGE |
TMRL123 | 0:674f1e460248 | 16 | |
TMRL123 | 0:674f1e460248 | 17 | |
TMRL123 | 0:674f1e460248 | 18 | /* Definition of the SD card */ |
TMRL123 | 0:674f1e460248 | 19 | #define SPI_FREQUENCY 1000000 |
TMRL123 | 0:674f1e460248 | 20 | |
TMRL123 | 0:674f1e460248 | 21 | /* Definition of the SD card */ |
TMRL123 | 0:674f1e460248 | 22 | //#define SD_EN |
TMRL123 | 0:674f1e460248 | 23 | |
TMRL123 | 0:674f1e460248 | 24 | /* LoRa definitions */ |
TMRL123 | 0:674f1e460248 | 25 | |
TMRL123 | 0:674f1e460248 | 26 | /* Set this flag to display debug messages on the console */ |
TMRL123 | 0:674f1e460248 | 27 | #define DEBUG_MESSAGE |
TMRL123 | 0:674f1e460248 | 28 | |
TMRL123 | 0:674f1e460248 | 29 | /* Set this flag to '1' to use the LoRa modulation */ |
TMRL123 | 0:674f1e460248 | 30 | #define USE_MODEM_LORA 1 |
TMRL123 | 0:674f1e460248 | 31 | #define USE_MODEM_FSK !USE_MODEM_LORA |
TMRL123 | 0:674f1e460248 | 32 | #define RF_FREQUENCY RF_FREQUENCY_915_0 // Hz |
TMRL123 | 0:674f1e460248 | 33 | #define TX_OUTPUT_POWER 14 // 20 dBm |
TMRL123 | 0:674f1e460248 | 34 | |
TMRL123 | 0:674f1e460248 | 35 | #if USE_MODEM_LORA == 1 |
TMRL123 | 0:674f1e460248 | 36 | |
TMRL123 | 0:674f1e460248 | 37 | #define LORA_BANDWIDTH 125000 // LoRa default, details in SX1276::BandwidthMap |
TMRL123 | 0:674f1e460248 | 38 | #define LORA_SPREADING_FACTOR LORA_SF7 |
TMRL123 | 0:674f1e460248 | 39 | #define LORA_CODINGRATE LORA_ERROR_CODING_RATE_4_5 |
TMRL123 | 0:674f1e460248 | 40 | |
TMRL123 | 0:674f1e460248 | 41 | #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx |
TMRL123 | 0:674f1e460248 | 42 | #define LORA_SYMBOL_TIMEOUT 5 // Symbols |
TMRL123 | 0:674f1e460248 | 43 | #define LORA_FIX_LENGTH_PAYLOAD_ON false |
TMRL123 | 0:674f1e460248 | 44 | #define LORA_FHSS_ENABLED false |
TMRL123 | 0:674f1e460248 | 45 | #define LORA_NB_SYMB_HOP 4 |
TMRL123 | 0:674f1e460248 | 46 | #define LORA_IQ_INVERSION_ON false |
TMRL123 | 0:674f1e460248 | 47 | #define LORA_CRC_ENABLED true |
TMRL123 | 0:674f1e460248 | 48 | |
TMRL123 | 0:674f1e460248 | 49 | #endif |
TMRL123 | 0:674f1e460248 | 50 | |
TMRL123 | 0:674f1e460248 | 51 | |
TMRL123 | 0:674f1e460248 | 52 | #define RX_TIMEOUT_VALUE 0 // In ms |
TMRL123 | 0:674f1e460248 | 53 | #define TX_TIMEOUT_VALUE 1000000 // In ms |
TMRL123 | 0:674f1e460248 | 54 | |
TMRL123 | 0:674f1e460248 | 55 | typedef struct { |
TMRL123 | 0:674f1e460248 | 56 | /* Header for identification of updated informations */ |
TMRL123 | 0:674f1e460248 | 57 | bool header [8]; |
TMRL123 | 0:674f1e460248 | 58 | int time; // Time between transmissions |
TMRL123 | 0:674f1e460248 | 59 | int32_t ag[3]; // Acceleration of the accelerometer and gyroscope LSM6DSL |
TMRL123 | 0:674f1e460248 | 60 | int32_t w[3]; // Angular velocity of LSM6DSL |
TMRL123 | 0:674f1e460248 | 61 | int32_t a[3]; // Acceleration of the accelerometer LSM303AGR |
TMRL123 | 0:674f1e460248 | 62 | int32_t m [3]; // Heading of LSM303AGR |
TMRL123 | 0:674f1e460248 | 63 | float p; // Pressure of LPS22HB |
TMRL123 | 0:674f1e460248 | 64 | float temperatureLPS22HB; // Temperature from LPS22HB |
TMRL123 | 0:674f1e460248 | 65 | float humidity; // Humidity of HTS221 |
TMRL123 | 0:674f1e460248 | 66 | float temperatureHTS221; // Temperature from HTS221 |
TMRL123 | 0:674f1e460248 | 67 | unsigned long timeOfWeek; //GPS time of week |
TMRL123 | 0:674f1e460248 | 68 | long timeOfWeekFracPart; // GPS time of week fractional part |
TMRL123 | 0:674f1e460248 | 69 | unsigned char gpsFix; // GPS fix |
TMRL123 | 0:674f1e460248 | 70 | long ecefx; // GPS X posiition |
TMRL123 | 0:674f1e460248 | 71 | long ecefy; // GPS Y posistion |
TMRL123 | 0:674f1e460248 | 72 | long ecefz; // GPS Z postion |
TMRL123 | 0:674f1e460248 | 73 | unsigned long positionAcc3D; // GPS 3D position accuracy |
TMRL123 | 0:674f1e460248 | 74 | long ecefvx; // GPS X velocity |
TMRL123 | 0:674f1e460248 | 75 | long ecefvy; // GPS Y velocity |
TMRL123 | 0:674f1e460248 | 76 | long ecefvz; // GPS Z velocity |
TMRL123 | 0:674f1e460248 | 77 | unsigned long speedAcc; // GPS speed accuracy |
TMRL123 | 0:674f1e460248 | 78 | unsigned char numbSat; // GPS number of satellites conected |
TMRL123 | 0:674f1e460248 | 79 | bool drogueStatus; // Drogue parachute status provided by Avionics |
TMRL123 | 0:674f1e460248 | 80 | bool mainStatus; //Main parachute status provided by Avionics |
TMRL123 | 0:674f1e460248 | 81 | float pressureBar; // Pressure by COTS Altimeter |
TMRL123 | 0:674f1e460248 | 82 | float temperature; // Temperature by COTS Altimeter |
TMRL123 | 0:674f1e460248 | 83 | bool mainStatusCOTS; // Main parachute status provided by COTS Altimeter |
TMRL123 | 0:674f1e460248 | 84 | bool drogueStatusCOTS; // Drogue status provided by COTS Altimeter |
TMRL123 | 0:674f1e460248 | 85 | int16_t timeStamp; //Timestamp from COTS Altimeter |
TMRL123 | 0:674f1e460248 | 86 | int16_t aglAlt; //AGL Altitude from COTS Altimeter |
TMRL123 | 0:674f1e460248 | 87 | int8_t battery; //Battery voltage reading from COTS Altimeter |
TMRL123 | 0:674f1e460248 | 88 | }Data; // Data struct |
TMRL123 | 0:674f1e460248 | 89 | |
TMRL123 | 0:674f1e460248 | 90 | Data data; |
TMRL123 | 0:674f1e460248 | 91 | |
TMRL123 | 0:674f1e460248 | 92 | /* LoRa modem instances and configurations */ |
TMRL123 | 0:674f1e460248 | 93 | |
TMRL123 | 0:674f1e460248 | 94 | static RadioEvents_t RadioEvents; // Calback functions struct |
TMRL123 | 0:674f1e460248 | 95 | |
TMRL123 | 0:674f1e460248 | 96 | SX1276Generic *Radio; // Definition of a Radio object |
TMRL123 | 0:674f1e460248 | 97 | |
TMRL123 | 0:674f1e460248 | 98 | /* Configuration function */ |
TMRL123 | 0:674f1e460248 | 99 | void SystemClock_Config(void); |
TMRL123 | 0:674f1e460248 | 100 | |
TMRL123 | 0:674f1e460248 | 101 | bool received = false; // Flag to indicate the end of reception |
TMRL123 | 0:674f1e460248 | 102 | |
TMRL123 | 0:674f1e460248 | 103 | |
TMRL123 | 0:674f1e460248 | 104 | /* Callback functions prototypes */ |
TMRL123 | 0:674f1e460248 | 105 | |
TMRL123 | 0:674f1e460248 | 106 | // Brief Function to be executed on Radio Tx Done event |
TMRL123 | 0:674f1e460248 | 107 | void OnTxDone(void *radio, void *userThisPtr, void *userData); |
TMRL123 | 0:674f1e460248 | 108 | |
TMRL123 | 0:674f1e460248 | 109 | // Brief Function to be executed on Radio Rx Done event |
TMRL123 | 0:674f1e460248 | 110 | void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ); |
TMRL123 | 0:674f1e460248 | 111 | |
TMRL123 | 0:674f1e460248 | 112 | // Brief Function executed on Radio Tx Timeout event |
TMRL123 | 0:674f1e460248 | 113 | void OnTxTimeout(void *radio, void *userThisPtr, void *userData); |
TMRL123 | 0:674f1e460248 | 114 | |
TMRL123 | 0:674f1e460248 | 115 | // Brief Function executed on Radio Rx Timeout event |
TMRL123 | 0:674f1e460248 | 116 | void OnRxTimeout(void *radio, void *userThisPtr, void *userData); |
TMRL123 | 0:674f1e460248 | 117 | |
TMRL123 | 0:674f1e460248 | 118 | // Brief Function executed on Radio Rx Error event |
TMRL123 | 0:674f1e460248 | 119 | void OnRxError(void *radio, void *userThisPtr, void *userData); |
TMRL123 | 0:674f1e460248 | 120 | |
TMRL123 | 0:674f1e460248 | 121 | // Brief Function executed on Radio Fhss Change Channel event |
TMRL123 | 0:674f1e460248 | 122 | void OnFhssChangeChannel(void *radio, void *userThisPtr, void *userData, uint8_t channelIndex); |
TMRL123 | 0:674f1e460248 | 123 | |
TMRL123 | 0:674f1e460248 | 124 | #ifdef DEBUG_MESSAGE |
TMRL123 | 0:674f1e460248 | 125 | /* Serial communication to debug program */ |
TMRL123 | 0:674f1e460248 | 126 | BufferedSerial *ser; |
TMRL123 | 0:674f1e460248 | 127 | #endif |
TMRL123 | 0:674f1e460248 | 128 | |
TMRL123 | 0:674f1e460248 | 129 | int main() { |
TMRL123 | 0:674f1e460248 | 130 | data.header [0] = 0; |
TMRL123 | 0:674f1e460248 | 131 | data.header [1] = 0; |
TMRL123 | 0:674f1e460248 | 132 | data.header [2] = 0; |
TMRL123 | 0:674f1e460248 | 133 | data.header [3] = 0; |
TMRL123 | 0:674f1e460248 | 134 | data.header [4] = 0; |
TMRL123 | 0:674f1e460248 | 135 | data.header [5] = 0; |
TMRL123 | 0:674f1e460248 | 136 | data.header [6] = 0; |
TMRL123 | 0:674f1e460248 | 137 | data.header [7] = 0; |
TMRL123 | 0:674f1e460248 | 138 | data.time = 0; |
TMRL123 | 0:674f1e460248 | 139 | data.ag[0] = 0; |
TMRL123 | 0:674f1e460248 | 140 | data.ag[1] = 0; |
TMRL123 | 0:674f1e460248 | 141 | data.ag[2] = 0; |
TMRL123 | 0:674f1e460248 | 142 | data.w[0] = 0; |
TMRL123 | 0:674f1e460248 | 143 | data.w[1] = 0; |
TMRL123 | 0:674f1e460248 | 144 | data.w[2] = 0; |
TMRL123 | 0:674f1e460248 | 145 | data.a[0] = 0; |
TMRL123 | 0:674f1e460248 | 146 | data.a[1] = 0; |
TMRL123 | 0:674f1e460248 | 147 | data.a[2] = 0; |
TMRL123 | 0:674f1e460248 | 148 | data.m [0] = 0; |
TMRL123 | 0:674f1e460248 | 149 | data.m [1] = 0; |
TMRL123 | 0:674f1e460248 | 150 | data.m [2] = 0; |
TMRL123 | 0:674f1e460248 | 151 | data.p = 0; |
TMRL123 | 0:674f1e460248 | 152 | data.temperatureLPS22HB = 0; |
TMRL123 | 0:674f1e460248 | 153 | data.humidity = 0; |
TMRL123 | 0:674f1e460248 | 154 | data.temperatureHTS221 = 0; |
TMRL123 | 0:674f1e460248 | 155 | data.timeOfWeek = 0; |
TMRL123 | 0:674f1e460248 | 156 | data.timeOfWeekFracPart = 0; |
TMRL123 | 0:674f1e460248 | 157 | data.gpsFix = 0; |
TMRL123 | 0:674f1e460248 | 158 | data.ecefx = 0; |
TMRL123 | 0:674f1e460248 | 159 | data.ecefy = 0; |
TMRL123 | 0:674f1e460248 | 160 | data.ecefz = 0; |
TMRL123 | 0:674f1e460248 | 161 | data.positionAcc3D = 0; |
TMRL123 | 0:674f1e460248 | 162 | data.ecefvx = 0; |
TMRL123 | 0:674f1e460248 | 163 | data.ecefvy = 0; |
TMRL123 | 0:674f1e460248 | 164 | data.ecefvz = 0; |
TMRL123 | 0:674f1e460248 | 165 | data.speedAcc = 0; |
TMRL123 | 0:674f1e460248 | 166 | data.numbSat = 0; |
TMRL123 | 0:674f1e460248 | 167 | data.drogueStatus = 0; |
TMRL123 | 0:674f1e460248 | 168 | data.mainStatus = 0; |
TMRL123 | 0:674f1e460248 | 169 | data.pressureBar = 0; |
TMRL123 | 0:674f1e460248 | 170 | data.temperature = 0; |
TMRL123 | 0:674f1e460248 | 171 | data.mainStatusCOTS = 0; |
TMRL123 | 0:674f1e460248 | 172 | data.drogueStatusCOTS = 0; |
TMRL123 | 0:674f1e460248 | 173 | data.timeStamp = 0; |
TMRL123 | 0:674f1e460248 | 174 | data.aglAlt = 0; |
TMRL123 | 0:674f1e460248 | 175 | data.battery = 0; |
TMRL123 | 0:674f1e460248 | 176 | |
TMRL123 | 0:674f1e460248 | 177 | SystemClock_Config(); /* Synchronize clock for TX and RX boards */ |
TMRL123 | 0:674f1e460248 | 178 | |
TMRL123 | 0:674f1e460248 | 179 | /* Serial configuration */ |
TMRL123 | 0:674f1e460248 | 180 | #ifdef DEBUG_MESSAGE |
TMRL123 | 0:674f1e460248 | 181 | ser = new BufferedSerial(USBTX, USBRX); |
TMRL123 | 0:674f1e460248 | 182 | ser->baud(115200); |
TMRL123 | 0:674f1e460248 | 183 | ser->format(8); |
TMRL123 | 0:674f1e460248 | 184 | #endif |
TMRL123 | 0:674f1e460248 | 185 | |
TMRL123 | 0:674f1e460248 | 186 | /* General Header*/ |
TMRL123 | 0:674f1e460248 | 187 | #ifdef DEBUG_MESSAGE |
TMRL123 | 0:674f1e460248 | 188 | ser->printf ("Telemetry Rx inicial version program\r\n\r\n"); |
TMRL123 | 0:674f1e460248 | 189 | #endif |
TMRL123 | 0:674f1e460248 | 190 | |
TMRL123 | 0:674f1e460248 | 191 | /* Radio setup */ |
TMRL123 | 0:674f1e460248 | 192 | #ifdef DEBUG_MESSAGE |
TMRL123 | 0:674f1e460248 | 193 | ser->printf("\r\n--- Starting the modem LoRa ---\r\n"); |
TMRL123 | 0:674f1e460248 | 194 | #endif |
TMRL123 | 0:674f1e460248 | 195 | Radio = new SX1276Generic(NULL, MURATA_SX1276, |
TMRL123 | 0:674f1e460248 | 196 | LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, |
TMRL123 | 0:674f1e460248 | 197 | LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5, |
TMRL123 | 0:674f1e460248 | 198 | LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO); |
TMRL123 | 0:674f1e460248 | 199 | #ifdef DEBUG_MESSAGE |
TMRL123 | 0:674f1e460248 | 200 | ser->printf("SX1276 Simple transmission aplication\r\n" ); |
TMRL123 | 0:674f1e460248 | 201 | ser->printf("Frequency: %.1f\r\n", (double)RF_FREQUENCY/1000000.0); |
TMRL123 | 0:674f1e460248 | 202 | ser->printf("TXPower: %d dBm\r\n", TX_OUTPUT_POWER); |
TMRL123 | 0:674f1e460248 | 203 | ser->printf("Bandwidth: %d Hz\r\n", LORA_BANDWIDTH); |
TMRL123 | 0:674f1e460248 | 204 | ser->printf("Spreading factor: SF%d\r\n", LORA_SPREADING_FACTOR); |
TMRL123 | 0:674f1e460248 | 205 | #endif |
TMRL123 | 0:674f1e460248 | 206 | |
TMRL123 | 0:674f1e460248 | 207 | // Initialize Radio driver |
TMRL123 | 0:674f1e460248 | 208 | RadioEvents.TxDone = OnTxDone; |
TMRL123 | 0:674f1e460248 | 209 | RadioEvents.RxDone = OnRxDone; |
TMRL123 | 0:674f1e460248 | 210 | RadioEvents.RxError = OnRxError; |
TMRL123 | 0:674f1e460248 | 211 | RadioEvents.TxTimeout = OnTxTimeout; |
TMRL123 | 0:674f1e460248 | 212 | RadioEvents.RxTimeout = OnRxTimeout; |
TMRL123 | 0:674f1e460248 | 213 | |
TMRL123 | 0:674f1e460248 | 214 | for (int i = 0; Radio->Init( &RadioEvents ) == false && i < 40; i++) { |
TMRL123 | 0:674f1e460248 | 215 | #ifdef DEBUG_MESSAGE |
TMRL123 | 0:674f1e460248 | 216 | ser->printf("Radio could not be detected!\r\n"); |
TMRL123 | 0:674f1e460248 | 217 | #endif |
TMRL123 | 0:674f1e460248 | 218 | wait_ms(500); |
TMRL123 | 0:674f1e460248 | 219 | } |
TMRL123 | 0:674f1e460248 | 220 | |
TMRL123 | 0:674f1e460248 | 221 | // Display the board type |
TMRL123 | 0:674f1e460248 | 222 | #ifdef DEBUG_MESSAGE |
TMRL123 | 0:674f1e460248 | 223 | switch(Radio->DetectBoardType()) { |
TMRL123 | 0:674f1e460248 | 224 | case SX1276MB1LAS: |
TMRL123 | 0:674f1e460248 | 225 | ser->printf(" > Board Type: SX1276MB1LAS <\r\n"); |
TMRL123 | 0:674f1e460248 | 226 | break; |
TMRL123 | 0:674f1e460248 | 227 | case SX1276MB1MAS: |
TMRL123 | 0:674f1e460248 | 228 | ser->printf(" > Board Type: SX1276MB1LAS <\r\n"); |
TMRL123 | 0:674f1e460248 | 229 | break; |
TMRL123 | 0:674f1e460248 | 230 | case MURATA_SX1276: |
TMRL123 | 0:674f1e460248 | 231 | ser->printf(" > Board Type: MURATA_SX1276_STM32L0 <\r\n"); |
TMRL123 | 0:674f1e460248 | 232 | break; |
TMRL123 | 0:674f1e460248 | 233 | case RFM95_SX1276: |
TMRL123 | 0:674f1e460248 | 234 | ser->printf(" > HopeRF RFM95xx <\r\n"); |
TMRL123 | 0:674f1e460248 | 235 | break; |
TMRL123 | 0:674f1e460248 | 236 | default: |
TMRL123 | 0:674f1e460248 | 237 | ser->printf(" > Board Type: unknown <\r\n"); |
TMRL123 | 0:674f1e460248 | 238 | } |
TMRL123 | 0:674f1e460248 | 239 | #endif |
TMRL123 | 0:674f1e460248 | 240 | Radio->SetChannel(RF_FREQUENCY ); // Sets the frequency of the communication |
TMRL123 | 0:674f1e460248 | 241 | |
TMRL123 | 0:674f1e460248 | 242 | // Debug message of the state of fhss |
TMRL123 | 0:674f1e460248 | 243 | #ifdef DEBUG_MESSAGE |
TMRL123 | 0:674f1e460248 | 244 | if (LORA_FHSS_ENABLED) { |
TMRL123 | 0:674f1e460248 | 245 | ser->printf(" > LORA FHSS Mode <\r\n"); |
TMRL123 | 0:674f1e460248 | 246 | } |
TMRL123 | 0:674f1e460248 | 247 | if (!LORA_FHSS_ENABLED) { |
TMRL123 | 0:674f1e460248 | 248 | ser->printf(" > LORA Mode <\r\n"); |
TMRL123 | 0:674f1e460248 | 249 | } |
TMRL123 | 0:674f1e460248 | 250 | #endif |
TMRL123 | 0:674f1e460248 | 251 | // Sets the configuration of the transmission |
TMRL123 | 0:674f1e460248 | 252 | Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, |
TMRL123 | 0:674f1e460248 | 253 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, |
TMRL123 | 0:674f1e460248 | 254 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
TMRL123 | 0:674f1e460248 | 255 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
TMRL123 | 0:674f1e460248 | 256 | LORA_IQ_INVERSION_ON, 2000 ); |
TMRL123 | 0:674f1e460248 | 257 | |
TMRL123 | 0:674f1e460248 | 258 | // Sets the configuration of the reception |
TMRL123 | 0:674f1e460248 | 259 | Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
TMRL123 | 0:674f1e460248 | 260 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
TMRL123 | 0:674f1e460248 | 261 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, |
TMRL123 | 0:674f1e460248 | 262 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
TMRL123 | 0:674f1e460248 | 263 | LORA_IQ_INVERSION_ON, true ); |
TMRL123 | 0:674f1e460248 | 264 | |
TMRL123 | 0:674f1e460248 | 265 | #ifdef SD_EN |
TMRL123 | 0:674f1e460248 | 266 | SPI spi (PA_7, PA_6, PA_5); |
TMRL123 | 0:674f1e460248 | 267 | spi.frequency (SPI_FREQUENCY); |
TMRL123 | 0:674f1e460248 | 268 | SDCard sdCard (&spi, PB_10); |
TMRL123 | 0:674f1e460248 | 269 | uint8_t sdData[sizeof(data)]; |
TMRL123 | 0:674f1e460248 | 270 | int block = 0; |
TMRL123 | 0:674f1e460248 | 271 | #endif |
TMRL123 | 0:674f1e460248 | 272 | |
TMRL123 | 0:674f1e460248 | 273 | Radio->Rx(RX_TIMEOUT_VALUE); // Puts the device in reception mode continuously |
TMRL123 | 0:674f1e460248 | 274 | while( 1 ) |
TMRL123 | 0:674f1e460248 | 275 | { |
TMRL123 | 0:674f1e460248 | 276 | //After the receiving, puts the device again in receive mode |
TMRL123 | 0:674f1e460248 | 277 | if (received == true) { |
TMRL123 | 0:674f1e460248 | 278 | #ifdef SD_EN |
TMRL123 | 0:674f1e460248 | 279 | // Saving the data on SD Card |
TMRL123 | 0:674f1e460248 | 280 | memcpy (sdData, &data, sizeof(data)); |
TMRL123 | 0:674f1e460248 | 281 | while(sdCard.write(&sdData[0],block) == 0); |
TMRL123 | 0:674f1e460248 | 282 | block ++; |
TMRL123 | 0:674f1e460248 | 283 | while (sdCard.write (&sdData[64],block) == 0); |
TMRL123 | 0:674f1e460248 | 284 | block++; |
TMRL123 | 0:674f1e460248 | 285 | while (sdCard.write(&sdData[128],block) == 0); |
TMRL123 | 0:674f1e460248 | 286 | block++; |
TMRL123 | 0:674f1e460248 | 287 | #endif |
TMRL123 | 0:674f1e460248 | 288 | #ifdef DEBUG_MESSAGE |
TMRL123 | 0:674f1e460248 | 289 | ser->printf("I received %d mg, %d mg, %d mg, %d mg, %d mg, %d mg, %d mdps, %d mdps, %d mdps\r\n", data.a[0], data.a[1], data.a[2], data.ag[0], data.ag[1], data.ag[2], data.w[0], data.w[1], data.w[2]); |
TMRL123 | 0:674f1e460248 | 290 | ser->printf("and %d mG, %d mG, %d mG, %g %%, %g C, %g C, %g mBar\r\n", data.m[0], data.m[1], data.m[2], data.humidity, data.temperatureHTS221, data.temperatureLPS22HB, data.p); |
TMRL123 | 0:674f1e460248 | 291 | ser->printf("and time - %d ms, time of the week - %d ms, time of week frac part - %d ns\r\n",data.time, data.timeOfWeek, data.timeOfWeekFracPart); |
TMRL123 | 0:674f1e460248 | 292 | ser->printf("and GPS fix %c, ECEFX %d cm, ECEFY %d cm, ECEFZ %d cm\r\n", data.gpsFix, data.ecefx, data.ecefy, data.ecefz); |
TMRL123 | 0:674f1e460248 | 293 | ser->printf("and 3D Position accuracy %d cm, ECEFVX %d cm/s, ECEFVY %d cm/s, ECEFVZ %d cm/s, Speed accuracy %d cm/s\r\n", data.positionAcc3D, data.ecefvx, data.ecefvy, data.ecefvz, data.speedAcc); |
TMRL123 | 0:674f1e460248 | 294 | ser->printf("and Number of satelites %x, Drogue Status %x, Main status %x, BMP280 %f bar, temperature %f C\r\n", data.numbSat, data.drogueStatus, data.mainStatus, data.pressureBar, data.temperature); |
TMRL123 | 0:674f1e460248 | 295 | ser->printf("Main Status COTS %x, Drogue Status COTS %x, Time Stamp %d s, AGL altitude %d m, Battery voltage %d V\r\n",data.mainStatusCOTS, data.drogueStatusCOTS, data.timeStamp, data.aglAlt, data.battery); |
TMRL123 | 0:674f1e460248 | 296 | ser->printf("Header: %d, %d, %d, %d, %d, %d, %d, %d\r\n",data.header[0], data.header[1], data.header[2], data.header[3], data.header[4], data.header[5], data.header[6], data.header[7]); |
TMRL123 | 0:674f1e460248 | 297 | #endif |
TMRL123 | 0:674f1e460248 | 298 | received = false; |
TMRL123 | 0:674f1e460248 | 299 | Radio->Rx(RX_TIMEOUT_VALUE); |
TMRL123 | 0:674f1e460248 | 300 | } |
TMRL123 | 0:674f1e460248 | 301 | } |
TMRL123 | 0:674f1e460248 | 302 | } |
TMRL123 | 0:674f1e460248 | 303 | |
TMRL123 | 0:674f1e460248 | 304 | |
TMRL123 | 0:674f1e460248 | 305 | void SystemClock_Config(void) |
TMRL123 | 0:674f1e460248 | 306 | { |
TMRL123 | 0:674f1e460248 | 307 | #ifdef B_L072Z_LRWAN1_LORA |
TMRL123 | 0:674f1e460248 | 308 | /* |
TMRL123 | 0:674f1e460248 | 309 | * The L072Z_LRWAN1_LORA clock setup is somewhat differnt from the Nucleo board. |
TMRL123 | 0:674f1e460248 | 310 | * It has no LSE. |
TMRL123 | 0:674f1e460248 | 311 | */ |
TMRL123 | 0:674f1e460248 | 312 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
TMRL123 | 0:674f1e460248 | 313 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
TMRL123 | 0:674f1e460248 | 314 | |
TMRL123 | 0:674f1e460248 | 315 | /* Enable HSE Oscillator and Activate PLL with HSE as source */ |
TMRL123 | 0:674f1e460248 | 316 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
TMRL123 | 0:674f1e460248 | 317 | RCC_OscInitStruct.HSEState = RCC_HSE_OFF; |
TMRL123 | 0:674f1e460248 | 318 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
TMRL123 | 0:674f1e460248 | 319 | RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; |
TMRL123 | 0:674f1e460248 | 320 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
TMRL123 | 0:674f1e460248 | 321 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
TMRL123 | 0:674f1e460248 | 322 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_6; |
TMRL123 | 0:674f1e460248 | 323 | RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_3; |
TMRL123 | 0:674f1e460248 | 324 | |
TMRL123 | 0:674f1e460248 | 325 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
TMRL123 | 0:674f1e460248 | 326 | // Error_Handler(); |
TMRL123 | 0:674f1e460248 | 327 | } |
TMRL123 | 0:674f1e460248 | 328 | |
TMRL123 | 0:674f1e460248 | 329 | /* Set Voltage scale1 as MCU will run at 32MHz */ |
TMRL123 | 0:674f1e460248 | 330 | __HAL_RCC_PWR_CLK_ENABLE(); |
TMRL123 | 0:674f1e460248 | 331 | __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
TMRL123 | 0:674f1e460248 | 332 | |
TMRL123 | 0:674f1e460248 | 333 | /* Poll VOSF bit of in PWR_CSR. Wait until it is reset to 0 */ |
TMRL123 | 0:674f1e460248 | 334 | while (__HAL_PWR_GET_FLAG(PWR_FLAG_VOS) != RESET) {}; |
TMRL123 | 0:674f1e460248 | 335 | |
TMRL123 | 0:674f1e460248 | 336 | /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 |
TMRL123 | 0:674f1e460248 | 337 | clocks dividers */ |
TMRL123 | 0:674f1e460248 | 338 | RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); |
TMRL123 | 0:674f1e460248 | 339 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
TMRL123 | 0:674f1e460248 | 340 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
TMRL123 | 0:674f1e460248 | 341 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; |
TMRL123 | 0:674f1e460248 | 342 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
TMRL123 | 0:674f1e460248 | 343 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { |
TMRL123 | 0:674f1e460248 | 344 | // Error_Handler(); |
TMRL123 | 0:674f1e460248 | 345 | } |
TMRL123 | 0:674f1e460248 | 346 | #endif |
TMRL123 | 0:674f1e460248 | 347 | } |
TMRL123 | 0:674f1e460248 | 348 | |
TMRL123 | 0:674f1e460248 | 349 | void OnTxDone(void *radio, void *userThisPtr, void *userData) |
TMRL123 | 0:674f1e460248 | 350 | { |
TMRL123 | 0:674f1e460248 | 351 | Radio->Sleep( ); |
TMRL123 | 0:674f1e460248 | 352 | #ifdef DEBUG_MESSAGE |
TMRL123 | 0:674f1e460248 | 353 | ser->printf("> OnTxDone\r\n"); |
TMRL123 | 0:674f1e460248 | 354 | #endif |
TMRL123 | 0:674f1e460248 | 355 | } |
TMRL123 | 0:674f1e460248 | 356 | |
TMRL123 | 0:674f1e460248 | 357 | void OnTxTimeout(void *radio, void *userThisPtr, void *userData) |
TMRL123 | 0:674f1e460248 | 358 | { |
TMRL123 | 0:674f1e460248 | 359 | Radio->Sleep( ); |
TMRL123 | 0:674f1e460248 | 360 | #ifdef DEBUG_MESSAGE |
TMRL123 | 0:674f1e460248 | 361 | ser->printf("> OnTxTimeout\r\n"); |
TMRL123 | 0:674f1e460248 | 362 | #endif |
TMRL123 | 0:674f1e460248 | 363 | } |
TMRL123 | 0:674f1e460248 | 364 | |
TMRL123 | 0:674f1e460248 | 365 | void OnRxTimeout(void *radio, void *userThisPtr, void *userData) |
TMRL123 | 0:674f1e460248 | 366 | { |
TMRL123 | 0:674f1e460248 | 367 | Radio->Sleep( ); |
TMRL123 | 0:674f1e460248 | 368 | #ifdef DEBUG_MESSAGE |
TMRL123 | 0:674f1e460248 | 369 | ser->printf("> OnRxTimeout\r\n"); |
TMRL123 | 0:674f1e460248 | 370 | #endif |
TMRL123 | 0:674f1e460248 | 371 | } |
TMRL123 | 0:674f1e460248 | 372 | |
TMRL123 | 0:674f1e460248 | 373 | void OnRxError(void *radio, void *userThisPtr, void *userData) |
TMRL123 | 0:674f1e460248 | 374 | { |
TMRL123 | 0:674f1e460248 | 375 | Radio->Sleep( ); |
TMRL123 | 0:674f1e460248 | 376 | received = true; |
TMRL123 | 0:674f1e460248 | 377 | #ifdef DEBUG_MESSAGE |
TMRL123 | 0:674f1e460248 | 378 | ser->printf("> OnRxError\r\n"); |
TMRL123 | 0:674f1e460248 | 379 | #endif |
TMRL123 | 0:674f1e460248 | 380 | } |
TMRL123 | 0:674f1e460248 | 381 | |
TMRL123 | 0:674f1e460248 | 382 | void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) |
TMRL123 | 0:674f1e460248 | 383 | { |
TMRL123 | 0:674f1e460248 | 384 | Radio->Sleep( ); |
TMRL123 | 0:674f1e460248 | 385 | received = true; |
TMRL123 | 0:674f1e460248 | 386 | memcpy(&data, payload, sizeof(data)); |
TMRL123 | 0:674f1e460248 | 387 | /* #ifdef DEBUG_MESSAGE |
TMRL123 | 0:674f1e460248 | 388 | ser->printf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d\r\n", rssi, snr); |
TMRL123 | 0:674f1e460248 | 389 | ser->printf("I received %d mg, %d mg, %d mg, %d mg, %d mg, %d mg, %d mdps, %d mdps, %d mdps\r\n", data.a[0], data.a[1], data.a[2], data.ag[0], data.ag[1], data.ag[2], data.w[0], data.w[1], data.w[2]); |
TMRL123 | 0:674f1e460248 | 390 | ser->printf("and %d mG, %d mG, %d mG, %g %%, %g C, %g C, %g mBar\r\n", data.m[0], data.m[1], data.m[2], data.humidity, data.temperatureHTS221, data.temperatureLPS22HB, data.p); |
TMRL123 | 0:674f1e460248 | 391 | ser->printf("and time - %d ms, time of the week - %d ms, time of week frac part - %d ns\r\n",data.time, data.timeOfWeek, data.timeOfWeekFracPart); |
TMRL123 | 0:674f1e460248 | 392 | ser->printf("and GPS fix %c, ECEFX %d cm, ECEFY %d cm, ECEFZ %d cm\r\n", data.gpsFix, data.ecefx, data.ecefy, data.ecefz); |
TMRL123 | 0:674f1e460248 | 393 | ser->printf("and 3D Position accuracy %d cm, ECEFVX %d cm/s, ECEFVY %d cm/s, ECEFVZ %d cm/s, Speed accuracy %d cm/s\r\n", data.positionAcc3D, data.ecefvx, data.ecefvy, data.ecefvz, data.speedAcc); |
TMRL123 | 0:674f1e460248 | 394 | ser->printf("and Number of satelites %x, Drogue Status %x, Main status %x, BMP280 %f bar, temperature %f C\r\n", data.numbSat, data.drogueStatus, data.mainStatus, data.pressureBar, data.temperature); |
TMRL123 | 0:674f1e460248 | 395 | ser->printf("Main Status COTS %x, Drogue Status COTS %x, Time Stamp %d s, AGL altitude %d m, Battery voltage %d V\r\n",data.mainStatusCOTS, data.drogueStatusCOTS, data.timeStamp, data.aglAlt, data.battery); |
TMRL123 | 0:674f1e460248 | 396 | ser->printf("Header: %d, %d, %d, %d, %d, %d, %d, %d\r\n",data.header[0], data.header[1], data.header[2], data.header[3], data.header[4], data.header[5], data.header[6], data.header[7]); |
TMRL123 | 0:674f1e460248 | 397 | #endif */ |
TMRL123 | 0:674f1e460248 | 398 | } |
TMRL123 | 0:674f1e460248 | 399 |