File: hd44780-usb4all.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 (475 lines) | stat: -rw-r--r-- 13,560 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
/** \file server/drivers/hd44780-usb4all.c
 * USB-4-all connection type of hd44780 driver for Hitachi HD44780 based
 * LCD displays connected to an USB-4-all controller.
 *
 * USB-4-all is PIC based USB controller, see
 * http://www.sprut.de/electronic/pic/projekte/usb4all/usb4all.htm
 *
 * It supports
 * - up to two displays with each up to 2x40 or 4x20 lines/characters.
 * - contrast control (PWM1)
 * - brightness control (PWM2)
 * - 4x4 keypad (RA0 - RA3 - X lines, RA4, RA5, RC6, RC7 - Y lines)
 *
 * Only the MCD version is supported!
 */

/*-
 * Copyright (C) 2012 Thomas Mohaupt <thomas.mohaupt@gmail.com>
 *               2010 Roland Wundrig <http://wundrig.de/>
 *
 * Some code parts are adapted from Roland Wundrig linux demo code
 * (part of usb4all package)
 *
 * This file is released under the GNU General Public License version 2.
 * Refer to the COPYING file distributed with this package.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <usb.h>

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

/* connection type specific functions to be exposed using pointers in init() */
void usb4all_HD44780_senddata(PrivateData *p, unsigned char displayID, unsigned char flags, unsigned char ch);
void usb4all_HD44780_close(PrivateData *p);
void usb4all_HD44780_backlight(PrivateData *p, unsigned char state);
void usb4all_HD44780_set_contrast(PrivateData *p, unsigned char value);
unsigned char usb4all_HD44780_readkeypad(PrivateData *p, unsigned int Ydata);

/**
 * Send a command with data to usb4all controller and reads the answer.
 * Even if some inputs should be read a command to do this has to send before.
 * \param p            pointer to private data structure
 * \param data_out     pointer to data send to controller
 * \param data_in      pointer to buffer where read in data is stored
 * \return             number of bytes read in
 */
int
usb4all_data_io(PrivateData *p, tx_buffer * data_out, tx_buffer * data_in)
{
	int res;

	if (p->usbMode == MODE_BULK) {
		res = usb_bulk_write(p->usbHandle,
				     p->usbEpOut,
				     (char *)data_out->buffer,
				     data_out->use_count,
				     TIMEOUT_WRITE);
	}
	else {
		res = usb_interrupt_write(p->usbHandle,
					  p->usbEpOut,
					  (char *)data_out->buffer,
					  data_out->use_count,
					  TIMEOUT_WRITE);
	}

	if (res < 0) {
		p->hd44780_functions->drv_report(RPT_WARNING,
		   "usb4all_data_io: unable to send, result = %d ...", res);
		return -1;
	}

	/* if a reset command is send the usb4all controller doesn't answer */
	if (data_out->buffer[0] == USB4ALL_RESET) {
		return 0;
	}

	if (res != data_out->use_count) {
		p->hd44780_functions->drv_report(RPT_WARNING,
						 "usb4all_data_io: Want to send %d bytes but currently only %d bytes was send!?",
						 data_out->use_count, res);
		return -1;
	}

	if (p->usbMode == MODE_BULK) {
		res = usb_bulk_read(p->usbHandle,
				    p->usbEpIn,
				    (char *)data_in->buffer,
				    USB4ALL_RX_MAX,
				    TIMEOUT_READ);
	}
	else {
		res = usb_interrupt_read(p->usbHandle,
					 p->usbEpIn,
					 (char *)data_in->buffer,
					 USB4ALL_RX_MAX,
					 TIMEOUT_READ);
	}

	return res;
}

/**
 * Set the pwm value on the pwm channel
 * \param p            pointer to private date structure
 * \param pwm_channel  the pwm channel
 * \param value        the high-part of each pwm cylce
 */
void
usb4all_set_pwm_value(PrivateData *p, unsigned char pwm_channel, unsigned char value)
{
	p->tx_buf.buffer[0] = 0x56 + pwm_channel;
	p->tx_buf.buffer[1] = 0x02;
	p->tx_buf.buffer[2] = value;
	p->tx_buf.buffer[3] = 0x00;
	p->tx_buf.use_count = 4;
	usb4all_data_io(p, &p->tx_buf, &p->rx_buf);
}

