Rev Author Line No. Line
3331 kaklik 1 /*
2 * i2c_usb.c - test application for the i2c-tiny-usb interface
3 * http://www.harbaum.org/till/i2c_tiny_usb
4 *
5 *
6 */
7  
8 // #define NO_USB
9  
10 /* Includes */
11 #include <hildon/hildon-program.h>
12  
13 #include <gtk/gtkmain.h>
14 #include <gtk/gtklabel.h>
15  
16 #include <glib.h>
17 #include <errno.h>
18  
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <usb.h>
23  
24 /* ds1621 chip address (A0-A2 tied low) */
25 #define DS1621_ADDR 0x48
26  
27 /* pcf8574 chip address (A0-A2 tied low) */
28 #define PCF8574_ADDR 0x20
29  
30 #define LOOPS 100
31  
32 #define USB_CTRL_IN (USB_TYPE_CLASS | USB_ENDPOINT_IN)
33 #define USB_CTRL_OUT (USB_TYPE_CLASS)
34  
35 /* the vendor and product id was donated by ftdi ... many thanks!*/
36 #define I2C_TINY_USB_VID 0x0403
37 #define I2C_TINY_USB_PID 0xc631
38  
39 #define I2C_M_RD 0x01
40  
41 /* commands via USB, must e.g. match command ids firmware */
42 #define CMD_ECHO 0
43 #define CMD_GET_FUNC 1
44 #define CMD_SET_DELAY 2
45 #define CMD_GET_STATUS 3
46 #define CMD_I2C_IO 4
47 #define CMD_I2C_BEGIN 1 // flag to I2C_IO
48 #define CMD_I2C_END 2 // flag to I2C_IO
49  
50 #define STATUS_IDLE 0
51 #define STATUS_ADDRESS_ACK 1
52 #define STATUS_ADDRESS_NAK 2
53  
54 usb_dev_handle *handle = NULL;
55 gboolean pcf8574_present = 0;
56 gboolean ds1621_present = 0;
57  
58 /* global reference to main window */
59 HildonWindow *window;
60 void error(char *msg, char *parm);
61  
62 /* write a set of bytes to the i2c_tiny_usb device */
63 int i2c_tiny_usb_write(int request, int value, int index) {
64 if(usb_control_msg(handle, USB_CTRL_OUT, request,
65 value, index, NULL, 0, 1000) < 0) {
66 error("USB error: %s", usb_strerror());
67 return -1;
68 }
69 return 1;
70 }
71  
72 /* read a set of bytes from the i2c_tiny_usb device */
73 int i2c_tiny_usb_read(unsigned char cmd, void *data, int len) {
74 int nBytes;
75  
76 /* send control request and accept return value */
77 nBytes = usb_control_msg(handle,
78 USB_CTRL_IN,
79 cmd, 0, 0, data, len, 1000);
80  
81 if(nBytes < 0) {
82 error("USB error: %s", usb_strerror());
83 return nBytes;
84 }
85  
86 return 0;
87 }
88  
89 /* get i2c usb interface firmware version */
90 void i2c_tiny_usb_get_func(GtkWidget *vbox) {
91 unsigned long func;
92  
93 if(i2c_tiny_usb_read(CMD_GET_FUNC, &func, sizeof(func)) == 0) {
94 char str[64];
95  
96 sprintf(str, "Functionality = %lx\n", func);
97 gtk_box_pack_start(GTK_BOX(vbox), gtk_label_new(str), FALSE, FALSE, 0);
98 } else
99 gtk_box_pack_start(GTK_BOX(vbox),
100 gtk_label_new("Unable to read functionality"),
101 FALSE, FALSE, 0);
102 }
103  
104 /* set a value in the I2C_USB interface */
105 void i2c_tiny_usb_set(unsigned char cmd, int value) {
106 if(usb_control_msg(handle,
107 USB_TYPE_VENDOR, cmd, value, 0,
108 NULL, 0, 1000) < 0) {
109 error("USB error: %s", usb_strerror());
110 }
111 }
112  
113 /* get the current transaction status from the i2c_tiny_usb interface */
114 int i2c_tiny_usb_get_status(void) {
115 int i;
116 unsigned char status;
117  
118 if((i=i2c_tiny_usb_read(CMD_GET_STATUS, &status, sizeof(status))) < 0) {
119 error("Error reading status", NULL);
120 return i;
121 }
122  
123 return status;
124 }
125  
126 /* write command and read an 8 or 16 bit value from the given chip */
127 int i2c_read_with_cmd(unsigned char addr, char cmd, int length) {
128 unsigned char result[2];
129  
130 if((length < 0) || (length > sizeof(result))) {
131 error("Request exceeds buffer size", NULL);
132 return -1;
133 }
134  
135 /* write one byte register address to chip */
136 if(usb_control_msg(handle, USB_CTRL_OUT,
137 CMD_I2C_IO + CMD_I2C_BEGIN
138 + ((!length)?CMD_I2C_END:0),
139 0, addr, &cmd, 1,
140 1000) < 1) {
141 error("USB error: %s", usb_strerror());
142 return -1;
143 }
144  
145 if(i2c_tiny_usb_get_status() != STATUS_ADDRESS_ACK) {
146 error("Write command status failed!", NULL);
147 return -1;
148 }
149  
150 // just a test? return ok
151 if(!length) return 0;
152  
153 if(usb_control_msg(handle,
154 USB_CTRL_IN,
155 CMD_I2C_IO + CMD_I2C_END,
156 I2C_M_RD, addr, (char*)result, length,
157 1000) < 1) {
158 error("USB error: %s", usb_strerror());
159 return -1;
160 }
161  
162 if(i2c_tiny_usb_get_status() != STATUS_ADDRESS_ACK) {
163 error("read data status failed", NULL);
164 return -1;
165 }
166  
167 // return 16 bit result
168 if(length == 2)
169 return 256*result[0] + result[1];
170  
171 // return 8 bit result
172 return result[0];
173 }
174  
175 /* write a single byte to the i2c client */
176 int i2c_write_byte(unsigned char addr, char data) {
177  
178 /* write one byte register address to chip */
179 if(usb_control_msg(handle, USB_CTRL_OUT,
180 CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END,
181 0, addr, &data, 1,
182 1000) < 1) {
183 error("USB error: %s", usb_strerror());
184 return -1;
185 }
186  
187 if(i2c_tiny_usb_get_status() != STATUS_ADDRESS_ACK) {
188 error("Write command status failed", NULL);
189 return -1;
190 }
191  
192 return 0;
193 }
194  
195 /* write a command byte and a single byte to the i2c client */
196 int i2c_write_cmd_and_byte(unsigned char addr, char cmd, char data) {
197 char msg[2];
198  
199 msg[0] = cmd;
200 msg[1] = data;
201  
202 /* write one byte register address to chip */
203 if(usb_control_msg(handle, USB_CTRL_OUT,
204 CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END,
205 0, addr, msg, 2,
206 1000) < 1) {
207 error("USB error: %s", usb_strerror());
208 return -1;
209 }
210  
211 if(i2c_tiny_usb_get_status() != STATUS_ADDRESS_ACK) {
212 error("Write command status failed", NULL);
213 return -1;
214 }
215  
216 return 0;
217 }
218  
219 /* write a command byte and a 16 bit value to the i2c client */
220 int i2c_write_cmd_and_word(unsigned char addr, char cmd, int data) {
221 char msg[3];
222  
223 msg[0] = cmd;
224 msg[1] = data >> 8;
225 msg[2] = data & 0xff;
226  
227 /* write one byte register address to chip */
228 if(usb_control_msg(handle, USB_CTRL_OUT,
229 CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END,
230 0, addr, msg, 3,
231 1000) < 1) {
232 error("USB error: %s", usb_strerror());
233 return -1;
234 }
235  
236 if(i2c_tiny_usb_get_status() != STATUS_ADDRESS_ACK) {
237 error("Write command status failed", NULL);
238 return -1;
239 }
240  
241 return 0;
242 }
243  
244 /* read ds1621 control register */
245 void ds1621_read_control(void) {
246 int result;
247  
248 do {
249 result = i2c_read_with_cmd(DS1621_ADDR, 0xac, 1);
250 } while(!(result & 0x80));
251 }
252  
253 /************************** GUI related code ************************/
254  
255 static void
256 button_clicked (GtkButton* button, gpointer data) {
257 GtkWidget **checkBits = (GtkWidget**)data;
258 int i, value = 0;
259  
260 for(i=0;i<8;i++) {
261 value <<= 1;
262  
263 if(GTK_WIDGET_STATE(checkBits[i]))
264 value |= 1;
265 }
266  
267 #ifndef NO_USB
268 if(pcf8574_present)
269 i2c_write_byte(PCF8574_ADDR, value);
270 #endif
271 }
272  
273 static gboolean
274 update_temperature(gpointer data) {
275 GtkLabel *label = (GtkLabel*)data;
276 char str[32];
277 int temp, counter, slope;
278  
279 /* just write command 0xee to start conversion */
280 if(i2c_read_with_cmd(DS1621_ADDR, 0xee, 0) < 0)
281 return 0;
282  
283 ds1621_read_control();
284  
285 temp = i2c_read_with_cmd(DS1621_ADDR, 0xaa, 2);
286 if(temp < 0) return 0;
287  
288 /* read counter and slope values */
289 counter = i2c_read_with_cmd(DS1621_ADDR, 0xa8, 1);
290 slope = i2c_read_with_cmd(DS1621_ADDR, 0xa9, 1);
291  
292 /* use counter and slope to adjust temperature (see ds1621 datasheet) */
293 temp = (temp & 0xff00) - 256/4;
294 temp += 256 * (slope - counter) / slope;
295  
296 sprintf(str, "%d.%03d °C", temp>>8, 1000 * (temp & 0xff) / 256);
297 gtk_label_set_text(label, str);
298  
299 return 1;
300 }
301  
302 void error(char *msg, char *parm) {
303 GtkWidget *dialog;
304  
305 dialog = gtk_message_dialog_new(GTK_WINDOW(window),
306 GTK_DIALOG_DESTROY_WITH_PARENT,
307 GTK_MESSAGE_ERROR,
308 GTK_BUTTONS_CLOSE,
309 msg, parm);
310 gtk_dialog_run(GTK_DIALOG(dialog));
311 gtk_widget_destroy(dialog);
312 }
313  
314 int main(int argc, char *argv[]) {
315 /* Create needed variables */
316 HildonProgram *program;
317 int i;
318  
319 /* pcf8574 parallel port interface */
320 GtkWidget *vbox;
321 GtkWidget *ds1621Frame;
322 GtkWidget *tempLabel;
323 GtkWidget *pcf8574Frame;
324 GtkWidget *hbox, *buttonBox;
325 GtkWidget *checkBits[8];
326 GSource *temp_timer_src;
327  
328 struct usb_bus *bus;
329 struct usb_device *dev;
330 int ret;
331  
332 /* Initialize the GTK. */
333 gtk_init(&argc, &argv);
334  
335 /* Create the hildon program and setup the title */
336 program = HILDON_PROGRAM(hildon_program_get_instance());
337 g_set_application_name("I²C-Tiny-USB Demo");
338  
339 /* Create HildonWindow and set it to HildonProgram */
340 window = HILDON_WINDOW(hildon_window_new());
341 hildon_program_add_window(program, window);
342  
343 /************* main view **************/
344  
345 gtk_container_add(GTK_CONTAINER(window),
346 GTK_WIDGET(vbox = gtk_vbox_new(FALSE, 0)));
347  
348 /************* hardware initialization **************/
349 #ifndef NO_USB
350 usb_init();
351  
352 usb_find_busses();
353 usb_find_devices();
354  
355 for(bus = usb_get_busses(); bus; bus = bus->next) {
356 for(dev = bus->devices; dev; dev = dev->next) {
357 if((dev->descriptor.idVendor == I2C_TINY_USB_VID) &&
358 (dev->descriptor.idProduct == I2C_TINY_USB_PID)) {
359 char str[128];
360  
361 sprintf(str, "\nI²C-Tiny-USB device on bus %s device %s",
362 bus->dirname, dev->filename);
363  
364 gtk_box_pack_start(GTK_BOX(vbox), gtk_label_new(str), FALSE, FALSE, 0);
365  
366 /* open device */
367 if(!(handle = usb_open(dev))) {
368 error("Cannot open the device: %s", usb_strerror());
369 exit(EIO);
370 }
371 break;
372 }
373 }
374 }
375  
376 if(!handle) {
377 error("No i2c_tiny_usb device attached", NULL);
378 exit(ENODEV);
379 }
380  
381 /* Get exclusive access to interface 0 */
382 ret = usb_claim_interface(handle, 0);
383 if (ret != 0) {
384 error("USB error: %s", usb_strerror());
385 exit(EPERM);
386 }
387  
388 /* do some testing */
389 i2c_tiny_usb_get_func(vbox);
390  
391 /* try to set i2c clock to 100kHz (10us), will actually result in ~50kHz */
392 /* since the software generated i2c clock isn't too exact. in fact setting */
393 /* it to 10us doesn't do anything at all since this already is the default */
394 i2c_tiny_usb_set(CMD_SET_DELAY, 10);
395 #else
396 gtk_box_pack_start(GTK_BOX(vbox), gtk_label_new("\nNO_USB option set"),
397 FALSE, FALSE, 0);
398 gtk_box_pack_start(GTK_BOX(vbox), gtk_label_new("No functions available\n"),
399 FALSE, FALSE, 0);
400 #endif
401  
402 /************* create ds1621 frame **************/
403  
404 gtk_container_add(GTK_CONTAINER(vbox),
405 GTK_WIDGET(ds1621Frame = gtk_frame_new(" ds1621 ")));
406  
407 gtk_container_add(GTK_CONTAINER(ds1621Frame),
408 GTK_WIDGET(hbox = gtk_hbox_new(FALSE, 0)));
409  
410 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new("Temperature:"),
411 FALSE, FALSE, 32);
412  
413 gtk_box_pack_start(GTK_BOX(hbox), tempLabel = gtk_label_new("--- °C"),
414 FALSE, FALSE, 0);
415  
416 #ifndef NO_USB
417 /* try to access ds1621 at address DS1621_ADDR */
418 if(usb_control_msg(handle, USB_CTRL_IN,
419 CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END,
420 0, DS1621_ADDR, NULL, 0,
421 1000) < 0) {
422 error("USB error: %s", usb_strerror());
423 goto quit;
424 }
425  
426 if(i2c_tiny_usb_get_status() == STATUS_ADDRESS_ACK) {
427 ds1621_present = 1;
428  
429 /* activate one shot mode */
430 if(i2c_write_cmd_and_byte(DS1621_ADDR, 0xac, 0x01) < 0)
431 goto quit;
432  
433 /* wait 10ms */
434 usleep(10000);
435  
436 /* build an update timer for the temperature display */
437 temp_timer_src = g_timeout_source_new(1000);
438 g_source_set_callback(temp_timer_src, update_temperature, tempLabel, NULL);
439 g_source_attach (temp_timer_src, NULL);
440 g_source_unref(temp_timer_src);
441 } else
442 gtk_frame_set_label((GtkFrame*)ds1621Frame, " ds1621 - not found ");
443 #endif
444  
445 /************* create pcf8574 frame **************/
446  
447 /* create a frame for the pcf8574 elements */
448 gtk_container_add(GTK_CONTAINER(vbox),
449 GTK_WIDGET(pcf8574Frame = gtk_frame_new(" pcf8574 ")));
450  
451 gtk_container_add(GTK_CONTAINER(pcf8574Frame),
452 GTK_WIDGET(hbox = gtk_hbox_new(FALSE, 0)));
453  
454 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new("Output bits:"),
455 FALSE, FALSE, 32);
456  
457 /* create a button box for the eight buttons */
458 gtk_box_pack_start(GTK_BOX(hbox), buttonBox = gtk_hbutton_box_new(),
459 FALSE, FALSE, 0);
460  
461 gtk_button_box_set_child_size((GtkButtonBox*)buttonBox, 0, 0);
462  
463 /* add the eight buttons */
464 for(i=0;i<8;i++) {
465 gtk_container_add(GTK_CONTAINER(buttonBox),
466 GTK_WIDGET(checkBits[i] = gtk_check_button_new()));
467  
468 g_signal_connect(G_OBJECT(checkBits[i]), "clicked",
469 G_CALLBACK(button_clicked), checkBits);
470 }
471  
472 #ifndef NO_USB
473 /* try to access pcf8574 at address PCF8574_ADDR */
474 if(usb_control_msg(handle, USB_CTRL_IN,
475 CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END,
476 0, PCF8574_ADDR, NULL, 0,
477 1000) < 0) {
478  
479 error("USB error: %s", usb_strerror());
480 goto quit;
481 }
482  
483 if(i2c_tiny_usb_get_status() == STATUS_ADDRESS_ACK) {
484 pcf8574_present = 1;
485 i2c_write_byte(PCF8574_ADDR, 0x00); /* default value */
486 } else
487 gtk_frame_set_label((GtkFrame*)pcf8574Frame, " pcf8574 - not found ");
488 #endif
489  
490 /*************************** *************************/
491  
492 gtk_box_pack_start(GTK_BOX(vbox),
493 gtk_label_new("\nhttp://www.harbaum.org/till/i2c_tiny_usb\n"),
494 FALSE, FALSE, 0);
495  
496 /* begin the main application */
497 gtk_widget_show_all(GTK_WIDGET(window));
498  
499 /* Connect signal to X in the upper corner */
500 g_signal_connect(G_OBJECT(window), "delete_event",
501 G_CALLBACK(gtk_main_quit), NULL);
502  
503 gtk_main();
504  
505 quit:
506 ret = usb_release_interface(handle, 0);
507 if (ret)
508 error("USB error: %s\n", usb_strerror());
509  
510 usb_close(handle);
511  
512 return 0;
513 }