SSD1351.c 9.2 KB

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