From 1ca21bc39b091d0a6deae94a4c360bd15ee12e99 Mon Sep 17 00:00:00 2001 From: Pawel Jalocha Date: Tue, 13 Oct 2020 02:30:04 +0100 Subject: [PATCH] Minor improvements and debug for LoRaWAN code --- bin-arch.sh | 2 +- main/disp_oled.cpp | 18 +++++++++---- main/lorawan.h | 2 +- main/main.cpp | 2 +- main/rf.cpp | 65 +++++++++++++++++++++++++--------------------- 5 files changed, 51 insertions(+), 38 deletions(-) diff --git a/bin-arch.sh b/bin-arch.sh index 051706a..401fe12 100644 --- a/bin-arch.sh +++ b/bin-arch.sh @@ -1 +1 @@ -tar cvzf esp32-ogn-tracker-bin.tgz flash_USB?.sh esptool.py build/partitions.bin build/bootloader/bootloader.bin build/app-template.bin +tar cvzf esp32-ogn-tracker-bin.tgz flash_USB?.sh esptool.py build/partitions.bin build/bootloader/bootloader.bin build/app-template.bin utils/read_log utils/serial_dump diff --git a/main/disp_oled.cpp b/main/disp_oled.cpp index bac7171..b330ff6 100644 --- a/main/disp_oled.cpp +++ b/main/disp_oled.cpp @@ -830,14 +830,22 @@ void OLED_DrawLoRaWAN(u8g2_t *OLED, GPS_Position *GPS) // draw LoRaWAN status pa #ifdef WITH_LORAWAN const char *StateName[4] = { "Not-Joined", "Join-Req", "+Joined+", "PktSend" } ; int Len=Format_String(Line, "LoRaWAN: "); - if(WANdev.State<=3) Len+=Format_String(Line+Len, StateName[WANdev.State]); + if(WANdev.State==2) Len+=Format_Hex(Line+Len, WANdev.DevAddr); + else if(WANdev.State<=3) Len+=Format_String(Line+Len, StateName[WANdev.State]); else Len+=Format_Hex(Line+Len, WANdev.State); - Line[Len]=0; - u8g2_DrawStr(OLED, 0, 24, Line); + Line[Len]=0; u8g2_DrawStr(OLED, 0, 24, Line); + Len =Format_String(Line , "Up: "); Len+=Format_Hex(Line+Len, (uint16_t)WANdev.UpCount); Len+=Format_String(Line+Len, " Dn: "); Len+=Format_Hex(Line+Len, (uint16_t)WANdev.DnCount); - Line[Len]=0; - u8g2_DrawStr(OLED, 0, 36, Line); + Line[Len]=0; u8g2_DrawStr(OLED, 0, 36, Line); + + Len =Format_String(Line , "Rx:"); + Len+=Format_SignDec(Line+Len, (int16_t)WANdev.RxRSSI, 3); + Len+=Format_String(Line+Len, "dBm "); + Len+=Format_SignDec(Line+Len, ((int16_t)WANdev.RxSNR*10+2)>>2, 2, 1); + Len+=Format_String(Line+Len, "dB"); + Line[Len]=0; u8g2_DrawStr(OLED, 0, 48, Line); + // if(WANdev.State>=2) { } /* Len=0; diff --git a/main/lorawan.h b/main/lorawan.h index 7341816..88dd4fa 100644 --- a/main/lorawan.h +++ b/main/lorawan.h @@ -33,7 +33,7 @@ class LoRaWANnode uint32_t LastTx; // [sec] last transmission uint32_t RxCount; // [packets] received from the network uint32_t LastRx; // [sec] when last heard from the network - uint8_t RxSNR; // SNR on receive + int8_t RxSNR; // [0.25dB] SNR on receive int8_t RxRSSI; // [dBm] signal strength union { uint8_t Flags; diff --git a/main/main.cpp b/main/main.cpp index a83e6c9..2f804ef 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -123,7 +123,7 @@ void app_main(void) #ifdef WITH_SOUND xTaskCreate(vTaskSOUND, "SOUND", 2048, 0, tskIDLE_PRIORITY+3, 0); #endif - xTaskCreate(vTaskTICK , "TICK", 1024, 0, tskIDLE_PRIORITY+2, 0); + xTaskCreate(vTaskTICK , "TICK", 1024, 0, tskIDLE_PRIORITY+3, 0); // xTaskCreate(vTaskCTRL, "CTRL", 1536, 0, tskIDLE_PRIORITY+2, 0); vTaskCTRL(0); // run directly the CTRL task, instead of creating a separate one. diff --git a/main/rf.cpp b/main/rf.cpp index 47f21ee..859d6ff 100644 --- a/main/rf.cpp +++ b/main/rf.cpp @@ -204,7 +204,7 @@ static uint8_t StartRFchip(void) #endif return Version; } // read the RF chip version and return it -static TickType_t WAN_RespTick = xTaskGetTickCount(); // when to expect the WAN response +static TickType_t WAN_RespTick = 0; // when to expect the WAN response static RFM_LoRa_RxPacket WAN_RxPacket; // packet received from WAN // static WAN_Setup() // static WAN_Back() @@ -273,16 +273,16 @@ extern "C" { if(WAN_RespLeft<=5) WANdev.State--; else if(WAN_RespLeft<200) { WANrx=1; } xSemaphoreTake(CONS_Mutex, portMAX_DELAY); - Format_String(CONS_UART_Write, "LoRaWAN Rx: "); + Format_UnsDec(CONS_UART_Write, xTaskGetTickCount(), 4, 3); + Format_String(CONS_UART_Write, "s LoRaWAN Rx: "); Format_SignDec(CONS_UART_Write, WAN_RespLeft); - Format_String(CONS_UART_Write, "ms "); - Format_UnsDec(CONS_UART_Write, xTaskGetTickCount()); Format_String(CONS_UART_Write, "ms\n"); xSemaphoreGive(CONS_Mutex); } - if(WANrx) - { TRX.WriteMode(RF_OPMODE_STANDBY); // TRX to standby + if(WANrx) // if reception expected from WAN + { int RxLen=0; + TRX.WriteMode(RF_OPMODE_STANDBY); // TRX to standby TRX.setLoRa(); // switch to LoRa mode (through sleep) TRX.WriteMode(RF_OPMODE_LORA_STANDBY); // TRX in standby SetFreqPlanWAN(); // WAN frequency plan @@ -290,31 +290,37 @@ extern "C" TRX.setChannel(WANdev.Chan); // set the channel TRX.LoRa_InvertIQ(1); TRX.LoRa_setCRC(0); TRX.LoRa_setIRQ(0); // setup for WAN RX TRX.WriteMode(RF_OPMODE_LORA_RX_SINGLE); // wait for a single packet - int Wait=WAN_RespLeft+100; + int Wait=WAN_RespLeft+100; // 100ms timeout after the expected reception for( ; Wait>0; Wait--) { vTaskDelay(1); - if(TRX.readIRQ()) break; } + if(TRX.readIRQ()) break; } // IRQ signals packet reception if(Wait) { TRX.LoRa_ReceivePacket(WAN_RxPacket); xSemaphoreTake(CONS_Mutex, portMAX_DELAY); - Format_String(CONS_UART_Write, "LoRaWAN Rx: "); + Format_UnsDec(CONS_UART_Write, xTaskGetTickCount(), 4, 3); + Format_String(CONS_UART_Write, "s LoRaWAN Rx: "); Format_UnsDec(CONS_UART_Write, (uint16_t)WAN_RxPacket.Len); Format_String(CONS_UART_Write, "B/"); Format_UnsDec(CONS_UART_Write, (unsigned)Wait); - Format_String(CONS_UART_Write, "ms "); - Format_UnsDec(CONS_UART_Write, xTaskGetTickCount()); Format_String(CONS_UART_Write, "ms\n"); xSemaphoreGive(CONS_Mutex); - if(WANdev.State==1) WANdev.procJoinAccept(WAN_RxPacket); - else if(WANdev.State==3) WANdev.procRxData(WAN_RxPacket); + if(WANdev.State==1) WANdev.procJoinAccept(WAN_RxPacket); // if join-request state then expect a join-accept packet + else if(WANdev.State==3) RxLen=WANdev.procRxData(WAN_RxPacket); // if data send then respect ACK and/or downlink data packet } else WANdev.State--; - WANdev.WriteToNVS(); - TRX.setFSK(); // back to FSK - SetFreqPlanOGN(); // OGN frequency plan - TRX.OGN_Configure(0, OGN_SYNC); // OGN config + TRX.setFSK(); // back to FSK + SetFreqPlanOGN(); // OGN frequency plan + TRX.OGN_Configure(0, OGN_SYNC); // OGN config SetRxChannel(); TRX.WriteMode(RF_OPMODE_RECEIVER); // switch to receive mode + WANdev.WriteToNVS(); // store new WAN state in flash + if(RxLen>0) // if Downlink data received + { xSemaphoreTake(CONS_Mutex, portMAX_DELAY); + Format_String(CONS_UART_Write, "LoRaWAN Msg: "); + Format_UnsDec(CONS_UART_Write, (uint16_t)RxLen); + Format_String(CONS_UART_Write, "B"); + Format_String(CONS_UART_Write, "\n"); + xSemaphoreGive(CONS_Mutex); } } else #else @@ -412,18 +418,18 @@ extern "C" const FANET_Packet *FNTpkt = FNT_TxFIFO.getRead(0); // read the packet from the FANET transmitt queue if(FNTpkt) // was there any ? { TRX.setLoRa(); // switch TRX to LoRa - TRX.FNT_Configure(); + TRX.FNT_Configure(); // configure for FANET // TRX.setChannel(0); // configure for FANET - TRX.WriteTxPower(Parameters.getTxPower()); - TRX.WriteMode(RF_OPMODE_LORA_RX_CONT); + TRX.WriteTxPower(Parameters.getTxPower()); // transmission power + TRX.WriteMode(RF_OPMODE_LORA_RX_CONT); // continous receiver mode vTaskDelay(2); - for(uint8_t Wait=50; Wait; Wait--) - { vTaskDelay(1); - uint8_t Stat = TRX.ReadByte(REG_LORA_MODEM_STATUS); - if((Stat&0x0B)==0) break; } + for(uint8_t Wait=50; Wait; Wait--) // + { vTaskDelay(1); // every milisecond + uint8_t Stat = TRX.ReadByte(REG_LORA_MODEM_STATUS); // receiver status + if((Stat&0x0B)==0) break; } // 0:signal-det, 1:signal-sync, 3:header-valid TRX.WriteMode(RF_OPMODE_LORA_STANDBY); - TRX.FNT_SendPacket(FNTpkt->Byte, FNTpkt->Len); - FNT_TxFIFO.Read(); // remove the last packet from the queue + TRX.FNT_SendPacket(FNTpkt->Byte, FNTpkt->Len); // transmit the FANET packet + FNT_TxFIFO.Read(); // remove the last packet from the queue /* xSemaphoreTake(CONS_Mutex, portMAX_DELAY); Format_String(CONS_UART_Write, "TRX: "); @@ -500,11 +506,10 @@ extern "C" if(Mode!=RF_OPMODE_LORA_TX) break; } WAN_RespTick=xTaskGetTickCount()+RespDelay; // when to expect the response: 5sec after the end of Join-Request packet xSemaphoreTake(CONS_Mutex, portMAX_DELAY); - Format_String(CONS_UART_Write, "LoRaWAN Tx: "); + Format_UnsDec(CONS_UART_Write, xTaskGetTickCount(), 4, 3); + Format_String(CONS_UART_Write, "s LoRaWAN Tx: "); Format_UnsDec(CONS_UART_Write, (unsigned)TxPktLen); - Format_String(CONS_UART_Write, "B "); - Format_UnsDec(CONS_UART_Write, xTaskGetTickCount()); - Format_String(CONS_UART_Write, "ms\n"); + Format_String(CONS_UART_Write, "B\n"); xSemaphoreGive(CONS_Mutex); } TRX.setFSK(); // back to FSK