Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Custom_LSM303 Custom_UBloxGPS LRAT-mbed-os USBDevice mbed-lora-radio-drv stm32EEPROM
Fork of LRAT-example-lorawan by
Diff: main.cpp
- Revision:
- 35:73b3963c6dd3
- Parent:
- 34:341fb423e74b
- Child:
- 36:dcc6f89fa39a
diff -r 341fb423e74b -r 73b3963c6dd3 main.cpp --- a/main.cpp Tue Aug 21 14:56:58 2018 +0000 +++ b/main.cpp Mon Aug 27 15:09:11 2018 +0000 @@ -15,6 +15,8 @@ * limitations under the License. */ //#define TARGET_LRAT 1 +#define TARGET_DISCO1 1 +//#define TARGET_DISCO2 1 //#define SENSOR_TEMP 1 #include <stdio.h> #include "mbed.h" @@ -93,18 +95,20 @@ */ static lorawan_app_callbacks_t callbacks; -#if defined(TARGET_LRAT) +/* +#if defined(TARGET_LRAT) or defined(TARGET_DISCO2) #include "USBSerial.h" USBSerial serial; FileHandle* mbed::mbed_override_console(int) { return &serial; } #endif +*/ -int mytime = 0; -int mybatt = 0; -double mylat = 0; -double mylon = 0; +uint32_t mytime = 0; +uint8_t mybatt = 0; +double mylat = 0; +double mylon = 0; int16_t myAccX = 0; int16_t myAccY = 0; @@ -160,10 +164,7 @@ #define LSM303_REG_ACC_INT1_SRC_A 0x31 #define LSM303_REG_ACC_INT1_THS_A 0x32 #define LSM303_REG_ACC_INT1_DURATION_A 0x33 -/* -//#define LSM303_REG_MAG_CRA_REG_M 0x00 -//#define LSM303_REG_MAG_MR_REG_M 0x02 -*/ + #define LSM303_REG_MAG_OFFSET_X_REG_L_M 0x45 #define LSM303_REG_MAG_OFFSET_X_REG_H_M 0x46 #define LSM303_REG_MAG_OFFSET_Y_REG_L_M 0x47 @@ -186,11 +187,17 @@ //#define LSM303_REG_MAG_OUTZ_L_REG_M 0x6C //#define LSM303_REG_MAG_OUTZ_H_REG_M 0x6D +#define EEPROM_MAX 0x17FF + #if defined(TARGET_LRAT) #define LEDR PB_6 #define LEDG PB_7 #define LEDB PB_5 #define LEDW PB_2 + #define PIN_ACC PB_14 + #define PIN_MAG PB_12 + #define PIN_BTN PA_5 + #define PIN_ALT PA_4 #define LSM303_ADR_ACC 0x3A #define LSM303_REG_MAG_WHO_AM_I_M 0x0F #define LSM303_WHO_ACC 0x41 @@ -220,6 +227,13 @@ #define LEDG PB_5 #define LEDB PB_6 #define LEDW PA_5 + #define PIN_ACC PB_14 // Not really. + #if defined(TARGET_DISCO2) + #define PIN_MAG PB_13 + #else + #define PIN_MAG PA_10 + #endif + #define PIN_BTN PB_2 #define LSM303_ADR_ACC 0x32 #define LSM303_REG_MAG_WHO_AM_I_M 0x4F #define LSM303_WHO_ACC 0x33 @@ -244,33 +258,41 @@ #endif I2C i2c(PB_9, PB_8); -InterruptIn accPin(PB_14); -InterruptIn magPin(PA_10); +InterruptIn accPin(PIN_ACC); +InterruptIn magPin(PIN_MAG); +InterruptIn btnPin(PIN_BTN); -char cfg; +uint8_t cfg; char ret; char rda = '\0'; char cmd[2]; char buf[83]; -int pos = 0; -char *res; -char sPass[26] = "[\u001b[32mPASS\u001b[0m]"; -char sFail[26] = "[\u001b[31mFAIL\u001b[0m]"; +uint8_t pos = 0; + int accShift = 0; int accScale = 0; int accEvent = 0; -uint16_t accSFire = 0; -uint16_t accHFire = 0; -uint16_t accSLIRQ = 0; -uint16_t accSHIRQ = 0; +uint8_t accSFire = 0; +uint8_t accHFire = 0; +uint8_t accSLIRQ = 0; +uint8_t accSHIRQ = 0; int magEvent = 0; -uint16_t magSFire = 0; -uint16_t magHFire = 0; -uint16_t magSLIRQ = 0; -uint16_t magSHIRQ = 0; +uint8_t magSFire = 0; +uint8_t magHFire = 0; +uint8_t magSLIRQ = 0; +uint8_t magSHIRQ = 0; +uint8_t btnHFire = 0; +char *res; +char sPass[26] = "[\u001b[32mPASS\u001b[0m]"; +char sFail[26] = "[\u001b[31mFAIL\u001b[0m]"; char cmdSendLoop[9] = "SendLoop"; +time_t tInit = 0; +time_t tBump = 0; +time_t tLast = 0; +time_t tNext = 0; + DigitalOut myLedR(LEDR); DigitalOut myLedG(LEDG); DigitalOut myLedB(LEDB); @@ -284,14 +306,28 @@ void accRead(); void gpsRead(); +void onBtnIrq() +{ + btnHFire++; +// tBump = time(NULL); +// tNext = tBump; + tNext = 0; +} + void onAccIrq() { accHFire++; +// tBump = time(NULL); +// tNext = tBump; + tNext = 0; } void onMagIrq() { magHFire++; +// tBump = time(NULL); +// tNext = tBump; + tNext = 0; } void accDumpCfg() @@ -333,6 +369,7 @@ */ int main (void) { + //i2c.frequency(400000); wait(4); printf("\r\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\r\n"); @@ -346,7 +383,8 @@ wait(0.01); } wait(4); - +// btnPin.rise(&onBtnIrq); + // setup tracing setup_trace(); @@ -402,17 +440,53 @@ #endif printf("Quality: %02x AccShift: %d AccScale: %d\r\n", cfg, accShift, accScale); - while(1) - { - time_t tNow = time(NULL); - printf("Clock: %d\r\n", tNow); + time_t tNow; + /* +// while(1) +// { + tNow = time(NULL); + printf("RTC: %08X\r\n", tNow); #if defined(SENSOR_TEMP) tmpRead(); #endif magRead(); accRead(); gpsRead(); - wait(4); + printf("TIM: %d, SAT: %d, LAT: %f, LON: %f\r\n", mytime, mybatt, mylat, mylon); + printf("IRQ: A=%02X M=%02X B=%02X\r\n", accHFire, magHFire, btnHFire); + wait(1); +// } + */ + + printf("CONTROL LOOP GOES HERE!\r\n"); + +#if defined(TARGET_LRAT) +#else + for (int i = 0; i < 6; i++) + { + tNow = time(NULL); + printf("RTC: %08X\r\n", tNow); + tNext = tNow + 10; + printf("NXT: %08X\r\n", tNext); + // Clear any pending IRQs + cmd[0] = LSM303_REG_MAG_INT_SOURCE_REG_M; + i2c.write(LSM303_ADR_MAG, cmd, 1); + i2c.read(LSM303_ADR_MAG, &ret, 1); + while (time(NULL) < tNext) + { + wait(1); + } + printf("Timer #%d complete.\r\n", i); + } +#endif + + for (int i = 0; i <= 64; i++) + { + myLedR = i & 0x01; + myLedG = i & 0x02; + myLedB = i & 0x04; + myLedW = i & 0x08; + wait(0.01); } // Initialize LoRaWAN stack @@ -423,6 +497,7 @@ printf("\r\n Mbed LoRaWANStack initialized \r\n"); printf("MBED_CONF_LORA_APP_PORT: %d", MBED_CONF_LORA_APP_PORT); + printf("MBED_CONF_LORA_DUTY_CYCLE_ON: %d", MBED_CONF_LORA_DUTY_CYCLE_ON); // prepare application callbacks callbacks.events = mbed::callback(lora_event_handler); @@ -642,6 +717,7 @@ void magInitSequence() { + printf("In magInitSequence()...\r\n"); myLedR = 0; myLedG = 0; cmd[0] = LSM303_REG_MAG_WHO_AM_I_M; @@ -709,23 +785,26 @@ myLedB = 0; wait(0.5); } - /* +#if defined(TARGET_LRAT) + // LRAT MAG IRQ SETUP GOES HERE +#else // MAG INTERRUPT SETUP cmd[0] = LSM303_REG_MAG_INT_THS_L_REG_M; - cmd[1] = 0xF4; + cmd[1] = 0xE8; i2c.write(LSM303_ADR_MAG, cmd, 2); cmd[0] = LSM303_REG_MAG_INT_THS_H_REG_M; - cmd[1] = 0x01; + cmd[1] = 0x03; i2c.write(LSM303_ADR_MAG, cmd, 2); cmd[0] = LSM303_REG_MAG_INT_CTRL_REG_M; cmd[1] = 0xE7; i2c.write(LSM303_ADR_MAG, cmd, 2); magPin.rise(&onMagIrq); - */ +#endif } void accInitSequence() { + printf("In accInitSequence()...\r\n"); myLedR = 0; myLedG = 0; cmd[0] = LSM303_REG_ACC_WHO_AM_I_A; @@ -820,7 +899,9 @@ cmd[1] = 0x08; i2c.write(LSM303_ADR_ACC, cmd, 2); */ - /* +#if defined(TARGET_LRAT) + // LRAT ACC IRQ SETUP GOES HERE +#else // ACC INTERRUPT SETUP // Enable Interrupt Pin cmd[0] = LSM303_REG_ACC_CTRL_REG3_A; @@ -851,16 +932,23 @@ cmd[1] = 0x2A; //cmd[1] = 0x0A; i2c.write(LSM303_ADR_ACC, cmd, 2); - //accPin.rise(&onAccIrq); - */ + accPin.rise(&onAccIrq); +#endif } void gpsInitSequence() { - myLedG = 1; - myLedR = 1; + printf("In gpsInitSequence()...\r\n"); + gpsRead(); + if (mytime > 0) + { + set_time(mytime); + tInit = mytime; + myLedG = 1; + } + else + myLedR = 1; - // LED Confirmation Output - GPS for (int i = 0; i < 2; i++) { myLedB = 1; wait(0.3); @@ -1040,6 +1128,10 @@ { bool gpsDone = false; bool fixGood = false; + uint8_t crcPass = 0; + uint8_t crcFail = 0; + uint32_t tDate = 0; + uint32_t tTime = 0; myLedW = 1; pos = 0; ret = 0xFF; @@ -1063,20 +1155,27 @@ buf[pos] = 0x00; // NMEA Validation uint16_t crc = 0; - char clr; + char clr = '\0'; if (buf[0] == '$' && buf[pos-3] == '*') { - int i; - for (i = 1; i < pos-3; i++) + for (int i = 1; i < pos-3; i++) { crc ^= buf[i]; } } if (crc == ((buf[pos-2] < 'A' ? buf[pos-2] - '0' : buf[pos-2] - '7') << 4 | (buf[pos-1] < 'A' ? buf[pos-1] - '0' : buf[pos-1] - '7'))) + { clr = '2'; // 2 = Green + crcPass++; + } else + { clr = '1'; // 1 = Red + crcFail++; + } printf("GPS: [\u001b[3%cm%02X\u001b[0m] |%s|\r\n", clr, crc, buf); + if (clr == '2') + { // Global Positioning System Fix Data if(strncmp(buf, "$GNGGA", 6) == 0) { @@ -1093,54 +1192,67 @@ { mylat = fldLat / (fldN_S == 'S' ? -100 : 100); mylon = fldLon / (fldE_W == 'W' ? -100 : 100); - mytime = (uint32_t)fldTim; + //mytime = (uint32_t)fldTim; mybatt = (fldSat & 0xF0 ? 0x0F : fldSat & 0x0F); } } // Satellite status if(strncmp(buf, "$GNGSA", 6) == 0) { - printf("GNGSA> "); + //printf("GNGSA> "); //sscanf(cmd, "$GPGSA,%c,%d,%d", &tf, &fix, &nst); //pc.printf("GPGSA Type fix: %c, 3D fix: %d, number of sat: %d\r\n", tf, fix, nst); char fldTyp; int fldDim, fldSat; sscanf(buf, "$GNGSA,%c,%d,%d", &fldTyp, &fldDim, &fldSat); - printf("Typ: %c, Pos: %d, Sat: %d\r\n", fldTyp, fldDim, fldSat); + //printf("Typ: %c, Pos: %d, Sat: %d\r\n", fldTyp, fldDim, fldSat); } // Geographic position, Latitude and Longitude if(strncmp(buf, "$GNGLL", 6) == 0) { - printf("GNGLL> "); + //printf("GNGLL> "); //sscanf(cmd, "$GPGLL,%f,%c,%f,%c,%f", &latitude, &ns, &longitude, &ew, &timefix); //pc.printf("GPGLL Latitude: %f %c, Longitude: %f %c, Fix taken at: %f\n", latitude, ns, longitude, ew, timefix); float fldTim; double fldLat, fldLon; char fldN_S, fldE_W; sscanf(buf, "$GNGLL,%lf,%c,%lf,%c,%f", &fldLat, &fldN_S, &fldLon, &fldE_W, &fldTim); - printf("Lat: %.5f %c, Lon: %.5f %c, Sec: %.2f\r\n", fldLat, fldN_S, fldLon, fldE_W, fldTim); + //printf("Lat: %.5f %c, Lon: %.5f %c, Sec: %.2f\r\n", fldLat, fldN_S, fldLon, fldE_W, fldTim); } // Geographic position, Latitude and Longitude if(strncmp(buf, "$GNRMC", 6) == 0) { + //printf("GPS: [\u001b[3%cm%02X\u001b[0m] |%s|\r\n", clr, crc, buf); printf("GNRMC> "); //sscanf(cmd, "$GPRMC,%f,%c,%f,%c,%f,%c,%f,,%d", &timefix, &status, &latitude, &ns, &longitude, &ew, &speed, &date); //pc.printf("GPRMC Fix taken at: %f, Status: %c, Latitude: %f %c, Longitude: %f %c, Speed: %f, Date: %d\n", timefix, status, latitude, ns, longitude, ew, speed, date); - float fldTim, fldSpd; + float fldTim, fldSpd, fldTrk; + fldTrk = 0; double fldLat, fldLon; char fldSts, fldN_S, fldE_W; - int fldDat; - sscanf(buf, "$GNRMC,%f,%c,%lf,%c,%lf,%c,%f,,%d", &fldTim, &fldSts, &fldLat, &fldN_S, &fldLon, &fldE_W, &fldSpd, &fldDat); + uint32_t fldDat; + if (sscanf(buf, "$GNRMC,,%c", &fldSts) != 1 && + sscanf(buf, "$GNRMC,%f,%c,,,,,,,%d", &fldTim, &fldSts, &fldDat) != 3 && + sscanf(buf, "$GNRMC,%f,%c,%lf,%c,%lf,%c,%f,,%d", &fldTim, &fldSts, &fldLat, &fldN_S, &fldLon, &fldE_W, &fldSpd, &fldDat) != 8 && + sscanf(buf, "$GNRMC,%f,%c,%lf,%c,%lf,%c,%f,%f,%d", &fldTim, &fldSts, &fldLat, &fldN_S, &fldLon, &fldE_W, &fldSpd, &fldTrk, &fldDat) != 9) + printf("[\u001b[33mWARN\u001b[0m] Invalid GNRMC packet detected.\r\n"); + //sscanf(buf, "$GNRMC,%f,%c,%lf,%c,%lf,%c,%f,%7[^,],%d", &fldTim, &fldSts, &fldLat, &fldN_S, &fldLon, &fldE_W, &fldSpd, fldFoo, &fldDat); printf("Sec: %.2f, Sts: %c, Lat: %.5f %c, Lon: %.5f %c, Spd: %.3f, Dat: %06d\r\n", fldTim, fldSts, fldLat, fldN_S, fldLon, fldE_W, fldSpd, fldDat); - if (fldSts == 'A') - fixGood = true; + if (clr == '2') + { + tTime = (uint32_t)fldTim; + tDate = fldDat; + if (fldSts == 'A') + fixGood = true; + } + } } pos = 0; i2c.read(NEOM8M_ADR_GPS, &ret, 1); } else { - printf("WARN: Expected '\n', received '%02x'.\r\n", ret); + printf("[\u001b[33mWARN\u001b[0m] Expected '0A', received '%02X'.\r\n", ret); } } else if (pos == 82) @@ -1157,6 +1269,32 @@ if (pos > 0) printf("GPS: |%s|\r\n", buf); myLedW = 0; + if (crcFail) + printf("[\u001b[33mWARN\u001b[0m] CRC PASS: %d FAIL: %d\r\n", crcPass, crcFail); + + struct tm ts; + time_t t; + + uint8_t tDay = tDate / 10000; + uint8_t tMon = (tDate - (tDay * 10000)) / 100; + uint8_t tYear = (tDate - ((tDay * 10000) + (tMon * 100))) + 100; + uint8_t tHour = tTime / 10000; + uint8_t tMin = (tTime - (tHour * 10000)) / 100; + uint8_t tSec = (tTime - ((tHour * 10000) + (tMin * 100))); + + ts.tm_year = tYear; + ts.tm_mon = tMon - 1; + ts.tm_mday = tDay; + ts.tm_hour = tHour; + ts.tm_min = tMin; + ts.tm_sec = tSec; + t = mktime(&ts); + //printf("DAT: %06d TIM: %d\r\n", tDate, tTime); + //printf("CNV: %04d-%02d-%02d@%02d:%02d:%02d\r\n", tYear, tMon, tDay, tHour, tMin, tSec); + //printf("T: %d\t%s", t, ctime(&t)); + printf("GPS: %08X\t%s", t, ctime(&t)); + mytime = t; + if (fixGood) myLedG = 1; else