From 46a46b4069693dd19bb00a3049496864192eb28c Mon Sep 17 00:00:00 2001 From: "jacob.eva" Date: Thu, 9 Jan 2025 21:26:58 +0000 Subject: [PATCH] Revert radiolib work and fix SX127X issues #39 --- Config.h | 2 - Framing.h | 28 +-------- RNode_Firmware_CE.ino | 97 +++++++++++-------------------- Radio.cpp | 46 +++++++++------ Radio.hpp | 1 + Utilities.h | 130 ++++++++++++++++++------------------------ 6 files changed, 120 insertions(+), 184 deletions(-) diff --git a/Config.h b/Config.h index 41646ec..a4c2711 100644 --- a/Config.h +++ b/Config.h @@ -80,8 +80,6 @@ bool memory_low = false; uint8_t implicit_l = 0; - uint8_t packet_interface = 0xFF; - uint8_t op_mode = MODE_HOST; uint8_t model = 0x00; uint8_t hwrev = 0x00; diff --git a/Framing.h b/Framing.h index 980edbd..cde6e6f 100644 --- a/Framing.h +++ b/Framing.h @@ -77,32 +77,10 @@ #define CMD_RESET_BYTE 0xF8 #define CMD_INTERFACES 0x64 + + #define CMD_DATA 0x00 - #define CMD_INT0_DATA 0x00 - #define CMD_INT1_DATA 0x10 - #define CMD_INT2_DATA 0x20 - #define CMD_INT3_DATA 0x70 - #define CMD_INT4_DATA 0x75 - #define CMD_INT5_DATA 0x90 - #define CMD_INT6_DATA 0xA0 - #define CMD_INT7_DATA 0xB0 - #define CMD_INT8_DATA 0xC0 - #define CMD_INT9_DATA 0xD0 - #define CMD_INT10_DATA 0xE0 - #define CMD_INT11_DATA 0xF0 - - #define CMD_SEL_INT0 0x1E - #define CMD_SEL_INT1 0x1F - #define CMD_SEL_INT2 0x2F - #define CMD_SEL_INT3 0x74 - #define CMD_SEL_INT4 0x7F - #define CMD_SEL_INT5 0x9F - #define CMD_SEL_INT6 0xAF - #define CMD_SEL_INT7 0xBF - #define CMD_SEL_INT8 0xCF - #define CMD_SEL_INT9 0xDF - #define CMD_SEL_INT10 0xEF - #define CMD_SEL_INT11 0xFF + #define CMD_SEL_INT 0x1F #define DETECT_REQ 0x73 #define DETECT_RESP 0x46 diff --git a/RNode_Firmware_CE.ino b/RNode_Firmware_CE.ino index 961d293..cbefd06 100644 --- a/RNode_Firmware_CE.ino +++ b/RNode_Firmware_CE.ino @@ -349,12 +349,15 @@ void lora_receive(RadioInterface* radio) { } inline void kiss_write_packet(int index) { - // We need to convert the interface index to the command byte representation - uint8_t cmd_byte = getInterfaceCommandByte(index); + + // Print index of interface the packet came from + serial_write(FEND); + serial_write(CMD_SEL_INT); + serial_write(index); + serial_write(FEND); serial_write(FEND); - // Add index of interface the packet came from - serial_write(cmd_byte); + serial_write(CMD_DATA); for (uint16_t i = 0; i < read_len[index]; i++) { #if MCU_VARIANT == MCU_NRF52 @@ -689,39 +692,27 @@ void transmit(RadioInterface* radio, uint16_t size) { void serialCallback(uint8_t sbyte) { if (IN_FRAME && sbyte == FEND && - (command == CMD_INT0_DATA - || command == CMD_INT1_DATA - || command == CMD_INT2_DATA - || command == CMD_INT3_DATA - || command == CMD_INT4_DATA - || command == CMD_INT5_DATA - || command == CMD_INT6_DATA - || command == CMD_INT7_DATA - || command == CMD_INT8_DATA - || command == CMD_INT9_DATA - || command == CMD_INT10_DATA - || command == CMD_INT11_DATA)) { + command == CMD_DATA) { IN_FRAME = false; - if (getInterfaceIndex(command) < INTERFACE_COUNT) { - uint8_t index = getInterfaceIndex(command); - if (!fifo16_isfull(&packet_starts[index]) && (queued_bytes[index] < (getQueueSize(index)))) { - uint16_t s = current_packet_start[index]; - int32_t e = queue_cursor[index]-1; if (e == -1) e = (getQueueSize(index))-1; + if (interface < INTERFACE_COUNT) { + if (!fifo16_isfull(&packet_starts[interface]) && (queued_bytes[interface] < (getQueueSize(interface)))) { + uint16_t s = current_packet_start[interface]; + int32_t e = queue_cursor[interface]-1; if (e == -1) e = (getQueueSize(interface))-1; uint16_t l; if (s != e) { - l = (s < e) ? e - s + 1: (getQueueSize(index)) - s + e + 1; + l = (s < e) ? e - s + 1: (getQueueSize(interface)) - s + e + 1; } else { l = 1; } if (l >= MIN_L) { - queue_height[index]++; + queue_height[interface]++; - fifo16_push(&packet_starts[index], s); - fifo16_push(&packet_lengths[index], l); - current_packet_start[index] = queue_cursor[index]; + fifo16_push(&packet_starts[interface], s); + fifo16_push(&packet_lengths[interface], l); + current_packet_start[interface] = queue_cursor[interface]; } } @@ -735,33 +726,10 @@ void serialCallback(uint8_t sbyte) { // Have a look at the command byte first if (frame_len == 0 && command == CMD_UNKNOWN) { command = sbyte; - if (command == CMD_SEL_INT0 - || command == CMD_SEL_INT1 - || command == CMD_SEL_INT2 - || command == CMD_SEL_INT3 - || command == CMD_SEL_INT4 - || command == CMD_SEL_INT5 - || command == CMD_SEL_INT6 - || command == CMD_SEL_INT7 - || command == CMD_SEL_INT8 - || command == CMD_SEL_INT9 - || command == CMD_SEL_INT10 - || command == CMD_SEL_INT11) { - interface = getInterfaceIndex(command); - } - } else if (command == CMD_INT0_DATA - || command == CMD_INT1_DATA - || command == CMD_INT2_DATA - || command == CMD_INT3_DATA - || command == CMD_INT4_DATA - || command == CMD_INT5_DATA - || command == CMD_INT6_DATA - || command == CMD_INT7_DATA - || command == CMD_INT8_DATA - || command == CMD_INT9_DATA - || command == CMD_INT10_DATA - || command == CMD_INT11_DATA) { + } else if (command == CMD_SEL_INT) { + interface = sbyte; + } else if (command == CMD_DATA) { if (bt_state != BT_STATE_CONNECTED) cable_state = CABLE_STATE_CONNECTED; if (sbyte == FESC) { ESCAPE = true; @@ -772,12 +740,11 @@ void serialCallback(uint8_t sbyte) { ESCAPE = false; } - if (getInterfaceIndex(command) < INTERFACE_COUNT) { - uint8_t index = getInterfaceIndex(command); - if (queue_height[index] < CONFIG_QUEUE_MAX_LENGTH && queued_bytes[index] < (getQueueSize(index))) { - queued_bytes[index]++; - packet_queue[index][queue_cursor[index]++] = sbyte; - if (queue_cursor[index] == (getQueueSize(index))) queue_cursor[index] = 0; + if (interface < INTERFACE_COUNT) { + if (queue_height[interface] < CONFIG_QUEUE_MAX_LENGTH && queued_bytes[interface] < (getQueueSize(interface))) { + queued_bytes[interface]++; + packet_queue[interface][queue_cursor[interface]++] = sbyte; + if (queue_cursor[interface] == (getQueueSize(interface))) queue_cursor[interface] = 0; } } } @@ -962,7 +929,7 @@ void serialCallback(uint8_t sbyte) { } else if (command == CMD_STAT_TX) { kiss_indicate_stat_tx(); } else if (command == CMD_STAT_RSSI) { - kiss_indicate_stat_rssi(); + kiss_indicate_stat_rssi(interface_obj[interface]); } else if (command == CMD_RADIO_LOCK) { selected_radio = interface_obj[interface]; update_radio_lock(selected_radio); @@ -1320,7 +1287,7 @@ 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) { - packet_interface = modem_packet->interface; + uint8_t packet_interface = modem_packet->interface; read_len[packet_interface] = modem_packet->len; last_rssi = modem_packet->rssi; last_snr_raw = modem_packet->snr_raw; @@ -1328,15 +1295,15 @@ void loop() { free(modem_packet); modem_packet = NULL; - kiss_indicate_stat_rssi(); - kiss_indicate_stat_snr(); + kiss_indicate_stat_rssi(interface_obj[packet_interface]); + kiss_indicate_stat_snr(interface_obj[packet_interface]); kiss_write_packet(packet_interface); } #elif MCU_VARIANT == MCU_NRF52 modem_packet_t *modem_packet = NULL; if(modem_packet_queue && xQueueReceive(modem_packet_queue, &modem_packet, 0) == pdTRUE && modem_packet) { - packet_interface = modem_packet->interface; + uint8_t packet_interface = modem_packet->interface; read_len[packet_interface] = modem_packet->len; last_rssi = modem_packet->rssi; last_snr_raw = modem_packet->snr_raw; @@ -1344,8 +1311,8 @@ void loop() { free(modem_packet); modem_packet = NULL; - kiss_indicate_stat_rssi(); - kiss_indicate_stat_snr(); + kiss_indicate_stat_rssi(packet_interface); + kiss_indicate_stat_snr(packet_interface); kiss_write_packet(packet_interface); } #endif diff --git a/Radio.cpp b/Radio.cpp index 8ffe276..c84169e 100644 --- a/Radio.cpp +++ b/Radio.cpp @@ -1094,7 +1094,7 @@ sx127x::sx127x(uint8_t index, SPIClass* spi, int ss, int sclk, int mosi, int mis _spiSettings(8E6, MSBFIRST, SPI_MODE0), _spiModem(spi), _ss(ss), _sclk(sclk), _mosi(mosi), _miso(miso), _reset(reset), _dio0(dio0), - _busy(busy), _frequency(0), _packetIndex(0), _preinit_done(false) + _busy(busy), _frequency(0), _packetIndex(0), _preinit_done(false), _bw(0) { setTimeout(0); // TODO, figure out why this has to be done. Using the index to reference the @@ -1384,10 +1384,12 @@ void sx127x::setTxPower(int level, int outputPin) { writeRegister(REG_PA_DAC_7X, 0x84); writeRegister(REG_PA_CONFIG_7X, PA_BOOST_7X | (level - 2)); + _txp = level; } } -int8_t sx127x::getTxPower() { byte txp = readRegister(REG_PA_CONFIG_7X); return ((int8_t)txp) - 126; } // temporary fix for SX127X power weirdness +//int8_t sx127x::getTxPower() { return readRegister(REG_PA_CONFIG_7X) - 126; } +int8_t sx127x::getTxPower() { return _txp; } void sx127x::setFrequency(uint32_t frequency) { _frequency = frequency; @@ -1409,7 +1411,12 @@ uint32_t sx127x::getFrequency() { uint64_t frm = (uint64_t)frf*32000000; uint32_t frequency = (frm >> 19); - return frequency; + // TODO, figure out why frequency from module is slightly wrong + if (_frequency != frequency) { + return _frequency; + } else { + return frequency; + } } void sx127x::setSpreadingFactor(int sf) { @@ -1436,20 +1443,26 @@ uint8_t sx127x::getSpreadingFactor() } uint32_t sx127x::getSignalBandwidth() { - byte bw = (readRegister(REG_MODEM_CONFIG_1_7X) >> 4); - switch (bw) { - case 0: return 7.8E3; - case 1: return 10.4E3; - case 2: return 15.6E3; - case 3: return 20.8E3; - case 4: return 31.25E3; - case 5: return 41.7E3; - case 6: return 62.5E3; - case 7: return 125E3; - case 8: return 250E3; - case 9: return 500E3; } + uint8_t bw; + if (_radio_online) { + bw = (readRegister(REG_MODEM_CONFIG_1_7X) >> 4); + } else { + bw = _bw; + } + switch (bw) { + case 0: return 7.8E3; + case 1: return 10.4E3; + case 2: return 15.6E3; + case 3: return 20.8E3; + case 4: return 31.25E3; + case 5: return 41.7E3; + case 6: return 62.5E3; + case 7: return 125E3; + case 8: return 250E3; + case 9: return 500E3; + } - return 0; + return 0; } void sx127x::setSignalBandwidth(uint32_t sbw) { @@ -1477,6 +1490,7 @@ void sx127x::setSignalBandwidth(uint32_t sbw) { } writeRegister(REG_MODEM_CONFIG_1_7X, (readRegister(REG_MODEM_CONFIG_1_7X) & 0x0f) | (bw << 4)); + _bw = bw; handleLowDataRate(); optimizeModemSensitivity(); } diff --git a/Radio.hpp b/Radio.hpp index 905dc9a..3ce4c45 100644 --- a/Radio.hpp +++ b/Radio.hpp @@ -546,6 +546,7 @@ private: bool _preinit_done; uint8_t _sf; uint8_t _cr; + uint8_t _bw; }; class sx128x : public RadioInterface { diff --git a/Utilities.h b/Utilities.h index 4c45d8d..c76e212 100644 --- a/Utilities.h +++ b/Utilities.h @@ -653,6 +653,10 @@ void kiss_indicate_error(uint8_t error_code) { void kiss_indicate_radiostate(RadioInterface* radio) { serial_write(FEND); + serial_write(CMD_SEL_INT); + serial_write(radio->getIndex()); + serial_write(FEND); + serial_write(FEND); serial_write(CMD_RADIO_STATE); serial_write(radio->getRadioOnline()); serial_write(FEND); @@ -680,7 +684,11 @@ void kiss_indicate_stat_tx() { //serial_write(FEND); } -void kiss_indicate_stat_rssi() { +void kiss_indicate_stat_rssi(RadioInterface* radio) { + serial_write(FEND); + serial_write(CMD_SEL_INT); + serial_write(radio->getIndex()); + serial_write(FEND); uint8_t packet_rssi_val = (uint8_t)(last_rssi+rssi_offset); serial_write(FEND); serial_write(CMD_STAT_RSSI); @@ -688,7 +696,11 @@ void kiss_indicate_stat_rssi() { serial_write(FEND); } -void kiss_indicate_stat_snr() { +void kiss_indicate_stat_snr(RadioInterface* radio) { + serial_write(FEND); + serial_write(CMD_SEL_INT); + serial_write(radio->getIndex()); + serial_write(FEND); serial_write(FEND); serial_write(CMD_STAT_SNR); escaped_serial_write(last_snr_raw); @@ -697,6 +709,10 @@ void kiss_indicate_stat_snr() { void kiss_indicate_radio_lock(RadioInterface* radio) { serial_write(FEND); + serial_write(CMD_SEL_INT); + serial_write(radio->getIndex()); + serial_write(FEND); + serial_write(FEND); serial_write(CMD_RADIO_LOCK); serial_write(radio->getRadioLock()); serial_write(FEND); @@ -704,6 +720,10 @@ void kiss_indicate_radio_lock(RadioInterface* radio) { void kiss_indicate_spreadingfactor(RadioInterface* radio) { serial_write(FEND); + serial_write(CMD_SEL_INT); + serial_write(radio->getIndex()); + serial_write(FEND); + serial_write(FEND); serial_write(CMD_SF); serial_write(radio->getSpreadingFactor()); serial_write(FEND); @@ -711,6 +731,10 @@ void kiss_indicate_spreadingfactor(RadioInterface* radio) { void kiss_indicate_codingrate(RadioInterface* radio) { serial_write(FEND); + serial_write(CMD_SEL_INT); + serial_write(radio->getIndex()); + serial_write(FEND); + serial_write(FEND); serial_write(CMD_CR); serial_write(radio->getCodingRate4()); serial_write(FEND); @@ -726,6 +750,10 @@ void kiss_indicate_implicit_length() { void kiss_indicate_txpower(RadioInterface* radio) { int8_t txp = radio->getTxPower(); serial_write(FEND); + serial_write(CMD_SEL_INT); + serial_write(radio->getIndex()); + serial_write(FEND); + serial_write(FEND); serial_write(CMD_TXPOWER); serial_write(txp); serial_write(FEND); @@ -734,6 +762,10 @@ void kiss_indicate_txpower(RadioInterface* radio) { void kiss_indicate_bandwidth(RadioInterface* radio) { uint32_t bw = radio->getSignalBandwidth(); serial_write(FEND); + serial_write(CMD_SEL_INT); + serial_write(radio->getIndex()); + serial_write(FEND); + serial_write(FEND); serial_write(CMD_BANDWIDTH); escaped_serial_write(bw>>24); escaped_serial_write(bw>>16); @@ -745,6 +777,10 @@ void kiss_indicate_bandwidth(RadioInterface* radio) { void kiss_indicate_frequency(RadioInterface* radio) { uint32_t freq = radio->getFrequency(); serial_write(FEND); + serial_write(CMD_SEL_INT); + serial_write(radio->getIndex()); + serial_write(FEND); + serial_write(FEND); serial_write(CMD_FREQUENCY); escaped_serial_write(freq>>24); escaped_serial_write(freq>>16); @@ -765,6 +801,10 @@ void kiss_indicate_interface(int index) { void kiss_indicate_st_alock(RadioInterface* radio) { uint16_t at = (uint16_t)(radio->getSTALock()*100*100); serial_write(FEND); + serial_write(CMD_SEL_INT); + serial_write(radio->getIndex()); + serial_write(FEND); + serial_write(FEND); serial_write(CMD_ST_ALOCK); escaped_serial_write(at>>8); escaped_serial_write(at); @@ -774,6 +814,10 @@ void kiss_indicate_st_alock(RadioInterface* radio) { void kiss_indicate_lt_alock(RadioInterface* radio) { uint16_t at = (uint16_t)(radio->getLTALock()*100*100); serial_write(FEND); + serial_write(CMD_SEL_INT); + serial_write(radio->getIndex()); + serial_write(FEND); + serial_write(FEND); serial_write(CMD_LT_ALOCK); escaped_serial_write(at>>8); escaped_serial_write(at); @@ -785,6 +829,10 @@ void kiss_indicate_channel_stats(RadioInterface* radio) { uint16_t atl = (uint16_t)(radio->getLongtermAirtime()*100*100); uint16_t cls = (uint16_t)(radio->getTotalChannelUtil()*100*100); uint16_t cll = (uint16_t)(radio->getLongtermChannelUtil()*100*100); + serial_write(FEND); + serial_write(CMD_SEL_INT); + serial_write(radio->getIndex()); + serial_write(FEND); serial_write(FEND); serial_write(CMD_STAT_CHTM); escaped_serial_write(ats>>8); @@ -804,6 +852,10 @@ void kiss_indicate_phy_stats(RadioInterface* radio) { uint16_t prs = (uint16_t)(radio->getPreambleLength()+4); uint16_t prt = (uint16_t)((radio->getPreambleLength()+4)*radio->getSymbolTime()); uint16_t cst = (uint16_t)(radio->getCSMASlotMS()); + serial_write(FEND); + serial_write(CMD_SEL_INT); + serial_write(radio->getIndex()); + serial_write(FEND); serial_write(FEND); serial_write(CMD_STAT_PHYPRM); escaped_serial_write(lst>>8); @@ -1085,80 +1137,6 @@ uint8_t getRandom(RadioInterface* radio) { } } -uint8_t getInterfaceIndex(uint8_t byte) { - switch (byte) { - case CMD_INT0_DATA: - case CMD_SEL_INT0: - return 0; - case CMD_INT1_DATA: - case CMD_SEL_INT1: - return 1; - case CMD_INT2_DATA: - case CMD_SEL_INT2: - return 2; - case CMD_INT3_DATA: - case CMD_SEL_INT3: - return 3; - case CMD_INT4_DATA: - case CMD_SEL_INT4: - return 4; - case CMD_INT5_DATA: - case CMD_SEL_INT5: - return 5; - case CMD_INT6_DATA: - case CMD_SEL_INT6: - return 6; - case CMD_INT7_DATA: - case CMD_SEL_INT7: - return 7; - case CMD_INT8_DATA: - case CMD_SEL_INT8: - return 8; - case CMD_INT9_DATA: - case CMD_SEL_INT9: - return 9; - case CMD_INT10_DATA: - case CMD_SEL_INT10: - return 10; - case CMD_INT11_DATA: - case CMD_SEL_INT11: - return 11; - default: - return 0; - } -} - -uint8_t getInterfaceCommandByte(uint8_t index) { - switch (index) { - case 0: - return CMD_INT0_DATA; - case 1: - return CMD_INT1_DATA; - case 2: - return CMD_INT2_DATA; - case 3: - return CMD_INT3_DATA; - case 4: - return CMD_INT4_DATA; - case 5: - return CMD_INT5_DATA; - case 6: - return CMD_INT6_DATA; - case 7: - return CMD_INT7_DATA; - case 8: - return CMD_INT8_DATA; - case 9: - return CMD_INT9_DATA; - case 10: - return CMD_INT10_DATA; - case 11: - return CMD_INT11_DATA; - default: - return 0; - } -} - uint16_t getQueueSize(uint8_t index) { switch (index) { case 0: