luat_lcd_gc9106l.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "gc9106l"
  8. #include "luat_log.h"
  9. #define LCD_W 128
  10. #define LCD_H 160
  11. #define LCD_DIRECTION 0
  12. static int gc9106l_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 gc9106l_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 gc9106l_close(luat_lcd_conf_t* conf) {
  26. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  27. return 0;
  28. }
  29. static int gc9106l_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. // 配置CS脚的GPIO
  37. luat_gpio_mode(conf->pin_cs, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, 0);
  38. luat_gpio_mode(conf->pin_dc, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, 0); // DC
  39. luat_gpio_mode(conf->pin_pwr, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, 0); // POWER
  40. luat_gpio_mode(conf->pin_rst, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, 0); // RST
  41. luat_gpio_mode(conf->pin_dc, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH); // DC
  42. luat_gpio_mode(conf->pin_pwr, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW); // POWER
  43. luat_gpio_mode(conf->pin_rst, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW); // RST
  44. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  45. luat_gpio_set(conf->pin_rst, Luat_GPIO_LOW);
  46. luat_timer_mdelay(100);
  47. luat_gpio_set(conf->pin_rst, Luat_GPIO_HIGH);
  48. // 发送初始化命令
  49. //------------------------------------gc9106lS Frame Rate-----------------------------------------//
  50. lcd_write_cmd(conf,0xFE);
  51. lcd_write_cmd(conf,0xEF);
  52. lcd_write_cmd(conf,0xB3);
  53. lcd_write_data(conf,0x03);
  54. lcd_write_cmd(conf,0x21);
  55. lcd_write_cmd(conf,0x36);
  56. if(conf->direction==0)lcd_write_data(conf,0x08);
  57. else if(conf->direction==1)lcd_write_data(conf,0xC8);
  58. else if(conf->direction==2)lcd_write_data(conf,0x68);
  59. else lcd_write_data(conf,0xA8);
  60. lcd_write_cmd(conf,0x3A);
  61. lcd_write_data(conf,0x05);
  62. lcd_write_cmd(conf,0xB4);
  63. lcd_write_data(conf,0x21);
  64. lcd_write_cmd(conf,0xF0);
  65. lcd_write_data(conf,0x2D);
  66. lcd_write_data(conf,0x54);
  67. lcd_write_data(conf,0x24);
  68. lcd_write_data(conf,0x61);
  69. lcd_write_data(conf,0xAB);
  70. lcd_write_data(conf,0x2E);
  71. lcd_write_data(conf,0x2F);
  72. lcd_write_data(conf,0x00);
  73. lcd_write_data(conf,0x20);
  74. lcd_write_data(conf,0x10);
  75. lcd_write_data(conf,0X10);
  76. lcd_write_data(conf,0x17);
  77. lcd_write_data(conf,0x13);
  78. lcd_write_data(conf,0x0F);
  79. lcd_write_cmd(conf,0xF1);
  80. lcd_write_data(conf,0x02);
  81. lcd_write_data(conf,0x22);
  82. lcd_write_data(conf,0x25);
  83. lcd_write_data(conf,0x35);
  84. lcd_write_data(conf,0xA8);
  85. lcd_write_data(conf,0x08);
  86. lcd_write_data(conf,0x08);
  87. lcd_write_data(conf,0x00);
  88. lcd_write_data(conf,0x00);
  89. lcd_write_data(conf,0x09);
  90. lcd_write_data(conf,0X09);
  91. lcd_write_data(conf,0x17);
  92. lcd_write_data(conf,0x18);
  93. lcd_write_data(conf,0x0F);
  94. lcd_write_cmd(conf,0xFE);
  95. lcd_write_cmd(conf,0xFF);
  96. /* Sleep Out */
  97. lcd_write_cmd(conf,0x11);
  98. /* wait for power stability */
  99. luat_timer_mdelay(100);
  100. luat_lcd_clear(conf,WHITE);
  101. /* display on */
  102. luat_lcd_display_on(conf);
  103. return 0;
  104. };
  105. // TODO 这里的color是ARGB, 需要转为lcd所需要的格式
  106. static int gc9106l_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) {
  107. uint16_t i = 0, j = 0;
  108. uint32_t size = 0, size_remain = 0;
  109. uint8_t *fill_buf = NULL;
  110. size = (x_end - x_start+1) * (y_end - y_start+1) * 2;
  111. if (size > conf->w*conf->h/10)
  112. {
  113. /* the number of remaining to be filled */
  114. size_remain = size - conf->w*conf->h/10;
  115. size = conf->w*conf->h/10;
  116. }
  117. luat_lcd_set_address(conf,x_start, y_start, x_end, y_end);
  118. fill_buf = (uint8_t *)luat_heap_malloc(size);
  119. if (fill_buf)
  120. {
  121. /* fast fill */
  122. while (1)
  123. {
  124. for (i = 0; i < size / 2; i++)
  125. {
  126. fill_buf[2 * i] = color[i]>>8;
  127. //color >> 8;
  128. fill_buf[2 * i + 1] = color[i];
  129. }
  130. luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
  131. luat_spi_send(conf->port, fill_buf, size);
  132. /* Fill completed */
  133. if (size_remain == 0)
  134. break;
  135. /* calculate the number of fill next time */
  136. if (size_remain > conf->w*conf->h/10)
  137. {
  138. size_remain = size_remain - conf->w*conf->h/10;
  139. }
  140. else
  141. {
  142. size = size_remain;
  143. size_remain = 0;
  144. }
  145. }
  146. luat_heap_free(fill_buf);
  147. }
  148. else
  149. {
  150. for (i = y_start; i <= y_end; i++)
  151. {
  152. for (j = x_start; j <= x_end; j++)lcd_write_half_word(conf,color[i]);
  153. }
  154. }
  155. return 0;
  156. }
  157. const luat_lcd_opts_t lcd_opts_gc9106l = {
  158. .name = "gc9106l",
  159. .init = gc9106l_init,
  160. .close = gc9106l_close,
  161. .draw = gc9106l_draw,
  162. .sleep = gc9106l_sleep,
  163. .wakeup = gc9106l_wakeup,
  164. };