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:
5 * http://ugfx.org/license.html
12 #define GDISP_DRIVER_VMT GDISPVMT_ST7565_ERGODOX
13 #include "drivers/gdisp/st7565ergodox/gdisp_lld_config.h"
14 #include "src/gdisp/gdisp_driver.h"
16 #include "board_ST7565.h"
18 /*===========================================================================*/
19 /* Driver local definitions. */
20 /*===========================================================================*/
22 #ifndef GDISP_SCREEN_HEIGHT
23 #define GDISP_SCREEN_HEIGHT 32
25 #ifndef GDISP_SCREEN_WIDTH
26 #define GDISP_SCREEN_WIDTH 128
28 #ifndef GDISP_INITIAL_CONTRAST
29 #define GDISP_INITIAL_CONTRAST 35
31 #ifndef GDISP_INITIAL_BACKLIGHT
32 #define GDISP_INITIAL_BACKLIGHT 100
35 #define GDISP_FLG_NEEDFLUSH (GDISP_FLG_DRIVER<<0)
37 #include "drivers/gdisp/st7565ergodox/st7565.h"
39 /*===========================================================================*/
40 /* Driver config defaults for backward compatibility. */
41 /*===========================================================================*/
42 #ifndef ST7565_LCD_BIAS
43 #define ST7565_LCD_BIAS ST7565_LCD_BIAS_7
46 #define ST7565_ADC ST7565_ADC_NORMAL
48 #ifndef ST7565_COM_SCAN
49 #define ST7565_COM_SCAN ST7565_COM_SCAN_INC
51 #ifndef ST7565_PAGE_ORDER
52 #define ST7565_PAGE_ORDER 0,1,2,3
55 /*===========================================================================*/
56 /* Driver local functions. */
57 /*===========================================================================*/
63 uint8_t ram[GDISP_SCREEN_HEIGHT * GDISP_SCREEN_WIDTH / 8];
66 // Some common routines and macros
67 #define PRIV(g) ((PrivData*)g->priv)
68 #define RAM(g) (PRIV(g)->ram)
70 static GFXINLINE void write_cmd(GDisplay* g, uint8_t cmd) {
71 PRIV(g)->data[PRIV(g)->data_pos++] = cmd;
74 static GFXINLINE void flush_cmd(GDisplay* g) {
75 write_data(g, PRIV(g)->data, PRIV(g)->data_pos);
76 PRIV(g)->data_pos = 0;
79 #define write_cmd2(g, cmd1, cmd2) { write_cmd(g, cmd1); write_cmd(g, cmd2); }
80 #define write_cmd3(g, cmd1, cmd2, cmd3) { write_cmd(g, cmd1); write_cmd(g, cmd2); write_cmd(g, cmd3); }
82 // Some common routines and macros
83 #define delay(us) gfxSleepMicroseconds(us)
84 #define delay_ms(ms) gfxSleepMilliseconds(ms)
86 #define xyaddr(x, y) ((x) + ((y)>>3)*GDISP_SCREEN_WIDTH)
87 #define xybit(y) (1<<((y)&7))
89 /*===========================================================================*/
90 /* Driver exported functions. */
91 /*===========================================================================*/
94 * As this controller can't update on a pixel boundary we need to maintain the
95 * the entire display surface in memory so that we can do the necessary bit
96 * operations. Fortunately it is a small display in monochrome.
97 * 64 * 128 / 8 = 1024 bytes.
100 LLDSPEC bool_t gdisp_lld_init(GDisplay *g) {
101 // The private area is the display surface.
102 g->priv = gfxAlloc(sizeof(PrivData));
103 PRIV(g)->buffer2 = false;
104 PRIV(g)->data_pos = 0;
106 // Initialise the board interface
110 setpin_reset(g, TRUE);
111 gfxSleepMilliseconds(20);
112 setpin_reset(g, FALSE);
113 gfxSleepMilliseconds(20);
117 write_cmd(g, ST7565_RESET);
118 write_cmd(g, ST7565_LCD_BIAS);
119 write_cmd(g, ST7565_ADC);
120 write_cmd(g, ST7565_COM_SCAN);
122 write_cmd(g, ST7565_RESISTOR_RATIO | 0x1);
123 write_cmd2(g, ST7565_CONTRAST, GDISP_INITIAL_CONTRAST);
125 // turn on internal power supply (VC=1, VR=1, VF=1)
126 write_cmd(g, ST7565_POWER_CONTROL | 0x07);
128 write_cmd(g, ST7565_INVERT_DISPLAY);
129 write_cmd(g, ST7565_ALLON_NORMAL);
130 write_cmd(g, ST7565_DISPLAY_ON);
132 write_cmd(g, ST7565_START_LINE | 0);
133 write_cmd(g, ST7565_RMW);
142 /* Initialise the GDISP structure */
143 g->g.Width = GDISP_SCREEN_WIDTH;
144 g->g.Height = GDISP_SCREEN_HEIGHT;
145 g->g.Orientation = GDISP_ROTATE_0;
146 g->g.Powermode = powerOn;
147 g->g.Backlight = GDISP_INITIAL_BACKLIGHT;
148 g->g.Contrast = GDISP_INITIAL_CONTRAST;
152 #if GDISP_HARDWARE_FLUSH
153 LLDSPEC void gdisp_lld_flush(GDisplay *g) {
156 // Don't flush if we don't need it.
157 if (!(g->flags & GDISP_FLG_NEEDFLUSH))
162 unsigned dstOffset = (PRIV(g)->buffer2 ? 4 : 0);
163 for (p = 0; p < 4; p++) {
164 write_cmd(g, ST7565_PAGE | (p + dstOffset));
165 write_cmd(g, ST7565_COLUMN_MSB | 0);
166 write_cmd(g, ST7565_COLUMN_LSB | 0);
167 write_cmd(g, ST7565_RMW);
170 write_data(g, RAM(g) + (p*GDISP_SCREEN_WIDTH), GDISP_SCREEN_WIDTH);
173 unsigned line = (PRIV(g)->buffer2 ? 32 : 0);
174 write_cmd(g, ST7565_START_LINE | line);
176 PRIV(g)->buffer2 = !PRIV(g)->buffer2;
179 g->flags &= ~GDISP_FLG_NEEDFLUSH;
183 #if GDISP_HARDWARE_DRAWPIXEL
184 LLDSPEC void gdisp_lld_draw_pixel(GDisplay *g) {
187 switch(g->g.Orientation) {
193 case GDISP_ROTATE_90:
195 y = GDISP_SCREEN_HEIGHT-1 - g->p.x;
197 case GDISP_ROTATE_180:
198 x = GDISP_SCREEN_WIDTH-1 - g->p.x;
199 y = GDISP_SCREEN_HEIGHT-1 - g->p.y;
201 case GDISP_ROTATE_270:
202 x = GDISP_SCREEN_HEIGHT-1 - g->p.y;
206 if (gdispColor2Native(g->p.color) != Black)
207 RAM(g)[xyaddr(x, y)] |= xybit(y);
209 RAM(g)[xyaddr(x, y)] &= ~xybit(y);
210 g->flags |= GDISP_FLG_NEEDFLUSH;
214 #if GDISP_HARDWARE_PIXELREAD
215 LLDSPEC color_t gdisp_lld_get_pixel_color(GDisplay *g) {
218 switch(g->g.Orientation) {
224 case GDISP_ROTATE_90:
226 y = GDISP_SCREEN_HEIGHT-1 - g->p.x;
228 case GDISP_ROTATE_180:
229 x = GDISP_SCREEN_WIDTH-1 - g->p.x;
230 y = GDISP_SCREEN_HEIGHT-1 - g->p.y;
232 case GDISP_ROTATE_270:
233 x = GDISP_SCREEN_HEIGHT-1 - g->p.y;
237 return (RAM(g)[xyaddr(x, y)] & xybit(y)) ? White : Black;
241 LLDSPEC void gdisp_lld_blit_area(GDisplay *g) {
242 uint8_t* buffer = (uint8_t*)g->p.ptr;
243 int linelength = g->p.cx;
244 for (int i = 0; i < g->p.cy; i++) {
245 unsigned dstx = g->p.x;
246 unsigned dsty = g->p.y + i;
247 unsigned srcx = g->p.x1;
248 unsigned srcy = g->p.y1 + i;
249 unsigned srcbit = srcy * g->p.x2 + srcx;
250 for(int j=0; j < linelength; j++) {
251 uint8_t src = buffer[srcbit / 8];
252 uint8_t bit = 7-(srcbit % 8);
253 uint8_t bitset = (src >> bit) & 1;
254 uint8_t* dst = &(RAM(g)[xyaddr(dstx, dsty)]);
259 *dst &= ~xybit(dsty);
267 #if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
268 LLDSPEC void gdisp_lld_control(GDisplay *g) {
270 case GDISP_CONTROL_POWER:
271 if (g->g.Powermode == (powermode_t)g->p.ptr)
273 switch((powermode_t)g->p.ptr) {
279 write_cmd(g, ST7565_DISPLAY_OFF);
286 write_cmd(g, ST7565_DISPLAY_ON);
293 g->g.Powermode = (powermode_t)g->p.ptr;
296 case GDISP_CONTROL_ORIENTATION:
297 if (g->g.Orientation == (orientation_t)g->p.ptr)
299 switch((orientation_t)g->p.ptr) {
300 /* Rotation is handled by the drawing routines */
302 case GDISP_ROTATE_180:
303 g->g.Height = GDISP_SCREEN_HEIGHT;
304 g->g.Width = GDISP_SCREEN_WIDTH;
306 case GDISP_ROTATE_90:
307 case GDISP_ROTATE_270:
308 g->g.Height = GDISP_SCREEN_WIDTH;
309 g->g.Width = GDISP_SCREEN_HEIGHT;
314 g->g.Orientation = (orientation_t)g->p.ptr;
317 case GDISP_CONTROL_CONTRAST:
318 g->g.Contrast = (unsigned)g->p.ptr & 63;
321 write_cmd2(g, ST7565_CONTRAST, g->g.Contrast);
327 #endif // GDISP_NEED_CONTROL
329 #endif // GFX_USE_GDISP