TX

Dependencies:   mbed BufferedSerial SX1276GenericLib X_NUCLEO_IKS01A2

Committer:
LucasKB
Date:
Mon Jun 17 00:07:48 2019 +0000
Revision:
2:91a80cc7d87d
Parent:
1:bd8b9ad01400
Tx

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TMRL123 0:a73914f20498 1 /* Includes */
TMRL123 0:a73914f20498 2 #include "mbed.h" /* Mbed include */
TMRL123 0:a73914f20498 3
TMRL123 0:a73914f20498 4 #include "XNucleoIKS01A2.h" /* Sensors include*/
TMRL123 0:a73914f20498 5
TMRL123 0:a73914f20498 6 /* LoRa includes */
TMRL123 0:a73914f20498 7 #include "PinMap.h"
TMRL123 0:a73914f20498 8 #include "sx1276-mbed-hal.h"
TMRL123 0:a73914f20498 9
LucasKB 2:91a80cc7d87d 10 /* Serial communication include */
LucasKB 2:91a80cc7d87d 11 #include "BufferedSerial.h"
LucasKB 2:91a80cc7d87d 12
LucasKB 2:91a80cc7d87d 13 /* SD card includes */
LucasKB 2:91a80cc7d87d 14 #include "SDCard_Y.hh"
LucasKB 2:91a80cc7d87d 15
LucasKB 2:91a80cc7d87d 16 /* GPS include */
LucasKB 2:91a80cc7d87d 17 #include "UbxGpsNavSol.hh"
LucasKB 2:91a80cc7d87d 18
LucasKB 2:91a80cc7d87d 19
LucasKB 2:91a80cc7d87d 20 /* GPS definitions */
LucasKB 2:91a80cc7d87d 21 #define GPS_BAUDRATE 115200
LucasKB 2:91a80cc7d87d 22 //#define GPS_EN
LucasKB 2:91a80cc7d87d 23
LucasKB 2:91a80cc7d87d 24 /* Definition of buzzer time in ms */
LucasKB 2:91a80cc7d87d 25 #define BUZZER_TIME 500
LucasKB 2:91a80cc7d87d 26
LucasKB 2:91a80cc7d87d 27 /* Definition of Disable enable flag */
LucasKB 2:91a80cc7d87d 28 //#define KEY_ENABLE
LucasKB 2:91a80cc7d87d 29
LucasKB 2:91a80cc7d87d 30 /* Definition of the SD card */
LucasKB 2:91a80cc7d87d 31 #define SPI_FREQUENCY 1000000
LucasKB 2:91a80cc7d87d 32
LucasKB 2:91a80cc7d87d 33 /* Definition of the SD card */
LucasKB 2:91a80cc7d87d 34 //#define SD_EN
LucasKB 2:91a80cc7d87d 35
TMRL123 0:a73914f20498 36 /* LoRa definitions */
TMRL123 0:a73914f20498 37
LucasKB 2:91a80cc7d87d 38 /* Set this flag to display debug messages on the console */
LucasKB 2:91a80cc7d87d 39 #define DEBUG_MESSAGE
TMRL123 0:a73914f20498 40
LucasKB 2:91a80cc7d87d 41 /* Set this flag to '1' to use the LoRa modulation */
LucasKB 2:91a80cc7d87d 42 #define USE_MODEM_LORA 1
LucasKB 2:91a80cc7d87d 43 #define USE_MODEM_FSK !USE_MODEM_LORA
TMRL123 0:a73914f20498 44 #define RF_FREQUENCY RF_FREQUENCY_915_0 // Hz
LucasKB 2:91a80cc7d87d 45 #define TX_OUTPUT_POWER 14 // 20 dBm
TMRL123 0:a73914f20498 46
TMRL123 0:a73914f20498 47 #if USE_MODEM_LORA == 1
TMRL123 0:a73914f20498 48
TMRL123 0:a73914f20498 49 #define LORA_BANDWIDTH 125000 // LoRa default, details in SX1276::BandwidthMap
TMRL123 0:a73914f20498 50 #define LORA_SPREADING_FACTOR LORA_SF7
TMRL123 0:a73914f20498 51 #define LORA_CODINGRATE LORA_ERROR_CODING_RATE_4_5
TMRL123 0:a73914f20498 52
TMRL123 0:a73914f20498 53 #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
TMRL123 0:a73914f20498 54 #define LORA_SYMBOL_TIMEOUT 5 // Symbols
TMRL123 0:a73914f20498 55 #define LORA_FIX_LENGTH_PAYLOAD_ON false
TMRL123 0:a73914f20498 56 #define LORA_FHSS_ENABLED false
TMRL123 0:a73914f20498 57 #define LORA_NB_SYMB_HOP 4
TMRL123 0:a73914f20498 58 #define LORA_IQ_INVERSION_ON false
TMRL123 0:a73914f20498 59 #define LORA_CRC_ENABLED true
TMRL123 0:a73914f20498 60
TMRL123 0:a73914f20498 61 #endif
TMRL123 0:a73914f20498 62
TMRL123 0:a73914f20498 63
LucasKB 2:91a80cc7d87d 64 #define RX_TIMEOUT_VALUE 0 // In ms
LucasKB 2:91a80cc7d87d 65 #define TX_TIMEOUT_VALUE 1000000 // In ms
LucasKB 2:91a80cc7d87d 66 #define BUFFER_SIZE 64 // In bytes
TMRL123 0:a73914f20498 67
TMRL123 0:a73914f20498 68 /* Sensors instances */
TMRL123 0:a73914f20498 69
LucasKB 2:91a80cc7d87d 70
TMRL123 0:a73914f20498 71 /* Instantiate the expansion board */
TMRL123 0:a73914f20498 72 static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
TMRL123 0:a73914f20498 73
TMRL123 0:a73914f20498 74 /* Retrieve the composing elements of the expansion board */
TMRL123 0:a73914f20498 75 static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer;
TMRL123 0:a73914f20498 76 static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
TMRL123 0:a73914f20498 77 static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor;
TMRL123 0:a73914f20498 78 static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
TMRL123 0:a73914f20498 79 static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer;
TMRL123 0:a73914f20498 80
LucasKB 2:91a80cc7d87d 81 typedef struct {
LucasKB 2:91a80cc7d87d 82 uint8_t header; // Header for identification of updated informations - 0 0 p temp LSM6DSL LSM303AGR
LucasKB 2:91a80cc7d87d 83 int time; // Time between transmissions
LucasKB 2:91a80cc7d87d 84 float p; // Pressure of LPS22HB
LucasKB 2:91a80cc7d87d 85 float temperatureLPS22HB; // Temperature from LPS22HB
LucasKB 2:91a80cc7d87d 86 int32_t ag[3]; // Acceleration of the accelerometer and gyroscope LSM6DSL
LucasKB 2:91a80cc7d87d 87 int32_t w[3]; // Angular velocity of LSM6DSL
LucasKB 2:91a80cc7d87d 88 int32_t a[3]; // Acceleration of the accelerometer LSM303AGR
LucasKB 2:91a80cc7d87d 89 int32_t m[3]; // Heading of LSM303AGR
LucasKB 2:91a80cc7d87d 90 }Pkg1;
LucasKB 2:91a80cc7d87d 91
LucasKB 1:bd8b9ad01400 92 typedef struct {
LucasKB 2:91a80cc7d87d 93 uint8_t header; // Header for identification of updated informations - 0 1 InternalCommunication HTS221
LucasKB 2:91a80cc7d87d 94 int time; // Time between transmissions
LucasKB 2:91a80cc7d87d 95 bool drogueStatus; // Drogue parachute status provided by Avionics
LucasKB 2:91a80cc7d87d 96 bool mainStatus; //Main parachute status provided by Avionics
LucasKB 2:91a80cc7d87d 97 bool mainStatusCOTS; // Main parachute status provided by COTS Altimeter
LucasKB 2:91a80cc7d87d 98 bool drogueStatusCOTS; // Drogue status provided by COTS Altimeter
LucasKB 2:91a80cc7d87d 99 float pressureBar; // Pressure by COTS Altimeter
LucasKB 2:91a80cc7d87d 100 float temperature; // Temperature by COTS Altimeter
LucasKB 2:91a80cc7d87d 101 int16_t timeStamp; // Timestamp from COTS Altimeter
LucasKB 2:91a80cc7d87d 102 int16_t aglAlt; // AGL Altitude from COTS Altimeter
LucasKB 2:91a80cc7d87d 103 int8_t battery; // Battery voltage reading from COTS Altimeter
LucasKB 2:91a80cc7d87d 104 float humidity; // Humidity of HTS221
LucasKB 2:91a80cc7d87d 105 float temperatureHTS221; // Temperature from HTS221
LucasKB 2:91a80cc7d87d 106 //uint8_t filler[25];
LucasKB 2:91a80cc7d87d 107 }Pkg2;
LucasKB 2:91a80cc7d87d 108
LucasKB 2:91a80cc7d87d 109 typedef struct {
LucasKB 2:91a80cc7d87d 110 uint8_t header; // Header for identification of updated informations - 1 0 GPS
LucasKB 2:91a80cc7d87d 111 int time; // Time between transmissions
LucasKB 2:91a80cc7d87d 112 unsigned long timeOfWeek; //GPS time of week
LucasKB 2:91a80cc7d87d 113 long timeOfWeekFracPart; // GPS time of week fractional part
LucasKB 2:91a80cc7d87d 114 unsigned char gpsFix; // GPS fix
LucasKB 2:91a80cc7d87d 115 long ecefx; // GPS X posiition
LucasKB 2:91a80cc7d87d 116 long ecefy; // GPS Y posistion
LucasKB 2:91a80cc7d87d 117 long ecefz; // GPS Z postion
LucasKB 2:91a80cc7d87d 118 unsigned long positionAcc3D; // GPS 3D position accuracy
LucasKB 2:91a80cc7d87d 119 long ecefvx; // GPS X velocity
LucasKB 2:91a80cc7d87d 120 long ecefvy; // GPS Y velocity
LucasKB 2:91a80cc7d87d 121 long ecefvz; // GPS Z velocity
LucasKB 2:91a80cc7d87d 122 unsigned long speedAcc; // GPS speed accuracy
LucasKB 2:91a80cc7d87d 123 unsigned char numbSat; // GPS number of satellites conected
LucasKB 2:91a80cc7d87d 124 //uint8_t filler[8];
LucasKB 2:91a80cc7d87d 125 }Pkg3;
LucasKB 2:91a80cc7d87d 126
LucasKB 2:91a80cc7d87d 127
LucasKB 2:91a80cc7d87d 128 union Data {
LucasKB 2:91a80cc7d87d 129 Pkg1 pkg1;
LucasKB 2:91a80cc7d87d 130 Pkg2 pkg2;
LucasKB 2:91a80cc7d87d 131 Pkg3 pkg3;
LucasKB 2:91a80cc7d87d 132 };
LucasKB 2:91a80cc7d87d 133
LucasKB 2:91a80cc7d87d 134 Data data;
LucasKB 2:91a80cc7d87d 135
TMRL123 0:a73914f20498 136
TMRL123 0:a73914f20498 137 /* LoRa modem instances and configurations */
TMRL123 0:a73914f20498 138
TMRL123 0:a73914f20498 139 static RadioEvents_t RadioEvents; // Calback functions struct
TMRL123 0:a73914f20498 140
LucasKB 2:91a80cc7d87d 141 SX1276Generic *Radio; // Definition of a Radio object
TMRL123 0:a73914f20498 142
LucasKB 2:91a80cc7d87d 143 /* Configuration function */
TMRL123 0:a73914f20498 144 void SystemClock_Config(void);
TMRL123 0:a73914f20498 145
LucasKB 2:91a80cc7d87d 146 bool transmited = true;// Flag to indicate the end of transmission
LucasKB 2:91a80cc7d87d 147 uint8_t state = 0; // Flag to indicate state of transmission, i.e. which package is to be transmited
TMRL123 0:a73914f20498 148
TMRL123 0:a73914f20498 149 /* Callback functions prototypes */
LucasKB 2:91a80cc7d87d 150
LucasKB 2:91a80cc7d87d 151 // Brief Function to be executed on Radio Tx Done event
TMRL123 0:a73914f20498 152 void OnTxDone(void *radio, void *userThisPtr, void *userData);
TMRL123 0:a73914f20498 153
LucasKB 2:91a80cc7d87d 154 // Brief Function to be executed on Radio Rx Done event
TMRL123 0:a73914f20498 155 void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
TMRL123 0:a73914f20498 156
LucasKB 2:91a80cc7d87d 157 // Brief Function executed on Radio Tx Timeout event
TMRL123 0:a73914f20498 158 void OnTxTimeout(void *radio, void *userThisPtr, void *userData);
TMRL123 0:a73914f20498 159
LucasKB 2:91a80cc7d87d 160 // Brief Function executed on Radio Rx Timeout event
TMRL123 0:a73914f20498 161 void OnRxTimeout(void *radio, void *userThisPtr, void *userData);
TMRL123 0:a73914f20498 162
LucasKB 2:91a80cc7d87d 163 // Brief Function executed on Radio Rx Error event
TMRL123 0:a73914f20498 164 void OnRxError(void *radio, void *userThisPtr, void *userData);
TMRL123 0:a73914f20498 165
LucasKB 2:91a80cc7d87d 166 // Brief Function executed on Radio Fhss Change Channel event
TMRL123 0:a73914f20498 167 void OnFhssChangeChannel(void *radio, void *userThisPtr, void *userData, uint8_t channelIndex);
TMRL123 0:a73914f20498 168
LucasKB 2:91a80cc7d87d 169 #ifdef DEBUG_MESSAGE
TMRL123 0:a73914f20498 170 /* Serial communication to debug program */
LucasKB 2:91a80cc7d87d 171 BufferedSerial *ser;
LucasKB 2:91a80cc7d87d 172 #endif
TMRL123 0:a73914f20498 173
LucasKB 2:91a80cc7d87d 174 /* Buzzer definition */
LucasKB 2:91a80cc7d87d 175 DigitalOut buzzer (PA_0);
LucasKB 2:91a80cc7d87d 176
LucasKB 2:91a80cc7d87d 177 Timer timer2;
TMRL123 0:a73914f20498 178
TMRL123 0:a73914f20498 179 int main() {
LucasKB 2:91a80cc7d87d 180 #ifdef SD_EN
LucasKB 2:91a80cc7d87d 181 Timer timerSD;
LucasKB 2:91a80cc7d87d 182 uint8_t bufferSD[512]; // Buffer to store data to save in SD card
LucasKB 2:91a80cc7d87d 183 uint8_t bufferSDCounter = 0; // Counter to access correct position of bufferSD
LucasKB 2:91a80cc7d87d 184 for(int i = 0; i < 512; i++){
LucasKB 2:91a80cc7d87d 185 bufferSD[i] = 0;
LucasKB 2:91a80cc7d87d 186 }
LucasKB 2:91a80cc7d87d 187 timerSD.start();
LucasKB 2:91a80cc7d87d 188 #endif
LucasKB 2:91a80cc7d87d 189 timer2.start();
LucasKB 2:91a80cc7d87d 190 buzzer = 0;
LucasKB 2:91a80cc7d87d 191 /* Power on*/
LucasKB 2:91a80cc7d87d 192 buzzer = 1;
LucasKB 2:91a80cc7d87d 193 wait_ms (BUZZER_TIME);
LucasKB 2:91a80cc7d87d 194 buzzer = 0;
LucasKB 2:91a80cc7d87d 195 wait_ms (BUZZER_TIME);
LucasKB 2:91a80cc7d87d 196 /* Set to zero all parameters of the data struct */
LucasKB 2:91a80cc7d87d 197 /*
LucasKB 2:91a80cc7d87d 198 pkg1.header = 0x00;
LucasKB 2:91a80cc7d87d 199 pkg2.header = 0x40;
LucasKB 2:91a80cc7d87d 200 pkg3.header = 0x80;
LucasKB 2:91a80cc7d87d 201 pkg1.time = 0;
LucasKB 2:91a80cc7d87d 202 pkg2.time = 0;
LucasKB 2:91a80cc7d87d 203 pkg3.time = 0;
LucasKB 2:91a80cc7d87d 204 pkg1.ag[0] = 0;
LucasKB 2:91a80cc7d87d 205 pkg1.ag[1] = 0;
LucasKB 2:91a80cc7d87d 206 pkg1.ag[2] = 0;
LucasKB 2:91a80cc7d87d 207 pkg1.w[0] = 0;
LucasKB 2:91a80cc7d87d 208 pkg1.w[1] = 0;
LucasKB 2:91a80cc7d87d 209 pkg1.w[2] = 0;
LucasKB 2:91a80cc7d87d 210 pkg1.a[0] = 0;
LucasKB 2:91a80cc7d87d 211 pkg1.a[1] = 0;
LucasKB 2:91a80cc7d87d 212 pkg1.a[2] = 0;
LucasKB 2:91a80cc7d87d 213 pkg1.m [0] = 0;
LucasKB 2:91a80cc7d87d 214 pkg1.m [1] = 0;
LucasKB 2:91a80cc7d87d 215 pkg1.m [2] = 0;
LucasKB 2:91a80cc7d87d 216 pkg1.p = 0;
LucasKB 2:91a80cc7d87d 217 pkg1.temperatureLPS22HB = 0;
LucasKB 2:91a80cc7d87d 218 pkg2.humidity = 0;
LucasKB 2:91a80cc7d87d 219 pkg2.temperatureHTS221 = 0;
LucasKB 2:91a80cc7d87d 220 pkg3.timeOfWeek = 0;
LucasKB 2:91a80cc7d87d 221 pkg3.timeOfWeekFracPart = 0;
LucasKB 2:91a80cc7d87d 222 pkg3.gpsFix = 0;
LucasKB 2:91a80cc7d87d 223 pkg3.ecefx = 0;
LucasKB 2:91a80cc7d87d 224 pkg3.ecefy = 0;
LucasKB 2:91a80cc7d87d 225 pkg3.ecefz = 0;
LucasKB 2:91a80cc7d87d 226 pkg3.positionAcc3D = 0;
LucasKB 2:91a80cc7d87d 227 pkg3.ecefvx = 0;
LucasKB 2:91a80cc7d87d 228 pkg3.ecefvy = 0;
LucasKB 2:91a80cc7d87d 229 pkg3.ecefvz = 0;
LucasKB 2:91a80cc7d87d 230 pkg3.speedAcc = 0;
LucasKB 2:91a80cc7d87d 231 pkg3.numbSat = 0;
LucasKB 2:91a80cc7d87d 232 pkg2.drogueStatus = 0;
LucasKB 2:91a80cc7d87d 233 pkg2.mainStatus = 0;
LucasKB 2:91a80cc7d87d 234 pkg2.pressureBar = 0;
LucasKB 2:91a80cc7d87d 235 pkg2.temperature = 0;
LucasKB 2:91a80cc7d87d 236 pkg2.mainStatusCOTS = 0;
LucasKB 2:91a80cc7d87d 237 pkg2.drogueStatusCOTS = 0;
LucasKB 2:91a80cc7d87d 238 pkg2.timeStamp = 0;
LucasKB 2:91a80cc7d87d 239 pkg2.aglAlt = 0;
LucasKB 2:91a80cc7d87d 240 pkg2.battery = 0;
TMRL123 0:a73914f20498 241
LucasKB 2:91a80cc7d87d 242 for(int i = 0; i < sizeof(pkg2.filler); i++){
LucasKB 2:91a80cc7d87d 243 pkg2.filler[i] = 0;
LucasKB 2:91a80cc7d87d 244 }
LucasKB 2:91a80cc7d87d 245 for(int i = 0; i < sizeof(pkg3.filler); i++){
LucasKB 2:91a80cc7d87d 246 pkg3.filler[i] = 0;
LucasKB 2:91a80cc7d87d 247 }
LucasKB 2:91a80cc7d87d 248 */
LucasKB 2:91a80cc7d87d 249
LucasKB 2:91a80cc7d87d 250 #ifdef GPS_EN
LucasKB 2:91a80cc7d87d 251 //Serial connections (GPS)(TX,RX,baud)
LucasKB 2:91a80cc7d87d 252 UbxGpsNavSol gps(PA_9, PA_10, GPS_BAUDRATE);
LucasKB 2:91a80cc7d87d 253 #endif
TMRL123 0:a73914f20498 254
LucasKB 2:91a80cc7d87d 255 SystemClock_Config(); /* Synchronize clock for TX and RX boards */
LucasKB 2:91a80cc7d87d 256
LucasKB 2:91a80cc7d87d 257 /* Serial configuration */
LucasKB 2:91a80cc7d87d 258 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 259 ser = new BufferedSerial(USBTX, USBRX);
LucasKB 2:91a80cc7d87d 260 ser->baud(115200);
LucasKB 2:91a80cc7d87d 261 ser->format(8);
LucasKB 2:91a80cc7d87d 262 #endif
LucasKB 2:91a80cc7d87d 263
LucasKB 2:91a80cc7d87d 264 /* General Header*/
LucasKB 2:91a80cc7d87d 265 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 266 ser->printf ("Telemetry Tx inicial version program\r\n\r\n");
TMRL123 0:a73914f20498 267 uint8_t id; //Sensor id parameter for debug purpose
LucasKB 2:91a80cc7d87d 268 #endif
TMRL123 0:a73914f20498 269
TMRL123 0:a73914f20498 270 /* Enable all sensors */
LucasKB 2:91a80cc7d87d 271 if (hum_temp->enable() != 0) {
LucasKB 2:91a80cc7d87d 272 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 273 ser->printf ("Humidity sensor not enabled\r\n");
LucasKB 2:91a80cc7d87d 274 #endif
LucasKB 2:91a80cc7d87d 275 }
LucasKB 2:91a80cc7d87d 276 if (press_temp->enable() != 0) {
LucasKB 2:91a80cc7d87d 277 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 278 ser->printf ("Temperature sensor not enabled\r\n");
LucasKB 2:91a80cc7d87d 279 #endif
LucasKB 2:91a80cc7d87d 280 }
LucasKB 2:91a80cc7d87d 281 if (magnetometer->enable() != 0) {
LucasKB 2:91a80cc7d87d 282 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 283 ser->printf ("Magnetometer sensor not enabled\r\n");
LucasKB 2:91a80cc7d87d 284 #endif
LucasKB 2:91a80cc7d87d 285 }
LucasKB 2:91a80cc7d87d 286 if (accelerometer->enable() != 0) {
LucasKB 2:91a80cc7d87d 287 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 288 ser->printf ("Accelerometer1 sensor not enabled\r\n");
LucasKB 2:91a80cc7d87d 289 #endif
LucasKB 2:91a80cc7d87d 290 }
LucasKB 2:91a80cc7d87d 291 if (acc_gyro->enable_x() != 0) {
LucasKB 2:91a80cc7d87d 292 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 293 ser->printf ("Gyroscope sensor not enabled\r\n");
LucasKB 2:91a80cc7d87d 294 #endif
LucasKB 2:91a80cc7d87d 295 }
LucasKB 2:91a80cc7d87d 296 if (acc_gyro->enable_g() != 0) {
LucasKB 2:91a80cc7d87d 297 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 298 ser->printf ("Accelerometer2 sensor not enabled\r\n");
LucasKB 2:91a80cc7d87d 299 #endif
LucasKB 2:91a80cc7d87d 300 }
TMRL123 0:a73914f20498 301
LucasKB 2:91a80cc7d87d 302 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 303 ser->printf("\r\n--- Starting the sensors ---\r\n");
LucasKB 2:91a80cc7d87d 304
TMRL123 0:a73914f20498 305 hum_temp->read_id(&id);
LucasKB 2:91a80cc7d87d 306 ser->printf("HTS221 humidity & temperature = 0x%X\r\n", id);
TMRL123 0:a73914f20498 307 press_temp->read_id(&id);
LucasKB 2:91a80cc7d87d 308 ser->printf("LPS22HB pressure & temperature = 0x%X\r\n", id);
TMRL123 0:a73914f20498 309 magnetometer->read_id(&id);
LucasKB 2:91a80cc7d87d 310 ser->printf("LSM303AGR magnetometer = 0x%X\r\n", id);
TMRL123 0:a73914f20498 311 accelerometer->read_id(&id);
LucasKB 2:91a80cc7d87d 312 ser->printf("LSM303AGR accelerometer = 0x%X\r\n", id);
TMRL123 0:a73914f20498 313 acc_gyro->read_id(&id);
LucasKB 2:91a80cc7d87d 314 ser->printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id);
LucasKB 2:91a80cc7d87d 315
LucasKB 2:91a80cc7d87d 316 ser->printf("\r\n");
LucasKB 2:91a80cc7d87d 317 #endif
TMRL123 0:a73914f20498 318
LucasKB 2:91a80cc7d87d 319 /* Radio setup */
LucasKB 2:91a80cc7d87d 320 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 321 ser->printf("\r\n--- Starting the modem LoRa ---\r\n");
LucasKB 2:91a80cc7d87d 322 #endif
TMRL123 0:a73914f20498 323 Radio = new SX1276Generic(NULL, MURATA_SX1276,
TMRL123 0:a73914f20498 324 LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET,
TMRL123 0:a73914f20498 325 LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5,
TMRL123 0:a73914f20498 326 LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO);
LucasKB 2:91a80cc7d87d 327 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 328 ser->printf("SX1276 Simple transmission aplication\r\n" );
LucasKB 2:91a80cc7d87d 329 ser->printf("Frequency: %.1f\r\n", (double)RF_FREQUENCY/1000000.0);
LucasKB 2:91a80cc7d87d 330 ser->printf("TXPower: %d dBm\r\n", TX_OUTPUT_POWER);
LucasKB 2:91a80cc7d87d 331 ser->printf("Bandwidth: %d Hz\r\n", LORA_BANDWIDTH);
LucasKB 2:91a80cc7d87d 332 ser->printf("Spreading factor: SF%d\r\n", LORA_SPREADING_FACTOR);
LucasKB 2:91a80cc7d87d 333 #endif
TMRL123 0:a73914f20498 334
TMRL123 0:a73914f20498 335 // Initialize Radio driver
TMRL123 0:a73914f20498 336 RadioEvents.TxDone = OnTxDone;
TMRL123 0:a73914f20498 337 RadioEvents.RxDone = OnRxDone;
TMRL123 0:a73914f20498 338 RadioEvents.RxError = OnRxError;
TMRL123 0:a73914f20498 339 RadioEvents.TxTimeout = OnTxTimeout;
LucasKB 2:91a80cc7d87d 340 RadioEvents.RxTimeout = OnRxTimeout;
LucasKB 2:91a80cc7d87d 341
LucasKB 2:91a80cc7d87d 342 for (int i = 0; Radio->Init( &RadioEvents ) != true && i < 40; i++) {
LucasKB 2:91a80cc7d87d 343 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 344 ser->printf("Radio could not be detected!\r\n");
LucasKB 2:91a80cc7d87d 345 #endif
LucasKB 2:91a80cc7d87d 346 buzzer = 1;
LucasKB 2:91a80cc7d87d 347 wait_ms (BUZZER_TIME);
LucasKB 2:91a80cc7d87d 348 buzzer = 0;
LucasKB 2:91a80cc7d87d 349 wait_ms (BUZZER_TIME);
TMRL123 0:a73914f20498 350 }
TMRL123 0:a73914f20498 351
LucasKB 2:91a80cc7d87d 352 // Display the board type
LucasKB 2:91a80cc7d87d 353 #ifdef DEBUG_MESSAGE
TMRL123 0:a73914f20498 354 switch(Radio->DetectBoardType()) {
TMRL123 0:a73914f20498 355 case SX1276MB1LAS:
LucasKB 2:91a80cc7d87d 356 ser->printf(" > Board Type: SX1276MB1LAS <\r\n");
TMRL123 0:a73914f20498 357 break;
TMRL123 0:a73914f20498 358 case SX1276MB1MAS:
LucasKB 2:91a80cc7d87d 359 ser->printf(" > Board Type: SX1276MB1LAS <\r\n");
LucasKB 2:91a80cc7d87d 360 break;
TMRL123 0:a73914f20498 361 case MURATA_SX1276:
LucasKB 2:91a80cc7d87d 362 ser->printf(" > Board Type: MURATA_SX1276_STM32L0 <\r\n");
TMRL123 0:a73914f20498 363 break;
TMRL123 0:a73914f20498 364 case RFM95_SX1276:
LucasKB 2:91a80cc7d87d 365 ser->printf(" > HopeRF RFM95xx <\r\n");
TMRL123 0:a73914f20498 366 break;
TMRL123 0:a73914f20498 367 default:
LucasKB 2:91a80cc7d87d 368 ser->printf(" > Board Type: unknown <\r\n");
TMRL123 0:a73914f20498 369 }
LucasKB 2:91a80cc7d87d 370 #endif
LucasKB 2:91a80cc7d87d 371 Radio->SetChannel(RF_FREQUENCY ); // Sets the frequency of the communication
TMRL123 0:a73914f20498 372
LucasKB 2:91a80cc7d87d 373 // Debug message of the state of fhss
LucasKB 2:91a80cc7d87d 374 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 375 if (LORA_FHSS_ENABLED) {
LucasKB 2:91a80cc7d87d 376 ser->printf(" > LORA FHSS Mode <\r\n");
LucasKB 2:91a80cc7d87d 377 }
LucasKB 2:91a80cc7d87d 378 if (!LORA_FHSS_ENABLED) {
LucasKB 2:91a80cc7d87d 379 ser->printf(" > LORA Mode <\r\n");
LucasKB 2:91a80cc7d87d 380 }
LucasKB 2:91a80cc7d87d 381 #endif
LucasKB 2:91a80cc7d87d 382 // Sets the configuration of the transmission
TMRL123 0:a73914f20498 383 Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
TMRL123 0:a73914f20498 384 LORA_SPREADING_FACTOR, LORA_CODINGRATE,
TMRL123 0:a73914f20498 385 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
TMRL123 0:a73914f20498 386 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
TMRL123 0:a73914f20498 387 LORA_IQ_INVERSION_ON, 2000 );
TMRL123 0:a73914f20498 388
LucasKB 2:91a80cc7d87d 389 // Sets the configuration of the reception
TMRL123 0:a73914f20498 390 Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
TMRL123 0:a73914f20498 391 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
TMRL123 0:a73914f20498 392 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
TMRL123 0:a73914f20498 393 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
TMRL123 0:a73914f20498 394 LORA_IQ_INVERSION_ON, true );
TMRL123 0:a73914f20498 395
LucasKB 2:91a80cc7d87d 396
LucasKB 2:91a80cc7d87d 397 #ifdef SD_EN
LucasKB 2:91a80cc7d87d 398 SPI spi (PA_7, PA_6, PA_5);
LucasKB 2:91a80cc7d87d 399 spi.frequency (SPI_FREQUENCY);
LucasKB 2:91a80cc7d87d 400 SDCard sdCard (&spi, PB_10);
LucasKB 2:91a80cc7d87d 401 uint8_t sdData[BUFFER_SIZE];
LucasKB 2:91a80cc7d87d 402 int block = 0;
LucasKB 2:91a80cc7d87d 403 #endif
LucasKB 2:91a80cc7d87d 404
TMRL123 0:a73914f20498 405
LucasKB 2:91a80cc7d87d 406 Radio->Tx(TX_TIMEOUT_VALUE); // Puts the device in transmission mode for a long period
LucasKB 1:bd8b9ad01400 407
LucasKB 2:91a80cc7d87d 408 Timer timerHTS221; // Timer for HTS221
LucasKB 2:91a80cc7d87d 409 Timer timerGPS; // Timer for GPS
LucasKB 2:91a80cc7d87d 410 timerHTS221.start(); // Starting timer for humidity sensor
LucasKB 2:91a80cc7d87d 411 timerGPS.start();
LucasKB 2:91a80cc7d87d 412
LucasKB 2:91a80cc7d87d 413 //----------------> Starting the main loop <----------------
LucasKB 2:91a80cc7d87d 414 while(1) { // 1
TMRL123 0:a73914f20498 415
LucasKB 2:91a80cc7d87d 416 if(state == 0){ // Update and send pkg1 // 2
LucasKB 2:91a80cc7d87d 417 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 418 //ser->printf(" Here\r\n");
LucasKB 2:91a80cc7d87d 419 #endif
LucasKB 2:91a80cc7d87d 420 // Initialization of pkg1
LucasKB 2:91a80cc7d87d 421 data.pkg1.header = 0xC0;
LucasKB 2:91a80cc7d87d 422 data.pkg1.time = 0;
LucasKB 2:91a80cc7d87d 423 data.pkg1.ag[0] = 0;
LucasKB 2:91a80cc7d87d 424 data.pkg1.ag[1] = 0;
LucasKB 2:91a80cc7d87d 425 data.pkg1.ag[2] = 0;
LucasKB 2:91a80cc7d87d 426 data.pkg1.w[0] = 0;
LucasKB 2:91a80cc7d87d 427 data.pkg1.w[1] = 0;
LucasKB 2:91a80cc7d87d 428 data.pkg1.w[2] = 0;
LucasKB 2:91a80cc7d87d 429 data.pkg1.a[0] = 0;
LucasKB 2:91a80cc7d87d 430 data.pkg1.a[1] = 0;
LucasKB 2:91a80cc7d87d 431 data.pkg1.a[2] = 0;
LucasKB 2:91a80cc7d87d 432 data.pkg1.m [0] = 0;
LucasKB 2:91a80cc7d87d 433 data.pkg1.m [1] = 0;
LucasKB 2:91a80cc7d87d 434 data.pkg1.m [2] = 0;
LucasKB 2:91a80cc7d87d 435 data.pkg1.p = 0;
LucasKB 2:91a80cc7d87d 436 data.pkg1.temperatureLPS22HB = 0;
LucasKB 2:91a80cc7d87d 437
LucasKB 2:91a80cc7d87d 438 if (press_temp->get_pressure(&data.pkg1.p) == 0) { // Get the pressure // 3
LucasKB 2:91a80cc7d87d 439 data.pkg1.header |= 0xE0; // LPS22HB updated
LucasKB 2:91a80cc7d87d 440 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 441 //ser->printf("The pressure data from LPS22HB was read\r\n");
LucasKB 2:91a80cc7d87d 442 #endif
LucasKB 2:91a80cc7d87d 443 } else { // 3 4
LucasKB 2:91a80cc7d87d 444 data.pkg1.header &= 0xDC; // LPS22HB was not updated
LucasKB 2:91a80cc7d87d 445 } // 4
LucasKB 2:91a80cc7d87d 446
LucasKB 2:91a80cc7d87d 447 if (press_temp->get_temperature(&data.pkg1.temperatureLPS22HB) == 0) { // Get temperature from LPS22HB // 5
LucasKB 2:91a80cc7d87d 448 data.pkg1.header |= 0xD0; // LPS22HB updated
LucasKB 2:91a80cc7d87d 449 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 450 //ser->printf("The temperature data from LPS22HB was read\r\n");
LucasKB 2:91a80cc7d87d 451 #endif
LucasKB 2:91a80cc7d87d 452 } else { // 5 6
LucasKB 2:91a80cc7d87d 453 data.pkg1.header &= 0xEC; // LPS22HB not updated
LucasKB 2:91a80cc7d87d 454 } // 6
LucasKB 2:91a80cc7d87d 455
LucasKB 2:91a80cc7d87d 456 if (accelerometer->get_x_axes(data.pkg1.a) == 0) {// Get the acceleration // 7
LucasKB 2:91a80cc7d87d 457 data.pkg1.header |= 0xC4; // LSM303AGR updated
LucasKB 2:91a80cc7d87d 458 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 459 //ser->printf("The acceleration data from LSM303AGR was read\r\n");
LucasKB 2:91a80cc7d87d 460 #endif
LucasKB 2:91a80cc7d87d 461 } else { // 7 8
LucasKB 2:91a80cc7d87d 462 data.pkg1.header &= 0xF8; // LSM303AGR not updated
LucasKB 2:91a80cc7d87d 463 } // 8
LucasKB 2:91a80cc7d87d 464
LucasKB 2:91a80cc7d87d 465 if (acc_gyro->get_x_axes(data.pkg1.ag) == 0) {// Get the acceleration // 9
LucasKB 2:91a80cc7d87d 466 data.pkg1.header |= 0xC8; // LSM6DSL updated
LucasKB 2:91a80cc7d87d 467 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 468 //ser->printf("The acceleration data from LSM6DSL was read\r\n");
LucasKB 2:91a80cc7d87d 469 #endif
LucasKB 2:91a80cc7d87d 470 } else { // 9 10
LucasKB 2:91a80cc7d87d 471 data.pkg1.header &= 0xF4; // LSM6DSL not updated
LucasKB 2:91a80cc7d87d 472 } // 10
LucasKB 2:91a80cc7d87d 473
LucasKB 2:91a80cc7d87d 474 if (acc_gyro->get_g_axes(data.pkg1.w) == 0) {// Get the angular velocity // 11
LucasKB 2:91a80cc7d87d 475 data.pkg1.header |= 0xC8; // LSM6DSL updated
LucasKB 2:91a80cc7d87d 476 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 477 //ser->printf("The angular velocity data from LSM6DSL was read\r\n");
LucasKB 2:91a80cc7d87d 478 #endif
LucasKB 2:91a80cc7d87d 479 } else { //11 12
LucasKB 2:91a80cc7d87d 480 data.pkg1.header &= 0xF4; // LSM6DSL not updated
LucasKB 2:91a80cc7d87d 481 } // 12
LucasKB 2:91a80cc7d87d 482
LucasKB 2:91a80cc7d87d 483 if (magnetometer->get_m_axes(data.pkg1.m) == 0) { // Get the magnetometer heading // 13
LucasKB 2:91a80cc7d87d 484 data.pkg1.header |= 0xC4; // LSM303AGR updated
LucasKB 2:91a80cc7d87d 485 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 486 //ser->printf("The heading data from LSM6DSL was read\r\n");
LucasKB 2:91a80cc7d87d 487 #endif
LucasKB 2:91a80cc7d87d 488 } else { // 13 14
LucasKB 2:91a80cc7d87d 489 data.pkg1.header &= 0xF8; // LSM303AGR not updated
LucasKB 2:91a80cc7d87d 490 } // 14
LucasKB 2:91a80cc7d87d 491
LucasKB 2:91a80cc7d87d 492 #ifdef SD_EN
LucasKB 2:91a80cc7d87d 493 data.pkg1.time = timerSD.read_ms();
LucasKB 2:91a80cc7d87d 494 memcpy(&bufferSD[bufferSDCounter], &data, BUFFER_SIZE);
LucasKB 2:91a80cc7d87d 495 bufferSDCounter += BUFFER_SIZE;
LucasKB 2:91a80cc7d87d 496 timerSD.reset();
LucasKB 2:91a80cc7d87d 497 #endif
LucasKB 2:91a80cc7d87d 498 data.pkg1.time = timer2.read_ms();
LucasKB 2:91a80cc7d87d 499 if(transmited){ // Only sends a new packet when the device already have transmited the previous one //15
LucasKB 2:91a80cc7d87d 500 transmited = false;
LucasKB 2:91a80cc7d87d 501 Radio->Send( &data, BUFFER_SIZE );
LucasKB 2:91a80cc7d87d 502 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 503 //ser->printf(" Transmited = true, State = %d\r\n", state);
LucasKB 2:91a80cc7d87d 504 ser->printf("%x,%d,%f,%f,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n", data.pkg1.header, data.pkg1.time, data.pkg1.p, data.pkg1.temperatureLPS22HB, data.pkg1.ag[0], data.pkg1.ag[1], data.pkg1.ag[2], data.pkg1.w[0], data.pkg1.w[1], data.pkg1.w[2], data.pkg1.a[0], data.pkg1.a[1], data.pkg1.a[2], data.pkg1.m[0], data.pkg1.m[1], data.pkg1.m[2]);
LucasKB 2:91a80cc7d87d 505 #endif
LucasKB 2:91a80cc7d87d 506 state = 1;
LucasKB 2:91a80cc7d87d 507 } // 15
LucasKB 2:91a80cc7d87d 508 } // 2
TMRL123 0:a73914f20498 509
LucasKB 2:91a80cc7d87d 510 else if(state == 1){ // Update and send pkg2 // 16
LucasKB 2:91a80cc7d87d 511 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 512 //ser->printf(" Here1\r\n");
LucasKB 2:91a80cc7d87d 513 #endif
LucasKB 2:91a80cc7d87d 514 // Initialization of pkg2
LucasKB 2:91a80cc7d87d 515 data.pkg2.header = 0x40;
LucasKB 2:91a80cc7d87d 516 data.pkg2.time = 0;
LucasKB 2:91a80cc7d87d 517 data.pkg2.humidity = 0;
LucasKB 2:91a80cc7d87d 518 data.pkg2.temperatureHTS221 = 0;
LucasKB 2:91a80cc7d87d 519 data.pkg2.drogueStatus = 0;
LucasKB 2:91a80cc7d87d 520 data.pkg2.mainStatus = 0;
LucasKB 2:91a80cc7d87d 521 data.pkg2.pressureBar = 0;
LucasKB 2:91a80cc7d87d 522 data.pkg2.temperature = 0;
LucasKB 2:91a80cc7d87d 523 data.pkg2.mainStatusCOTS = 0;
LucasKB 2:91a80cc7d87d 524 data.pkg2.drogueStatusCOTS = 0;
LucasKB 2:91a80cc7d87d 525 data.pkg2.timeStamp = 0;
LucasKB 2:91a80cc7d87d 526 data.pkg2.aglAlt = 0;
LucasKB 2:91a80cc7d87d 527 data.pkg2.battery = 0;
LucasKB 2:91a80cc7d87d 528
LucasKB 2:91a80cc7d87d 529 // Check internal comunication for avionicas data
LucasKB 2:91a80cc7d87d 530 // Implement this part here
LucasKB 2:91a80cc7d87d 531 // ...
LucasKB 2:91a80cc7d87d 532 // end
LucasKB 2:91a80cc7d87d 533
LucasKB 2:91a80cc7d87d 534 if (timerHTS221.read_ms() >= 10) { //17
LucasKB 2:91a80cc7d87d 535 if (hum_temp->get_humidity(&data.pkg2.humidity)) { // Get humidity //18
LucasKB 2:91a80cc7d87d 536 data.pkg2.header |= 0x50; // HTS221 updated
LucasKB 2:91a80cc7d87d 537 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 538 //ser->printf("The humidity data from HTS221 was read\r\n");
LucasKB 2:91a80cc7d87d 539 #endif
LucasKB 2:91a80cc7d87d 540 } else {
LucasKB 2:91a80cc7d87d 541 data.pkg2.header &= 0x60; // HTS221 not updated
LucasKB 2:91a80cc7d87d 542 } // 18
LucasKB 2:91a80cc7d87d 543
LucasKB 2:91a80cc7d87d 544 if (hum_temp->get_temperature(&data.pkg2.temperatureHTS221)== 0) { // Get temperature from HTS221 //19
LucasKB 2:91a80cc7d87d 545 data.pkg2.header |= 0x50; // HTS221 updated
LucasKB 2:91a80cc7d87d 546 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 547 //ser->printf("The temperature data from HTS221 was read\r\n");
LucasKB 2:91a80cc7d87d 548 #endif
LucasKB 2:91a80cc7d87d 549 } else {
LucasKB 2:91a80cc7d87d 550 data.pkg2.header &= 0x60; // HTS221 not updated
LucasKB 2:91a80cc7d87d 551 } //19
LucasKB 2:91a80cc7d87d 552
LucasKB 2:91a80cc7d87d 553 timerHTS221.reset();
LucasKB 2:91a80cc7d87d 554 }//17
LucasKB 2:91a80cc7d87d 555
LucasKB 2:91a80cc7d87d 556 #ifdef SD_EN
LucasKB 2:91a80cc7d87d 557 data.pkg2.time = timerSD.read_ms();
LucasKB 2:91a80cc7d87d 558 memcpy(&bufferSD[bufferSDCounter], &data, BUFFER_SIZE);
LucasKB 2:91a80cc7d87d 559 bufferSDCounter += BUFFER_SIZE;
LucasKB 2:91a80cc7d87d 560 timerSD.reset();
LucasKB 2:91a80cc7d87d 561 #endif
LucasKB 2:91a80cc7d87d 562 data.pkg2.time = timer2.read_ms();
LucasKB 2:91a80cc7d87d 563 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 564 //ser->printf(" Transmited = %d\r\n, State = %d", transmited, state);
LucasKB 2:91a80cc7d87d 565 #endif
LucasKB 2:91a80cc7d87d 566 if(transmited){ // Only sends a new packet when the device already have transmited the previous one //20
LucasKB 2:91a80cc7d87d 567 transmited = false;
LucasKB 2:91a80cc7d87d 568 Radio->Send( &data, BUFFER_SIZE );
LucasKB 2:91a80cc7d87d 569 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 570 //ser->printf(" Transmited = true, State = %d\r\n", state);
LucasKB 2:91a80cc7d87d 571 ser->printf("%x,%d,%d%d%d%d,%f,%f,%d,%d,%d,%f,%f\r\n", data.pkg2.header, data.pkg2.time, data.pkg2.drogueStatus, data.pkg2.mainStatus, data.pkg2.mainStatusCOTS, data.pkg2.drogueStatusCOTS, data.pkg2.pressureBar, data.pkg2.temperature, data.pkg2.timeStamp, data.pkg2.aglAlt, data.pkg2.battery, data.pkg2.humidity, data.pkg2.temperatureHTS221);
LucasKB 2:91a80cc7d87d 572 #endif
LucasKB 2:91a80cc7d87d 573 state = 2;
LucasKB 2:91a80cc7d87d 574 }//20
LucasKB 2:91a80cc7d87d 575 }//16
TMRL123 0:a73914f20498 576
LucasKB 2:91a80cc7d87d 577 else if(state == 2){//21
LucasKB 2:91a80cc7d87d 578 data.pkg3.header = 0x80;
LucasKB 2:91a80cc7d87d 579 data.pkg3.time = 0;
LucasKB 2:91a80cc7d87d 580 data.pkg3.timeOfWeek = 0;
LucasKB 2:91a80cc7d87d 581 data.pkg3.timeOfWeekFracPart = 0;
LucasKB 2:91a80cc7d87d 582 data.pkg3.gpsFix = 0;
LucasKB 2:91a80cc7d87d 583 data.pkg3.ecefx = 0;
LucasKB 2:91a80cc7d87d 584 data.pkg3.ecefy = 0;
LucasKB 2:91a80cc7d87d 585 data.pkg3.ecefz = 0;
LucasKB 2:91a80cc7d87d 586 data.pkg3.positionAcc3D = 0;
LucasKB 2:91a80cc7d87d 587 data.pkg3.ecefvx = 0;
LucasKB 2:91a80cc7d87d 588 data.pkg3.ecefvy = 0;
LucasKB 2:91a80cc7d87d 589 data.pkg3.ecefvz = 0;
LucasKB 2:91a80cc7d87d 590 data.pkg3.speedAcc = 0;
LucasKB 2:91a80cc7d87d 591 data.pkg3.numbSat = 0;
LucasKB 2:91a80cc7d87d 592
LucasKB 2:91a80cc7d87d 593 if (timerGPS.read_ms() >= 10) {//22
LucasKB 2:91a80cc7d87d 594 #ifdef GPS_EN
LucasKB 2:91a80cc7d87d 595 if (gps.readable()) {//23
LucasKB 2:91a80cc7d87d 596 if (gps.ready()) {//24
LucasKB 2:91a80cc7d87d 597 data.pkg3.ecefx = gps.ecefX;
LucasKB 2:91a80cc7d87d 598 data.pkg3.ecefy = gps.ecefY;
LucasKB 2:91a80cc7d87d 599 data.pkg3.ecefz = gps.ecefZ;
LucasKB 2:91a80cc7d87d 600 data.pkg3.ecefvx = gps.ecefVX;
LucasKB 2:91a80cc7d87d 601 data.pkg3.ecefvy = gps.ecefVY;
LucasKB 2:91a80cc7d87d 602 data.pkg3.ecefvz = gps.ecefVZ;
LucasKB 2:91a80cc7d87d 603 data.pkg3.timeOfWeek = gps.iTOW;
LucasKB 2:91a80cc7d87d 604 data.pkg3.timeOfWeekFracPart = gps.fTOW;
LucasKB 2:91a80cc7d87d 605 data.pkg3.positionAcc3D = gps.pAcc;
LucasKB 2:91a80cc7d87d 606 data.pkg3.speedAcc = gps.sAcc;
LucasKB 2:91a80cc7d87d 607 data.pkg3.numbSat = gps.numSV;
LucasKB 2:91a80cc7d87d 608 data.pkg3.gpsFix = gps.gpsFix;
LucasKB 2:91a80cc7d87d 609 data.pkg3.header |= 0xA0; // GPS updated
LucasKB 2:91a80cc7d87d 610 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 611 //ser->printf("The GPS data was read\r\n");
LucasKB 2:91a80cc7d87d 612 #endif
LucasKB 2:91a80cc7d87d 613 } else {
LucasKB 2:91a80cc7d87d 614 data.pkg3.header &= 0x80; // GPS not updated
LucasKB 2:91a80cc7d87d 615 }//24
LucasKB 2:91a80cc7d87d 616 }//23
LucasKB 2:91a80cc7d87d 617 #endif
LucasKB 2:91a80cc7d87d 618
LucasKB 2:91a80cc7d87d 619 timerGPS.reset();
LucasKB 2:91a80cc7d87d 620 }//22
LucasKB 2:91a80cc7d87d 621 #ifdef SD_EN
LucasKB 2:91a80cc7d87d 622 data.pkg3.time = timerSD.read_ms();
LucasKB 2:91a80cc7d87d 623 memcpy(&bufferSD[bufferSDCounter], &data, BUFFER_SIZE);
LucasKB 2:91a80cc7d87d 624 bufferSDCounter += BUFFER_SIZE;
LucasKB 2:91a80cc7d87d 625 timerSD.reset();
LucasKB 2:91a80cc7d87d 626 #endif
LucasKB 2:91a80cc7d87d 627 data.pkg3.time = timer2.read_ms();
LucasKB 2:91a80cc7d87d 628 if(transmited){ // Only sends a new packet when the device already have transmited the previous one //25
LucasKB 2:91a80cc7d87d 629 transmited = false;
LucasKB 2:91a80cc7d87d 630 Radio->Send( &data, BUFFER_SIZE );
LucasKB 2:91a80cc7d87d 631 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 632 //ser->printf(" Transmited = true, State = %d\r\n", state);
LucasKB 2:91a80cc7d87d 633 ser->printf("%x,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n", data.pkg3.header, data.pkg3.time, data.pkg3.timeOfWeek, data.pkg3.timeOfWeekFracPart, data.pkg3.gpsFix, data.pkg3.ecefx, data.pkg3.ecefy, data.pkg3.ecefz, data.pkg3.positionAcc3D, data.pkg3.ecefvx, data.pkg3.ecefvy, data.pkg3.ecefvz, data.pkg3.speedAcc, data.pkg3.numbSat);
LucasKB 2:91a80cc7d87d 634 #endif
LucasKB 2:91a80cc7d87d 635 state = 0;
LucasKB 2:91a80cc7d87d 636 }//25
LucasKB 2:91a80cc7d87d 637 }//21
LucasKB 2:91a80cc7d87d 638
LucasKB 2:91a80cc7d87d 639 #ifdef SD_EN
LucasKB 2:91a80cc7d87d 640 if(bufferSDCounter >= 511){ // When the bufferSD is full, save the informations //26
LucasKB 2:91a80cc7d87d 641 while(sdCard.write(&sdData[0],block) == 0);
LucasKB 2:91a80cc7d87d 642 block ++;
LucasKB 2:91a80cc7d87d 643 }//26
LucasKB 2:91a80cc7d87d 644 #endif
LucasKB 2:91a80cc7d87d 645
LucasKB 2:91a80cc7d87d 646 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 647 //ser->printf(" Fim do loop\r\n");
LucasKB 2:91a80cc7d87d 648 #endif
LucasKB 2:91a80cc7d87d 649
LucasKB 2:91a80cc7d87d 650
LucasKB 2:91a80cc7d87d 651 }//1
LucasKB 2:91a80cc7d87d 652 //----------------> Ending the main loop <----------------
TMRL123 0:a73914f20498 653 }
TMRL123 0:a73914f20498 654
TMRL123 0:a73914f20498 655 void SystemClock_Config(void)
TMRL123 0:a73914f20498 656 {
TMRL123 0:a73914f20498 657 #ifdef B_L072Z_LRWAN1_LORA
TMRL123 0:a73914f20498 658 /*
TMRL123 0:a73914f20498 659 * The L072Z_LRWAN1_LORA clock setup is somewhat differnt from the Nucleo board.
TMRL123 0:a73914f20498 660 * It has no LSE.
TMRL123 0:a73914f20498 661 */
TMRL123 0:a73914f20498 662 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
TMRL123 0:a73914f20498 663 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
TMRL123 0:a73914f20498 664
TMRL123 0:a73914f20498 665 /* Enable HSE Oscillator and Activate PLL with HSE as source */
TMRL123 0:a73914f20498 666 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
TMRL123 0:a73914f20498 667 RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
TMRL123 0:a73914f20498 668 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
TMRL123 0:a73914f20498 669 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
TMRL123 0:a73914f20498 670 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
TMRL123 0:a73914f20498 671 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
TMRL123 0:a73914f20498 672 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_6;
TMRL123 0:a73914f20498 673 RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_3;
TMRL123 0:a73914f20498 674
TMRL123 0:a73914f20498 675 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
TMRL123 0:a73914f20498 676 // Error_Handler();
TMRL123 0:a73914f20498 677 }
TMRL123 0:a73914f20498 678
TMRL123 0:a73914f20498 679 /* Set Voltage scale1 as MCU will run at 32MHz */
TMRL123 0:a73914f20498 680 __HAL_RCC_PWR_CLK_ENABLE();
TMRL123 0:a73914f20498 681 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
TMRL123 0:a73914f20498 682
TMRL123 0:a73914f20498 683 /* Poll VOSF bit of in PWR_CSR. Wait until it is reset to 0 */
TMRL123 0:a73914f20498 684 while (__HAL_PWR_GET_FLAG(PWR_FLAG_VOS) != RESET) {};
TMRL123 0:a73914f20498 685
TMRL123 0:a73914f20498 686 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
TMRL123 0:a73914f20498 687 clocks dividers */
TMRL123 0:a73914f20498 688 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
TMRL123 0:a73914f20498 689 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
TMRL123 0:a73914f20498 690 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
TMRL123 0:a73914f20498 691 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
TMRL123 0:a73914f20498 692 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
TMRL123 0:a73914f20498 693 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {
TMRL123 0:a73914f20498 694 // Error_Handler();
TMRL123 0:a73914f20498 695 }
TMRL123 0:a73914f20498 696 #endif
TMRL123 0:a73914f20498 697 }
TMRL123 0:a73914f20498 698
TMRL123 0:a73914f20498 699
TMRL123 0:a73914f20498 700
TMRL123 0:a73914f20498 701 void OnTxDone(void *radio, void *userThisPtr, void *userData)
TMRL123 0:a73914f20498 702 {
TMRL123 0:a73914f20498 703 Radio->Sleep( );
TMRL123 0:a73914f20498 704 transmited = true;
LucasKB 2:91a80cc7d87d 705 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 706 ser->printf("> OnTxDone\r\n");
LucasKB 2:91a80cc7d87d 707 //if(state == 0){
LucasKB 2:91a80cc7d87d 708 //ser->printf("Package transmited: %d\r\n", pkg3.header >> 6);
LucasKB 2:91a80cc7d87d 709 //ser->printf("Header: %x, time: %d, timeOfWeek: %d, frac: %d, gpsFix: %d\r\n", pkg3.header, pkg3.time, pkg3.timeOfWeek, pkg3.timeOfWeekFracPart, pkg3.gpsFix);
LucasKB 2:91a80cc7d87d 710 //ser->printf("eceFx: %d, eceFy: %d, eceFz: %d, posAcc3D: %d, eceFvx: %d, eceFvy: %d, eceFvz: %d\r\n", pkg3.ecefx, pkg3.ecefy, pkg3.ecefz, pkg3.positionAcc3D, pkg3.ecefvx, pkg3.ecefvy, pkg3.ecefvz);
LucasKB 2:91a80cc7d87d 711 //ser->printf("speedAcc: %d, numbSat: %d\r\n", pkg3.speedAcc, pkg3.numbSat);
LucasKB 2:91a80cc7d87d 712 //ser->printf("%x,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n", pkg3.header, pkg3.time, pkg3.timeOfWeek, pkg3.timeOfWeekFracPart, pkg3.gpsFix, pkg3.ecefx, pkg3.ecefy, pkg3.ecefz, pkg3.positionAcc3D, pkg3.ecefvx, pkg3.ecefvy, pkg3.ecefvz, pkg3.speedAcc, pkg3.numbSat);
LucasKB 2:91a80cc7d87d 713 //}
LucasKB 2:91a80cc7d87d 714 //else if(state == 1){
LucasKB 2:91a80cc7d87d 715 //ser->printf("Package transmited: %d\r\n", pkg1.header >> 6);
LucasKB 2:91a80cc7d87d 716 //ser->printf("Header: %x, time: %d, p: %f, tempLPS22HB: %f, ag: %d; %d; %d, w: %d; %d; %d, a: %d; %d; %d, m: %d; %d; %d\r\n", pkg1.header, pkg1.time, pkg1.p, pkg1.temperatureLPS22HB, pkg1.ag[0], pkg1.ag[1], pkg1.ag[2], pkg1.w[0], pkg1.w[1], pkg1.w[2], pkg1.a[0], pkg1.a[1], pkg1.a[2], pkg1.m[0], pkg1.m[1], pkg1.m[2]);
LucasKB 2:91a80cc7d87d 717 //ser->printf("%x,%d,%f,%f,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n", pkg1.header, pkg1.time, pkg1.p, pkg1.temperatureLPS22HB, pkg1.ag[0], pkg1.ag[1], pkg1.ag[2], pkg1.w[0], pkg1.w[1], pkg1.w[2], pkg1.a[0], pkg1.a[1], pkg1.a[2], pkg1.m[0], pkg1.m[1], pkg1.m[2]);
LucasKB 2:91a80cc7d87d 718 //}
LucasKB 2:91a80cc7d87d 719 //else if(state == 2){
LucasKB 2:91a80cc7d87d 720 //ser->printf("Package transmited: %d\r\n", pkg2.header >> 6);
LucasKB 2:91a80cc7d87d 721 //ser->printf("Header: %x, time: %d, parachuteStatus: %d%d%d%d, pressureBar: %f, temperature: %f, timeStamp: %d, aglAlt: %d, battery: %d, humidity: %f, tempHTS221: %f\r\n", pkg2.header, pkg2.time, pkg2.drogueStatus, pkg2.mainStatus, pkg2.mainStatusCOTS, pkg2.drogueStatusCOTS, pkg2.pressureBar, pkg2.temperature, pkg2.timeStamp, pkg2.aglAlt, pkg2.battery, pkg2.humidity, pkg2.temperatureHTS221);
LucasKB 2:91a80cc7d87d 722 //ser->printf("%x,%d,%d%d%d%d,%f,%f,%d,%d,%d,%f,%f\r\n", pkg2.header, pkg2.time, pkg2.drogueStatus, pkg2.mainStatus, pkg2.mainStatusCOTS, pkg2.drogueStatusCOTS, pkg2.pressureBar, pkg2.temperature, pkg2.timeStamp, pkg2.aglAlt, pkg2.battery, pkg2.humidity, pkg2.temperatureHTS221);
LucasKB 2:91a80cc7d87d 723 //}
LucasKB 2:91a80cc7d87d 724 #endif
LucasKB 2:91a80cc7d87d 725 timer2.reset();
TMRL123 0:a73914f20498 726 }
TMRL123 0:a73914f20498 727
TMRL123 0:a73914f20498 728 void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
TMRL123 0:a73914f20498 729 {
TMRL123 0:a73914f20498 730 Radio->Sleep( );
LucasKB 2:91a80cc7d87d 731 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 732 ser->printf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d\r\n", rssi, snr);
LucasKB 2:91a80cc7d87d 733 #endif
TMRL123 0:a73914f20498 734 }
TMRL123 0:a73914f20498 735
TMRL123 0:a73914f20498 736 void OnTxTimeout(void *radio, void *userThisPtr, void *userData)
TMRL123 0:a73914f20498 737 {
TMRL123 0:a73914f20498 738 Radio->Sleep( );
LucasKB 2:91a80cc7d87d 739 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 740 ser->printf("> OnTxTimeout\r\n");
LucasKB 2:91a80cc7d87d 741 #endif
TMRL123 0:a73914f20498 742 }
TMRL123 0:a73914f20498 743
TMRL123 0:a73914f20498 744 void OnRxTimeout(void *radio, void *userThisPtr, void *userData)
TMRL123 0:a73914f20498 745 {
TMRL123 0:a73914f20498 746 Radio->Sleep( );
LucasKB 2:91a80cc7d87d 747 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 748 ser->printf("> OnRxTimeout\r\n");
LucasKB 2:91a80cc7d87d 749 #endif
TMRL123 0:a73914f20498 750 }
TMRL123 0:a73914f20498 751
TMRL123 0:a73914f20498 752 void OnRxError(void *radio, void *userThisPtr, void *userData)
TMRL123 0:a73914f20498 753 {
TMRL123 0:a73914f20498 754 Radio->Sleep( );
LucasKB 2:91a80cc7d87d 755 #ifdef DEBUG_MESSAGE
LucasKB 2:91a80cc7d87d 756 ser->printf("> OnRxError\r\n");
LucasKB 2:91a80cc7d87d 757 #endif
TMRL123 0:a73914f20498 758 }