ST77xx.c 11 KB

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