Browse Source

LillyGo TDeck support

WattleFoxxo 10 months ago
parent
commit
f100894882

+ 38 - 0
boards/t-deck.json

@@ -0,0 +1,38 @@
+{
+  "build": {
+    "arduino": {
+      "ldscript": "esp32s3_out.ld",
+      "partitions": "default_16MB.csv",
+      "memory_type": "qio_opi"
+    },
+    "core": "esp32",
+    "extra_flags": [
+      "-DARDUINO_USB_MODE=1",
+      "-DARDUINO_RUNNING_CORE=1",
+      "-DARDUINO_EVENT_RUNNING_CORE=1"
+    ],
+    "f_cpu": "240000000L",
+    "f_flash": "80000000L",
+    "flash_mode": "qio",
+    "hwids": [["0x303A", "0x1001"]],
+    "mcu": "esp32s3",
+    "variant": "esp32s3"
+  },
+  "connectivity": ["wifi", "bluetooth"],
+  "debug": {
+    "default_tool": "esp-builtin",
+    "onboard_tools": ["esp-builtin"],
+    "openocd_target": "esp32s3.cfg"
+  },
+  "frameworks": ["arduino", "espidf"],
+  "name": "LilyGo T-Deck (16M Flash 8M PSRAM)",
+  "upload": {
+    "flash_size": "16MB",
+    "maximum_ram_size": 327680,
+    "maximum_size": 16777216,
+    "require_upload_port": true,
+    "speed": 921600
+  },
+  "url": "https://www.lilygo.cc",
+  "vendor": "LilyGo"
+}

+ 141 - 0
src/helpers/ui/ST7789LCDDisplay.cpp

