summaryrefslogtreecommitdiff
path: root/commandline/i2c-util.c
blob: be33d4beabc97be3d858e0a660eb170d19843694 (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
/*
 * Copyright (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH
 *       and (c) 2016 by Daniel Friesel
 * License: GNU GPL v2
 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <usb.h>		/* this is libusb, see http://libusb.sourceforge.net/ */

#include "i2c-util.h"
#include "../global_config.h"

#define USBDEV_SHARED_VENDOR    0x16C0	/* VOTI */
#define USBDEV_SHARED_PRODUCT   0x05DC	/* Obdev's free shared PID */
/* Use obdev's generic shared VID/PID pair and follow the rules outlined
 * in firmware/usbdrv/USBID-License.txt.
 */

unsigned char bit_sda = 6;
unsigned char bit_scl = 7;

unsigned char val_sda = 1;
unsigned char val_scl = 1;

usb_dev_handle *handle = NULL;

static int usbGetStringAscii(usb_dev_handle * dev, int index, int langid, char
		*buf, int buflen)
{
	char buffer[256];
	int rval, i;

	if ((rval =
	     usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR,
			     (USB_DT_STRING << 8) + index, langid, buffer,
			     sizeof(buffer), 1000)) < 0)
		return rval;
	if (buffer[1] != USB_DT_STRING)
		return 0;
	if ((unsigned char)buffer[0] < rval)
		rval = (unsigned char)buffer[0];
	rval /= 2;
	/* lossy conversion to ISO Latin1 */
	for (i = 1; i < rval; i++) {
		if (i > buflen)	/* destination buffer overflow */
			break;
		buf[i - 1] = buffer[2 * i];
		if (buffer[2 * i + 1] != 0)	/* outside of ISO Latin1 range */
			buf[i - 1] = '?';
	}
	buf[i - 1] = 0;
	return i - 1;
}

/* PowerSwitch uses the free shared default VID/PID. If you want to see an
 * example device lookup where an individually reserved PID is used, see our
 * RemoteSensor reference implementation.
 */

#define USB_ERROR_NOTFOUND  1
#define USB_ERROR_ACCESS    2
#define USB_ERROR_IO        3

static int usbOpenDevice(usb_dev_handle ** device, int vendor, char *vendorName,
			 int product, char *productName, int upperversion,
			 int lowerversion)
{
	struct usb_bus *bus;
	struct usb_device *dev;
	usb_dev_handle *handle = NULL;
	int errorCode = USB_ERROR_NOTFOUND;
	int major, minor;
	static int didUsbInit = 0;

	if (!didUsbInit) {
		didUsbInit = 1;
		usb_init();
	}
	usb_find_busses();
	usb_find_devices();
	for (bus = usb_get_busses(); bus; bus = bus->next) {
		for (dev = bus->devices; dev; dev = dev->next) {
			if (dev->descriptor.idVendor == vendor
			    && dev->descriptor.idProduct == product) {
				char string[256];
				int len;
				handle = usb_open(dev);	/* we need to open the device in order to query strings */
				if (!handle) {
					errorCode = USB_ERROR_ACCESS;
					fprintf(stderr,
						"Warning: cannot open USB device: %s\n",
						usb_strerror());
					continue;
				}
				if (vendorName == NULL && productName == NULL) {	/* name does not matter */
					break;
				}
				/* now check whether the names match: */
				len =
				    usbGetStringAscii(handle,
						      dev->descriptor.iManufacturer, 0x0409,
						      string, sizeof(string));
				if (len < 0) {
					errorCode = USB_ERROR_IO;
					fprintf(stderr,
						"Warning: cannot query manufacturer for device: %s\n",
						usb_strerror());
				} else {
					errorCode = USB_ERROR_NOTFOUND;
					/* fprintf(stderr, "seen device from vendor ->%s<-\n", string); */
					if (strcmp(string, vendorName) == 0) {
						len =
						    usbGetStringAscii(handle,
								      dev->descriptor.iProduct,
								      0x0409,
								      string, sizeof(string));
						if (len < 0) {
							errorCode = USB_ERROR_IO;
							fprintf(stderr,
								"Warning: cannot query product for device: %s\n",
								usb_strerror());
						} else {
							errorCode = USB_ERROR_NOTFOUND;
							/* fprintf(stderr, "seen product ->%s<-\n", string); */
							if (strcmp(string, productName) == 0)
								break;
						}
					}
				}
				usb_close(handle);
				handle = NULL;
			}
		}
		if (handle)
			break;
	}
	if (handle != NULL) {
		errorCode = 0;
		*device = handle;

		minor = dev->descriptor.bcdDevice & 0xff;
		major = dev->descriptor.bcdDevice >> 8;

		if (major != USBDEV_VERSION_MAJOR) {
			fprintf(stderr,
					"Error: Firmware and host software version are not compatible\n"
					"Firmware: %2d.%02d\n    Host: %2d.%02d\n"
					"Please upgrade the firmware or downgrade the CLI\n",
					major, minor, USBDEV_VERSION_MAJOR, USBDEV_VERSION_MINOR);
			errorCode = -1;
		}
		else if (minor != USBDEV_VERSION_MINOR) {
			fprintf(stderr,
					"Note: Firmware and host software version may be incompatible\n"
					"Firmware: %2d.%02d\n    Host: %2d.%02d\n"
					"Please upgrade the firmware or downgrade the CLI\n",
					major, minor, USBDEV_VERSION_MAJOR, USBDEV_VERSION_MINOR);
		}
	}
	return errorCode;
}

