Skip to content

Commit

Permalink
Pre-allocate DMA buffer during display init. (#6)
Browse files Browse the repository at this point in the history
* Pre-allocate DMA buffer during display init.

Fixes #3

* Add esp_timer dependency
  • Loading branch information
atanisoft authored Jan 6, 2023
1 parent 4f0808d commit a9d11b9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
36 changes: 21 additions & 15 deletions esp_lcd_ili9488.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ typedef struct
int y_gap;
uint8_t memory_access_control;
uint8_t color_mode;
size_t buffer_size;
uint8_t *color_buffer;
} ili9488_panel_t;

enum ili9488_constants
Expand Down Expand Up @@ -195,22 +197,12 @@ static esp_err_t panel_ili9488_draw_bitmap(
SEND_COORDS(y_start, y_end, io, LCD_CMD_RASET);

// When the ILI9488 is used in 18-bit color mode we need to convert the
// incoming color data from RGB565 (16-bit) to RGB666. This unfortunately
// requires a buffer to be allocated.
// incoming color data from RGB565 (16-bit) to RGB666.
//
// NOTE: 16-bit color does not work via SPI interface :(
if (ili9488->color_mode == ILI9488_COLOR_MODE_18BIT)
{
uint8_t *buf = NULL;
while (buf == NULL)
{
buf = (uint8_t *) heap_caps_malloc(color_data_len * 3, MALLOC_CAP_DMA);
if (buf == NULL)
{
ESP_LOGW(TAG, "Could not allocate enough DMA memory!");
}
}

uint8_t *buf = ili9488->color_buffer;
uint16_t *raw_color_data = (uint16_t *) color_data;
for (uint32_t i = 0, pixel_index = 0; i < color_data_len; i++) {
buf[pixel_index++] = (uint8_t) (((raw_color_data[i] & 0xF800) >> 8) |
Expand All @@ -221,7 +213,6 @@ static esp_err_t panel_ili9488_draw_bitmap(
}

esp_lcd_panel_io_tx_color(io, LCD_CMD_RAMWR, buf, color_data_len * 3);
heap_caps_free(buf);
}
else
{
Expand Down Expand Up @@ -331,6 +322,7 @@ static esp_err_t panel_ili9488_disp_on_off(esp_lcd_panel_t *panel, bool on_off)
esp_err_t esp_lcd_new_panel_ili9488(
const esp_lcd_panel_io_handle_t io,
const esp_lcd_panel_dev_config_t *panel_dev_config,
const size_t buffer_size,
esp_lcd_panel_handle_t *ret_panel)
{
esp_err_t ret = ESP_OK;
Expand All @@ -351,11 +343,22 @@ esp_err_t esp_lcd_new_panel_ili9488(
"configure GPIO for RESET line failed");
}

ili9488->color_mode = ILI9488_COLOR_MODE_18BIT;
if (panel_dev_config->bits_per_pixel == 16)
{
ili9488->color_mode = ILI9488_COLOR_MODE_16BIT;
}
else
{
ESP_GOTO_ON_FALSE(buffer_size > 0, ESP_ERR_INVALID_ARG, err, TAG,
"Color conversion buffer size must be specified");
ili9488->color_mode = ILI9488_COLOR_MODE_18BIT;

// Allocate DMA buffer for color conversions
ili9488->color_buffer =
(uint8_t *)heap_caps_malloc(buffer_size * 3, MALLOC_CAP_DMA);
ESP_GOTO_ON_FALSE(ili9488->color_buffer, ESP_ERR_NO_MEM, err, TAG,
"Failed to allocate DMA color conversion buffer");
}

ili9488->memory_access_control = LCD_CMD_MX_BIT | LCD_CMD_BGR_BIT;
switch (panel_dev_config->color_space)
Expand All @@ -372,7 +375,6 @@ esp_err_t esp_lcd_new_panel_ili9488(
"Unsupported color mode!");
}


ili9488->io = io;
ili9488->reset_gpio_num = panel_dev_config->reset_gpio_num;
ili9488->reset_level = panel_dev_config->flags.reset_active_high;
Expand Down Expand Up @@ -401,6 +403,10 @@ esp_err_t esp_lcd_new_panel_ili9488(
{
gpio_reset_pin(panel_dev_config->reset_gpio_num);
}
if (ili9488->color_buffer != NULL)
{
heap_caps_free(ili9488->color_buffer);
}
free(ili9488);
}
return ret;
Expand Down
2 changes: 1 addition & 1 deletion examples/lvgl/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

idf_component_register(SRCS main.c
REQUIRES driver freertos esp_lcd)
REQUIRES driver freertos esp_lcd esp_timer)
5 changes: 3 additions & 2 deletions examples/lvgl/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
#include <driver/ledc.h>
#include <driver/spi_master.h>
#include <esp_err.h>
#include <esp_freertos_hooks.h>
#include <esp_log.h>
#include <esp_lcd_panel_io.h>
#include <esp_lcd_panel_vendor.h>
#include <esp_lcd_panel_ops.h>
#include <esp_lcd_ili9488.h>
#include <esp_timer.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <esp_freertos_hooks.h>
#include <lvgl.h>
#include <stdio.h>
#include "sdkconfig.h"
Expand Down Expand Up @@ -192,7 +193,7 @@ void initialize_display()
ESP_ERROR_CHECK(
esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)SPI2_HOST, &io_config, &lcd_io_handle));

ESP_ERROR_CHECK(esp_lcd_new_panel_ili9488(lcd_io_handle, &lcd_config, &lcd_handle));
ESP_ERROR_CHECK(esp_lcd_new_panel_ili9488(lcd_io_handle, &lcd_config, LV_BUFFER_SIZE, &lcd_handle));

ESP_ERROR_CHECK(esp_lcd_panel_reset(lcd_handle));
ESP_ERROR_CHECK(esp_lcd_panel_init(lcd_handle));
Expand Down
2 changes: 1 addition & 1 deletion idf_component.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ dependencies:
idf: '>=4.4.2'
description: esp_lcd driver for ILI9488 displays
url: https://github.com/atanisoft/esp_lcd_ili9488
version: 1.0.6
version: 1.0.7
8 changes: 6 additions & 2 deletions include/esp_lcd_ili9488.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,24 @@ extern "C" {
*
* @param[in] io LCD panel IO handle
* @param[in] panel_dev_config general panel device configuration
* @param[in] buffer_size size of buffer to allocate for color conversions.
* @param[out] ret_panel Returned LCD panel handle
* @return
* - ESP_ERR_INVALID_ARG if parameter is invalid
* - ESP_ERR_NO_MEM if out of memory
* - ESP_OK on success
*
* NOTE: If you are using the SPI interface you *MUST* 18-bit color mode
* in @param panel_dev_config field bits_per_pixel.
* in @param panel_dev_config field bits_per_pixel and @param buffer_size
* must be provided.
*
* NOTE: For parallel IO (Intel 8080) interface 16-bit color mode should
* be used.
* be used and @param buffer_size will be ignored.
*/
esp_err_t esp_lcd_new_panel_ili9488(const esp_lcd_panel_io_handle_t io,
const esp_lcd_panel_dev_config_t *panel_dev_config,
const size_t buffer_size,
esp_lcd_panel_handle_t *ret_panel);

#ifdef __cplusplus
Expand Down

0 comments on commit a9d11b9

Please sign in to comment.