The official cloned µGFX library repository.

gdisp_lld_ST7565.c 7.3KB

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