/**
 * Init the usb-4-all controller part for PWM
 * The PWM outputs can be used to control bightness and contrast
 * \param p            pointer to private date structure
 * \param pwm_channel  channel to initialize (1 or 2)
 */
void
usb4all_init_pwm(PrivateData *p, unsigned char pwm_channel)
{
	p->hd44780_functions->drv_report(RPT_INFO,
		"init usb4all controller for PWM channel %d ", pwm_channel);

	p->tx_buf.buffer[0] = 0x056 + pwm_channel;
	p->tx_buf.buffer[1] = 0x01;
	p->tx_buf.buffer[2] = 0x05;
	p->tx_buf.use_count = 3;
	usb4all_data_io(p, &p->tx_buf, &p->rx_buf);
}

/**
 * Init the usb-4-all controller part for digital I/O (keypad)
 * \param p          pointer to private date structure
 */
void
usb4all_init_keypad(PrivateData *p)
{
	p->hd44780_functions->drv_report(RPT_INFO, "init usb4all controller for keypad");

	/* Input  lines (X) RA0, RA1, RA2, RA3 */
	p->tx_buf.buffer[0] = 0x50;
	p->tx_buf.buffer[1] = 0x04;
	p->tx_buf.buffer[2] = 0x0F;
	p->tx_buf.buffer[3] = 0x00;
	p->tx_buf.buffer[4] = 0x00;
	p->tx_buf.use_count = 5;
	usb4all_data_io(p, &p->tx_buf, &p->rx_buf);

	/* Output lines (Y) RA4, RA5, RC6, RC7 */
	p->tx_buf.buffer[0] = 0x50;
	p->tx_buf.buffer[1] = 0x05;
	p->tx_buf.buffer[2] = 0x30;
	p->tx_buf.buffer[3] = 0x00;
	p->tx_buf.buffer[4] = 0xC0;
	p->tx_buf.use_count = 5;
	usb4all_data_io(p, &p->tx_buf, &p->rx_buf);
}

/**
 * Init the usb-4-all controller part for LCD displays
 * \param p          pointer to private date structure
 * \param displayID  display to init
 * \param rows       number of lines
 * \param cols       number of characters
 */
void
usb4all_init_lcd(PrivateData *p, unsigned char displayID, unsigned char rows, unsigned char cols)
{
	p->hd44780_functions->drv_report(RPT_INFO,
		      "init usb4all controller for display %d ", displayID);

	p->tx_buf.buffer[0] = 0x054 + displayID;
	p->tx_buf.buffer[1] = 0x01;
	p->tx_buf.buffer[2] = rows;
	p->tx_buf.buffer[3] = cols;
	p->tx_buf.use_count = 4;
	usb4all_data_io(p, &p->tx_buf, &p->rx_buf);
}

/**
 * Init the usb-4-all controller
 * \param p          pointer to private date structure
 */
void
usb4all_init(PrivateData *p)
{
	usb4all_init_lcd(p, 1, p->dispVOffset[0], p->width);
	if (p->numDisplays == 2) {
		usb4all_init_lcd(p, 2, p->dispVOffset[1], p->width);
	}

	usb4all_init_pwm(p, USB4ALL_PWM_CONTRAST);
	if (p->have_backlight) {
		usb4all_init_pwm(p, USB4ALL_PWM_BRIGHTNESS);
	}

	if (p->have_keypad) {
		usb4all_init_keypad(p);
	}
}

/**
 * Identifies the usb transfer mode and endpoint addresses.
 * Values are stored in the corresponding private data
 * fields (usbMode, usbEpIn and usbEpOut).
 * \param p      Pointer to PrivateData structure
 * \param iface  Pointer to USB interface descriptor
 */
