From b0f49c0cce74844ab68c6cae364db6be6718c3ec Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Sat, 29 Oct 2022 00:53:39 +0200 Subject: [PATCH] Added display, power management and other foundations for bluetooth connectivity. --- Config.h | 43 +++++++++++++++- Framing.h | 4 ++ RNode_Firmware.ino | 65 ++++++++++++++++++++++-- Utilities.h | 122 ++++++++++++++++----------------------------- 4 files changed, 148 insertions(+), 86 deletions(-) diff --git a/Config.h b/Config.h index f9477a6..d3a6952 100644 --- a/Config.h +++ b/Config.h @@ -4,7 +4,7 @@ #define CONFIG_H #define MAJ_VERS 0x01 - #define MIN_VERS 0x1E + #define MIN_VERS 0x28 #define PLATFORM_AVR 0x90 #define PLATFORM_ESP32 0x80 @@ -36,6 +36,17 @@ #elif defined(ESP32) #define PLATFORM PLATFORM_ESP32 #define MCU_VARIANT MCU_ESP32 + + #define CABLE_STATE_DISCONNECTED 0x00 + #define CABLE_STATE_CONNECTED 0x01 + uint8_t cable_state = CABLE_STATE_DISCONNECTED; + + #define BT_STATE_NA 0xff + #define BT_STATE_OFF 0x00 + #define BT_STATE_ON 0x01 + #define BT_STATE_PAIRING 0x02 + #define BT_STATE_CONNECTED 0x03 + uint8_t bt_state = BT_STATE_NA; #else #error "The firmware cannot be compiled for the selected MCU variant" #endif @@ -49,6 +60,10 @@ // MCU dependent configuration parameters + #define HAS_DISPLAY false + #define HAS_BLUETOOTH false + #define HAS_PMU false + #if MCU_VARIANT == MCU_1284P const int pin_cs = 4; const int pin_reset = 3; @@ -102,6 +117,7 @@ const int pin_dio = 26; const int pin_led_rx = 2; const int pin_led_tx = 4; + #define HAS_PMU true #elif BOARD_MODEL == BOARD_HUZZAH32 const int pin_cs = 4; const int pin_reset = 36; @@ -119,6 +135,8 @@ const int pin_led_rx = 22; const int pin_led_tx = 22; #endif + #define HAS_DISPLAY true + #define HAS_BLUETOOTH true #elif BOARD_MODEL == BOARD_LORA32_V2_1 const int pin_cs = 18; const int pin_reset = 23; @@ -130,6 +148,9 @@ const int pin_led_rx = 25; const int pin_led_tx = 25; #endif + #define HAS_DISPLAY true + #define HAS_BLUETOOTH true + #define HAS_PMU true #elif BOARD_MODEL == BOARD_HELTEC32_V2 const int pin_cs = 18; const int pin_reset = 23; @@ -152,6 +173,8 @@ const int pin_led_rx = 22; const int pin_led_tx = 22; #endif + #define HAS_DISPLAY true + #define HAS_BLUETOOTH true #elif BOARD_MODEL == BOARD_RNODE_NG_21 const int pin_cs = 18; const int pin_reset = 23; @@ -163,10 +186,15 @@ const int pin_led_rx = 25; const int pin_led_tx = 25; #endif + #define HAS_DISPLAY true + #define HAS_BLUETOOTH true + #define HAS_PMU true #else #error An unsupported board was selected. Cannot compile RNode firmware. #endif + bool mw_radio_online = false; + #define CONFIG_UART_BUFFER_SIZE 6144 #define CONFIG_QUEUE_SIZE 6144 #define CONFIG_QUEUE_MAX_LENGTH 200 @@ -206,6 +234,8 @@ bool radio_locked = true; bool radio_online = false; bool hw_ready = false; + bool disp_ready = false; + bool pmu_ready = false; bool promisc = false; bool implicit = false; uint8_t implicit_l = 0; @@ -214,9 +244,10 @@ uint8_t model = 0x00; uint8_t hwrev = 0x00; + int current_rssi = -292; int last_rssi = -292; uint8_t last_rssi_raw = 0x00; - uint8_t last_snr_raw = 0x00; + uint8_t last_snr_raw = 0x80; uint8_t seq = 0xFF; uint16_t read_len = 0; @@ -249,6 +280,14 @@ const uint8_t SIG_SYNCED = 0x02; const uint8_t RX_ONGOING = 0x04; + // Power management + #define BATTERY_STATE_DISCHARGING 0x01 + #define BATTERY_STATE_CHARGING 0x02 + #define BATTERY_STATE_CHARGED 0x03 + float battery_voltage = 0.0; + float battery_percent = 0.0; + uint8_t battery_state = 0x00; + // Boot flags #define START_FROM_BOOTLOADER 0x01 #define START_FROM_POWERON 0x02 diff --git a/Framing.h b/Framing.h index cc5d591..f86eec8 100644 --- a/Framing.h +++ b/Framing.h @@ -27,6 +27,10 @@ #define CMD_BLINK 0x30 #define CMD_RANDOM 0x40 + #define CMD_FB_EXT 0x41 + #define CMD_FB_READ 0x42 + #define CMD_FB_WRITE 0x43 + #define CMD_BOARD 0x47 #define CMD_PLATFORM 0x48 #define CMD_MCU 0x49 diff --git a/RNode_Firmware.ino b/RNode_Firmware.ino index 1df8fc5..4f27efe 100644 --- a/RNode_Firmware.ino +++ b/RNode_Firmware.ino @@ -24,8 +24,6 @@ #include #include "Utilities.h" - - FIFOBuffer serialFIFO; uint8_t serialBuffer[CONFIG_UART_BUFFER_SIZE+1]; @@ -89,9 +87,8 @@ void setup() { LoRa.setPins(pin_cs, pin_reset, pin_dio); #if MCU_VARIANT == MCU_ESP32 - #if BOARD_MODEL == BOARD_TBEAM - Wire.begin(I2C_SDA, I2C_SCL); - initPMU(); + #if HAS_PMU == true + pmu_ready = init_pmu(); #endif kiss_indicate_reset(); @@ -99,6 +96,14 @@ void setup() { // Validate board health, EEPROM and config validateStatus(); + + #if HAS_DISPLAY + disp_ready = display_init(); + #endif + + #if HAS_BLUETOOTH + bt_ready = bt_init(); + #endif } void lora_receive() { @@ -448,6 +453,7 @@ void serialCallback(uint8_t sbyte) { if (frame_len == 0 && command == CMD_UNKNOWN) { command = sbyte; } else if (command == CMD_DATA) { + cable_state = CABLE_STATE_CONNECTED; if (sbyte == FESC) { ESCAPE = true; } else { @@ -547,6 +553,7 @@ void serialCallback(uint8_t sbyte) { set_implicit_length(sbyte); kiss_indicate_implicit_length(); } else if (command == CMD_RADIO_STATE) { + cable_state = CABLE_STATE_CONNECTED; if (sbyte == 0xFF) { kiss_indicate_radiostate(); } else if (sbyte == 0x00) { @@ -571,6 +578,7 @@ void serialCallback(uint8_t sbyte) { kiss_indicate_random(getRandom()); } else if (command == CMD_DETECT) { if (sbyte == DETECT_REQ) { + cable_state = CABLE_STATE_CONNECTED; kiss_indicate_detect(); } } else if (command == CMD_PROMISC) { @@ -623,6 +631,40 @@ void serialCallback(uint8_t sbyte) { eeprom_conf_save(); } else if (command == CMD_CONF_DELETE) { eeprom_conf_delete(); + } else if (command == CMD_FB_EXT) { + #if HAS_DISPLAY == true + if (sbyte == 0xFF) { + kiss_indicate_fbstate(); + } else if (sbyte == 0x00) { + ext_fb_disable(); + kiss_indicate_fbstate(); + } else if (sbyte == 0x01) { + ext_fb_enable(); + kiss_indicate_fbstate(); + } + #endif + } else if (command == CMD_FB_WRITE) { + if (sbyte == FESC) { + ESCAPE = true; + } else { + if (ESCAPE) { + if (sbyte == TFEND) sbyte = FEND; + if (sbyte == TFESC) sbyte = FESC; + ESCAPE = false; + } + cbuf[frame_len++] = sbyte; + } + + if (frame_len == 9) { + uint8_t line = cbuf[0]; + if (line > 63) line = 63; + int fb_o = line*8; + memcpy(fb+fb_o, cbuf+1, 8); + } + } else if (command == CMD_FB_READ) { + if (sbyte != 0x00) { + kiss_indicate_fb(); + } } } } @@ -637,6 +679,7 @@ void updateModemStatus() { #endif uint8_t status = LoRa.modemStatus(); + current_rssi = LoRa.currentRssi(); last_status_update = millis(); #if MCU_VARIANT == MCU_ESP32 @@ -783,6 +826,18 @@ void loop() { #else if (!fifo_isempty_locked(&serialFIFO)) serial_poll(); #endif + + #if HAS_DISPLAY + if (disp_ready) { + update_display(); + } + #endif + + #if HAS_PMU + if (pmu_ready) { + update_pmu(); + } + #endif } volatile bool serial_polling = false; diff --git a/Utilities.h b/Utilities.h index 1c78b9b..5d46926 100644 --- a/Utilities.h +++ b/Utilities.h @@ -6,6 +6,18 @@ #include "Framing.h" #include "MD5.h" +#if HAS_DISPLAY == true + #include "Display.h" +#endif + +#if HAS_BLUETOOTH == true + #include "Bluetooth.h" +#endif + +#if HAS_PMU == true + #include "Power.h" +#endif + #if MCU_VARIANT == MCU_ESP32 #include "soc/rtc_wdt.h" #define ISR_VECT IRAM_ATTR @@ -568,6 +580,37 @@ void kiss_indicate_random(uint8_t byte) { Serial.write(FEND); } +void kiss_indicate_fbstate() { + Serial.write(FEND); + Serial.write(CMD_FB_EXT); + #if HAS_DISPLAY + if (disp_ext_fb) { + Serial.write(0x01); + } else { + Serial.write(0x00); + } + #else + Serial.write(0xFF) + #endif + Serial.write(FEND); +} + +void kiss_indicate_fb() { + Serial.write(FEND); + Serial.write(CMD_FB_READ); + #if HAS_DISPLAY + for (int i = 0; i < 512; i++) { + uint8_t byte = fb[i]; + if (byte == FEND) { Serial.write(FESC); byte = TFEND; } + if (byte == FESC) { Serial.write(FESC); byte = TFESC; } + Serial.write(byte); + } + #else + Serial.write(0xFF) + #endif + Serial.write(FEND); +} + void kiss_indicate_ready() { Serial.write(FEND); Serial.write(CMD_READY); @@ -1087,82 +1130,3 @@ inline void fifo16_init(FIFOBuffer16 *f, uint16_t *buffer, uint16_t size) { inline uint16_t fifo16_len(FIFOBuffer16 *f) { return (f->end - f->begin); } - -#if BOARD_MODEL == BOARD_TBEAM - #include - AXP20X_Class PMU; - - bool initPMU() - { - if (PMU.begin(Wire, AXP192_SLAVE_ADDRESS) == AXP_FAIL) { - return false; - } - /* - * The charging indicator can be turned on or off - * * * */ - PMU.setChgLEDMode(AXP20X_LED_OFF); - - /* - * The default ESP32 power supply has been turned on, - * no need to set, please do not set it, if it is turned off, - * it will not be able to program - * - * PMU.setDCDC3Voltage(3300); - * PMU.setPowerOutPut(AXP192_DCDC3, AXP202_ON); - * - * * * */ - - /* - * Turn off unused power sources to save power - * **/ - - PMU.setPowerOutPut(AXP192_DCDC1, AXP202_OFF); - PMU.setPowerOutPut(AXP192_DCDC2, AXP202_OFF); - PMU.setPowerOutPut(AXP192_LDO2, AXP202_OFF); - PMU.setPowerOutPut(AXP192_LDO3, AXP202_OFF); - PMU.setPowerOutPut(AXP192_EXTEN, AXP202_OFF); - - /* - * Set the power of LoRa and GPS module to 3.3V - **/ - PMU.setLDO2Voltage(3300); //LoRa VDD - PMU.setLDO3Voltage(3300); //GPS VDD - PMU.setDCDC1Voltage(3300); //3.3V Pin next to 21 and 22 is controlled by DCDC1 - - PMU.setPowerOutPut(AXP192_DCDC1, AXP202_ON); - - // Turn on SX1276 - PMU.setPowerOutPut(AXP192_LDO2, AXP202_ON); - - // Turn off GPS - PMU.setPowerOutPut(AXP192_LDO3, AXP202_OFF); - - pinMode(PMU_IRQ, INPUT_PULLUP); - attachInterrupt(PMU_IRQ, [] { - // pmu_irq = true; - }, FALLING); - - PMU.adc1Enable(AXP202_VBUS_VOL_ADC1 | - AXP202_VBUS_CUR_ADC1 | - AXP202_BATT_CUR_ADC1 | - AXP202_BATT_VOL_ADC1, - AXP202_ON); - - PMU.enableIRQ(AXP202_VBUS_REMOVED_IRQ | - AXP202_VBUS_CONNECT_IRQ | - AXP202_BATT_REMOVED_IRQ | - AXP202_BATT_CONNECT_IRQ, - AXP202_ON); - PMU.clearIRQ(); - - return true; - } - - void disablePeripherals() - { - PMU.setPowerOutPut(AXP192_DCDC1, AXP202_OFF); - PMU.setPowerOutPut(AXP192_LDO2, AXP202_OFF); - PMU.setPowerOutPut(AXP192_LDO3, AXP202_OFF); - } - -#endif