Improved ESP32 compatibility

This commit is contained in:
Mark Qvist 2022-01-14 21:12:33 +01:00
parent 7cc777ff3f
commit 1afc9fd2a9
2 changed files with 111 additions and 140 deletions

View File

@ -83,7 +83,7 @@
#define BOARD_MODEL BOARD_TBEAM #define BOARD_MODEL BOARD_TBEAM
#define CONFIG_UART_BUFFER_SIZE 64 #define CONFIG_UART_BUFFER_SIZE 6144
#define CONFIG_QUEUE_SIZE 6144 #define CONFIG_QUEUE_SIZE 6144
#define CONFIG_QUEUE_MAX_LENGTH 250 #define CONFIG_QUEUE_MAX_LENGTH 250

View File

@ -22,18 +22,13 @@ volatile bool serial_buffering = false;
char sbuf[128]; char sbuf[128];
#if MCU_VARIANT == MCU_ESP32 #if MCU_VARIANT == MCU_ESP32
#include "soc/rtc_wdt.h"
#define ISR_VECT IRAM_ATTR
bool packet_ready = false; bool packet_ready = false;
#else
#define ISR_VECT
#endif #endif
void setup() { void setup() {
#if MCU_VARIANT == MCU_ESP32 #if MCU_VARIANT == MCU_ESP32
delay(500); delay(500);
EEPROM.begin(EEPROM_SIZE); EEPROM.begin(EEPROM_SIZE);
// TODO: Check this
Serial.setRxBufferSize(CONFIG_UART_BUFFER_SIZE); Serial.setRxBufferSize(CONFIG_UART_BUFFER_SIZE);
#endif #endif
@ -70,15 +65,8 @@ void setup() {
LoRa.setPins(pin_cs, pin_reset, pin_dio); LoRa.setPins(pin_cs, pin_reset, pin_dio);
#if MCU_VARIANT == MCU_ESP32 #if MCU_VARIANT == MCU_ESP32
// ESP32-specific initialisation Wire.begin(I2C_SDA, I2C_SCL);
Serial1.begin(GPS_BAUD_RATE, SERIAL_8N1, PIN_GPS_RX, PIN_GPS_TX); initPMU();
// rtc_wdt_protect_off();
// rtc_wdt_set_stage(RTC_WDT_STAGE0, RTC_WDT_STAGE_ACTION_RESET_SYSTEM);
// rtc_wdt_set_time(RTC_WDT_STAGE0, 25);
// rtc_wdt_protect_on();
// rtc_wdt_enable();
kiss_indicate_reset(); kiss_indicate_reset();
#endif #endif
@ -97,7 +85,7 @@ void lora_receive() {
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 (int i = 0; i < read_len; i++) { for (uint16_t i = 0; i < read_len; i++) {
uint8_t byte = pbuf[i]; uint8_t byte = pbuf[i];
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; }
@ -110,123 +98,89 @@ inline void kiss_write_packet() {
#endif #endif
} }
inline void getPacketData(int len) { inline void getPacketData(uint16_t len) {
while (len-- && read_len < MTU) { while (len-- && read_len < MTU) {
pbuf[read_len++] = LoRa.read(); pbuf[read_len++] = LoRa.read();
} }
} }
#if MCU_VARIANT == MCU_ESP32
portMUX_TYPE isr_lock = portMUX_INITIALIZER_UNLOCKED;
#endif
bool handling_packet = false;
void ISR_VECT receive_callback(int packet_size) { void ISR_VECT receive_callback(int packet_size) {
#if MCU_VARIANT == MCU_ESP32 if (!promisc) {
portENTER_CRITICAL_ISR(&isr_lock); // The standard operating mode allows large
#endif // packets with a payload up to 500 bytes,
// by combining two raw LoRa packets.
// We read the 1-byte header and extract
// packet sequence number and split flags
uint8_t header = LoRa.read(); packet_size--;
uint8_t sequence = packetSequence(header);
bool ready = false;
#if MCU_VARIANT == MCU_ESP32 if (isSplitPacket(header) && seq == SEQ_UNSET) {
if (!handling_packet) { // This is the first part of a split
handling_packet = true; // packet, so we set the seq variable
#endif // and add the data to the buffer
read_len = 0;
seq = sequence;
if (!promisc) {
// The standard operating mode allows large
// packets with a payload up to 500 bytes,
// by combining two raw LoRa packets.
// We read the 1-byte header and extract
// packet sequence number and split flags
uint8_t header = LoRa.read(); packet_size--;
uint8_t sequence = packetSequence(header);
bool ready = false;
if (isSplitPacket(header) && seq == SEQ_UNSET) {
// This is the first part of a split
// packet, so we set the seq variable
// and add the data to the buffer
read_len = 0;
seq = sequence;
#if MCU_VARIANT != MCU_ESP32
last_rssi = LoRa.packetRssi();
last_snr_raw = LoRa.packetSnrRaw();
#endif
getPacketData(packet_size);
} else if (isSplitPacket(header) && seq == sequence) {
// 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
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;
} else if (isSplitPacket(header) && seq != sequence) {
// This split packet does not carry the
// same sequence id, so we must assume
// that we are seeing the first part of
// a new split packet.
read_len = 0;
seq = sequence;
#if MCU_VARIANT != MCU_ESP32
last_rssi = LoRa.packetRssi();
last_snr_raw = LoRa.packetSnrRaw();
#endif
getPacketData(packet_size);
} else if (!isSplitPacket(header)) {
// This is not a split packet, so we
// just read it and set the ready
// flag to true.
if (seq != SEQ_UNSET) {
// If we already had part of a split
// packet in the buffer, we clear it.
read_len = 0;
seq = SEQ_UNSET;
}
#if MCU_VARIANT != MCU_ESP32
last_rssi = LoRa.packetRssi();
last_snr_raw = LoRa.packetSnrRaw();
#endif
getPacketData(packet_size);
ready = true;
}
if (ready) {
#if MCU_VARIANT != MCU_ESP32
// We first signal the RSSI of the
// recieved packet to the host.
kiss_indicate_stat_rssi();
kiss_indicate_stat_snr();
// And then write the entire packet
kiss_write_packet();
#else
packet_ready = true;
#endif
}
} else {
#if MCU_VARIANT != MCU_ESP32 #if MCU_VARIANT != MCU_ESP32
// In promiscuous mode, raw packets are
// output directly to the host
read_len = 0;
last_rssi = LoRa.packetRssi(); last_rssi = LoRa.packetRssi();
last_snr_raw = LoRa.packetSnrRaw(); last_snr_raw = LoRa.packetSnrRaw();
getPacketData(packet_size); #endif
getPacketData(packet_size);
} else if (isSplitPacket(header) && seq == sequence) {
// 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
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;
} else if (isSplitPacket(header) && seq != sequence) {
// This split packet does not carry the
// same sequence id, so we must assume
// that we are seeing the first part of
// a new split packet.
read_len = 0;
seq = sequence;
#if MCU_VARIANT != MCU_ESP32
last_rssi = LoRa.packetRssi();
last_snr_raw = LoRa.packetSnrRaw();
#endif
getPacketData(packet_size);
} else if (!isSplitPacket(header)) {
// This is not a split packet, so we
// just read it and set the ready
// flag to true.
if (seq != SEQ_UNSET) {
// If we already had part of a split
// packet in the buffer, we clear it.
read_len = 0;
seq = SEQ_UNSET;
}
#if MCU_VARIANT != MCU_ESP32
last_rssi = LoRa.packetRssi();
last_snr_raw = LoRa.packetSnrRaw();
#endif
getPacketData(packet_size);
ready = true;
}
if (ready) {
#if MCU_VARIANT != MCU_ESP32
// We first signal the RSSI of the // We first signal the RSSI of the
// recieved packet to the host. // recieved packet to the host.
kiss_indicate_stat_rssi(); kiss_indicate_stat_rssi();
@ -234,21 +188,33 @@ void ISR_VECT receive_callback(int packet_size) {
// And then write the entire packet // And then write the entire packet
kiss_write_packet(); kiss_write_packet();
#else #else
read_len = 0;
getPacketData(packet_size);
packet_ready = true; packet_ready = true;
#endif #endif
#if MCU_VARIANT == MCU_ESP32
} }
handling_packet = false; } else {
#if MCU_VARIANT != MCU_ESP32
// In promiscuous mode, raw packets are
// output directly to the host
read_len = 0;
last_rssi = LoRa.packetRssi();
last_snr_raw = LoRa.packetSnrRaw();
getPacketData(packet_size);
// We first signal the RSSI of the
// recieved packet to the host.
kiss_indicate_stat_rssi();
kiss_indicate_stat_snr();
// And then write the entire packet
kiss_write_packet();
#else
read_len = 0;
getPacketData(packet_size);
packet_ready = true;
#endif #endif
} }
#if MCU_VARIANT == MCU_ESP32
portEXIT_CRITICAL_ISR(&isr_lock);
#endif
} }
@ -628,9 +594,22 @@ 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
portENTER_CRITICAL(&update_lock);
#endif
uint8_t status = LoRa.modemStatus(); uint8_t status = LoRa.modemStatus();
last_status_update = millis(); last_status_update = millis();
#if MCU_VARIANT == MCU_ESP32
portEXIT_CRITICAL(&update_lock);
#endif
if (status & SIG_DETECT == SIG_DETECT) { stat_signal_detected = true; } else { stat_signal_detected = false; } if (status & SIG_DETECT == SIG_DETECT) { stat_signal_detected = true; } else { stat_signal_detected = false; }
if (status & SIG_SYNCED == SIG_SYNCED) { stat_signal_synced = true; } else { stat_signal_synced = false; } if (status & SIG_SYNCED == SIG_SYNCED) { stat_signal_synced = true; } else { stat_signal_synced = false; }
if (status & RX_ONGOING == RX_ONGOING) { stat_rx_ongoing = true; } else { stat_rx_ongoing = false; } if (status & RX_ONGOING == RX_ONGOING) { stat_rx_ongoing = true; } else { stat_rx_ongoing = false; }
@ -801,14 +780,6 @@ void buffer_serial() {
#endif #endif
} }
#if MCU_VARIANT == MCU_ESP32
// Discard GPS data for now
c = 0;
while (c < MAX_CYCLES && Serial1.available()) {
Serial1.read();
}
#endif
serial_buffering = false; serial_buffering = false;
} }
} }