@@ -0,0 +1,141 @@
+#include "ST7789LCDDisplay.h"
+
+#ifndef DISPLAY_ROTATION
+  #define DISPLAY_ROTATION 3
+#endif
+
+#ifndef DISPLAY_SCALE_X
+  #define DISPLAY_SCALE_X 2.5f // 320 / 128
+#endif
+
+#ifndef DISPLAY_SCALE_Y
+  #define DISPLAY_SCALE_Y 3.75f // 240 / 64
+#endif
+
+#define DISPLAY_WIDTH 240
+#define DISPLAY_HEIGHT 320
+
+bool ST7789LCDDisplay::i2c_probe(TwoWire& wire, uint8_t addr) {
+  return true;
+}
+
+bool ST7789LCDDisplay::begin() {
+  if (!_isOn) {
+    if (_peripher_power) _peripher_power->claim();
+
+    pinMode(PIN_TFT_LEDA_CTL, OUTPUT);
+    digitalWrite(PIN_TFT_LEDA_CTL, HIGH);
+    digitalWrite(PIN_TFT_RST, HIGH);
+
+    // Im not sure if this is just a t-deck problem or not, if your display is slow try this.
+    #ifdef LILYGO_TDECK
+      displaySPI.begin(PIN_TFT_SCL, -1, PIN_TFT_SDA, PIN_TFT_CS);
+    #endif
+
+    display.init(DISPLAY_WIDTH, DISPLAY_HEIGHT);
+    display.setRotation(DISPLAY_ROTATION);
+
+    display.setSPISpeed(40e6);
+
+    display.fillScreen(ST77XX_BLACK);
+    display.setTextColor(ST77XX_WHITE);
+    display.setTextSize(2); 
+    display.cp437(true); // Use full 256 char 'Code Page 437' font
+  
+    _isOn = true;
+  }
+
+  return true;
+}
+
+void ST7789LCDDisplay::turnOn() {
+  ST7789LCDDisplay::begin();
+}
+
+void ST7789LCDDisplay::turnOff() {
+  if (_isOn) {
+    digitalWrite(PIN_TFT_LEDA_CTL, HIGH);
+    digitalWrite(PIN_TFT_RST, LOW);
+    digitalWrite(PIN_TFT_LEDA_CTL, LOW);
+    _isOn = false;
+
+    if (_peripher_power) _peripher_power->release();
+  }
+}
+
+void ST7789LCDDisplay::clear() {
+  display.fillScreen(ST77XX_BLACK);
+}
+
+void ST7789LCDDisplay::startFrame(Color bkg) {
+  display.fillScreen(ST77XX_BLACK);
+  display.setTextColor(ST77XX_WHITE);
+  display.setTextSize(1); // This one affects size of Please wait... message
+  display.cp437(true); // Use full 256 char 'Code Page 437' font
+}
+
+void ST7789LCDDisplay::setTextSize(int sz) {
+  display.setTextSize(sz);
+}
+
+void ST7789LCDDisplay::setColor(Color c) {
+  switch (c) {
+    case DisplayDriver::DARK :
+      _color = ST77XX_BLACK;
+      break;
+    case DisplayDriver::LIGHT : 
+      _color = ST77XX_WHITE;
+      break;
+    case DisplayDriver::RED : 
+      _color = ST77XX_RED;
+      break;
+    case DisplayDriver::GREEN : 
+      _color = ST77XX_GREEN;
+      break;
+    case DisplayDriver::BLUE : 
+      _color = ST77XX_BLUE;
+      break;
+    case DisplayDriver::YELLOW : 
+      _color = ST77XX_YELLOW;
+      break;
+    case DisplayDriver::ORANGE : 
+      _color = ST77XX_ORANGE;
+      break;
+    default:
+      _color = ST77XX_WHITE;
+      break;
+  }
+  display.setTextColor(_color);
+}
+
+void ST7789LCDDisplay::setCursor(int x, int y) {
+  display.setCursor(x * DISPLAY_SCALE_X, y * DISPLAY_SCALE_Y);
+}
+
+void ST7789LCDDisplay::print(const char* str) {
+  display.print(str);
+}
+
+void ST7789LCDDisplay::fillRect(int x, int y, int w, int h) {
+  display.fillRect(x * DISPLAY_SCALE_X, y * DISPLAY_SCALE_Y, w * DISPLAY_SCALE_X, h * DISPLAY_SCALE_Y, _color);
+}
+
+void ST7789LCDDisplay::drawRect(int x, int y, int w, int h) {
+  display.drawRect(x * DISPLAY_SCALE_X, y * DISPLAY_SCALE_Y, w * DISPLAY_SCALE_X, h * DISPLAY_SCALE_Y, _color);
+}
+
+void ST7789LCDDisplay::drawXbm(int x, int y, const uint8_t* bits, int w, int h) {
+  display.drawBitmap(x * DISPLAY_SCALE_X, y * DISPLAY_SCALE_Y, bits, w, h, _color);
+}
+
+uint16_t ST7789LCDDisplay::getTextWidth(const char* str) {
+  int16_t x1, y1;
+  uint16_t w, h;
+  display.getTextBounds(str, 0, 0, &x1, &y1, &w, &h);
+
+  return w / DISPLAY_SCALE_X;
+}
+
+void ST7789LCDDisplay::endFrame() {
+  // display.display();
+}

+ 60 - 0
src/helpers/ui/ST7789LCDDisplay.h

@@ -0,0 +1,60 @@
+#pragma once
+
+#include "DisplayDriver.h"
+#include <Wire.h>
+#include <SPI.h>
+#include <Adafruit_GFX.h>
+#include <Adafruit_ST7789.h>
+#include <helpers/RefCountedDigitalPin.h>
+
+class ST7789LCDDisplay : public DisplayDriver {
+  #ifdef LILYGO_TDECK
+    SPIClass displaySPI;
+  #endif
+  Adafruit_ST7789 display;
+  bool _isOn;
+  uint16_t _color;
+  RefCountedDigitalPin* _peripher_power;
+
+  bool i2c_probe(TwoWire& wire, uint8_t addr);
+public:
+#ifdef USE_PIN_TFT
+  ST7789LCDDisplay(RefCountedDigitalPin* peripher_power=NULL) : DisplayDriver(128, 64), 
+      display(PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_SDA, PIN_TFT_SCL, PIN_TFT_RST),
+      _peripher_power(peripher_power)
+  {
+    _isOn = false;
+  }
+#elif LILYGO_TDECK
+  ST7789LCDDisplay(RefCountedDigitalPin* peripher_power=NULL) : DisplayDriver(128, 64),
+      displaySPI(HSPI),
+      display(&displaySPI, PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_RST),
+      _peripher_power(peripher_power)
+  {
+    _isOn = false;
+  }
+#else
+  ST7789LCDDisplay(RefCountedDigitalPin* peripher_power=NULL) : DisplayDriver(128, 64), 
+      display(&SPI, PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_RST),
+      _peripher_power(peripher_power)
+  {
+    _isOn = false;
+  }
+#endif
+  bool begin();
+
+  bool isOn() override { return _isOn; }
+  void turnOn() override;
+  void turnOff() override;
+  void clear() override;
+  void startFrame(Color bkg = DARK) override;
+  void setTextSize(int sz) override;
+  void setColor(Color c) override;
+  void setCursor(int x, int y) override;
+  void print(const char* str) override;
+  void fillRect(int x, int y, int w, int h) override;
+  void drawRect(int x, int y, int w, int h) override;
+  void drawXbm(int x, int y, const uint8_t* bits, int w, int h) override;
+  uint16_t getTextWidth(const char* str) override;
+  void endFrame() override;
+};

