From a8903a8d30beb444a1628a4f4b4161bcafac9430 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Fri, 10 Nov 2023 09:39:29 -0600 Subject: [PATCH 1/3] feat(box-3): Closes #29 add support for selecting between box and box-3 hardware targets --- components/box-emu-hal/CMakeLists.txt | 2 +- components/box-emu-hal/Kconfig.projbuild | 16 +++++ components/box-emu-hal/include/box.hpp | 75 ++++++++++++++++++++++ components/box-emu-hal/include/box_3.hpp | 75 ++++++++++++++++++++++ components/box-emu-hal/include/fs_init.hpp | 2 + components/box-emu-hal/include/hal.hpp | 11 ++++ components/box-emu-hal/include/hal_i2c.hpp | 1 + components/box-emu-hal/src/fs_init.cpp | 12 ++-- components/box-emu-hal/src/hal_i2c.cpp | 21 +++--- components/box-emu-hal/src/i2s_audio.cpp | 42 ++++++------ components/box-emu-hal/src/input.cpp | 48 ++++++++++---- components/box-emu-hal/src/spi_lcd.cpp | 71 ++++++++++---------- components/espp | 2 +- 13 files changed, 287 insertions(+), 91 deletions(-) create mode 100644 components/box-emu-hal/Kconfig.projbuild create mode 100644 components/box-emu-hal/include/box.hpp create mode 100644 components/box-emu-hal/include/box_3.hpp create mode 100644 components/box-emu-hal/include/hal.hpp diff --git a/components/box-emu-hal/CMakeLists.txt b/components/box-emu-hal/CMakeLists.txt index 07f9d036..407fa9b0 100644 --- a/components/box-emu-hal/CMakeLists.txt +++ b/components/box-emu-hal/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( INCLUDE_DIRS "include" SRC_DIRS "src" - REQUIRES "driver" "heap" "fatfs" "esp_lcd" "esp_psram" "spi_flash" "nvs_flash" "codec" "display" "display_drivers" "mcp23x17" "input_drivers" "tt21100" "drv2605" "event_manager" "i2c" + REQUIRES "driver" "heap" "fatfs" "esp_lcd" "esp_psram" "spi_flash" "nvs_flash" "codec" "display" "display_drivers" "mcp23x17" "input_drivers" "tt21100" "gt911" "drv2605" "event_manager" "i2c" ) diff --git a/components/box-emu-hal/Kconfig.projbuild b/components/box-emu-hal/Kconfig.projbuild new file mode 100644 index 00000000..5c232860 --- /dev/null +++ b/components/box-emu-hal/Kconfig.projbuild @@ -0,0 +1,16 @@ +menu "BOX Emulator HAL Configuration" + + choice + prompt "Hardware Configuration" + default HARDWARE_BOX + help + Select the dev-kit / hardware you're using. + config HARDWARE_BOX + bool "ESP BOX" + config HARDWARE_BOX_3 + bool "ESP BOX 3" + config HARDWARE_TDECK + bool "LILYGO T DECK" + endchoice + +endmenu diff --git a/components/box-emu-hal/include/box.hpp b/components/box-emu-hal/include/box.hpp new file mode 100644 index 00000000..ec713f1f --- /dev/null +++ b/components/box-emu-hal/include/box.hpp @@ -0,0 +1,75 @@ +#pragma once + +#include "hal/spi_types.h" +#include "driver/gpio.h" +#include "driver/i2s_std.h" +#include "driver/spi_master.h" + +#include "i2c.hpp" +#include "st7789.hpp" +#include "touchpad_input.hpp" +#include "tt21100.hpp" + +namespace box_hal { + +static constexpr std::string_view dev_kit = "ESP32-S3-BOX"; + +// internal i2c (touchscreen, audio codec) +static constexpr auto internal_i2c_port = I2C_NUM_0; +static constexpr auto internal_i2c_clock_speed = 400 * 1000; +static constexpr gpio_num_t internal_i2c_sda = GPIO_NUM_8; +static constexpr gpio_num_t internal_i2c_scl = GPIO_NUM_18; + +// external I2c (peripherals) +static constexpr auto external_i2c_port = I2C_NUM_1; +static constexpr auto external_i2c_clock_speed = 400 * 1000; +static constexpr gpio_num_t external_i2c_sda = GPIO_NUM_41; +static constexpr gpio_num_t external_i2c_scl = GPIO_NUM_40; + +// LCD +static constexpr int lcd_clock_speed = 60 * 1000 * 1000; +static constexpr auto lcd_spi_num = SPI2_HOST; +static constexpr gpio_num_t lcd_cs = GPIO_NUM_5; +static constexpr gpio_num_t lcd_mosi = GPIO_NUM_6; +static constexpr gpio_num_t lcd_sclk = GPIO_NUM_7; +static constexpr gpio_num_t lcd_reset = GPIO_NUM_48; +static constexpr gpio_num_t lcd_dc = GPIO_NUM_4; +static constexpr gpio_num_t backlight = GPIO_NUM_45; +static constexpr size_t display_width = 320; +static constexpr size_t display_height = 240; +static constexpr bool backlight_value = true; +static constexpr bool reset_value = false; +static constexpr bool invert_colors = true; +static constexpr auto rotation = espp::Display::Rotation::LANDSCAPE; +static constexpr bool mirror_x = true; +static constexpr bool mirror_y = true; +using DisplayDriver = espp::St7789; + +// touch +static constexpr bool touch_swap_xy = false; +static constexpr bool touch_invert_x = false; +static constexpr bool touch_invert_y = false; +static constexpr gpio_num_t touch_interrupt = GPIO_NUM_3; +using TouchDriver = espp::Tt21100; + #define TOUCH_DRIVER_USE_WRITE 0 + #define TOUCH_DRIVER_USE_READ 1 + #define TOUCH_DRIVER_USE_WRITE_READ 0 + +// sound +static constexpr gpio_num_t sound_power_pin = GPIO_NUM_46; +static constexpr auto i2s_port = I2S_NUM_0; +static constexpr gpio_num_t i2s_mck_io = GPIO_NUM_2; +static constexpr gpio_num_t i2s_bck_io = GPIO_NUM_17; +static constexpr gpio_num_t i2s_ws_io = GPIO_NUM_47; +static constexpr gpio_num_t i2s_do_io = GPIO_NUM_15; +static constexpr gpio_num_t i2s_di_io = GPIO_NUM_16; +static constexpr gpio_num_t mute_pin = GPIO_NUM_1; + +// uSD card +static constexpr gpio_num_t sdcard_cs = GPIO_NUM_10; +static constexpr gpio_num_t sdcard_mosi = GPIO_NUM_11; +static constexpr gpio_num_t sdcard_miso = GPIO_NUM_13; +static constexpr gpio_num_t sdcard_sclk = GPIO_NUM_12; +static constexpr auto sdcard_spi_num = SPI3_HOST; + +} // namespace box_hal diff --git a/components/box-emu-hal/include/box_3.hpp b/components/box-emu-hal/include/box_3.hpp new file mode 100644 index 00000000..7e908f19 --- /dev/null +++ b/components/box-emu-hal/include/box_3.hpp @@ -0,0 +1,75 @@ +#pragma once + +#include "hal/spi_types.h" +#include "driver/gpio.h" +#include "driver/i2s_std.h" +#include "driver/spi_master.h" + +#include "i2c.hpp" +#include "st7789.hpp" +#include "touchpad_input.hpp" +#include "gt911.hpp" + +namespace box_hal { + +static constexpr std::string_view dev_kit = "ESP32-S3-BOX-3"; + +// internal i2c (touchscreen, audio codec) +static constexpr auto internal_i2c_port = I2C_NUM_0; +static constexpr auto internal_i2c_clock_speed = 400 * 1000; +static constexpr gpio_num_t internal_i2c_sda = GPIO_NUM_8; +static constexpr gpio_num_t internal_i2c_scl = GPIO_NUM_18; + +// external I2c (peripherals) +static constexpr auto external_i2c_port = I2C_NUM_1; +static constexpr auto external_i2c_clock_speed = 400 * 1000; +static constexpr gpio_num_t external_i2c_sda = GPIO_NUM_41; +static constexpr gpio_num_t external_i2c_scl = GPIO_NUM_40; + +// LCD +static constexpr int lcd_clock_speed = 60 * 1000 * 1000; +static constexpr auto lcd_spi_num = SPI2_HOST; +static constexpr gpio_num_t lcd_cs = GPIO_NUM_5; +static constexpr gpio_num_t lcd_mosi = GPIO_NUM_6; +static constexpr gpio_num_t lcd_sclk = GPIO_NUM_7; +static constexpr gpio_num_t lcd_reset = GPIO_NUM_48; +static constexpr gpio_num_t lcd_dc = GPIO_NUM_4; +static constexpr gpio_num_t backlight = GPIO_NUM_47; // was 45 on ESP32-S3-BOX +static constexpr size_t display_width = 320; +static constexpr size_t display_height = 240; +static constexpr bool backlight_value = true; +static constexpr bool reset_value = true; // was false on ESP32-S3-BOX +static constexpr bool invert_colors = true; +static constexpr auto rotation = espp::Display::Rotation::LANDSCAPE; +static constexpr bool mirror_x = true; +static constexpr bool mirror_y = true; +using DisplayDriver = espp::St7789; + +// touch +static constexpr bool touch_swap_xy = false; +static constexpr bool touch_invert_x = false; +static constexpr bool touch_invert_y = false; +static constexpr gpio_num_t touch_interrupt = GPIO_NUM_3; +using TouchDriver = espp::Gt911; + #define TOUCH_DRIVER_USE_WRITE 1 + #define TOUCH_DRIVER_USE_READ 0 + #define TOUCH_DRIVER_USE_WRITE_READ 1 + +// sound +static constexpr gpio_num_t sound_power_pin = GPIO_NUM_46; +static constexpr auto i2s_port = I2S_NUM_0; +static constexpr gpio_num_t i2s_mck_io = GPIO_NUM_2; +static constexpr gpio_num_t i2s_bck_io = GPIO_NUM_17; +static constexpr gpio_num_t i2s_ws_io = GPIO_NUM_45; // was 47 on ESP32-S3-BOX +static constexpr gpio_num_t i2s_do_io = GPIO_NUM_15; +static constexpr gpio_num_t i2s_di_io = GPIO_NUM_16; +static constexpr gpio_num_t mute_pin = GPIO_NUM_1; + +// uSD card +static constexpr gpio_num_t sdcard_cs = GPIO_NUM_10; +static constexpr gpio_num_t sdcard_mosi = GPIO_NUM_11; +static constexpr gpio_num_t sdcard_miso = GPIO_NUM_13; +static constexpr gpio_num_t sdcard_sclk = GPIO_NUM_12; +static constexpr auto sdcard_spi_num = SPI3_HOST; + +} // namespace box_hal diff --git a/components/box-emu-hal/include/fs_init.hpp b/components/box-emu-hal/include/fs_init.hpp index e4325dfa..6804a3d7 100644 --- a/components/box-emu-hal/include/fs_init.hpp +++ b/components/box-emu-hal/include/fs_init.hpp @@ -10,4 +10,6 @@ #include "sdmmc_cmd.h" #define MOUNT_POINT "/sdcard" +#include "hal.hpp" + void fs_init(); diff --git a/components/box-emu-hal/include/hal.hpp b/components/box-emu-hal/include/hal.hpp new file mode 100644 index 00000000..343dfaa5 --- /dev/null +++ b/components/box-emu-hal/include/hal.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include + +#if defined(CONFIG_HARDWARE_BOX) +#include "box.hpp" +#elif defined(CONFIG_HARDWARE_BOX_3) +#include "box_3.hpp" +#else +#error "Unsupported hardware configuration specified" +#endif diff --git a/components/box-emu-hal/include/hal_i2c.hpp b/components/box-emu-hal/include/hal_i2c.hpp index 8b668ac6..3c249be5 100644 --- a/components/box-emu-hal/include/hal_i2c.hpp +++ b/components/box-emu-hal/include/hal_i2c.hpp @@ -2,6 +2,7 @@ #include +#include "hal.hpp" #include "i2c.hpp" extern std::shared_ptr internal_i2c; diff --git a/components/box-emu-hal/src/fs_init.cpp b/components/box-emu-hal/src/fs_init.cpp index 76a1141e..eb25ee01 100644 --- a/components/box-emu-hal/src/fs_init.cpp +++ b/components/box-emu-hal/src/fs_init.cpp @@ -2,6 +2,8 @@ #include "format.hpp" +using namespace box_hal; + static void sdcard_init() { esp_err_t ret; @@ -27,13 +29,13 @@ static void sdcard_init() { // For setting a specific frequency, use host.max_freq_khz (range 400kHz - 20MHz for SDSPI) // Example: for fixed frequency of 10MHz, use host.max_freq_khz = 10000; sdmmc_host_t host = SDSPI_HOST_DEFAULT(); - host.slot = SPI3_HOST; + host.slot = sdcard_spi_num; // host.max_freq_khz = 10 * 1000; spi_bus_config_t bus_cfg = { - .mosi_io_num = GPIO_NUM_11, - .miso_io_num = GPIO_NUM_13, - .sclk_io_num = GPIO_NUM_12, + .mosi_io_num = sdcard_mosi, + .miso_io_num = sdcard_miso, + .sclk_io_num = sdcard_sclk, .quadwp_io_num = -1, .quadhd_io_num = -1, .max_transfer_sz = 8192, @@ -48,7 +50,7 @@ static void sdcard_init() { // This initializes the slot without card detect (CD) and write protect (WP) signals. // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals. sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT(); - slot_config.gpio_cs = GPIO_NUM_10; + slot_config.gpio_cs = sdcard_cs; slot_config.host_id = host_id; fmt::print("Mounting filesystem\n"); diff --git a/components/box-emu-hal/src/hal_i2c.cpp b/components/box-emu-hal/src/hal_i2c.cpp index e55420c4..3638d33d 100644 --- a/components/box-emu-hal/src/hal_i2c.cpp +++ b/components/box-emu-hal/src/hal_i2c.cpp @@ -1,29 +1,24 @@ #include "hal_i2c.hpp" -// Only used for the touchpad and the imu -static constexpr i2c_port_t I2C_INTERNAL = I2C_NUM_0; -// used for our peripherals (external to the ESP S3 BOX) -static constexpr i2c_port_t I2C_EXTERNAL = I2C_NUM_1; -static constexpr int I2C_FREQ_HZ = (400*1000); -static constexpr int I2C_TIMEOUT_MS = 10; - std::shared_ptr internal_i2c = nullptr; std::shared_ptr external_i2c = nullptr; static bool initialized = false; +using namespace box_hal; + void i2c_init() { if (initialized) return; internal_i2c = std::make_shared(espp::I2c::Config{ - .port = I2C_INTERNAL, - .sda_io_num = GPIO_NUM_8, - .scl_io_num = GPIO_NUM_18, + .port = internal_i2c_port, + .sda_io_num = internal_i2c_sda, + .scl_io_num = internal_i2c_scl, .sda_pullup_en = GPIO_PULLUP_ENABLE, .scl_pullup_en = GPIO_PULLUP_ENABLE}); external_i2c = std::make_shared(espp::I2c::Config{ - .port = I2C_EXTERNAL, - .sda_io_num = GPIO_NUM_41, - .scl_io_num = GPIO_NUM_40, + .port = external_i2c_port, + .sda_io_num = external_i2c_sda, + .scl_io_num = external_i2c_scl, .sda_pullup_en = GPIO_PULLUP_ENABLE, .scl_pullup_en = GPIO_PULLUP_ENABLE}); initialized = true; diff --git a/components/box-emu-hal/src/i2s_audio.cpp b/components/box-emu-hal/src/i2s_audio.cpp index fa33ed0a..13a57c1f 100644 --- a/components/box-emu-hal/src/i2s_audio.cpp +++ b/components/box-emu-hal/src/i2s_audio.cpp @@ -22,6 +22,10 @@ #include "es7210.h" #include "es8311.h" +#include "hal.hpp" + +using namespace box_hal; + /** * Look at * https://github.com/espressif/esp-idf/blob/master/examples/peripherals/i2s/i2s_codec/i2s_es8311/main/i2s_es8311_example.c @@ -29,14 +33,6 @@ * https://github.com/espressif/esp-box/blob/master/components/bsp/src/peripherals/bsp_i2s.c */ -/* I2S port and GPIOs */ -#define I2S_NUM (I2S_NUM_0) -#define I2S_MCK_IO (GPIO_NUM_2) -#define I2S_BCK_IO (GPIO_NUM_17) -#define I2S_WS_IO (GPIO_NUM_47) -#define I2S_DO_IO (GPIO_NUM_15) -#define I2S_DI_IO (GPIO_NUM_16) - /* Example configurations */ #define EXAMPLE_MCLK_MULTIPLE (I2S_MCLK_MULTIPLE_256) // If not using 24-bit data width, 256 should be enough #define EXAMPLE_MCLK_FREQ_HZ (AUDIO_SAMPLE_RATE * EXAMPLE_MCLK_MULTIPLE) @@ -86,7 +82,7 @@ static esp_err_t i2s_driver_init(void) printf("initializing i2s driver...\n"); auto ret_val = ESP_OK; printf("Using newer I2S standard\n"); - i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM, I2S_ROLE_MASTER); + i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(i2s_port, I2S_ROLE_MASTER); chan_cfg.auto_clear = true; // Auto clear the legacy data in the DMA buffer ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_handle, &rx_handle)); i2s_std_clk_config_t clock_cfg = I2S_STD_CLK_DEFAULT_CONFIG(AUDIO_SAMPLE_RATE); @@ -95,11 +91,11 @@ static esp_err_t i2s_driver_init(void) .clk_cfg = clock_cfg, .slot_cfg = slot_cfg, .gpio_cfg = { - .mclk = I2S_MCK_IO, - .bclk = I2S_BCK_IO, - .ws = I2S_WS_IO, - .dout = I2S_DO_IO, - .din = I2S_DI_IO, + .mclk = i2s_mck_io, + .bclk = i2s_bck_io, + .ws = i2s_ws_io, + .dout = i2s_do_io, + .din = i2s_di_io, .invert_flags = { .mclk_inv = false, .bclk_inv = false, @@ -177,7 +173,6 @@ static void gpio_isr_handler(void *arg) { } static void init_mute_button(void) { - static constexpr size_t MUTE_GPIO = 1; // create the gpio event queue gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t)); // setup gpio interrupts for mute button @@ -185,18 +180,18 @@ static void init_mute_button(void) { memset(&io_conf, 0, sizeof(io_conf)); // interrupt on any edge (since MUTE is connected to flipflop, see note below) io_conf.intr_type = GPIO_INTR_ANYEDGE; - io_conf.pin_bit_mask = (1< bool { - static uint32_t io_num; + static gpio_num_t io_num; if (xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) { // invert the state since these are active low switches - bool pressed = !gpio_get_level((gpio_num_t)io_num); + bool pressed = !gpio_get_level(io_num); // see if it's the mute button - if (io_num == MUTE_GPIO) { + if (io_num == mute_pin) { // NOTE: the MUTE is actually connected to a flip-flop which holds // state, so pressing it actually toggles the state that we see on // the ESP pin. Therefore, when we get an edge trigger, we should @@ -231,7 +226,6 @@ static void init_mute_button(void) { static bool initialized = false; void audio_init() { if (initialized) return; - auto pwr_ctl = GPIO_NUM_46; /* Config power control IO */ static esp_err_t bsp_io_config_state = ESP_FAIL; @@ -239,7 +233,7 @@ void audio_init() { gpio_config_t io_conf; io_conf.intr_type = GPIO_INTR_DISABLE; io_conf.mode = GPIO_MODE_OUTPUT; - io_conf.pin_bit_mask = 1ULL << pwr_ctl; + io_conf.pin_bit_mask = 1ULL << (int)sound_power_pin; io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; io_conf.pull_up_en = GPIO_PULLUP_DISABLE; bsp_io_config_state = gpio_config(&io_conf); @@ -250,7 +244,7 @@ void audio_init() { printf("Failed initialize power control IO\n"); } - gpio_set_level(pwr_ctl, 1); + gpio_set_level(sound_power_pin, 1); i2s_driver_init(); es7210_init_default(); diff --git a/components/box-emu-hal/src/input.cpp b/components/box-emu-hal/src/input.cpp index 2e053b27..52f72e8b 100644 --- a/components/box-emu-hal/src/input.cpp +++ b/components/box-emu-hal/src/input.cpp @@ -7,28 +7,32 @@ #include "mcp23x17.hpp" #include "touchpad_input.hpp" -#include "tt21100.hpp" using namespace std::chrono_literals; +using namespace box_hal; static std::shared_ptr mcp23x17; -static std::shared_ptr tt21100; +static std::shared_ptr touch_driver; static std::shared_ptr touchpad; /** * Touch Controller configuration */ void touchpad_read(uint8_t* num_touch_points, uint16_t* x, uint16_t* y, uint8_t* btn_state) { + *num_touch_points = 0; // get the latest data from the device std::error_code ec; - tt21100->update(ec); + bool new_data = touch_driver->update(ec); if (ec) { - fmt::print("error updating tt21100: {}\n", ec.message()); + fmt::print("error updating touch_driver: {}\n", ec.message()); + return; + } + if (!new_data) { return; } // now hand it off - tt21100->get_touch_point(num_touch_points, x, y); - *btn_state = tt21100->get_home_button_state(); + touch_driver->get_touch_point(num_touch_points, x, y); + *btn_state = touch_driver->get_home_button_state(); } static std::atomic initialized = false; @@ -36,18 +40,30 @@ void init_input() { if (initialized) return; fmt::print("Initializing input drivers...\n"); - fmt::print("Initializing Tt21100\n"); - tt21100 = std::make_shared(espp::Tt21100::Config{ - .read = std::bind(&espp::I2c::read, internal_i2c.get(), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), + fmt::print("Initializing touch driver\n"); + touch_driver = std::make_shared(TouchDriver::Config{ +#if TOUCH_DRIVER_USE_WRITE + .write = std::bind(&espp::I2c::write, internal_i2c.get(), std::placeholders::_1, + std::placeholders::_2, std::placeholders::_3), +#endif +#if TOUCH_DRIVER_USE_READ + .read = std::bind(&espp::I2c::read_at_register, internal_i2c.get(), std::placeholders::_1, + std::placeholders::_2, std::placeholders::_3), +#endif +#if TOUCH_DRIVER_USE_WRITE_READ + .write_read = std::bind(&espp::I2c::write_read, internal_i2c.get(), std::placeholders::_1, + std::placeholders::_2, std::placeholders::_3, + std::placeholders::_4, std::placeholders::_5), +#endif .log_level = espp::Logger::Verbosity::WARN }); fmt::print("Initializing touchpad\n"); touchpad = std::make_shared(espp::TouchpadInput::Config{ .touchpad_read = touchpad_read, - .swap_xy = false, - .invert_x = true, - .invert_y = false, + .swap_xy = touch_swap_xy, + .invert_x = touch_invert_x, + .invert_y = touch_invert_y, .log_level = espp::Logger::Verbosity::WARN }); @@ -84,8 +100,16 @@ extern "C" void get_input_state(struct InputState* state) { // start, select = A0, A1 std::error_code ec; auto a_pins = mcp23x17->get_pins(espp::Mcp23x17::Port::A, ec); + if (ec) { + fmt::print("error getting pins from mcp23x17: {}\n", ec.message()); + return; + } // d-pad, abxy = B0-B3, B4-B7 auto b_pins = mcp23x17->get_pins(espp::Mcp23x17::Port::B, ec); + if (ec) { + fmt::print("error getting pins from mcp23x17: {}\n", ec.message()); + return; + } is_a_pressed = !(b_pins & 1<<4); is_b_pressed = !(b_pins & 1<<5); is_x_pressed = !(b_pins & 1<<6); diff --git a/components/box-emu-hal/src/spi_lcd.cpp b/components/box-emu-hal/src/spi_lcd.cpp index 65d0e03d..e6b57071 100644 --- a/components/box-emu-hal/src/spi_lcd.cpp +++ b/components/box-emu-hal/src/spi_lcd.cpp @@ -1,15 +1,16 @@ +#include "hal.hpp" + #include "hal/spi_types.h" #include "driver/spi_master.h" #include "display.hpp" -#include "st7789.hpp" #include "spi_lcd.h" +using namespace box_hal; + static spi_device_handle_t spi; -static constexpr size_t display_width = 320; -static constexpr size_t display_height = 240; static constexpr size_t pixel_buffer_size = display_width*NUM_ROWS_IN_FRAME_BUFFER; std::shared_ptr display; @@ -22,7 +23,6 @@ static uint8_t *frame_buffer1; // 2. Sets some bits for other signaling (such as LVGL FLUSH) static constexpr int FLUSH_BIT = (1 << (int)espp::display_drivers::Flags::FLUSH_BIT); static constexpr int DC_LEVEL_BIT = (1 << (int)espp::display_drivers::Flags::DC_LEVEL_BIT); -static constexpr int DC_PIN_NUM = 4; // This function is called (in irq context!) just before a transmission starts. // It will set the D/C line to the value indicated in the user field @@ -31,7 +31,7 @@ static void lcd_spi_pre_transfer_callback(spi_transaction_t *t) { uint32_t user_flags = (uint32_t)(t->user); bool dc_level = user_flags & DC_LEVEL_BIT; - gpio_set_level((gpio_num_t)DC_PIN_NUM, dc_level); + gpio_set_level(lcd_dc, dc_level); } // This function is called (in irq context!) just after a transmission ends. It @@ -105,17 +105,17 @@ extern "C" void lcd_send_lines(int xs, int ys, int xe, int ye, const uint8_t *da } trans[i].flags = SPI_TRANS_USE_TXDATA; } - trans[0].tx_data[0] = (uint8_t)espp::St7789::Command::caset; + trans[0].tx_data[0] = (uint8_t)DisplayDriver::Command::caset; trans[1].tx_data[0] = (xs)>> 8; trans[1].tx_data[1] = (xs)&0xff; trans[1].tx_data[2] = (xe)>>8; trans[1].tx_data[3] = (xe)&0xff; - trans[2].tx_data[0] = (uint8_t)espp::St7789::Command::raset; + trans[2].tx_data[0] = (uint8_t)DisplayDriver::Command::raset; trans[3].tx_data[0] = (ys)>>8; trans[3].tx_data[1] = (ys)&0xff; trans[3].tx_data[2] = (ye)>>8; trans[3].tx_data[3] = (ye)&0xff; - trans[4].tx_data[0] = (uint8_t)espp::St7789::Command::ramwr; + trans[4].tx_data[0] = (uint8_t)DisplayDriver::Command::ramwr; trans[5].tx_buffer = data; trans[5].length = length*8; // undo SPI_TRANS_USE_TXDATA flag @@ -167,10 +167,10 @@ extern "C" void lcd_write_frame(const uint16_t xs, const uint16_t ys, const uint .y1 = (lv_coord_t)(ys), .x2 = (lv_coord_t)(xs+width-1), .y2 = (lv_coord_t)(ys+height-1)}; - espp::St7789::fill(nullptr, &area, (lv_color_t*)data); + DisplayDriver::fill(nullptr, &area, (lv_color_t*)data); } else { // don't have data, so clear the area (set to 0) - espp::St7789::clear(xs, ys, width, height); + DisplayDriver::clear(xs, ys, width, height); } } @@ -183,41 +183,42 @@ extern "C" void lcd_init() { spi_bus_config_t buscfg; memset(&buscfg, 0, sizeof(buscfg)); - buscfg.mosi_io_num=GPIO_NUM_6; - buscfg.miso_io_num=-1; - buscfg.sclk_io_num=GPIO_NUM_7; - buscfg.quadwp_io_num=-1; - buscfg.quadhd_io_num=-1; - buscfg.max_transfer_sz=display_width * display_height * sizeof(lv_color_t) + 8; + buscfg.mosi_io_num = lcd_mosi; + buscfg.miso_io_num = -1; + buscfg.sclk_io_num = lcd_sclk; + buscfg.quadwp_io_num = -1; + buscfg.quadhd_io_num = -1; + buscfg.max_transfer_sz = display_width * display_height * sizeof(lv_color_t) + 8; static spi_device_interface_config_t devcfg; memset(&devcfg, 0, sizeof(devcfg)); - devcfg.mode=0; + devcfg.mode = 0; // devcfg.flags = SPI_DEVICE_NO_RETURN_RESULT; - devcfg.clock_speed_hz=60*1000*1000; - devcfg.input_delay_ns=0; - devcfg.spics_io_num=GPIO_NUM_5; - devcfg.queue_size=spi_queue_size; - devcfg.pre_cb=lcd_spi_pre_transfer_callback; - devcfg.post_cb=lcd_spi_post_transfer_callback; + devcfg.clock_speed_hz = lcd_clock_speed; + devcfg.input_delay_ns = 0; + devcfg.spics_io_num = lcd_cs; + devcfg.queue_size = spi_queue_size; + devcfg.pre_cb = lcd_spi_pre_transfer_callback; + devcfg.post_cb = lcd_spi_post_transfer_callback; //Initialize the SPI bus - ret=spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO); + ret = spi_bus_initialize(lcd_spi_num, &buscfg, SPI_DMA_CH_AUTO); ESP_ERROR_CHECK(ret); //Attach the LCD to the SPI bus - ret=spi_bus_add_device(SPI2_HOST, &devcfg, &spi); + ret = spi_bus_add_device(lcd_spi_num, &devcfg, &spi); ESP_ERROR_CHECK(ret); // initialize the controller - espp::St7789::initialize(espp::display_drivers::Config{ + DisplayDriver::initialize(espp::display_drivers::Config{ .lcd_write = lcd_write, .lcd_send_lines = lcd_send_lines, - .reset_pin = (gpio_num_t)48, - .data_command_pin = (gpio_num_t)DC_PIN_NUM, - .backlight_pin = (gpio_num_t)45, - .backlight_on_value = true, - .invert_colors = true, - .mirror_x = true, - .mirror_y = true, + .reset_pin = lcd_reset, + .data_command_pin = lcd_dc, + .backlight_pin = backlight, + .backlight_on_value = backlight_value, + .reset_value = reset_value, + .invert_colors = invert_colors, + .mirror_x = mirror_x, + .mirror_y = mirror_y }); // initialize the display / lvgl using namespace std::chrono_literals; @@ -225,11 +226,11 @@ extern "C" void lcd_init() { .width = display_width, .height = display_height, .pixel_buffer_size = pixel_buffer_size, - .flush_callback = espp::St7789::flush, + .flush_callback = DisplayDriver::flush, .update_period = 5ms, .double_buffered = true, .allocation_flags = MALLOC_CAP_8BIT | MALLOC_CAP_DMA, - .rotation = espp::Display::Rotation::LANDSCAPE, + .rotation = rotation, .software_rotation_enabled = true, }); diff --git a/components/espp b/components/espp index b6ec4c46..c07e2fdb 160000 --- a/components/espp +++ b/components/espp @@ -1 +1 @@ -Subproject commit b6ec4c46ef1cfb09beae141406c30966ba80772f +Subproject commit c07e2fdb344856dc7b1c01234a2e16bd8d3ea0d7 From e1a5b72884b6190d31d28fe01d97657214ff08b3 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Fri, 10 Nov 2023 09:47:37 -0600 Subject: [PATCH 2/3] fix: fix build and config for box configuration --- components/box-emu-hal/include/box.hpp | 2 +- components/box-emu-hal/src/input.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/components/box-emu-hal/include/box.hpp b/components/box-emu-hal/include/box.hpp index ec713f1f..8dd12a71 100644 --- a/components/box-emu-hal/include/box.hpp +++ b/components/box-emu-hal/include/box.hpp @@ -47,7 +47,7 @@ using DisplayDriver = espp::St7789; // touch static constexpr bool touch_swap_xy = false; -static constexpr bool touch_invert_x = false; +static constexpr bool touch_invert_x = true; static constexpr bool touch_invert_y = false; static constexpr gpio_num_t touch_interrupt = GPIO_NUM_3; using TouchDriver = espp::Tt21100; diff --git a/components/box-emu-hal/src/input.cpp b/components/box-emu-hal/src/input.cpp index 52f72e8b..9561e0f2 100644 --- a/components/box-emu-hal/src/input.cpp +++ b/components/box-emu-hal/src/input.cpp @@ -47,7 +47,7 @@ void init_input() { std::placeholders::_2, std::placeholders::_3), #endif #if TOUCH_DRIVER_USE_READ - .read = std::bind(&espp::I2c::read_at_register, internal_i2c.get(), std::placeholders::_1, + .read = std::bind(&espp::I2c::read, internal_i2c.get(), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), #endif #if TOUCH_DRIVER_USE_WRITE_READ From 7188c78bea239c32548b6b6785adf2c2401fc55a Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Fri, 10 Nov 2023 09:53:03 -0600 Subject: [PATCH 3/3] readme: update --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index f037a3ad..cc7bb419 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,9 @@ https://user-images.githubusercontent.com/213467/236730090-56c3bd64-86e4-4b9b-a9 https://user-images.githubusercontent.com/213467/220791336-eb24116d-0958-4ab7-88bd-f6a5bd6d7eb1.mp4 +As of https://github.com/esp-cpp/esp-box-emu/pull/34 I am starting to add +initial support for the new ESP32-S3-BOX-3. + ## Description This project is a little retro game emulation system running on ESP32-S3-BOX. It