first commit

This commit is contained in:
valeh
2020-12-22 14:30:09 +02:00
commit 26b0ba5954
1832 changed files with 17777948 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
# drv
CFLAGS += -DLV_CONF_INCLUDE_SIMPLE
CFLAGS += -DILI9341_BCKL_ACTIVE_LVL=0
# Set to 1 if your display have touch support, otherwise set it to 0.
CFLAGS += -DTOUCH_SUPPORT=1
COMPONENT_SRCDIRS := .
COMPONENT_ADD_INCLUDEDIRS := $(COMPONENT_SRCDIRS) .. ../

View File

@@ -0,0 +1,124 @@
/**
* @file disp_spi.c
*
*/
/*********************
* INCLUDES
*********************/
#include "disp_spi.h"
#include "esp_system.h"
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include <string.h>
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include "freertos/task.h"
#include "lvgl/lvgl.h"
#include "ili9341.h"
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
static void IRAM_ATTR spi_ready (spi_transaction_t *trans);
/**********************
* STATIC VARIABLES
**********************/
static spi_device_handle_t spi;
static volatile bool spi_trans_in_progress;
static volatile bool spi_color_sent;
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
void disp_spi_init(void)
{
esp_err_t ret;
spi_bus_config_t buscfg={
.miso_io_num=-1,
.mosi_io_num=DISP_SPI_MOSI,
.sclk_io_num=DISP_SPI_CLK,
.quadwp_io_num=-1,
.quadhd_io_num=-1,
.max_transfer_sz = DISP_BUF_SIZE * 2,
};
spi_device_interface_config_t devcfg={
.clock_speed_hz=80*1000*1000, //Clock out at 40 MHz
.mode=0, //SPI mode 0
.spics_io_num=DISP_SPI_CS, //CS pin
.queue_size=1,
.pre_cb=NULL,
.post_cb=spi_ready,
.flags = SPI_DEVICE_HALFDUPLEX
};
//Initialize the SPI bus
ret=spi_bus_initialize(HSPI_HOST, &buscfg, 1);
assert(ret==ESP_OK);
//Attach the LCD to the SPI bus
ret=spi_bus_add_device(HSPI_HOST, &devcfg, &spi);
assert(ret==ESP_OK);
}
void disp_spi_send_data(uint8_t * data, uint16_t length)
{
if (length == 0) return; //no need to send anything
while(spi_trans_in_progress);
spi_transaction_t t = {
.length = length * 8, // transaction length is in bits
.tx_buffer = data
};
spi_trans_in_progress = true;
spi_color_sent = false; //Mark the "lv_flush_ready" NOT needs to be called in "spi_ready"
spi_device_queue_trans(spi, &t, portMAX_DELAY);
}
void disp_spi_send_colors(uint8_t * data, uint16_t length)
{
if (length == 0) return; //no need to send anything
while(spi_trans_in_progress);
spi_transaction_t t = {
.length = length * 8, // transaction length is in bits
.tx_buffer = data
};
spi_trans_in_progress = true;
spi_color_sent = true; //Mark the "lv_flush_ready" needs to be called in "spi_ready"
spi_device_queue_trans(spi, &t, portMAX_DELAY);
}
/**********************
* STATIC FUNCTIONS
**********************/
static void IRAM_ATTR spi_ready (spi_transaction_t *trans)
{
spi_trans_in_progress = false;
lv_disp_t * disp = lv_refr_get_disp_refreshing();
if(spi_color_sent) lv_disp_flush_ready(&disp->driver);
}

View File

