aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gdisp/KS0108/board_KS0108_example.h
blob: ffc6e9c26f22bbec58c7930405396c0dc720ec43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * This file is subject to the terms of the GFX License. If a copy of
 * the license was not distributed with this file, you can obtain one at:
 *
 *              http://ugfx.org/license.html
 */
/* KS0107/0108 Driver
 * Pinout: There are at least two different Pinouts!
 * Version 1:
 * 1 - CS1       High Active
 * 2 - CS2       High Active
 * 3 - GND
 * 4 - 5V
 * 5 - CONTRAST
 * 6 - RS(=DC)   High = Data, Low = Command
 * 7 - R/W       High = Read, Low = Write
 * 8 - E         Data is KS0108_latched at the falling Edge
 * 9..16 - D0..7
 * Version 2:
 * 1 - GND
 * 2 - 5V
 * 3 - Vo (Operating Voltage for LCD)
 * 4 - RS         High = Data, Low = Command
 * 5 - R/W        High = Read, Low = Write
 * 6 - E          Data is KS0108_latched at the falling Edge
 * 7..14 - D0..7
 * 15 - CS1       High Active
 * 16 - CS2       High Active
 * 17 - /RST      Low Active
 * 18 - Vout (Output voltage for LCD)
 * 19 - LEDA
 * 20 - LEDK
 *
 */
#ifndef _GDISP_LLD_BOARD_H
#define _GDISP_LLD_BOARD_H

// My Settings
#define GDISP_SCREEN_HEIGHT				64
#define GDISP_SCREEN_WIDTH				128
#define KS0108_NEED_READ                GFXOFF
#define KS0108_HAS_RESET                GFXOFF
#define KS0108_NEED_BACKLIGHT           GFXOFF
#define KS0108_NEED_PWMBACKLIGHT        GFXOFF
#define KS0108_NOP_DLY                  GFXON  //doesn't work for me without!

#define LINE_D0      PAL_LINE(GPIOB, 0U)
#define LINE_D1      PAL_LINE(GPIOB, 1U)
#define LINE_D2      PAL_LINE(GPIOB, 2U)
#define LINE_D3      PAL_LINE(GPIOB, 3U)
#define LINE_D4      PAL_LINE(GPIOB, 4U)
#define LINE_D5      PAL_LINE(GPIOB, 5U)
#define LINE_D6      PAL_LINE(GPIOB, 6U)
#define LINE_D7      PAL_LINE(GPIOB, 7U)
#define LINE_BUSY    PAL_LINE(GPIOB, 7U)
#define LINE_CS1     PAL_LINE(GPIOB, 8U)
#define LINE_CS2     PAL_LINE(GPIOB, 9U)
#define LINE_E       PAL_LINE(GPIOB, 10U)
#define LINE_DC      PAL_LINE(GPIOB, 11U)
#if KS0108_NEED_READ
	#define LINE_RW      PAL_LINE(GPIOB, 12U)
#endif
#if KS0108_NEED_BACKLIGHT
	#define LINE_BL      PAL_LINE(GPIOB, 13U)
#endif
#if KS0108_HAS_RESET
	#define LINE_RESET   PAL_LINE(GPIOB, 13U)
#endif

typedef struct {
	ioline_t CS1;	// Chip Select1 PIN
	ioline_t CS2;	// Chip Select2 PIN
	ioline_t DC;	// Register selector PIN
	ioline_t E;		// Enable PIN
	ioline_t BUSY;	// Busy PIN (Same as D[7])
	#if KS0108_NEED_READ
		ioline_t RW;	// Read/Write PIN
	#endif
	#if KS0108_HAS_RESET
		ioline_t RST;	// Reset PIN
	#endif
	#if KS0108_NEED_BACKLIGHT
		ioline_t BL;	// Backlight PIN
	#endif
	ioline_t	D[8];
	ioline_t	CS[CHIPS];
	} lcd_pins_t;
	
/*===========================================================================*/
/* Driver local variables.                                                   */
/*===========================================================================*/
static const lcd_pins_t lcdpins = {
	LINE_CS1,
	LINE_CS2,
	LINE_DC,
	LINE_E,
	LINE_BUSY,
	#if KS0108_NEED_READ
		LINE_RW,
	#endif
	#if KS0108_HAS_RESET
		LINE_RST,
	#endif
	#if KS0108_NEED_BACKLIGHT
		LINE_BL,
	#endif
	{
		LINE_D0,
		LINE_D1,
		LINE_D2,
		LINE_D3,
		LINE_D4,
		LINE_D5,
		LINE_D6,
		LINE_D7
	},
	{
		LINE_CS1,
		LINE_CS2
	}
};

