diff --git a/include/lorawan.h b/include/lorawan.h index 66d77f5b..94957c25 100644 --- a/include/lorawan.h +++ b/include/lorawan.h @@ -26,5 +26,9 @@ 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_enqueuedata(uint8_t messageType, MessageBuffer_t *message); +void lora_queuereset(void); +void lora_housekeeping(void); +esp_err_t lora_stack_init(); #endif \ No newline at end of file diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 35c9d944..ae0f08c0 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -341,4 +341,70 @@ void lora_send(osjob_t *job) { lora_send); } -#endif // HAS_LORA \ No newline at end of file +esp_err_t lora_stack_init() { +#ifndef HAS_LORA + return ESP_OK; // continue main program +#else + LoraSendQueue = xQueueCreate(SEND_QUEUE_SIZE, sizeof(MessageBuffer_t)); + if (LoraSendQueue == 0) { + ESP_LOGE(TAG, "Could not create LORA send queue. Aborting."); + return ESP_FAIL; + } + ESP_LOGI(TAG, "LORA send queue created, size %d Bytes", + SEND_QUEUE_SIZE * PAYLOAD_BUFFER_SIZE); + + 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 + + LMIC_startJoining(); // start joining + return ESP_OK; // continue main program +#endif +} + +#endif // HAS_LORA + +void lora_enqueuedata(uint8_t messageType, MessageBuffer_t *message) { + // enqueue message in LORA send queue +#ifdef HAS_LORA + BaseType_t ret = + xQueueSendToBack(LoraSendQueue, (void *)message, (TickType_t)0); + if (ret == pdTRUE) { + ESP_LOGI(TAG, "%d bytes enqueued for LORA interface", + message->MessageSize); + } else { + ESP_LOGW(TAG, "LORA sendqueue is full"); + } +#endif +} + +void lora_queuereset(void) { +#ifdef HAS_LORA + xQueueReset(LoraSendQueue); +#endif +} + +void lora_housekeeping(void) { +#ifdef HAS_LORA +// ESP_LOGD(TAG, "loraloop %d bytes left", +// uxTaskGetStackHighWaterMark(LoraTask)); +#endif +} diff --git a/src/main.cpp b/src/main.cpp index de80e718..46bf6ddf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -56,7 +56,7 @@ uint8_t volatile channel = 0; // channel rotation counter uint16_t volatile macs_total = 0, macs_wifi = 0, macs_ble = 0, batt_voltage = 0; // globals for display -hw_timer_t *channelSwitch, *sendCycle, *homeCycle, *displaytimer; // irq tasks +hw_timer_t *channelSwitch = NULL, *sendCycle = NULL, *homeCycle = NULL, *displaytimer = NULL; // irq tasks TaskHandle_t irqHandlerTask, wifiSwitchTask; std::set macs; // container holding unique MAC adress hashes @@ -161,39 +161,8 @@ void setup() { // initialize LoRa #ifdef HAS_LORA strcat_P(features, " LORA"); - LoraSendQueue = xQueueCreate(SEND_QUEUE_SIZE, sizeof(MessageBuffer_t)); - if (LoraSendQueue == 0) { - ESP_LOGE(TAG, "Could not create LORA send queue. Aborting."); - exit(0); - } else - ESP_LOGI(TAG, "LORA send queue created, size %d Bytes", - SEND_QUEUE_SIZE * PAYLOAD_BUFFER_SIZE); - - 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 - - LMIC_startJoining(); // start joining - #endif + assert(lora_stack_init() == ESP_OK); // initialize SPI #ifdef HAS_SPI diff --git a/src/senddata.cpp b/src/senddata.cpp index 89657114..b8edda34 100644 --- a/src/senddata.cpp +++ b/src/senddata.cpp @@ -13,19 +13,14 @@ void SendData(uint8_t port) { : (PAYLOAD_ENCODER == 4 ? LPP2PORT : LPP1PORT); memcpy(SendBuffer.Message, payload.getBuffer(), payload.getSize()); - // enqueue message in LoRa send queue -#ifdef HAS_LORA - if (xQueueSendToBack(LoraSendQueue, (void *)&SendBuffer, (TickType_t)0) == - pdTRUE) - ESP_LOGI(TAG, "%d bytes enqueued to send on LoRa", payload.getSize()); -#endif - + // enqueue message in device's send queues + lora_enqueuedata(port, &SendBuffer); spi_enqueuedata(port, &SendBuffer); // clear counter if not in cumulative counter mode if ((port == COUNTERPORT) && (cfg.countermode != 1)) { reset_counters(); // clear macs container and reset all counters - get_salt(); // get new salt for salting hashes + get_salt(); // get new salt for salting hashes ESP_LOGI(TAG, "Counter cleared"); } } // SendData @@ -59,9 +54,6 @@ void sendPayload() { } // sendpayload() void flushQueues() { -#ifdef HAS_LORA - xQueueReset(LoraSendQueue); -#endif - + lora_queuereset(); spi_queuereset(); } \ No newline at end of file diff --git a/src/spislave.cpp b/src/spislave.cpp index 7cd8d729..c6268d81 100644 --- a/src/spislave.cpp +++ b/src/spislave.cpp @@ -131,7 +131,7 @@ void spi_enqueuedata(uint8_t messageType, MessageBuffer_t *message) { BaseType_t ret = xQueueSendToBack(SPISendQueue, (void *)message, (TickType_t)0); if (ret == pdTRUE) { - ESP_LOGI(TAG, "%d bytes enqueued for SPI consumption", + ESP_LOGI(TAG, "%d bytes enqueued for SPI interface", message->MessageSize); } else { ESP_LOGW(TAG, "SPI sendqueue is full");