Work on ESP32 compatibility

This commit is contained in:
Mark Qvist 2022-01-11 02:54:32 +01:00
parent 2e8525c601
commit a0af475c31
3 changed files with 145 additions and 99 deletions

View File

@ -79,6 +79,10 @@
#define EEPROM_SIZE 1024 #define EEPROM_SIZE 1024
#define EEPROM_OFFSET EEPROM_SIZE-EEPROM_RESERVED #define EEPROM_OFFSET EEPROM_SIZE-EEPROM_RESERVED
#define GPS_BAUD_RATE 9600
#define PIN_GPS_TX 12
#define PIN_GPS_RX 34
#endif #endif
#define eeprom_addr(a) (a+EEPROM_OFFSET) #define eeprom_addr(a) (a+EEPROM_OFFSET)

View File

@ -34,7 +34,7 @@ void setup() {
delay(500); delay(500);
EEPROM.begin(EEPROM_SIZE); EEPROM.begin(EEPROM_SIZE);
// TODO: Check this // TODO: Check this
//Serial.setRxBufferSize(CONFIG_UART_BUFFER_SIZE); Serial.setRxBufferSize(CONFIG_UART_BUFFER_SIZE);
#endif #endif
// Seed the PRNG // Seed the PRNG
@ -71,13 +71,15 @@ void setup() {
#if MCU_VARIANT == MCU_ESP32 #if MCU_VARIANT == MCU_ESP32
// ESP32-specific initialisation // ESP32-specific initialisation
// The WDT is disabled for now. This Serial1.begin(GPS_BAUD_RATE, SERIAL_8N1, PIN_GPS_RX, PIN_GPS_TX);
// should be re-enabled as soon as any
// Core0-related features are used // rtc_wdt_protect_off();
rtc_wdt_protect_off();
rtc_wdt_disable();
// rtc_wdt_set_stage(RTC_WDT_STAGE0, RTC_WDT_STAGE_ACTION_RESET_SYSTEM); // rtc_wdt_set_stage(RTC_WDT_STAGE0, RTC_WDT_STAGE_ACTION_RESET_SYSTEM);
// rtc_wdt_set_time(RTC_WDT_STAGE0, 25); // rtc_wdt_set_time(RTC_WDT_STAGE0, 25);
// rtc_wdt_protect_on();
// rtc_wdt_enable();
kiss_indicate_reset();
#endif #endif
// Validate board health, EEPROM and config // Validate board health, EEPROM and config
@ -108,7 +110,27 @@ inline void kiss_write_packet() {
#endif #endif
} }
inline void getPacketData(int len) {
while (len-- && read_len < MTU) {
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
portENTER_CRITICAL_ISR(&isr_lock);
#endif
#if MCU_VARIANT == MCU_ESP32
if (!handling_packet) {
handling_packet = true;
#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,
@ -132,6 +154,7 @@ void ISR_VECT receive_callback(int packet_size) {
#endif #endif
getPacketData(packet_size); getPacketData(packet_size);
} else if (isSplitPacket(header) && seq == sequence) { } else if (isSplitPacket(header) && seq == sequence) {
// 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
@ -145,6 +168,7 @@ void ISR_VECT receive_callback(int packet_size) {
getPacketData(packet_size); getPacketData(packet_size);
seq = SEQ_UNSET; seq = SEQ_UNSET;
ready = true; ready = true;
} else if (isSplitPacket(header) && seq != sequence) { } else if (isSplitPacket(header) && seq != sequence) {
// This split packet does not carry the // This split packet does not carry the
// same sequence id, so we must assume // same sequence id, so we must assume
@ -159,6 +183,7 @@ void ISR_VECT receive_callback(int packet_size) {
#endif #endif
getPacketData(packet_size); getPacketData(packet_size);
} else if (!isSplitPacket(header)) { } else if (!isSplitPacket(header)) {
// This is not a split packet, so we // This is not a split packet, so we
// just read it and set the ready // just read it and set the ready
@ -211,11 +236,19 @@ void ISR_VECT receive_callback(int packet_size) {
kiss_write_packet(); kiss_write_packet();
#else #else
// Promiscous mode is not supported on ESP32 for now
getPacketData(packet_size);
read_len = 0; read_len = 0;
getPacketData(packet_size);
packet_ready = true;
#endif
#if MCU_VARIANT == MCU_ESP32
}
handling_packet = false;
#endif #endif
} }
#if MCU_VARIANT == MCU_ESP32
portEXIT_CRITICAL_ISR(&isr_lock);
#endif
} }
@ -747,7 +780,7 @@ void serial_poll() {
#if MCU_VARIANT != MCU_ESP32 #if MCU_VARIANT != MCU_ESP32
#define MAX_CYCLES 20 #define MAX_CYCLES 20
#else #else
#define MAX_CYCLES 1 #define MAX_CYCLES 10
#endif #endif
void buffer_serial() { void buffer_serial() {
if (!serial_buffering) { if (!serial_buffering) {
@ -768,6 +801,14 @@ void buffer_serial() {
#endif #endif
} }
#if MCU_VARIANT == MCU_ESP32
// Discard GPS data for now
c = 0;
while (c < MAX_CYCLES && Serial1.available()) {
uint8_t void_c = Serial1.read();
}
#endif
serial_buffering = false; serial_buffering = false;
} }
} }

View File

@ -31,8 +31,8 @@ uint8_t boot_vector = 0x00;
void led_tx_on() { digitalWrite(pin_led_tx, HIGH); } void led_tx_on() { digitalWrite(pin_led_tx, HIGH); }
void led_tx_off() { digitalWrite(pin_led_tx, LOW); } void led_tx_off() { digitalWrite(pin_led_tx, LOW); }
#elif MCU_VARIANT == MCU_ESP32 #elif MCU_VARIANT == MCU_ESP32
void led_rx_on() { digitalWrite(pin_led_rx, LOW); } void led_rx_on() { digitalWrite(pin_led_rx, HIGH); }
void led_rx_off() { digitalWrite(pin_led_rx, HIGH); } void led_rx_off() { digitalWrite(pin_led_rx, LOW); }
void led_tx_on() { digitalWrite(pin_led_tx, LOW); } void led_tx_on() { digitalWrite(pin_led_tx, LOW); }
void led_tx_off() { digitalWrite(pin_led_tx, HIGH); } void led_tx_off() { digitalWrite(pin_led_tx, HIGH); }
#endif #endif
@ -216,6 +216,13 @@ void escapedSerialWrite(uint8_t byte) {
Serial.write(byte); Serial.write(byte);
} }
void kiss_indicate_reset() {
Serial.write(FEND);
Serial.write(CMD_RESET);
Serial.write(CMD_RESET_BYTE);
Serial.write(FEND);
}
void kiss_indicate_error(uint8_t error_code) { void kiss_indicate_error(uint8_t error_code) {
Serial.write(FEND); Serial.write(FEND);
Serial.write(CMD_ERROR); Serial.write(CMD_ERROR);
@ -389,12 +396,6 @@ inline uint8_t packetSequence(uint8_t header) {
return header >> 4; return header >> 4;
} }
inline void getPacketData(int len) {
while (len--) {
pbuf[read_len++] = LoRa.read();
}
}
void setSpreadingFactor() { void setSpreadingFactor() {
if (radio_online) LoRa.setSpreadingFactor(lora_sf); if (radio_online) LoRa.setSpreadingFactor(lora_sf);
} }