Skip to content

Commit

Permalink
- Fixed shared SPI bus esp_3248S035
Browse files Browse the repository at this point in the history
- I2C hosts for touch
- Removed switching Little endian mapping for st7796 and ili9341
  • Loading branch information
rzeldent committed Nov 17, 2023
1 parent 2f9b003 commit f7b5e27
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 133 deletions.
7 changes: 3 additions & 4 deletions example/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
; https://docs.platformio.org/page/projectconf.html

[env]
platform = espressif32
framework = arduino

build_flags =
-Ofast
-Wall
Expand All @@ -27,9 +30,7 @@ build_flags =
-D CONFIG_XPT2046_Z_THRESHOLD=600

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino

build_flags =
${env.build_flags}
Expand All @@ -42,9 +43,7 @@ build_flags =
#-D ESP32_3248S035C # OK

[env:esp32-s3-devkitc-1]
platform = espressif32
board = esp32-s3-devkitc-1
framework = arduino

build_flags =
${env.build_flags}
Expand Down
15 changes: 15 additions & 0 deletions include/esp32_smartdisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ constexpr esp_lcd_touch_config_t xpt2046_touch_config = {
#define USES_CST816S
#include <driver/i2c.h>
#include "esp_lcd_touch_cst816s.h"
#define CST816S_I2C_HOST 0
constexpr i2c_config_t cst816s_i2c_config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = 33,
Expand Down Expand Up @@ -249,6 +250,7 @@ constexpr esp_lcd_touch_config_t xpt2046_touch_config = {
#define USES_GT911
#include <driver/i2c.h>
#include "esp_lcd_touch_gt911.h"
#define GT911_I2C_HOST 0
const i2c_config_t gt911_i2c_config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = 33,
Expand Down Expand Up @@ -353,6 +355,7 @@ constexpr esp_lcd_touch_config_t xpt2046_touch_config = {
#define USES_GT911
#include <driver/i2c.h>
#include "esp_lcd_touch_gt911.h"
#define GT911_I2C_HOST 0
const i2c_config_t gt911_i2c_config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = 19,
Expand Down Expand Up @@ -446,6 +449,7 @@ constexpr esp_lcd_touch_config_t xpt2046_touch_config = {
#define USES_GT911
#include <driver/i2c.h>
#include "esp_lcd_touch_gt911.h"
#define GT911_I2C_HOST 0
const i2c_config_t gt911_i2c_config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = 19,
Expand Down Expand Up @@ -539,6 +543,7 @@ constexpr esp_lcd_touch_config_t xpt2046_touch_config = {
#define USES_GT911
#include <driver/i2c.h>
#include "esp_lcd_touch_gt911.h"
#define GT911_I2C_HOST 0
const i2c_config_t gt911_i2c_config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = 19,
Expand Down Expand Up @@ -632,6 +637,7 @@ constexpr esp_lcd_touch_config_t xpt2046_touch_config = {
#define USES_GT911
#include <driver/i2c.h>
#include "esp_lcd_touch_gt911.h"
#define GT911_I2C_HOST 0
const i2c_config_t gt911_i2c_config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = 19,
Expand All @@ -657,6 +663,15 @@ constexpr esp_lcd_touch_config_t gt911_touch_config = {
#endif
#endif

#if defined(USES_ILI9341) || defined(USES_ST7796)
// These use an SPI interface. Because display is LSB first the option LV_COLOR_16_SWAP must be set
#if LV_COLOR_16_SWAP == 0
#error "LV_COLOR_16_SWAP should be set to 1 in lv_conf.h because of SPI interface"
#endif
#endif

// Exported functions

// Initialize the display and touch
extern void smartdisplay_init();
// Set the brightness of the backlight display
Expand Down
12 changes: 5 additions & 7 deletions src/smartdisplay_cst816s.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
#include "esp_lcd_touch.h"
#include "esp_lcd_touch_cst816s.h"

#define I2C_HOST 0

void gt911_lvgl_touch_cb(lv_indev_drv_t *drv, lv_indev_data_t *data)
void cst816s_lvgl_touch_cb(lv_indev_drv_t *drv, lv_indev_data_t *data)
{
const auto touch_handle = (esp_lcd_touch_handle_t)drv->user_data;
uint16_t touchpad_x[1] = {0};
Expand All @@ -32,12 +30,12 @@ void gt911_lvgl_touch_cb(lv_indev_drv_t *drv, lv_indev_data_t *data)
void lvgl_touch_init(lv_indev_drv_t *drv)
{
// Create I2C bus
ESP_ERROR_CHECK(i2c_param_config(I2C_HOST, &cst816s_i2c_config));
ESP_ERROR_CHECK(i2c_driver_install(I2C_HOST, cst816s_i2c_config.mode, 0, 0, 0));
ESP_ERROR_CHECK(i2c_param_config(CST816S_I2C_HOST, &cst816s_i2c_config));
ESP_ERROR_CHECK(i2c_driver_install(CST816S_I2C_HOST, cst816s_i2c_config.mode, 0, 0, 0));

// Create IO handle
esp_lcd_panel_io_handle_t io_handle;
ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)I2C_HOST, &cst816s_io_i2c_config, &io_handle));
ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)CST816S_I2C_HOST, &cst816s_io_i2c_config, &io_handle));

// Create touch configuration
esp_lcd_touch_config_t touch_config = cst816s_touch_config;
Expand All @@ -47,7 +45,7 @@ void lvgl_touch_init(lv_indev_drv_t *drv)
ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_cst816s(io_handle, &touch_config, &touch_handle));
drv->type = LV_INDEV_TYPE_POINTER;
drv->user_data = touch_handle;
drv->read_cb = gt911_lvgl_touch_cb;
drv->read_cb = cst816s_lvgl_touch_cb;
}

#endif
8 changes: 3 additions & 5 deletions src/smartdisplay_gt911.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include "esp_lcd_touch.h"
#include "esp_lcd_touch_gt911.h"

#define I2C_HOST 0

void gt911_lvgl_touch_cb(lv_indev_drv_t *drv, lv_indev_data_t *data)
{
const auto touch_handle = (esp_lcd_touch_handle_t)drv->user_data;
Expand All @@ -32,12 +30,12 @@ void gt911_lvgl_touch_cb(lv_indev_drv_t *drv, lv_indev_data_t *data)
void lvgl_touch_init(lv_indev_drv_t *drv)
{
// Create I2C bus
ESP_ERROR_CHECK(i2c_param_config(I2C_HOST, &gt911_i2c_config));
ESP_ERROR_CHECK(i2c_driver_install(I2C_HOST, gt911_i2c_config.mode, 0, 0, 0));
ESP_ERROR_CHECK(i2c_param_config(GT911_I2C_HOST, &gt911_i2c_config));
ESP_ERROR_CHECK(i2c_driver_install(GT911_I2C_HOST, gt911_i2c_config.mode, 0, 0, 0));

// Create IO handle
esp_lcd_panel_io_handle_t io_handle;
ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)I2C_HOST, &gt911_io_i2c_config, &io_handle));
ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)GT911_I2C_HOST, &gt911_io_i2c_config, &io_handle));

