luat_lcd_st7789.c 3.9 KB

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