ST77xx.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. * Copyright (c) 2017-2018 Tara Keeling
  3. * 2020 Philippe G.
  4. *
  5. * This software is released under the MIT License.
  6. * https://opensource.org/licenses/MIT
  7. */
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdint.h>
  11. #include <stdbool.h>
  12. #include <esp_heap_caps.h>
  13. #include <esp_log.h>
  14. #include "gds.h"
  15. #include "gds_private.h"
  16. #define SHADOW_BUFFER
  17. #define USE_IRAM
  18. #define PAGE_BLOCK 2048
  19. #define ENABLE_WRITE 0x2c
  20. #define min(a,b) (((a) < (b)) ? (a) : (b))
  21. static char TAG[] = "ST77xx";
  22. enum { ST7735, ST7789 };
  23. struct PrivateSpace {
  24. uint8_t *iRAM, *Shadowbuffer;
  25. uint8_t MADCtl, PageSize;
  26. uint8_t Model;
  27. };
  28. // Functions are not declared to minimize # of lines
  29. static void WriteByte( struct GDS_Device* Device, uint8_t Data ) {
  30. Device->WriteData( Device, &Data, 1 );
  31. }
  32. static void SetColumnAddress( struct GDS_Device* Device, uint16_t Start, uint16_t End ) {
  33. uint32_t Addr = __builtin_bswap16(Start) | (__builtin_bswap16(End) << 16);
  34. Device->WriteCommand( Device, 0x2A );
  35. Device->WriteData( Device, (uint8_t*) &Addr, 4 );
  36. }
  37. static void SetRowAddress( struct GDS_Device* Device, uint16_t Start, uint16_t End ) {
  38. uint32_t Addr = __builtin_bswap16(Start) | (__builtin_bswap16(End) << 16);
  39. Device->WriteCommand( Device, 0x2B );
  40. Device->WriteData( Device, (uint8_t*) &Addr, 4 );
  41. }
  42. static void Update16( struct GDS_Device* Device ) {
  43. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  44. #ifdef SHADOW_BUFFER
  45. uint32_t *optr = (uint32_t*) Private->Shadowbuffer, *iptr = (uint32_t*) Device->Framebuffer;
  46. int FirstCol = Device->Width / 2, LastCol = 0, FirstRow = -1, LastRow = 0;
  47. for (int r = 0; r < Device->Height; r++) {
  48. // look for change and update shadow (cheap optimization = width is always a multiple of 2)
  49. for (int c = 0; c < Device->Width / 2; c++, iptr++, optr++) {
  50. if (*optr != *iptr) {
  51. *optr = *iptr;
  52. if (c < FirstCol) FirstCol = c;
  53. if (c > LastCol) LastCol = c;
  54. if (FirstRow < 0) FirstRow = r;
  55. LastRow = r;
  56. }
  57. }
  58. // wait for a large enough window - careful that window size might increase by more than a line at once !
  59. if (FirstRow < 0 || ((LastCol - FirstCol + 1) * (r - FirstRow + 1) * 4 < PAGE_BLOCK && r != Device->Height - 1)) continue;
  60. FirstCol *= 2;
  61. LastCol = LastCol * 2 + 1;
  62. SetRowAddress( Device, FirstRow, LastRow );
  63. SetColumnAddress( Device, FirstCol, LastCol );
  64. Device->WriteCommand( Device, ENABLE_WRITE );
  65. int ChunkSize = (LastCol - FirstCol + 1) * 2;
  66. // own use of IRAM has not proven to be much better than letting SPI do its copy
  67. if (Private->iRAM) {
  68. uint8_t *optr = Private->iRAM;
  69. for (int i = FirstRow; i <= LastRow; i++) {
  70. memcpy(optr, Private->Shadowbuffer + (i * Device->Width + FirstCol) * 2, ChunkSize);
  71. optr += ChunkSize;
  72. if (optr - Private->iRAM < PAGE_BLOCK && i < LastRow) continue;
  73. Device->WriteData(Device, Private->iRAM, optr - Private->iRAM);
  74. optr = Private->iRAM;
  75. }
  76. } else for (int i = FirstRow; i <= LastRow; i++) {
  77. Device->WriteData( Device, Private->Shadowbuffer + (i * Device->Width + FirstCol) * 2, ChunkSize );
  78. }
  79. FirstCol = Device->Width / 2; LastCol = 0;
  80. FirstRow = -1;
  81. }
  82. #else
  83. // always update by full lines
  84. SetColumnAddress( Device, 0, Device->Width - 1);
  85. for (int r = 0; r < Device->Height; r += min(Private->PageSize, Device->Height - r)) {
  86. int Height = min(Private->PageSize, Device->Height - r);
  87. SetRowAddress( Device, r, r + Height - 1 );
  88. Device->WriteCommand(Device, ENABLE_WRITE);
  89. if (Private->iRAM) {
  90. memcpy(Private->iRAM, Device->Framebuffer + r * Device->Width * 2, Height * Device->Width * 2 );
  91. Device->WriteData( Device, Private->iRAM, Height * Device->Width * 2 );
  92. } else {
  93. Device->WriteData( Device, Device->Framebuffer + r * Device->Width * 2, Height * Device->Width * 2 );
  94. }
  95. }
  96. #endif
  97. }
  98. static void Update24( struct GDS_Device* Device ) {
  99. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  100. #ifdef SHADOW_BUFFER
  101. uint16_t *optr = (uint16_t*) Private->Shadowbuffer, *iptr = (uint16_t*) Device->Framebuffer;
  102. int FirstCol = (Device->Width * 3) / 2, LastCol = 0, FirstRow = -1, LastRow = 0;
  103. for (int r = 0; r < Device->Height; r++) {
  104. // look for change and update shadow (cheap optimization = width always / by 2)
  105. for (int c = 0; c < (Device->Width * 3) / 2; c++, optr++, iptr++) {
  106. if (*optr != *iptr) {
  107. *optr = *iptr;
  108. if (c < FirstCol) FirstCol = c;
  109. if (c > LastCol) LastCol = c;
  110. if (FirstRow < 0) FirstRow = r;
  111. LastRow = r;
  112. }
  113. }
  114. // do we have enough to send (cols are divided by 3/2)
  115. if (FirstRow < 0 || ((((LastCol - FirstCol + 1) * 2 + 3 - 1) / 3) * (r - FirstRow + 1) * 3 < PAGE_BLOCK && r != Device->Height - 1)) continue;
  116. FirstCol = (FirstCol * 2) / 3;
  117. LastCol = (LastCol * 2 + 1) / 3;
  118. SetRowAddress( Device, FirstRow, LastRow );
  119. SetColumnAddress( Device, FirstCol, LastCol );
  120. Device->WriteCommand( Device, ENABLE_WRITE );
  121. int ChunkSize = (LastCol - FirstCol + 1) * 3;
  122. // own use of IRAM has not proven to be much better than letting SPI do its copy
  123. if (Private->iRAM) {
  124. uint8_t *optr = Private->iRAM;
  125. for (int i = FirstRow; i <= LastRow; i++) {
  126. memcpy(optr, Private->Shadowbuffer + (i * Device->Width + FirstCol) * 3, ChunkSize);
  127. optr += ChunkSize;
  128. if (optr - Private->iRAM < PAGE_BLOCK && i < LastRow) continue;
  129. Device->WriteData(Device, Private->iRAM, optr - Private->iRAM);
  130. optr = Private->iRAM;
  131. }
  132. } else for (int i = FirstRow; i <= LastRow; i++) {
  133. Device->WriteData( Device, Private->Shadowbuffer + (i * Device->Width + FirstCol) * 3, ChunkSize );
  134. }
  135. FirstCol = (Device->Width * 3) / 2; LastCol = 0;
  136. FirstRow = -1;
  137. }
  138. #else
  139. // always update by full lines
  140. SetColumnAddress( Device, 0, Device->Width - 1);
  141. Device->WriteCommand(Device, ENABLE_WRITE);
  142. for (int r = 0; r < Device->Height; r += Private->PageSize) {
  143. SetRowAddress( Device, r, r + Private->PageSize - 1 );
  144. if (Private->iRAM) {
  145. memcpy(Private->iRAM, Device->Framebuffer + r * Device->Width * 3, Private->PageSize * Device->Width * 3 );
  146. Device->WriteData( Device, Private->iRAM, Private->PageSize * Device->Width * 3 );
  147. } else {
  148. Device->WriteData( Device, Device->Framebuffer + r * Device->Width * 3, Private->PageSize * Device->Width * 3 );
  149. }
  150. }
  151. #endif
  152. }
  153. static void SetHFlip( struct GDS_Device* Device, bool On ) {
  154. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  155. Private->MADCtl = On ? (Private->MADCtl & ~(1 << 7)) : (Private->MADCtl | (1 << 7));
  156. Device->WriteCommand( Device, 0x36 );
  157. WriteByte( Device, Private->MADCtl );
  158. }
  159. static void SetVFlip( struct GDS_Device *Device, bool On ) {
  160. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  161. Private->MADCtl = On ? (Private->MADCtl | (1 << 6)) : (Private->MADCtl & ~(1 << 6));
  162. Device->WriteCommand( Device, 0x36 );
  163. WriteByte( Device, Private->MADCtl );
  164. }
  165. static void DisplayOn( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0x29 ); }
  166. static void DisplayOff( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0x28 ); }
  167. static void SetContrast( struct GDS_Device* Device, uint8_t Contrast ) {
  168. Device->WriteCommand( Device, 0x51 );
  169. WriteByte( Device, Contrast );
  170. }
  171. static bool Init( struct GDS_Device* Device ) {
  172. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  173. int Depth = (Device->Depth + 8 - 1) / 8;
  174. Private->PageSize = min(8, PAGE_BLOCK / (Device->Width * Depth));
  175. #ifdef SHADOW_BUFFER
  176. Private->Shadowbuffer = malloc( Device->FramebufferSize );
  177. memset(Private->Shadowbuffer, 0xFF, Device->FramebufferSize);
  178. #endif
  179. #ifdef USE_IRAM
  180. Private->iRAM = heap_caps_malloc( (Private->PageSize + 1) * Device->Width * Depth, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  181. #endif
  182. ESP_LOGI(TAG, "ST77xx with bit depth %u, page %u, iRAM %p", Device->Depth, Private->PageSize, Private->iRAM);
  183. // Sleepout + Booster
  184. Device->WriteCommand( Device, 0x11 );
  185. // need BGR & Address Mode
  186. Private->MADCtl = (1 << 3) | ((Device->Width > Device->Height ? 1 : 0) << 5);
  187. Device->WriteCommand( Device, 0x36 );
  188. WriteByte( Device, Private->MADCtl );
  189. // set flip modes & contrast
  190. GDS_SetContrast( Device, 0x7F );
  191. Device->SetVFlip( Device, false );
  192. Device->SetHFlip( Device, false );
  193. // set screen depth (16/18)
  194. Device->WriteCommand( Device, 0x3A );
  195. WriteByte( Device, Device->Depth == 24 ? 0x06 : 0x05 );
  196. // no Display Inversion
  197. Device->WriteCommand( Device, 0x20 );
  198. // gone with the wind
  199. Device->DisplayOn( Device );
  200. Device->Update( Device );
  201. return true;
  202. }
  203. static const struct GDS_Device ST77xx = {
  204. .DisplayOn = DisplayOn, .DisplayOff = DisplayOff,
  205. .SetVFlip = SetVFlip, .SetHFlip = SetHFlip,
  206. .Update = Update16, .Init = Init,
  207. .Mode = GDS_RGB565, .Depth = 16,
  208. };
  209. struct GDS_Device* ST77xx_Detect(char *Driver, struct GDS_Device* Device) {
  210. uint8_t Model;
  211. int Depth;
  212. if (strcasestr(Driver, "ST7735")) Model = ST7735;
  213. else if (strcasestr(Driver, "ST7789")) Model = ST7789;
  214. else return NULL;
  215. if (!Device) Device = calloc(1, sizeof(struct GDS_Device));
  216. *Device = ST77xx;
  217. ((struct PrivateSpace*) Device->Private)->Model = Model;
  218. sscanf(Driver, "%*[^:]:%u", &Depth);
  219. if (Depth == 18) {
  220. Device->Mode = GDS_RGB666;
  221. Device->Depth = 24;
  222. Device->Update = Update24;
  223. }
  224. if (Model == ST7789) Device->SetContrast = SetContrast;
  225. return Device;
  226. }