// Create touch configuration
esp_lcd_touch_config_t touch_config = gt911_touch_config;
Expand Down
14 changes: 4 additions & 10 deletions src/smartdisplay_ili9341.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

#include "esp_lcd_ili9341.h"

esp_lcd_panel_io_handle_t io_handle;
esp_lcd_panel_handle_t panel_handle;

bool ili9341_color_trans_done(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx)
{
const auto disp_driver = (lv_disp_drv_t *)user_ctx;
Expand All @@ -22,13 +19,6 @@ bool ili9341_color_trans_done(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_
void ili9341_lv_flush(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map)
{
const auto panel_handle = (esp_lcd_panel_handle_t)drv->user_data;
// This is really a pity. Only SPI MSB first is suported. So far unable to find a better solution.
// Ideal would to set display in LSB first
auto size = lv_area_get_width(area) * lv_area_get_height(area);
auto p = color_map;
while (size--)
p++->full = __bswap16(p->full);

ESP_ERROR_CHECK(esp_lcd_panel_draw_bitmap(panel_handle, area->x1, area->y1, area->x2 + 1, area->y2 + 1, color_map));
};

Expand All @@ -41,13 +31,17 @@ void lvgl_tft_init(lv_disp_drv_t *drv)
esp_lcd_panel_io_spi_config_t io_config = ili9341_io_spi_config;
io_config.on_color_trans_done = ili9341_color_trans_done;
io_config.user_ctx = drv;

esp_lcd_panel_io_handle_t io_handle;
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)ILI9341_SPI_HOST, &io_config, &io_handle));
// Create ili9341 panel handle
esp_lcd_panel_handle_t panel_handle;
ESP_ERROR_CHECK(esp_lcd_new_panel_ili9341(io_handle, &ili9341_panel_dev_config, &panel_handle));
ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
drv->user_data = panel_handle;
drv->flush_cb = ili9341_lv_flush;

