mirror of
https://github.com/markqvist/RNode_Firmware.git
synced 2025-01-13 16:29:39 -05:00
Attach packet RSSI and SNR to packet queue entry structs. Disable ISR spinlocks on ESP32 until tested.
This commit is contained in:
parent
1de5f3c796
commit
389745ad33
@ -45,6 +45,8 @@ volatile bool serial_buffering = false;
|
|||||||
#define MODEM_QUEUE_SIZE 4
|
#define MODEM_QUEUE_SIZE 4
|
||||||
typedef struct {
|
typedef struct {
|
||||||
size_t len;
|
size_t len;
|
||||||
|
int rssi;
|
||||||
|
int snr_raw;
|
||||||
uint8_t data[];
|
uint8_t data[];
|
||||||
} modem_packet_t;
|
} modem_packet_t;
|
||||||
static xQueueHandle modem_packet_queue = NULL;
|
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() {
|
inline void kiss_write_packet() {
|
||||||
serial_write(FEND);
|
serial_write(FEND);
|
||||||
serial_write(CMD_DATA);
|
serial_write(CMD_DATA);
|
||||||
|
|
||||||
for (uint16_t i = 0; i < read_len; i++) {
|
for (uint16_t i = 0; i < read_len; i++) {
|
||||||
#if MCU_VARIANT == MCU_ESP32
|
#if MCU_VARIANT == MCU_NRF52
|
||||||
portENTER_CRITICAL(&update_lock);
|
|
||||||
#elif MCU_VARIANT == MCU_NRF52
|
|
||||||
portENTER_CRITICAL();
|
portENTER_CRITICAL();
|
||||||
#endif
|
|
||||||
uint8_t byte = pbuf[i];
|
uint8_t byte = pbuf[i];
|
||||||
#if MCU_VARIANT == MCU_ESP32
|
|
||||||
portEXIT_CRITICAL(&update_lock);
|
|
||||||
#elif MCU_VARIANT == MCU_NRF52
|
|
||||||
portEXIT_CRITICAL();
|
portEXIT_CRITICAL();
|
||||||
|
#else
|
||||||
|
uint8_t byte = pbuf[i];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (byte == FEND) { serial_write(FESC); byte = TFEND; }
|
if (byte == FEND) { serial_write(FESC); byte = TFEND; }
|
||||||
if (byte == FESC) { serial_write(FESC); byte = TFESC; }
|
if (byte == FESC) { serial_write(FESC); byte = TFESC; }
|
||||||
serial_write(byte);
|
serial_write(byte);
|
||||||
}
|
}
|
||||||
|
|
||||||
serial_write(FEND);
|
serial_write(FEND);
|
||||||
read_len = 0;
|
read_len = 0;
|
||||||
|
|
||||||
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
||||||
packet_ready = false;
|
packet_ready = false;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if MCU_VARIANT == MCU_ESP32
|
#if MCU_VARIANT == MCU_ESP32
|
||||||
#if HAS_BLE
|
#if HAS_BLE
|
||||||
bt_flush();
|
bt_flush();
|
||||||
@ -277,17 +276,24 @@ inline void kiss_write_packet() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline void getPacketData(uint16_t len) {
|
inline void getPacketData(uint16_t len) {
|
||||||
|
#if MCU_VARIANT != MCU_NRF52
|
||||||
|
while (len-- && read_len < MTU) {
|
||||||
|
pbuf[read_len++] = LoRa->read();
|
||||||
|
}
|
||||||
|
#else
|
||||||
BaseType_t int_mask = taskENTER_CRITICAL_FROM_ISR();
|
BaseType_t int_mask = taskENTER_CRITICAL_FROM_ISR();
|
||||||
while (len-- && read_len < MTU) {
|
while (len-- && read_len < MTU) {
|
||||||
pbuf[read_len++] = LoRa->read();
|
pbuf[read_len++] = LoRa->read();
|
||||||
}
|
}
|
||||||
taskEXIT_CRITICAL_FROM_ISR(int_mask);
|
taskEXIT_CRITICAL_FROM_ISR(int_mask);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void ISR_VECT receive_callback(int packet_size) {
|
void ISR_VECT receive_callback(int packet_size) {
|
||||||
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
||||||
BaseType_t int_mask;
|
BaseType_t int_mask;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (!promisc) {
|
if (!promisc) {
|
||||||
// The standard operating mode allows large
|
// The standard operating mode allows large
|
||||||
// packets with a payload up to 500 bytes,
|
// 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
|
// This is the first part of a split
|
||||||
// packet, so we set the seq variable
|
// packet, so we set the seq variable
|
||||||
// and add the data to the buffer
|
// and add the data to the buffer
|
||||||
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
#if MCU_VARIANT == MCU_NRF52
|
||||||
int_mask = taskENTER_CRITICAL_FROM_ISR();
|
int_mask = taskENTER_CRITICAL_FROM_ISR(); read_len = 0; taskEXIT_CRITICAL_FROM_ISR(int_mask);
|
||||||
#endif
|
#else
|
||||||
read_len = 0;
|
read_len = 0;
|
||||||
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
|
||||||
taskEXIT_CRITICAL_FROM_ISR(int_mask);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
seq = sequence;
|
seq = sequence;
|
||||||
|
|
||||||
#if MCU_VARIANT != MCU_ESP32 && MCU_VARIANT != MCU_NRF52
|
#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
|
// This is the second part of a split
|
||||||
// packet, so we add it to the buffer
|
// packet, so we add it to the buffer
|
||||||
// and set the ready flag.
|
// and set the ready flag.
|
||||||
|
|
||||||
|
|
||||||
#if MCU_VARIANT != MCU_ESP32 && MCU_VARIANT != MCU_NRF52
|
#if MCU_VARIANT != MCU_ESP32 && MCU_VARIANT != MCU_NRF52
|
||||||
last_rssi = (last_rssi+LoRa->packetRssi())/2;
|
last_rssi = (last_rssi+LoRa->packetRssi())/2;
|
||||||
last_snr_raw = (last_snr_raw+LoRa->packetSnrRaw())/2;
|
last_snr_raw = (last_snr_raw+LoRa->packetSnrRaw())/2;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
getPacketData(packet_size);
|
getPacketData(packet_size);
|
||||||
|
|
||||||
seq = SEQ_UNSET;
|
seq = SEQ_UNSET;
|
||||||
ready = true;
|
ready = true;
|
||||||
|
|
||||||
@ -339,12 +341,10 @@ void ISR_VECT receive_callback(int packet_size) {
|
|||||||
// same sequence id, so we must assume
|
// same sequence id, so we must assume
|
||||||
// that we are seeing the first part of
|
// that we are seeing the first part of
|
||||||
// a new split packet.
|
// a new split packet.
|
||||||
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
#if MCU_VARIANT == MCU_NRF52
|
||||||
int_mask = taskENTER_CRITICAL_FROM_ISR();
|
int_mask = taskENTER_CRITICAL_FROM_ISR(); read_len = 0; taskEXIT_CRITICAL_FROM_ISR(int_mask);
|
||||||
#endif
|
#else
|
||||||
read_len = 0;
|
read_len = 0;
|
||||||
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
|
||||||
taskEXIT_CRITICAL_FROM_ISR(int_mask);
|
|
||||||
#endif
|
#endif
|
||||||
seq = sequence;
|
seq = sequence;
|
||||||
|
|
||||||
@ -363,12 +363,10 @@ void ISR_VECT receive_callback(int packet_size) {
|
|||||||
if (seq != SEQ_UNSET) {
|
if (seq != SEQ_UNSET) {
|
||||||
// If we already had part of a split
|
// If we already had part of a split
|
||||||
// packet in the buffer, we clear it.
|
// packet in the buffer, we clear it.
|
||||||
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
#if MCU_VARIANT == MCU_NRF52
|
||||||
int_mask = taskENTER_CRITICAL_FROM_ISR();
|
int_mask = taskENTER_CRITICAL_FROM_ISR(); read_len = 0; taskEXIT_CRITICAL_FROM_ISR(int_mask);
|
||||||
#endif
|
#else
|
||||||
read_len = 0;
|
read_len = 0;
|
||||||
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
|
|
||||||
taskEXIT_CRITICAL_FROM_ISR(int_mask);
|
|
||||||
#endif
|
#endif
|
||||||
seq = SEQ_UNSET;
|
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);
|
modem_packet_t *modem_packet = (modem_packet_t*)malloc(sizeof(modem_packet_t) + read_len);
|
||||||
if(!modem_packet) { memory_low = true; return; }
|
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
|
// Send packet to event queue, but free the
|
||||||
// allocated memory again if the queue is
|
// allocated memory again if the queue is
|
||||||
// unable to receive the packet.
|
// 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) {
|
if (!modem_packet_queue || xQueueSendFromISR(modem_packet_queue, &modem_packet, NULL) != pdPASS) {
|
||||||
free(modem_packet);
|
free(modem_packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -458,9 +461,7 @@ bool startRadio() {
|
|||||||
getFrequency();
|
getFrequency();
|
||||||
|
|
||||||
LoRa->enableCrc();
|
LoRa->enableCrc();
|
||||||
|
|
||||||
LoRa->onReceive(receive_callback);
|
LoRa->onReceive(receive_callback);
|
||||||
|
|
||||||
lora_receive();
|
lora_receive();
|
||||||
|
|
||||||
// Flash an info pattern to indicate
|
// 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() {
|
void updateModemStatus() {
|
||||||
#if MCU_VARIANT == MCU_ESP32
|
#if MCU_VARIANT == MCU_ESP32
|
||||||
portENTER_CRITICAL(&update_lock);
|
portENTER_CRITICAL(&update_lock);
|
||||||
@ -1343,15 +1348,13 @@ void loop() {
|
|||||||
#if MCU_VARIANT == MCU_ESP32
|
#if MCU_VARIANT == MCU_ESP32
|
||||||
modem_packet_t *modem_packet = NULL;
|
modem_packet_t *modem_packet = NULL;
|
||||||
if(modem_packet_queue && xQueueReceive(modem_packet_queue, &modem_packet, 0) == pdTRUE && modem_packet) {
|
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;
|
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);
|
free(modem_packet);
|
||||||
modem_packet = NULL;
|
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_rssi();
|
||||||
kiss_indicate_stat_snr();
|
kiss_indicate_stat_snr();
|
||||||
kiss_write_packet();
|
kiss_write_packet();
|
||||||
|
@ -562,6 +562,14 @@ int ISR_VECT sx126x::packetRssi() {
|
|||||||
return pkt_rssi;
|
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 ISR_VECT sx126x::packetSnrRaw() {
|
||||||
uint8_t buf[3] = {0};
|
uint8_t buf[3] = {0};
|
||||||
executeOpcodeRead(OP_PACKET_STATUS_6X, buf, 3);
|
executeOpcodeRead(OP_PACKET_STATUS_6X, buf, 3);
|
||||||
|
1
sx126x.h
1
sx126x.h
@ -36,6 +36,7 @@ public:
|
|||||||
|
|
||||||
int parsePacket(int size = 0);
|
int parsePacket(int size = 0);
|
||||||
int packetRssi();
|
int packetRssi();
|
||||||
|
int packetRssi(uint8_t pkt_snr_raw);
|
||||||
int currentRssi();
|
int currentRssi();
|
||||||
uint8_t packetRssiRaw();
|
uint8_t packetRssiRaw();
|
||||||
uint8_t currentRssiRaw();
|
uint8_t currentRssiRaw();
|
||||||
|
17
sx127x.cpp
17
sx127x.cpp
@ -230,6 +230,23 @@ uint8_t sx127x::packetRssiRaw() {
|
|||||||
return pkt_rssi_value;
|
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 ISR_VECT sx127x::packetRssi() {
|
||||||
int pkt_rssi = (int)readRegister(REG_PKT_RSSI_VALUE_7X) - RSSI_OFFSET;
|
int pkt_rssi = (int)readRegister(REG_PKT_RSSI_VALUE_7X) - RSSI_OFFSET;
|
||||||
int pkt_snr = packetSnr();
|
int pkt_snr = packetSnr();
|
||||||
|
1
sx127x.h
1
sx127x.h
@ -33,6 +33,7 @@ public:
|
|||||||
|
|
||||||
int parsePacket(int size = 0);
|
int parsePacket(int size = 0);
|
||||||
int packetRssi();
|
int packetRssi();
|
||||||
|
int packetRssi(uint8_t pkt_snr_raw);
|
||||||
int currentRssi();
|
int currentRssi();
|
||||||
uint8_t packetRssiRaw();
|
uint8_t packetRssiRaw();
|
||||||
uint8_t currentRssiRaw();
|
uint8_t currentRssiRaw();
|
||||||
|
@ -523,6 +523,14 @@ int ISR_VECT sx128x::packetRssi() {
|
|||||||
return pkt_rssi;
|
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 ISR_VECT sx128x::packetSnrRaw() {
|
||||||
uint8_t buf[5] = {0};
|
uint8_t buf[5] = {0};
|
||||||
executeOpcodeRead(OP_PACKET_STATUS_8X, buf, 5);
|
executeOpcodeRead(OP_PACKET_STATUS_8X, buf, 5);
|
||||||
|
1
sx128x.h
1
sx128x.h
@ -35,6 +35,7 @@ public:
|
|||||||
|
|
||||||
int parsePacket(int size = 0);
|
int parsePacket(int size = 0);
|
||||||
int packetRssi();
|
int packetRssi();
|
||||||
|
int packetRssi(uint8_t pkt_snr_raw);
|
||||||
int currentRssi();
|
int currentRssi();
|
||||||
uint8_t packetRssiRaw();
|
uint8_t packetRssiRaw();
|
||||||
uint8_t currentRssiRaw();
|
uint8_t currentRssiRaw();
|
||||||
|
Loading…
Reference in New Issue
Block a user