+ 35 - 0
variants/lilygo_tdeck/TDeckBoard.cpp

@@ -0,0 +1,35 @@
+#include <Arduino.h>
+#include "TDeckBoard.h"
+
+uint32_t deviceOnline = 0x00;
+
+void TDeckBoard::begin() {
+  
+  ESP32Board::begin();
+  
+  // Enable peripheral power
+  pinMode(PIN_PERF_POWERON, OUTPUT);
+  digitalWrite(PIN_PERF_POWERON, HIGH);
+
+  // Configure user button
+  pinMode(PIN_USER_BTN, INPUT);
+
+  // Configure LoRa Pins
+  pinMode(P_LORA_MISO, INPUT_PULLUP);
+  // pinMode(P_LORA_DIO_1, INPUT_PULLUP);
+
+  #ifdef P_LORA_TX_LED
+    digitalWrite(P_LORA_TX_LED, HIGH); // inverted pin for SX1276 - HIGH for off
+  #endif
+
+  esp_reset_reason_t reason = esp_reset_reason();
+  if (reason == ESP_RST_DEEPSLEEP) {
+    long wakeup_source = esp_sleep_get_ext1_wakeup_status();
+    if (wakeup_source & (1 << P_LORA_DIO_1)) {
+      startup_reason = BD_STARTUP_RX_PACKET; // received a LoRa packet (while in deep sleep)
+    }
+
+    rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
+    rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
+  }
+}

+ 68 - 0
variants/lilygo_tdeck/TDeckBoard.h

