ili9341-spi.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * lws abstract display implementation for ili9341 on spi
  3. *
  4. * Copyright (C) 2019 - 2020 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include <private-lib-core.h>
  25. #include <drivers/devices/display/ili9341.h>
  26. static uint8_t ili9341_320x240_init[] = {
  27. /*
  28. * This provides 70Hz 320x240 at RGB565, we assume im[3:0] is 1110
  29. * which is 4-bit SPI
  30. */
  31. 3, ILI9341_FACPWCTRB, 0x00, 0x83, 0x30,
  32. 4, ILI9341_FACDRTIMCTRA, 0x64, 0x03, 0x12, 0x81,
  33. 3, ILI9341_FACPWCTRA, 0x85, 0x01, 0x79,
  34. 5, ILI9341_FACPUMPRAT, 0x39, 0x2c, 0x00, 0x34, 0x02,
  35. 1, ILI9341_FACDRTIMCTR, 0x20,
  36. 2, ILI9341_FACPWCTR1, 0x00, 0x00,
  37. 1, ILI9341_PWCTR1, 0x26,
  38. 1, ILI9341_PWCTR2, 0x11,
  39. 2, ILI9341_VMCTR1, 0x35, 0x3e,
  40. 1, ILI9341_VMCTR2, 0xbe,
  41. 1, ILI9341_MADCTL, 0x28,
  42. 1, ILI9341_VSCRSADD, 0x00,
  43. 1, ILI9341_PIXFMT, 0x55,
  44. 2, ILI9341_FRMCTR1, 0x00, 0x1b,
  45. 1, ILI9341_FACSETGAMMACRV, 0x00,
  46. 1, ILI9341_GAMMASET, 0x01,
  47. 15, ILI9341_GMCTRP1, 0x0f, 0x31, 0x2b, 0x0c, 0x0e, 0x08, 0x4e,
  48. 0xf1, 0x37, 0x07, 0x10, 0x03, 0x0e, 0x09,
  49. 0x00,
  50. 15, ILI9341_GMCTRN1, 0x00, 0x0e, 0x14, 0x03, 0x11, 0x07, 0x31,
  51. 0xc1, 0x48, 0x08, 0x0f, 0x0c, 0x31, 0x36,
  52. 0x0f,
  53. 4, ILI9341_DFUNCTR, 0x0a, 0x82, 0x27, 0x00,
  54. };
  55. int
  56. lws_display_ili9341_spi_init(const struct lws_display *disp)
  57. {
  58. const lws_display_ili9341_t *ili = (const lws_display_ili9341_t *)disp;
  59. lws_spi_desc_t desc;
  60. size_t pos = 0;
  61. uint8_t u[8];
  62. lwsl_user("%s\n", __func__);
  63. /* hardware nRESET */
  64. if (ili->gpio) {
  65. ili->gpio->mode(ili->reset_gpio, LWSGGPIO_FL_WRITE |
  66. LWSGGPIO_FL_PULLUP);
  67. ili->gpio->set(ili->reset_gpio, 0);
  68. lws_msleep(1);
  69. ili->gpio->set(ili->reset_gpio, 1);
  70. lws_msleep(1);
  71. }
  72. /*
  73. * We cut the init table up into transactions... atm we just go with
  74. * the fact that bb spi is synchronous, using async / dma we can't use
  75. * a single desc on the stack like this
  76. */
  77. memset(&desc, 0, sizeof(desc));
  78. desc.count_cmd = 1;
  79. while (pos < LWS_ARRAY_SIZE(ili9341_320x240_init)) {
  80. desc.count_write = ili9341_320x240_init[pos++];
  81. desc.src = &ili9341_320x240_init[pos++];
  82. desc.data = &ili9341_320x240_init[pos];
  83. pos += desc.count_write;
  84. ili->spi->queue(ili->spi, &desc);
  85. }
  86. u[0] = ILI9341_SLPOUT;
  87. desc.src = &u[0];
  88. desc.count_write = 0;
  89. ili->spi->queue(ili->spi, &desc);
  90. lws_msleep(5);
  91. u[0] = ILI9341_DISPON;
  92. ili->spi->queue(ili->spi, &desc);
  93. return 0;
  94. }
  95. /* backlight handled by PWM */
  96. int
  97. lws_display_ili9341_spi_brightness(const struct lws_display *disp, uint8_t b)
  98. {
  99. return 0;
  100. }
  101. int
  102. lws_display_ili9341_spi_blit(const struct lws_display *disp, const uint8_t *src,
  103. lws_display_scalar x, lws_display_scalar y,
  104. lws_display_scalar w, lws_display_scalar h)
  105. {
  106. const lws_display_ili9341_t *ili = (const lws_display_ili9341_t *)disp;
  107. lws_spi_desc_t desc;
  108. uint8_t u[5];
  109. memset(&desc, 0, sizeof(desc));
  110. desc.count_cmd = 1;
  111. desc.src = &u[0];
  112. desc.count_write = 0;
  113. /*
  114. * Blit a line at a time
  115. */
  116. while (h--) {
  117. u[0] = ILI9341_CASET;
  118. desc.data = &u[1];
  119. u[1] = x;
  120. u[2] = x;
  121. u[3] = w >> 8;
  122. u[4] = w & 0xff;
  123. desc.count_write = 4;
  124. ili->spi->queue(ili->spi, &desc);
  125. u[0] = ILI9341_PASET;
  126. u[1] = y >> 8;
  127. u[2] = y & 0xff;
  128. u[3] = (y + 1) >> 8;
  129. u[4] = (y + 1) & 0xff;
  130. desc.count_write = 4;
  131. ili->spi->queue(ili->spi, &desc);
  132. u[0] = ILI9341_RAMWR;
  133. desc.data = src;
  134. desc.count_write = w * 2;
  135. ili->spi->queue(ili->spi, &desc);
  136. src += w * 2;
  137. y++;
  138. }
  139. return 0;
  140. }
  141. int
  142. lws_display_ili9341_spi_power(const struct lws_display *disp, int state)
  143. {
  144. const lws_display_ili9341_t *ili = (const lws_display_ili9341_t *)disp;
  145. lws_spi_desc_t desc;
  146. uint8_t u[1];
  147. memset(&desc, 0, sizeof(desc));
  148. desc.count_cmd = 1;
  149. desc.data = desc.src = &u[0];
  150. u[0] = state ? ILI9341_SLPOUT : ILI9341_SLPIN;
  151. ili->spi->queue(ili->spi, &desc);
  152. /* we're not going to do anything useful for 5ms after this */
  153. return 0;
  154. }