ILI9341.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. /*//----- Or Rotation: -----
  200. Private->MADCtl = 0x80; //Orientation 0 degree
  201. if (HFlip) { //Orientation 90 degree
  202. Private->MADCtl = 0x20;
  203. int a = Device->Height;
  204. int b = Device->Width;
  205. Device->Height = b;
  206. Device->Width = a;
  207. }
  208. if (Rotate) { //Orientation 180 degree
  209. Private->MADCtl = 0x40;
  210. }
  211. if (VFlip) { //Orientation 270 degree
  212. Private->MADCtl = 0xE0;
  213. int a = Device->Height;
  214. int b = Device->Width;
  215. Device->Height = b;
  216. Device->Width = a;
  217. }
  218. */
  219. ESP_LOGI(TAG, "SetLayout 255 Private->MADCtl=%hhu", Private->MADCtl);
  220. Device->WriteCommand( Device, 0x36 );
  221. WriteByte( Device, Private->MADCtl );
  222. #ifdef SHADOW_BUFFER
  223. // force a full refresh (almost ...)
  224. memset(Private->Shadowbuffer, 0xAA, Device->FramebufferSize);
  225. #endif
  226. }
  227. static void DisplayOn( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0x29 ); } //DISPON =0x29
  228. static void DisplayOff( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0x28 ); } //DISPOFF=0x28
  229. static void SetContrast( struct GDS_Device* Device, uint8_t Contrast ) {
  230. Device->WriteCommand( Device, 0x51 );
  231. WriteByte( Device, Contrast );
  232. Device->SetContrast = NULL;
  233. GDS_SetContrast( Device, Contrast );
  234. Device->SetContrast = SetContrast; // 0x00 value means the lowest brightness and 0xFF value means the highest brightness.
  235. }
  236. static bool Init( struct GDS_Device* Device ) {
  237. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  238. int Depth = (Device->Depth + 8 - 1) / 8;
  239. Private->PageSize = min(8, PAGE_BLOCK / (Device->Width * Depth));
  240. #ifdef SHADOW_BUFFER
  241. Private->Shadowbuffer = malloc( Device->FramebufferSize );
  242. memset(Private->Shadowbuffer, 0xFF, Device->FramebufferSize);
  243. #endif
  244. #ifdef USE_IRAM
  245. Private->iRAM = heap_caps_malloc( (Private->PageSize + 1) * Device->Width * Depth, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  246. #endif
  247. ESP_LOGI(TAG, "ILI9341 with bit default-depth %u, page %u, iRAM %p", Device->Depth, Private->PageSize, Private->iRAM);
  248. // Sleepout + Booster
  249. Device->WriteCommand( Device, 0x11 );
  250. // need BGR & Address Mode
  251. //Private->MADCtl = 1 << 3; // for ST77xx = 0x40
  252. //Private->MADCtl = 1 << 7; // for ILI9341 = 0x80 (320x240) or 0x20 (240x320)
  253. //Device->WriteCommand( Device, 0x36 );
  254. //WriteByte( Device, Private->MADCtl );
  255. // set flip modes & contrast
  256. GDS_SetContrast( Device, 0x7f );
  257. Device->SetLayout( Device, false, false, false );
  258. // set screen depth (16/18) *** INTERFACE PIXEL FORMAT: 0x66=18 bit; 0x55=16 bit
  259. Device->WriteCommand( Device, 0x3A );
  260. if (Private->Model == ILI9341_24) WriteByte( Device, Device->Depth == 24 ? 0x66 : 0x55 );
  261. else WriteByte( Device, Device->Depth == 24 ? 0x66 : 0x55 );
  262. ESP_LOGI(TAG, "ILI9341_Init 312 device-depth %u, 0x66/0x55=0x%X", Device->Depth, Device->Depth == 24 ? 0x66 : 0x55);
  263. // no Display Inversion (INVOFF=0x20 INVON=0x21)
  264. Device->WriteCommand( Device, 0x20 );
  265. //Gamma Correction: Enable next two line and enabel one of the Test0x Section... or build you own 15 Parameter...
  266. Device->WriteCommand( Device, 0xF2 ); WriteByte( Device, 0x03 ); // 3Gamma Function: Disable = default (0x02), Enable (0x03)
  267. Device->WriteCommand( Device, 0x26 ); WriteByte( Device, 0x01 ); // Gamma curve selected (0x01, 0x02, 0x04, 0x08) - A maximum of 4 fixed gamma curves can be selected
  268. //Gamma Correction Test01
  269. Device->WriteCommand( Device, 0xE0 ); // Positive Gamma Correction (15 Parameter)
  270. WriteByte( Device, 0x0F ); WriteByte( Device, 0x31 ); WriteByte( Device, 0x2B ); WriteByte( Device, 0x0C ); WriteByte( Device, 0x0E );
  271. WriteByte( Device, 0x08 ); WriteByte( Device, 0x4E ); WriteByte( Device, 0xF1 ); WriteByte( Device, 0x37 ); WriteByte( Device, 0x07 );
  272. WriteByte( Device, 0x10 ); WriteByte( Device, 0x03 ); WriteByte( Device, 0x0E ); WriteByte( Device, 0x09 ); WriteByte( Device, 0x00 );
  273. Device->WriteCommand( Device, 0xE1 ); // Negative Gamma Correction (15 Parameter)
  274. WriteByte( Device, 0x00 ); WriteByte( Device, 0x0E ); WriteByte( Device, 0x14 ); WriteByte( Device, 0x03 ); WriteByte( Device, 0x11 );
  275. WriteByte( Device, 0x07 ); WriteByte( Device, 0x31 ); WriteByte( Device, 0xC1 ); WriteByte( Device, 0x48 ); WriteByte( Device, 0x08 );
  276. WriteByte( Device, 0x0F ); WriteByte( Device, 0x0C ); WriteByte( Device, 0x31 ); WriteByte( Device, 0x36 ); WriteByte( Device, 0x0F );
  277. /*//Gamma Correction Test02
  278. Device->WriteCommand( Device, 0xE0 ); // Positive Gamma Correction (15 Parameter)
  279. WriteByte( Device, 0x1F ); WriteByte( Device, 0x1A ); WriteByte( Device, 0x18 ); WriteByte( Device, 0x0A ); WriteByte( Device, 0x0F );
  280. WriteByte( Device, 0x06 ); WriteByte( Device, 0x45 ); WriteByte( Device, 0x87 ); WriteByte( Device, 0x32 ); WriteByte( Device, 0x0a );
  281. WriteByte( Device, 0x07 ); WriteByte( Device, 0x02 ); WriteByte( Device, 0x07 ); WriteByte( Device, 0x05 ); WriteByte( Device, 0x00 );
  282. Device->WriteCommand( Device, 0xE1 ); // Negative Gamma Correction (15 Parameter)
  283. WriteByte( Device, 0x00 ); WriteByte( Device, 0x25 ); WriteByte( Device, 0x27 ); WriteByte( Device, 0x05 ); WriteByte( Device, 0x10 );
  284. WriteByte( Device, 0x09 ); WriteByte( Device, 0x3a ); WriteByte( Device, 0x78 ); WriteByte( Device, 0x4d ); WriteByte( Device, 0x05 );
  285. WriteByte( Device, 0x18 ); WriteByte( Device, 0x0d ); WriteByte( Device, 0x38 ); WriteByte( Device, 0x3a ); WriteByte( Device, 0x1F );
  286. */
  287. /*//Gamma Correction Test03
  288. Device->WriteCommand( Device, 0xE0 ); // Positive Gamma Correction (15 Parameter)
  289. WriteByte( Device, 0x0F ); WriteByte( Device, 0x3F ); WriteByte( Device, 0x2F ); WriteByte( Device, 0x0C ); WriteByte( Device, 0x10 );
  290. WriteByte( Device, 0x0A ); WriteByte( Device, 0x53 ); WriteByte( Device, 0xD5 ); WriteByte( Device, 0x40 ); WriteByte( Device, 0x0A );
  291. WriteByte( Device, 0x13 ); WriteByte( Device, 0x03 ); WriteByte( Device, 0x08 ); WriteByte( Device, 0x03 ); WriteByte( Device, 0x00 );
  292. Device->WriteCommand( Device, 0xE1 ); // Negative Gamma Correction (15 Parameter)
  293. WriteByte( Device, 0x00 ); WriteByte( Device, 0x00 ); WriteByte( Device, 0x10 ); WriteByte( Device, 0x03 ); WriteByte( Device, 0x0F );
  294. WriteByte( Device, 0x05 ); WriteByte( Device, 0x2C ); WriteByte( Device, 0xA2 ); WriteByte( Device, 0x3F ); WriteByte( Device, 0x05 );
  295. WriteByte( Device, 0x0E ); WriteByte( Device, 0x0C ); WriteByte( Device, 0x37 ); WriteByte( Device, 0x3c ); WriteByte( Device, 0x0F );
  296. */
  297. /*//Gamma Correction Test04 (no real values... only to test, that Gamme Correction works... you see very light cover-images)
  298. Device->WriteCommand( Device, 0xE0 ); // Positive Gamma Correction (15 Parameter)
  299. WriteByte( Device, 0x22 ); WriteByte( Device, 0x22 ); WriteByte( Device, 0x22 ); WriteByte( Device, 0x22 ); WriteByte( Device, 0x22 );
  300. WriteByte( Device, 0x22 ); WriteByte( Device, 0x22 ); WriteByte( Device, 0x22 ); WriteByte( Device, 0x22 ); WriteByte( Device, 0x22 );
  301. WriteByte( Device, 0x22 ); WriteByte( Device, 0x22 ); WriteByte( Device, 0x22 ); WriteByte( Device, 0x22 ); WriteByte( Device, 0x22 );
  302. Device->WriteCommand( Device, 0xE1 ); // Negative Gamma Correction (15 Parameter)
  303. WriteByte( Device, 0x10 ); WriteByte( Device, 0x10 ); WriteByte( Device, 0x10 ); WriteByte( Device, 0x10 ); WriteByte( Device, 0x10 );
  304. WriteByte( Device, 0x10 ); WriteByte( Device, 0x10 ); WriteByte( Device, 0x10 ); WriteByte( Device, 0x10 ); WriteByte( Device, 0x10 );
  305. WriteByte( Device, 0x10 ); WriteByte( Device, 0x10 ); WriteByte( Device, 0x10 ); WriteByte( Device, 0x10 ); WriteByte( Device, 0x10 );
  306. */
  307. // gone with the wind
  308. Device->DisplayOn( Device );
  309. Device->Update( Device );
  310. return true;
  311. }
  312. static const struct GDS_Device ILI9341_X = {
  313. .DisplayOn = DisplayOn, .DisplayOff = DisplayOff,
  314. .SetLayout = SetLayout,
  315. .Update = Update16, .Init = Init,
  316. .Mode = GDS_RGB565, .Depth = 16,
  317. };
  318. struct GDS_Device* ILI9341_Detect(char *Driver, struct GDS_Device* Device) {
  319. uint8_t Model;
  320. //int Depth=18; // 18bit (=24bit) colordepth
  321. int Depth=16; // 16bit colordepth
  322. if (strcasestr(Driver, "ILI9341")) Model = ILI9341;
  323. else if (strcasestr(Driver, "ILI9341_24")) Model = ILI9341_24; //for future use...
  324. else return NULL;
  325. //ESP_LOGI(TAG, "ILI9341_Detect 383 Model=%hhu (0=ILI9341, 1=ILI9341_24) and Depth=%d", Model,Depth);
  326. if (!Device) Device = calloc(1, sizeof(struct GDS_Device));
  327. *Device = ILI9341_X;
  328. sscanf(Driver, "%*[^:]:%u", &Depth); // NVS-Parameter driver=ILI9341[:16|18]
  329. struct PrivateSpace* Private = (struct PrivateSpace*) Device->Private;
  330. Private->Model = Model;
  331. ESP_LOGI(TAG, "ILI9341_Detect 391 Driver= %s Depth=%d", Driver, Depth);
  332. if (Depth == 18) {
  333. Device->Mode = GDS_RGB888;
  334. Device->Depth = 24;
  335. Device->Update = Update24;
  336. }
  337. if (Model == ILI9341_24) Device->SetContrast = SetContrast;
  338. return Device;
  339. }