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"
+1 -1
View File
@@ -1,5 +1,5 @@
idf_component_register(
SRC_DIRS "Source"
INCLUDE_DIRS "Source"
REQUIRES Tactility M5stackShared vfs fatfs M5Unified
REQUIRES Tactility M5stackShared M5Unified
)
+45 -10
View File
@@ -1,14 +1,49 @@
#include "M5stackCore2.h"
#include "m5stack_shared.h"
extern const tt::hal::sdcard::SdCard m5stack_core2_sdcard;
#include "M5stackShared.h"
extern const tt::hal::Configuration m5stack_core2 = {
.bootstrap = &m5stack_bootstrap,
.init_graphics = &m5stack_lvgl_init,
.display = {
.set_backlight_duty = nullptr
},
.sdcard = &m5stack_core2_sdcard,
.power = &m5stack_power
.initPower = &m5stack_bootstrap,
.initLvgl = &m5stack_lvgl_init,
.sdcard = &m5stack_sdcard,
.power = &m5stack_power,
.i2c = {
// Internal
tt::hal::i2c::Configuration {
.port = I2C_NUM_0,
.initMode = tt::hal::i2c::InitByExternal,
.canReinit = false,
.hasMutableConfiguration = false,
.timeout = 1000,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_21,
.scl_io_num = GPIO_NUM_22,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master = {
.clk_speed = 400000
},
.clk_flags = 0
}
},
// External (Grove)
tt::hal::i2c::Configuration {
.port = I2C_NUM_1,
.initMode = tt::hal::i2c::InitByExternal,
.canReinit = true,
.hasMutableConfiguration = true,
.timeout = 1000,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_32,
.scl_io_num = GPIO_NUM_33,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master = {
.clk_speed = 400000
},
.clk_flags = 0
}
}
}
};
@@ -1,86 +0,0 @@
#include "Check.h"
#include "Log.h"
#include "Hal/Sdcard.h"
#include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"
#define TAG "m5stack_core2_sdcard"
#define CORE2_SDCARD_SPI_HOST SPI2_HOST
#define CORE2_SDCARD_PIN_CS GPIO_NUM_4
#define CORE2_SDCARD_SPI_FREQUENCY 800000U
#define CORE2_SDCARD_FORMAT_ON_MOUNT_FAILED false
#define CORE2_SDCARD_MAX_OPEN_FILES 4
#define CORE2_SDCARD_ALLOC_UNIT_SIZE (16 * 1024)
#define CORE2_SDCARD_STATUS_CHECK_ENABLED false
typedef struct {
const char* mount_point;
sdmmc_card_t* card;
} MountData;
static void* 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 = CORE2_SDCARD_FORMAT_ON_MOUNT_FAILED,
.max_files = CORE2_SDCARD_MAX_OPEN_FILES,
.allocation_unit_size = CORE2_SDCARD_ALLOC_UNIT_SIZE,
.disk_status_check_enable = CORE2_SDCARD_STATUS_CHECK_ENABLED
};
sdmmc_card_t* card;
// Init without card detect (CD) and write protect (WD)
sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
slot_config.gpio_cs = CORE2_SDCARD_PIN_CS;
slot_config.host_id = CORE2_SDCARD_SPI_HOST;
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
host.max_freq_khz = CORE2_SDCARD_SPI_FREQUENCY;
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 nullptr;
}
auto* data = static_cast<MountData*>(malloc(sizeof(MountData)));
*data = (MountData) {
.mount_point = mount_point,
.card = card,
};
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);
return (data != nullptr) && (sdmmc_get_status(data->card) == ESP_OK);
}
extern const tt::hal::sdcard::SdCard m5stack_core2_sdcard = {
.mount = &sdcard_mount,
.unmount = &sdcard_unmount,
.is_mounted = &sdcard_is_mounted,
.mount_behaviour = tt::hal::sdcard::MountBehaviourAnytime
};
-1
View File
@@ -1,6 +1,5 @@
idf_component_register(
SRC_DIRS "Source"
INCLUDE_DIRS "Source"
PRIV_INCLUDE_DIRS "Private"
REQUIRES Tactility M5stackShared vfs fatfs M5Unified
)
-13
View File
@@ -1,13 +0,0 @@
#pragma once
#include "driver/spi_common.h"
#include "driver/gpio.h"
// SD Card
#define CORES3_SDCARD_SPI_HOST SPI2_HOST
#define CORES3_SDCARD_PIN_CS GPIO_NUM_4
#define CORES3_SDCARD_SPI_FREQUENCY 800000U
#define CORES3_SDCARD_FORMAT_ON_MOUNT_FAILED false
#define CORES3_SDCARD_MAX_OPEN_FILES 4
#define CORES3_SDCARD_ALLOC_UNIT_SIZE (16 * 1024)
#define CORES3_SDCARD_STATUS_CHECK_ENABLED false
@@ -0,0 +1,51 @@
#include "M5stackCoreS3.h"
#include "M5stackShared.h"
extern const tt::hal::sdcard::SdCard m5stack_cores3_sdcard;
const tt::hal::Configuration m5stack_cores3 = {
.initPower = &m5stack_bootstrap,
.initLvgl = &m5stack_lvgl_init,
.sdcard = &m5stack_sdcard,
.power = &m5stack_power,
.i2c = {
// Internal
tt::hal::i2c::Configuration {
.port = I2C_NUM_0,
.initMode = tt::hal::i2c::InitByExternal,
.canReinit = false,
.hasMutableConfiguration = false,
.timeout = 1000,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_12,
.scl_io_num = GPIO_NUM_11,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master = {
.clk_speed = 400000
},
.clk_flags = 0
}
},
// External (Grove)
tt::hal::i2c::Configuration {
.port = I2C_NUM_1,
.initMode = tt::hal::i2c::InitByExternal,
.canReinit = true,
.hasMutableConfiguration = true,
.timeout = 1000,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_2,
.scl_io_num = GPIO_NUM_1,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master = {
.clk_speed = 400000
},
.clk_flags = 0
}
}
}
};
@@ -1,14 +0,0 @@
#include "m5stack_cores3.h"
#include "m5stack_shared.h"
extern const tt::hal::sdcard::SdCard m5stack_cores3_sdcard;
const tt::hal::Configuration m5stack_cores3 = {
.bootstrap = &m5stack_bootstrap,
.init_graphics = &m5stack_lvgl_init,
.display = {
.set_backlight_duty = nullptr
},
.sdcard = &m5stack_cores3_sdcard,
.power = &m5stack_power
};
@@ -1,79 +0,0 @@
#include "Hal/Sdcard.h"
#include "Check.h"
#include "Log.h"
#include "config.h"
#include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"
#define TAG "m5stack_cores3_sdcard"
typedef struct {
const char* mount_point;
sdmmc_card_t* card;
} MountData;
static void* 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 = CORES3_SDCARD_FORMAT_ON_MOUNT_FAILED,
.max_files = CORES3_SDCARD_MAX_OPEN_FILES,
.allocation_unit_size = CORES3_SDCARD_ALLOC_UNIT_SIZE,
.disk_status_check_enable = CORES3_SDCARD_STATUS_CHECK_ENABLED
};
sdmmc_card_t* card;
// Init without card detect (CD) and write protect (WD)
sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
slot_config.gpio_cs = CORES3_SDCARD_PIN_CS;
slot_config.host_id = CORES3_SDCARD_SPI_HOST;
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
host.max_freq_khz = CORES3_SDCARD_SPI_FREQUENCY;
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 nullptr;
}
auto* data = static_cast<MountData*>(malloc(sizeof(MountData)));
*data = (MountData) {
.mount_point = mount_point,
.card = card
};
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);
return (data != nullptr) && (sdmmc_get_status(data->card) == ESP_OK);
}
extern const tt::hal::sdcard::SdCard m5stack_cores3_sdcard = {
.mount = &sdcard_mount,
.unmount = &sdcard_unmount,
.is_mounted = &sdcard_is_mounted,
.mount_behaviour = tt::hal::sdcard::MountBehaviourAnytime
};
+1 -1
View File
@@ -1,5 +1,5 @@
idf_component_register(
SRC_DIRS "Source"
INCLUDE_DIRS "Source"
REQUIRES Tactility esp_lvgl_port M5Unified
REQUIRES Tactility esp_lvgl_port M5Unified vfs fatfs
)
@@ -4,7 +4,7 @@
#define TAG "cores3_touch"
static void touch_read_callback(TT_UNUSED lv_indev_t* indev, lv_indev_data_t* data) {
static void read_callback(TT_UNUSED lv_indev_t* indev, lv_indev_data_t* data) {
lgfx::touch_point_t point; // Making it static makes it unreliable
bool touched = M5.Lcd.getTouch(&point) > 0;
if (!touched) {
@@ -24,6 +24,6 @@ _Nullable lv_indev_t* m5stack_lvgl_touch() {
}
lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER);
lv_indev_set_read_cb(indev, touch_read_callback);
lv_indev_set_read_cb(indev, read_callback);
return indev;
}
@@ -3,7 +3,8 @@
#include "Hal/Power.h"
#include "Hal/Sdcard.h"
extern bool m5stack_lvgl_init();
extern bool m5stack_bootstrap();
extern bool m5stack_lvgl_init();
extern const tt::hal::Power m5stack_power;
extern const tt::hal::sdcard::SdCard m5stack_sdcard;
+38
View File
@@ -0,0 +1,38 @@
#include "Hal/Power.h"
#include "M5Unified.hpp"
/**
* M5.Power by default doesn't have a check to see if charging is enabled.
* However, it's always enabled by default after boot, so we cover that here:
*/
static bool charging_enabled = true;
static bool is_charging() {
return M5.Power.isCharging() == m5::Power_Class::is_charging;
}
static bool is_charging_enabled() {
return charging_enabled;
}
static void set_charging_enabled(bool enabled) {
charging_enabled = enabled; // Local shadow copy because M5 API doesn't provide a function for it
M5.Power.setBatteryCharge(enabled);
}
static uint8_t get_charge_level() {
uint16_t scaled = (uint16_t)M5.Power.getBatteryLevel() * 255 / 100;
return (uint8_t)scaled;
}
static int32_t get_current() {
return M5.Power.getBatteryCurrent();
}
extern const tt::hal::Power m5stack_power = {
.is_charging = &is_charging,
.is_charging_enabled = &is_charging_enabled,
.set_charging_enabled = &set_charging_enabled,
.get_charge_level = &get_charge_level,
.get_current = &get_current
};
+138
View File
@@ -0,0 +1,138 @@
#include "Check.h"
#include "Log.h"
#include "Hal/Sdcard.h"
#include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"
#define TAG "m5stack_sdcard"
#define SDCARD_SPI_HOST SPI2_HOST
#define SDCARD_PIN_CS GPIO_NUM_4
#define SDCARD_SPI_FREQUENCY 800000U
#define SDCARD_FORMAT_ON_MOUNT_FAILED false
#define SDCARD_MAX_OPEN_FILES 4
#define SDCARD_ALLOC_UNIT_SIZE (16 * 1024)
#define SDCARD_STATUS_CHECK_ENABLED false
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(GPIO_NUM_4) | BIT64(GPIO_NUM_5),
.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(GPIO_NUM_4, 1) != ESP_OK) {
TT_LOG_E(TAG, "Failed to set board CS pin high");
return false;
}
if (gpio_set_level(GPIO_NUM_5, 1) != ESP_OK) {
TT_LOG_E(TAG, "Failed to set board CS pin high");
return false;
}
return true;
}
static void* 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 = SDCARD_FORMAT_ON_MOUNT_FAILED,
.max_files = SDCARD_MAX_OPEN_FILES,
.allocation_unit_size = SDCARD_ALLOC_UNIT_SIZE,
.disk_status_check_enable = SDCARD_STATUS_CHECK_ENABLED,
.use_one_fat = false
};
sdmmc_card_t* card;
// Init without card detect (CD) and write protect (WD)
sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
slot_config.gpio_cs = SDCARD_PIN_CS;
slot_config.host_id = SDCARD_SPI_HOST;
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
host.max_freq_khz = SDCARD_SPI_FREQUENCY;
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 nullptr;
}
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);
return (data != nullptr) && (sdmmc_get_status(data->card) == ESP_OK);
}
extern const tt::hal::sdcard::SdCard m5stack_sdcard = {
.mount = &sdcard_init_and_mount,
.unmount = &sdcard_unmount,
.is_mounted = &sdcard_is_mounted,
.mount_behaviour = tt::hal::sdcard::MountBehaviourAnytime
};
@@ -1,38 +0,0 @@
#include "Hal/Power.h"
#include "M5Unified.hpp"
/**
* M5.Power by default doesn't have a check to see if charging is enabled.
* However, it's always enabled by default after boot, so we cover that here:
*/
static bool is_charging_enabled = true;
static bool power_is_charging() {
return M5.Power.isCharging() == m5::Power_Class::is_charging;
}
static bool power_is_charging_enabled() {
return is_charging_enabled;
}
static void power_set_charging_enabled(bool enabled) {
is_charging_enabled = enabled; // Local shadow copy because M5 API doesn't provide a function for it
M5.Power.setBatteryCharge(enabled);
}
static uint8_t power_get_charge_level() {
uint16_t scaled = (uint16_t)M5.Power.getBatteryLevel() * 255 / 100;
return (uint8_t)scaled;
}
static int32_t power_get_current() {
return M5.Power.getBatteryCurrent();
}
extern const tt::hal::Power m5stack_power = {
.is_charging = &power_is_charging,
.is_charging_enabled = &power_is_charging_enabled,
.set_charging_enabled = &power_set_charging_enabled,
.get_charge_level = &power_get_charge_level,
.get_current = &power_get_current
};
@@ -5,11 +5,10 @@
bool ws3t_bootstrap();
extern const tt::hal::Configuration waveshare_s3_touch = {
.bootstrap = &ws3t_bootstrap,
.init_graphics = &ws3t_init_lvgl,
.display = {
.set_backlight_duty = nullptr // TODO: This requires implementing the CH422G IO expander
},
.initPower = &ws3t_bootstrap,
.initLvgl = &ws3t_init_lvgl,
.display = { .setBacklightDuty = nullptr },
.sdcard = nullptr,
.power = nullptr
.power = nullptr,
.i2c = {}
};
+42 -4
View File
@@ -7,11 +7,49 @@ bool twodotfour_bootstrap();
extern const tt::hal::sdcard::SdCard twodotfour_sdcard;
const tt::hal::Configuration yellow_board_24inch_cap = {
.bootstrap = &twodotfour_bootstrap,
.init_graphics = &twodotfour_lvgl_init,
.initPower = &twodotfour_bootstrap,
.initLvgl = &twodotfour_lvgl_init,
.display = {
.set_backlight_duty = &twodotfour_backlight_set
.setBacklightDuty = &twodotfour_backlight_set
},
.sdcard = &twodotfour_sdcard,
.power = nullptr
.power = nullptr,
.i2c = {
tt::hal::i2c::Configuration {
.port = I2C_NUM_0,
.initMode = tt::hal::i2c::InitDisabled,
.canReinit = true,
.hasMutableConfiguration = true,
.timeout = 1000,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_NC,
.scl_io_num = GPIO_NUM_NC,
.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_NC,
.scl_io_num = GPIO_NUM_NC,
.sda_pullup_en = GPIO_PULLUP_DISABLE,
.scl_pullup_en = GPIO_PULLUP_DISABLE,
.master = {
.clk_speed = 400000
},
.clk_flags = 0
}
}
}
};