LMIC message queuing refactored

This commit is contained in:
Verkehrsrot 2019-08-30 14:06:30 +02:00
parent 1cf12452ba
commit 63ff668a96
2 changed files with 37 additions and 17 deletions

View File

@ -387,17 +387,39 @@ void lora_send(void *pvParameters) {
MessageBuffer_t SendBuffer; MessageBuffer_t SendBuffer;
while (1) { while (1) {
// fetch next or wait for payload to send from queue
if (xQueueReceive(LoraSendQueue, &SendBuffer, portMAX_DELAY) == pdTRUE) {
if (LMIC.opmode & OP_TXRXPEND) // LMIC is busy, we can't send... // wait until we are joined if we are not
lora_enqueuedata(&SendBuffer); // ...so we re-enqueue the message while (!LMIC.devaddr) {
else if (LMIC_setTxData2(SendBuffer.MessagePort, SendBuffer.Message, vTaskDelay(pdMS_TO_TICKS(500));
SendBuffer.MessageSize, }
(cfg.countermode & 0x02)) == 0)
// fetch next or wait for payload to send from queue
if (xQueueReceive(LoraSendQueue, &SendBuffer, portMAX_DELAY) != pdTRUE) {
ESP_LOGE(TAG, "Premature return from xQueueReceive() with no data!");
continue;
}
// attempt to transmit payload
else {
switch (LMIC_setTxData2(SendBuffer.MessagePort, SendBuffer.Message,
SendBuffer.MessageSize,
(cfg.countermode & 0x02))) {
case 0:
ESP_LOGI(TAG, "%d byte(s) delivered to LMIC", SendBuffer.MessageSize); ESP_LOGI(TAG, "%d byte(s) delivered to LMIC", SendBuffer.MessageSize);
else // LMIC stack denied tx ... break;
lora_enqueuedata(&SendBuffer); // ...so we re-enqueue the message case -1: // LMIC already has a tx message pending
ESP_LOGD(TAG, "LMIC busy, message re-enqueued");
vTaskDelay(pdMS_TO_TICKS(1000 + random(500))); // wait a while
lora_enqueuedata(&SendBuffer); // re-enqueue the undeliverd message
break;
case -2: // message size exceeds LMIC buffer size
ESP_LOGW(TAG, "Message size exceeds LMIC buffer, message deleted");
break;
default: // unknown LMIC return code
ESP_LOGE(TAG, "Unknown LMIC error, message deleted");
} // switch
} }
delay(2); // yield to CPU delay(2); // yield to CPU
} }
@ -423,6 +445,10 @@ esp_err_t lora_stack_init() {
&lmicTask, // task handle &lmicTask, // task handle
1); // CPU core 1); // CPU core
if (!LMIC_startJoining()) { // start joining
ESP_LOGI(TAG, "Already joined");
}
// start lmic send task // start lmic send task
xTaskCreatePinnedToCore(lora_send, // task function xTaskCreatePinnedToCore(lora_send, // task function
"lorasendtask", // name of task "lorasendtask", // name of task
@ -432,10 +458,6 @@ esp_err_t lora_stack_init() {
&lorasendTask, // task handle &lorasendTask, // task handle
1); // CPU core 1); // CPU core
if (!LMIC_startJoining()) { // start joining
ESP_LOGI(TAG, "Already joined");
}
return ESP_OK; return ESP_OK;
} }
@ -460,9 +482,7 @@ void lora_enqueuedata(MessageBuffer_t *message) {
ret = xQueueSendToBack(LoraSendQueue, (void *)message, (TickType_t)0); ret = xQueueSendToBack(LoraSendQueue, (void *)message, (TickType_t)0);
break; break;
} }
if (ret == pdTRUE) if (ret != pdTRUE)
ESP_LOGD(TAG, "LORA sendqueue data enqueued");
else
ESP_LOGW(TAG, "LORA sendqueue is full"); ESP_LOGW(TAG, "LORA sendqueue is full");
} }

View File

@ -56,7 +56,7 @@ void spi_slave_task(void *param) {
memset(txbuf, 0, sizeof(txbuf)); memset(txbuf, 0, sizeof(txbuf));
memset(rxbuf, 0, sizeof(rxbuf)); memset(rxbuf, 0, sizeof(rxbuf));
// wait until data to send arrivey // fetch next or wait for payload to send from queue
if (xQueueReceive(SPISendQueue, &msg, portMAX_DELAY) != pdTRUE) { if (xQueueReceive(SPISendQueue, &msg, portMAX_DELAY) != pdTRUE) {
ESP_LOGE(TAG, "Premature return from xQueueReceive() with no data!"); ESP_LOGE(TAG, "Premature return from xQueueReceive() with no data!");
continue; continue;