Compare commits

..
5 Commits
Author SHA1 Message Date
jlehmann f8909fc353 print LMIC.opmode before sleep 2020-06-01 19:31:36 +02:00
jlehmann ab86c843b1 cancel transactions at timeout 2020-06-01 19:04:39 +02:00
jlehmann 6d3053d8ad try to make it more stable... 2020-06-01 12:57:54 +02:00
jlehmann 85b0f6db06 refactor 2020-05-30 20:11:19 +02:00
jlehmann 0c5c673e5e add delay, see https://github.com/mcci-catena/arduino-lmic/issues/547 2020-05-30 18:01:13 +02:00
2 changed files with 67 additions and 196 deletions
+66 -195
View File
@@ -143,8 +143,6 @@ bool fFlash;
bool fUsbPower;
// have we printed the sleep info?
bool g_fPrintedSleeping = false;
// the job that's used to synchronize us with the LMIC code
static osjob_t iterationJob;
static osjob_t sendJob;
@@ -690,22 +688,22 @@ void StartNewIteration() {
gLed.Set(LedPattern::TwoShort);
}
// Loop while sending is in progress, timeout just in case after 300 seconds
// Loop sending is in progress, timeout just in case after 300 seconds
long start_time = millis();
if (config_data.debug_level > 0) {
gCatena.SafePrintf("waiting while send is in progress\n");
}
while (send_in_progress && ((millis() - start_time) < 300000))
{
os_runloop_once();
// gCatena.poll();
// yield();
gCatena.poll();
yield();
}
// handle timeout...
if (send_in_progress) {
if (config_data.debug_level > 0) {
gCatena.SafePrintf("looks like we timed out waiting for sending to finish...\n", wait_time);
}
LMIC_clrTxData();
send_in_progress = false;
}
wait_time = (uint32_t)((millis() - start_time) / 1000);
@@ -727,9 +725,9 @@ void StartNewIteration() {
sleep_time_sec = 5;
}
// for the first <INIT_PACKETS> iterations, we set the sleep time to 10 seconds only...
// for the first <INIT_PACKETS> iterations, we set the sleep time to 120 seconds only...
if (iteration <= INIT_PACKETS) {
sleep_time_sec = 10;
sleep_time_sec = 120;
}
if (config_data.debug_level > 0) {
@@ -745,16 +743,26 @@ void StartNewIteration() {
}
}
// if we need to periodically reboot, we can do it now...
if (uint32_t(millis()) > gRebootMs) {
// time to reboot
if (config_data.debug_level > 0) {
gCatena.SafePrintf("Reached threshold to reboot...\n");
Serial.flush();
}
NVIC_SystemReset();
}
if (config_data.debug_level > 0) {
gCatena.SafePrintf("LMIC.opmode just before Sleeping: %#x\n", LMIC.opmode);
}
if (!fUsbPower) {
DoDeepSleep(sleep_time_sec);
if (! stop_iterations) {
StartNewIteration();
}
//os_setTimedCallback(
// &iterationJob,
// os_getTime() + sec2osticks(2),
// startNewIterationCb);
}
else {
if (config_data.debug_level > 0) {
@@ -791,9 +799,6 @@ void startSendingUplink(bool firstTime)
fConfirmed = true;
}
os_runloop_once();
//gCatena.poll();
if (firstTime) {
if (config_data.debug_level > 0) {
gCatena.SafePrintf("SendBuffer firstTime\n");
@@ -802,6 +807,7 @@ void startSendingUplink(bool firstTime)
package_counter++;
} else {
if (config_data.debug_level > 0) {
gCatena.SafePrintf("LMIC.opmode just before SendBuffer: %#x\n", LMIC.opmode);
gCatena.SafePrintf("SendBuffer not firstTime\n");
}
gLoRaWAN.SendBuffer((uint8_t*)&lora_data, sizeof(LORA_data), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort);
@@ -819,12 +825,12 @@ static void sendBufferDoneCb(
if (config_data.debug_level > 1) {
gLed.Set(LedPattern::Settling);
gCatena.SafePrintf("LMIC.opmode in sendBufferDoneCb: %#x\n", LMIC.opmode);
}
pFn = settleDoneCb;
if (! fStatus)
{
send_in_progress = false;
if (!gLoRaWAN.IsProvisioned())
{
// we'll talk about it at the callback.
@@ -834,7 +840,7 @@ static void sendBufferDoneCb(
gLoRaWAN.Shutdown();
}
else if (config_data.debug_level > 0) {
gCatena.SafePrintf("send buffer failed\n");
gCatena.SafePrintf("send buffer failed, LMIC.opmode: %#x\n", LMIC.opmode);
}
}
@@ -858,130 +864,45 @@ static void txNotProvisionedCb(
}
}
static void settleDoneCb(
osjob_t* pSendJob)
{
const bool fDeepSleep = checkDeepSleep();
if (config_data.debug_level > 0) {
gCatena.SafePrintf("settleDoneCb\n");
}
if (config_data.debug_level > 2) {
// Terry vv
gCatena.SafePrintf("LMIC.rxDelay: %i\n", LMIC.rxDelay);
gCatena.SafePrintf("LMIC.dn2Dr: %i\n", LMIC.dn2Dr);
gCatena.SafePrintf("LMIC.dn2Freq: %i\n", LMIC.dn2Freq);
gCatena.SafePrintf("LMIC.rx1DrOffset: %i\n", LMIC.rx1DrOffset);
gCatena.SafePrintf("LMIC.adrAckReq: %i\n", LMIC.adrAckReq);
gCatena.SafePrintf("LMIC.adrEnabled: %i\n", LMIC.adrEnabled);
// Terry ^^
}
if (uint32_t(millis()) > gRebootMs) {
// time to reboot
NVIC_SystemReset();
}
if (! g_fPrintedSleeping)
doSleepAlert(fDeepSleep);
/* count what we're up to */
updateSleepCounters();
if (fDeepSleep)
doDeepSleep(pSendJob);
else
doLightSleep(pSendJob);
}
bool checkDeepSleep(void)
{
bool const fDeepSleepTest = gCatena.GetOperatingFlags() &
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fDeepSleepTest);
bool fDeepSleep;
if (fDeepSleepTest)
{
fDeepSleep = true;
}
#ifdef USBCON
else if (Serial.dtr())
{
fDeepSleep = false;
}
#endif
else if (gCatena.GetOperatingFlags() &
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fDisableDeepSleep))
{
fDeepSleep = false;
}
else if ((gCatena.GetOperatingFlags() &
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fUnattended)) != 0)
{
fDeepSleep = true;
}
else
{
fDeepSleep = false;
}
return fDeepSleep;
return !fUsbPower;
}
void doSleepAlert(const bool fDeepSleep)
{
g_fPrintedSleeping = true;
if (config_data.debug_level > 0) {
gCatena.SafePrintf("We wait until is is safe to go to sleep...\n");
}
if (fDeepSleep)
{
bool const fDeepSleepTest = gCatena.GetOperatingFlags() &
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fDeepSleepTest);
const uint32_t deepSleepDelay = fDeepSleepTest ? 10 : 30;
if (config_data.debug_level > 2) {
gCatena.SafePrintf("using deep sleep in %u secs"
#ifdef USBCON
" (USB will disconnect while asleep)"
#endif
": ",
deepSleepDelay
);
}
// sleep and print
if (config_data.debug_level > 1) {
gLed.Set(LedPattern::TwoShort);
}
for (auto n = deepSleepDelay; n > 0; --n)
for (int i = 0; i <= 15; i++) {
long prevPrint = millis();
while (os_queryTimeCriticalJobs(ms2osticks(2000)) != 0)
{
uint32_t tNow = millis();
while (uint32_t(millis() - tNow) < 1000)
{
os_runloop_once();
//gCatena.poll();
//yield();
}
if (config_data.debug_level > 2) {
gCatena.SafePrintf(".");
gCatena.poll();
yield();
if (millis() - prevPrint > 1000) {
prevPrint = millis();
if (config_data.debug_level > 0) {
gCatena.SafePrintf("LMIC.opmode: %#x in loop %d\n", LMIC.opmode, i);
}
}
}
if (config_data.debug_level > 2) {
gCatena.SafePrintf("\nStarting deep sleep.\n");
}
uint32_t tNow = millis();
while (uint32_t(millis() - tNow) < 100)
{
os_runloop_once();
//gCatena.poll();
//yield();
}
}
else if (config_data.debug_level > 2) {
gCatena.SafePrintf("using light sleep\n");
// we wait an extra five seconds...
//uint32_t tNow = millis();
//while (uint32_t(millis() - tNow) < 5000)
//{
// gCatena.poll();
// yield();
//}
if (config_data.debug_level > 0) {
gCatena.SafePrintf("Now it is safe to go to sleep\n");
}
}
void updateSleepCounters(void)
@@ -995,10 +916,9 @@ void updateSleepCounters(void)
else if (gTxCycleCount == 1)
{
// it's now one (otherwise we couldn't be here.)
if (config_data.debug_level > 2) {
if (config_data.debug_level > 0) {
gCatena.SafePrintf("resetting tx cycle to default: %u\n", CATCFG_T_CYCLE);
}
gTxCycleCount = 0;
gTxCycle = CATCFG_T_CYCLE;
}
@@ -1008,30 +928,28 @@ void updateSleepCounters(void)
}
}
void doDeepSleep(osjob_t *pJob)
{
bool const fDeepSleepTest = gCatena.GetOperatingFlags() &
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fDeepSleepTest);
uint32_t const sleepInterval = CATCFG_GetInterval(
fDeepSleepTest ? CATCFG_T_CYCLE_TEST : gTxCycle
);
static void settleDoneCb(
osjob_t* pSendJob)
{
if (config_data.debug_level > 0) {
gCatena.SafePrintf("doDeepSleep, sleepInterval: %d...\n", sleepInterval);
gCatena.SafePrintf("settleDoneCb - we are at the end of the callback chain!\n");
}
/* ok... now it's time for a deep sleep */
gLed.Set(LedPattern::Off);
deepSleepPrepare();
const bool fDeepSleep = checkDeepSleep();
/* sleep */
gCatena.Sleep(sleepInterval);
if (uint32_t(millis()) > gRebootMs)
{
// time to reboot
NVIC_SystemReset();
}
/* recover from sleep */
deepSleepRecovery();
doSleepAlert(fDeepSleep);
/* and now... we're awake again. trigger another measurement */
sleepDoneCb(pJob);
/* count what we're up to */
updateSleepCounters();
send_in_progress = false;
}
void deepSleepPrepare(void)
@@ -1053,53 +971,6 @@ void deepSleepRecovery(void)
gSPI2.begin();
}
void doLightSleep(osjob_t *pJob)
{
uint32_t interval = sec2osticks(CATCFG_GetInterval(gTxCycle));
if (config_data.debug_level > 1) {
gLed.Set(LedPattern::Sleeping);
gCatena.SafePrintf("doLightSleep\n");
}
if (gCatena.GetOperatingFlags() &
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fQuickLightSleep))
{
interval = 1;
}
os_setTimedCallback(
&iterationJob,
os_getTime() + interval,
sleepDoneCb
);
}
static void sleepDoneCb(osjob_t* pJob)
{
if (config_data.debug_level > 1) {
gLed.Set(LedPattern::WarmingUp);
}
if (config_data.debug_level > 0) {
gCatena.SafePrintf("sleepDoneCb\n");
}
os_setTimedCallback(
pJob,
os_getTime() + sec2osticks(CATCFG_T_WARMUP),
warmupDoneCb);
}
static void warmupDoneCb(osjob_t* pJob)
{
if (config_data.debug_level > 0) {
gCatena.SafePrintf("warmupDoneCb\n");
}
send_in_progress = false;
}
static void startNewIterationCb(osjob_t* pJob)
{
+1 -1
View File
@@ -56,7 +56,7 @@ enum {
|
\****************************************************************************/
static const int32_t fwVersion = 20200529;
static const int32_t fwVersion = 20200601;
static const byte INIT_PACKAGE_INTERVAL = 100; // send an init package every 100 packages;
static const byte MAX_VALUES_TO_SEND = 8;