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:
- 37:df69df1bcb1a
- Parent:
- 36:dcc6f89fa39a
- Child:
- 38:fd7a407bfe3e
--- a/main.cpp Mon Aug 27 21:06:30 2018 +0000
+++ b/main.cpp Wed Aug 29 20:18:49 2018 +0000
@@ -20,6 +20,10 @@
//#define TARGET_DISCO2 1
//#define SENSOR_TEMP 1
+#define DCHILL 600 // Cycle delay in chill mode.
+#define DPANIC 60 // Cycle delay in panic mode.
+#define NPANIC 5 // Number of unprovoked panic cycles before cooldown.
+
#include <stdio.h>
#include "mbed.h"
@@ -232,8 +236,10 @@
#define PIN_ACC PB_14 // Not really.
#if defined(TARGET_DISCO2)
#define PIN_MAG PB_13
+ #define PIN_GPS PA_10
#else
#define PIN_MAG PA_10
+ #define PIN_GPS PB_13
#endif
#define PIN_BTN PB_2
#define LSM303_ADR_ACC 0x32
@@ -264,11 +270,16 @@
InterruptIn magPin(PIN_MAG);
InterruptIn btnPin(PIN_BTN);
+#if defined(TARGET_DISCO1) or defined(TARGET_DISCO2)
+DigitalOut gpsPin(PIN_GPS);
+#endif
+
uint8_t cfg;
char ret;
char rda = '\0';
char cmd[2];
-char buf[83];
+//char buf[83];
+char buf[192];
uint8_t pos = 0;
int accShift = 0;
@@ -289,12 +300,22 @@
char sPass[26] = "[\u001b[32mPASS\u001b[0m]";
char sFail[26] = "[\u001b[31mFAIL\u001b[0m]";
char cmdSendLoop[9] = "SendLoop";
+//char cmdStopGNSS[14] = {0xB5, 0x62, 0x06, 0x57, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x54, 0x4F, 0x50};
+
+//#define CMDSTOPGNSS {0xFF, 0xB5, 0x62, 0x06, 0x57, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x54, 0x4F, 0x50}
+//char cmdStopGNSS[16] {0xB5, 0x62, 0x06, 0x57, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // Leave 2 bytes for CRC.
+//char cmdStopGNSS[16] {0xB5, 0x62, 0x06, 0x57, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00, 0x53, 0x54, 0x4F, 0x50}; // Leave 2 bytes for CRC.
+char cmdStopGNSS[16] {0xB5, 0x62, 0x06, 0x57, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00, 0x50, 0x4F, 0x54, 0x53}; // Leave 2 bytes for CRC.
+char cmdReadGNSS[8] {0xB5, 0x62, 0x06, 0x57, 0x00, 0x00};
+char cmdReadCfgU[8] {0xB5, 0x62, 0x06, 0x1B, 0x00, 0x00}; // Leave 2 bytes for CRC.
+char cmdPollPort[9] {0xB5, 0x62, 0x06, 0x00, 0x01, 0x00, 0x00};
time_t tInit = 0;
time_t tBump = 0;
time_t tLast = 0;
time_t tNext = 0;
time_t tSend = 0;
+time_t tCalm = 0;
DigitalOut myLedR(LEDR);
DigitalOut myLedG(LEDG);
@@ -308,6 +329,7 @@
void magRead();
void accRead();
void gpsRead();
+void ubxRead();
void send_message();
void onBtnIrq()
@@ -362,6 +384,13 @@
printf("|\r\n");
}
+// Clear any pending MAG IRQs
+inline void magWipeIrq() {
+ cmd[0] = LSM303_REG_MAG_INT_SOURCE_REG_M;
+ i2c.write(LSM303_ADR_MAG, cmd, 1);
+ i2c.read(LSM303_ADR_MAG, &ret, 1);
+}
+
/**
* Entry point for application
*/
@@ -382,13 +411,17 @@
}
wait(4);
// btnPin.rise(&onBtnIrq);
-
+ printf("Boot flash complete.\r\n");
// setup tracing
setup_trace();
+ printf("Trace setup complete.\r\n");
+
// stores the status of a call to LoRaWAN protocol
lorawan_status_t retcode;
+ printf("Pre-sensor init.\r\n");
+
/* I2C init */
ret = 0x00;
magDumpCfg();
@@ -398,6 +431,7 @@
gpsInitSequence();
magDumpCfg();
accDumpCfg();
+ printf("Post-sensor init.\r\n");
cfg = 0x00;
#if defined(TARGET_LRAT)
@@ -465,6 +499,25 @@
wait(0.01);
}
+ uint8_t crcA = 0;
+ uint8_t crcB = 0;
+ for(int i = 2; i <= 13; i++)
+ {
+ crcA = crcA + cmdStopGNSS[i];
+ crcB = crcB + crcA;
+ }
+ cmdStopGNSS[14] = crcA;
+ cmdStopGNSS[15] = crcB;
+ printf("UBX CRC: %02X %02X\r\n", crcA, crcB);
+ i2c.write(NEOM8M_ADR_GPS, cmdStopGNSS, 16);
+ ubxRead();
+ ubxRead();
+ ubxRead();
+ wait(1);
+ ubxRead();
+ return 0;
+
+
// Initialize LoRaWAN stack
if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
printf("\r\n LoRa initialization failed! \r\n");
@@ -538,15 +591,20 @@
while (1) {
while (time(NULL) < tSend)
wait(0.1);
+ if (tSend == 0)
+ tCalm = time(NULL) + (DPANIC * NPANIC);
+ tSend = time(NULL) + (time(NULL) < tCalm ? DPANIC : DCHILL);
+ magWipeIrq();
printf("IT'S PACKET TIME!\r\n");
ev_queue.call(send_message);
ev_queue.dispatch();
printf("\r\n- = - = - = - = - = - DISPATCH 2 COMPLETE - = - = - = - = - = -\r\n");
- tSend = time(NULL) + 300;
+ //tSend = time(NULL) + 300;
+ //tSend = time(NULL) + (time(NULL) < tCalm ? DPANIC : DCHILL);
// 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);
+// cmd[0] = LSM303_REG_MAG_INT_SOURCE_REG_M;
+// i2c.write(LSM303_ADR_MAG, cmd, 1);
+// i2c.read(LSM303_ADR_MAG, &ret, 1);
}
return 0;
@@ -1280,7 +1338,8 @@
printf("[\u001b[33mWARN\u001b[0m] Expected '0A', received '%02X'.\r\n", ret);
}
}
- else if (pos == 82)
+ //else if (pos == 82)
+ else if (pos == 191)
{
buf[pos] = 0x00;
printf("GPS: |%s| ...\r\n", buf);
@@ -1333,4 +1392,43 @@
}
}
+void ubxRead()
+{
+ uint8_t crcPass = 0;
+ uint8_t crcFail = 0;
+
+ myLedW = 1;
+ pos = 0;
+ ret = 0xFF;
+ cmd[0] = 0xFF;
+ i2c.write(NEOM8M_ADR_GPS, cmd, 1);
+ while (ret == 0xFF)
+ {
+ i2c.read(NEOM8M_ADR_GPS, &ret, 1);
+ }
+ while (ret != 0xFF)
+ {
+ buf[pos++] = ret;
+ i2c.read(NEOM8M_ADR_GPS, &ret, 1);
+ }
+ printf("UBX: | ");
+ for (int i = 0; i <= pos; i++) { printf("%02X ", buf[i]); }
+ printf("|\r\n", buf);
+ myLedW = 0;
+ //if (crcFail)
+ // printf("[\u001b[33mWARN\u001b[0m] CRC PASS: %d FAIL: %d\r\n", crcPass, crcFail);
+
+ if (pos > 0)
+ myLedG = 1;
+ else
+ myLedR = 1;
+
+ for (int i = 0; i < 10; i++) {
+ myLedB = 1;
+ wait(0.1);
+ myLedB = 0;
+ wait(0.1);
+ }
+}
+
// EOF
