ILI9341.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /**
  2. * Copyright (c) 2017-2018 Tara Keeling
  3. * 2020 Philippe G.
  4. * 2021 Mumpf and Harry1999
  5. *
  6. * This software is released under the MIT License.
  7. * https://opensource.org/licenses/MIT
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdint.h>
  12. #include <stdbool.h>
  13. #include <esp_heap_caps.h>
  14. #include <esp_log.h>
  15. #include "gds.h"
  16. #include "gds_private.h"
  17. #define SHADOW_BUFFER
  18. #define USE_IRAM
  19. #define PAGE_BLOCK 2048
  20. #define ENABLE_WRITE 0x2c
  21. #define MADCTL_MX 0x40
  22. #define TFT_RGB_BGR 0x08
  23. #define min(a,b) (((a) < (b)) ? (a) : (b))
  24. static char TAG[] = "ILI9341";
  25. enum { ILI9341, ILI9341_24 }; //ILI9341_24 for future use...
  26. struct PrivateSpace {
  27. uint8_t *iRAM, *Shadowbuffer;
  28. struct {
  29. uint16_t Height, Width;
  30. } Offset;
  31. uint8_t MADCtl, PageSize;
  32. uint8_t Model;
  33. };
  34. // Functions are not declared to minimize # of lines
  35. static void WriteByte( struct GDS_Device* Device, uint8_t Data ) {
  36. Device->WriteData( Device, &Data, 1 );
  37. }
  38. static void SetColumnAddress( struct GDS_Device* Device, uint16_t Start, uint16_t End ) {
  39. uint32_t Addr = __builtin_bswap16(Start) | (__builtin_bswap16(End) << 16);
  40. Device->WriteCommand( Device, 0x2A );
  41. Device->WriteData( Device, (uint8_t*) &Addr, 4 );
  42. }
  43. static void SetRowAddress( struct GDS_Device* Device, uint16_t Start, uint16_t End ) {
  44. uint32_t Addr = __builtin_bswap16(Start) | (__builtin_bswap16(End) << 16);
  45. Device->WriteCommand( Device, 0x2B );
  46. Device->WriteData( Device, (uint8_t*) &Addr, 4 );
  47. }
  48. static void Update16( struct GDS_Device* Device ) {
  49. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  50. #ifdef SHADOW_BUFFER
  51. uint32_t *optr = (uint32_t*) Private->Shadowbuffer, *iptr = (uint32_t*) Device->Framebuffer;
  52. int FirstCol = Device->Width / 2, LastCol = 0, FirstRow = -1, LastRow = 0;
  53. for (int r = 0; r < Device->Height; r++) {
  54. // look for change and update shadow (cheap optimization = width is always a multiple of 2)
  55. for (int c = 0; c < Device->Width / 2; c++, iptr++, optr++) {
  56. if (*optr != *iptr) {
  57. *optr = *iptr;
  58. if (c < FirstCol) FirstCol = c;
  59. if (c > LastCol) LastCol = c;
  60. if (FirstRow < 0) FirstRow = r;
  61. LastRow = r;
  62. }
  63. }
  64. // wait for a large enough window - careful that window size might increase by more than a line at once !
  65. if (FirstRow < 0 || ((LastCol - FirstCol + 1) * (r - FirstRow + 1) * 4 < PAGE_BLOCK && r != Device->Height - 1)) continue;
  66. FirstCol *= 2;
  67. LastCol = LastCol * 2 + 1;
  68. SetRowAddress( Device, FirstRow + Private->Offset.Height, LastRow + Private->Offset.Height);
  69. SetColumnAddress( Device, FirstCol + Private->Offset.Width, LastCol + Private->Offset.Width );
  70. Device->WriteCommand( Device, ENABLE_WRITE );
  71. int ChunkSize = (LastCol - FirstCol + 1) * 2;
  72. // own use of IRAM has not proven to be much better than letting SPI do its copy
  73. if (Private->iRAM) {
  74. uint8_t *optr = Private->iRAM;
  75. for (int i = FirstRow; i <= LastRow; i++) {
  76. memcpy(optr, Private->Shadowbuffer + (i * Device->Width + FirstCol) * 2, ChunkSize);
  77. optr += ChunkSize;
  78. if (optr - Private->iRAM <= (PAGE_BLOCK - ChunkSize) && i < LastRow) continue;
  79. Device->WriteData(Device, Private->iRAM, optr - Private->iRAM);
  80. optr = Private->iRAM;
  81. }
  82. } else for (int i = FirstRow; i <= LastRow; i++) {
  83. Device->WriteData( Device, Private->Shadowbuffer + (i * Device->Width + FirstCol) * 2, ChunkSize );
  84. }
  85. FirstCol = Device->Width / 2; LastCol = 0;
  86. FirstRow = -1;
  87. }
  88. #else
  89. // always update by full lines
  90. SetColumnAddress( Device, Private->Offset.Width, Device->Width - 1);
  91. for (int r = 0; r < Device->Height; r += min(Private->PageSize, Device->Height - r)) {
  92. int Height = min(Private->PageSize, Device->Height - r);
  93. SetRowAddress( Device, Private->Offset.Height + r, Private->Offset.Height + r + Height - 1 );
  94. Device->WriteCommand(Device, ENABLE_WRITE);
  95. if (Private->iRAM) {
  96. memcpy(Private->iRAM, Device->Framebuffer + r * Device->Width * 2, Height * Device->Width * 2 );
  97. Device->WriteData( Device, Private->iRAM, Height * Device->Width * 2 );
  98. } else {
  99. Device->WriteData( Device, Device->Framebuffer + r * Device->Width * 2, Height * Device->Width * 2 );
  100. }
  101. }
  102. #endif
  103. }
  104. static void Update24( struct GDS_Device* Device ) {
  105. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  106. #ifdef SHADOW_BUFFER
  107. uint16_t *optr = (uint16_t*) Private->Shadowbuffer, *iptr = (uint16_t*) Device->Framebuffer;
  108. int FirstCol = (Device->Width * 3) / 2, LastCol = 0, FirstRow = -1, LastRow = 0;
  109. for (int r = 0; r < Device->Height; r++) {
  110. // look for change and update shadow (cheap optimization = width always / by 2)
  111. for (int c = 0; c < (Device->Width * 3) / 2; c++, optr++, iptr++) {
  112. if (*optr != *iptr) {
  113. *optr = *iptr;
  114. if (c < FirstCol) FirstCol = c;
  115. if (c > LastCol) LastCol = c;
  116. if (FirstRow < 0) FirstRow = r;
  117. LastRow = r;
  118. }
  119. }
  120. // do we have enough to send (cols are divided by 3/2)
  121. if (FirstRow < 0 || ((((LastCol - FirstCol + 1) * 2 ) / 3) * (r - FirstRow + 1) * 4 < PAGE_BLOCK && r != Device->Height - 1)) continue;
  122. FirstCol = (FirstCol * 2) / 3;
  123. LastCol = (LastCol * 2 + 1 ) / 3;
  124. SetRowAddress( Device, FirstRow + Private->Offset.Height, LastRow + Private->Offset.Height);
  125. SetColumnAddress( Device, FirstCol + Private->Offset.Width, LastCol + Private->Offset.Width );
  126. Device->WriteCommand( Device, ENABLE_WRITE );
  127. int ChunkSize = (LastCol - FirstCol + 1) * 3;
  128. // own use of IRAM has not proven to be much better than letting SPI do its copy
  129. if (Private->iRAM) {
  130. uint8_t *optr = Private->iRAM;
  131. for (int i = FirstRow; i <= LastRow; i++) {
  132. memcpy(optr, Private->Shadowbuffer + (i * Device->Width + FirstCol) * 3, ChunkSize);
  133. optr += ChunkSize;
  134. if (optr - Private->iRAM <= (PAGE_BLOCK - ChunkSize) && i < LastRow) continue;
  135. Device->WriteData(Device, Private->iRAM, optr - Private->iRAM);
  136. optr = Private->iRAM;
  137. }
  138. } else for (int i = FirstRow; i <= LastRow; i++) {
  139. Device->WriteData( Device, Private->Shadowbuffer + (i * Device->Width + FirstCol) * 3, ChunkSize );
  140. }
  141. FirstCol = (Device->Width * 3) / 2; LastCol = 0;
  142. FirstRow = -1;
  143. }
  144. #else
  145. // always update by full lines
  146. SetColumnAddress( Device, Private->Offset.Width, Device->Width - 1);
  147. for (int r = 0; r < Device->Height; r += min(Private->PageSize, Device->Height - r)) {
  148. int Height = min(Private->PageSize, Device->Height - r);
  149. SetRowAddress( Device, Private->Offset.Height + r, Private->Offset.Height + r + Height - 1 );
  150. Device->WriteCommand(Device, ENABLE_WRITE);
  151. if (Private->iRAM) {
  152. memcpy(Private->iRAM, Device->Framebuffer + r * Device->Width * 3, Height * Device->Width * 3 );
  153. Device->WriteData( Device, Private->iRAM, Height * Device->Width * 3 );
  154. } else {
  155. Device->WriteData( Device, Device->Framebuffer + r * Device->Width * 3, Height * Device->Width * 3 );
  156. }
  157. }
  158. #endif
  159. }
  160. static void SetLayout( struct GDS_Device* Device, bool HFlip, bool VFlip, bool Rotate ) {
  161. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  162. ESP_LOGI(TAG, "SetLayout 197 HFlip=%d VFlip=%d Rotate=%d (1=true)", HFlip, VFlip, Rotate);
  163. // D/CX RDX WRX D17-8 D7 D6 D5 D4 D3 D2 D1 D0 HEX
  164. //Command 0 1 ↑ XX 0 0 1 1 0 1 1 0 36h
  165. //Parameter 1 1 ↑ XX MY MX MV ML BGR MH 0 0 00
  166. //Orientation 0: MADCtl = 0x80 = 1000 0000 (MY=1)
  167. if ((Device->Height)>(Device->Width)){ //Resolution = 320x240
  168. Private->MADCtl = (1 << 7); // 0x80 = default (no Rotation an no Flip)
  169. if (HFlip) { //Flip Horizontal
  170. int a = Private->MADCtl;
  171. Private->MADCtl = (a ^ (1 << 7));
  172. }
  173. if (Rotate) { //Rotate 180 degr.
  174. int a = Private->MADCtl;
  175. a = (a ^ (1 << 7));
  176. Private->MADCtl = (a ^ (1 << 6));
  177. }
  178. if (VFlip) { //Flip Vertical
  179. int a = Private->MADCtl;
  180. Private->MADCtl = (a ^ (1 << 6));
  181. }
  182. } else { //Resolution = 240x320
  183. Private->MADCtl = (1 << 5); // 0x20 = default (no Rotation an no Flip)
  184. if (HFlip) { //Flip Horizontal
  185. int a = Private->MADCtl;
  186. Private->MADCtl = (a ^ (1 << 6));
  187. }
  188. if (Rotate) { //Rotate 180 degr.
  189. int a = Private->MADCtl;
  190. a = (a ^ (1 << 7));
  191. Private->MADCtl = (a ^ (1 << 6));
  192. }
  193. if (VFlip) { //Flip Vertical
  194. int a = Private->MADCtl;
  195. Private->MADCtl = (a ^ (1 << 7));
  196. }
  197. }
  198. ESP_LOGI(TAG, "SetLayout 255 Private->MADCtl=%hhu", Private->MADCtl);
  199. Device->WriteCommand( Device, 0x36 );
  200. WriteByte( Device, Private->MADCtl );
  201. #ifdef SHADOW_BUFFER
  202. // force a full refresh (almost ...)
  203. memset(Private->Shadowbuffer, 0xAA, Device->FramebufferSize);
  204. #endif
  205. }
  206. static void DisplayOn( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0x29 ); } //DISPON =0x29
  207. static void DisplayOff( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0x28 ); } //DISPOFF=0x28
  208. static void SetContrast( struct GDS_Device* Device, uint8_t Contrast ) {
  209. Device->WriteCommand( Device, 0x51 );
  210. WriteByte( Device, Contrast );
  211. Device->SetContrast = NULL;
  212. GDS_SetContrast( Device, Contrast );
  213. Device->SetContrast = SetContrast; // 0x00 value means the lowest brightness and 0xFF value means the highest brightness.
  214. }
  215. static bool Init( struct GDS_Device* Device ) {
  216. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  217. int Depth = (Device->Depth + 8 - 1) / 8;
  218. Private->PageSize = min(8, PAGE_BLOCK / (Device->Width * Depth));
  219. #ifdef SHADOW_BUFFER
  220. Private->Shadowbuffer = malloc( Device->FramebufferSize );
  221. memset(Private->Shadowbuffer, 0xFF, Device->FramebufferSize);
  222. #endif
  223. #ifdef USE_IRAM
  224. Private->iRAM = heap_caps_malloc( (Private->PageSize + 1) * Device->Width * Depth, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  225. #endif
  226. ESP_LOGI(TAG, "ILI9341 with bit default-depth %u, page %u, iRAM %p", Device->Depth, Private->PageSize, Private->iRAM);
  227. // Sleepout + Booster
  228. Device->WriteCommand( Device, 0x11 );
  229. // set flip modes & contrast
  230. GDS_SetContrast( Device, 0x7f );
  231. Device->SetLayout( Device, false, false, false );
  232. // set screen depth (16/18) *** INTERFACE PIXEL FORMAT: 0x66=18 bit; 0x55=16 bit
  233. Device->WriteCommand( Device, 0x3A );
  234. if (Private->Model == ILI9341_24) WriteByte( Device, Device->Depth == 24 ? 0x66 : 0x55 );
  235. else WriteByte( Device, Device->Depth == 24 ? 0x66 : 0x55 );
  236. ESP_LOGI(TAG, "ILI9341_Init 312 device-depth %u, 0x66/0x55=0x%X", Device->Depth, Device->Depth == 24 ? 0x66 : 0x55);
  237. // no Display Inversion (INVOFF=0x20 INVON=0x21)
  238. Device->WriteCommand( Device, 0x20 );
  239. //Gamma Correction: Enable next two line and enabel one of the Test0x Section... or build you own 15 Parameter...
  240. Device->WriteCommand( Device, 0xF2 ); WriteByte( Device, 0x03 ); // 3Gamma Function: Disable = default (0x02), Enable (0x03)
  241. Device->WriteCommand( Device, 0x26 ); WriteByte( Device, 0x01 ); // Gamma curve selected (0x01, 0x02, 0x04, 0x08) - A maximum of 4 fixed gamma curves can be selected
  242. //Gamma Correction Test01
  243. Device->WriteCommand( Device, 0xE0 ); // Positive Gamma Correction (15 Parameter)
  244. WriteByte( Device, 0x0F ); WriteByte( Device, 0x31 ); WriteByte( Device, 0x2B ); WriteByte( Device, 0x0C ); WriteByte( Device, 0x0E );
  245. WriteByte( Device, 0x08 ); WriteByte( Device, 0x4E ); WriteByte( Device, 0xF1 ); WriteByte( Device, 0x37 ); WriteByte( Device, 0x07 );
  246. WriteByte( Device, 0x10 ); WriteByte( Device, 0x03 ); WriteByte( Device, 0x0E ); WriteByte( Device, 0x09 ); WriteByte( Device, 0x00 );
  247. Device->WriteCommand( Device, 0xE1 ); // Negative Gamma Correction (15 Parameter)
  248. WriteByte( Device, 0x00 ); WriteByte( Device, 0x0E ); WriteByte( Device, 0x14 ); WriteByte( Device, 0x03 ); WriteByte( Device, 0x11 );
  249. WriteByte( Device, 0x07 ); WriteByte( Device, 0x31 ); WriteByte( Device, 0xC1 ); WriteByte( Device, 0x48 ); WriteByte( Device, 0x08 );
  250. WriteByte( Device, 0x0F ); WriteByte( Device, 0x0C ); WriteByte( Device, 0x31 ); WriteByte( Device, 0x36 ); WriteByte( Device, 0x0F );
  251. // gone with the wind
  252. Device->DisplayOn( Device );
  253. Device->Update( Device );
  254. return true;
  255. }
  256. static const struct GDS_Device ILI9341_X = {
  257. .DisplayOn = DisplayOn, .DisplayOff = DisplayOff,
  258. .SetLayout = SetLayout,
  259. .Update = Update16, .Init = Init,
  260. .Mode = GDS_RGB565, .Depth = 16,
  261. };
  262. struct GDS_Device* ILI9341_Detect(char *Driver, struct GDS_Device* Device) {
  263. uint8_t Model;
  264. int Depth=16; // 16bit colordepth
  265. if (strcasestr(Driver, "ILI9341")) Model = ILI9341;
  266. else if (strcasestr(Driver, "ILI9341_24")) Model = ILI9341_24; //for future use...
  267. else return NULL;
  268. if (!Device) Device = calloc(1, sizeof(struct GDS_Device));
  269. *Device = ILI9341_X;
  270. sscanf(Driver, "%*[^:]:%u", &Depth); // NVS-Parameter driver=ILI9341[:16|18]
  271. struct PrivateSpace* Private = (struct PrivateSpace*) Device->Private;
  272. Private->Model = Model;
  273. ESP_LOGI(TAG, "ILI9341_Detect 391 Driver= %s Depth=%d", Driver, Depth);
  274. if (Depth == 18) {
  275. Device->Mode = GDS_RGB888;
  276. Device->Depth = 24;
  277. Device->Update = Update24;
  278. }
  279. if (Model == ILI9341_24) Device->SetContrast = SetContrast;
  280. return Device;
  281. }