ILI9341.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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, struct GDS_Layout *Layout ) {
  161. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  162. ESP_LOGI(TAG, "SetLayout 197 HFlip=%d VFlip=%d Rotate=%d (1=true)", Layout->HFlip, Layout->VFlip, Layout->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 (Layout->HFlip) { //Flip Horizontal
  170. int a = Private->MADCtl;
  171. Private->MADCtl = (a ^ (1 << 7));
  172. }
  173. if (Layout->Rotate) { //Rotate 180 degr.
  174. int a = Private->MADCtl;
  175. a = (a ^ (1 << 7));
  176. Private->MADCtl = (a ^ (1 << 6));
  177. }
  178. if (Layout->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 (Layout->HFlip) { //Flip Horizontal
  185. int a = Private->MADCtl;
  186. Private->MADCtl = (a ^ (1 << 6));
  187. }
  188. if (Layout->Rotate) { //Rotate 180 degr.
  189. int a = Private->MADCtl;
  190. a = (a ^ (1 << 7));
  191. Private->MADCtl = (a ^ (1 << 6));
  192. }
  193. if (Layout->VFlip) { //Flip Vertical
  194. int a = Private->MADCtl;
  195. Private->MADCtl = (a ^ (1 << 7));
  196. }
  197. }
  198. Private->MADCtl = Layout->ColorSwap ? (Private->MADCtl | (1 << 3)) : (Private->MADCtl & ~(1 << 3));
  199. ESP_LOGI(TAG, "SetLayout 255 Private->MADCtl=%hhu", Private->MADCtl);
  200. Device->WriteCommand( Device, 0x36 );
  201. WriteByte( Device, Private->MADCtl );
  202. Device->WriteCommand( Device, Layout->Invert ? 0x21 : 0x20 );
  203. #ifdef SHADOW_BUFFER
  204. // force a full refresh (almost ...)
  205. memset(Private->Shadowbuffer, 0xAA, Device->FramebufferSize);
  206. #endif
  207. }
  208. static void DisplayOn( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0x29 ); } //DISPON =0x29
  209. static void DisplayOff( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0x28 ); } //DISPOFF=0x28
  210. static void SetContrast( struct GDS_Device* Device, uint8_t Contrast ) {
  211. Device->WriteCommand( Device, 0x51 );
  212. WriteByte( Device, Contrast );
  213. Device->SetContrast = NULL;
  214. GDS_SetContrast( Device, Contrast );
  215. Device->SetContrast = SetContrast; // 0x00 value means the lowest brightness and 0xFF value means the highest brightness.
  216. }
  217. static bool Init( struct GDS_Device* Device ) {
  218. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  219. int Depth = (Device->Depth + 8 - 1) / 8;
  220. Private->PageSize = min(8, PAGE_BLOCK / (Device->Width * Depth));
  221. #ifdef SHADOW_BUFFER
  222. Private->Shadowbuffer = malloc( Device->FramebufferSize );
  223. memset(Private->Shadowbuffer, 0xFF, Device->FramebufferSize);
  224. #endif
  225. #ifdef USE_IRAM
  226. Private->iRAM = heap_caps_malloc( (Private->PageSize + 1) * Device->Width * Depth, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  227. #endif
  228. ESP_LOGI(TAG, "ILI9341 with bit default-depth %u, page %u, iRAM %p", Device->Depth, Private->PageSize, Private->iRAM);
  229. // Sleepout + Booster
  230. Device->WriteCommand( Device, 0x11 );
  231. // set flip modes & contrast
  232. GDS_SetContrast( Device, 0x7f );
  233. struct GDS_Layout Layout = { };
  234. Device->SetLayout( Device, &Layout );
  235. // set screen depth (16/18) *** INTERFACE PIXEL FORMAT: 0x66=18 bit; 0x55=16 bit
  236. Device->WriteCommand( Device, 0x3A );
  237. if (Private->Model == ILI9341_24) WriteByte( Device, Device->Depth == 24 ? 0x66 : 0x55 );
  238. else WriteByte( Device, Device->Depth == 24 ? 0x66 : 0x55 );
  239. ESP_LOGI(TAG, "ILI9341_Init 312 device-depth %u, 0x66/0x55=0x%X", Device->Depth, Device->Depth == 24 ? 0x66 : 0x55);
  240. // no Display Inversion (INVOFF=0x20 INVON=0x21)
  241. Device->WriteCommand( Device, 0x20 );
  242. //Gamma Correction: Enable next two line and enabel one of the Test0x Section... or build you own 15 Parameter...
  243. Device->WriteCommand( Device, 0xF2 ); WriteByte( Device, 0x03 ); // 3Gamma Function: Disable = default (0x02), Enable (0x03)
  244. Device->WriteCommand( Device, 0x26 ); WriteByte( Device, 0x01 ); // Gamma curve selected (0x01, 0x02, 0x04, 0x08) - A maximum of 4 fixed gamma curves can be selected
  245. //Gamma Correction Test01
  246. Device->WriteCommand( Device, 0xE0 ); // Positive Gamma Correction (15 Parameter)
  247. WriteByte( Device, 0x0F ); WriteByte( Device, 0x31 ); WriteByte( Device, 0x2B ); WriteByte( Device, 0x0C ); WriteByte( Device, 0x0E );
  248. WriteByte( Device, 0x08 ); WriteByte( Device, 0x4E ); WriteByte( Device, 0xF1 ); WriteByte( Device, 0x37 ); WriteByte( Device, 0x07 );
  249. WriteByte( Device, 0x10 ); WriteByte( Device, 0x03 ); WriteByte( Device, 0x0E ); WriteByte( Device, 0x09 ); WriteByte( Device, 0x00 );
  250. Device->WriteCommand( Device, 0xE1 ); // Negative Gamma Correction (15 Parameter)
  251. WriteByte( Device, 0x00 ); WriteByte( Device, 0x0E ); WriteByte( Device, 0x14 ); WriteByte( Device, 0x03 ); WriteByte( Device, 0x11 );
  252. WriteByte( Device, 0x07 ); WriteByte( Device, 0x31 ); WriteByte( Device, 0xC1 ); WriteByte( Device, 0x48 ); WriteByte( Device, 0x08 );
  253. WriteByte( Device, 0x0F ); WriteByte( Device, 0x0C ); WriteByte( Device, 0x31 ); WriteByte( Device, 0x36 ); WriteByte( Device, 0x0F );
  254. // gone with the wind
  255. Device->DisplayOn( Device );
  256. Device->Update( Device );
  257. return true;
  258. }
  259. static const struct GDS_Device ILI9341_X = {
  260. .DisplayOn = DisplayOn, .DisplayOff = DisplayOff,
  261. .SetLayout = SetLayout,
  262. .Update = Update16, .Init = Init,
  263. .Mode = GDS_RGB565, .Depth = 16,
  264. };
  265. struct GDS_Device* ILI9341_Detect(char *Driver, struct GDS_Device* Device) {
  266. uint8_t Model;
  267. int Depth=16; // 16bit colordepth
  268. if (strcasestr(Driver, "ILI9341")) Model = ILI9341;
  269. else if (strcasestr(Driver, "ILI9341_24")) Model = ILI9341_24; //for future use...
  270. else return NULL;
  271. if (!Device) Device = calloc(1, sizeof(struct GDS_Device));
  272. *Device = ILI9341_X;
  273. sscanf(Driver, "%*[^:]:%u", &Depth); // NVS-Parameter driver=ILI9341[:16|18]
  274. struct PrivateSpace* Private = (struct PrivateSpace*) Device->Private;
  275. Private->Model = Model;
  276. ESP_LOGI(TAG, "ILI9341_Detect 391 Driver= %s Depth=%d", Driver, Depth);
  277. if (Depth == 18) {
  278. Device->Mode = GDS_RGB888;
  279. Device->Depth = 24;
  280. Device->Update = Update24;
  281. }
  282. if (Model == ILI9341_24) Device->SetContrast = SetContrast;
  283. return Device;
  284. }