gdisp_lld_ST7565.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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_ERGODOX
  10. #include "drivers/gdisp/st7565ergodox/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 32
  18. #endif
  19. #ifndef GDISP_SCREEN_WIDTH
  20. #define GDISP_SCREEN_WIDTH 128
  21. #endif
  22. #ifndef GDISP_INITIAL_CONTRAST
  23. #define GDISP_INITIAL_CONTRAST 0
  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 "drivers/gdisp/st7565ergodox/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 ram[GDISP_SCREEN_HEIGHT * GDISP_SCREEN_WIDTH / 8];
  51. }PrivData;
  52. // Some common routines and macros
  53. #define PRIV(g) ((PrivData*)g->priv)
  54. #define RAM(g) (PRIV(g)->ram)
  55. #define write_cmd2(g, cmd1, cmd2) { write_cmd(g, cmd1); write_cmd(g, cmd2); }
  56. #define write_cmd3(g, cmd1, cmd2, cmd3) { write_cmd(g, cmd1); write_cmd(g, cmd2); write_cmd(g, cmd3); }
  57. // Some common routines and macros
  58. #define delay(us) gfxSleepMicroseconds(us)
  59. #define delay_ms(ms) gfxSleepMilliseconds(ms)
  60. #define xyaddr(x, y) ((x) + ((y)>>3)*GDISP_SCREEN_WIDTH)
  61. #define xybit(y) (1<<((y)&7))
  62. /*===========================================================================*/
  63. /* Driver exported functions. */
  64. /*===========================================================================*/
  65. /*
  66. * As this controller can't update on a pixel boundary we need to maintain the
  67. * the entire display surface in memory so that we can do the necessary bit
  68. * operations. Fortunately it is a small display in monochrome.
  69. * 64 * 128 / 8 = 1024 bytes.
  70. */
  71. LLDSPEC bool_t gdisp_lld_init(GDisplay *g) {
  72. // The private area is the display surface.
  73. g->priv = gfxAlloc(sizeof(PrivData));
  74. PRIV(g)->buffer2 = false;
  75. // Initialise the board interface
  76. init_board(g);
  77. // Hardware reset
  78. setpin_reset(g, TRUE);
  79. gfxSleepMilliseconds(20);
  80. setpin_reset(g, FALSE);
  81. gfxSleepMilliseconds(20);
  82. acquire_bus(g);
  83. write_cmd(g, ST7565_DISPLAY_OFF);
  84. write_cmd(g, ST7565_LCD_BIAS);
  85. write_cmd(g, ST7565_ADC);
  86. write_cmd(g, ST7565_COM_SCAN);
  87. write_cmd(g, ST7565_START_LINE | 0);
  88. write_cmd(g, ST7565_RESISTOR_RATIO | 0x6);
  89. // turn on voltage converter (VC=1, VR=0, VF=0)
  90. write_cmd(g, ST7565_POWER_CONTROL | 0x04);
  91. delay_ms(50);
  92. // turn on voltage regulator (VC=1, VR=1, VF=0)
  93. write_cmd(g, ST7565_POWER_CONTROL | 0x06);
  94. delay_ms(50);
  95. // turn on voltage follower (VC=1, VR=1, VF=1)
  96. write_cmd(g, ST7565_POWER_CONTROL | 0x07);
  97. delay_ms(50);
  98. write_cmd(g, 0xE2);
  99. write_cmd(g, ST7565_COM_SCAN);
  100. write_cmd2(g, ST7565_CONTRAST, GDISP_INITIAL_CONTRAST*64/101);
  101. //write_cmd2(g, ST7565_CONTRAST, 0);
  102. write_cmd(g, ST7565_DISPLAY_ON);
  103. write_cmd(g, ST7565_ALLON_NORMAL);
  104. write_cmd(g, ST7565_INVERT_DISPLAY);
  105. write_cmd(g, ST7565_RMW);
  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 = powerOn;
  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. unsigned dstOffset = (PRIV(g)->buffer2 ? 4 : 0);
  127. for (p = 0; p < 4; p++) {
  128. write_cmd(g, ST7565_PAGE | (p + dstOffset));
  129. write_cmd(g, ST7565_COLUMN_MSB | 0);
  130. write_cmd(g, ST7565_COLUMN_LSB | 0);
  131. write_cmd(g, ST7565_RMW);
  132. write_data(g, RAM(g) + (p*GDISP_SCREEN_WIDTH), GDISP_SCREEN_WIDTH);
  133. }
  134. unsigned line = (PRIV(g)->buffer2 ? 32 : 0);
  135. write_cmd(g, ST7565_START_LINE | line);
  136. PRIV(g)->buffer2 = !PRIV(g)->buffer2;
  137. release_bus(g);
  138. g->flags &= ~GDISP_FLG_NEEDFLUSH;
  139. }
  140. #endif
  141. #if GDISP_HARDWARE_DRAWPIXEL
  142. LLDSPEC void gdisp_lld_draw_pixel(GDisplay *g) {
  143. coord_t x, y;
  144. switch(g->g.Orientation) {
  145. default:
  146. case GDISP_ROTATE_0:
  147. x = g->p.x;
  148. y = g->p.y;
  149. break;
  150. case GDISP_ROTATE_90:
  151. x = g->p.y;
  152. y = GDISP_SCREEN_HEIGHT-1 - g->p.x;
  153. break;
  154. case GDISP_ROTATE_180:
  155. x = GDISP_SCREEN_WIDTH-1 - g->p.x;
  156. y = GDISP_SCREEN_HEIGHT-1 - g->p.y;
  157. break;
  158. case GDISP_ROTATE_270:
  159. x = GDISP_SCREEN_HEIGHT-1 - g->p.y;
  160. y = g->p.x;
  161. break;
  162. }
  163. if (gdispColor2Native(g->p.color) != Black)
  164. RAM(g)[xyaddr(x, y)] |= xybit(y);
  165. else
  166. RAM(g)[xyaddr(x, y)] &= ~xybit(y);
  167. g->flags |= GDISP_FLG_NEEDFLUSH;
  168. }
  169. #endif
  170. #if GDISP_HARDWARE_PIXELREAD
  171. LLDSPEC color_t gdisp_lld_get_pixel_color(GDisplay *g) {
  172. coord_t x, y;
  173. switch(g->g.Orientation) {
  174. default:
  175. case GDISP_ROTATE_0:
  176. x = g->p.x;
  177. y = g->p.y;
  178. break;
  179. case GDISP_ROTATE_90:
  180. x = g->p.y;
  181. y = GDISP_SCREEN_HEIGHT-1 - g->p.x;
  182. break;
  183. case GDISP_ROTATE_180:
  184. x = GDISP_SCREEN_WIDTH-1 - g->p.x;
  185. y = GDISP_SCREEN_HEIGHT-1 - g->p.y;
  186. break;
  187. case GDISP_ROTATE_270:
  188. x = GDISP_SCREEN_HEIGHT-1 - g->p.y;
  189. y = g->p.x;
  190. break;
  191. }
  192. return (RAM(g)[xyaddr(x, y)] & xybit(y)) ? White : Black;
  193. }
  194. #endif
  195. #if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
  196. LLDSPEC void gdisp_lld_control(GDisplay *g) {
  197. switch(g->p.x) {
  198. case GDISP_CONTROL_POWER:
  199. if (g->g.Powermode == (powermode_t)g->p.ptr)
  200. return;
  201. switch((powermode_t)g->p.ptr) {
  202. case powerOff:
  203. case powerSleep:
  204. case powerDeepSleep:
  205. acquire_bus(g);
  206. write_cmd(g, ST7565_DISPLAY_OFF);
  207. release_bus(g);
  208. break;
  209. case powerOn:
  210. acquire_bus(g);
  211. write_cmd(g, ST7565_DISPLAY_ON);
  212. release_bus(g);
  213. break;
  214. default:
  215. return;
  216. }
  217. g->g.Powermode = (powermode_t)g->p.ptr;
  218. return;
  219. case GDISP_CONTROL_ORIENTATION:
  220. if (g->g.Orientation == (orientation_t)g->p.ptr)
  221. return;
  222. switch((orientation_t)g->p.ptr) {
  223. /* Rotation is handled by the drawing routines */
  224. case GDISP_ROTATE_0:
  225. case GDISP_ROTATE_180:
  226. g->g.Height = GDISP_SCREEN_HEIGHT;
  227. g->g.Width = GDISP_SCREEN_WIDTH;
  228. break;
  229. case GDISP_ROTATE_90:
  230. case GDISP_ROTATE_270:
  231. g->g.Height = GDISP_SCREEN_WIDTH;
  232. g->g.Width = GDISP_SCREEN_HEIGHT;
  233. break;
  234. default:
  235. return;
  236. }
  237. g->g.Orientation = (orientation_t)g->p.ptr;
  238. return;
  239. case GDISP_CONTROL_CONTRAST:
  240. if ((unsigned)g->p.ptr > 100)
  241. g->p.ptr = (void *)100;
  242. acquire_bus(g);
  243. write_cmd2(g, ST7565_CONTRAST, ((((unsigned)g->p.ptr)<<6)/101) & 0x3F);
  244. release_bus(g);
  245. g->g.Contrast = (unsigned)g->p.ptr;
  246. return;
  247. }
  248. }
  249. #endif // GDISP_NEED_CONTROL
  250. #endif // GFX_USE_GDISP