mcu_ab568x/app/platform/gui/tft/tft_lcd_driver.c
2025-05-30 18:03:10 +08:00

212 lines
5.5 KiB
C

#include "include.h"
static tft_driver_t *tft_driver = NULL;
// 0x02: 1CMD 1ADDR 1DATA
AT(.com_text.tft_spi)
void tft_write_cmd(u8 cmd)
{
TFT_SPI_CS_DIS();
delay_us(1);
TFT_SPI_CS_EN();
TFT_SPI_CMD_EN();
DESPICON &= ~BIT(3); // 1BIT
tft_spi_sendbyte(cmd);
}
// 0x12: 1CMD 4ADDR 4DATA
AT(.com_text.tft_spi)
void tft_write_cmd12(u8 cmd)
{
TFT_SPI_CS_DIS();
delay_us(1);
TFT_SPI_CS_EN();
DESPICON &= ~BIT(3); // 1BIT
tft_spi_sendbyte(0x12);
DESPICON |= BIT(3); // 4BIT
tft_spi_sendbyte(0x00);
tft_spi_sendbyte(cmd);
tft_spi_sendbyte(0x00);
}
// 0x03: 1CMD 1ADDR 1DATA
AT(.com_text.tft_spi)
void tft_read_cmd(u8 cmd)
{
TFT_SPI_CS_DIS();
delay_us(1);
TFT_SPI_CS_EN();
DESPICON |= BIT(3); // 1BIT
tft_spi_sendbyte(0x03);
tft_spi_sendbyte(0x00);
tft_spi_sendbyte(cmd);
tft_spi_sendbyte(0x00);
}
AT(.com_text.tft_spi)
void tft_write_data(u8 data)
{
TFT_SPI_CS_DIS();
delay_us(1);
TFT_SPI_CS_EN();
TFT_SPI_DATA_EN();
tft_spi_sendbyte(data);
}
AT(.com_text.tft_spi)
void tft_write_end(void)
{
TFT_SPI_CS_DIS();
}
AT(.com_text.tft_spi)
void tft_set_window(u16 x0, u16 y0, u16 x1, u16 y1)
{
tft_driver->set_window(x0, y0, x1, y1);
}
AT(.com_text.tft_spi)
void tft_write_data_start(void)
{
tft_write_cmd(0x2C); // TFT_RAMWR
}
AT(.com_text.tft_spi)
void tft_read_id_cmd(u8 cmd)
{
TFT_SPI_CS_DIS();
delay_us(1);
TFT_SPI_CS_EN();
DESPICON = (DESPICON & ~(0x03 << 2)) | (0x01 << 2); // 1data in 1data out
tft_spi_sendbyte(cmd);
}
void tft_dummy_clock(void)
{
GPIOAFEN &= ~BIT(4);
GPIOADE |= BIT(4);
GPIOACLR = BIT(4);
GPIOADIR &= ~BIT(4);
delay_us(1);
GPIOASET = BIT(4);
delay_us(1);
GPIOACLR = BIT(4);
GPIOAFEN |= BIT(4);
}
void tft_hw_reset(void)
{
GPIOAFEN &= ~BIT(3); // RS
GPIOADE |= BIT(3);
GPIOASET = BIT(3);
GPIOADIR &= ~BIT(3);
}
extern tft_driver_t tft_driver_240_st7789;
extern tft_driver_t tft_driver_240_JD9853;
extern tft_driver_t tft_driver_240_JD9853a;
extern tft_driver_t tft_driver_240_gcgd3030;
extern tft_driver_t tft_driver_128_xsj3023hsd;
extern tft_driver_t tft_driver_240_284_xsj3030;
extern tft_driver_t tft_driver_240_240_nv3002b;
extern tft_driver_t tft_driver_240_240_gc9307;
extern tft_driver_t tft_driver_240_296_boe7789p3;
extern tft_driver_t tft_driver_240_240_st7789p3;
extern tft_driver_t tft_driver_240_296_gc9307boe;
#if GUI_TFT_COMPATIBLE_DRIVER
#define TFT_DRIVER_NUMBER sizeof(tft_driver_compatible) / sizeof(tft_driver_compatible[0])
/* 根据需求添置自动兼容的屏驱 */
static const tft_driver_t *tft_driver_compatible[] = {
#if (GUI_SCREEN_WIDTH == 240 && GUI_SCREEN_HEIGHT == 296)
#if(FLASH_SIZE == FSIZE_4M)
&tft_driver_240_JD9853,
&tft_driver_240_296_gc9307boe,
#else
&tft_driver_240_JD9853,
&tft_driver_240_gcgd3030,
#endif
#else
&tft_driver_240_240_gc9307,
&tft_driver_240_240_st7789p3,
#endif
};
#endif
void tft_driver_init(void)
{
int32_t read_id = 0;
static uint8_t read_buf[4] = {0};
/* 配置过屏驱直接初始化 */
if (tft_driver != NULL) {
goto _tft_init;
}
/* 根据已知屏幕ID兼容选择屏驱 */
#if GUI_TFT_COMPATIBLE_DRIVER
bool tft_auto_init = false;
for (int i = 0; i < TFT_DRIVER_NUMBER; i++)
{
DESPIBAUD = 80; /*读取ID, CLK降到2M*/
tft_driver_compatible[i]->read_id(read_buf);
read_id = read_buf[0] << 16 | read_buf[1] << 8 | read_buf[2];
DESPIBAUD = sys_cb.despi_baud;
printf("tft_driver_compatible[%d]->id = %d",i,tft_driver_compatible[i]->id);
if (tft_driver_compatible[i]->id == read_id)
{
tft_driver = tft_driver_compatible[i];
tft_auto_init = true;
break;
}
}
if (tft_auto_init) {
goto _tft_init;
}
#endif
/* 未自动初始化, 采用默认屏驱初始化 */
#if (GUI_SELECT == GUI_TFT_240_ST7789)
tft_driver = &tft_driver_240_st7789;
#elif (GUI_SELECT == GUI_TFT_240_296_GCGD3030)
tft_driver = &tft_driver_240_gcgd3030;
#elif (GUI_SELECT == GUI_TFT_128_160_XSJ3023HSD)
tft_driver = &tft_driver_128_xsj3023hsd;
#elif (GUI_SELECT == GUI_TFT_240_296_JD9853)
tft_driver = &tft_driver_240_JD9853;
#elif (GUI_SELECT == GUI_TFT_240_296_JD9853A)
tft_driver = &tft_driver_240_JD9853a;
#elif (GUI_SELECT == GUI_TFT_240_284_XSJ3030)
tft_driver = &tft_driver_240_284_xsj3030;
#elif (GUI_SELECT == GUI_TFT_240_240_NV3002B1N)
tft_driver = &tft_driver_240_240_nv3002b;
#elif (GUI_SELECT == GUI_TFT_240_240_GC9307)
tft_driver = &tft_driver_240_240_gc9307;
#elif (GUI_SELECT == GUI_TFT_240_296_BOE7789P3)
tft_driver = &tft_driver_240_296_boe7789p3;
#elif (GUI_SELECT == GUI_TFT_240_240_ST7789P3)
tft_driver = &tft_driver_240_240_st7789p3;
#elif (GUI_SELECT == GUI_TFT_240_296_GC9037BOE)
tft_driver = &tft_driver_240_296_gc9307boe;
#else
#error "Please Select GUI Display"
#endif
DESPIBAUD = 80; /*读取ID, CLK降到2M*/
tft_driver->read_id(read_buf);
read_id = read_buf[0] << 16 | read_buf[1] << 8 | read_buf[2];
DESPIBAUD = sys_cb.despi_baud;
/* 已自动兼容或采用默认配置合适屏驱, 开始TFT初始化 */
_tft_init:
printf("[%s %d][ID]:0x%06x\n", __func__, __LINE__, read_id);
tft_driver->init();
}