Skip to content

Commit 531ad57

Browse files
authored
Merge pull request #507 from espressif/feat/bsp_deinit_display
bsp(esp32_p4_function_ev_board): Add deinit/remove/delete functions for display (BSP-598)
2 parents e5daac9 + edb30f2 commit 531ad57

File tree

5 files changed

+132
-34
lines changed

5 files changed

+132
-34
lines changed

bsp/esp32_p4_function_ev_board/esp32_p4_function_ev_board.c

Lines changed: 95 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ static i2c_master_bus_handle_t i2c_handle = NULL; // I2C Handle
5050
static i2s_chan_handle_t i2s_tx_chan = NULL;
5151
static i2s_chan_handle_t i2s_rx_chan = NULL;
5252
static const audio_codec_data_if_t *i2s_data_if = NULL; /* Codec data interface */
53+
static bsp_lcd_handles_t disp_handles;
54+
static esp_ldo_channel_handle_t disp_phy_pwr_chan = NULL;
55+
static esp_lcd_touch_handle_t tp = NULL;
56+
static esp_lcd_panel_io_handle_t tp_io_handle = NULL;
5357

5458
/* Can be used for `i2s_std_gpio_config_t` and/or `i2s_std_config_t` initialization */
5559
#define BSP_I2S_GPIO_CFG \
@@ -96,8 +100,10 @@ esp_err_t bsp_i2c_init(void)
96100

97101
esp_err_t bsp_i2c_deinit(void)
98102
{
99-
BSP_ERROR_CHECK_RETURN_ERR(i2c_del_master_bus(i2c_handle));
100-
i2c_initialized = false;
103+
if (i2c_initialized && i2c_handle) {
104+
BSP_ERROR_CHECK_RETURN_ERR(i2c_del_master_bus(i2c_handle));
105+
i2c_initialized = false;
106+
}
101107
return ESP_OK;
102108
}
103109

@@ -348,6 +354,18 @@ esp_err_t bsp_display_brightness_init(void)
348354
return ESP_OK;
349355
}
350356