static GFXINLINE void init_board(GDisplay* g){
	(void) g;
	g->board = 0;
	int ii;

	for (ii = 0; ii < CHIPS; ii++){
		palSetLineMode(lcdpins.CS[ii], PAL_MODE_OUTPUT_PUSHPULL);
		palClearLine(lcdpins.CS[ii]);
	}
	palSetLineMode(lcdpins.DC,  PAL_MODE_OUTPUT_PUSHPULL);
	palClearLine(lcdpins.DC);
	palSetLineMode(lcdpins.E,   PAL_MODE_OUTPUT_PUSHPULL);
	palSetLine(lcdpins.E);
	#if KS0108_NEED_READ
		palSetLineMode(lcdpins.RW,  PAL_MODE_OUTPUT_PUSHPULL);
		palClearLine(lcdpins.RW);
	#endif
	#if KS0108_HAS_RESET
		palSetLineMode(lcdpins.RST, PAL_MODE_OUTPUT_PUSHPULL);
		palSetLine(lcdpins.RST);
	#endif
	for(ii = 0; ii < 8; ii++) {
		palSetLineMode(lcdpins.D[ii],  PAL_MODE_OUTPUT_PUSHPULL);
		palClearLine(lcdpins.D[ii]);
	}
	#if KS0108_NEED_BACKLIGHT
		#if KS0108_NEED_PWMBACKLIGHT
			palSetLineMode(lcdpins.BL,   PAL_MODE_ALTERNATE(1));
		#else
			palSetLineMode(lcdpins.BL,   PAL_MODE_OUTPUT_PUSHPULL);
		#endif
	#endif
}

static GFXINLINE void post_init_board(GDisplay *g) {
	(void) g;
}

static GFXINLINE void setpin_reset(GDisplay *g, bool_t state) {
	(void) g;
	(void) state;
	
	#if KS0108_HAS_RESET  //Make Hardware Reset
		if (state)
		  palClearLine(lcdpins.RST);
		else
		  palSetLine(lcdpins.RST);
	#endif
}

static GFXINLINE void set_backlight(GDisplay *g, uint8_t percent) {
  (void) g;
  (void) percent;
}

static GFXINLINE void acquire_bus(GDisplay *g) {
	(void) g;
}

static GFXINLINE void release_bus(GDisplay *g) {
	(void) g;
}

void KS0108_delay(uint16_t microsec){
	#if KS0108_NOP_DLY
		uint16_t cn;
		for (cn=0;cn< microsec;cn++){

			asm("nop");asm("nop");asm("nop");asm("nop");
			asm("nop");asm("nop");asm("nop");asm("nop");
			asm("nop");asm("nop");asm("nop");asm("nop");
			asm("nop");asm("nop");asm("nop");asm("nop");

			asm("nop");asm("nop");asm("nop");asm("nop");
			asm("nop");asm("nop");asm("nop");asm("nop");
			asm("nop");asm("nop");asm("nop");asm("nop");
			asm("nop");asm("nop");asm("nop");asm("nop");

			asm("nop");asm("nop");asm("nop");asm("nop");
			asm("nop");asm("nop");asm("nop");asm("nop");
			asm("nop");asm("nop");asm("nop");asm("nop");
			asm("nop");asm("nop");asm("nop");asm("nop");

			asm("nop");asm("nop");asm("nop");asm("nop");
			asm("nop");asm("nop");asm("nop");asm("nop");
			asm("nop");asm("nop");asm("nop");asm("nop");
			asm("nop");asm("nop");asm("nop");asm("nop");

			//asm("nop");asm("nop");asm("nop");asm("nop");
			//asm("nop");asm("nop");asm("nop");asm("nop");
			//1us
		}
	#else
		gfxSleepMicroseconds(microsec);
	#endif
}

static GFXINLINE void KS0108_latch(void){
	palClearLine(lcdpins.E);
	KS0108_delay(1);
	palSetLine(lcdpins.E);
}

static GFXINLINE void KS0108_write(uint8_t value){
	int ii;

	for(ii = 0; ii < 8; ii++){
		if(value & (1 << ii))
			palSetLine(lcdpins.D[ii]);
		else
			palClearLine(lcdpins.D[ii]);
	}
	KS0108_delay(1);
}

static GFXINLINE void KS0108_select(uint8_t chip){
	uint8_t ii;
	KS0108_delay(1);
	for (ii = 0; ii < CHIPS; ii++){
		if (ii == chip)
			palSetLine(lcdpins.CS[ii]);
	}
	KS0108_delay(1);
}

static GFXINLINE void KS0108_unselect(void){
	uint8_t ii;
	
	KS0108_delay(1);
	for (ii = 0; ii < CHIPS; ii++){
		palClearLine(lcdpins.CS[ii]);
	}
}

