committed by
GitHub
parent
6d80144e12
commit
85e26636a3
@@ -0,0 +1,5 @@
|
||||
idf_component_register(
|
||||
SRC_DIRS "."
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES Tactility esp_lvgl_port esp_lcd esp_lcd_touch_gt911 driver vfs fatfs
|
||||
)
|
||||
@@ -0,0 +1,101 @@
|
||||
#include "config.h"
|
||||
#include "display_i.h"
|
||||
#include "driver/spi_common.h"
|
||||
#include "keyboard.h"
|
||||
#include "TactilityCore.h"
|
||||
|
||||
#define TAG "tdeck_bootstrap"
|
||||
|
||||
static bool tdeck_power_on() {
|
||||
gpio_config_t device_power_signal_config = {
|
||||
.pin_bit_mask = BIT64(TDECK_POWERON_GPIO),
|
||||
.mode = GPIO_MODE_OUTPUT,
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
};
|
||||
|
||||
if (gpio_config(&device_power_signal_config) != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (gpio_set_level(TDECK_POWERON_GPIO, 1) != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool init_i2c() {
|
||||
const i2c_config_t i2c_conf = {
|
||||
.mode = I2C_MODE_MASTER,
|
||||
.sda_io_num = GPIO_NUM_18,
|
||||
.scl_io_num = GPIO_NUM_8,
|
||||
.sda_pullup_en = GPIO_PULLUP_DISABLE,
|
||||
.scl_pullup_en = GPIO_PULLUP_DISABLE,
|
||||
.master = {
|
||||
.clk_speed = 400000
|
||||
}
|
||||
};
|
||||
|
||||
return i2c_param_config(TDECK_I2C_BUS_HANDLE, &i2c_conf) == ESP_OK && i2c_driver_install(TDECK_I2C_BUS_HANDLE, i2c_conf.mode, 0, 0, 0) == ESP_OK;
|
||||
}
|
||||
|
||||
static bool init_spi() {
|
||||
spi_bus_config_t bus_config = {
|
||||
.mosi_io_num = TDECK_SPI_PIN_MOSI,
|
||||
.miso_io_num = TDECK_SPI_PIN_MISO,
|
||||
.sclk_io_num = TDECK_SPI_PIN_SCLK,
|
||||
.quadwp_io_num = -1, // Quad SPI LCD driver is not yet supported
|
||||
.quadhd_io_num = -1, // Quad SPI LCD driver is not yet supported
|
||||
.max_transfer_sz = TDECK_SPI_TRANSFER_SIZE_LIMIT,
|
||||
};
|
||||
|
||||
if (spi_bus_initialize(TDECK_SPI_HOST, &bus_config, SPI_DMA_CH_AUTO) != ESP_OK) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool tdeck_bootstrap() {
|
||||
ESP_LOGI(TAG, "Power on");
|
||||
if (!tdeck_power_on()) {
|
||||
TT_LOG_E(TAG, "Power on failed");
|
||||
}
|
||||
|
||||
/**
|
||||
* Without this delay, the touch driver randomly fails when the device is USB-powered:
|
||||
* > lcd_panel.io.i2c: panel_io_i2c_rx_buffer(135): i2c transaction failed
|
||||
* > GT911: touch_gt911_read_cfg(352): GT911 read error!
|
||||
* This might not be a problem with a lipo, but I haven't been able to test that.
|
||||
* I tried to solve it just like I did with the keyboard:
|
||||
* By reading from I2C until it succeeds and to then init the driver.
|
||||
* It doesn't work, because it never recovers from the error.
|
||||
*/
|
||||
TT_LOG_I(TAG, "Waiting after power-on");
|
||||
tt::delay_ms(TDECK_POWERON_DELAY);
|
||||
|
||||
TT_LOG_I(TAG, "Init I2C");
|
||||
if (!init_i2c()) {
|
||||
TT_LOG_E(TAG, "Init I2C failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
TT_LOG_I(TAG, "Init SPI");
|
||||
if (!init_spi()) {
|
||||
TT_LOG_E(TAG, "Init SPI failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Don't turn the backlight on yet - Tactility init will take care of it
|
||||
TT_LOG_I(TAG, "Init backlight");
|
||||
if (!tdeck_backlight_init()) {
|
||||
TT_LOG_E(TAG, "Init backlight failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
keyboard_wait_for_response();
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
#pragma once
|
||||
|
||||
#include "driver/i2c.h"
|
||||
#include "driver/gpio.h"
|
||||
|
||||
// Main bus, used by GT911 touch hardware and keyboard
|
||||
#define TDECK_I2C_BUS_HANDLE I2C_NUM_0
|
||||
|
||||
// SPI
|
||||
#define TDECK_SPI_HOST SPI2_HOST
|
||||
#define TDECK_SPI_PIN_SCLK GPIO_NUM_40
|
||||
#define TDECK_SPI_PIN_MOSI GPIO_NUM_41
|
||||
#define TDECK_SPI_PIN_MISO GPIO_NUM_38
|
||||
#define TDECK_SPI_TRANSFER_SIZE_LIMIT (TDECK_LCD_HORIZONTAL_RESOLUTION * TDECK_LCD_SPI_TRANSFER_HEIGHT * (TDECK_LCD_BITS_PER_PIXEL / 8))
|
||||
|
||||
// Power on
|
||||
#define TDECK_POWERON_GPIO GPIO_NUM_10
|
||||
#define TDECK_POWERON_DELAY 2000 // milliseconds - see bootstrap.c for explanation
|
||||
|
||||
// Display
|
||||
#define TDECK_LCD_SPI_HOST SPI2_HOST
|
||||
#define TDECK_LCD_PIN_CS GPIO_NUM_12
|
||||
#define TDECK_LCD_PIN_DC GPIO_NUM_11 // RS
|
||||
#define TDECK_LCD_PIN_BACKLIGHT GPIO_NUM_42
|
||||
#define TDECK_LCD_SPI_FREQUENCY 40000000
|
||||
#define TDECK_LCD_HORIZONTAL_RESOLUTION 320
|
||||
#define TDECK_LCD_VERTICAL_RESOLUTION 240
|
||||
#define TDECK_LCD_BITS_PER_PIXEL 16
|
||||
#define TDECK_LCD_DRAW_BUFFER_HEIGHT (TDECK_LCD_VERTICAL_RESOLUTION / 10)
|
||||
#define TDECK_LCD_SPI_TRANSFER_HEIGHT (TDECK_LCD_VERTICAL_RESOLUTION / 10)
|
||||
|
||||
// LVGL
|
||||
// The minimum task stack seems to be about 3500, but that crashes the wifi app in some scenarios
|
||||
// At 4000, it crashes when the fps renderer is available
|
||||
#define TDECK_LVGL_TASK_STACK_DEPTH 8192
|
||||
|
||||
// Dipslay backlight (PWM)
|
||||
#define TDECK_LCD_BACKLIGHT_LEDC_TIMER LEDC_TIMER_0
|
||||
#define TDECK_LCD_BACKLIGHT_LEDC_MODE LEDC_LOW_SPEED_MODE
|
||||
#define TDECK_LCD_BACKLIGHT_LEDC_CHANNEL LEDC_CHANNEL_0
|
||||
#define TDECK_LCD_BACKLIGHT_LEDC_DUTY_RES LEDC_TIMER_8_BIT
|
||||
#define TDECK_LCD_BACKLIGHT_LEDC_FREQUENCY (4000)
|
||||
|
||||
// Touch (GT911)
|
||||
#define TDECK_TOUCH_I2C_BUS_HANDLE TDECK_I2C_BUS_HANDLE
|
||||
#define TDECK_TOUCH_X_MAX 240
|
||||
#define TDECK_TOUCH_Y_MAX 320
|
||||
#define TDECK_TOUCH_PIN_INT GPIO_NUM_16
|
||||
|
||||
// SD Card
|
||||
#define TDECK_SDCARD_SPI_HOST SPI2_HOST
|
||||
#define TDECK_SDCARD_PIN_CS GPIO_NUM_39
|
||||
#define TDECK_SDCARD_SPI_FREQUENCY 800000U
|
||||
#define TDECK_SDCARD_FORMAT_ON_MOUNT_FAILED false
|
||||
#define TDECK_SDCARD_MAX_OPEN_FILES 4
|
||||
#define TDECK_SDCARD_ALLOC_UNIT_SIZE (16 * 1024)
|
||||
#define TDECK_SDCARD_STATUS_CHECK_ENABLED false
|
||||
|
||||
// Keyboard
|
||||
#define TDECK_KEYBOARD_I2C_BUS_HANDLE TDECK_I2C_BUS_HANDLE
|
||||
#define TDECK_KEYBOARD_SLAVE_ADDRESS 0x55
|
||||
|
||||
// Lora (optional)
|
||||
#define TDECK_RADIO_PIN_CS GPIO_NUM_9
|
||||
@@ -0,0 +1,143 @@
|
||||
#include "config.h"
|
||||
#include "driver/ledc.h"
|
||||
#include "driver/spi_master.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_lcd_panel_ops.h"
|
||||
#include "esp_lcd_panel_vendor.h"
|
||||
#include "esp_lvgl_port.h"
|
||||
#include "Log.h"
|
||||
|
||||
#define TAG "tdeck_display"
|
||||
|
||||
bool tdeck_backlight_init() {
|
||||
ledc_timer_config_t ledc_timer = {
|
||||
.speed_mode = TDECK_LCD_BACKLIGHT_LEDC_MODE,
|
||||
.duty_resolution = TDECK_LCD_BACKLIGHT_LEDC_DUTY_RES,
|
||||
.timer_num = TDECK_LCD_BACKLIGHT_LEDC_TIMER,
|
||||
.freq_hz = TDECK_LCD_BACKLIGHT_LEDC_FREQUENCY,
|
||||
.clk_cfg = LEDC_AUTO_CLK
|
||||
};
|
||||
|
||||
if (ledc_timer_config(&ledc_timer) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Backlight led timer config failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void tdeck_backlight_set(uint8_t duty) {
|
||||
ledc_channel_config_t ledc_channel = {
|
||||
.gpio_num = TDECK_LCD_PIN_BACKLIGHT,
|
||||
.speed_mode = TDECK_LCD_BACKLIGHT_LEDC_MODE,
|
||||
.channel = TDECK_LCD_BACKLIGHT_LEDC_CHANNEL,
|
||||
.intr_type = LEDC_INTR_DISABLE,
|
||||
.timer_sel = TDECK_LCD_BACKLIGHT_LEDC_TIMER,
|
||||
.duty = duty,
|
||||
.hpoint = 0
|
||||
};
|
||||
|
||||
// Setting the config in the timer init and then calling ledc_set_duty() doesn't work when
|
||||
// the app is running. For an unknown reason we have to call this config method every time:
|
||||
if (ledc_channel_config(&ledc_channel) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to configure display backlight");
|
||||
}
|
||||
}
|
||||
|
||||
lv_display_t* tdeck_display_init() {
|
||||
const esp_lcd_panel_io_spi_config_t panel_io_config = {
|
||||
.cs_gpio_num = TDECK_LCD_PIN_CS,
|
||||
.dc_gpio_num = TDECK_LCD_PIN_DC,
|
||||
.spi_mode = 0,
|
||||
.pclk_hz = TDECK_LCD_SPI_FREQUENCY,
|
||||
.trans_queue_depth = 10,
|
||||
.on_color_trans_done = nullptr,
|
||||
.user_ctx = nullptr,
|
||||
.lcd_cmd_bits = 8,
|
||||
.lcd_param_bits = 8,
|
||||
.flags = {
|
||||
.dc_low_on_data = 0,
|
||||
.octal_mode = 0,
|
||||
.quad_mode = 0,
|
||||
.sio_mode = 1,
|
||||
.lsb_first = 0,
|
||||
.cs_high_active = 0,
|
||||
}
|
||||
};
|
||||
|
||||
esp_lcd_panel_io_handle_t io_handle;
|
||||
if (esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)TDECK_LCD_SPI_HOST, &panel_io_config, &io_handle) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "failed to create panel IO");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const esp_lcd_panel_dev_config_t panel_config = {
|
||||
.reset_gpio_num = GPIO_NUM_NC,
|
||||
.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB,
|
||||
.data_endian = LCD_RGB_DATA_ENDIAN_BIG,
|
||||
.bits_per_pixel = TDECK_LCD_BITS_PER_PIXEL,
|
||||
.flags = {
|
||||
.reset_active_high = 0
|
||||
},
|
||||
.vendor_config = nullptr
|
||||
};
|
||||
|
||||
esp_lcd_panel_handle_t panel_handle;
|
||||
if (esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "failed to create panel");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (esp_lcd_panel_reset(panel_handle) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "failed to reset panel");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (esp_lcd_panel_init(panel_handle) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "failed to init panel");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (esp_lcd_panel_invert_color(panel_handle, true) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "failed to init panel");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (esp_lcd_panel_swap_xy(panel_handle, true) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "failed to init panel");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (esp_lcd_panel_mirror(panel_handle, true, false) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "failed to init panel");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (esp_lcd_panel_disp_on_off(panel_handle, true) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "failed to turn display on");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const lvgl_port_display_cfg_t disp_cfg = {
|
||||
.io_handle = io_handle,
|
||||
.panel_handle = panel_handle,
|
||||
.buffer_size = TDECK_LCD_HORIZONTAL_RESOLUTION * TDECK_LCD_DRAW_BUFFER_HEIGHT * (TDECK_LCD_BITS_PER_PIXEL / 8),
|
||||
.double_buffer = true, // Disable to free up SPIRAM
|
||||
.hres = TDECK_LCD_HORIZONTAL_RESOLUTION,
|
||||
.vres = TDECK_LCD_VERTICAL_RESOLUTION,
|
||||
.monochrome = false,
|
||||
.rotation = {
|
||||
.swap_xy = true,
|
||||
.mirror_x = true,
|
||||
.mirror_y = false,
|
||||
},
|
||||
.flags = {
|
||||
.buff_dma = false,
|
||||
.buff_spiram = true,
|
||||
.sw_rotate = false,
|
||||
.swap_bytes = true
|
||||
}
|
||||
};
|
||||
|
||||
return lvgl_port_add_disp(&disp_cfg);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "lvgl.h"
|
||||
|
||||
lv_display_t* tdeck_display_init();
|
||||
|
||||
bool tdeck_backlight_init();
|
||||
|
||||
void tdeck_backlight_set(uint8_t duty);
|
||||
@@ -0,0 +1,86 @@
|
||||
#include "keyboard.h"
|
||||
#include "config.h"
|
||||
#include "lvgl.h"
|
||||
#include "TactilityCore.h"
|
||||
#include "Ui/LvglKeypad.h"
|
||||
#include <driver/i2c.h>
|
||||
|
||||
#define TAG "tdeck_keyboard"
|
||||
|
||||
typedef struct {
|
||||
lv_indev_t* device;
|
||||
} KeyboardData;
|
||||
|
||||
static inline esp_err_t keyboard_i2c_read(uint8_t* output) {
|
||||
return i2c_master_read_from_device(
|
||||
TDECK_KEYBOARD_I2C_BUS_HANDLE,
|
||||
TDECK_KEYBOARD_SLAVE_ADDRESS,
|
||||
output,
|
||||
1,
|
||||
configTICK_RATE_HZ / 10
|
||||
);
|
||||
}
|
||||
|
||||
void keyboard_wait_for_response() {
|
||||
TT_LOG_I(TAG, "Waiting for keyboard response...");
|
||||
bool awake = false;
|
||||
uint8_t read_buffer = 0x00;
|
||||
do {
|
||||
awake = keyboard_i2c_read(&read_buffer) == ESP_OK;
|
||||
if (!awake) {
|
||||
tt::delay_ms(100);
|
||||
}
|
||||
} while (!awake);
|
||||
TT_LOG_I(TAG, "Keyboard responded");
|
||||
}
|
||||
|
||||
/**
|
||||
* The callback simulates press and release events, because the T-Deck
|
||||
* keyboard only publishes press events on I2C.
|
||||
* LVGL currently works without those extra release events, but they
|
||||
* are implemented for correctness and future compatibility.
|
||||
*
|
||||
* @param indev_drv
|
||||
* @param data
|
||||
*/
|
||||
static void keyboard_read_callback(TT_UNUSED lv_indev_t* indev, lv_indev_data_t* data) {
|
||||
static uint8_t last_buffer = 0x00;
|
||||
uint8_t read_buffer = 0x00;
|
||||
|
||||
// Defaults
|
||||
data->key = 0;
|
||||
data->state = LV_INDEV_STATE_RELEASED;
|
||||
|
||||
if (keyboard_i2c_read(&read_buffer) == ESP_OK) {
|
||||
if (read_buffer == 0 && read_buffer != last_buffer) {
|
||||
TT_LOG_I(TAG, "Released %d", last_buffer);
|
||||
data->key = last_buffer;
|
||||
data->state = LV_INDEV_STATE_RELEASED;
|
||||
} else if (read_buffer != 0) {
|
||||
TT_LOG_I(TAG, "Pressed %d", read_buffer);
|
||||
data->key = read_buffer;
|
||||
data->state = LV_INDEV_STATE_PRESSED;
|
||||
}
|
||||
}
|
||||
|
||||
last_buffer = read_buffer;
|
||||
}
|
||||
|
||||
Keyboard keyboard_alloc(_Nullable lv_disp_t* display) {
|
||||
auto* data = static_cast<KeyboardData*>(malloc(sizeof(KeyboardData)));
|
||||
|
||||
data->device = lv_indev_create();
|
||||
lv_indev_set_type(data->device, LV_INDEV_TYPE_KEYPAD);
|
||||
lv_indev_set_read_cb(data->device, &keyboard_read_callback);
|
||||
lv_indev_set_display(data->device, display);
|
||||
|
||||
tt::lvgl::keypad_set_indev(data->device);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
void keyboard_free(Keyboard keyboard) {
|
||||
auto* data = static_cast<KeyboardData*>(keyboard);
|
||||
lv_indev_delete(data->device);
|
||||
free(data);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "lvgl.h"
|
||||
|
||||
void keyboard_wait_for_response();
|
||||
|
||||
typedef void* Keyboard;
|
||||
|
||||
Keyboard keyboard_alloc(_Nullable lv_disp_t* display);
|
||||
void keyboard_free(Keyboard keyboard);
|
||||
@@ -0,0 +1,17 @@
|
||||
#include "lilygo_tdeck.h"
|
||||
#include "display_i.h"
|
||||
|
||||
bool tdeck_bootstrap();
|
||||
bool tdeck_init_lvgl();
|
||||
|
||||
extern const tt::hal::sdcard::SdCard tdeck_sdcard;
|
||||
|
||||
extern const tt::hal::Configuration lilygo_tdeck = {
|
||||
.bootstrap = &tdeck_bootstrap,
|
||||
.init_graphics = &tdeck_init_lvgl,
|
||||
.display = {
|
||||
.set_backlight_duty = &tdeck_backlight_set
|
||||
},
|
||||
.sdcard = &tdeck_sdcard,
|
||||
.power = nullptr
|
||||
};
|
||||
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "Hal/Configuration.h"
|
||||
|
||||
extern const tt::hal::Configuration lilygo_tdeck;
|
||||
@@ -0,0 +1,67 @@
|
||||
#include "config.h"
|
||||
#include "display_i.h"
|
||||
#include "esp_lvgl_port.h"
|
||||
#include "keyboard.h"
|
||||
#include "Log.h"
|
||||
#include "Ui/LvglSync.h"
|
||||
#include "Thread.h"
|
||||
|
||||
#define TAG "tdeck_lvgl"
|
||||
|
||||
bool tdeck_init_touch(esp_lcd_panel_io_handle_t* io_handle, esp_lcd_touch_handle_t* touch_handle);
|
||||
|
||||
bool tdeck_init_lvgl() {
|
||||
static lv_disp_t* display = nullptr;
|
||||
static esp_lcd_panel_io_handle_t touch_io_handle;
|
||||
static esp_lcd_touch_handle_t touch_handle;
|
||||
|
||||
const lvgl_port_cfg_t lvgl_cfg = {
|
||||
.task_priority = tt::THREAD_PRIORITY_RENDER,
|
||||
.task_stack = TDECK_LVGL_TASK_STACK_DEPTH,
|
||||
.task_affinity = -1, // core pinning
|
||||
.task_max_sleep_ms = 500,
|
||||
.timer_period_ms = 5
|
||||
};
|
||||
|
||||
TT_LOG_D(TAG, "LVGL port init");
|
||||
if (lvgl_port_init(&lvgl_cfg) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "LVGL port init failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Add display
|
||||
|
||||
TT_LOG_D(TAG, "Creating display");
|
||||
display = tdeck_display_init();
|
||||
if (display == nullptr) {
|
||||
TT_LOG_E(TAG, "Creating display failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Add touch
|
||||
|
||||
TT_LOG_D(TAG, "Creating touch");
|
||||
if (!tdeck_init_touch(&touch_io_handle, &touch_handle)) {
|
||||
TT_LOG_E(TAG, "Creating touch failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
const lvgl_port_touch_cfg_t touch_cfg = {
|
||||
.disp = display,
|
||||
.handle = touch_handle,
|
||||
};
|
||||
|
||||
TT_LOG_D(TAG, "Adding touch");
|
||||
lv_indev_t _Nullable* touch_indev = lvgl_port_add_touch(&touch_cfg);
|
||||
if (touch_indev == nullptr) {
|
||||
TT_LOG_E(TAG, "Adding touch failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set syncing functions
|
||||
tt::lvgl::sync_set(&lvgl_port_lock, &lvgl_port_unlock);
|
||||
|
||||
keyboard_alloc(display);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
#include "Hal/Sdcard.h"
|
||||
#include "Check.h"
|
||||
#include "Log.h"
|
||||
#include "config.h"
|
||||
|
||||
#include "esp_vfs_fat.h"
|
||||
#include "sdmmc_cmd.h"
|
||||
#include "Ui/LvglSync.h"
|
||||
|
||||
#define TAG "tdeck_sdcard"
|
||||
|
||||
typedef struct {
|
||||
const char* mount_point;
|
||||
sdmmc_card_t* card;
|
||||
} MountData;
|
||||
|
||||
/**
|
||||
* Before we can initialize the sdcard's SPI communications, we have to set all
|
||||
* other SPI pins on the board high.
|
||||
* See https://github.com/espressif/esp-idf/issues/1597
|
||||
* See https://github.com/Xinyuan-LilyGO/T-Deck/blob/master/examples/UnitTest/UnitTest.ino
|
||||
* @return success result
|
||||
*/
|
||||
static bool sdcard_init() {
|
||||
TT_LOG_D(TAG, "init");
|
||||
|
||||
gpio_config_t config = {
|
||||
.pin_bit_mask = BIT64(TDECK_SDCARD_PIN_CS) | BIT64(TDECK_RADIO_PIN_CS) | BIT64(TDECK_LCD_PIN_CS),
|
||||
.mode = GPIO_MODE_OUTPUT,
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
};
|
||||
|
||||
if (gpio_config(&config) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "GPIO init failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (gpio_set_level(TDECK_SDCARD_PIN_CS, 1) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to set board CS pin high");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (gpio_set_level(TDECK_RADIO_PIN_CS, 1) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to set radio CS pin high");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (gpio_set_level(TDECK_LCD_PIN_CS, 1) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to set TFT CS pin high");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void* _Nullable sdcard_mount(const char* mount_point) {
|
||||
TT_LOG_I(TAG, "Mounting %s", mount_point);
|
||||
|
||||
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
|
||||
.format_if_mount_failed = TDECK_SDCARD_FORMAT_ON_MOUNT_FAILED,
|
||||
.max_files = TDECK_SDCARD_MAX_OPEN_FILES,
|
||||
.allocation_unit_size = TDECK_SDCARD_ALLOC_UNIT_SIZE,
|
||||
.disk_status_check_enable = TDECK_SDCARD_STATUS_CHECK_ENABLED
|
||||
};
|
||||
|
||||
// Init without card detect (CD) and write protect (WD)
|
||||
sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
|
||||
slot_config.gpio_cs = TDECK_SDCARD_PIN_CS;
|
||||
slot_config.host_id = TDECK_SDCARD_SPI_HOST;
|
||||
|
||||
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
|
||||
// The following value is from T-Deck repo's UnitTest.ino project:
|
||||
// https://github.com/Xinyuan-LilyGO/T-Deck/blob/master/examples/UnitTest/UnitTest.ino
|
||||
// Observation: Using this automatically sets the bus to 20MHz
|
||||
host.max_freq_khz = TDECK_SDCARD_SPI_FREQUENCY;
|
||||
|
||||
sdmmc_card_t* card;
|
||||
esp_err_t ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
|
||||
|
||||
if (ret != ESP_OK) {
|
||||
if (ret == ESP_FAIL) {
|
||||
TT_LOG_E(TAG, "Mounting failed. Ensure the card is formatted with FAT.");
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Mounting failed (%s)", esp_err_to_name(ret));
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
auto* data = static_cast<MountData*>(malloc(sizeof(MountData)));
|
||||
*data = (MountData) {
|
||||
.mount_point = mount_point,
|
||||
.card = card,
|
||||
};
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
static void* sdcard_init_and_mount(const char* mount_point) {
|
||||
if (!sdcard_init()) {
|
||||
TT_LOG_E(TAG, "Failed to set SPI CS pins high. This is a pre-requisite for mounting.");
|
||||
return NULL;
|
||||
}
|
||||
auto* data = static_cast<MountData*>(sdcard_mount(mount_point));
|
||||
if (data == nullptr) {
|
||||
TT_LOG_E(TAG, "Mount failed for %s", mount_point);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
sdmmc_card_print_info(stdout, data->card);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
static void sdcard_unmount(void* context) {
|
||||
auto* data = static_cast<MountData*>(context);
|
||||
TT_LOG_I(TAG, "Unmounting %s", data->mount_point);
|
||||
|
||||
tt_assert(data != nullptr);
|
||||
if (esp_vfs_fat_sdcard_unmount(data->mount_point, data->card) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Unmount failed for %s", data->mount_point);
|
||||
}
|
||||
|
||||
free(data);
|
||||
}
|
||||
|
||||
static bool sdcard_is_mounted(void* context) {
|
||||
auto* data = static_cast<MountData*>(context);
|
||||
/**
|
||||
* The SD card and the screen are on the same SPI bus.
|
||||
* Writing and reading to the bus from 2 devices at the same time causes crashes.
|
||||
* This work-around ensures that this check is only happening when LVGL isn't rendering.
|
||||
*/
|
||||
if (tt::lvgl::lock(100)) {
|
||||
bool result = (data != nullptr) && (sdmmc_get_status(data->card) == ESP_OK);
|
||||
tt::lvgl::unlock();
|
||||
return result;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
extern const tt::hal::sdcard::SdCard tdeck_sdcard = {
|
||||
.mount = &sdcard_init_and_mount,
|
||||
.unmount = &sdcard_unmount,
|
||||
.is_mounted = &sdcard_is_mounted,
|
||||
.mount_behaviour = tt::hal::sdcard::MountBehaviourAtBoot
|
||||
};
|
||||
@@ -0,0 +1,58 @@
|
||||
#include "config.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_lcd_panel_io_interface.h"
|
||||
#include "esp_lcd_touch_gt911.h"
|
||||
#include "Log.h"
|
||||
#include "Kernel.h"
|
||||
|
||||
#define TAG "tdeck_touch"
|
||||
|
||||
bool tdeck_init_touch(esp_lcd_panel_io_handle_t* io_handle, esp_lcd_touch_handle_t* touch_handle) {
|
||||
const esp_lcd_panel_io_i2c_config_t touch_io_config = ESP_LCD_TOUCH_IO_I2C_GT911_CONFIG();
|
||||
|
||||
// TODO: Revert on new ESP-IDF version
|
||||
static_assert(ESP_IDF_VERSION == ESP_IDF_VERSION_VAL(5, 3, 1));
|
||||
esp_lcd_new_panel_io_i2c(
|
||||
(esp_lcd_i2c_bus_handle_t)TDECK_TOUCH_I2C_BUS_HANDLE,
|
||||
&touch_io_config,
|
||||
io_handle
|
||||
);
|
||||
/*
|
||||
if (
|
||||
esp_lcd_new_panel_io_i2c(
|
||||
(esp_lcd_i2c_bus_handle_t)TDECK_TOUCH_I2C_BUS_HANDLE,
|
||||
&touch_io_config,
|
||||
io_handle
|
||||
) != ESP_OK
|
||||
) {
|
||||
TT_LOG_E(TAG, "touch io i2c creation failed");
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
|
||||
esp_lcd_touch_config_t config = {
|
||||
.x_max = TDECK_TOUCH_X_MAX,
|
||||
.y_max = TDECK_TOUCH_Y_MAX,
|
||||
.rst_gpio_num = GPIO_NUM_NC,
|
||||
.int_gpio_num = TDECK_TOUCH_PIN_INT,
|
||||
.levels = {
|
||||
.reset = 0,
|
||||
.interrupt = 0,
|
||||
},
|
||||
.flags = {
|
||||
.swap_xy = 1,
|
||||
.mirror_x = 1,
|
||||
.mirror_y = 0,
|
||||
},
|
||||
.process_coordinates = nullptr,
|
||||
.interrupt_callback = nullptr,
|
||||
.user_data = nullptr
|
||||
};
|
||||
|
||||
if (esp_lcd_touch_new_i2c_gt911(*io_handle, &config, touch_handle) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "gt911 driver creation failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user