From 51afd310a5e84102b96f2cd8a5411d041c1fe425 Mon Sep 17 00:00:00 2001 From: "jacob.eva" Date: Wed, 11 Sep 2024 20:18:43 +0100 Subject: [PATCH] Revert to ISR handling packets and cap SX1280 data rate --- Config.h | 1 + RNode_Firmware_CE.ino | 61 +++++++++++++++---------------------------- Radio.cpp | 12 +++++---- 3 files changed, 29 insertions(+), 45 deletions(-) diff --git a/Config.h b/Config.h index acc6a81..7f4c505 100644 --- a/Config.h +++ b/Config.h @@ -78,6 +78,7 @@ bool pmu_ready = false; bool promisc = false; bool implicit = false; + volatile bool packet_ready = false; uint8_t implicit_l = 0; uint8_t op_mode = MODE_HOST; diff --git a/RNode_Firmware_CE.ino b/RNode_Firmware_CE.ino index 6492bb4..2ead632 100644 --- a/RNode_Firmware_CE.ino +++ b/RNode_Firmware_CE.ino @@ -320,6 +320,7 @@ inline void kiss_write_packet(int index) { } serial_write(FEND); read_len = 0; + packet_ready = false; } inline void getPacketData(RadioInterface* radio, uint16_t len) { @@ -358,7 +359,7 @@ void receive_callback(uint8_t index, int packet_size) { getPacketData(selected_radio, packet_size); seq = SEQ_UNSET; - ready = true; + packet_ready = true; } else if (isSplitPacket(header) && seq != sequence) { // This split packet does not carry the @@ -383,7 +384,7 @@ void receive_callback(uint8_t index, int packet_size) { } getPacketData(selected_radio, packet_size); - ready = true; + packet_ready = true; } } else { // In promiscuous mode, raw packets are @@ -391,26 +392,9 @@ void receive_callback(uint8_t index, int packet_size) { read_len = 0; getPacketData(selected_radio, packet_size); - ready = true; + packet_ready = true; } - if (ready) { - #if MCU_VARIANT == MCU_ESP32 - portENTER_CRITICAL(&update_lock); - #elif MCU_VARIANT == MCU_NRF52 - portENTER_CRITICAL(); - #endif - last_rssi = selected_radio->packetRssi(); - last_snr_raw = selected_radio->packetSnrRaw(); - #if MCU_VARIANT == MCU_ESP32 - portEXIT_CRITICAL(&update_lock); - #elif MCU_VARIANT == MCU_NRF52 - portEXIT_CRITICAL(); - #endif - kiss_indicate_stat_rssi(); - kiss_indicate_stat_snr(); - kiss_write_packet(index); - } last_rx = millis(); } @@ -1168,7 +1152,23 @@ void validate_status() { } void loop() { - packet_poll(); + if (packet_ready) { + #if MCU_VARIANT == MCU_ESP32 + portENTER_CRITICAL(&update_lock); + #elif MCU_VARIANT == MCU_NRF52 + portENTER_CRITICAL(); + #endif + last_rssi = selected_radio->packetRssi(); + last_snr_raw = selected_radio->packetSnrRaw(); + #if MCU_VARIANT == MCU_ESP32 + portEXIT_CRITICAL(&update_lock); + #elif MCU_VARIANT == MCU_NRF52 + portEXIT_CRITICAL(); + #endif + kiss_indicate_stat_rssi(); + kiss_indicate_stat_snr(); + kiss_write_packet(1); + } bool ready = false; for (int i = 0; i < INTERFACE_COUNT; i++) { @@ -1313,25 +1313,6 @@ void poll_buffers() { process_serial(); } -void packet_poll() { - // If we have received a packet on an interface which needs to be processed - while (!fifo_isempty(&packet_rdy_interfaces)) { - #if MCU_VARIANT == MCU_ESP32 - portENTER_CRITICAL(&update_lock); - #elif MCU_VARIANT == MCU_NRF52 - portENTER_CRITICAL(); - #endif - uint8_t packet_int = fifo_pop(&packet_rdy_interfaces); - #if MCU_VARIANT == MCU_ESP32 - portEXIT_CRITICAL(&update_lock); - #elif MCU_VARIANT == MCU_NRF52 - portEXIT_CRITICAL(); - #endif - selected_radio = interface_obj[packet_int]; - selected_radio->handleDio0Rise(); - } -} - volatile bool serial_polling = false; void serial_poll() { serial_polling = true; diff --git a/Radio.cpp b/Radio.cpp index c7dd012..e0df264 100644 --- a/Radio.cpp +++ b/Radio.cpp @@ -96,10 +96,11 @@ extern RadioInterface* interface_obj[]; // which one is high. We can then use the index of this pin in the 2D array to // signal the correct interface to the main loop void ISR_VECT onDio0Rise() { + BaseType_t int_status = taskENTER_CRITICAL_FROM_ISR(); for (int i = 0; i < INTERFACE_COUNT; i++) { if (digitalRead(interface_pins[i][5]) == HIGH) { if (interface_obj[i]->getPacketValidity()) { - fifo_push(&packet_rdy_interfaces, i); + interface_obj[i]->handleDio0Rise(); } if (interfaces[i] == SX128X) { // On the SX1280, there is a bug which can cause the busy line @@ -111,6 +112,7 @@ void ISR_VECT onDio0Rise() { } } } + taskEXIT_CRITICAL_FROM_ISR(int_status); } sx126x::sx126x(uint8_t index, SPIClass* spi, bool tcxo, bool dio2_as_rf_switch, int ss, int sclk, int mosi, int miso, int reset, int dio0, int busy, int rxen) : @@ -2622,11 +2624,11 @@ void sx128x::updateBitrate() { _csma_slot_ms = 10; float target_preamble_symbols; - if (_bitrate <= LORA_FAST_BITRATE_THRESHOLD) { + //if (_bitrate <= LORA_FAST_BITRATE_THRESHOLD) { target_preamble_symbols = (LORA_PREAMBLE_TARGET_MS/_lora_symbol_time_ms)-LORA_PREAMBLE_SYMBOLS_HW; - } else { - target_preamble_symbols = (LORA_PREAMBLE_FAST_TARGET_MS/_lora_symbol_time_ms)-LORA_PREAMBLE_SYMBOLS_HW; - } + //} else { + /*target_preamble_symbols = (LORA_PREAMBLE_FAST_TARGET_MS/_lora_symbol_time_ms)-LORA_PREAMBLE_SYMBOLS_HW; + }*/ if (target_preamble_symbols < LORA_PREAMBLE_SYMBOLS_MIN) { target_preamble_symbols = LORA_PREAMBLE_SYMBOLS_MIN; } else {