luat_lcd_st7789.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "luat_base.h"
  2. #include "luat_lcd.h"
  3. #include "luat_gpio.h"
  4. #include "luat_spi.h"
  5. #include "luat_malloc.h"
  6. #include "luat_timer.h"
  7. #define LUAT_LOG_TAG "st7789"
  8. #include "luat_log.h"
  9. #define LCD_W 240
  10. #define LCD_H 320
  11. #define LCD_DIRECTION 0
  12. static int st7789_sleep(luat_lcd_conf_t* conf) {
  13. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  14. luat_timer_mdelay(5);
  15. lcd_write_cmd(conf,0x10);
  16. return 0;
  17. }
  18. static int st7789_wakeup(luat_lcd_conf_t* conf) {
  19. luat_gpio_set(conf->pin_pwr, Luat_GPIO_HIGH);
  20. luat_timer_mdelay(5);
  21. lcd_write_cmd(conf,0x11);
  22. //luat_timer_mdelay(120); // 外部休眠就好了吧
  23. return 0;
  24. }
  25. static int st7789_close(luat_lcd_conf_t* conf) {
  26. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  27. return 0;
  28. }
  29. static int st7789_init(luat_lcd_conf_t* conf) {
  30. if (conf->w == 0)
  31. conf->w = LCD_W;
  32. if (conf->h == 0)
  33. conf->h = LCD_H;
  34. if (conf->direction == 0)
  35. conf->direction = LCD_DIRECTION;
  36. luat_gpio_mode(conf->pin_dc, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH); // DC
  37. luat_gpio_mode(conf->pin_pwr, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW); // POWER
  38. luat_gpio_mode(conf->pin_rst, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW); // RST
  39. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  40. luat_gpio_set(conf->pin_rst, Luat_GPIO_LOW);
  41. luat_timer_mdelay(100);
  42. luat_gpio_set(conf->pin_rst, Luat_GPIO_HIGH);
  43. // 发送初始化命令
  44. /* Memory Data Access Control */
  45. lcd_write_cmd(conf,0x36);
  46. if(conf->direction==0)lcd_write_data(conf,0x00);
  47. else if(conf->direction==1)lcd_write_data(conf,0xC0);
  48. else if(conf->direction==2)lcd_write_data(conf,0x70);
  49. else lcd_write_data(conf,0xA0);
  50. /* RGB 5-6-5-bit */
  51. lcd_write_cmd(conf,0x3A);
  52. lcd_write_data(conf,0x65);
  53. /* Porch Setting */
  54. lcd_write_cmd(conf,0xB2);
  55. lcd_write_data(conf,0x0C);
  56. lcd_write_data(conf,0x0C);
  57. lcd_write_data(conf,0x00);
  58. lcd_write_data(conf,0x33);
  59. lcd_write_data(conf,0x33);
  60. /* Gate Control */
  61. lcd_write_cmd(conf,0xB7);
  62. lcd_write_data(conf,0x35);
  63. /* VCOM Setting */
  64. lcd_write_cmd(conf,0xBB);
  65. lcd_write_data(conf,0x19);
  66. /* LCM Control */
  67. lcd_write_cmd(conf,0xC0);
  68. lcd_write_data(conf,0x2C);
  69. /* VDV and VRH Command Enable */
  70. lcd_write_cmd(conf,0xC2);
  71. lcd_write_data(conf,0x01);
  72. /* VRH Set */
  73. lcd_write_cmd(conf,0xC3);
  74. lcd_write_data(conf,0x12);
  75. /* VDV Set */
  76. lcd_write_cmd(conf,0xC4);
  77. lcd_write_data(conf,0x20);
  78. /* Frame Rate Control in Normal Mode */
  79. lcd_write_cmd(conf,0xC6);
  80. lcd_write_data(conf,0x0F);
  81. /* Power Control 1 */
  82. lcd_write_cmd(conf,0xD0);
  83. lcd_write_data(conf,0xA4);
  84. lcd_write_data(conf,0xA1);
  85. /* Positive Voltage Gamma Control */
  86. lcd_write_cmd(conf,0xE0);
  87. lcd_write_data(conf,0xD0);
  88. lcd_write_data(conf,0x04);
  89. lcd_write_data(conf,0x0D);
  90. lcd_write_data(conf,0x11);
  91. lcd_write_data(conf,0x13);
  92. lcd_write_data(conf,0x2B);
  93. lcd_write_data(conf,0x3F);
  94. lcd_write_data(conf,0x54);
  95. lcd_write_data(conf,0x4C);
  96. lcd_write_data(conf,0x18);
  97. lcd_write_data(conf,0x0D);
  98. lcd_write_data(conf,0x0B);
  99. lcd_write_data(conf,0x1F);
  100. lcd_write_data(conf,0x23);
  101. /* Negative Voltage Gamma Control */
  102. lcd_write_cmd(conf,0xE1);
  103. lcd_write_data(conf,0xD0);
  104. lcd_write_data(conf,0x04);
  105. lcd_write_data(conf,0x0C);
  106. lcd_write_data(conf,0x11);
  107. lcd_write_data(conf,0x13);
  108. lcd_write_data(conf,0x2C);
  109. lcd_write_data(conf,0x3F);
  110. lcd_write_data(conf,0x44);
  111. lcd_write_data(conf,0x51);
  112. lcd_write_data(conf,0x2F);
  113. lcd_write_data(conf,0x1F);
  114. lcd_write_data(conf,0x1F);
  115. lcd_write_data(conf,0x20);
  116. lcd_write_data(conf,0x23);
  117. /* Display Inversion On */
  118. lcd_write_cmd(conf,0x21);
  119. /* Sleep Out */
  120. lcd_write_cmd(conf,0x11);
  121. /* wait for power stability */
  122. luat_timer_mdelay(100);
  123. luat_lcd_clear(conf,WHITE);
  124. /* display on */
  125. luat_lcd_display_on(conf);
  126. return 0;
  127. };
  128. 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) {
  129. uint32_t size = size = (x_end - x_start+1) * (y_end - y_start+1) * 2;
  130. luat_lcd_set_address(conf,x_start, y_start, x_end, y_end);
  131. luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
  132. luat_spi_send(conf->port, (const char*)color, size);
  133. return 0;
  134. }
  135. const luat_lcd_opts_t lcd_opts_st7789 = {
  136. .name = "st7789",
  137. .init = st7789_init,
  138. .close = st7789_close,
  139. .draw = st7789_draw,
  140. .sleep = st7789_sleep,
  141. .wakeup = st7789_wakeup,
  142. };