I2C Implementation (#84)

This commit is contained in:
Ken Van Hoeylandt
2024-11-24 18:04:57 +01:00
committed by GitHub
parent 881c8517bf
commit d8731eaa17
51 changed files with 936 additions and 433 deletions
+55
View File
@@ -0,0 +1,55 @@
#include "TactilityCore.h"
#include "config.h"
#include "display.h"
#include "keyboard.h"
#include <driver/spi_common.h>
#define TAG "tdeck"
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,
};
return spi_bus_initialize(TDECK_SPI_HOST, &bus_config, SPI_DMA_CH_AUTO) == ESP_OK;
}
bool tdeck_init_hardware() {
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;
}
// We wait for the keyboard peripheral to be booted up
keyboard_wait_for_response();
return true;
}
+46
View File
@@ -0,0 +1,46 @@
#include "config.h"
#include "TactilityCore.h"
#define TAG "tdeck"
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;
}
bool tdeck_init_power() {
ESP_LOGI(TAG, "Power on");
if (!tdeck_power_on()) {
TT_LOG_E(TAG, "Power on failed");
return false;
}
/**
* 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);
return true;
}
-101
View File
@@ -1,101 +0,0 @@
#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;
}
+10 -3
View File
@@ -15,7 +15,8 @@ bool tdeck_backlight_init() {
.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
.clk_cfg = LEDC_AUTO_CLK,
.deconfigure = false
};
if (ledc_timer_config(&ledc_timer) != ESP_OK) {
@@ -34,7 +35,10 @@ void tdeck_backlight_set(uint8_t duty) {
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = TDECK_LCD_BACKLIGHT_LEDC_TIMER,
.duty = duty,
.hpoint = 0
.hpoint = 0,
.flags = {
.output_invert = 0
}
};
// Setting the config in the timer init and then calling ledc_set_duty() doesn't work when
@@ -56,7 +60,9 @@ lv_display_t* tdeck_display_init() {
.lcd_cmd_bits = 8,
.lcd_param_bits = 8,
.flags = {
.dc_high_on_cmd = 0,
.dc_low_on_data = 0,
.dc_low_on_param = 0,
.octal_mode = 0,
.quad_mode = 0,
.sio_mode = 1,
@@ -123,6 +129,7 @@ lv_display_t* tdeck_display_init() {
.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
.trans_size = 0,
.hres = TDECK_LCD_HORIZONTAL_RESOLUTION,
.vres = TDECK_LCD_VERTICAL_RESOLUTION,
.monochrome = false,
@@ -136,7 +143,7 @@ lv_display_t* tdeck_display_init() {
.buff_spiram = true,
.sw_rotate = false,
.swap_bytes = true
}
},
};
return lvgl_port_add_disp(&disp_cfg);
+5 -10
View File
@@ -3,6 +3,7 @@
#include "lvgl.h"
#include "TactilityCore.h"
#include "Ui/LvglKeypad.h"
#include "Hal/I2c/I2c.h"
#include <driver/i2c.h>
#define TAG "tdeck_keyboard"
@@ -11,14 +12,8 @@ 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
);
static inline bool keyboard_i2c_read(uint8_t* output) {
return tt::hal::i2c::masterRead(TDECK_KEYBOARD_I2C_BUS_HANDLE, TDECK_KEYBOARD_SLAVE_ADDRESS, output, 1);
}
void keyboard_wait_for_response() {
@@ -26,7 +21,7 @@ void keyboard_wait_for_response() {
bool awake = false;
uint8_t read_buffer = 0x00;
do {
awake = keyboard_i2c_read(&read_buffer) == ESP_OK;
awake = keyboard_i2c_read(&read_buffer);
if (!awake) {
tt::delay_ms(100);
}
@@ -51,7 +46,7 @@ static void keyboard_read_callback(TT_UNUSED lv_indev_t* indev, lv_indev_data_t*
data->key = 0;
data->state = LV_INDEV_STATE_RELEASED;
if (keyboard_i2c_read(&read_buffer) == ESP_OK) {
if (keyboard_i2c_read(&read_buffer)) {
if (read_buffer == 0 && read_buffer != last_buffer) {
TT_LOG_I(TAG, "Released %d", last_buffer);
data->key = last_buffer;
+46 -6
View File
@@ -1,17 +1,57 @@
#include "lilygo_tdeck.h"
#include "display_i.h"
#include "display.h"
bool tdeck_bootstrap();
bool tdeck_init_power();
bool tdeck_init_hardware();
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,
.initPower = tdeck_init_power,
.initHardware = tdeck_init_hardware,
.initLvgl = &tdeck_init_lvgl,
.display = {
.set_backlight_duty = &tdeck_backlight_set
.setBacklightDuty = &tdeck_backlight_set
},
.sdcard = &tdeck_sdcard,
.power = nullptr
.power = nullptr,
.i2c = {
tt::hal::i2c::Configuration {
.port = I2C_NUM_0,
.initMode = tt::hal::i2c::InitByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.timeout = 1000,
.config = (i2c_config_t) {
.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
},
.clk_flags = 0
}
},
tt::hal::i2c::Configuration {
.port = I2C_NUM_1,
.initMode = tt::hal::i2c::InitDisabled,
.canReinit = true,
.hasMutableConfiguration = true,
.timeout = 1000,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_43,
.scl_io_num = GPIO_NUM_44,
.sda_pullup_en = GPIO_PULLUP_DISABLE,
.scl_pullup_en = GPIO_PULLUP_DISABLE,
.master = {
.clk_speed = 400000
},
.clk_flags = 0
}
}
}
};
+4 -4
View File
@@ -1,10 +1,10 @@
#include "Log.h"
#include "Thread.h"
#include "Ui/LvglSync.h"
#include "config.h"
#include "display_i.h"
#include "display.h"
#include "esp_lvgl_port.h"
#include "keyboard.h"
#include "Log.h"
#include "Ui/LvglSync.h"
#include "Thread.h"
#define TAG "tdeck_lvgl"