gdisp_lld_ST7565.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * This file is subject to the terms of the GFX License. If a copy of
  3. * the license was not distributed with this file, you can obtain one at:
  4. *
  5. * http://ugfx.org/license.html
  6. */
  7. #include "gfx.h"
  8. #if GFX_USE_GDISP
  9. #define GDISP_DRIVER_VMT GDISPVMT_ST7565_QMK
  10. #include "gdisp_lld_config.h"
  11. #include "src/gdisp/gdisp_driver.h"
  12. #include "board_st7565.h"
  13. /*===========================================================================*/
  14. /* Driver local definitions. */
  15. /*===========================================================================*/
  16. #pragma once
  17. #endif
  18. #ifndef GDISP_SCREEN_WIDTH
  19. #define GDISP_SCREEN_WIDTH LCD_WIDTH
  20. #endif
  21. #ifndef GDISP_INITIAL_CONTRAST
  22. #define GDISP_INITIAL_CONTRAST 35
  23. #endif
  24. #ifndef GDISP_INITIAL_BACKLIGHT
  25. #define GDISP_INITIAL_BACKLIGHT 100
  26. #endif
  27. #define GDISP_FLG_NEEDFLUSH (GDISP_FLG_DRIVER<<0)
  28. #include "st7565.h"
  29. /*===========================================================================*/
  30. /* Driver config defaults for backward compatibility. */
  31. /*===========================================================================*/
  32. #ifndef ST7565_LCD_BIAS
  33. #define ST7565_LCD_BIAS ST7565_LCD_BIAS_7
  34. #endif
  35. #ifndef ST7565_ADC
  36. #define ST7565_ADC ST7565_ADC_NORMAL
  37. #endif
  38. #ifndef ST7565_COM_SCAN
  39. #define ST7565_COM_SCAN ST7565_COM_SCAN_INC
  40. #endif
  41. #ifndef ST7565_PAGE_ORDER
  42. #define ST7565_PAGE_ORDER 0,1,2,3
  43. #endif
  44. /*===========================================================================*/
  45. /* Driver local functions. */
  46. /*===========================================================================*/
  47. typedef struct{
  48. bool_t buffer2;
  49. uint8_t data_pos;
  50. uint8_t data[16];
  51. uint8_t ram[GDISP_SCREEN_HEIGHT * GDISP_SCREEN_WIDTH / 8];
  52. }PrivData;
  53. // Some common routines and macros
  54. #define PRIV(g) ((PrivData*)g->priv)
  55. #define RAM(g) (PRIV(g)->ram)
  56. static GFXINLINE void write_cmd(GDisplay* g, uint8_t cmd) {
  57. PRIV(g)->data[PRIV(g)->data_pos++] = cmd;
  58. }
  59. static GFXINLINE void flush_cmd(GDisplay* g) {
  60. write_data(g, PRIV(g)->data, PRIV(g)->data_pos);
  61. PRIV(g)->data_pos = 0;
  62. }
  63. #define write_cmd2(g, cmd1, cmd2) { write_cmd(g, cmd1); write_cmd(g, cmd2); }
  64. #define write_cmd3(g, cmd1, cmd2, cmd3) { write_cmd(g, cmd1); write_cmd(g, cmd2); write_cmd(g, cmd3); }
  65. // Some common routines and macros
  66. #define delay(us) gfxSleepMicroseconds(us)
  67. #define delay_ms(ms) gfxSleepMilliseconds(ms)
  68. #define xyaddr(x, y) ((x) + ((y)>>3)*GDISP_SCREEN_WIDTH)
  69. #define xybit(y) (1<<((y)&7))
  70. /*===========================================================================*/
  71. /* Driver exported functions. */
  72. /*===========================================================================*/
  73. /*
  74. * As this controller can't update on a pixel boundary we need to maintain the
  75. * the entire display surface in memory so that we can do the necessary bit
  76. * operations. Fortunately it is a small display in monochrome.
  77. * 64 * 128 / 8 = 1024 bytes.
  78. */
  79. LLDSPEC bool_t gdisp_lld_init(GDisplay *g) {
  80. // The private area is the display surface.
  81. g->priv = gfxAlloc(sizeof(PrivData));
  82. PRIV(g)->buffer2 = false;
  83. PRIV(g)->data_pos = 0;
  84. // Initialise the board interface
  85. init_board(g);
  86. // Hardware reset
  87. setpin_reset(g, TRUE);
  88. gfxSleepMilliseconds(20);
  89. setpin_reset(g, FALSE);
  90. gfxSleepMilliseconds(20);
  91. acquire_bus(g);
  92. enter_cmd_mode(g);
  93. write_cmd(g, ST7565_RESET);
  94. write_cmd(g, ST7565_LCD_BIAS);
  95. write_cmd(g, ST7565_ADC);
  96. write_cmd(g, ST7565_COM_SCAN);
  97. write_cmd(g, ST7565_RESISTOR_RATIO | 0x1);
  98. write_cmd2(g, ST7565_CONTRAST, GDISP_INITIAL_CONTRAST);
  99. // turn on internal power supply (VC=1, VR=1, VF=1)
  100. write_cmd(g, ST7565_POWER_CONTROL | 0x07);
  101. write_cmd(g, ST7565_INVERT_DISPLAY);
  102. write_cmd(g, ST7565_ALLON_NORMAL);
  103. write_cmd(g, ST7565_START_LINE | 0);
  104. write_cmd(g, ST7565_RMW);
  105. flush_cmd(g);
  106. // Finish Init
  107. post_init_board(g);
  108. // Release the bus
  109. release_bus(g);
  110. /* Initialise the GDISP structure */
  111. g->g.Width = GDISP_SCREEN_WIDTH;
  112. g->g.Height = GDISP_SCREEN_HEIGHT;
  113. g->g.Orientation = GDISP_ROTATE_0;
  114. g->g.Powermode = powerOff;
  115. g->g.Backlight = GDISP_INITIAL_BACKLIGHT;
  116. g->g.Contrast = GDISP_INITIAL_CONTRAST;
  117. return TRUE;
  118. }
  119. #if GDISP_HARDWARE_FLUSH
  120. LLDSPEC void gdisp_lld_flush(GDisplay *g) {
  121. unsigned p;
  122. // Don't flush if we don't need it.
  123. if (!(g->flags & GDISP_FLG_NEEDFLUSH))
  124. return;
  125. acquire_bus(g);
  126. enter_cmd_mode(g);
  127. unsigned dstOffset = (PRIV(g)->buffer2 ? 4 : 0);
  128. for (p = 0; p < 4; p++) {
  129. write_cmd(g, ST7565_PAGE | (p + dstOffset));
  130. write_cmd(g, ST7565_COLUMN_MSB | 0);
  131. write_cmd(g, ST7565_COLUMN_LSB | 0);
  132. write_cmd(g, ST7565_RMW);
  133. flush_cmd(g);
  134. enter_data_mode(g);
  135. write_data(g, RAM(g) + (p*GDISP_SCREEN_WIDTH), GDISP_SCREEN_WIDTH);
  136. enter_cmd_mode(g);
  137. }
  138. unsigned line = (PRIV(g)->buffer2 ? 32 : 0);
  139. write_cmd(g, ST7565_START_LINE | line);
  140. flush_cmd(g);
  141. PRIV(g)->buffer2 = !PRIV(g)->buffer2;
  142. release_bus(g);
  143. g->flags &= ~GDISP_FLG_NEEDFLUSH;
  144. }
  145. #endif
  146. #if GDISP_HARDWARE_DRAWPIXEL
  147. LLDSPEC void gdisp_lld_draw_pixel(GDisplay *g) {
  148. coord_t x, y;
  149. switch(g->g.Orientation) {
  150. default:
  151. case GDISP_ROTATE_0:
  152. x = g->p.x;
  153. y = g->p.y;
  154. break;
  155. case GDISP_ROTATE_90:
  156. x = g->p.y;
  157. y = GDISP_SCREEN_HEIGHT-1 - g->p.x;
  158. break;
  159. case GDISP_ROTATE_180:
  160. x = GDISP_SCREEN_WIDTH-1 - g->p.x;
  161. y = GDISP_SCREEN_HEIGHT-1 - g->p.y;
  162. break;
  163. case GDISP_ROTATE_270:
  164. x = GDISP_SCREEN_HEIGHT-1 - g->p.y;
  165. y = g->p.x;
  166. break;
  167. }
  168. if (gdispColor2Native(g->p.color) != Black)
  169. RAM(g)[xyaddr(x, y)] |= xybit(y);
  170. else
  171. RAM(g)[xyaddr(x, y)] &= ~xybit(y);
  172. g->flags |= GDISP_FLG_NEEDFLUSH;
  173. }
  174. #endif
  175. #if GDISP_HARDWARE_PIXELREAD
  176. LLDSPEC color_t gdisp_lld_get_pixel_color(GDisplay *g) {
  177. coord_t x, y;
  178. switch(g->g.Orientation) {
  179. default:
  180. case GDISP_ROTATE_0:
  181. x = g->p.x;
  182. y = g->p.y;
  183. break;
  184. case GDISP_ROTATE_90:
  185. x = g->p.y;
  186. y = GDISP_SCREEN_HEIGHT-1 - g->p.x;
  187. break;
  188. case GDISP_ROTATE_180:
  189. x = GDISP_SCREEN_WIDTH-1 - g->p.x;
  190. y = GDISP_SCREEN_HEIGHT-1 - g->p.y;
  191. break;
  192. case GDISP_ROTATE_270:
  193. x = GDISP_SCREEN_HEIGHT-1 - g->p.y;
  194. y = g->p.x;
  195. break;
  196. }
  197. return (RAM(g)[xyaddr(x, y)] & xybit(y)) ? White : Black;
  198. }
  199. #endif
  200. LLDSPEC void gdisp_lld_blit_area(GDisplay *g) {
  201. uint8_t* buffer = (uint8_t*)g->p.ptr;
  202. int linelength = g->p.cx;
  203. for (int i = 0; i < g->p.cy; i++) {
  204. unsigned dstx = g->p.x;
  205. unsigned dsty = g->p.y + i;
  206. unsigned srcx = g->p.x1;
  207. unsigned srcy = g->p.y1 + i;
  208. unsigned srcbit = srcy * g->p.x2 + srcx;
  209. for(int j=0; j < linelength; j++) {
  210. uint8_t src = buffer[srcbit / 8];
  211. uint8_t bit = 7-(srcbit % 8);
  212. uint8_t bitset = (src >> bit) & 1;
  213. uint8_t* dst = &(RAM(g)[xyaddr(dstx, dsty)]);
  214. if (bitset) {
  215. *dst |= xybit(dsty);
  216. }
  217. else {
  218. *dst &= ~xybit(dsty);
  219. }
  220. dstx++;
  221. srcbit++;
  222. }
  223. }
  224. g->flags |= GDISP_FLG_NEEDFLUSH;
  225. }
  226. #if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
  227. LLDSPEC void gdisp_lld_control(GDisplay *g) {
  228. switch(g->p.x) {
  229. case GDISP_CONTROL_POWER:
  230. if (g->g.Powermode == (powermode_t)g->p.ptr)
  231. return;
  232. switch((powermode_t)g->p.ptr) {
  233. case powerOff:
  234. case powerSleep:
  235. case powerDeepSleep:
  236. acquire_bus(g);
  237. enter_cmd_mode(g);
  238. write_cmd(g, ST7565_DISPLAY_OFF);
  239. flush_cmd(g);
  240. release_bus(g);
  241. break;
  242. case powerOn:
  243. acquire_bus(g);
  244. enter_cmd_mode(g);
  245. write_cmd(g, ST7565_DISPLAY_ON);
  246. flush_cmd(g);
  247. release_bus(g);
  248. break;
  249. default:
  250. return;
  251. }
  252. g->g.Powermode = (powermode_t)g->p.ptr;
  253. return;
  254. case GDISP_CONTROL_ORIENTATION:
  255. if (g->g.Orientation == (orientation_t)g->p.ptr)
  256. return;
  257. switch((orientation_t)g->p.ptr) {
  258. /* Rotation is handled by the drawing routines */
  259. case GDISP_ROTATE_0:
  260. case GDISP_ROTATE_180:
  261. g->g.Height = GDISP_SCREEN_HEIGHT;
  262. g->g.Width = GDISP_SCREEN_WIDTH;
  263. break;
  264. case GDISP_ROTATE_90:
  265. case GDISP_ROTATE_270:
  266. g->g.Height = GDISP_SCREEN_WIDTH;
  267. g->g.Width = GDISP_SCREEN_HEIGHT;
  268. break;
  269. default:
  270. return;
  271. }
  272. g->g.Orientation = (orientation_t)g->p.ptr;
  273. return;
  274. case GDISP_CONTROL_CONTRAST:
  275. g->g.Contrast = (unsigned)g->p.ptr & 63;
  276. acquire_bus(g);
  277. enter_cmd_mode(g);
  278. write_cmd2(g, ST7565_CONTRAST, g->g.Contrast);
  279. flush_cmd(g);
  280. release_bus(g);
  281. return;
  282. }
  283. }
  284. #endif // GDISP_NEED_CONTROL