-
Notifications
You must be signed in to change notification settings - Fork 1
/
hd44780.c
498 lines (406 loc) · 9.43 KB
/
hd44780.c
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#define SCROLL_REDRAW
#undef SCROLL_SHIFT
/*
* Copyright 2000-2001 by Geert Uytterhoeven <geert@linux-m68k.org>
*
* This programs is subject to the terms and conditions of the GNU General
* Public License
*/
#include <stdarg.h>
#ifdef __KERNEL__
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/console.h>
static int lcd_console_messages = 1;
#else /* !__KERNEL__ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define MOD_INC_USE_COUNT do { } while (0)
#define MOD_DEC_USE_COUNT do { } while (0)
typedef unsigned char u8;
static unsigned long loops_per_jiffy = 1;
static unsigned long max_udelay;
static void delay(unsigned long loops)
{
long i;
for (i = loops; i >= 0 ; i--)
__asm__ __volatile__("" : : : "memory");
}
#define HZ 100
#define CLOCKS_PER_JIFFY ((CLOCKS_PER_SEC+HZ-1)/HZ)
#define LPS_PREC 8
static void calibrate_delay(void)
{
unsigned long ticks;
printf("Calibrating delay loop.. ");
fflush(stdout);
loops_per_jiffy = (1<<12);
while ((loops_per_jiffy <<= 1)) {
ticks = clock();
delay(loops_per_jiffy);
ticks = clock() - ticks;
if (ticks >= CLOCKS_PER_JIFFY) {
loops_per_jiffy = (loops_per_jiffy / ticks) * CLOCKS_PER_JIFFY;
printf("ok - %lu.%02lu BogoMips\n\n", loops_per_jiffy/(500000/HZ),
(loops_per_jiffy/(5000/HZ)) % 100);
max_udelay = 0xffffffff/loops_per_jiffy;
return;
}
}
printf("failed\n");
exit(-1);
}
static void udelay(unsigned long usecs)
{
while (usecs > max_udelay) {
delay(0xffffffff/1000000);
usecs -= max_udelay;
}
delay(usecs*loops_per_jiffy/(1000000/HZ));
}
#endif /* !__KERNEL__ */
#include "hd44780.h"
#define LCD_COLS 20
#define LCD_ROWS 4
static int lcd_col = 0, lcd_row = 0;
#ifdef SCROLL_REDRAW
static char lcd_data[LCD_COLS*LCD_ROWS];
#endif /* SCROLL_REDRAW */
static const unsigned int lcd_row_offset[LCD_ROWS] = { 0, 64, 20, 84 };
#define LCD_DELAY_STROBE_US 1
#define LCD_DELAY_WRITE_US 50
#define LCD_DELAY_READ_US 6
#define LCD_DELAY_CLR 1437
static inline void lcd_delay_strobe(void) { udelay(LCD_DELAY_STROBE_US); }
static inline void lcd_delay_write(void) { udelay(LCD_DELAY_WRITE_US); }
static inline void lcd_delay_read(void) { udelay(LCD_DELAY_READ_US); }
static inline void lcd_delay_clr(void) { udelay(LCD_DELAY_CLR); }
/*
* Mid-Level LCD Access
*/
static int lcd_current_width = 8; /* The HD44780 boots up in 8-bit mode */
static const struct lcd_driver *lcd_driver = NULL;
static unsigned int lcd_stat_write = 0, lcd_stat_read = 0;
void lcd_register_driver(const struct lcd_driver *driver)
{
lcd_driver = driver;
}
void lcd_unregister_driver(const struct lcd_driver *driver)
{
lcd_driver = NULL;
}
void __lcd_write(u8 val, int rs)
{
lcd_stat_write++;
if (lcd_driver) {
if (lcd_driver->write)
lcd_driver->write(val, rs);
else {
lcd_driver->set_rs_rw(rs, 0);
if (lcd_current_width == 4) {
/* Write High Nibble */
lcd_driver->set_data(val);
lcd_driver->set_e(1);
lcd_delay_strobe();
lcd_driver->set_e(0);
lcd_delay_strobe();
/* Write Low Nibble */
val <<= 4;
}
lcd_driver->set_data(val);
lcd_driver->set_e(1);
lcd_delay_strobe();
lcd_driver->set_e(0);
/* Pause */
lcd_delay_write();
}
}
}
u8 __lcd_read(int rs)
{
u8 val = 0;
lcd_stat_read++;
if (lcd_driver) {
if (lcd_driver->read)
val = lcd_driver->read(rs);
else {
lcd_driver->set_rs_rw(rs, 1);
/* Read Byte or High Nibble */
lcd_driver->set_e(1);
lcd_delay_strobe();
val = lcd_driver->get_data();
lcd_driver->set_e(0);
if (lcd_current_width == 4) {
val &= 0xf0;
lcd_delay_strobe();
/* Read Low Nibble */
lcd_driver->set_e(1);
lcd_delay_strobe();
val |= lcd_driver->get_data() >> 4;
lcd_driver->set_e(0);
}
/* Pause */
lcd_delay_read();
}
}
return val;
}
/* ------------------------------------------------------------------------- */
/*
* High-Level LCD Access
*/
/*
* LCD Commands
*/
/*
* Clear Display
*/
void lcd_clr(void)
{
lcd_write_cmd(LCD_CMD_CLR);
lcd_delay_clr();
lcd_col = lcd_row = 0;
#ifdef SCROLL_REDRAW
memset(lcd_data, ' ', LCD_COLS*LCD_ROWS);
#endif /* SCROLL_REDRAW */
}
/*
* Return Home
*/
void lcd_home(void)
{
lcd_write_cmd(LCD_CMD_HOME);
lcd_col = lcd_row = 0;
}
/*
* Read Busy Flag and Address
*/
#define LCD_BUSY (128)
#define LCD_ADDR_MASK (127)
int lcd_is_busy(u8 *addr)
{
u8 val = lcd_read_cmd();
if (addr)
*addr = val & LCD_ADDR_MASK;
return val & LCD_BUSY ? 1 : 0;
}
void lcd_backlight(int light)
{
if (lcd_driver)
lcd_driver->set_bl(light);
}
/*
* Device-Specific Initialization for a 20x4 LCD
*/
#ifdef __KERNEL__
static void lcd_console_write(struct console *console, const char *s,
unsigned count)
{
while (count--)
lcd_putc(*s++);
}
static struct console lcd_console = {
name: "hd44780",
write: lcd_console_write,
// flags: CON_PRINTBUFFER | CON_CONSDEV | CON_ENABLED,
flags: CON_CONSDEV | CON_ENABLED,
index: -1
};
#endif /* __KERNEL__ */
void lcd_init(int width)
{
#ifdef __KERNEL__
if (loops_per_jiffy == (1<<12)) {
printk("lcd_init: delay loop not yet calibrated\n");
loops_per_jiffy = 50000000; /* Safe for <= 10000 BogoMIPS */
}
#else /* !__KERNEL__ */
if (loops_per_jiffy == 1)
calibrate_delay();
#endif /* !__KERNEL__ */
MOD_INC_USE_COUNT;
switch (width) {
case 8:
lcd_func(LCD_DATALEN_8, LCD_LINES_2, LCD_FONT_5x8);
lcd_current_width = 8;
break;
case 4:
lcd_func(LCD_DATALEN_4, LCD_LINES_2, LCD_FONT_5x8);
if (lcd_current_width == 8) {
lcd_current_width = 4;
lcd_func(LCD_DATALEN_4, LCD_LINES_2, LCD_FONT_5x8);
}
break;
}
lcd_ctrl(LCD_DISP_ON, LCD_CURSOR_ON, LCD_BLINK_ON);
lcd_mode(LCD_INC, LCD_SHIFT_OFF);
lcd_clr();
#ifdef __KERNEL__
if (lcd_console_messages)
register_console(&lcd_console);
#endif /* __KERNEL__ */
}
void lcd_cleanup(void)
{
#ifdef __KERNEL__
if (lcd_console_messages)
unregister_console(&lcd_console);
#else
printf("Statistics: %d writes, %d reads\n", lcd_stat_write, lcd_stat_read);
#endif /* __KERNEL__ */
/* Return to 8-bit mode */
lcd_func(LCD_DATALEN_8, LCD_LINES_2, LCD_FONT_5x8);
lcd_current_width = 8;
MOD_DEC_USE_COUNT;
}
/* ------------------------------------------------------------------------- */
/*
* LCD Text Support
*
* FIXME: split this off in a separate lcdtext module
*/
static void lcd_write_vec(const char *data, unsigned int n)
{
while (n--)
lcd_write(*data++);
}
#ifdef SCROLL_REDRAW
static void lcd_redraw_region(int sx, int sy, int width, int height)
{
int y;
for (y = sy; y < sy+height; y++) {
lcd_ddram(lcd_row_offset[y]+sx);
lcd_write_vec(&lcd_data[y*LCD_COLS+sx], width);
}
lcd_ddram(lcd_row_offset[lcd_row]+lcd_col);
}
static inline void lcd_redraw(void)
{
lcd_redraw_region(0, 0, LCD_COLS, LCD_ROWS);
}
static void lcd_scroll_up(void)
{
memmove(&lcd_data[0], &lcd_data[LCD_COLS], (LCD_ROWS-1)*LCD_COLS);
memset(&lcd_data[(LCD_ROWS-1)*LCD_COLS], ' ', LCD_COLS);
lcd_redraw();
}
#endif /* SCROLL_REDRAW */
#ifdef SCROLL_SHIFT
static int lcd_current_shift = 0;
static void lcd_scroll_up(void)
{
int dir, exor, i;
if (lcd_current_shift == 0) {
dir = LCD_SHIFT_LEFT;
exor = 0;
lcd_current_shift = 20;
} else {
dir = LCD_SHIFT_RIGHT;
exor = 2;
lcd_current_shift = 0;
}
for (i = 0; i < LCD_COLS; i++)
lcd_shift(LCD_SHIFT_DISP, dir);
lcd_ddram(lcd_row_offset[exor]);
for (i = 0; i < LCD_COLS; i++)
lcd_write(' ');
lcd_ddram(lcd_row_offset[1 ^ exor]);
for (i = 0; i < LCD_COLS; i++)
lcd_write(' ');
lcd_ddram(lcd_row_offset[lcd_row ^ exor]+lcd_col);
}
#endif /* SCROLL_SHIFT */
#ifdef __KERNEL__
static void lcd_blank(unsigned long data)
{
lcd_backlight(0);
}
static struct timer_list timer = {
function: lcd_blank
};
#define LCD_BLANK_TIMEOUT (60*HZ)
static void lcd_kick(void)
{
if (!timer_pending(&timer))
lcd_backlight(1);
mod_timer(&timer, jiffies+LCD_BLANK_TIMEOUT);
}
#endif /* !__KERNEL__ */
#ifdef SCROLL_REDRAW
void lcd_putc(char c)
{
#ifdef __KERNEL__
lcd_kick();
#endif /* !__KERNEL__ */
if (c == '\n') {
lcd_col = 0;
lcd_row++;
} else {
lcd_write(c);
lcd_data[lcd_row*LCD_COLS+lcd_col++] = c;
if (lcd_col == LCD_COLS) {
lcd_col = 0;
lcd_row++;
}
}
if (lcd_row == LCD_ROWS) {
lcd_scroll_up();
lcd_row--;
}
if (lcd_col == 0)
lcd_ddram(lcd_row_offset[lcd_row]);
}
#endif /* SCROLL_REDRAW */
#ifdef SCROLL_SHIFT
void lcd_putc(char c)
{
#ifdef __KERNEL__
lcd_kick();
#endif /* !__KERNEL__ */
if (c == '\n') {
lcd_col = 0;
lcd_row++;
} else {
lcd_write(c);
if (++lcd_col == LCD_COLS) {
lcd_col = 0;
lcd_row++;
}
}
if (lcd_row == LCD_ROWS) {
lcd_scroll_up();
lcd_row -= 2;
}
if (lcd_col == 0)
lcd_ddram(lcd_row_offset[lcd_row ^ (lcd_current_shift == 0 ? 0 : 2)]);
}
#endif /* SCROLL_SHIFT */
void lcd_puts(const char *s)
{
char c;
while ((c = *s++))
lcd_putc(c);
}
void lcd_printf(const char *fmt, ...)
{
static char buf[1024];
va_list args;
va_start(args, fmt);
vsprintf(buf, fmt, args);
va_end(args);
lcd_puts(buf);
}
#ifdef MODULE
int init_module(void)
{
return 0;
}
void cleanup_module(void)
{
del_timer(&timer);
}
#endif /* MODULE */