/*
#if KS0108_NEED_READ  //Hardware Read ##############  WORKS more or less
static GFXINLINE uint8_t read_KS0108(void) {
  uint8_t ii, data=0;

  for(ii = 0; ii < 8; ii++)
    palSetLineMode(lcdpins.D[ii], PAL_MODE_INPUT);  //Set pads to input
  palSetLine(lcdpins.RW);
  KS0108_delay(1);
  palClearLine(lcdpins.E);
  KS0108_delay(1);
  palSetLine(lcdpins.E);
  KS0108_delay(1);
//  palClearLine(lcdpins.RW);
//  KS0108_delay(1);
//  palSetLine(lcdpins.RW);
//  KS0108_delay(1);
  for(ii = 0; ii < 8; ii++){
    if (palReadLine(lcdpins.D[ii]) == PAL_HIGH){ //Read output
      data |= (1<<ii);
    }
  }
//  palClearLine(lcdpins.E);
  palClearLine(lcdpins.RW);                //Write again
  for(ii = 0; ii < 8; ii++) {
    palSetLineMode(lcdpins.D[ii],  PAL_MODE_OUTPUT_PUSHPULL);  //Set pads to output
  }
  return data;
}
#endif
*/
#if KS0108_NEED_READ  //Hardware Read ########### needs more testing but my display is broken
static GFXINLINE uint8_t read_KS0108(void) {
	uint8_t ii, data=0;

	for(ii = 0; ii < 8; ii++)
		palSetLineMode(lcdpins.D[ii], PAL_MODE_INPUT);  //Set pads to input
	palSetLine(lcdpins.RW);
	KS0108_delay(1);

	palClearLine(lcdpins.DC);
	KS0108_delay(1);


	while (palReadLine(lcdpins.BUSY)); //Wait for busy flag
	palSetLine(lcdpins.DC);
	/*
	KS0108_delay(2);
	KS0108_latch();
	KS0108_delay(2);
	//KS0108_latch();
	//KS0108_delay(1);
	*/
	palClearLine(lcdpins.RW);
	KS0108_delay(2);
	palSetLine(lcdpins.RW);
	KS0108_delay(1);
	palClearLine(lcdpins.DC);
	KS0108_delay(1);
	while (palReadLine(lcdpins.BUSY)); //Wait for busy flag
	//KS0108_delay(7);
	palSetLine(lcdpins.DC);

	//KS0108_delay(1);
	//KS0108_latch();
	//KS0108_delay(3);

	KS0108_delay(1);
	for(ii = 0; ii < 8; ii++){
		if (palReadLine(lcdpins.D[ii]) == PAL_HIGH) //Read output
			data |= (1<<ii);
	}
	//  palClearLine(lcdpins.E);
	palSetLine(lcdpins.DC);
	palClearLine(lcdpins.RW);                //Write again
	for(ii = 0; ii < 8; ii++) {
		palSetLineMode(lcdpins.D[ii],  PAL_MODE_OUTPUT_PUSHPULL);  //Set pads to output
	}
	return data;
}
#endif

static GFXINLINE void write_data(GDisplay* g, uint16_t data){
	(void)g;
	uint8_t bit, displayData;
	#if !KS0108_NEED_READ
		uint8_t	*p;
	#endif
	
	palSetLine(lcdpins.DC);
	KS0108_select((uint8_t)(data>>8));
	#if KS0108_NEED_READ
		displayData=read_KS0108();
	#else
		p = RAM(g) + (GDISP_SCREEN_WIDTH*(g->p.y >> 3)) + g->p.x;
		displayData = *p;
	#endif
	bit = 1 << (g->p.y & 7);			//Get Page bit

	if ((uint8_t)data){              //set bit
		KS0108_write(displayData | bit);
		#if !KS0108_NEED_READ
			*p = (displayData | bit);
		#endif
		KS0108_latch();
	} else {
		KS0108_write(displayData & ~bit);
		#if !KS0108_NEED_READ
			*p = (displayData & ~bit);
		#endif
		KS0108_latch();
	}
	KS0108_delay(2);          //one is too short!
	KS0108_unselect();        //Important
}

static GFXINLINE void write_cmd(GDisplay* g, uint16_t cmd){
	(void)g;
	palClearLine(lcdpins.DC);
	KS0108_select((uint8_t)(cmd>>8));
	KS0108_write((uint8_t)cmd);
	KS0108_latch();
	KS0108_unselect();         //Important
}

static GFXINLINE void setreadmode(GDisplay *g) {
	(void) g;
}

static GFXINLINE void setwritemode(GDisplay *g) {
	(void) g;
}

static GFXINLINE uint16_t read_data(GDisplay *g) {
	(void) g;
	return 0;
}
#endif /* _GDISP_LLD_BOARD_H */