void
usb4all_determine_usb_params(PrivateData *p, struct usb_interface_descriptor *iface)
{
	p->usbMode = -1;

	if ((iface->endpoint[0].bmAttributes & USB_ENDPOINT_TYPE_MASK) == USB_ENDPOINT_TYPE_INTERRUPT &&
	    (iface->endpoint[1].bmAttributes & USB_ENDPOINT_TYPE_MASK) == USB_ENDPOINT_TYPE_INTERRUPT) {
		p->usbMode = MODE_INT;
	}
	if ((iface->endpoint[0].bmAttributes & USB_ENDPOINT_TYPE_MASK) == USB_ENDPOINT_TYPE_BULK &&
	    (iface->endpoint[1].bmAttributes & USB_ENDPOINT_TYPE_MASK) == USB_ENDPOINT_TYPE_BULK) {
		p->usbMode = MODE_BULK;
	}

	if (p->usbMode == -1) {
		p->hd44780_functions->drv_report(RPT_ERR,
				  "Unsupported USB_ENDPOINT_TYPE = %d / %d",
		   iface->endpoint[0].bmAttributes & USB_ENDPOINT_TYPE_MASK,
		  iface->endpoint[1].bmAttributes & USB_ENDPOINT_TYPE_MASK);
		return;
	}

	if (iface->endpoint[0].bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
		p->usbEpIn = iface->endpoint[0].bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK;
		p->usbEpOut = iface->endpoint[1].bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK;
	}
	else {
		p->usbEpIn = iface->endpoint[1].bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK;
		p->usbEpOut = iface->endpoint[0].bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK;
	}
}

/**
 * Pseudo (empty) uPause function.
 * Override the uPause function as a usb4all device takes care of timing itself.
 * \param p      Pointer to PrivateData structure (unused)
 * \param usecs  Number of micro-seconds to sleep (ignored)
 */
void
usb4all_HD44780_uPause(PrivateData *p, int usecs)
{
}


/**
 * Initialize the driver.
 * \param drvthis  Pointer to driver structure.
 * \retval 0       Success.
 * \retval -1      Error.
 */
int
hd_init_usb4all(Driver *drvthis)
{
	PrivateData *p = (PrivateData *) drvthis->private_data;

	struct usb_bus *bus;

	p->hd44780_functions->senddata = usb4all_HD44780_senddata;
	p->hd44780_functions->close = usb4all_HD44780_close;

	p->hd44780_functions->set_contrast = usb4all_HD44780_set_contrast;
	p->hd44780_functions->backlight = usb4all_HD44780_backlight;
	p->hd44780_functions->readkeypad = usb4all_HD44780_readkeypad;

	/* try to find USB device */
	usb_init();
	usb_find_busses();
	usb_find_devices();

	p->usbHandle = NULL;
	for (bus = usb_get_busses(); bus != NULL; bus = bus->next) {
		struct usb_device *dev;

		for (dev = bus->devices; dev != NULL; dev = dev->next) {

			/* Check if this device is a usb4all device */
			if ((dev->descriptor.idVendor == USB4ALL_VENDORID) &&
			 (dev->descriptor.idProduct == USB4ALL_PRODUCTID)) {

				/* USB-4-all device found; try to open it */
				p->usbHandle = usb_open(dev);
				if (p->usbHandle == NULL) {
					report(RPT_WARNING, "hd_init_usb4all: unable to open device");
					/*
					 * Do not exit here because other
					 * (possibly working) devices may be
					 * connected.
					 */
				}
				else {
					report(RPT_INFO, "hd_init_usb4all: USB-4-all device found");
					usb4all_determine_usb_params(p, &dev->config[0].interface[0].altsetting[0]);
				}
			}
		}
	}

	/* No device found at all */
	if (p->usbHandle == NULL) {
		report(RPT_ERR, "hd_init_usb4all: no (matching) USB-4-all device found");
		return -1;
	}

	if (p->usbMode == -1) {
		report(RPT_ERR, "hd_init_usb4all: unknown usb mode");
		return -1;
	}

	/* allocate and initialize buffer */
	if ((p->tx_buf.buffer = malloc(USB4ALL_TX_MAX)) == NULL) {
		report(RPT_ERR, "hd_init_usb4all: could not allocate send buffer");
		usb4all_HD44780_close(p);
		return -1;
	}

	if ((p->rx_buf.buffer = malloc(USB4ALL_RX_MAX)) == NULL) {
		report(RPT_ERR, "hd_init_usb4all: could not allocate receive buffer");
		usb4all_HD44780_close(p);
		return -1;
	}

	common_init(p, IF_4BIT);

	/* replace uPause with empty one after initialization */
	p->hd44780_functions->uPause = usb4all_HD44780_uPause;

	/* initialize usb-4-all controller */
	usb4all_init(p);

	return 0;
}

