Skip to content

Commit

Permalink
bsp(esp32_p4_function_ev_board): Add deinit/remove/delete functions f…
Browse files Browse the repository at this point in the history
…or display and touch.
  • Loading branch information
espzav committed Feb 11, 2025
1 parent 35a10dc commit a6bc5a2
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 31 deletions.
92 changes: 72 additions & 20 deletions bsp/esp32_p4_function_ev_board/esp32_p4_function_ev_board.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ static i2c_master_bus_handle_t i2c_handle = NULL; // I2C Handle
static i2s_chan_handle_t i2s_tx_chan = NULL;
static i2s_chan_handle_t i2s_rx_chan = NULL;
static const audio_codec_data_if_t *i2s_data_if = NULL; /* Codec data interface */
static bsp_lcd_handles_t disp_handles;
static esp_ldo_channel_handle_t disp_phy_pwr_chan = NULL;
static esp_lcd_touch_handle_t tp = NULL;
static esp_lcd_panel_io_handle_t tp_io_handle = NULL;

/* Can be used for `i2s_std_gpio_config_t` and/or `i2s_std_config_t` initialization */
#define BSP_I2S_GPIO_CFG \
Expand Down Expand Up @@ -378,12 +382,11 @@ static esp_err_t bsp_enable_dsi_phy_power(void)
{
#if BSP_MIPI_DSI_PHY_PWR_LDO_CHAN > 0
// Turn on the power for MIPI DSI PHY, so it can go from "No Power" state to "Shutdown" state
static esp_ldo_channel_handle_t phy_pwr_chan = NULL;
esp_ldo_channel_config_t ldo_cfg = {
.chan_id = BSP_MIPI_DSI_PHY_PWR_LDO_CHAN,
.voltage_mv = BSP_MIPI_DSI_PHY_PWR_LDO_VOLTAGE_MV,
};
ESP_RETURN_ON_ERROR(esp_ldo_acquire_channel(&ldo_cfg, &phy_pwr_chan), TAG, "Acquire LDO channel for DPHY failed");
ESP_RETURN_ON_ERROR(esp_ldo_acquire_channel(&ldo_cfg, &disp_phy_pwr_chan), TAG, "Acquire LDO channel for DPHY failed");
ESP_LOGI(TAG, "MIPI DSI PHY Powered on");
#endif // BSP_MIPI_DSI_PHY_PWR_LDO_CHAN > 0

Expand Down Expand Up @@ -584,33 +587,58 @@ esp_err_t bsp_display_new_with_handles(const bsp_display_config_t *config, bsp_l

/* Return all handles */
ret_handles->io = io;
disp_handles.io = io;
#if CONFIG_BSP_LCD_TYPE_HDMI
ret_handles->io_cec = io_cec_dsi;
disp_handles.io_cec = io_cec_dsi;
ret_handles->io_avi = io_avi;
disp_handles.io_avi = io_avi;
#endif
ret_handles->mipi_dsi_bus = mipi_dsi_bus;
disp_handles.mipi_dsi_bus = mipi_dsi_bus;
ret_handles->panel = disp_panel;
disp_handles.panel = disp_panel;
ret_handles->control = NULL;
disp_handles.control = NULL;

ESP_LOGI(TAG, "Display initialized");

return ret;

err:
if (disp_panel) {
esp_lcd_panel_del(disp_panel);
bsp_display_delete();
return ret;
}

void bsp_display_delete(void)
{
if (disp_handles.panel) {
esp_lcd_panel_del(disp_handles.panel);
disp_handles.panel = NULL;
}
if (io) {
esp_lcd_panel_io_del(io);
if (disp_handles.io) {
esp_lcd_panel_io_del(disp_handles.io);
disp_handles.io = NULL;
}
#if CONFIG_BSP_LCD_TYPE_HDMI
if (io_cec_dsi) {
esp_lcd_panel_io_del(io_cec_dsi);
if (disp_handles.io_cec) {
esp_lcd_panel_io_del(disp_handles.io_cec);
disp_handles.io_cec = NULL;
}
if (io_avi) {
esp_lcd_panel_io_del(io_avi);
if (disp_handles.io_avi) {
esp_lcd_panel_io_del(disp_handles.io_avi);
disp_handles.io_avi = NULL;
}
#endif
if (mipi_dsi_bus) {
esp_lcd_del_dsi_bus(mipi_dsi_bus);
if (disp_handles.mipi_dsi_bus) {
esp_lcd_del_dsi_bus(disp_handles.mipi_dsi_bus);
disp_handles.mipi_dsi_bus = NULL;
}

if (disp_phy_pwr_chan) {
esp_ldo_release_channel(disp_phy_pwr_chan);
disp_phy_pwr_chan = NULL;
}
return ret;
}

#if !CONFIG_BSP_LCD_TYPE_HDMI
Expand Down Expand Up @@ -640,20 +668,31 @@ esp_err_t bsp_touch_new(const bsp_touch_config_t *config, esp_lcd_touch_handle_t
#endif
},
};
esp_lcd_panel_io_handle_t tp_io_handle = NULL;
esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_GT911_CONFIG();
tp_io_config.scl_speed_hz = CONFIG_BSP_I2C_CLK_SPEED_HZ;
ESP_RETURN_ON_ERROR(esp_lcd_new_panel_io_i2c(i2c_handle, &tp_io_config, &tp_io_handle), TAG, "");
return esp_lcd_touch_new_i2c_gt911(tp_io_handle, &tp_cfg, ret_touch);
}

void bsp_touch_delete(void)
{
if (tp) {
esp_lcd_touch_del(tp);
}
if (tp_io_handle) {
esp_lcd_panel_io_del(tp_io_handle);
tp_io_handle = NULL;
}

bsp_i2c_deinit();
}
#endif //!CONFIG_BSP_LCD_TYPE_HDMI

#if (BSP_CONFIG_NO_GRAPHIC_LIB == 0)
static lv_display_t *bsp_display_lcd_init(const bsp_display_cfg_t *cfg)
{
assert(cfg != NULL);
bsp_lcd_handles_t lcd_panels;
BSP_ERROR_CHECK_RETURN_NULL(bsp_display_new_with_handles(&cfg->hw_cfg, &lcd_panels));
BSP_ERROR_CHECK_RETURN_NULL(bsp_display_new_with_handles(&cfg->hw_cfg, &disp_handles));

uint32_t display_hres = 0;
uint32_t display_vres = 0;
Expand Down Expand Up @@ -692,9 +731,9 @@ static lv_display_t *bsp_display_lcd_init(const bsp_display_cfg_t *cfg)
/* Add LCD screen */
ESP_LOGD(TAG, "Add LCD screen");
const lvgl_port_display_cfg_t disp_cfg = {
.io_handle = lcd_panels.io,
.panel_handle = lcd_panels.panel,
.control_handle = lcd_panels.control,
.io_handle = disp_handles.io,
.panel_handle = disp_handles.panel,
.control_handle = disp_handles.control,
.buffer_size = cfg->buffer_size,
.double_buffer = cfg->double_buffer,
.hres = display_hres,
Expand Down Expand Up @@ -748,7 +787,6 @@ static lv_display_t *bsp_display_lcd_init(const bsp_display_cfg_t *cfg)
#if !CONFIG_BSP_LCD_TYPE_HDMI
static lv_indev_t *bsp_display_indev_init(lv_display_t *disp)
{
esp_lcd_touch_handle_t tp;
BSP_ERROR_CHECK_RETURN_NULL(bsp_touch_new(NULL, &tp));
assert(tp);

Expand Down Expand Up @@ -816,6 +854,20 @@ lv_display_t *bsp_display_start_with_config(const bsp_display_cfg_t *cfg)
return disp;
}

void bsp_display_remove(lv_display_t *display)
{
/* Deinit LVGL */
lvgl_port_remove_touch(disp_indev);
lvgl_port_remove_disp(display);
lvgl_port_deinit();

/* Deinit touch */
bsp_touch_delete();

/* Deinit display */
bsp_display_delete();
}

lv_indev_t *bsp_display_get_input_dev(void)
{
return disp_indev;
Expand Down
16 changes: 11 additions & 5 deletions bsp/esp32_p4_function_ev_board/include/bsp/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ typedef struct {
typedef struct {
esp_lcd_dsi_bus_handle_t mipi_dsi_bus; /*!< MIPI DSI bus handle */
esp_lcd_panel_io_handle_t io; /*!< ESP LCD IO handle */
#if CONFIG_BSP_LCD_TYPE_HDMI
esp_lcd_panel_io_handle_t io_cec; /*!< ESP LCD IO (HDMI CEC) handle */
esp_lcd_panel_io_handle_t io_avi; /*!< ESP LCD IO (HDMI AVI) handle */
#endif
esp_lcd_panel_handle_t panel; /*!< ESP LCD panel (color) handle */
esp_lcd_panel_handle_t control; /*!< ESP LCD panel (control) handle */
} bsp_lcd_handles_t;
Expand Down Expand Up @@ -125,13 +129,10 @@ esp_err_t bsp_display_new(const bsp_display_config_t *config, esp_lcd_panel_hand
* The display's backlight is not turned on either. You can use bsp_display_backlight_on/off(),
* bsp_display_brightness_set() (on supported boards) or implement your own backlight control.
*
* If you want to free resources allocated by this function, you can use esp_lcd API, ie.:
* If you want to free resources allocated by this function, you can use API:
*
* \code{.c}
* esp_lcd_panel_del(panel);
* esp_lcd_panel_del(control);
* esp_lcd_panel_io_del(io);
* esp_lcd_del_dsi_bus(mipi_dsi_bus);
* bsp_display_delete();
* \endcode
*
* @param[in] config display configuration
Expand All @@ -142,6 +143,11 @@ esp_err_t bsp_display_new(const bsp_display_config_t *config, esp_lcd_panel_hand
*/
esp_err_t bsp_display_new_with_handles(const bsp_display_config_t *config, bsp_lcd_handles_t *ret_handles);

/**
* @brief Delete display panel
*/
void bsp_display_delete(void);

/**
* @brief Initialize display's brightness
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -284,7 +284,7 @@ typedef struct {
/**
* @brief Initialize display
*
* This function initializes SPI, display controller and starts LVGL handling task.
* This function initializes MIPI-DSI, display controller and starts LVGL handling task.
* LCD backlight must be enabled separately by calling bsp_display_brightness_set()
*
* @return Pointer to LVGL display or NULL when error occured
Expand All @@ -294,7 +294,7 @@ lv_display_t *bsp_display_start(void);
/**
* @brief Initialize display
*
* This function initializes SPI, display controller and starts LVGL handling task.
* This function initializes MIPI-DSI, display controller and starts LVGL handling task.
* LCD backlight must be enabled separately by calling bsp_display_brightness_set()
*
* @param cfg display configuration
Expand All @@ -303,6 +303,15 @@ lv_display_t *bsp_display_start(void);
*/
lv_display_t *bsp_display_start_with_config(const bsp_display_cfg_t *cfg);

/**
* @brief Deinitialize display
*
* This function deinitializes MIPI-DSI, display controller and stops LVGL.
*
* @param @param[in] disp Pointer to LVGL display
*/
void bsp_display_remove(lv_display_t *display);

/**
* @brief Get pointer to input device (touch, buttons, ...)
*
Expand Down
11 changes: 8 additions & 3 deletions bsp/esp32_p4_function_ev_board/include/bsp/touch.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -32,10 +32,10 @@ typedef struct {
/**
* @brief Create new touchscreen
*
* If you want to free resources allocated by this function, you can use esp_lcd_touch API, ie.:
* If you want to free resources allocated by this function, you can use API:
*
* \code{.c}
* esp_lcd_touch_del(tp);
* bsp_touch_delete();
* \endcode
*
* @param[in] config touch configuration
Expand All @@ -46,6 +46,11 @@ typedef struct {
*/
esp_err_t bsp_touch_new(const bsp_touch_config_t *config, esp_lcd_touch_handle_t *ret_touch);

/**
* @brief Deinitialize touch
*/
void bsp_touch_delete(void);

#ifdef __cplusplus
}
#endif

0 comments on commit a6bc5a2

Please sign in to comment.