File: hd44780-rpi.c

package info (click to toggle)
lcdproc 0.5.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,064 kB
  • sloc: ansic: 59,645; sh: 1,740; perl: 681; makefile: 417
file content (487 lines) | stat: -rw-r--r-- 14,556 bytes parent folder | download
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
/** \file server/drivers/hd44780-rpi.c
 * \c rpi connection type of \c hd44780 driver for Hitachi HD44780 based LCD
 * displays connected to the GPIO pins on the Raspberry Pi.
 *
 * The LCD is operated in its 4 bit-mode. R/W (5) on the LCD MUST be hard wired
 * low to prevent 5V logic appearing on the GPIO pins.
 *
 * The default connections are:
 * header(GPIO)	  LCD
 * P1-12 (18)	  D7 (14)
 * P1-16 (23)	  D6 (13)
 * P1-18 (24)	  D5 (12)
 * P1-22 (25)	  D4 (11)
 * P1-24 (8)	  EN (6)
 * GND		  R/W (5)
 * P1-26 (7)	  RS (4)
 * P1-15 (22)	  EN2 (second controller, optional)
 * P1-11 (17)	  BL (backlight optional)
 *
 * Mappings can be set in the config file using the key-words:
 * pin_EN, pin_EN2, pin_RS, pin_D7, pin_D6, pin_D5, pin_D4, pin_BL
 * in the [HD44780] section.
 */

/*-
 * Copyright (c) 2012-2013 Paul Corner <paul_c@users.sourceforge.net>
 *                    2013 warhog <warhog@gmx.de> (Conditional ARM test)
 *                    2013 Serac <Raspberry Pi forum>
 *                               (Backlight & Rev2 board support)
 *
 * The code to access the GPIO on a Raspberry Pi draws on an example program
 * by Dom and Gert van Loo from:
 * http://elinux.org/Rpi_Low-level_peripherals#GPIO_Driving_Example_.28C.29
 *
 * This file is released under the GNU General Public License. Refer to the
 * COPYING file distributed with this package.
 */

/* Default GPIO pin assignment */
#define RPI_DEF_D7  18
#define RPI_DEF_D6  23
#define RPI_DEF_D5  24
#define RPI_DEF_D4  25
#define RPI_DEF_RS   7
#define RPI_DEF_EN   8
#define RPI_DEF_EN2 22
#define RPI_DEF_BL  17

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <errno.h>

#include "hd44780-rpi.h"
#include "hd44780-low.h"
#include "shared/report.h"

void lcdrpi_HD44780_senddata(PrivateData *p, unsigned char displayID, unsigned char flags, unsigned char ch);
void lcdrpi_HD44780_backlight(PrivateData *p, unsigned char state);
void lcdrpi_HD44780_close(PrivateData *p);

/**
 * Pointer to the memory mapped GPIO registers. Note the pointer type is
 * (unsigned int) not (char). This is important when calculating offset
 * adresses!
 *
 * This pointer is outside PrivataData as it needs to be volatile!
 */
static volatile unsigned int *gpio_map = NULL;

static unsigned int gpio_base_address = 0;

/*
 * Two different board revisions are currently in widespread use. The
 * GPIO mappings differ slightly - Not enough to kill a system yet.
 * Future models *may* have extra pins added or reassigned to other
 * functions. Having a revision dependent mapping gives the user fair
 * warning if they mess up.
 */
static const int gpio_pins_R1[GPIO_PINS] = {
	0, 1, -1, -1, 4, -1, -1, 7,
	8, 9, 10, 11, -1, -1, 14, 15,
	-1, 17, 18, -1, -1, 21, 22, 23,
	24, 25, -1, -1, -1, -1, -1, -1
};

static const int gpio_pins_R2[GPIO_PINS] = {
	-1, -1, 2, 3, 4, -1, -1, 7,
	8, 9, 10, 11, -1, -1, 14, 15,
	-1, 17, 18, -1, -1, -1, 22, 23,
	/* 28-31 accessible via P5 */
	24, 25, -1, 27, 28, 29, 30, 31
};


