luat_lcd_gc9306.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 "gc9306"
  8. #include "luat_log.h"
  9. #define LCD_W 240
  10. #define LCD_H 320
  11. #define LCD_DIRECTION 0
  12. static int gc9306_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 gc9306_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 gc9306_close(luat_lcd_conf_t* conf) {
  26. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  27. return 0;
  28. }
  29. static int gc9306_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. lcd_write_cmd(conf,0xfe);
  50. lcd_write_cmd(conf,0xef);
  51. lcd_write_cmd(conf,0x36);
  52. if(conf->direction==0)lcd_write_data(conf,0x48);
  53. else if(conf->direction==1)lcd_write_data(conf,0xE8);
  54. else if(conf->direction==2)lcd_write_data(conf,0x28);
  55. else lcd_write_data(conf,0x38);
  56. lcd_write_cmd(conf,0x3a);
  57. lcd_write_data(conf,0x05);
  58. lcd_write_cmd(conf,0xa4);
  59. lcd_write_data(conf,0x44);
  60. lcd_write_data(conf,0x44);
  61. lcd_write_cmd(conf,0xa5);
  62. lcd_write_data(conf,0x42);
  63. lcd_write_data(conf,0x42);
  64. lcd_write_cmd(conf,0xaa);
  65. lcd_write_data(conf,0x88);
  66. lcd_write_data(conf,0x88);
  67. lcd_write_cmd(conf,0xe8);
  68. lcd_write_data(conf,0x11);
  69. lcd_write_data(conf,0x0b);
  70. lcd_write_cmd(conf,0xe3);
  71. lcd_write_data(conf,0x01);
  72. lcd_write_data(conf,0x10);
  73. lcd_write_cmd(conf,0xff);
  74. lcd_write_data(conf,0x61);
  75. lcd_write_cmd(conf,0xac);
  76. lcd_write_data(conf,0x00);
  77. lcd_write_cmd(conf,0xad);
  78. lcd_write_data(conf,0x33);
  79. lcd_write_cmd(conf,0xae);
  80. lcd_write_data(conf,0x2b);
  81. lcd_write_cmd(conf,0xaf);
  82. lcd_write_data(conf,0x55);
  83. lcd_write_cmd(conf,0xa6);
  84. lcd_write_data(conf,0x2a);
  85. lcd_write_data(conf,0x2a);
  86. lcd_write_cmd(conf,0xa7);
  87. lcd_write_data(conf,0x2b);
  88. lcd_write_data(conf,0x2b);
  89. lcd_write_cmd(conf,0xa8);
  90. lcd_write_data(conf,0x18);
  91. lcd_write_data(conf,0x18);
  92. lcd_write_cmd(conf,0xa9);
  93. lcd_write_data(conf,0x2a);
  94. lcd_write_data(conf,0x2a);
  95. lcd_write_cmd(conf,0x2a);
  96. lcd_write_data(conf,0x00);
  97. lcd_write_data(conf,0x00);
  98. lcd_write_data(conf,0x00);
  99. lcd_write_data(conf,0xef);
  100. lcd_write_cmd(conf,0x2b);
  101. lcd_write_data(conf,0x00);
  102. lcd_write_data(conf,0x00);
  103. lcd_write_data(conf,0x01);
  104. lcd_write_data(conf,0x3f);
  105. lcd_write_cmd(conf,0x2c);
  106. lcd_write_cmd(conf,0xF0);
  107. lcd_write_data(conf,0x02);
  108. lcd_write_data(conf,0x01);
  109. lcd_write_data(conf,0x00);
  110. lcd_write_data(conf,0x06);
  111. lcd_write_data(conf,0x09);
  112. lcd_write_data(conf,0x0c);
  113. lcd_write_cmd(conf,0xF1);
  114. lcd_write_data(conf,0x01);
  115. lcd_write_data(conf,0x03);
  116. lcd_write_data(conf,0x00);
  117. lcd_write_data(conf,0x3a);
  118. lcd_write_data(conf,0x3e);
  119. lcd_write_data(conf,0x09);
  120. lcd_write_cmd(conf,0xF2);
  121. lcd_write_data(conf,0x0c);
  122. lcd_write_data(conf,0x09);
  123. lcd_write_data(conf,0x26);
  124. lcd_write_data(conf,0x07);
  125. lcd_write_data(conf,0x07);
  126. lcd_write_data(conf,0x30);
  127. lcd_write_cmd(conf,0xF3);
  128. lcd_write_data(conf,0x09);
  129. lcd_write_data(conf,0x06);
  130. lcd_write_data(conf,0x57);
  131. lcd_write_data(conf,0x03);
  132. lcd_write_data(conf,0x03);
  133. lcd_write_data(conf,0x6b);
  134. lcd_write_cmd(conf,0xF4);
  135. lcd_write_data(conf,0x0d);
  136. lcd_write_data(conf,0x1d);
  137. lcd_write_data(conf,0x1c);
  138. lcd_write_data(conf,0x06);
  139. lcd_write_data(conf,0x08);
  140. lcd_write_data(conf,0x0F);
  141. lcd_write_cmd(conf,0xF5);
  142. lcd_write_data(conf,0x0c);
  143. lcd_write_data(conf,0x05);
  144. lcd_write_data(conf,0x06);
  145. lcd_write_data(conf,0x33);
  146. lcd_write_data(conf,0x31);
  147. lcd_write_data(conf,0x0F);
  148. /* Sleep Out */
  149. lcd_write_cmd(conf,0x11);
  150. /* wait for power stability */
  151. luat_timer_mdelay(100);
  152. luat_lcd_clear(conf,WHITE);
  153. /* display on */
  154. luat_lcd_display_on(conf);
  155. lcd_write_cmd(conf,0x2c);
  156. return 0;
  157. };
  158. // TODO 这里的color是ARGB, 需要转为lcd所需要的格式
  159. static int gc9306_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) {
  160. uint16_t i = 0, j = 0;
  161. uint32_t size = 0, size_remain = 0;
  162. uint8_t *fill_buf = NULL;
  163. size = (x_end - x_start+1) * (y_end - y_start+1) * 2;
  164. if (size > conf->w*conf->h/10)
  165. {
  166. /* the number of remaining to be filled */
  167. size_remain = size - conf->w*conf->h/10;
  168. size = conf->w*conf->h/10;
  169. }
  170. luat_lcd_set_address(conf,x_start, y_start, x_end, y_end);
  171. fill_buf = (uint8_t *)luat_heap_malloc(size);
  172. if (fill_buf)
  173. {
  174. /* fast fill */
  175. while (1)
  176. {
  177. for (i = 0; i < size / 2; i++)
  178. {
  179. fill_buf[2 * i] = color[i]>>8;
  180. //color >> 8;
  181. fill_buf[2 * i + 1] = color[i];
  182. }
  183. luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
  184. luat_spi_send(conf->port, fill_buf, size);
  185. /* Fill completed */
  186. if (size_remain == 0)
  187. break;
  188. /* calculate the number of fill next time */
  189. if (size_remain > conf->w*conf->h/10)
  190. {
  191. size_remain = size_remain - conf->w*conf->h/10;
  192. }
  193. else
  194. {
  195. size = size_remain;
  196. size_remain = 0;
  197. }
  198. }
  199. luat_heap_free(fill_buf);
  200. }
  201. else
  202. {
  203. for (i = y_start; i <= y_end; i++)
  204. {
  205. for (j = x_start; j <= x_end; j++)lcd_write_half_word(conf,color[i]);
  206. }
  207. }
  208. return 0;
  209. }
  210. const luat_lcd_opts_t lcd_opts_gc9306 = {
  211. .name = "gc9306",
  212. .init = gc9306_init,
  213. .close = gc9306_close,
  214. .draw = gc9306_draw,
  215. .sleep = gc9306_sleep,
  216. .wakeup = gc9306_wakeup,
  217. };