| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- #include "luat_base.h"
- #include "luat_lcd.h"
- #include "luat_gpio.h"
- #include "luat_spi.h"
- #include "luat_malloc.h"
- #include "luat_timer.h"
- #define LUAT_LOG_TAG "st7789"
- #include "luat_log.h"
- #define LCD_W 240
- #define LCD_H 320
- #define LCD_DIRECTION 0
- static int st7789_sleep(luat_lcd_conf_t* conf) {
- luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
- luat_timer_mdelay(5);
- lcd_write_cmd(conf,0x10);
- return 0;
- }
- static int st7789_wakeup(luat_lcd_conf_t* conf) {
- luat_gpio_set(conf->pin_pwr, Luat_GPIO_HIGH);
- luat_timer_mdelay(5);
- lcd_write_cmd(conf,0x11);
- //luat_timer_mdelay(120); // 外部休眠就好了吧
- return 0;
- }
- static int st7789_close(luat_lcd_conf_t* conf) {
- luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
- return 0;
- }
- static int st7789_init(luat_lcd_conf_t* conf) {
- if (conf->w == 0)
- conf->w = LCD_W;
- if (conf->h == 0)
- conf->h = LCD_H;
- if (conf->direction == 0)
- conf->direction = LCD_DIRECTION;
- luat_gpio_mode(conf->pin_dc, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH); // DC
- luat_gpio_mode(conf->pin_pwr, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW); // POWER
- luat_gpio_mode(conf->pin_rst, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW); // RST
- luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
- luat_gpio_set(conf->pin_rst, Luat_GPIO_LOW);
- luat_timer_mdelay(100);
- luat_gpio_set(conf->pin_rst, Luat_GPIO_HIGH);
- // 发送初始化命令
- /* Memory Data Access Control */
- lcd_write_cmd(conf,0x36);
- if(conf->direction==0)lcd_write_data(conf,0x00);
- else if(conf->direction==1)lcd_write_data(conf,0xC0);
- else if(conf->direction==2)lcd_write_data(conf,0x70);
- else lcd_write_data(conf,0xA0);
- /* RGB 5-6-5-bit */
- lcd_write_cmd(conf,0x3A);
- lcd_write_data(conf,0x65);
- /* Porch Setting */
- lcd_write_cmd(conf,0xB2);
- lcd_write_data(conf,0x0C);
- lcd_write_data(conf,0x0C);
- lcd_write_data(conf,0x00);
- lcd_write_data(conf,0x33);
- lcd_write_data(conf,0x33);
- /* Gate Control */
- lcd_write_cmd(conf,0xB7);
- lcd_write_data(conf,0x35);
- /* VCOM Setting */
- lcd_write_cmd(conf,0xBB);
- lcd_write_data(conf,0x19);
- /* LCM Control */
- lcd_write_cmd(conf,0xC0);
- lcd_write_data(conf,0x2C);
- /* VDV and VRH Command Enable */
- lcd_write_cmd(conf,0xC2);
- lcd_write_data(conf,0x01);
- /* VRH Set */
- lcd_write_cmd(conf,0xC3);
- lcd_write_data(conf,0x12);
- /* VDV Set */
- lcd_write_cmd(conf,0xC4);
- lcd_write_data(conf,0x20);
- /* Frame Rate Control in Normal Mode */
- lcd_write_cmd(conf,0xC6);
- lcd_write_data(conf,0x0F);
- /* Power Control 1 */
- lcd_write_cmd(conf,0xD0);
- lcd_write_data(conf,0xA4);
- lcd_write_data(conf,0xA1);
- /* Positive Voltage Gamma Control */
- lcd_write_cmd(conf,0xE0);
- lcd_write_data(conf,0xD0);
- lcd_write_data(conf,0x04);
- lcd_write_data(conf,0x0D);
- lcd_write_data(conf,0x11);
- lcd_write_data(conf,0x13);
- lcd_write_data(conf,0x2B);
- lcd_write_data(conf,0x3F);
- lcd_write_data(conf,0x54);
- lcd_write_data(conf,0x4C);
- lcd_write_data(conf,0x18);
- lcd_write_data(conf,0x0D);
- lcd_write_data(conf,0x0B);
- lcd_write_data(conf,0x1F);
- lcd_write_data(conf,0x23);
- /* Negative Voltage Gamma Control */
- lcd_write_cmd(conf,0xE1);
- lcd_write_data(conf,0xD0);
- lcd_write_data(conf,0x04);
- lcd_write_data(conf,0x0C);
- lcd_write_data(conf,0x11);
- lcd_write_data(conf,0x13);
- lcd_write_data(conf,0x2C);
- lcd_write_data(conf,0x3F);
- lcd_write_data(conf,0x44);
- lcd_write_data(conf,0x51);
- lcd_write_data(conf,0x2F);
- lcd_write_data(conf,0x1F);
- lcd_write_data(conf,0x1F);
- lcd_write_data(conf,0x20);
- lcd_write_data(conf,0x23);
- /* Display Inversion On */
- lcd_write_cmd(conf,0x21);
- /* Sleep Out */
- lcd_write_cmd(conf,0x11);
- /* wait for power stability */
- luat_timer_mdelay(100);
- luat_lcd_clear(conf,WHITE);
- /* display on */
- luat_lcd_display_on(conf);
- return 0;
- };
- static int st7789_draw(luat_lcd_conf_t* conf, uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end, luat_color_t* color) {
- uint32_t size = size = (x_end - x_start+1) * (y_end - y_start+1) * 2;
- luat_lcd_set_address(conf,x_start, y_start, x_end, y_end);
- luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
- luat_spi_send(conf->port, (const char*)color, size);
- return 0;
- }
- const luat_lcd_opts_t lcd_opts_st7789 = {
- .name = "st7789",
- .init = st7789_init,
- .close = st7789_close,
- .draw = st7789_draw,
- .sleep = st7789_sleep,
- .wakeup = st7789_wakeup,
- };
|