Compare commits

..
4 Commits
Author SHA1 Message Date
jlehmann 59ce7ee44d 20200602 2020-06-02 21:21:05 +02:00
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
2 changed files with 112 additions and 45 deletions
+103 -36
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,21 +688,30 @@ 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();
delay(10);
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();
// we sleep 10 seconds...
start_time = millis();
while (send_in_progress && ((millis() - start_time) < 10000))
{
gCatena.poll();
yield();
}
send_in_progress = false;
}
wait_time = (uint32_t)((millis() - start_time) / 1000);
@@ -726,33 +733,11 @@ 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;
}
// Before we go to sleep, we'd like to be sure that this is safe!
if (config_data.debug_level > 0) {
gCatena.SafePrintf("We wait until is is safe to go to sleep...\n");
}
long loopCount = 0;
long prevPrint;
while(os_queryTimeCriticalJobs(ms2osticks(8000)) != 0)
{
loopCount++;
os_runloop_once();
if(millis() - prevPrint > 1000) {
prevPrint = millis();
if (config_data.debug_level > 0) {
gCatena.SafePrintf("LMIC.opmode: %#x\n", LMIC.opmode);
}
}
}
if (config_data.debug_level > 0) {
gCatena.SafePrintf("Now it is safe to go to sleep\n");
}
delay(10);
if (config_data.debug_level > 0) {
gCatena.SafePrintf("now going to sleep for %d seconds...\n", sleep_time_sec);
if (fUsbPower) {
@@ -776,6 +761,9 @@ void StartNewIteration() {
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);
@@ -785,6 +773,7 @@ void StartNewIteration() {
}
else {
if (! stop_iterations) {
if (config_data.debug_level > 0) {
gCatena.SafePrintf("light sleep; os_setTimedCallback for startNewIterationCb in %d...seconds\n", sleep_time_sec);
}
@@ -793,6 +782,7 @@ void StartNewIteration() {
os_getTime() + sec2osticks(sleep_time_sec),
startNewIterationCb);
}
}
}
void startSendingUplink(bool firstTime)
@@ -819,22 +809,31 @@ void startSendingUplink(bool firstTime)
fConfirmed = true;
}
os_runloop_once();
//gCatena.poll();
if (firstTime) {
if (config_data.debug_level > 0) {
gCatena.SafePrintf("SendBuffer firstTime\n");
}
gLoRaWAN.SendBuffer((uint8_t*)&lora_data_first, sizeof(LORA_data_first), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort);
if (gLoRaWAN.SendBuffer((uint8_t*)&lora_data_first, sizeof(LORA_data_first), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort)) {
package_counter++;
if (config_data.debug_level > 0) {
gCatena.SafePrintf("LMIC.opmode just after SendBuffer (successful): %#x\n", LMIC.opmode);
} else {
gCatena.SafePrintf("LMIC.opmode just before SendBuffer (failed): %#x\n", LMIC.opmode);
}
}
} 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);
if (gLoRaWAN.SendBuffer((uint8_t*)&lora_data, sizeof(LORA_data), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort)) {
package_counter++;
if (config_data.debug_level > 0) {
gCatena.SafePrintf("LMIC.opmode just after SendBuffer (successful): %#x\n", LMIC.opmode);
} else {
gCatena.SafePrintf("LMIC.opmode just before SendBuffer (failed): %#x\n", LMIC.opmode);
}
}
}
ClearLoraData();
@@ -846,8 +845,6 @@ static void sendBufferDoneCb(
{
osjobcb_t pFn;
send_in_progress = false;
if (config_data.debug_level > 1) {
gLed.Set(LedPattern::Settling);
gCatena.SafePrintf("LMIC.opmode in sendBufferDoneCb: %#x\n", LMIC.opmode);
@@ -889,6 +886,61 @@ static void txNotProvisionedCb(
}
}
bool checkDeepSleep(void)
{
return !fUsbPower;
}
void doSleepAlert(const bool fDeepSleep)
{
if (config_data.debug_level > 0) {
gCatena.SafePrintf("We wait until is is safe to go to sleep...\n");
}
for (int i = 0; i <= 15; i++) {
long prevPrint = millis();
while (os_queryTimeCriticalJobs(ms2osticks(2000)) != 0)
{
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 > 0) {
gCatena.SafePrintf("Now it is safe to go to sleep\n");
}
}
void updateSleepCounters(void)
{
// update the sleep parameters
if (gTxCycleCount > 1)
{
// values greater than one are decremented and ultimately reset to default.
--gTxCycleCount;
}
else if (gTxCycleCount == 1)
{
// it's now one (otherwise we couldn't be here.)
if (config_data.debug_level > 0) {
gCatena.SafePrintf("resetting tx cycle to default: %u\n", CATCFG_T_CYCLE);
}
gTxCycleCount = 0;
gTxCycle = CATCFG_T_CYCLE;
}
else
{
// it's zero. Leave it alone.
}
}
static void settleDoneCb(
osjob_t* pSendJob)
@@ -896,6 +948,21 @@ static void settleDoneCb(
if (config_data.debug_level > 0) {
gCatena.SafePrintf("settleDoneCb - we are at the end of the callback chain!\n");
}
const bool fDeepSleep = checkDeepSleep();
if (uint32_t(millis()) > gRebootMs)
{
// time to reboot
NVIC_SystemReset();
}
doSleepAlert(fDeepSleep);
/* count what we're up to */
updateSleepCounters();
send_in_progress = false;
}
void deepSleepPrepare(void)
+1 -1
View File
@@ -56,7 +56,7 @@ enum {
|
\****************************************************************************/
static const int32_t fwVersion = 20200530;
static const int32_t fwVersion = 20200602;
static const byte INIT_PACKAGE_INTERVAL = 100; // send an init package every 100 packages;
static const byte MAX_VALUES_TO_SEND = 8;