2020-12-17 06:02:55 +00:00
|
|
|
/**
|
|
|
|
* @file st7789.c
|
|
|
|
*
|
|
|
|
* Mostly taken from lbthomsen/esp-idf-littlevgl github.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "st7789.h"
|
|
|
|
|
|
|
|
#include "disp_spi.h"
|
2021-09-11 19:21:08 +00:00
|
|
|
#include "display_port.h"
|
2020-12-17 06:02:55 +00:00
|
|
|
|
|
|
|
/*********************
|
|
|
|
* DEFINES
|
|
|
|
*********************/
|
2021-07-30 00:05:17 +00:00
|
|
|
|
2020-12-17 06:02:55 +00:00
|
|
|
/**********************
|
|
|
|
* 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
|
|
|
|
**********************/
|
2021-08-26 23:58:36 +00:00
|
|
|
static void st7789_set_orientation(lv_disp_drv_t *drv, uint8_t orientation);
|
2021-08-26 15:42:26 +00:00
|
|
|
static void st7789_send_cmd(lv_disp_drv_t * drv, uint8_t cmd);
|
|
|
|
static void st7789_send_data(lv_disp_drv_t * drv, void *data, uint16_t length);
|
|
|
|
static void st7789_send_color(lv_disp_drv_t * drv, void *data, uint16_t length);
|
|
|
|
static void st7789_reset(lv_disp_drv_t * drv);
|
2020-12-17 06:02:55 +00:00
|
|
|
|
2022-05-13 03:15:34 +00:00
|
|
|
static void setup_initial_offsets(lv_disp_drv_t * drv);
|
|
|
|
static lv_coord_t get_display_hor_res(lv_disp_drv_t * drv);
|
|
|
|
static lv_coord_t get_display_ver_res(lv_disp_drv_t * drv);
|
2020-12-17 06:02:55 +00:00
|
|
|
/**********************
|
|
|
|
* STATIC VARIABLES
|
|
|
|
**********************/
|
2022-01-06 06:05:03 +00:00
|
|
|
static uint16_t user_x_offset = 0u;
|
|
|
|
static uint16_t user_y_offset = 0u;
|
2020-12-17 06:02:55 +00:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* MACROS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* GLOBAL FUNCTIONS
|
|
|
|
**********************/
|
2021-08-26 15:42:26 +00:00
|
|
|
void st7789_init(lv_disp_drv_t *drv)
|
2020-12-17 06:02:55 +00:00
|
|
|
{
|
2022-05-13 03:15:34 +00:00
|
|
|
setup_initial_offsets(drv);
|
2022-01-06 06:05:03 +00:00
|
|
|
|
2020-12-17 06:02:55 +00:00
|
|
|
lcd_init_cmd_t st7789_init_cmds[] = {
|
|
|
|
{0xCF, {0x00, 0x83, 0X30}, 3},
|
|
|
|
{0xED, {0x64, 0x03, 0X12, 0X81}, 4},
|
|
|
|
{ST7789_PWCTRL2, {0x85, 0x01, 0x79}, 3},
|
|
|
|
{0xCB, {0x39, 0x2C, 0x00, 0x34, 0x02}, 5},
|
|
|
|
{0xF7, {0x20}, 1},
|
|
|
|
{0xEA, {0x00, 0x00}, 2},
|
|
|
|
{ST7789_LCMCTRL, {0x26}, 1},
|
|
|
|
{ST7789_IDSET, {0x11}, 1},
|
|
|
|
{ST7789_VCMOFSET, {0x35, 0x3E}, 2},
|
|
|
|
{ST7789_CABCCTRL, {0xBE}, 1},
|
|
|
|
{ST7789_MADCTL, {0x00}, 1}, // Set to 0x28 if your display is flipped
|
|
|
|
{ST7789_COLMOD, {0x55}, 1},
|
2021-02-12 09:18:01 +00:00
|
|
|
|
|
|
|
#if ST7789_INVERT_COLORS == 1
|
|
|
|
{ST7789_INVON, {0}, 0}, // set inverted mode
|
|
|
|
#else
|
|
|
|
{ST7789_INVOFF, {0}, 0}, // set non-inverted mode
|
|
|
|
#endif
|
|
|
|
|
2020-12-17 06:02:55 +00:00
|
|
|
{ST7789_RGBCTRL, {0x00, 0x1B}, 2},
|
|
|
|
{0xF2, {0x08}, 1},
|
|
|
|
{ST7789_GAMSET, {0x01}, 1},
|
|
|
|
{ST7789_PVGAMCTRL, {0xD0, 0x00, 0x02, 0x07, 0x0A, 0x28, 0x32, 0x44, 0x42, 0x06, 0x0E, 0x12, 0x14, 0x17}, 14},
|
|
|
|
{ST7789_NVGAMCTRL, {0xD0, 0x00, 0x02, 0x07, 0x0A, 0x28, 0x31, 0x54, 0x47, 0x0E, 0x1C, 0x17, 0x1B, 0x1E}, 14},
|
|
|
|
{ST7789_CASET, {0x00, 0x00, 0x00, 0xEF}, 4},
|
|
|
|
{ST7789_RASET, {0x00, 0x00, 0x01, 0x3f}, 4},
|
|
|
|
{ST7789_RAMWR, {0}, 0},
|
|
|
|
{ST7789_GCTRL, {0x07}, 1},
|
|
|
|
{0xB6, {0x0A, 0x82, 0x27, 0x00}, 4},
|
|
|
|
{ST7789_SLPOUT, {0}, 0x80},
|
|
|
|
{ST7789_DISPON, {0}, 0x80},
|
|
|
|
{0, {0}, 0xff},
|
|
|
|
};
|
|
|
|
|
2021-08-26 15:42:26 +00:00
|
|
|
st7789_reset(drv);
|
2020-12-17 06:02:55 +00:00
|
|
|
|
|
|
|
//Send all the commands
|
|
|
|
uint16_t cmd = 0;
|
|
|
|
while (st7789_init_cmds[cmd].databytes!=0xff) {
|
2021-08-26 15:42:26 +00:00
|
|
|
st7789_send_cmd(drv, st7789_init_cmds[cmd].cmd);
|
|
|
|
st7789_send_data(drv, st7789_init_cmds[cmd].data, st7789_init_cmds[cmd].databytes&0x1F);
|
2020-12-17 06:02:55 +00:00
|
|
|
if (st7789_init_cmds[cmd].databytes & 0x80) {
|
2021-09-11 19:21:08 +00:00
|
|
|
display_port_delay(drv, 100);
|
2020-12-17 06:02:55 +00:00
|
|
|
}
|
|
|
|
cmd++;
|
|
|
|
}
|
|
|
|
|
2022-05-13 03:15:34 +00:00
|
|
|
/* NOTE: Setting rotation from lv_disp_drv_t instead of menuconfig */
|
|
|
|
lv_disp_rot_t rotation;
|
|
|
|
#if (LVGL_VERSION_MAJOR >= 8)
|
|
|
|
rotation = lv_disp_get_rotation((lv_disp_t *) &drv);
|
|
|
|
#else
|
|
|
|
rotation = lv_disp_get_rotation((lv_disp_t *) drv);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
st7789_set_orientation(drv, rotation);
|
2020-12-17 06:02:55 +00:00
|
|
|
}
|
|
|
|
|
2021-12-23 19:32:23 +00:00
|
|
|
/* The ST7789 display controller can drive up to 320*240 displays, when using a 240*240 or 240*135
|
|
|
|
* displays there's a gap of 80px or 40/52/53px respectively. 52px or 53x offset depends on display orientation.
|
|
|
|
* We need to edit the coordinates to take into account those gaps, this is not necessary in all orientations. */
|
2020-12-17 06:02:55 +00:00
|
|
|
void st7789_flush(lv_disp_drv_t * drv, const lv_area_t * area, lv_color_t * color_map)
|
|
|
|
{
|
|
|
|
uint8_t data[4] = {0};
|
|
|
|
|
|
|
|
uint16_t offsetx1 = area->x1;
|
|
|
|
uint16_t offsetx2 = area->x2;
|
|
|
|
uint16_t offsety1 = area->y1;
|
|
|
|
uint16_t offsety2 = area->y2;
|
2021-08-26 15:42:26 +00:00
|
|
|
uint32_t size = lv_area_get_width(area) * lv_area_get_height(area);
|
2020-12-17 06:02:55 +00:00
|
|
|
|
2022-05-13 03:15:34 +00:00
|
|
|
/* On LVGLv7 we have to manually update the driver orientation,
|
|
|
|
* in LVGLv8 we use the driver update callback. */
|
|
|
|
#if (LVGL_VERSION_MAJOR < 8)
|
|
|
|
static lv_disp_rot_t cached_rotation = LV_DISP_ROT_NONE;
|
|
|
|
lv_disp_rot_t rotation = lv_disp_get_rotation((lv_disp_t *) drv);
|
|
|
|
if (cached_rotation != rotation) {
|
|
|
|
st7789_set_orientation(drv, (uint8_t) rotation);
|
|
|
|
/* Update offset values */
|
|
|
|
setup_initial_offsets(drv);
|
|
|
|
cached_rotation = rotation;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-01-06 06:05:03 +00:00
|
|
|
offsetx1 += st7789_x_offset();
|
|
|
|
offsetx2 += st7789_x_offset();
|
|
|
|
offsety1 += st7789_y_offset();
|
|
|
|
offsety2 += st7789_y_offset();
|
2020-12-17 06:02:55 +00:00
|
|
|
|
|
|
|
/*Column addresses*/
|
2021-08-26 15:42:26 +00:00
|
|
|
st7789_send_cmd(drv, ST7789_CASET);
|
2020-12-17 06:02:55 +00:00
|
|
|
data[0] = (offsetx1 >> 8) & 0xFF;
|
|
|
|
data[1] = offsetx1 & 0xFF;
|
|
|
|
data[2] = (offsetx2 >> 8) & 0xFF;
|
|
|
|
data[3] = offsetx2 & 0xFF;
|
2021-08-26 15:42:26 +00:00
|
|
|
st7789_send_data(drv, data, 4);
|
2020-12-17 06:02:55 +00:00
|
|
|
|
|
|
|
/*Page addresses*/
|
2021-08-26 15:42:26 +00:00
|
|
|
st7789_send_cmd(drv, ST7789_RASET);
|
2020-12-17 06:02:55 +00:00
|
|
|
data[0] = (offsety1 >> 8) & 0xFF;
|
|
|
|
data[1] = offsety1 & 0xFF;
|
|
|
|
data[2] = (offsety2 >> 8) & 0xFF;
|
|
|
|
data[3] = offsety2 & 0xFF;
|
2021-08-26 15:42:26 +00:00
|
|
|
st7789_send_data(drv, data, 4);
|
2020-12-17 06:02:55 +00:00
|
|
|
|
|
|
|
/*Memory write*/
|
2021-08-26 15:42:26 +00:00
|
|
|
st7789_send_cmd(drv, ST7789_RAMWR);
|
|
|
|
st7789_send_color(drv, (void*) color_map, size * 2);
|
2020-12-17 06:02:55 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 06:05:03 +00:00
|
|
|
void st7789_set_x_offset(const uint16_t offset)
|
|
|
|
{
|
|
|
|
user_x_offset = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void st7789_set_y_offset(const uint16_t offset)
|
|
|
|
{
|
|
|
|
user_y_offset = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t st7789_x_offset(void)
|
|
|
|
{
|
|
|
|
return user_x_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t st7789_y_offset(void)
|
|
|
|
{
|
|
|
|
return user_y_offset;
|
|
|
|
}
|
|
|
|
|
2020-12-17 06:02:55 +00:00
|
|
|
/**********************
|
|
|
|
* STATIC FUNCTIONS
|
|
|
|
**********************/
|
2021-08-26 15:42:26 +00:00
|
|
|
static void st7789_send_cmd(lv_disp_drv_t *drv, uint8_t cmd)
|
2020-12-17 06:02:55 +00:00
|
|
|
{
|
|
|
|
disp_wait_for_pending_transactions();
|
2021-09-11 19:21:08 +00:00
|
|
|
display_port_gpio_dc(drv, 0);
|
2021-08-26 23:58:36 +00:00
|
|
|
disp_spi_send_data(&cmd, 1);
|
2020-12-17 06:02:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-26 15:42:26 +00:00
|
|
|
static void st7789_send_data(lv_disp_drv_t *drv, void * data, uint16_t length)
|
2020-12-17 06:02:55 +00:00
|
|
|
{
|
|
|
|
disp_wait_for_pending_transactions();
|
2021-09-11 19:21:08 +00:00
|
|
|
display_port_gpio_dc(drv, 1);
|
2021-08-26 23:58:36 +00:00
|
|
|
disp_spi_send_data(data, length);
|
2020-12-17 06:02:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-26 15:42:26 +00:00
|
|
|
static void st7789_send_color(lv_disp_drv_t *drv, void * data, uint16_t length)
|
2020-12-17 06:02:55 +00:00
|
|
|
{
|
|
|
|
disp_wait_for_pending_transactions();
|
2021-09-11 19:21:08 +00:00
|
|
|
display_port_gpio_dc(drv, 1);
|
2021-08-26 23:58:36 +00:00
|
|
|
disp_spi_send_colors(data, length);
|
2020-12-17 06:02:55 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 14:29:32 +00:00
|
|
|
/* Reset the display, if we don't have a reset pin we use software reset */
|
2021-08-26 15:42:26 +00:00
|
|
|
static void st7789_reset(lv_disp_drv_t *drv)
|
2021-05-17 14:29:32 +00:00
|
|
|
{
|
2021-09-09 04:08:18 +00:00
|
|
|
#if !defined(ST7789_SOFT_RST)
|
2021-09-11 19:21:08 +00:00
|
|
|
display_port_gpio_rst(drv, 0);
|
|
|
|
display_port_delay(drv, 100);
|
|
|
|
display_port_gpio_rst(drv, 1);
|
|
|
|
display_port_delay(drv, 100);
|
2021-05-17 14:29:32 +00:00
|
|
|
#else
|
2021-08-26 15:42:26 +00:00
|
|
|
st7789_send_cmd(drv, ST7789_SWRESET);
|
2021-09-11 19:21:08 +00:00
|
|
|
display_port_delay(drv, 5);
|
2021-05-17 14:29:32 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-08-26 15:42:26 +00:00
|
|
|
static void st7789_set_orientation(lv_disp_drv_t *drv, uint8_t orientation)
|
2020-12-17 06:02:55 +00:00
|
|
|
{
|
2021-02-12 09:18:01 +00:00
|
|
|
uint8_t data[] =
|
2020-12-17 06:02:55 +00:00
|
|
|
{
|
|
|
|
#if CONFIG_LV_PREDEFINED_DISPLAY_TTGO
|
|
|
|
0x60, 0xA0, 0x00, 0xC0
|
|
|
|
#else
|
2022-05-13 03:15:34 +00:00
|
|
|
0xC0, 0x60, 0x00, 0xA0
|
2020-12-17 06:02:55 +00:00
|
|
|
#endif
|
|
|
|
};
|
2021-02-12 09:18:01 +00:00
|
|
|
|
2021-08-26 15:42:26 +00:00
|
|
|
st7789_send_cmd(drv, ST7789_MADCTL);
|
|
|
|
st7789_send_data(drv, (void *) &data[orientation], 1);
|
2020-12-17 06:02:55 +00:00
|
|
|
}
|
2021-05-17 14:29:32 +00:00
|
|
|
|
2022-05-13 03:15:34 +00:00
|
|
|
static void setup_initial_offsets(lv_disp_drv_t * drv)
|
2022-01-06 06:05:03 +00:00
|
|
|
{
|
2022-05-13 03:15:34 +00:00
|
|
|
lv_disp_rot_t rotation;
|
|
|
|
#if (LVGL_VERSION_MAJOR >= 8)
|
|
|
|
rotation = lv_disp_get_rotation((lv_disp_t *) &drv);
|
|
|
|
#else
|
|
|
|
rotation = lv_disp_get_rotation((lv_disp_t *) drv);
|
|
|
|
#endif
|
|
|
|
|
2022-01-06 06:05:03 +00:00
|
|
|
#if (CONFIG_LV_TFT_DISPLAY_OFFSETS)
|
|
|
|
st7789_set_x_offset(CONFIG_LV_TFT_DISPLAY_X_OFFSET);
|
|
|
|
st7789_set_y_offset(CONFIG_LV_TFT_DISPLAY_Y_OFFSET);
|
2022-05-13 03:15:34 +00:00
|
|
|
#else
|
|
|
|
if (240U == get_display_hor_res(drv) && 135U == get_display_ver_res(drv))
|
|
|
|
{
|
|
|
|
if (LV_DISP_ROT_NONE == rotation || LV_DISP_ROT_180 == rotation)
|
|
|
|
{
|
|
|
|
st7789_set_x_offset(40);
|
|
|
|
st7789_set_y_offset(53);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
st7789_set_x_offset(0);
|
|
|
|
st7789_set_y_offset(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (135U == get_display_hor_res(drv) && 240U == get_display_ver_res(drv))
|
|
|
|
{
|
|
|
|
if (LV_DISP_ROT_90 == rotation || LV_DISP_ROT_270 == rotation)
|
|
|
|
{
|
|
|
|
st7789_set_x_offset(52);
|
|
|
|
st7789_set_y_offset(40);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
st7789_set_x_offset(0);
|
|
|
|
st7789_set_y_offset(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (240U == get_display_hor_res(drv) && 240U == get_display_ver_res(drv))
|
|
|
|
{
|
|
|
|
if (LV_DISP_ROT_NONE == rotation)
|
|
|
|
{
|
|
|
|
st7789_set_x_offset(80);
|
|
|
|
st7789_set_y_offset(0);
|
|
|
|
}
|
|
|
|
else if (LV_DISP_ROT_90 == rotation || LV_DISP_ROT_180 == rotation)
|
|
|
|
{
|
|
|
|
st7789_set_x_offset(0);
|
|
|
|
st7789_set_y_offset(0);
|
|
|
|
}
|
|
|
|
else if (LV_DISP_ROT_270 == rotation)
|
|
|
|
{
|
|
|
|
st7789_set_x_offset(0);
|
|
|
|
st7789_set_y_offset(80);
|
|
|
|
}
|
|
|
|
}
|
2022-01-06 06:05:03 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-09-10 00:36:32 +00:00
|
|
|
/* Display update callback, we could update the orientation in here
|
|
|
|
* NOTE Available only for LVGL v8 */
|
2021-09-09 03:59:52 +00:00
|
|
|
void st7789_update_cb(lv_disp_drv_t *drv)
|
|
|
|
{
|
2022-05-13 03:15:34 +00:00
|
|
|
lv_disp_rot_t rotation;
|
|
|
|
#if (LVGL_VERSION_MAJOR >= 8)
|
|
|
|
rotation = lv_disp_get_rotation((lv_disp_t *) &drv);
|
|
|
|
#else
|
|
|
|
rotation = lv_disp_get_rotation((lv_disp_t *) drv);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
st7789_set_orientation(drv, (uint8_t) rotation);
|
|
|
|
setup_initial_offsets(drv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static lv_coord_t get_display_hor_res(lv_disp_drv_t * drv)
|
|
|
|
{
|
|
|
|
lv_coord_t retval = 0;
|
|
|
|
|
|
|
|
#if (LVGL_VERSION_MAJOR >= 8)
|
|
|
|
retval = drv->hor_res;
|
|
|
|
#else
|
2021-09-09 03:59:52 +00:00
|
|
|
(void) drv;
|
2022-05-13 03:15:34 +00:00
|
|
|
retval = LV_HOR_RES_MAX;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static lv_coord_t get_display_ver_res(lv_disp_drv_t * drv)
|
|
|
|
{
|
|
|
|
lv_coord_t retval = 0;
|
|
|
|
|
|
|
|
#if (LVGL_VERSION_MAJOR >= 8)
|
|
|
|
retval = drv->ver_res;
|
|
|
|
#else
|
|
|
|
(void) drv;
|
|
|
|
retval = LV_VER_RES_MAX;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return retval;
|
2021-09-09 03:59:52 +00:00
|
|
|
}
|