@@ -0,0 +1,47 @@
/**
* @file disp_spi.h
*
*/
#ifndef DISP_SPI_H
#define DISP_SPI_H
#ifdef __cplusplus
extern "C" {
#endif
/*********************
* INCLUDES
*********************/
#include <stdint.h>
/*********************
* DEFINES
*********************/
#define DISP_SPI_MOSI CONFIG_OVC_PIN_MOSI
#define DISP_SPI_CLK CONFIG_OVC_PIN_CLK
#define DISP_SPI_CS CONFIG_OVC_PIN_CS
/**********************
* TYPEDEFS
**********************/
/**********************
* GLOBAL PROTOTYPES
**********************/
void disp_spi_init(void);
void disp_spi_send_data(uint8_t * data, uint16_t length);
void disp_spi_send_colors(uint8_t * data, uint16_t length);
/**********************
* MACROS
**********************/
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /*DISP_SPI_H*/

View File

@@ -0,0 +1,170 @@
/**
* @file ili9341.c
*
*/
/*********************
* INCLUDES
*********************/
#include "ili9341.h"
#include "disp_spi.h"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/*The LCD needs a bunch of command/argument values to be initialized. They are stored in this struct. */
typedef struct {
uint8_t cmd;
uint8_t data[16];
uint8_t databytes; //No of data in data; bit 7 = delay after set; 0xFF = end of cmds.
} lcd_init_cmd_t;
/**********************
* STATIC PROTOTYPES
**********************/
static void ili9341_send_cmd(uint8_t cmd);
static void ili9341_send_data(void * data, uint16_t length);
static void ili9341_send_color(void * data, uint16_t length);
/**********************
* STATIC VARIABLES
**********************/
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
void ili9341_init(void)
{
lcd_init_cmd_t ili_init_cmds[]={
{0xCF, {0x00, 0x83, 0X30}, 3},
{0xED, {0x64, 0x03, 0X12, 0X81}, 4},
{0xE8, {0x85, 0x01, 0x79}, 3},
{0xCB, {0x39, 0x2C, 0x00, 0x34, 0x02}, 5},
{0xF7, {0x20}, 1},
{0xEA, {0x00, 0x00}, 2},
{0xC0, {0x26}, 1}, /*Power control*/
{0xC1, {0x11}, 1}, /*Power control */
{0xC5, {0x35, 0x3E}, 2}, /*VCOM control*/
{0xC7, {0xBE}, 1}, /*VCOM control*/
{0x36, {0x28}, 1}, /*Memory Access Control*/
{0x3A, {0x55}, 1}, /*Pixel Format Set*/
{0xB1, {0x00, 0x1B}, 2},
{0xF2, {0x08}, 1},
{0x26, {0x01}, 1},
{0xE0, {0x1F, 0x1A, 0x18, 0x0A, 0x0F, 0x06, 0x45, 0X87, 0x32, 0x0A, 0x07, 0x02, 0x07, 0x05, 0x00}, 15},
{0XE1, {0x00, 0x25, 0x27, 0x05, 0x10, 0x09, 0x3A, 0x78, 0x4D, 0x05, 0x18, 0x0D, 0x38, 0x3A, 0x1F}, 15},
{0x2A, {0x00, 0x00, 0x00, 0xEF}, 4},
{0x2B, {0x00, 0x00, 0x01, 0x3f}, 4},
{0x2C, {0}, 0},
{0xB7, {0x07}, 1},
{0xB6, {0x0A, 0x82, 0x27, 0x00}, 4},
{0x11, {0}, 0x80},
{0x29, {0}, 0x80},
{0, {0}, 0xff},
};
//Initialize non-SPI GPIOs
gpio_set_direction(ILI9341_DC, GPIO_MODE_OUTPUT);
gpio_set_direction(ILI9341_RST, GPIO_MODE_OUTPUT);
// gpio_set_direction(ILI9341_BCKL, GPIO_MODE_OUTPUT);
//Reset the display
gpio_set_level(ILI9341_RST, 0);
vTaskDelay(100 / portTICK_RATE_MS);
gpio_set_level(ILI9341_RST, 1);
vTaskDelay(100 / portTICK_RATE_MS);
printf("ILI9341 initialization.\n");
//Send all the commands
uint16_t cmd = 0;
while (ili_init_cmds[cmd].databytes!=0xff) {
ili9341_send_cmd(ili_init_cmds[cmd].cmd);
ili9341_send_data(ili_init_cmds[cmd].data, ili_init_cmds[cmd].databytes&0x1F);
if (ili_init_cmds[cmd].databytes & 0x80) {
vTaskDelay(100 / portTICK_RATE_MS);
}
cmd++;
}
///Enable backlight
printf("Enable backlight.\n");
gpio_set_level(ILI9341_BCKL, ILI9341_BCKL_ACTIVE_LVL);
// Change screen rotation to Portrait.
uint8_t data[] = {0x88};
// this same command also sets rotation (portrait/landscape) and inverts colors.
// https://gist.github.com/motters/38a26a66020f674b6389063932048e4c#file-ili9844_defines-h-L24
ili9341_send_cmd(0x36);
ili9341_send_data(&data, 1);
}
void ili9341_flush(lv_disp_drv_t * drv, const lv_area_t * area, lv_color_t * color_map)
{
uint8_t data[4];
/*Column addresses*/
ili9341_send_cmd(0x2A);
data[0] = (area->x1 >> 8) & 0xFF;
data[1] = area->x1 & 0xFF;
data[2] = (area->x2 >> 8) & 0xFF;
data[3] = area->x2 & 0xFF;
ili9341_send_data(data, 4);
/*Page addresses*/
ili9341_send_cmd(0x2B);
data[0] = (area->y1 >> 8) & 0xFF;
data[1] = area->y1 & 0xFF;
data[2] = (area->y2 >> 8) & 0xFF;
data[3] = area->y2 & 0xFF;
ili9341_send_data(data, 4);
/*Memory write*/
ili9341_send_cmd(0x2C);
uint32_t size = lv_area_get_width(area) * lv_area_get_height(area);
ili9341_send_color((void*)color_map, size * 2);
}
/**********************
* STATIC FUNCTIONS
**********************/
static void ili9341_send_cmd(uint8_t cmd)
{
gpio_set_level(ILI9341_DC, 0); /*Command mode*/
disp_spi_send_data(&cmd, 1);
}
static void ili9341_send_data(void * data, uint16_t length)
{
gpio_set_level(ILI9341_DC, 1); /*Data mode*/
disp_spi_send_data(data, length);
}
static void ili9341_send_color(void * data, uint16_t length)
{
gpio_set_level(ILI9341_DC, 1); /*Data mode*/
disp_spi_send_colors(data, length);
}

View File

@@ -0,0 +1,49 @@
/**
* @file lv_templ.h
*
*/
#ifndef ILI9341_H
#define ILI9341_H
#ifdef __cplusplus
extern "C" {
#endif
/*********************
* INCLUDES
*********************/
#include "lvgl/lvgl.h"
/*********************
* DEFINES
*********************/
#define DISP_BUF_SIZE (LV_HOR_RES_MAX * 40)
#define ILI9341_DC CONFIG_OVC_PIN_DC
#define ILI9341_RST CONFIG_OVC_PIN_RESET
#define ILI9341_BCKL CONFIG_OVC_PIN_LITE
// if text/images are backwards, try setting this to 1
#define ILI9341_INVERT_DISPLAY 1
/**********************
* TYPEDEFS
**********************/
/**********************
* GLOBAL PROTOTYPES
**********************/
void ili9341_init(void);
void ili9341_flush(lv_disp_drv_t * drv, const lv_area_t * area, lv_color_t * color_map);
/**********************
* MACROS
**********************/
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /*ILI9341_H*/