You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
9.6 KiB

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