97 lines
3.2 KiB
C
97 lines
3.2 KiB
C
#include "display.h"
|
|
|
|
#include "board_config.h"
|
|
|
|
#include "driver/ledc.h"
|
|
#include "driver/spi_master.h"
|
|
#include "esp_lcd_panel_ops.h"
|
|
#include "esp_lcd_panel_st7789.h"
|
|
#include "esp_lcd_panel_vendor.h"
|
|
#include "esp_log.h"
|
|
|
|
static const char *TAG = "display";
|
|
|
|
void display_backlight_set_brightness(uint16_t brightness)
|
|
{
|
|
if (brightness > 1023) {
|
|
brightness = 1023;
|
|
}
|
|
ESP_ERROR_CHECK(
|
|
ledc_set_duty(LEDC_LOW_SPEED_MODE, BOARD_LCD_BK_LIGHT_CH, brightness));
|
|
ESP_ERROR_CHECK(
|
|
ledc_update_duty(LEDC_LOW_SPEED_MODE, BOARD_LCD_BK_LIGHT_CH));
|
|
}
|
|
|
|
void display_backlight_on(void)
|
|
{
|
|
display_backlight_set_brightness(1023);
|
|
}
|
|
|
|
esp_err_t display_init(esp_lcd_panel_io_handle_t *io_handle_out,
|
|
esp_lcd_panel_handle_t *panel_handle_out)
|
|
{
|
|
ESP_LOGI(TAG, "Configure LCD backlight PWM");
|
|
ledc_timer_config_t ledc_timer = {
|
|
.speed_mode = LEDC_LOW_SPEED_MODE,
|
|
.timer_num = BOARD_LCD_BK_LIGHT_TIMER,
|
|
.duty_resolution = LEDC_TIMER_10_BIT,
|
|
.freq_hz = BOARD_LCD_BK_LIGHT_FREQ,
|
|
.clk_cfg = LEDC_AUTO_CLK,
|
|
};
|
|
ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
|
|
|
|
ledc_channel_config_t ledc_channel = {
|
|
.speed_mode = LEDC_LOW_SPEED_MODE,
|
|
.channel = BOARD_LCD_BK_LIGHT_CH,
|
|
.timer_sel = BOARD_LCD_BK_LIGHT_TIMER,
|
|
.intr_type = LEDC_INTR_DISABLE,
|
|
.gpio_num = BOARD_PIN_BK_LIGHT,
|
|
.duty = 0,
|
|
.hpoint = 0,
|
|
.flags.output_invert = 1,
|
|
};
|
|
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
|
|
|
|
ESP_LOGI(TAG, "Initialize SPI bus");
|
|
spi_bus_config_t buscfg = {
|
|
.sclk_io_num = BOARD_PIN_LCD_SCLK,
|
|
.mosi_io_num = BOARD_PIN_LCD_MOSI,
|
|
.miso_io_num = BOARD_PIN_LCD_MISO,
|
|
.quadwp_io_num = -1,
|
|
.quadhd_io_num = -1,
|
|
.max_transfer_sz = BOARD_LCD_H_RES * 80 * sizeof(uint16_t),
|
|
};
|
|
ESP_ERROR_CHECK(spi_bus_initialize(BOARD_LCD_HOST, &buscfg, SPI_DMA_CH_AUTO));
|
|
|
|
ESP_LOGI(TAG, "Install panel IO");
|
|
esp_lcd_panel_io_handle_t io_handle = NULL;
|
|
esp_lcd_panel_io_spi_config_t io_config = {
|
|
.dc_gpio_num = BOARD_PIN_LCD_DC,
|
|
.cs_gpio_num = BOARD_PIN_LCD_CS,
|
|
.pclk_hz = BOARD_LCD_PIXEL_CLOCK_HZ,
|
|
.lcd_cmd_bits = BOARD_LCD_CMD_BITS,
|
|
.lcd_param_bits = BOARD_LCD_PARAM_BITS,
|
|
.spi_mode = 0,
|
|
.trans_queue_depth = 10,
|
|
};
|
|
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(BOARD_LCD_HOST, &io_config, &io_handle));
|
|
|
|
esp_lcd_panel_handle_t panel_handle = NULL;
|
|
esp_lcd_panel_dev_config_t panel_config = {
|
|
.reset_gpio_num = BOARD_PIN_LCD_RST,
|
|
.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_BGR,
|
|
.bits_per_pixel = 16,
|
|
};
|
|
ESP_LOGI(TAG, "Install ST7789 panel driver");
|
|
ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle));
|
|
ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
|
|
ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
|
|
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, false, false));
|
|
ESP_ERROR_CHECK(esp_lcd_panel_set_gap(panel_handle, BOARD_LCD_GAP_X, BOARD_LCD_GAP_Y));
|
|
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
|
|
|
|
*io_handle_out = io_handle;
|
|
*panel_handle_out = panel_handle;
|
|
return ESP_OK;
|
|
}
|