diff --git a/AppEsp32/Source/Boards.h b/AppEsp32/Source/Boards.h index b5fb1386..f08db744 100644 --- a/AppEsp32/Source/Boards.h +++ b/AppEsp32/Source/Boards.h @@ -13,7 +13,7 @@ #include "M5stackCore2.h" #define TT_BOARD_HARDWARE &m5stack_core2 #elif defined(CONFIG_TT_BOARD_M5STACK_CORES3) -#include "m5stack_cores3.h" +#include "M5stackCoreS3.h" #define TT_BOARD_HARDWARE &m5stack_cores3 #elif defined(CONFIG_TT_BOARD_WAVESHARE_S3_TOUCH) #include "waveshare_s3_touch.h" diff --git a/AppSim/Source/hardware_config.cpp b/AppSim/Source/hardware_config.cpp index 8f9f4558..4361f990 100644 --- a/AppSim/Source/hardware_config.cpp +++ b/AppSim/Source/hardware_config.cpp @@ -24,11 +24,11 @@ TT_UNUSED static void lvgl_deinit() { } extern const tt::hal::Configuration sim_hardware = { - .bootstrap = nullptr, - .init_graphics = &lvgl_init, + .initLvgl = &lvgl_init, .display = { - .set_backlight_duty = nullptr, + .setBacklightDuty = nullptr, }, .sdcard = nullptr, - .power = &power + .power = &power, + .i2c = {} }; diff --git a/Boards/LilygoTdeck/InitHardware.cpp b/Boards/LilygoTdeck/InitHardware.cpp new file mode 100644 index 00000000..c008fc85 --- /dev/null +++ b/Boards/LilygoTdeck/InitHardware.cpp @@ -0,0 +1,55 @@ +#include "TactilityCore.h" +#include "config.h" +#include "display.h" +#include "keyboard.h" +#include + +#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; +} \ No newline at end of file diff --git a/Boards/LilygoTdeck/InitPower.cpp b/Boards/LilygoTdeck/InitPower.cpp new file mode 100644 index 00000000..ee5bbd65 --- /dev/null +++ b/Boards/LilygoTdeck/InitPower.cpp @@ -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; +} diff --git a/Boards/LilygoTdeck/bootstrap.cpp b/Boards/LilygoTdeck/bootstrap.cpp deleted file mode 100644 index 8d08e4d9..00000000 --- a/Boards/LilygoTdeck/bootstrap.cpp +++ /dev/null @@ -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; -} diff --git a/Boards/LilygoTdeck/display.cpp b/Boards/LilygoTdeck/display.cpp index 2c9e4eaa..8141ac05 100644 --- a/Boards/LilygoTdeck/display.cpp +++ b/Boards/LilygoTdeck/display.cpp @@ -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); diff --git a/Boards/LilygoTdeck/display_i.h b/Boards/LilygoTdeck/display.h similarity index 100% rename from Boards/LilygoTdeck/display_i.h rename to Boards/LilygoTdeck/display.h diff --git a/Boards/LilygoTdeck/keyboard.cpp b/Boards/LilygoTdeck/keyboard.cpp index 1c15494e..609f7bf1 100644 --- a/Boards/LilygoTdeck/keyboard.cpp +++ b/Boards/LilygoTdeck/keyboard.cpp @@ -3,6 +3,7 @@ #include "lvgl.h" #include "TactilityCore.h" #include "Ui/LvglKeypad.h" +#include "Hal/I2c/I2c.h" #include #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; diff --git a/Boards/LilygoTdeck/lilygo_tdeck.cpp b/Boards/LilygoTdeck/lilygo_tdeck.cpp index e94652f9..da389c4c 100644 --- a/Boards/LilygoTdeck/lilygo_tdeck.cpp +++ b/Boards/LilygoTdeck/lilygo_tdeck.cpp @@ -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 + } + } + } }; diff --git a/Boards/LilygoTdeck/lvgl.cpp b/Boards/LilygoTdeck/lvgl.cpp index 2e168da0..4fdc446f 100644 --- a/Boards/LilygoTdeck/lvgl.cpp +++ b/Boards/LilygoTdeck/lvgl.cpp @@ -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" diff --git a/Boards/M5stackCore2/CMakeLists.txt b/Boards/M5stackCore2/CMakeLists.txt index 7fa44bd8..bcae790d 100644 --- a/Boards/M5stackCore2/CMakeLists.txt +++ b/Boards/M5stackCore2/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( SRC_DIRS "Source" INCLUDE_DIRS "Source" - REQUIRES Tactility M5stackShared vfs fatfs M5Unified + REQUIRES Tactility M5stackShared M5Unified ) diff --git a/Boards/M5stackCore2/Source/M5stackCore2.cpp b/Boards/M5stackCore2/Source/M5stackCore2.cpp index 0c057681..61aba450 100644 --- a/Boards/M5stackCore2/Source/M5stackCore2.cpp +++ b/Boards/M5stackCore2/Source/M5stackCore2.cpp @@ -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 + } + } + } }; diff --git a/Boards/M5stackCore2/Source/M5stackCore2Sdcard.cpp b/Boards/M5stackCore2/Source/M5stackCore2Sdcard.cpp deleted file mode 100644 index afe165fe..00000000 --- a/Boards/M5stackCore2/Source/M5stackCore2Sdcard.cpp +++ /dev/null @@ -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(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(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(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 -}; diff --git a/Boards/M5stackCoreS3/CMakeLists.txt b/Boards/M5stackCoreS3/CMakeLists.txt index 6dfb2371..7fa44bd8 100644 --- a/Boards/M5stackCoreS3/CMakeLists.txt +++ b/Boards/M5stackCoreS3/CMakeLists.txt @@ -1,6 +1,5 @@ idf_component_register( SRC_DIRS "Source" INCLUDE_DIRS "Source" - PRIV_INCLUDE_DIRS "Private" REQUIRES Tactility M5stackShared vfs fatfs M5Unified ) diff --git a/Boards/M5stackCoreS3/Private/config.h b/Boards/M5stackCoreS3/Private/config.h deleted file mode 100644 index 930d7862..00000000 --- a/Boards/M5stackCoreS3/Private/config.h +++ /dev/null @@ -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 diff --git a/Boards/M5stackCoreS3/Source/M5stackCoreS3.cpp b/Boards/M5stackCoreS3/Source/M5stackCoreS3.cpp new file mode 100644 index 00000000..1dd40ab9 --- /dev/null +++ b/Boards/M5stackCoreS3/Source/M5stackCoreS3.cpp @@ -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 + } + } + } +}; diff --git a/Boards/M5stackCoreS3/Source/m5stack_cores3.h b/Boards/M5stackCoreS3/Source/M5stackCoreS3.h similarity index 100% rename from Boards/M5stackCoreS3/Source/m5stack_cores3.h rename to Boards/M5stackCoreS3/Source/M5stackCoreS3.h diff --git a/Boards/M5stackCoreS3/Source/m5stack_cores3.cpp b/Boards/M5stackCoreS3/Source/m5stack_cores3.cpp deleted file mode 100644 index 0923f1e2..00000000 --- a/Boards/M5stackCoreS3/Source/m5stack_cores3.cpp +++ /dev/null @@ -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 -}; diff --git a/Boards/M5stackCoreS3/Source/m5stack_cores3_sdcard.cpp b/Boards/M5stackCoreS3/Source/m5stack_cores3_sdcard.cpp deleted file mode 100644 index 93957150..00000000 --- a/Boards/M5stackCoreS3/Source/m5stack_cores3_sdcard.cpp +++ /dev/null @@ -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(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(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(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 -}; diff --git a/Boards/M5stackShared/CMakeLists.txt b/Boards/M5stackShared/CMakeLists.txt index ae41f2a6..95b47957 100644 --- a/Boards/M5stackShared/CMakeLists.txt +++ b/Boards/M5stackShared/CMakeLists.txt @@ -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 ) diff --git a/Boards/M5stackShared/Source/m5stack_bootstrap.cpp b/Boards/M5stackShared/Source/Bootstrap.cpp similarity index 100% rename from Boards/M5stackShared/Source/m5stack_bootstrap.cpp rename to Boards/M5stackShared/Source/Bootstrap.cpp diff --git a/Boards/M5stackShared/Source/m5stack_lvgl.cpp b/Boards/M5stackShared/Source/Lvgl.cpp similarity index 100% rename from Boards/M5stackShared/Source/m5stack_lvgl.cpp rename to Boards/M5stackShared/Source/Lvgl.cpp diff --git a/Boards/M5stackShared/Source/m5stack_lvgl_display.cpp b/Boards/M5stackShared/Source/LvglDisplay.cpp similarity index 100% rename from Boards/M5stackShared/Source/m5stack_lvgl_display.cpp rename to Boards/M5stackShared/Source/LvglDisplay.cpp diff --git a/Boards/M5stackShared/Source/m5stack_lvgl_touch.cpp b/Boards/M5stackShared/Source/LvglTouch.cpp similarity index 82% rename from Boards/M5stackShared/Source/m5stack_lvgl_touch.cpp rename to Boards/M5stackShared/Source/LvglTouch.cpp index 0c3696ae..c0c3d24e 100644 --- a/Boards/M5stackShared/Source/m5stack_lvgl_touch.cpp +++ b/Boards/M5stackShared/Source/LvglTouch.cpp @@ -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; } diff --git a/Boards/M5stackShared/Source/m5stack_shared.h b/Boards/M5stackShared/Source/M5stackShared.h similarity index 76% rename from Boards/M5stackShared/Source/m5stack_shared.h rename to Boards/M5stackShared/Source/M5stackShared.h index b964b3e0..108d13d6 100644 --- a/Boards/M5stackShared/Source/m5stack_shared.h +++ b/Boards/M5stackShared/Source/M5stackShared.h @@ -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; diff --git a/Boards/M5stackShared/Source/Power.cpp b/Boards/M5stackShared/Source/Power.cpp new file mode 100644 index 00000000..ee29dc49 --- /dev/null +++ b/Boards/M5stackShared/Source/Power.cpp @@ -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 +}; diff --git a/Boards/M5stackShared/Source/Sdcard.cpp b/Boards/M5stackShared/Source/Sdcard.cpp new file mode 100644 index 00000000..73717e3e --- /dev/null +++ b/Boards/M5stackShared/Source/Sdcard.cpp @@ -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(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(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(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(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 +}; diff --git a/Boards/M5stackShared/Source/m5stack_power.cpp b/Boards/M5stackShared/Source/m5stack_power.cpp deleted file mode 100644 index 54c5b18c..00000000 --- a/Boards/M5stackShared/Source/m5stack_power.cpp +++ /dev/null @@ -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 -}; diff --git a/Boards/WaveshareS3Touch/waveshare_s3_touch.cpp b/Boards/WaveshareS3Touch/waveshare_s3_touch.cpp index c1bf35c5..dfd272a1 100644 --- a/Boards/WaveshareS3Touch/waveshare_s3_touch.cpp +++ b/Boards/WaveshareS3Touch/waveshare_s3_touch.cpp @@ -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 = {} }; diff --git a/Boards/YellowBoard/yellow_board.cpp b/Boards/YellowBoard/yellow_board.cpp index 6b1494b8..3d01276d 100644 --- a/Boards/YellowBoard/yellow_board.cpp +++ b/Boards/YellowBoard/yellow_board.cpp @@ -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 + } + } + } }; diff --git a/Documentation/ideas.md b/Documentation/ideas.md index 4643eeea..2a75eadf 100644 --- a/Documentation/ideas.md +++ b/Documentation/ideas.md @@ -18,6 +18,7 @@ - Publish firmwares with upload tool - De-duplicate WiFi SSIDs. - Move libs/M5 projects to boards/M5Shared +- Refactor hardware configuration init methods to return esp_err_t instead of bool # Core Ideas - Make a HAL? It would mainly be there to support PC development. It's a lot of effort for supporting what's effectively a dev-only feature. diff --git a/README.md b/README.md index 15310495..304dfbee 100644 --- a/README.md +++ b/README.md @@ -131,9 +131,9 @@ Directories explained: - `AppEsp32`: The ESP32 application example - `AppSim`: The PC/simulator application example - `Boards`: Contains ESP modules with drivers -- `Tactility`: The main application platform code ([src/](Tactility/Source)) +- `Tactility`: The main application platform code - `TactilityHeadless`: Service framework and default services. -- `TactilityCore`: Core functionality regarding threads, stdlib, etc. ([src/](TactilityCore/Source)) +- `TactilityCore`: Core functionality regarding threads, stdlib, etc. - `Libraries`: Contains a mix of regular libraries and ESP modules Until there is proper documentation, here are some pointers: diff --git a/Tactility/Source/Apps/Display/Display.cpp b/Tactility/Source/Apps/Display/Display.cpp index c80e1bf3..438b1c4a 100644 --- a/Tactility/Source/Apps/Display/Display.cpp +++ b/Tactility/Source/Apps/Display/Display.cpp @@ -15,7 +15,7 @@ static uint8_t backlight_duty = 255; static void slider_event_cb(lv_event_t* event) { auto* slider = static_cast(lv_event_get_target(event)); const Configuration* config = get_config(); - hal::SetBacklightDuty set_backlight_duty = config->hardware->display.set_backlight_duty; + hal::SetBacklightDuty set_backlight_duty = config->hardware->display.setBacklightDuty; if (set_backlight_duty != nullptr) { int32_t slider_value = lv_slider_get_value(slider); @@ -90,7 +90,7 @@ static void app_show(App app, lv_obj_t* parent) { lv_obj_set_pos(brightness_slider, 0, 30); const Configuration* config = get_config(); - hal::SetBacklightDuty set_backlight_duty = config->hardware->display.set_backlight_duty; + hal::SetBacklightDuty set_backlight_duty = config->hardware->display.setBacklightDuty; if (set_backlight_duty == nullptr) { lv_slider_set_value(brightness_slider, 255, LV_ANIM_OFF); lv_obj_add_state(brightness_slider, LV_STATE_DISABLED); diff --git a/Tactility/Source/LvglInit.cpp b/Tactility/Source/LvglInit.cpp index b57f2df5..7c7afba0 100644 --- a/Tactility/Source/LvglInit.cpp +++ b/Tactility/Source/LvglInit.cpp @@ -5,7 +5,7 @@ namespace tt { void lvgl_init(const hal::Configuration* config) { - hal::SetBacklightDuty set_backlight_duty = config->display.set_backlight_duty; + hal::SetBacklightDuty set_backlight_duty = config->display.setBacklightDuty; if (set_backlight_duty != nullptr) { int32_t backlight_duty = app::settings::display::preferences_get_backlight_duty(); set_backlight_duty(backlight_duty); diff --git a/Tactility/Source/Tactility.cpp b/Tactility/Source/Tactility.cpp index f41f4863..1c6348e4 100644 --- a/Tactility/Source/Tactility.cpp +++ b/Tactility/Source/Tactility.cpp @@ -127,7 +127,7 @@ void init(const Configuration* config) { // Assign early so starting services can use it config_instance = config; - headless_init(config->hardware); + init(*config->hardware); lvgl_init(config->hardware); diff --git a/TactilityCore/Source/Check.h b/TactilityCore/Source/Check.h index d9cc5fb2..f6ebd4a0 100644 --- a/TactilityCore/Source/Check.h +++ b/TactilityCore/Source/Check.h @@ -53,11 +53,7 @@ TT_NORETURN void tt_crash_implementation(); * @param optional message (const char*) */ -#ifdef NDEBUG -#define tt_check(x, ...) if (!(x)) { TT_LOG_E("check", "check failed: %s", #x); } -#else -#define tt_check(x, ...) assert(x) -#endif +#define tt_check(x, ...) if (!(x)) { TT_LOG_E("check", "Failed: %s", #x); }; /** Only in debug build: Assert condition and crash if assert failed */ #ifdef TT_DEBUG diff --git a/TactilityCore/Source/Log.h b/TactilityCore/Source/Log.h index 3d2bd3d6..84eb23ee 100644 --- a/TactilityCore/Source/Log.h +++ b/TactilityCore/Source/Log.h @@ -17,7 +17,7 @@ ESP_LOGI(tag, format, ##__VA_ARGS__) #define TT_LOG_D(tag, format, ...) \ ESP_LOGD(tag, format, ##__VA_ARGS__) -#define TT_LOG_T(tag, format, ...) \ +#define TT_LOG_V(tag, format, ...) \ ESP_LOGV(tag, format, ##__VA_ARGS__) #else diff --git a/TactilityCore/Source/Mutex.cpp b/TactilityCore/Source/Mutex.cpp index 2ba00723..fc944fc5 100644 --- a/TactilityCore/Source/Mutex.cpp +++ b/TactilityCore/Source/Mutex.cpp @@ -36,7 +36,6 @@ Mutex::Mutex(MutexType type) : type(type) { } tt_check(semaphore != nullptr); - } Mutex::~Mutex() { diff --git a/TactilityCore/Source/Mutex.h b/TactilityCore/Source/Mutex.h index 448f271d..c952b9a3 100644 --- a/TactilityCore/Source/Mutex.h +++ b/TactilityCore/Source/Mutex.h @@ -27,7 +27,7 @@ private: SemaphoreHandle_t semaphore; MutexType type; public: - Mutex(MutexType type); + explicit Mutex(MutexType type = MutexTypeNormal); ~Mutex(); TtStatus acquire(uint32_t timeout) const; diff --git a/TactilityHeadless/CMakeLists.txt b/TactilityHeadless/CMakeLists.txt index 644f9cfe..87a1f975 100644 --- a/TactilityHeadless/CMakeLists.txt +++ b/TactilityHeadless/CMakeLists.txt @@ -10,7 +10,7 @@ if (DEFINED ENV{ESP_IDF_VERSION}) SRCS ${SOURCE_FILES} INCLUDE_DIRS "Source/" PRIV_INCLUDE_DIRS "Private/" - REQUIRES esp_wifi nvs_flash spiffs + REQUIRES esp_wifi nvs_flash spiffs driver ) set(ASSETS_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../Data/assets") diff --git a/TactilityHeadless/Private/Hal/Hal_i.h b/TactilityHeadless/Private/Hal/Hal_i.h index 19791151..683a198d 100644 --- a/TactilityHeadless/Private/Hal/Hal_i.h +++ b/TactilityHeadless/Private/Hal/Hal_i.h @@ -4,6 +4,6 @@ namespace tt::hal { -void init(const Configuration* configuration); +void init(const Configuration& configuration); } // namespace diff --git a/TactilityHeadless/Source/Hal/Configuration.h b/TactilityHeadless/Source/Hal/Configuration.h index 4fc44875..7d89746d 100644 --- a/TactilityHeadless/Source/Hal/Configuration.h +++ b/TactilityHeadless/Source/Hal/Configuration.h @@ -1,36 +1,42 @@ #pragma once -#include "TactilityCore.h" -#include "Sdcard.h" #include "Power.h" +#include "Sdcard.h" +#include "I2c/I2c.h" +#include "TactilityCore.h" namespace tt::hal { -typedef bool (*Bootstrap)(); -typedef bool (*InitGraphics)(); +typedef bool (*InitPower)(); +typedef bool (*InitHardware)(); +typedef bool (*InitLvgl)(); typedef void (*SetBacklightDuty)(uint8_t); typedef struct { /** Set backlight duty */ - SetBacklightDuty set_backlight_duty; + _Nullable SetBacklightDuty setBacklightDuty; } Display; typedef struct { /** - * Optional bootstrapping method (e.g. to turn peripherals on) - * This is called after Tactility core init and before any other inits in the HardwareConfig. - * */ - const Bootstrap _Nullable bootstrap; - - /** - * Initializes LVGL with all relevant hardware. - * This includes the display and optional pointer devices (such as touch) or a keyboard. + * Called before I2C/SPI/etc is initialized. + * Used for powering on the peripherals manually. */ - const InitGraphics init_graphics; + const InitPower _Nullable initPower = nullptr; /** - * An interface for display features such as setting the backlight. - * This does nothing when a display isn't present. + * Called after I2C/SPI/etc is initialized. + * This can be used to communicate with built-in peripherals such as an I2C keyboard. + */ + const InitHardware _Nullable initHardware = nullptr; + + /** + * Create and initialize all LVGL devices. (e.g. display, touch, keyboard) + */ + const InitLvgl initLvgl; + + /** + * Display HAL functionality. */ const Display display; @@ -43,6 +49,11 @@ typedef struct { * An optional power interface for battery or other power delivery. */ const Power* _Nullable power; + + /** + * A list of i2c devices (can be empty, but preferably accurately represents the device capabilities) + */ + const std::vector i2c; } Configuration; } // namespace diff --git a/TactilityHeadless/Source/Hal/Hal.cpp b/TactilityHeadless/Source/Hal/Hal.cpp index e9f42f5a..5b424306 100644 --- a/TactilityHeadless/Source/Hal/Hal.cpp +++ b/TactilityHeadless/Source/Hal/Hal.cpp @@ -1,22 +1,31 @@ #include "Hal/Hal_i.h" +#include "Hal/I2c/I2c.h" #define TAG "hardware" namespace tt::hal { -void init(const Configuration* configuration) { - if (configuration->bootstrap != nullptr) { - TT_LOG_I(TAG, "Bootstrapping"); - tt_check(configuration->bootstrap(), "bootstrap failed"); +void init(const Configuration& configuration) { + if (configuration.initPower != nullptr) { + TT_LOG_I(TAG, "Init power"); + tt_check(configuration.initPower(), "Init power failed"); } - if (configuration->sdcard != nullptr) { + tt_check(i2c::init(configuration.i2c), "I2C init failed"); + if (configuration.initHardware != nullptr) { + TT_LOG_I(TAG, "Init hardware"); + tt_check(configuration.initHardware(), "Hardware init failed"); + } + + if (configuration.sdcard != nullptr) { TT_LOG_I(TAG, "Mounting sdcard"); - sdcard::mount(configuration->sdcard); + if (!sdcard::mount(configuration.sdcard)) { + TT_LOG_W(TAG, "SD card mount failed (init can continue)"); + } } - tt_check(configuration->init_graphics, "Graphics init not set"); - tt_check(configuration->init_graphics(), "Graphics init failed"); + tt_check(configuration.initLvgl, "Graphics init not set"); + tt_check(configuration.initLvgl(), "Graphics init failed"); } } // namespace diff --git a/TactilityHeadless/Source/Hal/I2c/I2c.cpp b/TactilityHeadless/Source/Hal/I2c/I2c.cpp new file mode 100644 index 00000000..16362eb4 --- /dev/null +++ b/TactilityHeadless/Source/Hal/I2c/I2c.cpp @@ -0,0 +1,191 @@ +#ifdef ESP_TARGET + +#include "I2c.h" +#include "Log.h" +#include "Mutex.h" + +#include + +namespace tt::hal::i2c { + +typedef struct Data { + Mutex mutex; + bool isConfigured = false; + bool isStarted = false; + Configuration configuration; +} Data; + +static Data dataArray[I2C_NUM_MAX]; + +#define TAG "i2c" + +void printInfo(const Data& data) { + TT_LOG_V(TAG, "I2C info for port %d", data.configuration.port); + TT_LOG_V(TAG, " isStarted: %d", data.isStarted); + TT_LOG_V(TAG, " isConfigured: %d", data.isConfigured); + TT_LOG_V(TAG, " initMode: %d", data.configuration.initMode); + TT_LOG_V(TAG, " canReinit: %d", data.configuration.canReinit); + TT_LOG_V(TAG, " hasMutableConfiguration: %d", data.configuration.hasMutableConfiguration); + TT_LOG_V(TAG, " SDA pin: %d", data.configuration.config.sda_io_num); + TT_LOG_V(TAG, " SCL pin: %d", data.configuration.config.scl_io_num); +} + +bool init(const std::vector& configurations) { + TT_LOG_I(TAG, "Init"); + for (const auto& configuration: configurations) { + if (configuration.config.mode != I2C_MODE_MASTER) { + TT_LOG_E(TAG, "Currently only master mode is supported"); + return false; + } + Data& data = dataArray[configuration.port]; + data.configuration = configuration; + data.isConfigured = true; + } + + for (const auto& config: configurations) { + printInfo(dataArray[config.port]); + if (config.initMode == InitByTactility) { + if (!start(config.port)) { + return false; + } + } else if (config.initMode == InitByExternal) { + dataArray[config.port].isStarted = true; + } + } + + return true; +} + +static bool configure_locked(i2c_port_t port, const i2c_config_t& configuration) { + Data& data = dataArray[port]; + if (data.isStarted) { + TT_LOG_E(TAG, "(%d) Cannot reconfigure while interface is started", port); + return ESP_ERR_INVALID_STATE; + } else if (!data.configuration.hasMutableConfiguration) { + TT_LOG_E(TAG, "(%d) Mutation not allowed by original configuration", port); + return ESP_ERR_NOT_ALLOWED; + } else { + data.configuration.config = configuration; + return ESP_OK; + } +} + +bool configure(i2c_port_t port, const i2c_config_t& configuration) { + lock(port); + bool result = configure_locked(port, configuration); + unlock(port); + return result; +} + +static bool startLocked(i2c_port_t port) { + Data& data = dataArray[port]; + printInfo(data); + Configuration& config = data.configuration; + + if (data.isStarted) { + TT_LOG_E(TAG, "(%d) Starting: Already started", port); + return false; + } + + if (!data.isConfigured) { + TT_LOG_E(TAG, "(%d) Starting: Not configured", port); + return false; + } + + esp_err_t result = i2c_param_config(port, &config.config); + if (result != ESP_OK) { + TT_LOG_E(TAG, "(%d) Starting: Failed to configure: %s", port, esp_err_to_name(result)); + return false; + } + + result = i2c_driver_install(port, config.config.mode, 0, 0, 0); + if (result != ESP_OK) { + TT_LOG_E(TAG, "(%d) Starting: Failed to install driver: %s", port, esp_err_to_name(result)); + return false; + } else { + data.isStarted = true; + } + + TT_LOG_I(TAG, "(%d) Started", port); + return true; +} + +bool start(i2c_port_t port) { + lock(port); + bool result = startLocked(port); + unlock(port); + return result; +} + +static bool stopLocked(i2c_port_t port) { + Data& data = dataArray[port]; + Configuration& config = data.configuration; + + if (!config.canReinit) { + TT_LOG_E(TAG, "(%d) Stopping: Not allowed to re-init", port); + return false; + } + + if (!data.isStarted) { + TT_LOG_E(TAG, "(%d) Stopping: Not started", port); + return false; + } + + esp_err_t result = i2c_driver_delete(port); + if (result != ESP_OK) { + TT_LOG_E(TAG, "(%d) Stopping: Failed to delete driver: %s", port, esp_err_to_name(result)); + return false; + } else { + data.isStarted = false; + } + + TT_LOG_I(TAG, "(%d) Stopped", port); + return true; +} + +bool stop(i2c_port_t port) { + lock(port); + bool result = stopLocked(port); + unlock(port); + return result; +} + +bool isStarted(i2c_port_t port) { + lock(port); + bool started = dataArray[port].isStarted; + unlock(port); + return started; +} + +bool masterRead(i2c_port_t port, uint8_t address, uint8_t* data, size_t dataSize) { + lock(port); + esp_err_t result = i2c_master_read_from_device(port, address, data, dataSize, dataArray[port].configuration.timeout); + unlock(port); + return result == ESP_OK; +} + +bool masterWrite(i2c_port_t port, uint16_t address, const uint8_t* data, uint16_t dataSize) { + lock(port); + esp_err_t result = i2c_master_write_to_device(port, address, data, dataSize, dataArray[port].configuration.timeout); + unlock(port); + return result == ESP_OK; +} + +bool masterWriteRead(i2c_port_t port, uint8_t address, const uint8_t* writeData, size_t writeDataSize, uint8_t* readData, size_t readDataSize) { + lock(port); + esp_err_t result = i2c_master_write_read_device(port, address, writeData, writeDataSize, readData, readDataSize, dataArray[port].configuration.timeout); + unlock(port); + return result == ESP_OK; +} + +TtStatus lock(i2c_port_t port, uint32_t timeout) { + return dataArray[port].mutex.acquire(timeout); +} + +TtStatus unlock(i2c_port_t port) { + return dataArray[port].mutex.release(); +} + +} // namespace + +#endif \ No newline at end of file diff --git a/TactilityHeadless/Source/Hal/I2c/I2c.h b/TactilityHeadless/Source/Hal/I2c/I2c.h new file mode 100644 index 00000000..87f0bb34 --- /dev/null +++ b/TactilityHeadless/Source/Hal/I2c/I2c.h @@ -0,0 +1,41 @@ +#pragma once + +#include "I2cCompat.h" +#include +#include + +namespace tt::hal::i2c { + +typedef enum { + InitByTactility, // Tactility will initialize it in the correct bootup phase + InitByExternal, // The device is already initialized and Tactility should assume it works + InitDisabled // Not initialized by default +} InitMode; + +typedef struct { + /** The port to operate on */ + i2c_port_t port; + /** Whether this bus should be initialized when device starts up */ + InitMode initMode; + /** Whether this bus can stopped and re-started. */ + bool canReinit; + /** Whether configuration can be changed. */ + bool hasMutableConfiguration; + /** Read/write timeout (not related to mutex locking mechanism) */ + TickType_t timeout; + /** Configuration that must be valid when initAtBoot is set to true. */ + i2c_config_t config; +} Configuration; + +bool init(const std::vector& configurations); + +bool start(i2c_port_t port); +bool stop(i2c_port_t port); +bool isStarted(i2c_port_t port); +bool masterRead(i2c_port_t port, uint8_t address, uint8_t* data, size_t dataSize); +bool masterWrite(i2c_port_t port, uint16_t address, const uint8_t* data, uint16_t dataSize); +bool masterWriteRead(i2c_port_t port, uint8_t address, const uint8_t* writeData, size_t writeDataSize, uint8_t* readData, size_t readDataSize); +TtStatus lock(i2c_port_t port, uint32_t timeout = UINT_MAX); +TtStatus unlock(i2c_port_t port); + +} // namespace diff --git a/TactilityHeadless/Source/Hal/I2c/I2cCompat.h b/TactilityHeadless/Source/Hal/I2c/I2cCompat.h new file mode 100644 index 00000000..6e524cc4 --- /dev/null +++ b/TactilityHeadless/Source/Hal/I2c/I2cCompat.h @@ -0,0 +1,41 @@ +#pragma once + +#ifdef ESP_TARGET + +#include + +#else + +#include +#include "portmacro.h" + +typedef int esp_err_t; + +typedef enum { + I2C_NUM_0 = 0, + I2C_NUM_1, + LP_I2C_NUM_0, + I2C_NUM_MAX, +} i2c_port_t; + +typedef enum{ + I2C_MODE_MASTER, + I2C_MODE_MAX, +} i2c_mode_t; + +typedef struct { + i2c_mode_t mode; + int sda_io_num; + int scl_io_num; + bool sda_pullup_en; + bool scl_pullup_en; + + union { + struct { + uint32_t clk_speed; + } master; + }; + uint32_t clk_flags; +} i2c_config_t; + +#endif diff --git a/TactilityHeadless/Source/Hal/I2c/I2cMock.cpp b/TactilityHeadless/Source/Hal/I2c/I2cMock.cpp new file mode 100644 index 00000000..b93ebd65 --- /dev/null +++ b/TactilityHeadless/Source/Hal/I2c/I2cMock.cpp @@ -0,0 +1,102 @@ +#ifndef ESP_TARGET + +/** + * This code is based on i2c_manager from https://github.com/ropg/i2c_manager/blob/master/i2c_manager/i2c_manager.c (original has MIT license) + */ +#include "I2c.h" +#include "Log.h" +#include "Mutex.h" + +namespace tt::hal::i2c { + +typedef struct Data { + Mutex mutex; + bool isConfigured = false; + bool isStarted = false; + Configuration configuration; +} Data; + +static Data dataArray[I2C_NUM_MAX]; + +#define TAG "i2c" + +bool init(const std::vector& configurations) { + TT_LOG_I(TAG, "Init"); + for (const auto& configuration: configurations) { + Data& data = dataArray[configuration.port]; + data.configuration = configuration; + data.isConfigured = true; + } + + for (const auto& config: configurations) { + if (config.initMode == InitByTactility && !start(config.port)) { + return false; + } else if (config.initMode == InitByExternal) { + dataArray[config.port].isStarted = true; + } + } + + return true; +} + +static bool configure_locked(i2c_port_t port, const i2c_config_t& configuration) { + Data& data = dataArray[port]; + if (data.isStarted) { + TT_LOG_E(TAG, "(%d) Cannot reconfigure while interface is started", port); + return false; + } else if (!data.configuration.hasMutableConfiguration) { + TT_LOG_E(TAG, "(%d) Mutation not allowed by original configuration", port); + return false; + } else { + data.configuration.config = configuration; + return true; + } +} + +esp_err_t configure(i2c_port_t port, const i2c_config_t& configuration) { + lock(port); + bool result = configure_locked(port, configuration); + unlock(port); + return result; +} + +bool start(i2c_port_t port) { + lock(port); + dataArray[port].isStarted = true; + unlock(port); + return false; +} + +bool stop(i2c_port_t port) { + lock(port); + dataArray[port].isStarted = false; + unlock(port); + return true; +} + +bool isStarted(i2c_port_t port) { + lock(port); + bool started = dataArray[port].isStarted; + unlock(port); + return started; +} + +bool read(i2c_port_t port, uint16_t address, uint32_t reg, uint8_t* buffer, uint16_t size) { + return false; +} + +bool write(i2c_port_t port, uint16_t address, uint32_t reg, const uint8_t* buffer, uint16_t size) { + return false; +} + +TtStatus lock(i2c_port_t port, uint32_t timeout) { + return dataArray[port].mutex.acquire(timeout); +} + +TtStatus unlock(i2c_port_t port) { + return dataArray[port].mutex.release(); +} + +} // namespace + +#endif \ No newline at end of file diff --git a/TactilityHeadless/Source/Services/Sdcard/Sdcard.cpp b/TactilityHeadless/Source/Services/Sdcard/Sdcard.cpp index 03a12b3b..ab546390 100644 --- a/TactilityHeadless/Source/Services/Sdcard/Sdcard.cpp +++ b/TactilityHeadless/Source/Services/Sdcard/Sdcard.cpp @@ -76,7 +76,7 @@ static int32_t sdcard_task(void* context) { } static void on_start(Service& service) { - if (get_hardware_config()->sdcard != nullptr) { + if (get_hardware_config().sdcard != nullptr) { ServiceData* data = service_data_alloc(); service.setData(data); data->thread->start(); diff --git a/TactilityHeadless/Source/Services/Wifi/WifiMock.cpp b/TactilityHeadless/Source/Services/Wifi/WifiMock.cpp index be82d171..1e1bc8e0 100644 --- a/TactilityHeadless/Source/Services/Wifi/WifiMock.cpp +++ b/TactilityHeadless/Source/Services/Wifi/WifiMock.cpp @@ -30,7 +30,7 @@ typedef struct { } Wifi; -static Wifi* wifi = NULL; +static Wifi* wifi = nullptr; // Forward declarations static void wifi_lock(Wifi* wifi); diff --git a/TactilityHeadless/Source/TactilityHeadless.cpp b/TactilityHeadless/Source/TactilityHeadless.cpp index dde90e50..a1917306 100644 --- a/TactilityHeadless/Source/TactilityHeadless.cpp +++ b/TactilityHeadless/Source/TactilityHeadless.cpp @@ -31,17 +31,18 @@ static void register_and_start_system_services() { } } -void headless_init(const hal::Configuration* config) { +void init(const hal::Configuration& config) { #ifdef ESP_PLATFORM esp_init(); #endif - hardwareConfig = config; + hardwareConfig = &config; hal::init(config); register_and_start_system_services(); } -const hal::Configuration* get_hardware_config() { - return hardwareConfig; +const hal::Configuration& get_hardware_config() { + tt_assert(hardwareConfig != nullptr); + return *hardwareConfig; } } // namespace diff --git a/TactilityHeadless/Source/TactilityHeadless.h b/TactilityHeadless/Source/TactilityHeadless.h index 3ef41d5d..bc011c57 100644 --- a/TactilityHeadless/Source/TactilityHeadless.h +++ b/TactilityHeadless/Source/TactilityHeadless.h @@ -5,8 +5,8 @@ namespace tt { -void headless_init(const hal::Configuration* config); +void init(const hal::Configuration& config); -const hal::Configuration* get_hardware_config(); +const hal::Configuration& get_hardware_config(); } // namespace