diff --git a/RNode_Firmware.ino b/RNode_Firmware.ino index a8ab553..2064abe 100644 --- a/RNode_Firmware.ino +++ b/RNode_Firmware.ino @@ -45,6 +45,8 @@ volatile bool serial_buffering = false; #define MODEM_QUEUE_SIZE 4 typedef struct { size_t len; + int rssi; + int snr_raw; uint8_t data[]; } modem_packet_t; static xQueueHandle modem_packet_queue = NULL; @@ -241,34 +243,31 @@ void lora_receive() { } } -#if MCU_VARIANT == MCU_ESP32 - portMUX_TYPE update_lock = portMUX_INITIALIZER_UNLOCKED; -#endif - inline void kiss_write_packet() { serial_write(FEND); serial_write(CMD_DATA); + for (uint16_t i = 0; i < read_len; i++) { - #if MCU_VARIANT == MCU_ESP32 - portENTER_CRITICAL(&update_lock); - #elif MCU_VARIANT == MCU_NRF52 + #if MCU_VARIANT == MCU_NRF52 portENTER_CRITICAL(); - #endif - uint8_t byte = pbuf[i]; - #if MCU_VARIANT == MCU_ESP32 - portEXIT_CRITICAL(&update_lock); - #elif MCU_VARIANT == MCU_NRF52 + uint8_t byte = pbuf[i]; portEXIT_CRITICAL(); + #else + uint8_t byte = pbuf[i]; #endif + if (byte == FEND) { serial_write(FESC); byte = TFEND; } if (byte == FESC) { serial_write(FESC); byte = TFESC; } serial_write(byte); } + serial_write(FEND); read_len = 0; + #if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52 packet_ready = false; #endif + #if MCU_VARIANT == MCU_ESP32 #if HAS_BLE bt_flush(); @@ -277,17 +276,24 @@ inline void kiss_write_packet() { } inline void getPacketData(uint16_t len) { - BaseType_t int_mask = taskENTER_CRITICAL_FROM_ISR(); - while (len-- && read_len < MTU) { - pbuf[read_len++] = LoRa->read(); - } - taskEXIT_CRITICAL_FROM_ISR(int_mask); + #if MCU_VARIANT != MCU_NRF52 + while (len-- && read_len < MTU) { + pbuf[read_len++] = LoRa->read(); + } + #else + BaseType_t int_mask = taskENTER_CRITICAL_FROM_ISR(); + while (len-- && read_len < MTU) { + pbuf[read_len++] = LoRa->read(); + } + taskEXIT_CRITICAL_FROM_ISR(int_mask); + #endif } void ISR_VECT receive_callback(int packet_size) { #if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52 - BaseType_t int_mask; + BaseType_t int_mask; #endif + if (!promisc) { // The standard operating mode allows large // packets with a payload up to 500 bytes, @@ -302,13 +308,12 @@ void ISR_VECT receive_callback(int packet_size) { // This is the first part of a split // packet, so we set the seq variable // and add the data to the buffer - #if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52 - int_mask = taskENTER_CRITICAL_FROM_ISR(); - #endif - read_len = 0; - #if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52 - taskEXIT_CRITICAL_FROM_ISR(int_mask); + #if MCU_VARIANT == MCU_NRF52 + int_mask = taskENTER_CRITICAL_FROM_ISR(); read_len = 0; taskEXIT_CRITICAL_FROM_ISR(int_mask); + #else + read_len = 0; #endif + seq = sequence; #if MCU_VARIANT != MCU_ESP32 && MCU_VARIANT != MCU_NRF52 @@ -322,15 +327,12 @@ void ISR_VECT receive_callback(int packet_size) { // This is the second part of a split // packet, so we add it to the buffer // and set the ready flag. - - #if MCU_VARIANT != MCU_ESP32 && MCU_VARIANT != MCU_NRF52 last_rssi = (last_rssi+LoRa->packetRssi())/2; last_snr_raw = (last_snr_raw+LoRa->packetSnrRaw())/2; #endif getPacketData(packet_size); - seq = SEQ_UNSET; ready = true; @@ -339,12 +341,10 @@ void ISR_VECT receive_callback(int packet_size) { // same sequence id, so we must assume // that we are seeing the first part of // a new split packet. - #if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52 - int_mask = taskENTER_CRITICAL_FROM_ISR(); - #endif - read_len = 0; - #if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52 - taskEXIT_CRITICAL_FROM_ISR(int_mask); + #if MCU_VARIANT == MCU_NRF52 + int_mask = taskENTER_CRITICAL_FROM_ISR(); read_len = 0; taskEXIT_CRITICAL_FROM_ISR(int_mask); + #else + read_len = 0; #endif seq = sequence; @@ -363,12 +363,10 @@ void ISR_VECT receive_callback(int packet_size) { if (seq != SEQ_UNSET) { // If we already had part of a split // packet in the buffer, we clear it. - #if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52 - int_mask = taskENTER_CRITICAL_FROM_ISR(); - #endif - read_len = 0; - #if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52 - taskEXIT_CRITICAL_FROM_ISR(int_mask); + #if MCU_VARIANT == MCU_NRF52 + int_mask = taskENTER_CRITICAL_FROM_ISR(); read_len = 0; taskEXIT_CRITICAL_FROM_ISR(int_mask); + #else + read_len = 0; #endif seq = SEQ_UNSET; } @@ -398,6 +396,12 @@ void ISR_VECT receive_callback(int packet_size) { modem_packet_t *modem_packet = (modem_packet_t*)malloc(sizeof(modem_packet_t) + read_len); if(!modem_packet) { memory_low = true; return; } + // Get packet RSSI and SNR + #if MCU_VARIANT == MCU_ESP32 + modem_packet->snr_raw = LoRa->packetSnrRaw(); + modem_packet->rssi = LoRa->packetRssi(modem_packet->snr_raw); + #endif + // Send packet to event queue, but free the // allocated memory again if the queue is // unable to receive the packet. @@ -406,7 +410,6 @@ void ISR_VECT receive_callback(int packet_size) { if (!modem_packet_queue || xQueueSendFromISR(modem_packet_queue, &modem_packet, NULL) != pdPASS) { free(modem_packet); } - #endif } } else { @@ -458,9 +461,7 @@ bool startRadio() { getFrequency(); LoRa->enableCrc(); - LoRa->onReceive(receive_callback); - lora_receive(); // Flash an info pattern to indicate @@ -1113,6 +1114,10 @@ void serialCallback(uint8_t sbyte) { } } +#if MCU_VARIANT == MCU_ESP32 + portMUX_TYPE update_lock = portMUX_INITIALIZER_UNLOCKED; +#endif + void updateModemStatus() { #if MCU_VARIANT == MCU_ESP32 portENTER_CRITICAL(&update_lock); @@ -1343,15 +1348,13 @@ void loop() { #if MCU_VARIANT == MCU_ESP32 modem_packet_t *modem_packet = NULL; if(modem_packet_queue && xQueueReceive(modem_packet_queue, &modem_packet, 0) == pdTRUE && modem_packet) { - memcpy(&pbuf, modem_packet->data, modem_packet->len); read_len = modem_packet->len; + last_rssi = modem_packet->rssi; + last_snr_raw = modem_packet->snr_raw; + memcpy(&pbuf, modem_packet->data, modem_packet->len); free(modem_packet); modem_packet = NULL; - portENTER_CRITICAL(&update_lock); - last_rssi = LoRa->packetRssi(); - last_snr_raw = LoRa->packetSnrRaw(); - portEXIT_CRITICAL(&update_lock); kiss_indicate_stat_rssi(); kiss_indicate_stat_snr(); kiss_write_packet(); diff --git a/sx126x.cpp b/sx126x.cpp index 1ed81a1..c0c07b1 100644 --- a/sx126x.cpp +++ b/sx126x.cpp @@ -562,6 +562,14 @@ int ISR_VECT sx126x::packetRssi() { return pkt_rssi; } +int ISR_VECT sx126x::packetRssi(uint8_t pkt_snr_raw) { + // may need more calculations here + uint8_t buf[3] = {0}; + executeOpcodeRead(OP_PACKET_STATUS_6X, buf, 3); + int pkt_rssi = -buf[0] / 2; + return pkt_rssi; +} + uint8_t ISR_VECT sx126x::packetSnrRaw() { uint8_t buf[3] = {0}; executeOpcodeRead(OP_PACKET_STATUS_6X, buf, 3); diff --git a/sx126x.h b/sx126x.h index 0aec47a..922c605 100644 --- a/sx126x.h +++ b/sx126x.h @@ -36,6 +36,7 @@ public: int parsePacket(int size = 0); int packetRssi(); + int packetRssi(uint8_t pkt_snr_raw); int currentRssi(); uint8_t packetRssiRaw(); uint8_t currentRssiRaw(); diff --git a/sx127x.cpp b/sx127x.cpp index 5548d51..8cbe402 100644 --- a/sx127x.cpp +++ b/sx127x.cpp @@ -230,6 +230,23 @@ uint8_t sx127x::packetRssiRaw() { return pkt_rssi_value; } +int ISR_VECT sx127x::packetRssi(uint8_t pkt_snr_raw) { + int pkt_rssi = (int)readRegister(REG_PKT_RSSI_VALUE_7X) - RSSI_OFFSET; + int pkt_snr = ((int8_t)pkt_snr_raw)*0.25; + + if (_frequency < 820E6) pkt_rssi -= 7; + + if (pkt_snr < 0) { + pkt_rssi += pkt_snr; + } else { + // Slope correction is (16/15)*pkt_rssi, + // this estimation looses one floating point + // operation, and should be precise enough. + pkt_rssi = (int)(1.066 * pkt_rssi); + } + return pkt_rssi; +} + int ISR_VECT sx127x::packetRssi() { int pkt_rssi = (int)readRegister(REG_PKT_RSSI_VALUE_7X) - RSSI_OFFSET; int pkt_snr = packetSnr(); diff --git a/sx127x.h b/sx127x.h index f154b44..12a8419 100644 --- a/sx127x.h +++ b/sx127x.h @@ -33,6 +33,7 @@ public: int parsePacket(int size = 0); int packetRssi(); + int packetRssi(uint8_t pkt_snr_raw); int currentRssi(); uint8_t packetRssiRaw(); uint8_t currentRssiRaw(); diff --git a/sx128x.cpp b/sx128x.cpp index e965a46..9dc4b3a 100644 --- a/sx128x.cpp +++ b/sx128x.cpp @@ -523,6 +523,14 @@ int ISR_VECT sx128x::packetRssi() { return pkt_rssi; } +int ISR_VECT sx128x::packetRssi(uint8_t pkt_snr_raw) { + // may need more calculations here + uint8_t buf[5] = {0}; + executeOpcodeRead(OP_PACKET_STATUS_8X, buf, 5); + int pkt_rssi = -buf[0] / 2; + return pkt_rssi; +} + uint8_t ISR_VECT sx128x::packetSnrRaw() { uint8_t buf[5] = {0}; executeOpcodeRead(OP_PACKET_STATUS_8X, buf, 5); diff --git a/sx128x.h b/sx128x.h index 213fc48..9e13eec 100644 --- a/sx128x.h +++ b/sx128x.h @@ -35,6 +35,7 @@ public: int parsePacket(int size = 0); int packetRssi(); + int packetRssi(uint8_t pkt_snr_raw); int currentRssi(); uint8_t packetRssiRaw(); uint8_t currentRssiRaw();