luat_lcd_ili9341.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 "ili9341"
  8. #include "luat_log.h"
  9. #define LCD_W 240
  10. #define LCD_H 320
  11. #define LCD_DIRECTION 0
  12. static int ili9341_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 ili9341_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 ili9341_close(luat_lcd_conf_t* conf) {
  26. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  27. return 0;
  28. }
  29. static int ili9341_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,0xCF);
  50. lcd_write_data(conf,0x00);
  51. lcd_write_data(conf,0xD9);
  52. lcd_write_data(conf,0X30);
  53. lcd_write_cmd(conf,0xED);
  54. lcd_write_data(conf,0x64);
  55. lcd_write_data(conf,0x03);
  56. lcd_write_data(conf,0X12);
  57. lcd_write_data(conf,0X81);
  58. lcd_write_cmd(conf,0xE8);
  59. lcd_write_data(conf,0x85);
  60. lcd_write_data(conf,0x10);
  61. lcd_write_data(conf,0x78);
  62. lcd_write_cmd(conf,0xCB);
  63. lcd_write_data(conf,0x39);
  64. lcd_write_data(conf,0x2C);
  65. lcd_write_data(conf,0x00);
  66. lcd_write_data(conf,0x34);
  67. lcd_write_data(conf,0x02);
  68. lcd_write_cmd(conf,0xF7);
  69. lcd_write_data(conf,0x20);
  70. lcd_write_cmd(conf,0xEA);
  71. lcd_write_data(conf,0x00);
  72. lcd_write_data(conf,0x00);
  73. lcd_write_cmd(conf,0xC0);
  74. lcd_write_data(conf,0x21);
  75. lcd_write_cmd(conf,0xC1);
  76. lcd_write_data(conf,0x12);
  77. lcd_write_cmd(conf,0xC5);
  78. lcd_write_data(conf,0x32);
  79. lcd_write_data(conf,0x3C);
  80. lcd_write_cmd(conf,0xC7);
  81. lcd_write_data(conf,0XC1);
  82. lcd_write_cmd(conf,0xC5);
  83. lcd_write_data(conf,0x1A);
  84. lcd_write_cmd(conf,0x36);
  85. lcd_write_data(conf,0x00);
  86. lcd_write_cmd(conf,0x36);
  87. if(conf->direction==0)lcd_write_data(conf,0x08);
  88. else if(conf->direction==1)lcd_write_data(conf,0xC8);
  89. else if(conf->direction==2)lcd_write_data(conf,0x78);
  90. else lcd_write_data(conf,0xA8);
  91. lcd_write_cmd(conf,0x3A);
  92. lcd_write_data(conf,0x55);
  93. lcd_write_cmd(conf,0xB1);
  94. lcd_write_data(conf,0x00);
  95. lcd_write_data(conf,0x18);
  96. lcd_write_cmd(conf,0xB6);
  97. lcd_write_data(conf,0x0A);
  98. lcd_write_data(conf,0xA2);
  99. lcd_write_cmd(conf,0xF2);
  100. lcd_write_data(conf,0x00);
  101. lcd_write_cmd(conf,0x26);
  102. lcd_write_data(conf,0x01);
  103. lcd_write_cmd(conf,0xE0);
  104. lcd_write_data(conf,0x0F);
  105. lcd_write_data(conf,0x20);
  106. lcd_write_data(conf,0x1E);
  107. lcd_write_data(conf,0x09);
  108. lcd_write_data(conf,0x12);
  109. lcd_write_data(conf,0x0B);
  110. lcd_write_data(conf,0x50);
  111. lcd_write_data(conf,0XBA);
  112. lcd_write_data(conf,0x44);
  113. lcd_write_data(conf,0x09);
  114. lcd_write_data(conf,0x14);
  115. lcd_write_data(conf,0x05);
  116. lcd_write_data(conf,0x23);
  117. lcd_write_data(conf,0x21);
  118. lcd_write_data(conf,0x00);
  119. lcd_write_cmd(conf,0XE1);
  120. lcd_write_data(conf,0x00);
  121. lcd_write_data(conf,0x19);
  122. lcd_write_data(conf,0x19);
  123. lcd_write_data(conf,0x00);
  124. lcd_write_data(conf,0x12);
  125. lcd_write_data(conf,0x07);
  126. lcd_write_data(conf,0x2D);
  127. lcd_write_data(conf,0x28);
  128. lcd_write_data(conf,0x3F);
  129. lcd_write_data(conf,0x02);
  130. lcd_write_data(conf,0x0A);
  131. lcd_write_data(conf,0x08);
  132. lcd_write_data(conf,0x25);
  133. lcd_write_data(conf,0x2D);
  134. lcd_write_data(conf,0x0F);
  135. /* Sleep Out */
  136. lcd_write_cmd(conf,0x11);
  137. /* wait for power stability */
  138. luat_timer_mdelay(100);
  139. luat_lcd_clear(conf,WHITE);
  140. /* display on */
  141. luat_lcd_display_on(conf);
  142. return 0;
  143. };
  144. // TODO 这里的color是ARGB, 需要转为lcd所需要的格式
  145. static int ili9341_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) {
  146. uint16_t i = 0, j = 0;
  147. uint32_t size = 0, size_remain = 0;
  148. uint8_t *fill_buf = NULL;
  149. size = (x_end - x_start+1) * (y_end - y_start+1) * 2;
  150. if (size > conf->w*conf->h/10)
  151. {
  152. /* the number of remaining to be filled */
  153. size_remain = size - conf->w*conf->h/10;
  154. size = conf->w*conf->h/10;
  155. }
  156. luat_lcd_set_address(conf,x_start, y_start, x_end, y_end);
  157. fill_buf = (uint8_t *)luat_heap_malloc(size);
  158. if (fill_buf)
  159. {
  160. /* fast fill */
  161. while (1)
  162. {
  163. for (i = 0; i < size / 2; i++)
  164. {
  165. fill_buf[2 * i] = color[i]>>8;
  166. //color >> 8;
  167. fill_buf[2 * i + 1] = color[i];
  168. }
  169. luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
  170. luat_spi_send(conf->port, fill_buf, size);
  171. /* Fill completed */
  172. if (size_remain == 0)
  173. break;
  174. /* calculate the number of fill next time */
  175. if (size_remain > conf->w*conf->h/10)
  176. {
  177. size_remain = size_remain - conf->w*conf->h/10;
  178. }
  179. else
  180. {
  181. size = size_remain;
  182. size_remain = 0;
  183. }
  184. }
  185. luat_heap_free(fill_buf);
  186. }
  187. else
  188. {
  189. for (i = y_start; i <= y_end; i++)
  190. {
  191. for (j = x_start; j <= x_end; j++)lcd_write_half_word(conf,color[i]);
  192. }
  193. }
  194. return 0;
  195. }
  196. const luat_lcd_opts_t lcd_opts_ili9341 = {
  197. .name = "ili9341",
  198. .init = ili9341_init,
  199. .close = ili9341_close,
  200. .draw = ili9341_draw,
  201. .sleep = ili9341_sleep,
  202. .wakeup = ili9341_wakeup,
  203. };