/**
 * Maps a memory region to the address space of the BM2835 GPIO controller.
 * \param drvthis  Pointer to driver structure.
 * \return         0 on success, -1 on error.
 */
static int
setup_io(Driver *drvthis)
{
	int mem_fd = 0;

	if (gpio_map != NULL) {
		report(RPT_ERR, "setup_io: IO already in use");
		return -1;
	}

	if ((mem_fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
		report(RPT_ERR, "setup_io: can not open /dev/mem");
		return -1;
	}

	gpio_map = (unsigned int *) mmap(NULL,
					 GPIO_BLOCK_SIZE,
					 PROT_READ | PROT_WRITE,
					 MAP_SHARED,
					 mem_fd,
					 gpio_base_address);

	if (gpio_map == MAP_FAILED) {
		report(RPT_ERR, "setup_io: mmap failed: %s", strerror(errno));
		close(mem_fd);
		return -1;
	}
	close(mem_fd);

	debug(RPT_INFO, "setup_io: finished");
	return 0;
}


/**
 * Check if LCDd runs on a Raspberry Pi board and which revision (Rev 1 or
 * Rev 2) it is.
 * \param drvthis  Pointer to driver structure.
 * \return         Pointer to a GPIO mapping table or NULL on error.
 */
static int const *
check_board_rev(Driver *drvthis)
{
	FILE *fp;
	char buf[80];
	char hw[8];
	unsigned int rev = 0;

	if ((fp = fopen("/proc/cpuinfo", "r")) == NULL) {
		report(RPT_ERR, "check_board_rev: Can not open /proc/cpuinfo");
		return NULL;
	}

	hw[0] = '\0';	/* initialize hw as empty string */
	while (!feof(fp)) {
		fgets(buf, sizeof(buf), fp);
		sscanf(buf, "Hardware	: %7s", hw);
		hw[7] = '\0';
		sscanf(buf, "Revision	: %x", &rev);
	}
	fclose(fp);

	/* On boards that have been overvolted, the MSB will be set */
	if ((strcmp(hw, "BCM2708") != 0 &&
	     strcmp(hw, "BCM2709") != 0 &&
	     strcmp(hw, "BCM2835") != 0) ||
	    rev & 0xFFFFFF == 0) {
		report(RPT_ERR, "check_board_rev: This board is not recognized as a Raspberry Pi!");
		return NULL;
	}

	/* detect boards based on WiringPi's logic: */
	/* + new style of detection: rev has 23rd bit set */
	if ((rev & (1 << 23)) != 0) {
		unsigned int bType = (rev & (0xFF << 4)) >> 4;

		if (bType <= 3 || bType == 5 || bType == 6) {
			/* older boards: A, B, A+, B+, ALPHA, CM */
			report(RPT_INFO, "check_board_rev: Revision 2 board detected");
			gpio_base_address = BCM2835_PERI_BASE_OLD + GPIO_BASE_OFFSET;
		}
		else {
			/* modern boards: Pi2 B, Pi3 B */
			report(RPT_INFO, "check_board_rev: Raspberry Pi 2 or higher detected");
			gpio_base_address = BCM2835_PERI_BASE_NEW + GPIO_BASE_OFFSET;
		}
		return gpio_pins_R2;
	}
	/* + old style of detection */
	else {
		gpio_base_address = BCM2835_PERI_BASE_OLD + GPIO_BASE_OFFSET;

		rev &= 0xFF;
		if (rev < 0x04) {
			/* Rev 1 boards will be 0x02 or 0x03. */
			report(RPT_INFO, "check_board_rev: Revision 1 board detected");
			return gpio_pins_R1;
		}
		else {
			/* Rev 2 boards are 0x04, and higher */
			report(RPT_INFO, "check_board_rev: Revision 2 board detected");
			return gpio_pins_R2;
		}
	}
}


/**
 * Check user provided GPIO pin configuration.
 * \param drvthis       Pointer to driver structure.
 * \param pin           Number of the GPIO pin to use.
 * \param allowed_pins  Pointer to array of allowed GPIO pins (depends on
 *                      board revision).
 * \param used_pins     Pointer to array of pin usage status.
 * \return  0 on success, -1 on error.
 */
static int
check_pin(Driver *drvthis, int pin, const int *allowed_pins, int *used_pins)
{
	/* Check for errors */
	if (pin >= GPIO_PINS || pin < 0) {
		report(RPT_ERR, "check_pin: GPIO pin %i out of range", pin);
		return -1;
	}
	if (allowed_pins[pin] < 0) {
		report(RPT_ERR, "check_pin: Use of GPIO pin %i not allowed", pin, allowed_pins[pin]);
		return -1;
	}
	if (used_pins[pin] == 1) {
		report(RPT_ERR, "check_pin: GPIO pin %i already used", pin);
		return -1;
	}

	/* Mark used */
	used_pins[pin] = 1;

	return 0;
}


/**
 * Configures a GPIO pin: Disable pull-up/down and set it up as output.
 * \param drvthis  Pointer to driver structure.
 * \param gpio     Number of the GPIO pin to use.
 */
static void
setup_gpio(Driver *drvthis, int gpio)
{
	volatile int i;

#define GPPUD    gpio_map + 37
#define GPPUDCLK gpio_map + 38

	/* Disable pull-up/down */
	*(GPPUD) &= ~3;

	/*
	 * After writing to the GPPUD register, need to wait 150 cycles as per
	 * p101 BCM2835.pdf. The following while loop uses approx five
	 * instructions plus another two to load the counter. Note: the int
	 * must be volatile or gcc will optimize loop out.
	 */
	i = 30;
	while (--i);

	/* Enable Pull-up/down clock register for the pin */
	*(GPPUDCLK + (gpio / 32)) = 1 << (gpio % 32);

	/* Another 150 cycle wait required after a write to GPPUDCLK */
	i = 30;
	while (--i);

	/* Write again to GPPUD and disable clock */
	*(GPPUD) &= ~3;
	*(GPPUDCLK + (gpio / 32)) = 0;

	/*
	 * Now configure the pin as output: This reads the complete GPSELx
	 * register, clears all bits for the selected pin, enables it as
	 * output, and writes back the register.
	 */
	*(gpio_map + (gpio / 10)) =
		(*(gpio_map + (gpio / 10)) & ~(7 << ((gpio % 10) * 3))) | (1 << ((gpio % 10) * 3));
}


/**
 * Send 4-bit data.
 *
 * \param p     Pointer to driver's private data structure.
 * \param ch    The value to send (lower nibble must contain the data).
 */
static void
send_nibble(PrivateData *p, unsigned char ch, unsigned char displayID)
{
	if (gpio_map != NULL) {
		SET_GPIO(p->rpi_gpio->d7, ch & 0x08);
		SET_GPIO(p->rpi_gpio->d6, ch & 0x04);
		SET_GPIO(p->rpi_gpio->d5, ch & 0x02);
		SET_GPIO(p->rpi_gpio->d4, ch & 0x01);
		p->hd44780_functions->uPause(p, 50);

		/* Data is clocked on the falling edge of EN */
		if (displayID == 1 || displayID == 0)
			SET_GPIO(p->rpi_gpio->en, 1);
		if (displayID == 2 || (p->numDisplays > 1 && displayID == 0))
			SET_GPIO(p->rpi_gpio->en2, 1);
		p->hd44780_functions->uPause(p, 50);

		if (displayID == 1 || displayID == 0)
			SET_GPIO(p->rpi_gpio->en, 0);
		if (displayID == 2 || (p->numDisplays > 1 && displayID == 0))
			SET_GPIO(p->rpi_gpio->en2, 0);
		p->hd44780_functions->uPause(p, 50);
	}		
}


/**
 * Free resources used by this connection type.
 * \param p  Pointer to driver's PrivateData structure.
 */
void
lcdrpi_HD44780_close(PrivateData *p)
{
	/* Configure all pins as input */
	INP_GPIO(p->rpi_gpio->en);
	INP_GPIO(p->rpi_gpio->rs);
	INP_GPIO(p->rpi_gpio->d7);
	INP_GPIO(p->rpi_gpio->d6);
	INP_GPIO(p->rpi_gpio->d5);
	INP_GPIO(p->rpi_gpio->d4);
	if (p->have_backlight)
		INP_GPIO(p->backlight_bit);
	if (p->numDisplays > 1)
		INP_GPIO(p->rpi_gpio->en2);

	/* Unmap and free memory */
	if (gpio_map != NULL)
		munmap((void *) gpio_map, GPIO_BLOCK_SIZE);
	if (p->rpi_gpio != NULL)
		free(p->rpi_gpio);
	p->rpi_gpio = NULL;
}

/**
 * Initialize the driver.
 * \param drvthis  Pointer to driver structure.
 * \retval 0       Success.
 * \retval -1      Error.
 */
int
hd_init_rpi(Driver *drvthis)
{
	PrivateData *p = (PrivateData *) drvthis->private_data;
	const int *allowed_gpio_pins = NULL;
	int used_pins[GPIO_PINS] = {};

	if ((allowed_gpio_pins = check_board_rev(drvthis)) == NULL)
		return -1;

	/* Get GPIO configuration */
	p->rpi_gpio = malloc(sizeof(struct rpi_gpio_map));
	if (p->rpi_gpio == NULL) {
		report(RPT_ERR, "hd_init_rpi: unable to allocate memory");
		return -1;
	}

	p->rpi_gpio->en = drvthis->config_get_int(drvthis->name, "pin_EN", 0, RPI_DEF_EN);
	p->rpi_gpio->rs = drvthis->config_get_int(drvthis->name, "pin_RS", 0, RPI_DEF_RS);
	p->rpi_gpio->d7 = drvthis->config_get_int(drvthis->name, "pin_D7", 0, RPI_DEF_D7);
	p->rpi_gpio->d6 = drvthis->config_get_int(drvthis->name, "pin_D6", 0, RPI_DEF_D6);
	p->rpi_gpio->d5 = drvthis->config_get_int(drvthis->name, "pin_D5", 0, RPI_DEF_D5);
	p->rpi_gpio->d4 = drvthis->config_get_int(drvthis->name, "pin_D4", 0, RPI_DEF_D4);

	debug(RPT_INFO, "hd_init_rpi: Pin EN mapped to GPIO%d", p->rpi_gpio->en);
	debug(RPT_INFO, "hd_init_rpi: Pin RS mapped to GPIO%d", p->rpi_gpio->rs);
	debug(RPT_INFO, "hd_init_rpi: Pin D4 mapped to GPIO%d", p->rpi_gpio->d4);
	debug(RPT_INFO, "hd_init_rpi: Pin D5 mapped to GPIO%d", p->rpi_gpio->d5);
	debug(RPT_INFO, "hd_init_rpi: Pin D6 mapped to GPIO%d", p->rpi_gpio->d6);
	debug(RPT_INFO, "hd_init_rpi: Pin D7 mapped to GPIO%d", p->rpi_gpio->d7);

	if (check_pin(drvthis, p->rpi_gpio->en, allowed_gpio_pins, used_pins) ||
	    check_pin(drvthis, p->rpi_gpio->rs, allowed_gpio_pins, used_pins) ||
	    check_pin(drvthis, p->rpi_gpio->d7, allowed_gpio_pins, used_pins) ||
	    check_pin(drvthis, p->rpi_gpio->d6, allowed_gpio_pins, used_pins) ||
	    check_pin(drvthis, p->rpi_gpio->d5, allowed_gpio_pins, used_pins) ||
	    check_pin(drvthis, p->rpi_gpio->d4, allowed_gpio_pins, used_pins)) {
		free(p->rpi_gpio);
		return -1;
	}

	if (p->numDisplays > 1) {	/* For displays with two controllers */
		p->rpi_gpio->en2 = drvthis->config_get_int(drvthis->name, "pin_EN2", 0, RPI_DEF_EN2);
		debug(RPT_INFO, "hd_init_rpi: Pin EN2 mapped to GPIO%d", p->rpi_gpio->en2);
		if (check_pin(drvthis, p->rpi_gpio->en2, allowed_gpio_pins, used_pins)) {
			free(p->rpi_gpio);
			return -1;
		}
	}

	if (p->have_backlight) {	/* Backlight setup is optional */
		p->backlight_bit = drvthis->config_get_int(drvthis->name, "pin_BL", 0, RPI_DEF_BL);
		debug(RPT_INFO, "hd_init_rpi: Backlight mapped to GPIO%d", p->backlight_bit);

		if (check_pin(drvthis, p->backlight_bit, allowed_gpio_pins, used_pins) != 0) {
			report(RPT_WARNING, "hd_init_rpi: Invalid backlight configuration - disabling backlight");
			p->have_backlight = 0;
		}
	}

	/* Now that configuration should be correct, set up the GPIO pins */
	if (setup_io(drvthis) < 0) {
		report(RPT_ERR, "hd_init_rpi: Failed to set up GPIO");
		free(p->rpi_gpio);
		return -1;
	}

	setup_gpio(drvthis, p->rpi_gpio->en);
	setup_gpio(drvthis, p->rpi_gpio->rs);
	setup_gpio(drvthis, p->rpi_gpio->d7);
	setup_gpio(drvthis, p->rpi_gpio->d6);
	setup_gpio(drvthis, p->rpi_gpio->d5);
	setup_gpio(drvthis, p->rpi_gpio->d4);

	p->hd44780_functions->senddata = lcdrpi_HD44780_senddata;
	p->hd44780_functions->close = lcdrpi_HD44780_close;

	if (p->have_backlight) {
		setup_gpio(drvthis, p->backlight_bit);
		p->hd44780_functions->backlight = lcdrpi_HD44780_backlight;
	}

	if (p->numDisplays > 1) {
		setup_gpio(drvthis, p->rpi_gpio->en2);
	}

	/* Setup the lcd in 4 bit mode: Send (FUNCSET | IF_8BIT) three times
	 * followed by (FUNCSET | IF_4BIT) using four nibbles. */
	SET_GPIO(p->rpi_gpio->rs, 0);
	send_nibble(p, (FUNCSET | IF_8BIT) >> 4, 0);
	p->hd44780_functions->uPause(p, 4100);
	send_nibble(p, (FUNCSET | IF_8BIT) >> 4, 0);
	p->hd44780_functions->uPause(p, 150);
	send_nibble(p, (FUNCSET | IF_8BIT) >> 4, 0);
	send_nibble(p, (FUNCSET | IF_4BIT) >> 4, 0);

	common_init(p, IF_4BIT);

	return 0;
}


/**
 * Send data or commands to the display.
 * \param p          Pointer to driver's private data structure.
 * \param displayID  ID of the display (or 0 for all) to send data to.
 * \param flags      Defines whether to end a command or data.
 * \param ch         The value to send.
 */
void
lcdrpi_HD44780_senddata(PrivateData *p, unsigned char displayID, unsigned char flags, unsigned char ch)
{
	/* Safeguard */
	if (gpio_map != NULL) {
		SET_GPIO(p->rpi_gpio->rs, (flags == RS_INSTR) ? 0 : 1);

		send_nibble(p, ch >> 4, displayID);
		send_nibble(p, ch, displayID);
	}
}


/**
 * Turn display backlight on or off.
 * \param p      Pointer to driver's private data structure.
 * \param state  New backlight status.
 */
void
lcdrpi_HD44780_backlight(PrivateData *p, unsigned char state)
{
	if (p->backlight_bit > -1 && p->backlight_bit < 32)
		SET_GPIO(p->backlight_bit, (state == BACKLIGHT_ON) ? 1 : 0);
}