From 258e830470ec2439329608315ca6c446ee862cc2 Mon Sep 17 00:00:00 2001 From: Verkehrsrot Date: Thu, 29 Aug 2019 10:13:47 +0200 Subject: [PATCH 1/3] move lmic os sendjob to new lora_send task --- include/lorawan.h | 2 +- src/lorawan.cpp | 51 ++++++++++++++++++++++++++--------------------- src/main.cpp | 1 + 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/include/lorawan.h b/include/lorawan.h index 5762297c..0010f9de 100644 --- a/include/lorawan.h +++ b/include/lorawan.h @@ -34,7 +34,7 @@ void os_getArtEui(u1_t *buf); void os_getDevEui(u1_t *buf); void showLoraKeys(void); void switch_lora(uint8_t sf, uint8_t tx); -void lora_send(osjob_t *job); +void lora_send(void *pvParameters); void lora_enqueuedata(MessageBuffer_t *message); void lora_queuereset(void); #if (TIME_SYNC_LORAWAN) diff --git a/src/lorawan.cpp b/src/lorawan.cpp index fefb5db0..67fb175b 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -18,9 +18,8 @@ static const char TAG[] = "lora"; #endif #endif -osjob_t sendjob; QueueHandle_t LoraSendQueue; -TaskHandle_t lmicTask = NULL; +TaskHandle_t lmicTask = NULL, lorasendTask = NULL; class MyHalConfig_t : public Arduino_LMIC::HalConfiguration_t { @@ -218,8 +217,6 @@ void onEvent(ev_t ev) { // Set data rate and transmit power (note: txpower seems to be ignored by // the library) switch_lora(cfg.lorasf, cfg.txpower); - // kickoff first send job - os_setCallback(&sendjob, lora_send); // show effective LoRa parameters after join ESP_LOGI(TAG, "DEVaddr=%08X", LMIC.devaddr); break; @@ -248,11 +245,6 @@ void onEvent(ev_t ev) { : PSTR("TX COMPLETE")); sprintf(display_line6, " "); // clear previous lmic status - // schedule next transmission with some random delay to prevent systematic - // collisions - os_setTimedCallback(&sendjob, os_getTime() + ms2osticks(random(500)), - lora_send); - if (LMIC.dataLen) { // did we receive payload data -> display info ESP_LOGI(TAG, "Received %d bytes of payload, RSSI %d SNR %d", LMIC.dataLen, LMIC.rssi, LMIC.snr / 4); @@ -394,23 +386,27 @@ void switch_lora(uint8_t sf, uint8_t tx) { } } -void lora_send(osjob_t *job) { +// LMIC send task +void lora_send(void *pvParameters) { + configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check + MessageBuffer_t SendBuffer; - // Check if there is not a current TX/RX job running - if (LMIC.opmode & OP_TXRXPEND) { - ESP_LOGE(TAG, "LMIC busy, data not sent and lost"); - return; - } + while (1) { + // fetch next / wait for payload to send from queue + if (xQueueReceive(LoraSendQueue, &SendBuffer, portMAX_DELAY) == pdTRUE) { - // fetch next payload to send from queue or wait until new payload shows up in - // queue - if (xQueueReceive(LoraSendQueue, &SendBuffer, portMAX_DELAY) == pdTRUE) { - if (LMIC_setTxData2(SendBuffer.MessagePort, SendBuffer.Message, - SendBuffer.MessageSize, (cfg.countermode & 0x02)) == 0) - ESP_LOGI(TAG, "%d byte(s) delivered to LMIC", SendBuffer.MessageSize); - else - lora_enqueuedata(&SendBuffer); // re-enqueue unsent message + // Check if there is not a current TX/RX job running + if (LMIC.opmode & OP_TXRXPEND) + lora_enqueuedata(&SendBuffer); // LMIC stack busy + else if (LMIC_setTxData2(SendBuffer.MessagePort, SendBuffer.Message, + SendBuffer.MessageSize, + (cfg.countermode & 0x02)) == 0) + ESP_LOGI(TAG, "%d byte(s) delivered to LMIC", SendBuffer.MessageSize); + else + lora_enqueuedata(&SendBuffer); // LMIC stack denied tx + } + delay(2); // yield to CPU } } @@ -434,6 +430,15 @@ esp_err_t lora_stack_init() { &lmicTask, // task handle 1); // CPU core + // starting lmic send task + xTaskCreatePinnedToCore(lora_send, // task function + "lorasendtask", // name of task + 2048, // stack size of task + (void *)1, // parameter of the task + 2, // priority of the task + &lorasendTask, // task handle + 1); // CPU core + if (!LMIC_startJoining()) { // start joining ESP_LOGI(TAG, "Already joined"); } diff --git a/src/main.cpp b/src/main.cpp index eb9398aa..f042165f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,6 +36,7 @@ timesync_req 1 3 processes realtime time sync requests 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 +lorasendtask 1 1 feed data from lora sendqueue to lmcic IDLE 1 0 ESP32 arduino scheduler -> runs wifi channel rotator Low priority numbers denote low priority tasks. From c97140d822315dc49e39054f153b3e5ba1568959 Mon Sep 17 00:00:00 2001 From: Verkehrsrot Date: Thu, 29 Aug 2019 10:15:11 +0200 Subject: [PATCH 2/3] remove TX TIMESYNC event display --- src/lorawan.cpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 67fb175b..1a509380 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -305,13 +305,7 @@ void onEvent(ev_t ev) { case EV_TXSTART: if (!(LMIC.opmode & OP_JOINING)) { -#if (TIME_SYNC_LORASERVER) - // if last packet sent was a timesync request, store TX time - if (LMIC.pendTxPort == TIMEPORT) - strcpy_P(buff, PSTR("TX TIMESYNC")); - else -#endif - strcpy_P(buff, PSTR("TX START")); + strcpy_P(buff, PSTR("TX START")); } break; From 1cf12452bac572f061856ae106b8ee4649ae4ef0 Mon Sep 17 00:00:00 2001 From: Verkehrsrot Date: Thu, 29 Aug 2019 20:23:22 +0200 Subject: [PATCH 3/3] lorawan.cpp code sanitizations --- src/lorawan.cpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 1a509380..bbf8b0a6 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -387,18 +387,17 @@ void lora_send(void *pvParameters) { MessageBuffer_t SendBuffer; while (1) { - // fetch next / wait for payload to send from queue + // fetch next or wait for payload to send from queue if (xQueueReceive(LoraSendQueue, &SendBuffer, portMAX_DELAY) == pdTRUE) { - // Check if there is not a current TX/RX job running - if (LMIC.opmode & OP_TXRXPEND) - lora_enqueuedata(&SendBuffer); // LMIC stack busy + if (LMIC.opmode & OP_TXRXPEND) // LMIC is busy, we can't send... + lora_enqueuedata(&SendBuffer); // ...so we re-enqueue the message else if (LMIC_setTxData2(SendBuffer.MessagePort, SendBuffer.Message, SendBuffer.MessageSize, (cfg.countermode & 0x02)) == 0) ESP_LOGI(TAG, "%d byte(s) delivered to LMIC", SendBuffer.MessageSize); - else - lora_enqueuedata(&SendBuffer); // LMIC stack denied tx + else // LMIC stack denied tx ... + lora_enqueuedata(&SendBuffer); // ...so we re-enqueue the message } delay(2); // yield to CPU } @@ -414,7 +413,7 @@ 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 + // start lorawan stack ESP_LOGI(TAG, "Starting LMIC..."); xTaskCreatePinnedToCore(lmictask, // task function "lmictask", // name of task @@ -424,12 +423,12 @@ esp_err_t lora_stack_init() { &lmicTask, // task handle 1); // CPU core - // starting lmic send task + // start lmic send task xTaskCreatePinnedToCore(lora_send, // task function "lorasendtask", // name of task 2048, // stack size of task (void *)1, // parameter of the task - 2, // priority of the task + 1, // priority of the task &lorasendTask, // task handle 1); // CPU core @@ -437,7 +436,7 @@ esp_err_t lora_stack_init() { ESP_LOGI(TAG, "Already joined"); } - return ESP_OK; // continue main program + return ESP_OK; } void lora_enqueuedata(MessageBuffer_t *message) {