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. };
  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. } else {
  164. Device->WriteCommand( Device, Layout->Invert ? 0x21 : 0x20 );
  165. }
  166. Private->MADCtl = Layout->HFlip ? (Private->MADCtl | (1 << 7)) : (Private->MADCtl & ~(1 << 7));
  167. Private->MADCtl = Layout->VFlip ? (Private->MADCtl | (1 << 6)) : (Private->MADCtl & ~(1 << 6));
  168. Private->MADCtl = Layout->Rotate ? (Private->MADCtl | (1 << 5)) : (Private->MADCtl & ~(1 << 5));
  169. Private->MADCtl = Layout->ColorSwap ? (Private->MADCtl & ~(1 << 3)) : (Private->MADCtl | (1 << 3));
  170. Device->WriteCommand( Device, 0x36 );
  171. WriteByte( Device, Private->MADCtl );
  172. #ifdef SHADOW_BUFFER
  173. // force a full refresh (almost ...)
  174. memset(Private->Shadowbuffer, 0xAA, Device->FramebufferSize);
  175. #endif
  176. }
  177. static void DisplayOn( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0x29 ); }
  178. static void DisplayOff( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0x28 ); }
  179. static void SetContrast( struct GDS_Device* Device, uint8_t Contrast ) {
  180. Device->WriteCommand( Device, 0x51 );
  181. WriteByte( Device, Contrast );
  182. Device->SetContrast = NULL;
  183. GDS_SetContrast( Device, Contrast );
  184. Device->SetContrast = SetContrast;
  185. }
  186. static bool Init( struct GDS_Device* Device ) {
  187. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  188. int Depth = (Device->Depth + 8 - 1) / 8;
  189. Private->PageSize = min(8, PAGE_BLOCK / (Device->Width * Depth));
  190. #ifdef SHADOW_BUFFER
  191. Private->Shadowbuffer = malloc( Device->FramebufferSize );
  192. memset(Private->Shadowbuffer, 0xFF, Device->FramebufferSize);
  193. #endif
  194. #ifdef USE_IRAM
  195. Private->iRAM = heap_caps_malloc( (Private->PageSize + 1) * Device->Width * Depth, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  196. #endif
  197. 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);
  198. // Sleepout + Booster
  199. Device->WriteCommand( Device, 0x11 );
  200. // need BGR & Address Mode
  201. Private->MADCtl = 1 << 3;
  202. Device->WriteCommand( Device, 0x36 );
  203. WriteByte( Device, Private->MADCtl );
  204. // set flip modes & contrast
  205. GDS_SetContrast( Device, 0x7f );
  206. struct GDS_Layout Layout = { };
  207. Device->SetLayout( Device, &Layout );
  208. // set screen depth (16/18)
  209. Device->WriteCommand( Device, 0x3A );
  210. if (Private->Model == ST7789) WriteByte( Device, Device->Depth == 24 ? 0x066 : 0x55 );
  211. else WriteByte( Device, Device->Depth == 24 ? 0x06 : 0x05 );
  212. // gone with the wind
  213. Device->DisplayOn( Device );
  214. Device->Update( Device );
  215. return true;
  216. }
  217. static const struct GDS_Device ST77xx = {
  218. .DisplayOn = DisplayOn, .DisplayOff = DisplayOff,
  219. .SetLayout = SetLayout,
  220. .Update = Update16, .Init = Init,
  221. .Mode = GDS_RGB565, .Depth = 16,
  222. };
  223. struct GDS_Device* ST77xx_Detect(char *Driver, struct GDS_Device* Device) {
  224. uint8_t Model;
  225. int Depth;
  226. if (strcasestr(Driver, "ST7735")) Model = ST7735;
  227. else if (strcasestr(Driver, "ST7789")) Model = ST7789;
  228. else return NULL;
  229. if (!Device) Device = calloc(1, sizeof(struct GDS_Device));
  230. *Device = ST77xx;
  231. sscanf(Driver, "%*[^:]:%u", &Depth);
  232. struct PrivateSpace* Private = (struct PrivateSpace*) Device->Private;
  233. Private->Model = Model;
  234. if (Model == ST7735) {
  235. sscanf(Driver, "%*[^:]%*[^x]%*[^=]=%hu", &Private->Offset.Height);
  236. sscanf(Driver, "%*[^:]%*[^y]%*[^=]=%hu", &Private->Offset.Width);
  237. }
  238. if (Depth == 18) {
  239. Device->Mode = GDS_RGB666;
  240. Device->Depth = 24;
  241. Device->Update = Update24;
  242. }
  243. if (Model == ST7789) Device->SetContrast = SetContrast;
  244. return Device;
  245. }