357+
esp_err_t bsp_display_brightness_deinit(void)
358+
{
359+
const ledc_timer_config_t LCD_backlight_timer = {
360+
.speed_mode = LEDC_LOW_SPEED_MODE,
361+
.timer_num = 1,
362+
.deconfigure = 1
363+
};
364+
BSP_ERROR_CHECK_RETURN_ERR(ledc_timer_pause(LEDC_LOW_SPEED_MODE, 1));
365+
BSP_ERROR_CHECK_RETURN_ERR(ledc_timer_config(&LCD_backlight_timer));
366+
return ESP_OK;
367+
}
368+
351369
esp_err_t bsp_display_brightness_set(int brightness_percent)
352370
{
353371
if (brightness_percent > 100) {
@@ -378,12 +396,11 @@ static esp_err_t bsp_enable_dsi_phy_power(void)
378396
{
379397
#if BSP_MIPI_DSI_PHY_PWR_LDO_CHAN > 0
380398
// Turn on the power for MIPI DSI PHY, so it can go from "No Power" state to "Shutdown" state
381-
static esp_ldo_channel_handle_t phy_pwr_chan = NULL;
382399
esp_ldo_channel_config_t ldo_cfg = {
383400
.chan_id = BSP_MIPI_DSI_PHY_PWR_LDO_CHAN,
384401
.voltage_mv = BSP_MIPI_DSI_PHY_PWR_LDO_VOLTAGE_MV,
385402
};
386-
ESP_RETURN_ON_ERROR(esp_ldo_acquire_channel(&ldo_cfg, &phy_pwr_chan), TAG, "Acquire LDO channel for DPHY failed");
403+
ESP_RETURN_ON_ERROR(esp_ldo_acquire_channel(&ldo_cfg, &disp_phy_pwr_chan), TAG, "Acquire LDO channel for DPHY failed");
387404
ESP_LOGI(TAG, "MIPI DSI PHY Powered on");
388405
#endif // BSP_MIPI_DSI_PHY_PWR_LDO_CHAN > 0
389406

@@ -584,33 +601,60 @@ esp_err_t bsp_display_new_with_handles(const bsp_display_config_t *config, bsp_l
584601

585602
/* Return all handles */
586603
ret_handles->io = io;
604+
disp_handles.io = io;
605+
#if CONFIG_BSP_LCD_TYPE_HDMI
606+
ret_handles->io_cec = io_cec_dsi;
607+
disp_handles.io_cec = io_cec_dsi;
608+
ret_handles->io_avi = io_avi;
609+
disp_handles.io_avi = io_avi;
610+
#endif
587611
ret_handles->mipi_dsi_bus = mipi_dsi_bus;
612+
disp_handles.mipi_dsi_bus = mipi_dsi_bus;
588613
ret_handles->panel = disp_panel;
614+
disp_handles.panel = disp_panel;
589615
ret_handles->control = NULL;
616+
disp_handles.control = NULL;
590617

591618
ESP_LOGI(TAG, "Display initialized");
592619

593620
return ret;
594621

595622
err:
596-
if (disp_panel) {
597-
esp_lcd_panel_del(disp_panel);
623+
bsp_display_delete();
624+
return ret;
625+
}
626+
627+
void bsp_display_delete(void)
628+
{
629+
if (disp_handles.panel) {
630+
esp_lcd_panel_del(disp_handles.panel);
631+
disp_handles.panel = NULL;
598632
}
599-
if (io) {
600-
esp_lcd_panel_io_del(io);
633+
if (disp_handles.io) {
634+
esp_lcd_panel_io_del(disp_handles.io);
635+
disp_handles.io = NULL;
601636
}
602637
#if CONFIG_BSP_LCD_TYPE_HDMI
603-
if (io_cec_dsi) {
604-
esp_lcd_panel_io_del(io_cec_dsi);
638+
if (disp_handles.io_cec) {
639+
esp_lcd_panel_io_del(disp_handles.io_cec);
640+
disp_handles.io_cec = NULL;
605641
}
606-
if (io_avi) {
607-
esp_lcd_panel_io_del(io_avi);
642+
if (disp_handles.io_avi) {
643+
esp_lcd_panel_io_del(disp_handles.io_avi);
644+
disp_handles.io_avi = NULL;
608645
}
609646
#endif
610-
if (mipi_dsi_bus) {
611-
esp_lcd_del_dsi_bus(mipi_dsi_bus);
647+
if (disp_handles.mipi_dsi_bus) {
648+
esp_lcd_del_dsi_bus(disp_handles.mipi_dsi_bus);
649+
disp_handles.mipi_dsi_bus = NULL;
612650
}
613-
return ret;
651+
652+
if (disp_phy_pwr_chan) {
653+
esp_ldo_release_channel(disp_phy_pwr_chan);
654+
disp_phy_pwr_chan = NULL;
655+
}
656+
657+
esp_err_t err = bsp_display_brightness_deinit();
614658
}
615659

616660
#if !CONFIG_BSP_LCD_TYPE_HDMI
@@ -640,20 +684,29 @@ esp_err_t bsp_touch_new(const bsp_touch_config_t *config, esp_lcd_touch_handle_t
640684
#endif
641685
},
642686
};
643-
esp_lcd_panel_io_handle_t tp_io_handle = NULL;
644687
esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_GT911_CONFIG();
645688
tp_io_config.scl_speed_hz = CONFIG_BSP_I2C_CLK_SPEED_HZ;
646689
ESP_RETURN_ON_ERROR(esp_lcd_new_panel_io_i2c(i2c_handle, &tp_io_config, &tp_io_handle), TAG, "");
647690
return esp_lcd_touch_new_i2c_gt911(tp_io_handle, &tp_cfg, ret_touch);
648691
}
692+
693+
void bsp_touch_delete(void)
694+
{
695+
if (tp) {
696+
esp_lcd_touch_del(tp);
697+
}
698+
if (tp_io_handle) {
699+
esp_lcd_panel_io_del(tp_io_handle);
700+
tp_io_handle = NULL;
701+
}
702+
}
649703
#endif //!CONFIG_BSP_LCD_TYPE_HDMI
650704

651705
#if (BSP_CONFIG_NO_GRAPHIC_LIB == 0)
652706
static lv_display_t *bsp_display_lcd_init(const bsp_display_cfg_t *cfg)
653707
{
654708
assert(cfg != NULL);
655-
bsp_lcd_handles_t lcd_panels;
656-
BSP_ERROR_CHECK_RETURN_NULL(bsp_display_new_with_handles(&cfg->hw_cfg, &lcd_panels));
709+
BSP_ERROR_CHECK_RETURN_NULL(bsp_display_new_with_handles(&cfg->hw_cfg, &disp_handles));
657710

658711
uint32_t display_hres = 0;
659712
uint32_t display_vres = 0;
@@ -692,9 +745,9 @@ static lv_display_t *bsp_display_lcd_init(const bsp_display_cfg_t *cfg)
692745
/* Add LCD screen */
693746
ESP_LOGD(TAG, "Add LCD screen");
694747
const lvgl_port_display_cfg_t disp_cfg = {
695-
.io_handle = lcd_panels.io,
696-
.panel_handle = lcd_panels.panel,
697-
.control_handle = lcd_panels.control,
748+
.io_handle = disp_handles.io,
749+
.panel_handle = disp_handles.panel,
750+
.control_handle = disp_handles.control,
698751
.buffer_size = cfg->buffer_size,
699752
.double_buffer = cfg->double_buffer,
700753
.hres = display_hres,
@@ -748,7 +801,6 @@ static lv_display_t *bsp_display_lcd_init(const bsp_display_cfg_t *cfg)
748801
#if !CONFIG_BSP_LCD_TYPE_HDMI
749802
static lv_indev_t *bsp_display_indev_init(lv_display_t *disp)
750803
{
751-
esp_lcd_touch_handle_t tp;
752804
BSP_ERROR_CHECK_RETURN_NULL(bsp_touch_new(NULL, &tp));
753805
assert(tp);
754806

@@ -816,6 +868,27 @@ lv_display_t *bsp_display_start_with_config(const bsp_display_cfg_t *cfg)
816868
return disp;
817869
}
818870

871+
void bsp_display_stop(lv_display_t *display)
872+
{
873+
/* Deinit LVGL */
874+
#if !CONFIG_BSP_LCD_TYPE_HDMI
875+
lvgl_port_remove_touch(disp_indev);
876+
#endif
877+
lvgl_port_remove_disp(display);
878+
lvgl_port_deinit();
879+
880+
#if !CONFIG_BSP_LCD_TYPE_HDMI
881+
/* Deinit touch */
882+
bsp_touch_delete();
883+
#endif
884+
885+
/* Deinit display */
886+
bsp_display_delete();
887+
888+
/* Deinit I2C if initialized */
889+
bsp_i2c_deinit();
890+
}
891+
819892
lv_indev_t *bsp_display_get_input_dev(void)
820893
{
821894
return disp_indev;

bsp/esp32_p4_function_ev_board/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "4.2.1"
1+
version: "4.2.2"
22
description: Board Support Package (BSP) for ESP32-P4 Function EV Board (preview)
33
url: https://github.com/espressif/esp-bsp/tree/master/bsp/esp32_p4_function_ev_board
44

bsp/esp32_p4_function_ev_board/include/bsp/display.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ typedef struct {
8888
typedef struct {
8989
esp_lcd_dsi_bus_handle_t mipi_dsi_bus; /*!< MIPI DSI bus handle */
9090
esp_lcd_panel_io_handle_t io; /*!< ESP LCD IO handle */
91+
#if CONFIG_BSP_LCD_TYPE_HDMI
92+
esp_lcd_panel_io_handle_t io_cec; /*!< ESP LCD IO (HDMI CEC) handle */
93+
esp_lcd_panel_io_handle_t io_avi; /*!< ESP LCD IO (HDMI AVI) handle */
94+
#endif
9195
esp_lcd_panel_handle_t panel; /*!< ESP LCD panel (color) handle */
9296
esp_lcd_panel_handle_t control; /*!< ESP LCD panel (control) handle */
9397
} bsp_lcd_handles_t;
@@ -125,13 +129,10 @@ esp_err_t bsp_display_new(const bsp_display_config_t *config, esp_lcd_panel_hand
125129
* The display's backlight is not turned on either. You can use bsp_display_backlight_on/off(),
126130
* bsp_display_brightness_set() (on supported boards) or implement your own backlight control.
127131
*
128-
* If you want to free resources allocated by this function, you can use esp_lcd API, ie.:
132+
* If you want to free resources allocated by this function, you can use API:
129133
*
130134
* \code{.c}
131-
* esp_lcd_panel_del(panel);
132-
* esp_lcd_panel_del(control);
133-
* esp_lcd_panel_io_del(io);
134-
* esp_lcd_del_dsi_bus(mipi_dsi_bus);
135+
* bsp_display_delete();
135136
* \endcode
136137
*
137138
* @param[in] config display configuration
@@ -142,6 +143,11 @@ esp_err_t bsp_display_new(const bsp_display_config_t *config, esp_lcd_panel_hand
142143
*/
143144
esp_err_t bsp_display_new_with_handles(const bsp_display_config_t *config, bsp_lcd_handles_t *ret_handles);
144145

146+
/**
147+
* @brief Delete display panel
148+
*/
149+
void bsp_display_delete(void);
150+
145151
/**
146152
* @brief Initialize display's brightness
147153
*
@@ -153,6 +159,11 @@ esp_err_t bsp_display_new_with_handles(const bsp_display_config_t *config, bsp_l
153159
*/
154160
esp_err_t bsp_display_brightness_init(void);
155161

162+
/**
163+
* @brief Deinitialize display's brightness
164+
*/
165+
esp_err_t bsp_display_brightness_deinit(void);
166+
156167
/**
157168
* @brief Set display's brightness
158169
*

bsp/esp32_p4_function_ev_board/include/bsp/esp32_p4_function_ev_board.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -284,7 +284,7 @@ typedef struct {
284284
/**
285285
* @brief Initialize display
286286
*
287-
* This function initializes SPI, display controller and starts LVGL handling task.
287+
* This function initializes MIPI-DSI, display controller and starts LVGL handling task.
288288
* LCD backlight must be enabled separately by calling bsp_display_brightness_set()
289289
*
290290
* @return Pointer to LVGL display or NULL when error occured
@@ -294,7 +294,7 @@ lv_display_t *bsp_display_start(void);
294294
/**
295295
* @brief Initialize display
296296
*
297-
* This function initializes SPI, display controller and starts LVGL handling task.
297+
* This function initializes MIPI-DSI, display controller and starts LVGL handling task.
298298
* LCD backlight must be enabled separately by calling bsp_display_brightness_set()
299299
*
300300
* @param cfg display configuration
@@ -303,6 +303,15 @@ lv_display_t *bsp_display_start(void);
303303
*/
304304
lv_display_t *bsp_display_start_with_config(const bsp_display_cfg_t *cfg);
305305

306+
/**
307+
* @brief Deinitialize display
308+
*
309+
* This function deinitializes MIPI-DSI, display controller and stops LVGL.
310+
*
311+
* @param @param[in] disp Pointer to LVGL display
312+
*/
313+
void bsp_display_stop(lv_display_t *display);
314+
306315
/**
307316
* @brief Get pointer to input device (touch, buttons, ...)
308317
*

bsp/esp32_p4_function_ev_board/include/bsp/touch.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -32,10 +32,10 @@ typedef struct {
3232
/**
3333
* @brief Create new touchscreen
3434
*
35-
* If you want to free resources allocated by this function, you can use esp_lcd_touch API, ie.:
35+
* If you want to free resources allocated by this function, you can use API:
3636
*
3737
* \code{.c}
38-
* esp_lcd_touch_del(tp);
38+
* bsp_touch_delete();
3939
* \endcode
4040
*
4141
* @param[in] config touch configuration
@@ -46,6 +46,11 @@ typedef struct {
4646
*/
4747
esp_err_t bsp_touch_new(const bsp_touch_config_t *config, esp_lcd_touch_handle_t *ret_touch);
4848

49+
/**
50+
* @brief Deinitialize touch
51+
*/
52+
void bsp_touch_delete(void);
53+
4954
#ifdef __cplusplus
5055
}
5156
#endif

0 commit comments

Comments
 (0)