@@ -0,0 +1,68 @@
+#pragma once
+
+#include <Wire.h>
+#include <Arduino.h>
+#include "helpers/ESP32Board.h"
+#include <driver/rtc_io.h>
+
+#define PIN_VBAT_READ 4
+#define BATTERY_SAMPLES 8
+#define ADC_MULTIPLIER (2.0f * 3.3f * 1000)
+
+class TDeckBoard : public ESP32Board {
+public:
+  void begin();
+
+  #ifdef P_LORA_TX_LED
+    void onBeforeTransmit() override{
+      digitalWrite(P_LORA_TX_LED, LOW); // turn TX LED on - invert pin for SX1276
+    }
+
+    void onAfterTransmit() override{
+      digitalWrite(P_LORA_TX_LED, HIGH); // turn TX LED off - invert pin for SX1276
+    }
+  #endif
+
+  void enterDeepSleep(uint32_t secs, int pin_wake_btn) {
+    esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
+
+    // Make sure the DIO1 and NSS GPIOs are hold on required levels during deep sleep
+    rtc_gpio_set_direction((gpio_num_t)P_LORA_DIO_1, RTC_GPIO_MODE_INPUT_ONLY);
+    rtc_gpio_pulldown_en((gpio_num_t)P_LORA_DIO_1);
+
+    rtc_gpio_hold_en((gpio_num_t)P_LORA_NSS);
+
+    if (pin_wake_btn < 0) {
+      esp_sleep_enable_ext1_wakeup( (1L << P_LORA_DIO_1), ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on: recv LoRa packet
+    } else {
+      esp_sleep_enable_ext1_wakeup( (1L << P_LORA_DIO_1) | (1L << pin_wake_btn), ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on: recv LoRa packet OR wake btn
+    }
+
+    if (secs > 0) {
+      esp_sleep_enable_timer_wakeup(secs * 1000000);
+    }
+
+    // Finally set ESP32 into sleep
+    esp_deep_sleep_start(); // CPU halts here and never returns!
+  }
+
+  uint16_t getBattMilliVolts() {
+    #if defined(PIN_VBAT_READ) && defined(ADC_MULTIPLIER)
+      analogReadResolution(12);
+
+      uint32_t raw = 0;
+      for (int i = 0; i < BATTERY_SAMPLES; i++) {
+        raw += analogRead(PIN_VBAT_READ);
+      }
+
+      raw = raw / BATTERY_SAMPLES;
+      return (ADC_MULTIPLIER * raw) / 4096;
+    #else
+      return 0;
+    #endif
+  }
+
+  const char* getManufacturerName() const{
+    return "LilyGo T-Deck";
+  }
+};

+ 96 - 0
variants/lilygo_tdeck/platformio.ini

@@ -0,0 +1,96 @@
+[LilyGo_TDeck]
+extends = esp32_base
+board = t-deck
+build_flags =
+  ${esp32_base.build_flags}
+  ${sensor_base.build_flags}
+  -I variants/lilygo_tdeck
+  -D LILYGO_TDECK
+  -D BOARD_HAS_PSRAM=1
+  -D CORE_DEBUG_LEVEL=1
+  -D ARDUINO_USB_CDC_ON_BOOT=1
+  -D PIN_USER_BTN=0  ; Trackball button
+  -D PIN_PERF_POWERON=10 ; Peripheral power pin
+  -D RADIO_CLASS=CustomSX1262
+  -D WRAPPER_CLASS=CustomSX1262Wrapper
+  -D LORA_TX_POWER=22
+  -D SX126X_DIO2_AS_RF_SWITCH=false
+  -D SX126X_CURRENT_LIMIT=140
+  -D SX126X_RX_BOOSTED_GAIN=1
+  -D SX126X_DIO3_TCXO_VOLTAGE=1.8f
+  -D P_LORA_DIO_1=45 ; LORA IRQ pin
+  -D P_LORA_NSS=9    ; LORA SS pin
+  -D P_LORA_RESET=17 ; LORA RST pin
+  -D P_LORA_BUSY=13  ; LORA Busy pin
+  -D P_LORA_SCLK=40  ; LORA SCLK pin
+  -D P_LORA_MISO=38  ; LORA MISO pin
+  -D P_LORA_MOSI=41  ; LORA MOSI pin
+  -D DISPLAY_CLASS=ST7789LCDDisplay
+  -D DISPLAY_SCALE_X=2.5
+  -D DISPLAY_SCALE_Y=3.75
+  -D PIN_TFT_RST=-1
+  -D PIN_TFT_VDD_CTL=-1
+  -D PIN_TFT_LEDA_CTL=42
+  -D PIN_TFT_CS=12
+  -D PIN_TFT_DC=11
+  -D PIN_TFT_SCL=40
+  -D PIN_TFT_SDA=41
+build_src_filter = ${esp32_base.build_src_filter}
+  +<../variants/lilygo_tdeck>
+lib_deps =
+  ${esp32_base.lib_deps}
+  ${sensor_base.lib_deps}
+  adafruit/Adafruit ST7735 and ST7789 Library @ ^1.11.0
+
+[env:LilyGo_TDeck_companion_radio_usb]
+extends = LilyGo_TDeck
+build_flags =
+  ${LilyGo_TDeck.build_flags}
+  -I examples/companion_radio/ui-new
+  -D MAX_CONTACTS=350
+  -D MAX_GROUP_CHANNELS=40
+  -D OFFLINE_QUEUE_SIZE=256
+build_src_filter = ${LilyGo_TDeck.build_src_filter}
+  +<helpers/esp32/*.cpp>
+  +<helpers/ui/MomentaryButton.cpp>
+  +<../examples/companion_radio/*.cpp>
+  +<../examples/companion_radio/ui-new/*.cpp>
+  +<helpers/ui/ST7789LCDDisplay.cpp>
+lib_deps =
+  ${LilyGo_TDeck.lib_deps}
+  densaugeo/base64 @ ~1.4.0
+
+[env:LilyGo_TDeck_companion_radio_ble]
+extends = LilyGo_TDeck
+build_flags =
+  ${LilyGo_TDeck.build_flags}
+  -I examples/companion_radio/ui-new
+  -D MAX_CONTACTS=350
+  -D MAX_GROUP_CHANNELS=40
+  -D BLE_PIN_CODE=123456
+  -D OFFLINE_QUEUE_SIZE=256
+build_src_filter = ${LilyGo_TDeck.build_src_filter}
+  +<helpers/esp32/*.cpp>
+  +<helpers/ui/MomentaryButton.cpp>
+  +<../examples/companion_radio/*.cpp>
+  +<../examples/companion_radio/ui-new/*.cpp>
+  +<helpers/ui/ST7789LCDDisplay.cpp>
+lib_deps =
+  ${LilyGo_TDeck.lib_deps}
+  densaugeo/base64 @ ~1.4.0
+
+[env:LilyGo_TDeck_repeater]
+extends = LilyGo_TDeck
+build_flags =
+  ${LilyGo_TDeck.build_flags}
+  -D ADVERT_NAME='"TDeck Repeater"'
+  -D ADVERT_LAT=0.0
+  -D ADVERT_LON=0.0
+  -D ADMIN_PASSWORD='"password"'
+  -D MAX_NEIGHBOURS=8
+build_src_filter = ${LilyGo_TDeck.build_src_filter}
+  +<../examples/simple_repeater>
+  +<helpers/ui/ST7789LCDDisplay.cpp>
+lib_deps =
+  ${LilyGo_TDeck.lib_deps}
+  ${esp32_ota.lib_deps}

+ 53 - 0
variants/lilygo_tdeck/target.cpp

@@ -0,0 +1,53 @@
+#include <Arduino.h>
+#include "target.h"
+
+TDeckBoard board;
+
+#if defined(P_LORA_SCLK)
+  static SPIClass spi;
+  RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY, spi);
+#else
+  RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY);
+#endif
+
+WRAPPER_CLASS radio_driver(radio, board);
+
+ESP32RTCClock fallback_clock;
+AutoDiscoverRTCClock rtc_clock(fallback_clock);
+SensorManager sensors;
+
+#ifdef DISPLAY_CLASS
+  DISPLAY_CLASS display;
+  MomentaryButton user_btn(PIN_USER_BTN, 1000, true);
+#endif
+
+bool radio_init() {
+  fallback_clock.begin();
+  rtc_clock.begin(Wire);
+
+#if defined(P_LORA_SCLK)
+  return radio.std_init(&spi);
+#else
+  return radio.std_init();
+#endif
+}
+
+uint32_t radio_get_rng_seed() {
+  return radio.random(0x7FFFFFFF);
+}
+
+void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr) {
+  radio.setFrequency(freq);
+  radio.setSpreadingFactor(sf);
+  radio.setBandwidth(bw);
+  radio.setCodingRate(cr);
+}
+
+void radio_set_tx_power(uint8_t dbm) {
+  radio.setOutputPower(dbm);
+}
+
+mesh::LocalIdentity radio_new_identity() {
+  RadioNoiseListener rng(radio);
+  return mesh::LocalIdentity(&rng); // create new random identity
+}

+ 29 - 0
variants/lilygo_tdeck/target.h

@@ -0,0 +1,29 @@
+#pragma once
+
+#define RADIOLIB_STATIC_ONLY 1
+#include <RadioLib.h>
+#include <helpers/radiolib/RadioLibWrappers.h>
+#include <helpers/radiolib/CustomSX1262Wrapper.h>
+#include <TDeckBoard.h>
+#include <helpers/AutoDiscoverRTCClock.h>
+#include <helpers/SensorManager.h>
+#ifdef DISPLAY_CLASS
+  #include <helpers/ui/ST7789LCDDisplay.h>
+  #include <helpers/ui/MomentaryButton.h>
+#endif
+
+extern TDeckBoard board;
+extern WRAPPER_CLASS radio_driver;
+extern AutoDiscoverRTCClock rtc_clock;
+extern SensorManager sensors;
+
+#ifdef DISPLAY_CLASS
+  extern DISPLAY_CLASS display;
+  extern MomentaryButton user_btn;
+#endif
+
+bool radio_init();
+uint32_t radio_get_rng_seed();
+void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr);
+void radio_set_tx_power(uint8_t dbm);
+mesh::LocalIdentity radio_new_identity();