gdisp_lld_ST7565.c 9.5 KB

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