ST77xx.c 10 KB

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