ILI9341.c 13 KB

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