// Turn display on
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
}
Expand Down
100 changes: 0 additions & 100 deletions src/smartdisplay_panel.cpp

This file was deleted.

1 change: 1 addition & 0 deletions src/smartdisplay_rgb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void lvgl_tft_init(lv_disp_drv_t *drv)
esp_lcd_rgb_panel_config_t tft_panel_config = esp_lcd_rgb_panel_config;
tft_panel_config.on_frame_trans_done = direct_io_frame_trans_done;
tft_panel_config.user_ctx = drv;

esp_lcd_panel_handle_t panel_handle;
ESP_ERROR_CHECK(esp_lcd_new_rgb_panel(&tft_panel_config, &panel_handle));
ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
Expand Down
9 changes: 2 additions & 7 deletions src/smartdisplay_st7796.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ bool st7796_color_trans_done(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_i
void st7796_lv_flush(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map)
{
const auto panel_handle = (esp_lcd_panel_handle_t)drv->user_data;
// This is really a pity. Only SPI MSB first is suported. So far unable to find a better solution.
// Ideal would to set display in LSB first
auto size = lv_area_get_width(area) * lv_area_get_height(area);
auto p = color_map;
while (size--)
p++->full = __bswap16(p->full);

ESP_ERROR_CHECK(esp_lcd_panel_draw_bitmap(panel_handle, area->x1, area->y1, area->x2 + 1, area->y2 + 1, color_map));
};

Expand All @@ -47,13 +40,15 @@ void lvgl_tft_init(lv_disp_drv_t *drv)
.reset_gpio_num = GPIO_NUM_NC,
.color_space = ESP_LCD_COLOR_SPACE_BGR,
.bits_per_pixel = 16};

esp_lcd_panel_handle_t panel_handle;
ESP_ERROR_CHECK(esp_lcd_new_panel_st7796(io_handle, &panel_dev_config, &panel_handle));

ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
drv->user_data = panel_handle;
drv->flush_cb = st7796_lv_flush;

// Turn display on
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
}
Expand Down
2 changes: 2 additions & 0 deletions src/smartdisplay_xpt2046.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ void xpt2046_lvgl_touch_cb(lv_indev_drv_t *drv, lv_indev_data_t *data)

void lvgl_touch_init(lv_indev_drv_t *drv)
{
#if ST7796_SPI_HOST != XPT2046_SPI_HOST
// Create SPI bus only if not already initialized (S035R shares the SPI bus)
ESP_ERROR_CHECK(spi_bus_initialize(XPT2046_SPI_HOST, &xpt2046_spi_bus_config, SPI_DMA_CH_AUTO));
#endif

// Attach the touch controller to the SPI bus
esp_lcd_panel_io_spi_config_t io_spi_config = xpt2046_io_spi_config;
Expand Down

0 comments on commit f7b5e27

Please sign in to comment.