luat_lcd_st7789.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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_init(luat_lcd_conf_t* conf) {
  13. if (conf->w == 0)
  14. conf->w = LCD_W;
  15. if (conf->h == 0)
  16. conf->h = LCD_H;
  17. if (conf->direction == 0)
  18. conf->direction = LCD_DIRECTION;
  19. if (conf->pin_pwr != 255)
  20. luat_gpio_mode(conf->pin_pwr, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW); // POWER
  21. luat_gpio_mode(conf->pin_dc, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH); // DC
  22. luat_gpio_mode(conf->pin_rst, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW); // RST
  23. if (conf->pin_pwr != 255)
  24. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  25. luat_gpio_set(conf->pin_rst, Luat_GPIO_LOW);
  26. luat_timer_mdelay(100);
  27. luat_gpio_set(conf->pin_rst, Luat_GPIO_HIGH);
  28. // 发送初始化命令
  29. lcd_write_cmd(conf,0x11);
  30. luat_timer_mdelay(120);
  31. /* Memory Data Access Control */
  32. lcd_write_cmd(conf,0x36);
  33. if(conf->direction==0)lcd_write_data(conf,0x00);
  34. else if(conf->direction==1)lcd_write_data(conf,0xC0);
  35. else if(conf->direction==2)lcd_write_data(conf,0x70);
  36. else if(conf->direction==3)lcd_write_data(conf,0xA0);
  37. /* RGB 5-6-5-bit */
  38. lcd_write_cmd(conf,0x3A);
  39. lcd_write_data(conf,0x05);
  40. /* Porch Setting */
  41. lcd_write_cmd(conf,0xB2);
  42. lcd_write_data(conf,0x0C);
  43. lcd_write_data(conf,0x0C);
  44. lcd_write_data(conf,0x00);
  45. lcd_write_data(conf,0x33);
  46. lcd_write_data(conf,0x33);
  47. /* Gate Control */
  48. lcd_write_cmd(conf,0xB7);
  49. lcd_write_data(conf,0x35);
  50. /* VCOM Setting */
  51. lcd_write_cmd(conf,0xBB);
  52. lcd_write_data(conf,0x32);
  53. /* LCM Control */
  54. // lcd_write_cmd(conf,0xC0);
  55. // lcd_write_data(conf,0x2C);
  56. /* VDV and VRH Command Enable */
  57. lcd_write_cmd(conf,0xC2);
  58. lcd_write_data(conf,0x01);
  59. /* VRH Set */
  60. lcd_write_cmd(conf,0xC3);
  61. lcd_write_data(conf,0x15);
  62. /* VDV Set */
  63. lcd_write_cmd(conf,0xC4);
  64. lcd_write_data(conf,0x20);
  65. /* Frame Rate Control in Normal Mode */
  66. lcd_write_cmd(conf,0xC6);
  67. lcd_write_data(conf,0x0F);
  68. /* Power Control 1 */
  69. lcd_write_cmd(conf,0xD0);
  70. lcd_write_data(conf,0xA4);
  71. lcd_write_data(conf,0xA1);
  72. /* Positive Voltage Gamma Control */
  73. lcd_write_cmd(conf,0xE0);
  74. lcd_write_data(conf,0xD0);
  75. lcd_write_data(conf,0x08);
  76. lcd_write_data(conf,0x0E);
  77. lcd_write_data(conf,0x09);
  78. lcd_write_data(conf,0x09);
  79. lcd_write_data(conf,0x05);
  80. lcd_write_data(conf,0x31);
  81. lcd_write_data(conf,0x33);
  82. lcd_write_data(conf,0x48);
  83. lcd_write_data(conf,0x17);
  84. lcd_write_data(conf,0x14);
  85. lcd_write_data(conf,0x15);
  86. lcd_write_data(conf,0x31);
  87. lcd_write_data(conf,0x34);
  88. /* Negative Voltage Gamma Control */
  89. lcd_write_cmd(conf,0xE1);
  90. lcd_write_data(conf,0xD0);
  91. lcd_write_data(conf,0x08);
  92. lcd_write_data(conf,0x0E);
  93. lcd_write_data(conf,0x09);
  94. lcd_write_data(conf,0x09);
  95. lcd_write_data(conf,0x15);
  96. lcd_write_data(conf,0x31);
  97. lcd_write_data(conf,0x33);
  98. lcd_write_data(conf,0x48);
  99. lcd_write_data(conf,0x17);
  100. lcd_write_data(conf,0x14);
  101. lcd_write_data(conf,0x15);
  102. lcd_write_data(conf,0x31);
  103. lcd_write_data(conf,0x34);
  104. /* Display Inversion On */
  105. lcd_write_cmd(conf,0x21);
  106. /* Sleep Out */
  107. lcd_write_cmd(conf,0x11);
  108. /* wait for power stability */
  109. luat_timer_mdelay(100);
  110. luat_lcd_clear(conf,WHITE);
  111. /* display on */
  112. luat_lcd_display_on(conf);
  113. return 0;
  114. };
  115. const luat_lcd_opts_t lcd_opts_st7789 = {
  116. .name = "st7789",
  117. .init = st7789_init,
  118. };