diff --git a/include/lorawan.h b/include/lorawan.h index 47170b4a..0b6cabad 100644 --- a/include/lorawan.h +++ b/include/lorawan.h @@ -21,8 +21,10 @@ #endif extern QueueHandle_t LoraSendQueue; +extern TaskHandle_t lmicTask; esp_err_t lora_stack_init(); +void lmictask(void *pvParameters); void onEvent(ev_t ev); void gen_lora_deveui(uint8_t *pdeveui); void RevBytes(unsigned char *b, size_t c); diff --git a/src/cyclic.cpp b/src/cyclic.cpp index 9658be78..9e338cef 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -34,6 +34,10 @@ void doHousekeeping() { ESP_LOGD(TAG, "IRQhandler %d bytes left | Taskstate = %d", uxTaskGetStackHighWaterMark(irqHandlerTask), eTaskGetState(irqHandlerTask)); +#if (HAS_LORA) + ESP_LOGD(TAG, "LMiCtask %d bytes left | Taskstate = %d", + uxTaskGetStackHighWaterMark(lmicTask), eTaskGetState(lmicTask)); +#endif #if (HAS_GPS) ESP_LOGD(TAG, "Gpsloop %d bytes left | Taskstate = %d", uxTaskGetStackHighWaterMark(GpsTask), eTaskGetState(GpsTask)); diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 228f3a1e..333218dd 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -20,6 +20,7 @@ static const char TAG[] = "lora"; osjob_t sendjob; QueueHandle_t LoraSendQueue; +TaskHandle_t lmicTask = NULL; class MyHalConfig_t : public Arduino_LMIC::HalConfiguration_t { @@ -423,28 +424,15 @@ esp_err_t lora_stack_init() { ESP_LOGI(TAG, "LORA send queue created, size %d Bytes", SEND_QUEUE_SIZE * sizeof(MessageBuffer_t)); + // starting lorawan stack ESP_LOGI(TAG, "Starting LMIC..."); - - os_init(); // initialize lmic run-time environment on core 1 - LMIC_reset(); // initialize lmic MAC - LMIC_setLinkCheckMode(0); - // This tells LMIC to make the receive windows bigger, in case your clock is - // faster or slower. This causes the transceiver to be earlier switched on, - // so consuming more power. You may sharpen (reduce) CLOCK_ERROR_PERCENTAGE - // in src/lmic_config.h if you are limited on battery. - LMIC_setClockError(MAX_CLOCK_ERROR * CLOCK_ERROR_PROCENTAGE / 100); - // Set the data rate to Spreading Factor 7. This is the fastest supported - // rate for 125 kHz channels, and it minimizes air time and battery power. - // Set the transmission power to 14 dBi (25 mW). - LMIC_setDrTxpow(DR_SF7, 14); - -#if defined(CFG_US915) || defined(CFG_au921) - // in the US, with TTN, it saves join time if we start on subband 1 - // (channels 8-15). This will get overridden after the join by parameters - // from the network. If working with other networks or in other regions, - // this will need to be changed. - LMIC_selectSubBand(1); -#endif + xTaskCreatePinnedToCore(lmictask, // task function + "lmictask", // name of task + 4096, // stack size of task + (void *)1, // parameter of the task + 2, // priority of the task + &lmicTask, // task handle + 1); // CPU core if (!LMIC_startJoining()) { // start joining ESP_LOGI(TAG, "Already joined"); @@ -531,4 +519,35 @@ finish: } // user_request_network_time_callback #endif // TIME_SYNC_LORAWAN +// LMIC lorawan stack task +void lmictask(void *pvParameters) { + configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check + + os_init(); // initialize lmic run-time environment on core 1 + LMIC_reset(); // initialize lmic MAC + LMIC_setLinkCheckMode(0); + // This tells LMIC to make the receive windows bigger, in case your clock is + // faster or slower. This causes the transceiver to be earlier switched on, + // so consuming more power. You may sharpen (reduce) CLOCK_ERROR_PERCENTAGE + // in src/lmic_config.h if you are limited on battery. + LMIC_setClockError(MAX_CLOCK_ERROR * CLOCK_ERROR_PROCENTAGE / 100); + // Set the data rate to Spreading Factor 7. This is the fastest supported + // rate for 125 kHz channels, and it minimizes air time and battery power. + // Set the transmission power to 14 dBi (25 mW). + LMIC_setDrTxpow(DR_SF7, 14); + +#if defined(CFG_US915) || defined(CFG_au921) + // in the US, with TTN, it saves join time if we start on subband 1 + // (channels 8-15). This will get overridden after the join by parameters + // from the network. If working with other networks or in other regions, + // this will need to be changed. + LMIC_selectSubBand(1); +#endif + + while (1) { + os_runloop_once(); // execute lmic scheduled jobs and events + delay(2); // yield to CPU + } +} // lmictask + #endif // HAS_LORA diff --git a/src/main.cpp b/src/main.cpp index 3674b6bb..d88722b5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,9 +33,10 @@ IDLE 0 0 ESP32 arduino scheduler -> runs wifi sniffer clockloop 1 4 generates realtime telegrams for external clock timesync_req 1 3 processes realtime time sync requests -irqhandler 1 2 display, timesync, gps, etc. triggered by timers -gpsloop 1 2 reads data from GPS via serial or i2c -looptask 1 1 runs the LMIC LoRa stack (arduino loop) +lmictask 1 2 MCCI LMiC LORAWAN stack +irqhandler 1 1 display, timesync, gps, etc. triggered by timers +gpsloop 1 1 reads data from GPS via serial or i2c +looptask 1 1 arduino loop (unused) IDLE 1 0 ESP32 arduino scheduler -> runs wifi channel rotator Low priority numbers denote low priority tasks. @@ -268,7 +269,7 @@ void setup() { "gpsloop", // name of task 2048, // stack size of task (void *)1, // parameter of the task - 2, // priority of the task + 1, // priority of the task &GpsTask, // task handle 1); // CPU core }