void i2c_getopt(int argc, char **argv)
{
	int opt;
	while ((opt = getopt(argc, argv, "c:d:")) != EOF) {
		switch (opt) {
		case 'c':
			bit_scl = atoi(optarg);
			break;
		case 'd':
			bit_sda = atoi(optarg);
			break;
		}
	}
}

unsigned char get_status()
{
	unsigned char buffer[8];
	int nBytes = usb_control_msg(handle,
				     USB_TYPE_VENDOR | USB_RECIP_DEVICE |
				     USB_ENDPOINT_IN, USBCMD_GETPORT, 0, 0, (char *)buffer,
				     sizeof(buffer), 5000);

	if (nBytes < 1) {
		fprintf(stderr, "ERR: read status: got %d bytes, expected 1\n",
				nBytes);
		exit(1);
	}
	return buffer[0];
}

static void verify_low(unsigned char bit, char *bitname)
{
	int i;
	for (i = 0; i < 10; i++) {
		if (~get_status() & (1 << bit))
			return;
		usleep(10);
	}
	fprintf(stderr, "%s stuck high for >1ms\n", bitname);
}

static void verify_high(unsigned char bit, char *bitname)
{
	int i;
	for (i = 0; i < 100; i++) {
		if (get_status() & (1 << bit))
			return;
		usleep(10);
	}
	fprintf(stderr, "%s stuck low for >1ms\n", bitname);
}

void verify_sda_low()
{
	verify_low(bit_sda, "SDA");
}

void verify_sda_high()
{
	verify_high(bit_sda, "SDA");
}

void verify_scl_low()
{
	verify_low(bit_scl, "SCL");
}

void verify_scl_high()
{
	verify_high(bit_scl, "SCL");
}

/*
 * Firmware: DDRB  = ~b  (with hardware pull-ups and PORTB = 0)
 * So to pull an output high (1), we turn it on, which sets it as input
 * -> pull-up works. for low (0): turn it off -> output -> pulled low
 */

void set_sda(char value)
{
	// discarded
	unsigned char buffer[8];

	val_sda = value;

	usb_control_msg(handle,
			USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
			USBCMD_SETPORT, 0, (val_sda << bit_sda) | (val_scl << bit_scl),
			(char *)buffer, sizeof(buffer), 5000);
}

void set_scl(char value)
{
	// discarded
	unsigned char buffer[8];

	val_scl = value;

	usb_control_msg(handle,
			USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
			USBCMD_SETPORT, 0, (val_sda << bit_sda) | (val_scl << bit_scl),
			(char *)buffer, sizeof(buffer), 5000);
}