/**
 * Send a single byte of data to the LCD.
 * \param p          pointer to private date structure
 * \param displayID  display to send data to (0 = all displays)
 * \param flags      data or instruction command (RS_DATA | RS_INSTR)
 * \param ch         character to display or instruction value
 */
void
usb4all_HD44780_senddata(PrivateData *p, unsigned char displayID, unsigned char flags, unsigned char ch)
{
	if (displayID == 0) {
		usb4all_HD44780_senddata(p, 1, flags, ch);
		if (p->numDisplays == 2) {
			usb4all_HD44780_senddata(p, 2, flags, ch);
		}
		return;
	}

	p->tx_buf.buffer[0] = 0x54 + displayID;
	p->tx_buf.buffer[1] = (flags == RS_DATA) ? 0x02 : 0x03;
	p->tx_buf.buffer[2] = ch;
	p->tx_buf.use_count = 3;
	usb4all_data_io(p, &p->tx_buf, &p->rx_buf);
}

/**
 * Turn display backlight on or off.
 * Backlight is turned on or off by toggeling between the brightness
 * and offbrightness values.
 * \param p      Pointer to driver's private data structure.
 * \param state  New backlight status.
 */
void
usb4all_HD44780_backlight(PrivateData *p, unsigned char state)
{
	/* get backlight brightness */
	int promille = (state == BACKLIGHT_ON) ? p->brightness : p->offbrightness;

	p->hd44780_functions->drv_debug(RPT_DEBUG,
	    "usb4all_HD44780_backlight: Setting backlight to %d", promille);

	/* and set it (converted from [0,1000] -> [255,0]) */
	usb4all_set_pwm_value(p, USB4ALL_PWM_BRIGHTNESS, ((1000 - promille) * 255) / 1000);
}

/**
 * Change display contrast.
 * \param p      Pointer to driver's private data structure.
 * \param value  New contrast value (one byte).
 */
void
usb4all_HD44780_set_contrast(PrivateData *p, unsigned char value)
{
	p->hd44780_functions->drv_debug(RPT_DEBUG,
	     "usb4all_HD44780_set_contrast: Setting contrast to %d", value);
	usb4all_set_pwm_value(p, USB4ALL_PWM_CONTRAST, value);
}

/**
 * Read the keypad
 * \param p      Pointer to driver's private data structure.
 * \param Ydata  the up to 4 bits that should be put on the Y side of the matrix
 * \return       the up to 4 bits that are read out on the X side of the matrix
 */
unsigned char
usb4all_HD44780_readkeypad(PrivateData *p, unsigned int Ydata)
{
	/* adjust and invert bits */
	Ydata = ((Ydata << 4) ^ 0xF0);

	/* Output lines (Y) are RA4, RA5, RC6, RC7 */
	p->tx_buf.buffer[0] = 0x50;
	p->tx_buf.buffer[1] = 0x02;
	p->tx_buf.buffer[2] = Ydata & 0x30;
	p->tx_buf.buffer[3] = 0x00;
	p->tx_buf.buffer[4] = Ydata & 0xC0;
	p->tx_buf.use_count = 5;

	usb4all_data_io(p, &p->tx_buf, &p->rx_buf);

	/* Read RA3 RA2 RA1 RA0 */
	p->tx_buf.buffer[0] = 0x50;
	p->tx_buf.buffer[1] = 0x03;
	p->tx_buf.use_count = 2;
	usb4all_data_io(p, &p->tx_buf, &p->rx_buf);

	/* filter and invert the result */
	return ((p->rx_buf.buffer[2] & 0x0F) | 0xF0) ^ 0xFF;
}

/**
 * Close the driver (do necessary clean-up).
 * \param p  Pointer to driver's private data structure.
 */
void
usb4all_HD44780_close(PrivateData *p)
{
	if (p->usbHandle != NULL) {
		if (p->have_backlight) {
			usb4all_HD44780_backlight(p, BACKLIGHT_OFF);
		}

		usb_close(p->usbHandle);
		p->usbHandle = NULL;
	}

	if (p->tx_buf.buffer != NULL) {
		free(p->tx_buf.buffer);
		p->tx_buf.buffer = NULL;
	}

	if (p->rx_buf.buffer != NULL) {
		free(p->rx_buf.buffer);
		p->rx_buf.buffer = NULL;
	}

}

/* EOF */