summaryrefslogtreecommitdiffstats
path: root/share/examples/ppi/ppilcd.c
blob: fd2506d66cd5ffc4b171614daf8010a6f5b3f074 (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
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
/*
 * Control LCD module hung off parallel port using the
 * ppi 'geek port' interface.
 *
 * $FreeBSD$
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <err.h>
#include <sysexits.h>

#include <dev/ppbus/ppbconf.h>
#include <dev/ppbus/ppi.h>

#define debug(lev, fmt, args...)	if (debuglevel >= lev) fprintf(stderr, fmt "\n" , ## args);

static void	usage(void);
static char	*progname;

#define	DEFAULT_DEVICE	"/dev/ppi0"

/* Driver functions */
static void	hd44780_prepare(char *devname, char *options);
static void	hd44780_finish(void);
static void	hd44780_command(int cmd);
static void	hd44780_putc(int c);

/* 
 * Commands 
 * Note that unrecognised command escapes are passed through with
 * the command value set to the ASCII value of the escaped character.
 */
#define CMD_RESET	0
#define CMD_BKSP	1
#define CMD_CLR		2
#define CMD_NL		3
#define CMD_CR		4
#define CMD_HOME	5

#define MAX_DRVOPT	10	/* maximum driver-specific options */

struct lcd_driver
{
    char	*l_code;
    char	*l_name;
    char	*l_options[MAX_DRVOPT];
    void	(* l_prepare)(char *name, char *options);
    void	(* l_finish)(void);
    void	(* l_command)(int cmd);
    void	(* l_putc)(int c);
};

static struct lcd_driver lcd_drivertab[] = {
    {
	"hd44780", 
	"Hitachi HD44780 and compatibles", 
	{
	    "Reset options:",
	    "    1     1-line display (default 2)",
	    "    B     Cursor blink enable",
	    "    C     Cursor enable",
	    "    F     Large font select",
	    NULL
	},
	hd44780_prepare, 
	hd44780_finish,
	hd44780_command, 
	hd44780_putc
    },
    {
	NULL, 
	NULL, 
	{
	    NULL
	}, 
	NULL, 
	NULL
    }
};

static void	do_char(struct lcd_driver *driver, char ch);

int	debuglevel = 0;
int	vflag = 0;

int
main(int argc, char *argv[]) 
{
    extern char		*optarg;
    extern int		optind;
    struct lcd_driver	*driver = &lcd_drivertab[0];
    char		*drivertype, *cp;
    char		*devname = DEFAULT_DEVICE;
    char		*drvopts = NULL;
    int			ch, i;

    if ((progname = strrchr(argv[0], '/'))) {
	progname++;
    } else {
	progname = argv[0];
    }

    drivertype = getenv("LCD_TYPE");
    
    while ((ch = getopt(argc, argv, "Dd:f:o:v")) != -1) {
	switch(ch) {
	case 'D':
	    debuglevel++;
	    break;
	case 'd':
	    drivertype = optarg;
	    break;
	case 'f':
	    devname = optarg;
	    break;
	case 'o':
	    drvopts = optarg;
	    break;
	case 'v':
	    vflag = 1;
	    break;
	default:
	    usage();
	}
    }
    argc -= optind;
    argv += optind;
    
    /* If an LCD type was specified, look it up */
    if (drivertype != NULL) {
	driver = NULL;
	for (i = 0; lcd_drivertab[i].l_code != NULL; i++) {
	    if (!strcmp(drivertype, lcd_drivertab[i].l_code)) {
		driver = &lcd_drivertab[i];
		break;
	    }
	}
	if (driver == NULL) {
	    warnx("LCD driver '%s' not known", drivertype);
	    usage();
	}
    }
    debug(1, "Driver selected for %s", driver->l_name);
    driver->l_prepare(devname, drvopts);
    atexit(driver->l_finish);

    if (argc > 0) {
	debug(2, "reading input from %d argument%s", argc, (argc > 1) ? "s" : "");
	for (i = 0; i < argc; i++)
	    for (cp = argv[i]; *cp; cp++)
		do_char(driver, *cp);
    } else {
	debug(2, "reading input from stdin");
	setvbuf(stdin, NULL, _IONBF, 0);
	while ((ch = fgetc(stdin)) != EOF)
	    do_char(driver, (char)ch);
    }
    exit(EX_OK);
}

static void
usage(void) 
{
    int		i, j;
    
    fprintf(stderr, "usage: %s [-v] [-d drivername] [-f device] [-o options] [args...]\n", progname);
    fprintf(stderr, "   -D      Increase debugging\n");
    fprintf(stderr, "   -f      Specify device, default is '%s'\n", DEFAULT_DEVICE);
    fprintf(stderr, "   -d      Specify driver, one of:\n");
    for (i = 0; lcd_drivertab[i].l_code != NULL; i++) {
	fprintf(stderr, "              %-10s (%s)%s\n", 
		lcd_drivertab[i].l_code, lcd_drivertab[i].l_name, (i == 0) ? " *default*" : "");
	if (lcd_drivertab[i].l_options[0] != NULL) {
	    
	    for (j = 0; lcd_drivertab[i].l_options[j] != NULL; j++)
		fprintf(stderr, "                  %s\n", lcd_drivertab[i].l_options[j]);
	}
    }
    fprintf(stderr, "  -o       Specify driver option string\n");
    fprintf(stderr, "  args     Message strings.  Embedded escapes supported:\n");
    fprintf(stderr, "                  \\b	Backspace\n");
    fprintf(stderr, "                  \\f	Clear display, home cursor\n");
    fprintf(stderr, "                  \\n	Newline\n");
    fprintf(stderr, "                  \\r	Carriage return\n");
    fprintf(stderr, "                  \\R	Reset display\n");
    fprintf(stderr, "                  \\v	Home cursor\n");
    fprintf(stderr, "                  \\\\	Literal \\\n");
    fprintf(stderr, "           If args not supplied, strings are read from standard input\n");
    exit(EX_USAGE);
}

static void
do_char(struct lcd_driver *driver, char ch)
{
    static int	esc = 0;
    
    if (esc) {
	switch(ch) {
	case 'b':
	    driver->l_command(CMD_BKSP);
	    break;
	case 'f':
	    driver->l_command(CMD_CLR);
	    break;
	case 'n':
	    driver->l_command(CMD_NL);
	    break;
	case 'r':
	    driver->l_command(CMD_CR);
	    break;
	case 'R':
	    driver->l_command(CMD_RESET);
	    break;
	case 'v':
	    driver->l_command(CMD_HOME);
	    break;
	case '\\':
	    driver->l_putc('\\');
	    break;
	default:
	    driver->l_command(ch);
	    break;
	}
	esc = 0;
    } else {
	if (ch == '\\') {
	    esc = 1;
	} else {
	    if (vflag || isprint(ch))
		driver->l_putc(ch);
	}
    }
}


/******************************************************************************
 * Driver for the Hitachi HD44780.  This is probably *the* most common driver
 * to be found on one- and two-line alphanumeric LCDs.
 *
 * This driver assumes the following connections :
 *
 * Parallel Port	LCD Module
 * --------------------------------
 * Strobe (1)		Enable (6)
 * Data (2-9)		Data (7-14)
 * Select In (17)	RS (4)
 * Auto Feed (14)	R/W (5)
 *
 * In addition, power must be supplied to the module, normally with
 * a circuit similar to this:
 *
 * VCC (+5V) O------o-------o--------O Module pin 2
 *                  |       | +
 *                  /      ---
 *                  \      --- 1uF
 *                  /       | -
 *                  \ <-----o--------O Module pin 3
 *                  /
 *                  \
 *                  |
 * GND       O------o----------------O Module pin 1
 *
 * The ground line should also be connected to the parallel port, on
 * one of the ground pins (eg. pin 25).
 *
 * Note that the pinning on some LCD modules has the odd and even pins
 * arranged as though reversed; check carefully before conecting a module
 * as it is possible to toast the HD44780 if the power is reversed.
 */

static int	hd_fd;
static u_int8_t	hd_cbits;
static int	hd_lines = 2;
static int	hd_blink = 0;
static int 	hd_cursor = 0;
static int	hd_font = 0;

#define HD_COMMAND	SELECTIN
#define HD_DATA		0
#define HD_READ		0
#define HD_WRITE	AUTOFEED

#define HD_BF		0x80		/* internal busy flag */
#define HD_ADDRMASK	0x7f		/* DDRAM address mask */

#define hd_sctrl(v)	{u_int8_t _val; _val = hd_cbits | v; ioctl(hd_fd, PPISCTRL, &_val);}
#define hd_sdata(v)	{u_int8_t _val; _val = v; ioctl(hd_fd, PPISDATA, &_val);}
#define hd_gdata(v)	ioctl(hd_fd, PPIGDATA, &v)

static void
hd44780_output(int type, int data)
{
    debug(3, "%s -> 0x%02x", (type == HD_COMMAND) ? "cmd " : "data", data);
    hd_sctrl(type | HD_WRITE | STROBE);	/* set direction, address */
    hd_sctrl(type | HD_WRITE);		/* raise E */
    hd_sdata((u_int8_t) data);		/* drive data */
    hd_sctrl(type | HD_WRITE | STROBE);	/* lower E */
}

static int
hd44780_input(int type) 
{
    u_int8_t	val;

    hd_sctrl(type | HD_READ | STROBE);	/* set direction, address */ 
    hd_sctrl(type | HD_READ);		/* raise E */
    hd_gdata(val);			/* read data */
    hd_sctrl(type | HD_READ | STROBE);	/* lower E */

    debug(3, "0x%02x -> %s", val, (type == HD_COMMAND) ? "cmd " : "data");
    return(val);
}

static void
hd44780_prepare(char *devname, char *options) 
{
    char	*cp = options;
    
    if ((hd_fd = open(devname, O_RDWR, 0)) == -1)
	err(EX_OSFILE, "can't open '%s'", devname);

    /* parse options */
    while (cp && *cp) {
	switch (*cp++) {
	case '1':
	    hd_lines = 1;
	    break;
	case 'B':
	    hd_blink = 1;
	    break;
	case 'C':
	    hd_cursor = 1;
	    break;
	case 'F':
	    hd_font = 1;
	    break;
	default:
	    errx(EX_USAGE, "hd44780: unknown option code '%c'", *(cp-1));
	}
    }

    /* Put LCD in idle state */
    if (ioctl(hd_fd, PPIGCTRL, &hd_cbits))		/* save other control bits */
	err(EX_IOERR, "ioctl PPIGCTRL failed (not a ppi device?)");
    hd_cbits &= ~(STROBE | SELECTIN | AUTOFEED);	/* set strobe, RS, R/W low */
    debug(2, "static control bits 0x%x", hd_cbits);
    hd_sctrl(STROBE);
    hd_sdata(0);

}

static void
hd44780_finish(void) 
{
    close(hd_fd);
}

static void
hd44780_command(int cmd) 
{
    u_int8_t	val;

    switch (cmd) {
    case CMD_RESET:	/* full manual reset and reconfigure as per datasheet */
	debug(1, "hd44780: reset to %d lines, %s font,%s%s cursor", 
	      hd_lines, hd_font ? "5x10" : "5x7", hd_cursor ? "" : " no", hd_blink ? " blinking" : "");
	val = 0x30;
	if (hd_lines == 2)
	    val |= 0x08;
	if (hd_font)
	    val |= 0x04;
	hd44780_output(HD_COMMAND, val);
	usleep(10000);
	hd44780_output(HD_COMMAND, val);
	usleep(1000);
	hd44780_output(HD_COMMAND, val);
	usleep(1000);
	val = 0x08;				/* display off */
	hd44780_output(HD_COMMAND, val);
	usleep(1000);
	val |= 0x04;				/* display on */
	if (hd_cursor)
	    val |= 0x02;
	if (hd_blink)
	    val |= 0x01;
	hd44780_output(HD_COMMAND, val);
	usleep(1000);
	hd44780_output(HD_COMMAND, 0x06);	/* shift cursor by increment */
	usleep(1000);
	/* FALLTHROUGH */

    case CMD_CLR:
	hd44780_output(HD_COMMAND, 0x01);
	usleep(2000);
	break;

    case CMD_BKSP:
	hd44780_output(HD_DATA, 0x10);		/* shift cursor left one */
	break;
	
    case CMD_NL:
	if (hd_lines == 2)
	    hd44780_output(HD_COMMAND, 0xc0);	/* beginning of second line */
	break;
	
    case CMD_CR:
	/* XXX will not work in 4-line mode, or where readback fails */
	val = hd44780_input(HD_COMMAND) & 0x3f;	/* mask character position, save line pos */
	hd44780_output(HD_COMMAND, 0x80 | val);
	break;
	
    case CMD_HOME:
	hd44780_output(HD_COMMAND, 0x02);
	usleep(2000);
	break;
	
    default:
	if (isprint(cmd)) {
	    warnx("unknown command %c", cmd);
	} else {
	    warnx("unknown command 0x%x", cmd);
	}
    }
    usleep(40);
}

static void
hd44780_putc(int c)
{
    hd44780_output(HD_DATA, c);
    usleep(40);
}

OpenPOWER on IntegriCloud