@@ -50,6 +50,10 @@ static i2c_master_bus_handle_t i2c_handle = NULL; // I2C Handle
5050static i2s_chan_handle_t i2s_tx_chan = NULL ;
5151static i2s_chan_handle_t i2s_rx_chan = NULL ;
5252static 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
97101esp_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+
351369esp_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
595622err :
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 )
652706static 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
749802static 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+
819892lv_indev_t * bsp_display_get_input_dev (void )
820893{
821894 return disp_indev ;
0 commit comments