unsigned char i2c_tx_byte(unsigned char byte)
{
	signed char i;
	unsigned char ack = 0;

	for (i = 7; i >= -1; i--) {
		if ((i < 0) || (byte & (1 << i))) {
			set_sda(1);
		}
		else {
			set_sda(0);
		}
		usleep(100);
		set_scl(1);
		usleep(100);
		if (i < 0) {
			if (get_status() & (1 << bit_sda))
				ack = 0;
			else
				ack = 1;
		}
		set_scl(0);
		usleep(100);
	}

	return ack;
}

unsigned char i2c_hw_tx_byte(unsigned char byte)
{
	unsigned char buffer[8];

	int nBytes = usb_control_msg(handle,
			USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
			USBCMD_TX, 0, byte,
			(char *)buffer, sizeof(buffer), 5000);

	if (nBytes < 1) {
		fprintf(stderr, "ERR: tx: got %d bytes, expected 1\n",
				nBytes);
		exit(1);
	}

	return buffer[0];
}

unsigned char i2c_rx_byte(unsigned char send_ack)
{
	signed char i;
	unsigned char ret = 0;

	set_sda(1);
	for (i = 7; i >= -1; i--) {
		if (( i < 0) && send_ack)
			set_sda(0);
		set_scl(1);
		usleep(100);
		if ((i >= 0) && ( get_status() & (1 << bit_sda)))
			ret |= (1 << i);
		if (( i < 0) && send_ack)
			set_sda(1);
		set_scl(0);
		usleep(100);
	}

	return ret;
}

unsigned char i2c_hw_rx_byte(unsigned char send_ack)
{
	unsigned char buffer[8];

	int nBytes = usb_control_msg(handle,
			USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
			USBCMD_RX, 0, send_ack,
			(char *)buffer, sizeof(buffer), 5000);

	if (nBytes < 1) {
		fprintf(stderr, "ERR: tx: got %d bytes, expected 1\n",
				nBytes);
		exit(1);
	}

	return buffer[0];
}

void i2c_start()
{
	set_sda(1);
	set_scl(1);
	usleep(1000);
	set_sda(0);
	usleep(1000);
	set_scl(0);
	verify_sda_low();
	verify_scl_low();
}

void i2c_hw_start()
{
	// discarded
	unsigned char buffer[8];

	usb_control_msg(handle,
			USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
			USBCMD_START, 0, 0,
			(char *)buffer, sizeof(buffer), 5000);
}

void i2c_stop()
{
	set_scl(1);
	usleep(100);
	verify_scl_high();
	set_sda(1);
	usleep(100);
	verify_sda_high();
}

void i2c_hw_stop()
{
	// discarded
	unsigned char buffer[8];

	usb_control_msg(handle,
			USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
			USBCMD_STOP, 0, 0,
			(char *)buffer, sizeof(buffer), 5000);
}

void i2c_init()
{
	usb_init();
	if (usbOpenDevice
	    (&handle, USBDEV_SHARED_VENDOR, "finalrewind.org",
	     USBDEV_SHARED_PRODUCT, "VUSB-I2C", 0, 1) != 0) {
		fprintf(stderr,
			"Could not find USB device \"VUSB-I2C\" with vid=0x%x pid=0x%x"
			" version~%d.%02d\n",
			USBDEV_SHARED_VENDOR, USBDEV_SHARED_PRODUCT,
			USBDEV_VERSION_MAJOR, USBDEV_VERSION_MINOR);
		exit(1);
	}
	i2c_hw_setbits();
}

void i2c_hw_setbits()
{
	// discarded
	unsigned char buffer[8];

	usb_control_msg(handle,
			USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
			USBCMD_SETBITS, 0, ((1 << bit_scl) << 8) | (1 << bit_sda),
			(char *)buffer, sizeof(buffer), 5000);
}

void i2c_deinit()
{
	usb_close(handle);
}