]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/ergodox/infinity/drivers/gdisp/st7565ergodox/gdisp_lld_ST7565.c
LCD initialization sequence according to the docs
[qmk_firmware.git] / keyboards / ergodox / infinity / drivers / gdisp / st7565ergodox / gdisp_lld_ST7565.c
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
8 #include "gfx.h"
9
10 #if GFX_USE_GDISP
11
12 #define GDISP_DRIVER_VMT                        GDISPVMT_ST7565_ERGODOX
13 #include "drivers/gdisp/st7565ergodox/gdisp_lld_config.h"
14 #include "src/gdisp/gdisp_driver.h"
15
16 #include "board_ST7565.h"
17
18 /*===========================================================================*/
19 /* Driver local definitions.                                                 */
20 /*===========================================================================*/
21
22 #ifndef GDISP_SCREEN_HEIGHT
23 #define GDISP_SCREEN_HEIGHT             32
24 #endif
25 #ifndef GDISP_SCREEN_WIDTH
26 #define GDISP_SCREEN_WIDTH              128
27 #endif
28 #ifndef GDISP_INITIAL_CONTRAST
29 #define GDISP_INITIAL_CONTRAST  35
30 #endif
31 #ifndef GDISP_INITIAL_BACKLIGHT
32 #define GDISP_INITIAL_BACKLIGHT 100
33 #endif
34
35 #define GDISP_FLG_NEEDFLUSH                     (GDISP_FLG_DRIVER<<0)
36
37 #include "drivers/gdisp/st7565ergodox/st7565.h"
38
39 /*===========================================================================*/
40 /* Driver config defaults for backward compatibility.                        */
41 /*===========================================================================*/
42 #ifndef ST7565_LCD_BIAS
43 #define ST7565_LCD_BIAS         ST7565_LCD_BIAS_7
44 #endif
45 #ifndef ST7565_ADC
46 #define ST7565_ADC              ST7565_ADC_NORMAL
47 #endif
48 #ifndef ST7565_COM_SCAN
49 #define ST7565_COM_SCAN         ST7565_COM_SCAN_INC
50 #endif
51 #ifndef ST7565_PAGE_ORDER
52 #define ST7565_PAGE_ORDER       0,1,2,3
53 #endif
54
55 /*===========================================================================*/
56 /* Driver local functions.                                                   */
57 /*===========================================================================*/
58
59 typedef struct{
60     bool_t buffer2;
61     uint8_t data_pos;
62     uint8_t data[16];
63     uint8_t ram[GDISP_SCREEN_HEIGHT * GDISP_SCREEN_WIDTH / 8];
64 }PrivData;
65
66 // Some common routines and macros
67 #define PRIV(g)                         ((PrivData*)g->priv)
68 #define RAM(g)                                                  (PRIV(g)->ram)
69
70 static GFXINLINE void write_cmd(GDisplay* g, uint8_t cmd) {
71     PRIV(g)->data[PRIV(g)->data_pos++] = cmd;
72 }
73
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;
77 }
78
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); }
81
82 // Some common routines and macros
83 #define delay(us)                       gfxSleepMicroseconds(us)
84 #define delay_ms(ms)            gfxSleepMilliseconds(ms)
85
86 #define xyaddr(x, y)            ((x) + ((y)>>3)*GDISP_SCREEN_WIDTH)
87 #define xybit(y)                        (1<<((y)&7))
88
89 /*===========================================================================*/
90 /* Driver exported functions.                                                */
91 /*===========================================================================*/
92
93 /*
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.
98  */
99
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;
105
106     // Initialise the board interface
107     init_board(g);
108
109     // Hardware reset
110     setpin_reset(g, TRUE);
111     gfxSleepMilliseconds(20);
112     setpin_reset(g, FALSE);
113     gfxSleepMilliseconds(20);
114     acquire_bus(g);
115     enter_cmd_mode(g);
116
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);
121
122     write_cmd(g, ST7565_RESISTOR_RATIO | 0x1);
123     write_cmd2(g, ST7565_CONTRAST, GDISP_INITIAL_CONTRAST);
124
125     // turn on internal power supply (VC=1, VR=1, VF=1)
126     write_cmd(g, ST7565_POWER_CONTROL | 0x07);
127
128     write_cmd(g, ST7565_INVERT_DISPLAY);
129     write_cmd(g, ST7565_ALLON_NORMAL);
130     write_cmd(g, ST7565_DISPLAY_ON);
131
132     write_cmd(g, ST7565_START_LINE | 0);
133     write_cmd(g, ST7565_RMW);
134     flush_cmd(g);
135
136     // Finish Init
137     post_init_board(g);
138
139     // Release the bus
140     release_bus(g);
141
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;
149     return TRUE;
150 }
151
152 #if GDISP_HARDWARE_FLUSH
153 LLDSPEC void gdisp_lld_flush(GDisplay *g) {
154     unsigned    p;
155
156     // Don't flush if we don't need it.
157     if (!(g->flags & GDISP_FLG_NEEDFLUSH))
158         return;
159
160     acquire_bus(g);
161     enter_cmd_mode(g);
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);
168         flush_cmd(g);
169         enter_data_mode(g);
170         write_data(g, RAM(g) + (p*GDISP_SCREEN_WIDTH), GDISP_SCREEN_WIDTH);
171         enter_cmd_mode(g);
172     }
173     unsigned line = (PRIV(g)->buffer2 ? 32 : 0);
174     write_cmd(g, ST7565_START_LINE | line);
175     flush_cmd(g);
176     PRIV(g)->buffer2 = !PRIV(g)->buffer2;
177     release_bus(g);
178
179     g->flags &= ~GDISP_FLG_NEEDFLUSH;
180 }
181 #endif
182
183 #if GDISP_HARDWARE_DRAWPIXEL
184 LLDSPEC void gdisp_lld_draw_pixel(GDisplay *g) {
185     coord_t             x, y;
186
187     switch(g->g.Orientation) {
188     default:
189     case GDISP_ROTATE_0:
190         x = g->p.x;
191         y = g->p.y;
192         break;
193     case GDISP_ROTATE_90:
194         x = g->p.y;
195         y = GDISP_SCREEN_HEIGHT-1 - g->p.x;
196         break;
197     case GDISP_ROTATE_180:
198         x = GDISP_SCREEN_WIDTH-1 - g->p.x;
199         y = GDISP_SCREEN_HEIGHT-1 - g->p.y;
200         break;
201     case GDISP_ROTATE_270:
202         x = GDISP_SCREEN_HEIGHT-1 - g->p.y;
203         y = g->p.x;
204         break;
205     }
206     if (gdispColor2Native(g->p.color) != Black)
207         RAM(g)[xyaddr(x, y)] |= xybit(y);
208     else
209         RAM(g)[xyaddr(x, y)] &= ~xybit(y);
210     g->flags |= GDISP_FLG_NEEDFLUSH;
211 }
212 #endif
213
214 #if GDISP_HARDWARE_PIXELREAD
215 LLDSPEC color_t gdisp_lld_get_pixel_color(GDisplay *g) {
216     coord_t             x, y;
217
218     switch(g->g.Orientation) {
219     default:
220     case GDISP_ROTATE_0:
221         x = g->p.x;
222         y = g->p.y;
223         break;
224     case GDISP_ROTATE_90:
225         x = g->p.y;
226         y = GDISP_SCREEN_HEIGHT-1 - g->p.x;
227         break;
228     case GDISP_ROTATE_180:
229         x = GDISP_SCREEN_WIDTH-1 - g->p.x;
230         y = GDISP_SCREEN_HEIGHT-1 - g->p.y;
231         break;
232     case GDISP_ROTATE_270:
233         x = GDISP_SCREEN_HEIGHT-1 - g->p.y;
234         y = g->p.x;
235         break;
236     }
237     return (RAM(g)[xyaddr(x, y)] & xybit(y)) ? White : Black;
238 }
239 #endif
240
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)]);
255             if (bitset) {
256                 *dst |= xybit(dsty);
257             }
258             else {
259                 *dst &= ~xybit(dsty);
260             }
261                         dstx++;
262             srcbit++;
263         }
264     }
265 }
266
267 #if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
268 LLDSPEC void gdisp_lld_control(GDisplay *g) {
269     switch(g->p.x) {
270     case GDISP_CONTROL_POWER:
271         if (g->g.Powermode == (powermode_t)g->p.ptr)
272             return;
273         switch((powermode_t)g->p.ptr) {
274         case powerOff:
275         case powerSleep:
276         case powerDeepSleep:
277             acquire_bus(g);
278             enter_cmd_mode(g);
279             write_cmd(g, ST7565_DISPLAY_OFF);
280             flush_cmd(g);
281             release_bus(g);
282             break;
283         case powerOn:
284             acquire_bus(g);
285             enter_cmd_mode(g);
286             write_cmd(g, ST7565_DISPLAY_ON);
287             flush_cmd(g);
288             release_bus(g);
289             break;
290         default:
291             return;
292         }
293         g->g.Powermode = (powermode_t)g->p.ptr;
294         return;
295
296         case GDISP_CONTROL_ORIENTATION:
297             if (g->g.Orientation == (orientation_t)g->p.ptr)
298                 return;
299             switch((orientation_t)g->p.ptr) {
300             /* Rotation is handled by the drawing routines */
301             case GDISP_ROTATE_0:
302             case GDISP_ROTATE_180:
303                 g->g.Height = GDISP_SCREEN_HEIGHT;
304                 g->g.Width = GDISP_SCREEN_WIDTH;
305                 break;
306             case GDISP_ROTATE_90:
307             case GDISP_ROTATE_270:
308                 g->g.Height = GDISP_SCREEN_WIDTH;
309                 g->g.Width = GDISP_SCREEN_HEIGHT;
310                 break;
311             default:
312                 return;
313             }
314             g->g.Orientation = (orientation_t)g->p.ptr;
315             return;
316
317             case GDISP_CONTROL_CONTRAST:
318                 g->g.Contrast = (unsigned)g->p.ptr & 63;
319                 acquire_bus(g);
320                 enter_cmd_mode(g);
321                 write_cmd2(g, ST7565_CONTRAST, g->g.Contrast);
322                 flush_cmd(g);
323                 release_bus(g);
324                 return;
325     }
326 }
327 #endif // GDISP_NEED_CONTROL
328
329 #endif // GFX_USE_GDISP