diff --git a/CMakeLists.txt b/CMakeLists.txt index 10262c1..b1ebffe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,15 +76,17 @@ if(CONFIG_LV_TOUCH_CONTROLLER) if(CONFIG_LV_TOUCH_DRIVER_PROTOCOL_SPI) list(APPEND SOURCES "lvgl_touch/tp_spi.c") - elseif(CONFIG_LV_TOUCH_DRIVER_PROTOCOL_I2C) - list(APPEND SOURCES "lvgl_touch/tp_i2c.c") endif() endif() +if(CONFIG_LV_I2C) + list(APPEND SOURCES "lvgl_i2c/i2c_manager.c") +endif() + idf_component_register(SRCS ${SOURCES} INCLUDE_DIRS ${LVGL_INCLUDE_DIRS} REQUIRES lvgl) - + target_compile_definitions(${COMPONENT_LIB} PUBLIC "-DLV_LVGL_H_INCLUDE_SIMPLE") else() diff --git a/Kconfig b/Kconfig index ae11925..36b4526 100644 --- a/Kconfig +++ b/Kconfig @@ -1,2 +1,14 @@ -rsource "lvgl_tft/Kconfig" -rsource "lvgl_touch/Kconfig" +menu "LVGL ESP Drivers" + + rsource "lvgl_tft/Kconfig" + + rsource "lvgl_touch/Kconfig" + +endmenu + +menu "I2C Port Settings" + depends on LV_I2C && !HAVE_I2C_MANAGER + + rsource "lvgl_i2c/Kconfig" + +endmenu diff --git a/README.md b/README.md index 89659a6..1908417 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ For a ready to use ESP32 project take look at the [lv_port_esp32](https://github - [Supported display controllers](#supported-display-controllers) - [Supported indev controllers](#supported-indev-controllers) - [Support for predefined development kits](#support-for-predefined-development-kits) +- [Thread-safe I2C with I2C Manager](#thread-safe-i2c-with-i2c-manager) **NOTE:** You need to set the display horizontal and vertical size, color depth and swap of RGB565 color on the LVGL configuration menuconfig (it's not handled automatically). @@ -35,8 +36,8 @@ swap of RGB565 color on the LVGL configuration menuconfig (it's not handled auto ## Supported indev controllers - XPT2046 -- FT3236 -- other FT6X36 or the FT6206 controllers should work as well (not tested) +- FT3236, FT6X36 +- FT6206 controllers should work as well (not tested) - STMPE610 - FT81x (Single, Dual, and Quad SPI) @@ -52,7 +53,7 @@ and sets the gpio numbers for the interface. |---------------------------|-----------------------|-----------|-----------|-----------| | ESP Wrover Kit v4.1 | ILI9341 | SPI | 240 | 320 | | M5Stack | ILI9341 | SPI | 240 | 320 | -| M5Core2 | ILI9341 | SPI | 240 | 320 | +| M5Stack Core2 | ILI9341 | SPI | 240 | 320 | | M5Stick | SH1107 | SPI | - | - | | M5StickC | ST7735S | SPI | 80 | 160 | | Adafruit 3.5 Featherwing | HX8357 | SPI | 480 | 320 | @@ -65,3 +66,16 @@ and sets the gpio numbers for the interface. **NOTE:** See [Supported display controllers](#supported-display-controllers) for more information on display configuration. **NOTE:** See [Supported indev controllers](#supported-indev-controllers) for more information about indev configuration. + + +## Thread-safe I2C with I2C Manager + +LVGL can use I2C to read from a touch sensor or write to a display, possibly +many times a second. Meanwhile, other tasks may also want to read from i2c +devices on the same bus. I2C using the ESP-IDF is not thread-safe. + +I2C Manager (`i2c_manager`) is a component that will let code in multiple threads +talk to devices on the I2C ports without getting in each other's way. These drivers +use a built-in copy of I2C Manager to talk to the I2C port, but you can also use +the I2C Manager component itself and have others play nice with LVGL and vice-versa. +[Click here](i2c_manager/README.md) for details. diff --git a/component.mk b/component.mk index dbd1105..0017d0b 100644 --- a/component.mk +++ b/component.mk @@ -1,7 +1,7 @@ # LVGL ESP32 drivers # Define sources and include dirs -COMPONENT_SRCDIRS := . lvgl_tft lvgl_touch +COMPONENT_SRCDIRS := . lvgl_tft lvgl_touch lvgl_i2c COMPONENT_ADD_INCLUDEDIRS := . # LVGL is supposed to be used as a ESP-IDF component @@ -44,4 +44,6 @@ $(call compile_only_if,$(and $(CONFIG_LV_TOUCH_CONTROLLER),$(CONFIG_LV_TOUCH_CON $(call compile_only_if,$(and $(CONFIG_LV_TOUCH_CONTROLLER),$(CONFIG_LV_TOUCH_CONTROLLER_RA8875)), lvgl_touch/ra8875_touch.o) $(call compile_only_if,$(and $(CONFIG_LV_TOUCH_CONTROLLER),$(CONFIG_LV_TOUCH_DRIVER_PROTOCOL_SPI)), lvgl_touch/tp_spi.o) -$(call compile_only_if,$(and $(CONFIG_LV_TOUCH_CONTROLLER),$(CONFIG_LV_TOUCH_DRIVER_PROTOCOL_I2C)), lvgl_touch/tp_i2c.o) + +# I2C Manager +$(call compile_only_if,$(CONFIG_LV_I2C), lvgl_i2c/i2c_manager.o) diff --git a/lvgl_helpers.c b/lvgl_helpers.c index 2e78fef..edc522f 100644 --- a/lvgl_helpers.c +++ b/lvgl_helpers.c @@ -14,9 +14,8 @@ #include "lvgl_touch/tp_spi.h" #include "lvgl_spi_conf.h" -#include "lvgl_i2c_conf.h" -#include "driver/i2c.h" +#include "lvgl_i2c/i2c_manager.h" #ifdef LV_LVGL_H_INCLUDE_SIMPLE #include "lvgl.h" @@ -68,7 +67,7 @@ void lvgl_driver_init(void) DISP_SPI_MISO, DISP_SPI_MOSI, DISP_SPI_CLK, SPI_BUS_MAX_TRANSFER_SZ, 1, DISP_SPI_IO2, DISP_SPI_IO3); - + disp_spi_add_device(TFT_SPI_HOST); disp_driver_init(); @@ -86,48 +85,29 @@ void lvgl_driver_init(void) TP_SPI_MISO, DISP_SPI_MOSI, DISP_SPI_CLK, SPI_BUS_MAX_TRANSFER_SZ, 1, -1, -1); - + disp_spi_add_device(TFT_SPI_HOST); tp_spi_add_device(TOUCH_SPI_HOST); - + disp_driver_init(); touch_driver_init(); return; #endif -#if defined (SHARED_I2C_BUS) - ESP_LOGI(TAG, "Initializing shared I2C master"); - - lvgl_i2c_driver_init(DISP_I2C_PORT, - DISP_I2C_SDA, DISP_I2C_SCL, - DISP_I2C_SPEED_HZ); - - disp_driver_init(); - touch_driver_init(); - - return; -#endif - /* Display controller initialization */ #if defined CONFIG_LV_TFT_DISPLAY_PROTOCOL_SPI ESP_LOGI(TAG, "Initializing SPI master for display"); - + lvgl_spi_driver_init(TFT_SPI_HOST, DISP_SPI_MISO, DISP_SPI_MOSI, DISP_SPI_CLK, SPI_BUS_MAX_TRANSFER_SZ, 1, DISP_SPI_IO2, DISP_SPI_IO3); - + disp_spi_add_device(TFT_SPI_HOST); - + disp_driver_init(); -#elif defined (CONFIG_LV_TFT_DISPLAY_PROTOCOL_I2C) - ESP_LOGI(TAG, "Initializing I2C master for display"); - /* Init the i2c master on the display driver code */ - lvgl_i2c_driver_init(DISP_I2C_PORT, - DISP_I2C_SDA, DISP_I2C_SCL, - DISP_I2C_SPEED_HZ); - +#elif defined (CONFIG_LV_I2C_DISPLAY) disp_driver_init(); #else #error "No protocol defined for display controller" @@ -137,22 +117,16 @@ void lvgl_driver_init(void) #if CONFIG_LV_TOUCH_CONTROLLER != TOUCH_CONTROLLER_NONE #if defined (CONFIG_LV_TOUCH_DRIVER_PROTOCOL_SPI) ESP_LOGI(TAG, "Initializing SPI master for touch"); - + lvgl_spi_driver_init(TOUCH_SPI_HOST, TP_SPI_MISO, TP_SPI_MOSI, TP_SPI_CLK, 0 /* Defaults to 4094 */, 2, -1, -1); - + tp_spi_add_device(TOUCH_SPI_HOST); - + touch_driver_init(); - #elif defined (CONFIG_LV_TOUCH_DRIVER_PROTOCOL_I2C) - ESP_LOGI(TAG, "Initializing I2C master for touch"); - - lvgl_i2c_driver_init(TOUCH_I2C_PORT, - TOUCH_I2C_SDA, TOUCH_I2C_SCL, - TOUCH_I2C_SPEED_HZ); - + #elif defined (CONFIG_LV_I2C_TOUCH) touch_driver_init(); #elif defined (CONFIG_LV_TOUCH_DRIVER_ADC) touch_driver_init(); @@ -165,42 +139,6 @@ void lvgl_driver_init(void) #endif } -/* Config the i2c master - * - * This should init the i2c master to be used on display and touch controllers. - * So we should be able to know if the display and touch controllers shares the - * same i2c master. - */ -bool lvgl_i2c_driver_init(int port, int sda_pin, int scl_pin, int speed_hz) -{ - esp_err_t err; - - ESP_LOGI(TAG, "Initializing I2C master port %d...", port); - ESP_LOGI(TAG, "SDA pin: %d, SCL pin: %d, Speed: %d (Hz)", - sda_pin, scl_pin, speed_hz); - - i2c_config_t conf = { - .mode = I2C_MODE_MASTER, - .sda_io_num = sda_pin, - .sda_pullup_en = GPIO_PULLUP_ENABLE, - .scl_io_num = scl_pin, - .scl_pullup_en = GPIO_PULLUP_ENABLE, - .master.clk_speed = speed_hz, - }; - - ESP_LOGI(TAG, "Setting I2C master configuration..."); - err = i2c_param_config(port, &conf); - assert(ESP_OK == err); - - ESP_LOGI(TAG, "Installing I2C master driver..."); - err = i2c_driver_install(port, - I2C_MODE_MASTER, - 0, 0 /*I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE */, - 0 /* intr_alloc_flags */); - assert(ESP_OK == err); - - return ESP_OK != err; -} /* Initialize spi bus master * diff --git a/lvgl_helpers.h b/lvgl_helpers.h index 081a843..8c599fe 100644 --- a/lvgl_helpers.h +++ b/lvgl_helpers.h @@ -88,14 +88,14 @@ extern "C" { * GLOBAL PROTOTYPES **********************/ +void lvgl_i2c_locking(void* leader); + /* Initialize detected SPI and I2C bus and devices */ void lvgl_driver_init(void); /* Initialize SPI master */ bool lvgl_spi_driver_init(int host, int miso_pin, int mosi_pin, int sclk_pin, int max_transfer_sz, int dma_channel, int quadwp_pin, int quadhd_pin); -/* Initialize I2C master */ -bool lvgl_i2c_driver_init(int port, int sda_pin, int scl_pin, int speed); /********************** * MACROS diff --git a/lvgl_i2c/Kconfig b/lvgl_i2c/Kconfig new file mode 100644 index 0000000..4011bcf --- /dev/null +++ b/lvgl_i2c/Kconfig @@ -0,0 +1,96 @@ +menu "I2C Port 0" + + config I2C_MANAGER_0_ENABLED + bool "Enable I2C port 0" + + if I2C_MANAGER_0_ENABLED + config I2C_MANAGER_0_SDA + int "SDA (GPIO pin)" + config I2C_MANAGER_0_SCL + int "SCL (GPIO pin)" + config I2C_MANAGER_0_FREQ_HZ + int "Frequency (Hz)" + default 400000 + range 100000 5000000 + help + The clock speed in Hz. Ranges from 100000 (100 kHz) to + 5000000 (5 Mhz). I2C busses that involve external wires may + have to be slower, and the real maximum speed the bus will + support depends on the value of the pullup resistors and the + design of the overall circuit. + config I2C_MANAGER_0_TIMEOUT + int "R/W timeout (ms)" + default 20 + range 10 1000 + help + Timeout for I2C read and write operations. This does not + include the time waiting for a lock. + config I2C_MANAGER_0_LOCK_TIMEOUT + int "Stale lock override (ms)" + default 50 + range 10 1000 + help + Timeout at which point an operation waiting for its turn on + the port will assume that whatever set the lock has died and + overrides it. Set this somewhat larger than the previous + timeout. + config I2C_MANAGER_0_PULLUPS + bool "Use ESP32 built-in bus pull-up resistors" + help + The I2C bus needs resistors to make sure it's in a defined + state when nobody is talking. Many circuits have external + pullup resistors already and turning these on will increase + power consumption slightly and may limit the speed your bus + can attain. Try with these off first if you don't know. + endif + +endmenu + + +menu "I2C Port 1" + + config I2C_MANAGER_1_ENABLED + bool "Enable I2C port 1" + + if I2C_MANAGER_1_ENABLED + config I2C_MANAGER_1_SDA + int "SDA (GPIO pin)" + config I2C_MANAGER_1_SCL + int "SCL (GPIO pin)" + config I2C_MANAGER_1_FREQ_HZ + int "Frequency (Hz)" + default 1000000 + range 100000 5000000 + help + The clock speed in Hz. Ranges from 100000 (100 kHz) to + 5000000 (5 Mhz). I2C busses that involve external wires may + have to be slower, and the real maximum speed the bus will + support depends on the value of the pullup resistors and the + design of the overall circuit. + config I2C_MANAGER_1_TIMEOUT + int "R/W timeout (ms)" + default 20 + range 10 1000 + help + Timeout for I2C read and write operations. This does not + include the time waiting for a lock. Default should be fine. + config I2C_MANAGER_1_LOCK_TIMEOUT + int "Stale lock override (ms)" + default 50 + help + Timeout at which point an operation waiting for its turn on + the port will assume that whatever set the lock has died and + overrides it. Set this somewhat larger than the previous + timeout. Default should be fine. + range 30 1000 + config I2C_MANAGER_1_PULLUPS + bool "Use ESP32 built-in bus pull-up resistors" + help + The I2C bus needs resistors to make sure it's in a defined + state when nobody is talking. Many circuits have external + pullup resistors already and turning these on will increase + power consumption slightly and may limit the speed your bus + can attain. Try with these off first if you don't know. + endif + +endmenu diff --git a/lvgl_i2c/README.md b/lvgl_i2c/README.md new file mode 100644 index 0000000..fb74e27 --- /dev/null +++ b/lvgl_i2c/README.md @@ -0,0 +1,88 @@ +# I2C in `lvgl_esp32_drivers` + + +  + + +## Information for users + +### I2C Manager support + +`lvgl_esp32_drivers` integrates [I2C Manager](https://github.com/ropg/i2c_manager), which is used in case you select a touch sensor or screen that uses the I2C bus. If you're just using LVGL you don't need to do anything special. + +I2C Manager can help if you are in a situation where you want to avoid "bus conflicts" on the I2C bus. Suppose you use LVGL with a touch sensor that uses I2C, and your device also has another I2C device that needs to be read frequently, such as a 3D-accelerometer. ESP-IDF is not inherently "thread-safe". So if you read that from another task than the one LVGL uses to read the touch data, you need some kind of mechanism to keep these communications from interfering. + +If you have (or write) a driver for that 3D-accelerometer that can use I2C Manager (or the I2C HAL and i2cdev abstraction layers that I2C Manager is compatible with) then put I2C Manager in your components directory by cloning the repository from below and in your main program do: + +```c +#include "i2c_manager.h" +#include "lvgl_helpers.h" + +[...] + +lvgl_i2c_locking(i2c_manager_locking()); +lv_init(); +lvgl_driver_init(); +``` + +The `lvgl_i2c_locking` part will cause the LVGL I2C driver to play nice with anything else that uses the I2C port(s) through I2C Manager. + + +  + + +## Information for LVGL driver developers + +I2C support in the LVGL ESP drivers is provided exclusively by the files in this directory. Driver code that uses I2C communicates through the functions provided in `i2c_manager.h`. + + +  + + +### Using I2C in an LVGL driver, a multi-step guide + +
+ +
Step 1
+ +
+The Kconfig entries for your driver only need to specify that you will be using I2C. This is done by adding `select LV_I2C_DISPLAY` or `select LV_I2C_TOUCH`. +
+ + +
Step 2
+ +
+To use the I2C port in your code you would do something like: + + +```c +#include "lvgl_i2c/i2c_manager.h" + +uint8_t data[2]; +lvgl_i2c_read(CONFIG_LV_I2C_TOUCH_PORT, 0x23, 0x42, &data, 2); +``` + +This causes a touch driver to read two bytes at register `0x42` from the IC at address `0x23`. Replace `CONFIG_LV_I2C_TOUCH_PORT` by `CONFIG_LV_I2C_DISPLAY_PORT` when this is a display instead of a touch driver. `lvgl_i2c_write` works much the same way, except it writes the bytes from the buffer instead of reading them. _(It's ignored above but these functions return `esp_err_t` so you can check if the I2C communication worked.)_ +
+ +
Step 3
+ +
+There is no step 3, you are already done. +
+ +
+ + +### Meanwhile, behind the scenes ... + +If any of the drivers selected by the user uses I2C, the menuconfig system will show an extra menu to select I2C port(s) for screen and/or touch sensor. An additional menu allows for setting of GPIO pins and bus speed of any port selected for use with LVGL. It's perfectly fine for a display and a touch sensor to be on the same I2C port or different ones. + + +  + + +## More information + +If you need more documentation, please refer to the [I2C Manager GitHub repository](https://github.com/ropg/i2c_manager) for more detailed information on how I2C manager works. There are features not in the simple example above, such as reads and writes without specifying a register, 16-bit registers, 10-bit I2C addressing and more. diff --git a/lvgl_i2c/i2c_manager.c b/lvgl_i2c/i2c_manager.c new file mode 100644 index 0000000..080d81a --- /dev/null +++ b/lvgl_i2c/i2c_manager.c @@ -0,0 +1,368 @@ +/* + +SPDX-License-Identifier: MIT + +MIT License + +Copyright (c) 2021 Rop Gonggrijp. Based on esp_i2c_helper by Mika Tuupola. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*/ + +#include +#include + +#include + +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" +#include "freertos/task.h" +#include + +#include "sdkconfig.h" + +#include "i2c_manager.h" + + +#if defined __has_include + #if __has_include ("esp_idf_version.h") + #include "esp_idf_version.h" + #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 3, 0) + #define HAS_CLK_FLAGS + #endif + #endif +#endif + + +static const char* TAG = I2C_TAG; + +static SemaphoreHandle_t I2C_FN(_local_mutex)[2] = { NULL, NULL }; +static SemaphoreHandle_t* I2C_FN(_mutex) = &I2C_FN(_local_mutex)[0]; + +static const uint8_t ACK_CHECK_EN = 1; + +#if defined (I2C_NUM_0) && defined (CONFIG_I2C_MANAGER_0_ENABLED) + #define I2C_ZERO I2C_NUM_0 + #if defined (CONFIG_I2C_MANAGER_0_PULLUPS) + #define I2C_MANAGER_0_PULLUPS true + #else + #define I2C_MANAGER_0_PULLUPS false + #endif + + #define I2C_MANAGER_0_TIMEOUT ( CONFIG_I2C_MANAGER_0_TIMEOUT / portTICK_RATE_MS ) + #define I2C_MANAGER_0_LOCK_TIMEOUT ( CONFIG_I2C_MANAGER_0_LOCK_TIMEOUT / portTICK_RATE_MS ) +#endif + + +#if defined (I2C_NUM_1) && defined (CONFIG_I2C_MANAGER_1_ENABLED) + #define I2C_ONE I2C_NUM_1 + #if defined (CONFIG_I2C_MANAGER_1_PULLUPS) + #define I2C_MANAGER_1_PULLUPS true + #else + #define I2C_MANAGER_1_PULLUPS false + #endif + + #define I2C_MANAGER_1_TIMEOUT ( CONFIG_I2C_MANAGER_1_TIMEOUT / portTICK_RATE_MS ) + #define I2C_MANAGER_1_LOCK_TIMEOUT ( CONFIG_I2C_MANAGER_1_LOCK_TIMEOUT / portTICK_RATE_MS ) +#endif + +#define ERROR_PORT(port, fail) { \ + ESP_LOGE(TAG, "Invalid port or not configured for I2C Manager: %d", (int)port); \ + return fail; \ +} + +#if defined(I2C_ZERO) && defined (I2C_ONE) + #define I2C_PORT_CHECK(port, fail) \ + if (port != I2C_NUM_0 && port != I2C_NUM_1) ERROR_PORT(port, fail); +#else + #if defined(I2C_ZERO) + #define I2C_PORT_CHECK(port, fail) \ + if (port != I2C_NUM_0) ERROR_PORT(port, fail); + #elif defined(I2C_ONE) + #define I2C_PORT_CHECK(port, fail) \ + if (port != I2C_NUM_1) ERROR_PORT(port, fail); + #else + #define I2C_PORT_CHECK(port, fail) \ + ERROR_PORT(port, fail); + #endif +#endif + +static void i2c_send_address(i2c_cmd_handle_t cmd, uint16_t addr, i2c_rw_t rw) { + if (addr & I2C_ADDR_10) { + i2c_master_write_byte(cmd, 0xF0 | ((addr & 0x3FF) >> 7) | rw, ACK_CHECK_EN); + i2c_master_write_byte(cmd, addr & 0xFF, ACK_CHECK_EN); + } else { + i2c_master_write_byte(cmd, (addr << 1) | rw, ACK_CHECK_EN); + } +} + +static void i2c_send_register(i2c_cmd_handle_t cmd, uint32_t reg) { + if (reg & I2C_REG_16) { + i2c_master_write_byte(cmd, (reg & 0xFF00) >> 8, ACK_CHECK_EN); + } + i2c_master_write_byte(cmd, reg & 0xFF, ACK_CHECK_EN); +} + +esp_err_t I2C_FN(_init)(i2c_port_t port) { + + I2C_PORT_CHECK(port, ESP_FAIL); + + esp_err_t ret = ESP_OK; + + if (I2C_FN(_mutex)[port] == 0) { + + ESP_LOGI(TAG, "Starting I2C master at port %d.", (int)port); + + I2C_FN(_mutex)[port] = xSemaphoreCreateMutex(); + + i2c_config_t conf = {0}; + + #ifdef HAS_CLK_FLAGS + conf.clk_flags = 0; + #endif + + #if defined (I2C_ZERO) + if (port == I2C_NUM_0) { + conf.sda_io_num = CONFIG_I2C_MANAGER_0_SDA; + conf.scl_io_num = CONFIG_I2C_MANAGER_0_SCL; + conf.sda_pullup_en = I2C_MANAGER_0_PULLUPS ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE; + conf.scl_pullup_en = conf.sda_pullup_en; + conf.master.clk_speed = CONFIG_I2C_MANAGER_0_FREQ_HZ; + } + #endif + + #if defined (I2C_ONE) + if (port == I2C_NUM_1) { + conf.sda_io_num = CONFIG_I2C_MANAGER_1_SDA; + conf.scl_io_num = CONFIG_I2C_MANAGER_1_SCL; + conf.sda_pullup_en = I2C_MANAGER_1_PULLUPS ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE; + conf.scl_pullup_en = conf.sda_pullup_en; + conf.master.clk_speed = CONFIG_I2C_MANAGER_1_FREQ_HZ; + } + #endif + + conf.mode = I2C_MODE_MASTER; + + ret = i2c_param_config(port, &conf); + ret |= i2c_driver_install(port, conf.mode, 0, 0, 0); + + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to initialise I2C port %d.", (int)port); + ESP_LOGW(TAG, "If it was already open, we'll use it with whatever settings were used " + "to open it. See I2C Manager README for details."); + } else { + ESP_LOGI(TAG, "Initialised port %d (SDA: %d, SCL: %d, speed: %d Hz.)", + port, conf.sda_io_num, conf.scl_io_num, conf.master.clk_speed); + } + + } + + return ret; +} + +esp_err_t I2C_FN(_read)(i2c_port_t port, uint16_t addr, uint32_t reg, uint8_t *buffer, uint16_t size) { + + I2C_PORT_CHECK(port, ESP_FAIL); + + esp_err_t result; + + // May seem weird, but init starts with a check if it's needed, no need for that check twice. + I2C_FN(_init)(port); + + ESP_LOGV(TAG, "Reading port %d, addr 0x%03x, reg 0x%04x", port, addr, reg); + + TickType_t timeout = 0; + #if defined (I2C_ZERO) + if (port == I2C_NUM_0) { + timeout = I2C_MANAGER_0_TIMEOUT; + } + #endif + #if defined (I2C_ONE) + if (port == I2C_NUM_1) { + timeout = I2C_MANAGER_1_TIMEOUT; + } + #endif + + if (I2C_FN(_lock)((int)port) == ESP_OK) { + i2c_cmd_handle_t cmd = i2c_cmd_link_create(); + if (!(reg & I2C_NO_REG)) { + /* When reading specific register set the addr pointer first. */ + i2c_master_start(cmd); + i2c_send_address(cmd, addr, I2C_MASTER_WRITE); + i2c_send_register(cmd, reg); + } + /* Read size bytes from the current pointer. */ + i2c_master_start(cmd); + i2c_send_address(cmd, addr, I2C_MASTER_READ); + i2c_master_read(cmd, buffer, size, I2C_MASTER_LAST_NACK); + i2c_master_stop(cmd); + result = i2c_master_cmd_begin(port, cmd, timeout); + i2c_cmd_link_delete(cmd); + I2C_FN(_unlock)((int)port); + } else { + ESP_LOGE(TAG, "Lock could not be obtained for port %d.", (int)port); + return ESP_ERR_TIMEOUT; + } + + if (result != ESP_OK) { + ESP_LOGW(TAG, "Error: %d", result); + } + + ESP_LOG_BUFFER_HEX_LEVEL(TAG, buffer, size, ESP_LOG_VERBOSE); + + return result; +} + +esp_err_t I2C_FN(_write)(i2c_port_t port, uint16_t addr, uint32_t reg, const uint8_t *buffer, uint16_t size) { + + I2C_PORT_CHECK(port, ESP_FAIL); + + esp_err_t result; + + // May seem weird, but init starts with a check if it's needed, no need for that check twice. + I2C_FN(_init)(port); + + ESP_LOGV(TAG, "Writing port %d, addr 0x%03x, reg 0x%04x", port, addr, reg); + + TickType_t timeout = 0; + #if defined (I2C_ZERO) + if (port == I2C_NUM_0) { + timeout = (CONFIG_I2C_MANAGER_0_TIMEOUT) / portTICK_RATE_MS; + } + #endif + #if defined (I2C_ONE) + if (port == I2C_NUM_1) { + timeout = (CONFIG_I2C_MANAGER_1_TIMEOUT) / portTICK_RATE_MS; + } + #endif + + if (I2C_FN(_lock)((int)port) == ESP_OK) { + i2c_cmd_handle_t cmd = i2c_cmd_link_create(); + i2c_master_start(cmd); + i2c_send_address(cmd, addr, I2C_MASTER_WRITE); + if (!(reg & I2C_NO_REG)) { + i2c_send_register(cmd, reg); + } + i2c_master_write(cmd, (uint8_t *)buffer, size, ACK_CHECK_EN); + i2c_master_stop(cmd); + result = i2c_master_cmd_begin( port, cmd, timeout); + i2c_cmd_link_delete(cmd); + I2C_FN(_unlock)((int)port); + } else { + ESP_LOGE(TAG, "Lock could not be obtained for port %d.", (int)port); + return ESP_ERR_TIMEOUT; + } + + if (result != ESP_OK) { + ESP_LOGW(TAG, "Error: %d", result); + } + + ESP_LOG_BUFFER_HEX_LEVEL(TAG, buffer, size, ESP_LOG_VERBOSE); + + return result; +} + +esp_err_t I2C_FN(_close)(i2c_port_t port) { + I2C_PORT_CHECK(port, ESP_FAIL); + vSemaphoreDelete(I2C_FN(_mutex)[port]); + I2C_FN(_mutex)[port] = NULL; + ESP_LOGI(TAG, "Closing I2C master at port %d", port); + return i2c_driver_delete(port); +} + +esp_err_t I2C_FN(_lock)(i2c_port_t port) { + I2C_PORT_CHECK(port, ESP_FAIL); + ESP_LOGV(TAG, "Mutex lock set for %d.", (int)port); + + TickType_t timeout; + #if defined (I2C_ZERO) + if (port == I2C_NUM_0) { + timeout = (CONFIG_I2C_MANAGER_0_LOCK_TIMEOUT) / portTICK_RATE_MS; + } + #endif + #if defined (I2C_ONE) + if (port == I2C_NUM_1) { + timeout = (CONFIG_I2C_MANAGER_1_LOCK_TIMEOUT) / portTICK_RATE_MS; + } + #endif + + if (xSemaphoreTake(I2C_FN(_mutex)[port], timeout) == pdTRUE) { + return ESP_OK; + } else { + ESP_LOGE(TAG, "Removing stale mutex lock from port %d.", (int)port); + I2C_FN(_force_unlock)(port); + return (xSemaphoreTake(I2C_FN(_mutex)[port], timeout) == pdTRUE ? ESP_OK : ESP_FAIL); + } +} + +esp_err_t I2C_FN(_unlock)(i2c_port_t port) { + I2C_PORT_CHECK(port, ESP_FAIL); + ESP_LOGV(TAG, "Mutex lock removed for %d.", (int)port); + return (xSemaphoreGive(I2C_FN(_mutex)[port]) == pdTRUE) ? ESP_OK : ESP_FAIL; +} + +esp_err_t I2C_FN(_force_unlock)(i2c_port_t port) { + I2C_PORT_CHECK(port, ESP_FAIL); + if (I2C_FN(_mutex)[port]) { + vSemaphoreDelete(I2C_FN(_mutex)[port]); + } + I2C_FN(_mutex)[port] = xSemaphoreCreateMutex(); + return ESP_OK; +} + + + +#ifdef I2C_OEM + + void I2C_FN(_locking)(void* leader) { + if (leader) { + ESP_LOGI(TAG, "Now following I2C Manager for locking"); + I2C_FN(_mutex) = (SemaphoreHandle_t*)leader; + } + } + +#else + + void* i2c_manager_locking() { + return (void*)i2c_manager_mutex; + } + + int32_t i2c_hal_read(void *handle, uint8_t address, uint8_t reg, uint8_t *buffer, uint16_t size) { + return i2c_manager_read(*(i2c_port_t*)handle, address, reg, buffer, size); + } + + int32_t i2c_hal_write(void *handle, uint8_t address, uint8_t reg, const uint8_t *buffer, uint16_t size) { + return i2c_manager_write(*(i2c_port_t*)handle, address, reg, buffer, size); + } + + static i2c_port_t port_zero = (i2c_port_t)0; + static i2c_port_t port_one = (i2c_port_t)1; + + static i2c_hal_t _i2c_hal[2] = { + {&i2c_hal_read, &i2c_hal_write, &port_zero}, + {&i2c_hal_read, &i2c_hal_write, &port_one} + }; + + void* i2c_hal(i2c_port_t port) { + I2C_PORT_CHECK(port, NULL); + return (void*)&_i2c_hal[port]; + } + +#endif diff --git a/lvgl_i2c/i2c_manager.h b/lvgl_i2c/i2c_manager.h new file mode 100644 index 0000000..f9ce585 --- /dev/null +++ b/lvgl_i2c/i2c_manager.h @@ -0,0 +1,76 @@ +#ifndef _I2C_MANAGER_H +#define _I2C_MANAGER_H + +#ifdef __cplusplus +extern "C" { +#endif + + +/* + + If you copy the i2c_manager files to your own component instead of + depending on i2c_manager, you MUST uncomment the define below + and put in some short string that identifies your component (such + as 'xyz'). This will cause i2c_manager to create functions named + xyz_i2c_* instead of i2c_manager_*. See README.md for details. + +*/ + +#define I2C_OEM lvgl + + +// Only here to get the I2C_NUM_0 and I2C_NUM_1 defines. +#include + +#define CONCATX(A, B) A ## B +#define CONCAT(A, B) CONCATX(A, B) +#define STR_LITERAL(s) # s +#define STR_EXPAND(s) STR_LITERAL(s) +#define STR_QUOTE(s) STR_EXPAND(STR_EXPAND(s)) + +#ifdef I2C_OEM + #define I2C_NAME_PREFIX CONCAT(I2C_OEM, _i2c) +#else + #define I2C_NAME_PREFIX i2c_manager +#endif +#define I2C_TAG STR_EXPAND(I2C_NAME_PREFIX) + +#define I2C_FN(s) CONCAT(I2C_NAME_PREFIX, s) + + +#define I2C_ADDR_10 ( 1 << 15 ) +#define I2C_REG_16 ( 1 << 31 ) +#define I2C_NO_REG ( 1 << 30 ) + +esp_err_t I2C_FN(_init)(i2c_port_t port); +esp_err_t I2C_FN(_read)(i2c_port_t port, uint16_t addr, uint32_t reg, uint8_t *buffer, uint16_t size); +esp_err_t I2C_FN(_write)(i2c_port_t port, uint16_t addr, uint32_t reg, const uint8_t *buffer, uint16_t size); +esp_err_t I2C_FN(_close)(i2c_port_t port); +esp_err_t I2C_FN(_lock)(i2c_port_t port); +esp_err_t I2C_FN(_unlock)(i2c_port_t port); +esp_err_t I2C_FN(_force_unlock)(i2c_port_t port); + + +#ifdef I2C_OEM + + void I2C_FN(_locking)(void* leader); + +#else + + void* i2c_manager_locking(); + + typedef struct { + int32_t (* read)(void *handle, uint8_t address, uint8_t reg, uint8_t *buffer, uint16_t size); + int32_t (* write)(void *handle, uint8_t address, uint8_t reg, const uint8_t *buffer, uint16_t size); + void *handle; + } i2c_hal_t; + + void* i2c_hal(i2c_port_t port); + +#endif + + +#ifdef __cplusplus +} +#endif +#endif diff --git a/lvgl_i2c_conf.h b/lvgl_i2c_conf.h deleted file mode 100644 index cf8ac45..0000000 --- a/lvgl_i2c_conf.h +++ /dev/null @@ -1,116 +0,0 @@ -/** - * @file lvgl_i2c_config.h - */ - -#ifndef LVGL_I2C_CONF_H -#define LVGL_I2C_CONF_H - -#ifdef __cplusplus -extern "C" { -#endif - -/********************* - * INCLUDES - *********************/ -#ifdef LV_LVGL_H_INCLUDE_SIMPLE -#include "lvgl.h" -#else -#include "lvgl/lvgl.h" -#endif - -/********************* - * DEFINES - *********************/ - -/* TODO: Define the I2C bus clock based on the selected display or touch - * controllers. */ - -/* Do both display and touch controllers uses I2C? */ -#if defined (CONFIG_LV_TOUCH_DRIVER_PROTOCOL_I2C) && \ - defined (CONFIG_LV_TFT_DISPLAY_PROTOCOL_I2C) - -#if defined (CONFIG_LV_DISPLAY_I2C_PORT_0) && \ - defined (CONFIG_LV_TOUCH_I2C_PORT_0) -#define SHARED_I2C_PORT -#define DISP_I2C_PORT I2C_NUM_0 -#endif - -#if defined (CONFIG_LV_DISPLAY_I2C_PORT_1) && \ - defined (CONFIG_LV_TOUCH_I2C_PORT_1) -#define SHARED_I2C_PORT -#define DISP_I2C_PORT I2C_NUM_1 -#endif - -#if !defined (SHARED_I2C_PORT) -#endif -#endif - -#if defined (SHARED_I2C_PORT) -/* If the port is shared the display and touch controllers must use the same - * SCL and SDA pins, otherwise let the user know with an error. */ -#if (CONFIG_LV_DISP_PIN_SDA != CONFIG_LV_TOUCH_I2C_SDA) || \ - (CONFIG_LV_DISP_PIN_SCL != CONFIG_LV_TOUCH_I2C_SCL) -#error "To share I2C port you need to choose the same SDA and SCL pins on both display and touch configurations" -#endif - -#define DISP_I2C_SDA CONFIG_LV_DISP_PIN_SDA -#define DISP_I2C_SCL CONFIG_LV_DISP_PIN_SCL -#define DISP_I2C_ORIENTATION TFT_ORIENTATION_LANDSCAPE - -/* Setting the I2C speed to the slowest one */ -#if DISP_I2C_SPEED_HZ < TOUCH_I2C_SPEED_HZ -#define DISP_I2C_SPEED_HZ 400000 /* DISP_I2C_SPEED_HZ */ -#else -#define DISP_I2C_SPEED_HZ 400000 /* DISP_I2C_SPEED_HZ */ -#endif - -#else - -/* lets check if the touch controller uses I2C... */ -#if defined (CONFIG_LV_TOUCH_DRIVER_PROTOCOL_I2C) -#if defined (CONFIG_LV_TOUCH_I2C_PORT_0) -#define TOUCH_I2C_PORT I2C_NUM_0 -#else -#define TOUCH_I2C_PORT I2C_NUM_1 -#endif -#define TOUCH_I2C_SDA CONFIG_LV_TOUCH_I2C_SDA -#define TOUCH_I2C_SCL CONFIG_LV_TOUCH_I2C_SCL -#define TOUCH_I2C_SPEED_HZ 400000 -#endif - -/* lets check if the display controller uses I2C... */ -#if defined (CONFIG_LV_TFT_DISPLAY_PROTOCOL_I2C) -#if defined (CONFIG_LV_DISPLAY_I2C_PORT_0) -#define DISP_I2C_PORT I2C_NUM_0 -#else -#define DISP_I2C_PORT I2C_NUM_1 -#endif - -#define DISP_I2C_SDA CONFIG_LV_DISP_PIN_SDA -#define DISP_I2C_SCL CONFIG_LV_DISP_PIN_SCL -#define DISP_I2C_ORIENTATION TFT_ORIENTATION_LANDSCAPE -#define DISP_I2C_SPEED_HZ 400000 -#endif - -#endif - -/********************** - * TYPEDEFS - **********************/ - - -/********************** - * GLOBAL PROTOTYPES - **********************/ - - -/********************** - * MACROS - **********************/ - - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#endif /*LVGL_I2C_CONF_H*/ diff --git a/lvgl_tft/Kconfig b/lvgl_tft/Kconfig index 0535580..f92351b 100644 --- a/lvgl_tft/Kconfig +++ b/lvgl_tft/Kconfig @@ -50,7 +50,7 @@ menu "LVGL TFT Display controller" config LV_PREDEFINED_DISPLAY_WEMOS_LOLIN bool "Wemos Lolin OLED" select LV_TFT_DISPLAY_CONTROLLER_SSD1306 - select LV_TFT_DISPLAY_PROTOCOL_I2C + select LV_I2C_DISPLAY select LV_TFT_DISPLAY_MONOCHROME select LV_THEME_MONO config LV_PREDEFINED_DISPLAY_ATAG @@ -187,7 +187,7 @@ menu "LVGL TFT Display controller" help Display controller protocol SPI - config LV_TFT_DISPLAY_PROTOCOL_I2C + config LV_I2C_DISPLAY bool help Display controller protocol I2C @@ -311,7 +311,7 @@ menu "LVGL TFT Display controller" config LV_TFT_DISPLAY_USER_CONTROLLER_SSD1306 bool "SSD1306" select LV_TFT_DISPLAY_CONTROLLER_SSD1306 - select LV_TFT_DISPLAY_PROTOCOL_I2C + select LV_I2C_DISPLAY select LV_TFT_DISPLAY_MONOCHROME config LV_TFT_DISPLAY_USER_CONTROLLER_FT81X bool "FT81X" @@ -502,18 +502,6 @@ menu "LVGL TFT Display controller" depends on LV_TFT_DISPLAY_SPI_TRANS_MODE_SIO endchoice - choice - prompt "Display I2C port" if LV_TFT_DISPLAY_PROTOCOL_I2C - default LV_DISPLAY_I2C_PORT_0 - help - Select the I2C port used by the display controller. - - config LV_DISPLAY_I2C_PORT_0 - bool "I2C PORT 0" - config LV_DISPLAY_I2C_PORT_1 - bool "I2C PORT 1" - endchoice - config LV_TFT_USE_CUSTOM_SPI_CLK_DIVIDER bool "Use custom SPI clock frequency." if LV_TFT_DISPLAY_PROTOCOL_SPI default n @@ -591,6 +579,16 @@ menu "LVGL TFT Display controller" default 80 if LV_TFT_SPI_CLK_DIVIDER_80 default 2 + config LV_M5STICKC_HANDLE_AXP192 + bool "Handle Backlight and TFT power for M5StickC using AXP192." if LV_PREDEFINED_DISPLAY_M5STICKC || LV_TFT_DISPLAY_CONTROLLER_ST7735S + default y if LV_PREDEFINED_DISPLAY_M5STICKC + select LV_I2C_DISPLAY + help + Display and TFT power supply on M5StickC is controlled using an + AXP192 Power Mangerment IC. Select yes if you want to enable TFT IC + (LDO3) and backlight power using AXP192 by LVGL, or select no if you + want to take care of power management in your own code. + config LV_INVERT_DISPLAY bool "IN DEPRECATION - Invert display." if LV_TFT_DISPLAY_CONTROLLER_RA8875 default n @@ -604,31 +602,6 @@ menu "LVGL TFT Display controller" If the colors look inverted on your display, try enabling this. If it didn't help try LVGL configuration -> Swap the 2 bytes of RGB565 color. - config LV_M5STICKC_HANDLE_AXP192 - bool "Handle Backlight and TFT power for M5StickC using AXP192." if LV_PREDEFINED_DISPLAY_M5STICKC || LV_TFT_DISPLAY_CONTROLLER_ST7735S - default y if LV_PREDEFINED_DISPLAY_M5STICKC - help - Display and TFT power supply on M5StickC is controlled using an AXP192 Power Mangerment IC. - Select yes if you want to enable TFT IC (LDO3) and backlight power using AXP192 by LVGL, or select no if you want to take care of - power management in your own code. - - config LV_AXP192_PIN_SDA - int "GPIO for AXP192 I2C SDA" - depends on LV_M5STICKC_HANDLE_AXP192 - range 0 39 - default 21 if LV_PREDEFINED_DISPLAY_M5STICKC - default 21 - help - Configure the AXP192 I2C SDA pin here. - - config LV_AXP192_PIN_SCL - int "GPIO for AXP192 I2C SCL" - depends on LV_M5STICKC_HANDLE_AXP192 - range 0 39 - default 22 if LV_PREDEFINED_DISPLAY_M5STICKC - default 22 - help - Configure the AXP192 I2C SDA pin here. # menu will be visible only when LV_PREDEFINED_DISPLAY_NONE is y menu "Display RA8875 Configuration" @@ -770,7 +743,7 @@ menu "LVGL TFT Display controller" config LV_DISP_SPI_MOSI int "GPIO for MOSI (Master Out Slave In)" if LV_TFT_DISPLAY_PROTOCOL_SPI range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 + range 0 46 if IDF_TARGET_ESP32S2 range 0 21 if IDF_TARGET_ESP32C3 default 23 if LV_PREDEFINED_DISPLAY_WROVER4 @@ -800,7 +773,7 @@ menu "LVGL TFT Display controller" int "GPIO for MISO (Master In Slave Out)" if LV_TFT_DISPLAY_PROTOCOL_SPI depends on LV_DISPLAY_USE_SPI_MISO range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 + range 0 46 if IDF_TARGET_ESP32S2 range 0 21 if IDF_TARGET_ESP32C3 default 19 if LV_PREDEFINED_PINS_TKOALA @@ -846,7 +819,7 @@ menu "LVGL TFT Display controller" config LV_DISP_SPI_CLK int "GPIO for CLK (SCK / Serial Clock)" if LV_TFT_DISPLAY_PROTOCOL_SPI range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 + range 0 46 if IDF_TARGET_ESP32S2 range 0 21 if IDF_TARGET_ESP32C3 default 18 if LV_PREDEFINED_DISPLAY_M5STACK || LV_PREDEFINED_DISPLAY_M5STICK @@ -875,7 +848,7 @@ menu "LVGL TFT Display controller" int "GPIO for CS (Slave Select)" if LV_TFT_DISPLAY_PROTOCOL_SPI depends on LV_DISPLAY_USE_SPI_CS range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 + range 0 46 if IDF_TARGET_ESP32S2 range 0 21 if IDF_TARGET_ESP32C3 default 5 if LV_PREDEFINED_PINS_38V1 @@ -904,7 +877,7 @@ menu "LVGL TFT Display controller" config LV_DISP_PIN_DC int "GPIO for DC (Data / Command)" if LV_TFT_DISPLAY_PROTOCOL_SPI range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 + range 0 46 if IDF_TARGET_ESP32S2 range 0 21 if IDF_TARGET_ESP32C3 depends on LV_DISPLAY_USE_DC @@ -942,7 +915,7 @@ menu "LVGL TFT Display controller" int "GPIO for Reset" if LV_TFT_DISPLAY_PROTOCOL_SPI && !LV_DISP_ST7789_SOFT_RESET depends on LV_DISP_USE_RST range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 + range 0 46 if IDF_TARGET_ESP32S2 range 0 21 if IDF_TARGET_ESP32C3 default 18 if LV_PREDEFINED_PINS_38V1 @@ -964,7 +937,7 @@ menu "LVGL TFT Display controller" config LV_DISP_PIN_BUSY int "GPIO for Busy" if LV_TFT_DISPLAY_CONTROLLER_IL3820 || LV_TFT_DISPLAY_CONTROLLER_JD79653A || LV_TFT_DISPLAY_CONTROLLER_UC8151D range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 + range 0 46 if IDF_TARGET_ESP32S2 range 0 21 if IDF_TARGET_ESP32C3 default 35 if LV_TFT_DISPLAY_CONTROLLER_IL3820 || LV_TFT_DISPLAY_CONTROLLER_JD79653A || LV_TFT_DISPLAY_CONTROLLER_UC8151D @@ -1005,7 +978,7 @@ menu "LVGL TFT Display controller" int "GPIO for Backlight Control" depends on LV_ENABLE_BACKLIGHT_CONTROL range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 + range 0 46 if IDF_TARGET_ESP32S2 range 0 21 if IDF_TARGET_ESP32C3 default 23 if LV_PREDEFINED_PINS_38V1 @@ -1023,30 +996,39 @@ menu "LVGL TFT Display controller" help Configure the display BCLK (LED) pin here. - config LV_DISP_PIN_SDA - int "GPIO for I2C SDA" if LV_TFT_DISPLAY_PROTOCOL_I2C - range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 - range 0 21 if IDF_TARGET_ESP32C3 - - default 5 if LV_PREDEFINED_DISPLAY_WEMOS_LOLIN - default 5 - - help - Configure the I2C SDA pin here. - - config LV_DISP_PIN_SCL - int "GPIO for I2C SCL" if LV_TFT_DISPLAY_PROTOCOL_I2C - range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 - range 0 21 if IDF_TARGET_ESP32C3 - - default 4 if LV_PREDEFINED_DISPLAY_WEMOS_LOLIN - default 4 - - help - Configure the I2C SCL pin here. - endmenu + choice + prompt "Select an I2C port for the display" + default LV_I2C_DISPLAY_PORT_0 + depends on LV_I2C_DISPLAY + + config LV_I2C_DISPLAY_PORT_0 + bool + prompt "I2C port 0" + help + I2C is shared peripheral managed by I2C Manager. In order to configure I2C Manager (pinout, etc.) see menu + Component config->I2C Port Settings. + + config LV_I2C_DISPLAY_PORT_1 + bool + prompt "I2C port 1" + help + I2C is shared peripheral managed by I2C Manager. In order to configure I2C Manager (pinout, etc.) see menu + Component config->I2C Port Settings. + + endchoice + + config LV_I2C + bool + default y if LV_I2C_DISPLAY + + config LV_I2C_DISPLAY_PORT + int + default 1 if LV_I2C_DISPLAY_PORT_1 + default 0 + endmenu + + + diff --git a/lvgl_tft/ili9341.h b/lvgl_tft/ili9341.h index 5768fbe..e0058b1 100644 --- a/lvgl_tft/ili9341.h +++ b/lvgl_tft/ili9341.h @@ -20,7 +20,8 @@ extern "C" { #else #include "lvgl/lvgl.h" #endif -#include "../lvgl_helpers.h" + +#include "sdkconfig.h" /********************* * DEFINES diff --git a/lvgl_tft/ssd1306.c b/lvgl_tft/ssd1306.c index 43e5966..5953d44 100644 --- a/lvgl_tft/ssd1306.c +++ b/lvgl_tft/ssd1306.c @@ -13,10 +13,9 @@ /********************* * INCLUDES *********************/ -#include "driver/i2c.h" #include "assert.h" -#include "lvgl_i2c_conf.h" +#include "lvgl_i2c/i2c_manager.h" #include "ssd1306.h" @@ -25,6 +24,7 @@ *********************/ #define TAG "SSD1306" +#define OLED_I2C_PORT (CONFIG_LV_I2C_DISPLAY_PORT) // SLA (0x3C) + WRITE_MODE (0x00) = 0x78 (0b01111000) #define OLED_I2C_ADDRESS 0x3C #define OLED_WIDTH 128 @@ -70,8 +70,6 @@ // Charge Pump (pg.62) #define OLED_CMD_SET_CHARGE_PUMP 0x8D // follow with 0x14 -#define OLED_IIC_FREQ_HZ 400000 // I2C colock frequency - /********************** * TYPEDEFS **********************/ @@ -213,44 +211,15 @@ void ssd1306_sleep_out(void) static uint8_t send_data(lv_disp_drv_t *disp_drv, void *bytes, size_t bytes_len) { (void) disp_drv; - esp_err_t err; uint8_t *data = (uint8_t *) bytes; - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - - i2c_master_start(cmd); - i2c_master_write_byte(cmd, (OLED_I2C_ADDRESS << 1) | I2C_MASTER_WRITE, true); - - for (size_t idx = 0; idx < bytes_len; idx++) { - i2c_master_write_byte(cmd, data[idx], true); - } - - i2c_master_stop(cmd); - - /* Send queued commands */ - err = i2c_master_cmd_begin(DISP_I2C_PORT, cmd, 10 / portTICK_PERIOD_MS); - i2c_cmd_link_delete(cmd); - - return ESP_OK == err ? 0 : 1; + return lvgl_i2c_write(OLED_I2C_PORT, OLED_I2C_ADDRESS, data[0], data + 1, bytes_len - 1 ); } static uint8_t send_pixels(lv_disp_drv_t *disp_drv, void *color_buffer, size_t buffer_len) { (void) disp_drv; - esp_err_t err; - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - i2c_master_start(cmd); - i2c_master_write_byte(cmd, (OLED_I2C_ADDRESS << 1) | I2C_MASTER_WRITE, true); - - i2c_master_write_byte(cmd, OLED_CONTROL_BYTE_DATA_STREAM, true); - i2c_master_write(cmd, (uint8_t *) color_buffer, buffer_len, true); - i2c_master_stop(cmd); - - /* Send queued commands */ - err = i2c_master_cmd_begin(DISP_I2C_PORT, cmd, 10 / portTICK_PERIOD_MS); - i2c_cmd_link_delete(cmd); - - return ESP_OK == err ? 0 : 1; + return lvgl_i2c_write(OLED_I2C_PORT, OLED_I2C_ADDRESS, OLED_CONTROL_BYTE_DATA_STREAM, color_buffer, buffer_len); } diff --git a/lvgl_tft/ssd1306.h b/lvgl_tft/ssd1306.h index 2145493..62bd0e6 100644 --- a/lvgl_tft/ssd1306.h +++ b/lvgl_tft/ssd1306.h @@ -25,8 +25,6 @@ extern "C" { /********************* * DEFINES *********************/ -#define SSD1306_SDA CONFIG_LV_DISP_PIN_SDA -#define SSD1306_SCL CONFIG_LV_DISP_PIN_SCL #define SSD1306_DISPLAY_ORIENTATION TFT_ORIENTATION_LANDSCAPE /********************** diff --git a/lvgl_tft/st7735s.c b/lvgl_tft/st7735s.c index 2507118..8be725b 100644 --- a/lvgl_tft/st7735s.c +++ b/lvgl_tft/st7735s.c @@ -8,12 +8,15 @@ *********************/ #include "st7735s.h" #include "disp_spi.h" -#include "driver/i2c.h" #include "driver/gpio.h" #include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#ifdef CONFIG_LV_M5STICKC_HANDLE_AXP192 + #include "lvgl_i2c/i2c_manager.h" +#endif + /********************* * DEFINES *********************/ @@ -38,7 +41,6 @@ static void st7735s_send_cmd(uint8_t cmd); static void st7735s_send_data(void * data, uint16_t length); static void st7735s_send_color(void * data, uint16_t length); static void st7735s_set_orientation(uint8_t orientation); -static void i2c_master_init(); static void axp192_write_byte(uint8_t addr, uint8_t data); static void axp192_init(); static void axp192_sleep_in(); @@ -60,7 +62,6 @@ uint8_t st7735s_portrait_mode = 0; void st7735s_init(void) { #ifdef CONFIG_LV_M5STICKC_HANDLE_AXP192 - i2c_master_init(); axp192_init(); #endif @@ -163,12 +164,16 @@ void st7735s_flush(lv_disp_drv_t * drv, const lv_area_t * area, lv_color_t * col void st7735s_sleep_in() { st7735s_send_cmd(0x10); - axp192_sleep_in(); + #ifdef CONFIG_LV_M5STICKC_HANDLE_AXP192 + axp192_sleep_in(); + #endif } void st7735s_sleep_out() { - axp192_sleep_out(); + #ifdef CONFIG_LV_M5STICKC_HANDLE_AXP192 + axp192_sleep_out(); + #endif st7735s_send_cmd(0x11); } @@ -218,55 +223,35 @@ static void st7735s_set_orientation(uint8_t orientation) st7735s_send_data((void *) &data[orientation], 1); } -static void i2c_master_init() -{ - i2c_config_t i2c_config = { - .mode = I2C_MODE_MASTER, - .sda_io_num = AXP192_SDA, - .scl_io_num = AXP192_SCL, - .sda_pullup_en = GPIO_PULLUP_ENABLE, - .scl_pullup_en = GPIO_PULLUP_ENABLE, - .master.clk_speed = 400000 - }; - i2c_param_config(I2C_NUM_0, &i2c_config); - i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0); -} +#ifdef CONFIG_LV_M5STICKC_HANDLE_AXP192 -static void axp192_write_byte(uint8_t addr, uint8_t data) -{ - esp_err_t ret; - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); + static void axp192_write_byte(uint8_t addr, uint8_t data) + { + err = lvgl_i2c_write(CONFIG_LV_I2C_DISPLAY_PORT, AXP192_I2C_ADDRESS, addr, &data, 1); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "AXP192 send failed. code: 0x%.2X", ret); + } + } - i2c_master_start(cmd); - i2c_master_write_byte(cmd, (AXP192_I2C_ADDRESS << 1) | I2C_MASTER_WRITE, true); - i2c_master_write_byte(cmd, addr, true); - i2c_master_write_byte(cmd, data, true); - i2c_master_stop(cmd); + static void axp192_init() + { + // information on how to init and use AXP192 ifor M5StickC taken from + // https://forum.m5stack.com/topic/1025/m5stickc-turn-off-screen-completely - ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 10/portTICK_PERIOD_MS); - if (ret != ESP_OK) { - ESP_LOGE(TAG, "AXP192 send failed. code: 0x%.2X", ret); - } - i2c_cmd_link_delete(cmd); -} + axp192_write_byte(0x10, 0xFF); // OLED_VPP Enable + axp192_write_byte(0x28, 0xCC); // Enable LDO2&LDO3, LED&TFT 3.0V + axp192_sleep_out(); + ESP_LOGI(TAG, "AXP192 initialized, power enabled for LDO2 and LDO3"); + } -static void axp192_init() -{ - // information on how to init and use AXP192 ifor M5StickC taken from - // https://forum.m5stack.com/topic/1025/m5stickc-turn-off-screen-completely + static void axp192_sleep_in() + { + axp192_write_byte(0x12, 0x4b); + } - axp192_write_byte(0x10, 0xFF); // OLED_VPP Enable - axp192_write_byte(0x28, 0xCC); // Enable LDO2&LDO3, LED&TFT 3.0V - axp192_sleep_out(); - ESP_LOGI(TAG, "AXP192 initialized, power enabled for LDO2 and LDO3"); -} + static void axp192_sleep_out() + { + axp192_write_byte(0x12, 0x4d); + } -static void axp192_sleep_in() -{ - axp192_write_byte(0x12, 0x4b); -} - -static void axp192_sleep_out() -{ - axp192_write_byte(0x12, 0x4d); -} +#endif diff --git a/lvgl_tft/st7735s.h b/lvgl_tft/st7735s.h index 71924bf..7422be6 100644 --- a/lvgl_tft/st7735s.h +++ b/lvgl_tft/st7735s.h @@ -29,9 +29,6 @@ extern "C" { #define ST7735S_RST CONFIG_LV_DISP_PIN_RST #define ST7735S_USE_RST CONFIG_LV_DISP_USE_RST -#define AXP192_SDA CONFIG_LV_AXP192_PIN_SDA -#define AXP192_SCL CONFIG_LV_AXP192_PIN_SCL - #define ST7735S_INVERT_COLORS CONFIG_LV_INVERT_COLORS // Defines are taken from diff --git a/lvgl_touch/Kconfig b/lvgl_touch/Kconfig index d4b71fc..031cfc9 100644 --- a/lvgl_touch/Kconfig +++ b/lvgl_touch/Kconfig @@ -23,7 +23,7 @@ menu "LVGL Touch controller" select LV_TOUCH_DRIVER_PROTOCOL_SPI bool "XPT2046" config LV_TOUCH_CONTROLLER_FT6X06 - select LV_TOUCH_DRIVER_PROTOCOL_I2C + select LV_I2C_TOUCH bool "FT6X06" config LV_TOUCH_CONTROLLER_STMPE610 select LV_TOUCH_DRIVER_PROTOCOL_SPI @@ -38,16 +38,16 @@ menu "LVGL Touch controller" select LV_TOUCH_DRIVER_DISPLAY bool "RA8875" config LV_TOUCH_CONTROLLER_GT911 - select LV_TOUCH_DRIVER_PROTOCOL_I2C + select LV_I2C_TOUCH bool "GT911" endchoice - + config LV_TOUCH_DRIVER_PROTOCOL_SPI bool help Touch controller protocol SPI - config LV_TOUCH_DRIVER_PROTOCOL_I2C + config LV_I2C_TOUCH bool help Touch controller protocol I2C @@ -62,30 +62,16 @@ menu "LVGL Touch controller" help Touch controller uses same interface/device as display (Note: Display must be initialized before touch) - - choice - prompt "Touch I2C port" - depends on LV_TOUCH_DRIVER_PROTOCOL_I2C - - default LV_TOUCH_I2C_PORT_0 - help - Select the I2C port used by the touch controller. - config LV_TOUCH_I2C_PORT_0 - bool "I2C PORT 0" - config LV_TOUCH_I2C_PORT_1 - bool "I2C PORT 1" - endchoice - choice prompt "Touch Controller SPI Bus." depends on LV_TOUCH_DRIVER_PROTOCOL_SPI - + default LV_TOUCH_CONTROLLER_SPI_VSPI if !IDF_TARGET_ESP32S2 default LV_TOUCH_CONTROLLER_SPI_FSPI if IDF_TARGET_ESP32S2 help Select the SPI Bus the TFT Display is attached to. - + config LV_TOUCH_CONTROLLER_SPI_HSPI bool "HSPI" config LV_TOUCH_CONTROLLER_SPI_VSPI @@ -93,7 +79,7 @@ menu "LVGL Touch controller" config LV_TOUCH_CONTROLLER_SPI_FSPI bool "FSPI" if IDF_TARGET_ESP32S2 endchoice - + menu "Touchpanel (XPT2046) Pin Assignments" depends on LV_TOUCH_CONTROLLER_XPT2046 @@ -101,7 +87,7 @@ menu "LVGL Touch controller" int prompt "GPIO for MISO (Master In Slave Out)" range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 + range 0 46 if IDF_TARGET_ESP32S2 range 0 21 if IDF_TARGET_ESP32C3 default 35 if LV_PREDEFINED_PINS_38V1 @@ -113,7 +99,7 @@ menu "LVGL Touch controller" int prompt "GPIO for MOSI (Master Out Slave In)" range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 + range 0 46 if IDF_TARGET_ESP32S2 range 0 21 if IDF_TARGET_ESP32C3 default 32 if LV_PREDEFINED_PINS_38V1 @@ -124,7 +110,7 @@ menu "LVGL Touch controller" config LV_TOUCH_SPI_CLK int "GPIO for CLK (SCK / Serial Clock)" range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 + range 0 46 if IDF_TARGET_ESP32S2 range 0 21 if IDF_TARGET_ESP32C3 default 26 if LV_PREDEFINED_PINS_38V1 @@ -135,8 +121,8 @@ menu "LVGL Touch controller" config LV_TOUCH_SPI_CS int "GPIO for CS (Slave Select)" range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 - + range 0 46 if IDF_TARGET_ESP32S2 + default 33 if LV_PREDEFINED_PINS_38V1 default 5 help @@ -145,7 +131,7 @@ menu "LVGL Touch controller" config LV_TOUCH_PIN_IRQ int "GPIO for IRQ (Interrupt Request)" range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 + range 0 46 if IDF_TARGET_ESP32S2 range 0 21 if IDF_TARGET_ESP32C3 default 27 if LV_PREDEFINED_PINS_38V4 @@ -153,7 +139,7 @@ menu "LVGL Touch controller" help Configure the touchpanel IRQ pin here. endmenu - + menu "Touchpanel Configuration (XPT2046)" depends on LV_TOUCH_CONTROLLER_XPT2046 @@ -180,7 +166,7 @@ menu "LVGL Touch controller" prompt "Maximum Y coordinate value." default 4095 if LV_PREDEFINED_PINS_38V4 default 1900 - + config LV_TOUCH_XY_SWAP bool prompt "Swap XY." @@ -211,31 +197,6 @@ menu "LVGL Touch controller" endchoice endmenu - menu "Touchpanel (FT6X06) Pin Assignments" - depends on LV_TOUCH_CONTROLLER_FT6X06 - - config LV_TOUCH_I2C_SDA - int - prompt "GPIO for SDA (I2C)" - range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 - range 0 21 if IDF_TARGET_ESP32C3 - - default 21 - help - Configure the I2C touchpanel SDA pin here. - - config LV_TOUCH_I2C_SCL - int "GPIO for clock signal SCL (I2C)" - range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 - range 0 21 if IDF_TARGET_ESP32C3 - - default 22 - help - Configure the I2C touchpanel SCL pin here. - endmenu - menu "Touchpanel Configuration (FT6X06)" depends on LV_TOUCH_CONTROLLER_FT6X06 @@ -249,13 +210,13 @@ menu "LVGL Touch controller" prompt "Invert X coordinate value." default n - config LV_FT6X36_INVERT_Y - bool - prompt "Invert Y coordinate value." - default n + config LV_FT6X36_INVERT_Y + bool + prompt "Invert Y coordinate value." + default n endmenu - + menu "Touchpanel (STMPE610) Pin Assignments" depends on LV_TOUCH_CONTROLLER_STMPE610 @@ -263,7 +224,7 @@ menu "LVGL Touch controller" int prompt "GPIO for MISO (Master In Slave Out)" range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 + range 0 46 if IDF_TARGET_ESP32S2 range 0 21 if IDF_TARGET_ESP32C3 default 35 if LV_PREDEFINED_PINS_38V1 @@ -278,7 +239,7 @@ menu "LVGL Touch controller" int prompt "GPIO for MOSI (Master Out Slave In)" range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 + range 0 46 if IDF_TARGET_ESP32S2 range 0 21 if IDF_TARGET_ESP32C3 default 32 if LV_PREDEFINED_PINS_38V1 @@ -291,7 +252,7 @@ menu "LVGL Touch controller" config LV_TOUCH_SPI_CLK int "GPIO for CLK (SCK / Serial Clock)" range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 + range 0 46 if IDF_TARGET_ESP32S2 range 0 21 if IDF_TARGET_ESP32C3 default 26 if LV_PREDEFINED_PINS_38V1 @@ -303,7 +264,7 @@ menu "LVGL Touch controller" config LV_TOUCH_SPI_CS int "GPIO for CS (Slave Select)" range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 + range 0 46 if IDF_TARGET_ESP32S2 range 0 21 if IDF_TARGET_ESP32C3 default 33 if LV_PREDEFINED_PINS_38V1 @@ -335,7 +296,7 @@ menu "LVGL Touch controller" int prompt "Maximum Y coordinate value." default 3800 - + config LV_TOUCH_XY_SWAP bool prompt "Swap XY." @@ -351,7 +312,7 @@ menu "LVGL Touch controller" prompt "Invert Y coordinate value." default y endmenu - + menu "Touchpanel (ADCRAW) Pin Assignments" depends on LV_TOUCH_CONTROLLER_ADCRAW @@ -372,7 +333,7 @@ menu "LVGL Touch controller" help Configure the touchpanel Y- pin. Must be ADC input. - + config LV_TOUCHSCREEN_RESISTIVE_PIN_XL int prompt "GPIO X-" @@ -502,31 +463,6 @@ menu "LVGL Touch controller" default y endmenu - - menu "Touchpanel (GT911) Pin Assignments" - depends on LV_TOUCH_CONTROLLER_GT911 - - config LV_TOUCH_I2C_SDA - int - prompt "GPIO for SDA (I2C)" - range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 - range 0 21 if IDF_TARGET_ESP32C3 - - default 2 - help - Configure the I2C touchpanel SDA pin here. - - config LV_TOUCH_I2C_SCL - int "GPIO for clock signal SCL (I2C)" - range 0 39 if IDF_TARGET_ESP32 - range 0 43 if IDF_TARGET_ESP32S2 - range 0 21 if IDF_TARGET_ESP32C3 - - default 3 - help - Configure the I2C touchpanel SCL pin here. - endmenu menu "Touchpanel Configuration (GT911)" depends on LV_TOUCH_CONTROLLER_GT911 @@ -548,4 +484,37 @@ menu "LVGL Touch controller" endmenu + choice + prompt "Select an I2C port for the touch panel" + default LV_I2C_TOUCH_PORT_0 + depends on LV_I2C_TOUCH + + config LV_I2C_TOUCH_PORT_0 + bool + prompt "I2C port 0" + help + I2C is shared peripheral managed by I2C Manager. In order to configure I2C Manager (pinout, etc.) see menu + Component config->I2C Port Settings. + + config LV_I2C_TOUCH_PORT_1 + bool + prompt "I2C port 1" + help + I2C is shared peripheral managed by I2C Manager. In order to configure I2C Manager (pinout, etc.) see menu + Component config->I2C Port Settings. + + endchoice + + config LV_I2C + bool + default y if LV_I2C_TOUCH + + config LV_I2C_TOUCH_PORT + int + default 1 if LV_I2C_TOUCH_PORT_1 + default 0 + endmenu + + + diff --git a/lvgl_touch/ft6x36.c b/lvgl_touch/ft6x36.c index a4d74ef..9e34845 100644 --- a/lvgl_touch/ft6x36.c +++ b/lvgl_touch/ft6x36.c @@ -1,33 +1,32 @@ /* * Copyright © 2020 Wolfgang Christl -* Permission is hereby granted, free of charge, to any person obtaining a copy of this -* software and associated documentation files (the “Software”), to deal in the Software -* without restriction, including without limitation the rights to use, copy, modify, merge, -* publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons +* Permission is hereby granted, free of charge, to any person obtaining a copy of this +* software and associated documentation files (the “Software”), to deal in the Software +* without restriction, including without limitation the rights to use, copy, modify, merge, +* publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons * to whom the Software is furnished to do so, subject to the following conditions: * -* The above copyright notice and this permission notice shall be included in all copies or +* The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR -* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE -* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +* +* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE +* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include -#include #ifdef LV_LVGL_H_INCLUDE_SIMPLE #include #else #include #endif #include "ft6x36.h" -#include "tp_i2c.h" -#include "../lvgl_i2c_conf.h" + +#include "lvgl_i2c/i2c_manager.h" #define TAG "FT6X36" @@ -36,22 +35,10 @@ ft6x36_status_t ft6x36_status; uint8_t current_dev_addr; // set during init esp_err_t ft6x06_i2c_read8(uint8_t slave_addr, uint8_t register_addr, uint8_t *data_buf) { - i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create(); - - i2c_master_start(i2c_cmd); - i2c_master_write_byte(i2c_cmd, (slave_addr << 1) | I2C_MASTER_WRITE, true); - i2c_master_write_byte(i2c_cmd, register_addr, I2C_MASTER_ACK); - - i2c_master_start(i2c_cmd); - i2c_master_write_byte(i2c_cmd, (slave_addr << 1) | I2C_MASTER_READ, true); - - i2c_master_read_byte(i2c_cmd, data_buf, I2C_MASTER_NACK); - i2c_master_stop(i2c_cmd); - esp_err_t ret = i2c_master_cmd_begin(TOUCH_I2C_PORT, i2c_cmd, 1000 / portTICK_RATE_MS); - i2c_cmd_link_delete(i2c_cmd); - return ret; + return lvgl_i2c_read(CONFIG_LV_I2C_TOUCH_PORT, slave_addr, register_addr, data_buf, 1); } + /** * @brief Read the FT6x36 gesture ID. Initialize first! * @param dev_addr: I2C FT6x36 Slave address. @@ -75,42 +62,29 @@ uint8_t ft6x36_get_gesture_id() { * @retval None */ void ft6x06_init(uint16_t dev_addr) { - if (!ft6x36_status.inited) { -/* I2C master is initialized before calling this function */ -#if 0 - esp_err_t code = i2c_master_init(); -#else - esp_err_t code = ESP_OK; -#endif + ft6x36_status.inited = true; + current_dev_addr = dev_addr; + uint8_t data_buf; + esp_err_t ret; + ESP_LOGI(TAG, "Found touch panel controller"); + if ((ret = ft6x06_i2c_read8(dev_addr, FT6X36_PANEL_ID_REG, &data_buf) != ESP_OK)) + ESP_LOGE(TAG, "Error reading from device: %s", + esp_err_to_name(ret)); // Only show error the first time + ESP_LOGI(TAG, "\tDevice ID: 0x%02x", data_buf); - if (code != ESP_OK) { - ft6x36_status.inited = false; - ESP_LOGE(TAG, "Error during I2C init %s", esp_err_to_name(code)); - } else { - ft6x36_status.inited = true; - current_dev_addr = dev_addr; - uint8_t data_buf; - esp_err_t ret; - ESP_LOGI(TAG, "Found touch panel controller"); - if ((ret = ft6x06_i2c_read8(dev_addr, FT6X36_PANEL_ID_REG, &data_buf) != ESP_OK)) - ESP_LOGE(TAG, "Error reading from device: %s", - esp_err_to_name(ret)); // Only show error the first time - ESP_LOGI(TAG, "\tDevice ID: 0x%02x", data_buf); + ft6x06_i2c_read8(dev_addr, FT6X36_CHIPSELECT_REG, &data_buf); + ESP_LOGI(TAG, "\tChip ID: 0x%02x", data_buf); - ft6x06_i2c_read8(dev_addr, FT6X36_CHIPSELECT_REG, &data_buf); - ESP_LOGI(TAG, "\tChip ID: 0x%02x", data_buf); + ft6x06_i2c_read8(dev_addr, FT6X36_DEV_MODE_REG, &data_buf); + ESP_LOGI(TAG, "\tDevice mode: 0x%02x", data_buf); - ft6x06_i2c_read8(dev_addr, FT6X36_DEV_MODE_REG, &data_buf); - ESP_LOGI(TAG, "\tDevice mode: 0x%02x", data_buf); + ft6x06_i2c_read8(dev_addr, FT6X36_FIRMWARE_ID_REG, &data_buf); + ESP_LOGI(TAG, "\tFirmware ID: 0x%02x", data_buf); - ft6x06_i2c_read8(dev_addr, FT6X36_FIRMWARE_ID_REG, &data_buf); - ESP_LOGI(TAG, "\tFirmware ID: 0x%02x", data_buf); + ft6x06_i2c_read8(dev_addr, FT6X36_RELEASECODE_REG, &data_buf); + ESP_LOGI(TAG, "\tRelease code: 0x%02x", data_buf); - ft6x06_i2c_read8(dev_addr, FT6X36_RELEASECODE_REG, &data_buf); - ESP_LOGI(TAG, "\tRelease code: 0x%02x", data_buf); - } - } } /** @@ -120,67 +94,29 @@ void ft6x06_init(uint16_t dev_addr) { * @retval Always false */ bool ft6x36_read(lv_indev_drv_t *drv, lv_indev_data_t *data) { - uint8_t data_xy[4]; // 2 bytes X | 2 bytes Y - uint8_t touch_pnt_cnt; // Number of detected touch points + if (!ft6x36_status.inited) { + ESP_LOGE(TAG, "Init first!"); + return 0x00; + } + uint8_t data_buf[5]; // 1 byte status, 2 bytes X, 2 bytes Y static int16_t last_x = 0; // 12bit pixel value static int16_t last_y = 0; // 12bit pixel value - ft6x06_i2c_read8(current_dev_addr, FT6X36_TD_STAT_REG, &touch_pnt_cnt); - if (touch_pnt_cnt != 1) { // ignore no touch & multi touch + esp_err_t ret = lvgl_i2c_read(CONFIG_LV_I2C_TOUCH_PORT, current_dev_addr, FT6X36_TD_STAT_REG, &data_buf[0], 5); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Error talking to touch IC: %s", esp_err_to_name(ret)); + } + uint8_t touch_pnt_cnt = data_buf[0]; // Number of detected touch points + + if (ret != ESP_OK || touch_pnt_cnt != 1) { // ignore no touch & multi touch data->point.x = last_x; data->point.y = last_y; data->state = LV_INDEV_STATE_REL; return false; } - // Read X value - i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create(); - - i2c_master_start(i2c_cmd); - i2c_master_write_byte(i2c_cmd, (current_dev_addr << 1) | I2C_MASTER_WRITE, true); - i2c_master_write_byte(i2c_cmd, FT6X36_P1_XH_REG, I2C_MASTER_ACK); - - i2c_master_start(i2c_cmd); - i2c_master_write_byte(i2c_cmd, (current_dev_addr << 1) | I2C_MASTER_READ, true); - - i2c_master_read_byte(i2c_cmd, &data_xy[0], I2C_MASTER_ACK); // reads FT6X36_P1_XH_REG - i2c_master_read_byte(i2c_cmd, &data_xy[1], I2C_MASTER_NACK); // reads FT6X36_P1_XL_REG - i2c_master_stop(i2c_cmd); - esp_err_t ret = i2c_master_cmd_begin(TOUCH_I2C_PORT, i2c_cmd, 1000 / portTICK_RATE_MS); - i2c_cmd_link_delete(i2c_cmd); - if (ret != ESP_OK) { - ESP_LOGE(TAG, "Error getting X coordinates: %s", esp_err_to_name(ret)); - data->point.x = last_x; - data->point.y = last_y; - data->state = LV_INDEV_STATE_REL; // no touch detected - return false; - } - - // Read Y value - i2c_cmd = i2c_cmd_link_create(); - - i2c_master_start(i2c_cmd); - i2c_master_write_byte(i2c_cmd, (current_dev_addr << 1) | I2C_MASTER_WRITE, true); - i2c_master_write_byte(i2c_cmd, FT6X36_P1_YH_REG, I2C_MASTER_ACK); - - i2c_master_start(i2c_cmd); - i2c_master_write_byte(i2c_cmd, (current_dev_addr << 1) | I2C_MASTER_READ, true); - - i2c_master_read_byte(i2c_cmd, &data_xy[2], I2C_MASTER_ACK); // reads FT6X36_P1_YH_REG - i2c_master_read_byte(i2c_cmd, &data_xy[3], I2C_MASTER_NACK); // reads FT6X36_P1_YL_REG - i2c_master_stop(i2c_cmd); - ret = i2c_master_cmd_begin(TOUCH_I2C_PORT, i2c_cmd, 1000 / portTICK_RATE_MS); - i2c_cmd_link_delete(i2c_cmd); - if (ret != ESP_OK) { - ESP_LOGE(TAG, "Error getting Y coordinates: %s", esp_err_to_name(ret)); - data->point.x = last_x; - data->point.y = last_y; - data->state = LV_INDEV_STATE_REL; // no touch detected - return false; - } - - last_x = ((data_xy[0] & FT6X36_MSB_MASK) << 8) | (data_xy[1] & FT6X36_LSB_MASK); - last_y = ((data_xy[2] & FT6X36_MSB_MASK) << 8) | (data_xy[3] & FT6X36_LSB_MASK); + last_x = ((data_buf[1] & FT6X36_MSB_MASK) << 8) | (data_buf[2] & FT6X36_LSB_MASK); + last_y = ((data_buf[3] & FT6X36_MSB_MASK) << 8) | (data_buf[4] & FT6X36_LSB_MASK); #if CONFIG_LV_FT6X36_INVERT_X last_x = LV_HOR_RES - last_x; diff --git a/lvgl_touch/ft6x36.h b/lvgl_touch/ft6x36.h index da466b6..ace2997 100644 --- a/lvgl_touch/ft6x36.h +++ b/lvgl_touch/ft6x36.h @@ -2,20 +2,20 @@ /* * Copyright © 2020 Wolfgang Christl -* Permission is hereby granted, free of charge, to any person obtaining a copy of this -* software and associated documentation files (the “Software”), to deal in the Software -* without restriction, including without limitation the rights to use, copy, modify, merge, -* publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons +* Permission is hereby granted, free of charge, to any person obtaining a copy of this +* software and associated documentation files (the “Software”), to deal in the Software +* without restriction, including without limitation the rights to use, copy, modify, merge, +* publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons * to whom the Software is furnished to do so, subject to the following conditions: * -* The above copyright notice and this permission notice shall be included in all copies or +* The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR -* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE -* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +* +* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE +* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ diff --git a/lvgl_touch/gt911.c b/lvgl_touch/gt911.c index e6cb972..03c3d9d 100644 --- a/lvgl_touch/gt911.c +++ b/lvgl_touch/gt911.c @@ -19,15 +19,14 @@ */ #include -#include #ifdef LV_LVGL_H_INCLUDE_SIMPLE #include #else #include #endif #include "gt911.h" -#include "tp_i2c.h" -#include "../lvgl_i2c_conf.h" + +#include "lvgl_i2c/i2c_manager.h" #define TAG "GT911" @@ -35,35 +34,12 @@ gt911_status_t gt911_status; //TODO: handle multibyte read and refactor to just one read transaction esp_err_t gt911_i2c_read(uint8_t slave_addr, uint16_t register_addr, uint8_t *data_buf, uint8_t len) { - i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create(); - - i2c_master_start(i2c_cmd); - i2c_master_write_byte(i2c_cmd, (slave_addr << 1) | I2C_MASTER_WRITE, true); - i2c_master_write_byte(i2c_cmd, (register_addr >> 8), I2C_MASTER_ACK); - i2c_master_write_byte(i2c_cmd, (register_addr & 0xFF), I2C_MASTER_ACK); - - i2c_master_start(i2c_cmd); - i2c_master_write_byte(i2c_cmd, (slave_addr << 1) | I2C_MASTER_READ, true); - - i2c_master_read_byte(i2c_cmd, data_buf, I2C_MASTER_NACK); - i2c_master_stop(i2c_cmd); - esp_err_t ret = i2c_master_cmd_begin(TOUCH_I2C_PORT, i2c_cmd, 1000 / portTICK_RATE_MS); - i2c_cmd_link_delete(i2c_cmd); - return ret; + return lvgl_i2c_read(CONFIG_LV_I2C_TOUCH_PORT, slave_addr, register_addr | I2C_REG_16, data_buf, len); } esp_err_t gt911_i2c_write8(uint8_t slave_addr, uint16_t register_addr, uint8_t data) { - i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create(); - - i2c_master_start(i2c_cmd); - i2c_master_write_byte(i2c_cmd, (slave_addr << 1) | I2C_MASTER_WRITE, true); - i2c_master_write_byte(i2c_cmd, (register_addr >> 8), I2C_MASTER_ACK); - i2c_master_write_byte(i2c_cmd, (register_addr & 0xFF), I2C_MASTER_ACK); - i2c_master_write_byte(i2c_cmd, data, I2C_MASTER_ACK); - i2c_master_stop(i2c_cmd); - esp_err_t ret = i2c_master_cmd_begin(TOUCH_I2C_PORT, i2c_cmd, 1000 / portTICK_RATE_MS); - i2c_cmd_link_delete(i2c_cmd); - return ret; + uint8_t buffer = data; + return lvgl_i2c_write(CONFIG_LV_I2C_TOUCH_PORT, slave_addr, register_addr | I2C_REG_16, &buffer, 1); } /** diff --git a/lvgl_touch/touch_driver.c b/lvgl_touch/touch_driver.c index 1212543..35fcc6b 100644 --- a/lvgl_touch/touch_driver.c +++ b/lvgl_touch/touch_driver.c @@ -4,7 +4,6 @@ #include "touch_driver.h" #include "tp_spi.h" -#include "tp_i2c.h" void touch_driver_init(void) diff --git a/lvgl_touch/tp_i2c.c b/lvgl_touch/tp_i2c.c deleted file mode 100644 index dc3371c..0000000 --- a/lvgl_touch/tp_i2c.c +++ /dev/null @@ -1,43 +0,0 @@ -/* -* Copyright © 2020 Wolfgang Christl - -* Permission is hereby granted, free of charge, to any person obtaining a copy of this -* software and associated documentation files (the “Software”), to deal in the Software -* without restriction, including without limitation the rights to use, copy, modify, merge, -* publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons -* to whom the Software is furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all copies or -* substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR -* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE -* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -*/ - -#include -#include - -#define I2C_MASTER_FREQ_HZ 100000 /* 100kHz*/ -#define I2C_MASTER_TX_BUF_DISABLE 0 /* I2C master doesn't need buffer */ -#define I2C_MASTER_RX_BUF_DISABLE 0 /* I2C master doesn't need buffer */ - -/** - * @brief ESP32 I2C init as master - * @ret ESP32 error code - */ -esp_err_t i2c_master_init(void) { - int i2c_master_port = I2C_NUM_0; - i2c_config_t conf; - conf.mode = I2C_MODE_MASTER; - conf.sda_io_num = CONFIG_LV_TOUCH_I2C_SDA; - conf.sda_pullup_en = GPIO_PULLUP_ENABLE; - conf.scl_io_num = CONFIG_LV_TOUCH_I2C_SCL; - conf.scl_pullup_en = GPIO_PULLUP_ENABLE; - conf.master.clk_speed = I2C_MASTER_FREQ_HZ; - i2c_param_config(i2c_master_port, &conf); - return i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0); -} diff --git a/lvgl_touch/tp_i2c.h b/lvgl_touch/tp_i2c.h deleted file mode 100644 index 5c1eb55..0000000 --- a/lvgl_touch/tp_i2c.h +++ /dev/null @@ -1,36 +0,0 @@ -/* -* Copyright © 2020 Wolfgang Christl - -* Permission is hereby granted, free of charge, to any person obtaining a copy of this -* software and associated documentation files (the “Software”), to deal in the Software -* without restriction, including without limitation the rights to use, copy, modify, merge, -* publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons -* to whom the Software is furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all copies or -* substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR -* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE -* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -*/ - -#ifndef __TS_H -#define __TS_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -esp_err_t i2c_master_init(void); - -#ifdef __cplusplus -} -#endif - -#endif /* __TS_H */