gdisp_lld_ST7565.c 10 KB

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