SSD1351.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. int ChunkSize = (LastCol - FirstCol + 1) * 2;
  63. // own use of IRAM has not proven to be much better than letting SPI do its copy
  64. if (Private->iRAM) {
  65. uint8_t *optr = Private->iRAM;
  66. for (int i = FirstRow; i <= LastRow; i++) {
  67. memcpy(optr, Private->Shadowbuffer + (i * Device->Width + FirstCol) * 2, ChunkSize);
  68. optr += ChunkSize;
  69. if (optr - Private->iRAM < PAGE_BLOCK && i < LastRow) continue;
  70. Device->WriteCommand( Device, ENABLE_WRITE );
  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->WriteCommand( Device, ENABLE_WRITE );
  76. Device->WriteData( Device, Private->Shadowbuffer + (i * Device->Width + FirstCol) * 2, ChunkSize );
  77. }
  78. FirstCol = Device->Width / 2; LastCol = 0;
  79. FirstRow = -1;
  80. }
  81. #else
  82. // always update by full lines
  83. SetColumnAddress( Device, 0, Device->Width - 1);
  84. for (int r = 0; r < Device->Height; r += min(Private->PageSize, Device->Height - r)) {
  85. int Height = min(Private->PageSize, Device->Height - r);
  86. SetRowAddress( Device, r, r + Height - 1 );
  87. if (Private->iRAM) {
  88. memcpy(Private->iRAM, Device->Framebuffer + r * Device->Width * 2, Height * Device->Width * 2 );
  89. Device->WriteCommand(Device, ENABLE_WRITE);
  90. Device->WriteData( Device, Private->iRAM, Height * Device->Width * 2 );
  91. } else {
  92. Device->WriteCommand(Device, ENABLE_WRITE);
  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. int FirstCol = (Device->Width * 3) / 2, LastCol = 0, FirstRow = -1, LastRow = 0;
  101. #ifdef SHADOW_BUFFER
  102. uint16_t *optr = (uint16_t*) Private->Shadowbuffer, *iptr = (uint16_t*) Device->Framebuffer;
  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. int ChunkSize = (LastCol - FirstCol + 1) * 3;
  121. // own use of IRAM has not proven to be much better than letting SPI do its copy
  122. if (Private->iRAM) {
  123. uint8_t *optr = Private->iRAM;
  124. for (int i = FirstRow; i <= LastRow; i++) {
  125. memcpy(optr, Private->Shadowbuffer + (i * Device->Width + FirstCol) * 3, ChunkSize);
  126. optr += ChunkSize;
  127. if (optr - Private->iRAM < PAGE_BLOCK && i < LastRow) continue;
  128. Device->WriteCommand( Device, ENABLE_WRITE );
  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->WriteCommand( Device, ENABLE_WRITE );
  134. Device->WriteData( Device, Private->Shadowbuffer + (i * Device->Width + FirstCol) * 3, ChunkSize );
  135. }
  136. FirstCol = (Device->Width * 3) / 2; LastCol = 0;
  137. FirstRow = -1;
  138. }
  139. #else
  140. // always update by full lines
  141. SetColumnAddress( Device, 0, Device->Width - 1);
  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->WriteCommand(Device, ENABLE_WRITE);
  147. Device->WriteData( Device, Private->iRAM, Private->PageSize * Device->Width * 3 );
  148. } else {
  149. Device->WriteCommand(Device, ENABLE_WRITE);
  150. Device->WriteData( Device, Device->Framebuffer + r * Device->Width * 3, Private->PageSize * Device->Width * 3 );
  151. }
  152. }
  153. #endif
  154. }
  155. static void SetHFlip( struct GDS_Device* Device, bool On ) {
  156. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  157. Private->ReMap = On ? (Private->ReMap & ~(1 << 1)) : (Private->ReMap | (1 << 1));
  158. Device->WriteCommand( Device, 0xA0 );
  159. WriteByte( Device, Private->ReMap );
  160. }
  161. static void SetVFlip( struct GDS_Device *Device, bool On ) {
  162. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  163. Private->ReMap = On ? (Private->ReMap | (1 << 4)) : (Private->ReMap & ~(1 << 4));
  164. Device->WriteCommand( Device, 0xA0 );
  165. WriteByte( Device, Private->ReMap );
  166. }
  167. static void DisplayOn( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0xAF ); }
  168. static void DisplayOff( struct GDS_Device* Device ) { Device->WriteCommand( Device, 0xAE ); }
  169. static void SetContrast( struct GDS_Device* Device, uint8_t Contrast ) {
  170. Device->WriteCommand( Device, 0xC7 );
  171. WriteByte( Device, Contrast >> 4);
  172. }
  173. static bool Init( struct GDS_Device* Device ) {
  174. struct PrivateSpace *Private = (struct PrivateSpace*) Device->Private;
  175. int Depth = (Device->Depth + 8 - 1) / 8;
  176. Private->PageSize = min(8, PAGE_BLOCK / (Device->Width * Depth));
  177. #ifdef SHADOW_BUFFER
  178. Private->Shadowbuffer = malloc( Device->FramebufferSize );
  179. memset(Private->Shadowbuffer, 0xFF, Device->FramebufferSize);
  180. #endif
  181. #ifdef USE_IRAM
  182. Private->iRAM = heap_caps_malloc( (Private->PageSize + 1) * Device->Width * Depth, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA );
  183. #endif
  184. ESP_LOGI(TAG, "SSD1351 with bit depth %u, page %u, iRAM %p", Device->Depth, Private->PageSize, Private->iRAM);
  185. // unlock (specially 0xA2)
  186. Device->WriteCommand( Device, 0xFD);
  187. WriteByte(Device, 0xB1);
  188. // set clocks
  189. /*
  190. Device->WriteCommand( Device, 0xB3 );
  191. WriteByte( Device, ( 0x08 << 4 ) | 0x00 );
  192. */
  193. // need to be off and disable display RAM
  194. Device->DisplayOff( Device );
  195. // need COM split (5)
  196. Private->ReMap = (1 << 5);
  197. // Display Offset
  198. Device->WriteCommand( Device, 0xA2 );
  199. WriteByte( Device, 0x00 );
  200. // Display Start Line
  201. Device->WriteCommand( Device, 0xA1 );
  202. WriteByte( Device, 0x00 );
  203. // set flip modes & contrast
  204. Device->SetContrast( Device, 0x7F );
  205. Device->SetVFlip( Device, false );
  206. Device->SetHFlip( Device, false );
  207. // set Adressing Mode Horizontal
  208. Private->ReMap |= (0 << 2);
  209. // set screen depth (16/18)
  210. if (Device->Depth == 24) Private->ReMap |= (0x02 << 6);
  211. // write ReMap byte
  212. Device->WriteCommand( Device, 0xA0 );
  213. WriteByte( Device, Private->ReMap );
  214. // no Display Inversion
  215. Device->WriteCommand( Device, 0xA6 );
  216. // gone with the wind
  217. Device->DisplayOn( Device );
  218. Device->Update( Device );
  219. return true;
  220. }
  221. static const struct GDS_Device SSD1351 = {
  222. .DisplayOn = DisplayOn, .DisplayOff = DisplayOff, .SetContrast = SetContrast,
  223. .SetVFlip = SetVFlip, .SetHFlip = SetHFlip,
  224. .Update = Update16, .Init = Init,
  225. .Mode = GDS_RGB565, .Depth = 16,
  226. };
  227. struct GDS_Device* SSD1351_Detect(char *Driver, struct GDS_Device* Device) {
  228. int Depth;
  229. if (!strcasestr(Driver, "SSD1351")) return NULL;
  230. if (!Device) Device = calloc(1, sizeof(struct GDS_Device));
  231. *Device = SSD1351;
  232. sscanf(Driver, "%*[^:]:%u", &Depth);
  233. if (Depth == 18) {
  234. Device->Mode = GDS_RGB666;
  235. Device->Depth = 24;
  236. Device->Update = Update24;
  237. }
  238. return Device;
  239. }