No changes between revisions
/Designs/Tools/i2c_Tiny_USB/SW/testapp/PC/Makefile |
---|
0,0 → 1,18 |
# |
# Makefile |
# |
APP = i2c_usb |
all: $(APP) |
clean: |
rm -f $(APP) |
$(APP): $(APP).c |
$(CC) -Wall -o $@ $(APP).c -lusb |
install: |
install $(APP) $(DESTDIR)/usr/bin |
install i2c_tiny_usb.rules $(DESTDIR)/etc/udev/rules.d |
install i2c_tiny_usb.desktop $(DESTDIR)/usr/share/applications/hildon |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/PC/Makefile.cygwin |
---|
0,0 → 1,15 |
# |
# Makefile.cygwin |
# |
APP = i2c_usb |
all: $(APP).exe |
clean: |
rm -f $(APP).exe |
$(APP).exe: $(APP).c |
$(CC) -Wall -DWIN -o $@ $(APP).c -lusb |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/PC/Makefile.macos |
---|
0,0 → 1,14 |
# |
# Makefile |
# |
APP = i2c_usb |
all: $(APP) |
clean: |
rm -f $(APP) |
$(APP): $(APP).c |
$(CC) -Wall -I/sw/include -o $@ $(APP).c -L/sw/lib -lusb |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/PC/Makefile.mingw |
---|
0,0 → 1,15 |
# |
# Makefile.xmingw |
# |
APP = i2c_usb |
all: $(APP).exe |
clean: |
rm -f $(APP).exe |
$(APP).exe: $(APP).c |
$(CC) -Wall -mno-cygwin -DWIN -o $@ $(APP).c -lusb |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/PC/Makefile.xmingw |
---|
0,0 → 1,19 |
# |
# Makefile.xmingw |
# |
XMINGW_ROOT = /usr/local/cross-tools/bin |
CC = $(XMINGW_ROOT)/i386-mingw32msvc-gcc |
APP = i2c_usb |
all: $(APP).exe |
clean: |
rm -f $(APP).exe |
$(APP).exe: $(APP).c |
$(CC) -Wall -DWIN -o $@ $(APP).c -lusb |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/PC/i2c_usb |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Added: svn:executable |
+* |
\ No newline at end of property |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/PC/i2c_usb.c |
---|
0,0 → 1,438 |
/* |
* i2c_usb.c - test application for the i2c-tiby-usb interface |
* http://www.harbaum.org/till/i2c_tiny_usb |
* |
* $Id: i2c_usb.c,v 1.5 2007/01/05 19:30:43 harbaum Exp $ |
*/ |
#include <stdio.h> |
#include <stdlib.h> |
#include <string.h> |
#include <usb.h> |
/* ds1621 chip address (A0-A2 tied low) */ |
#define DS1621_ADDR 0x48 |
/* pcf8574 chip address (A0-A2 tied low) */ |
#define PCF8574_ADDR 0x20 |
#define LOOPS 100 |
#define USB_CTRL_IN (USB_TYPE_CLASS | USB_ENDPOINT_IN) |
#define USB_CTRL_OUT (USB_TYPE_CLASS) |
/* the vendor and product id was donated by ftdi ... many thanks!*/ |
#define I2C_TINY_USB_VID 0x0403 |
#define I2C_TINY_USB_PID 0xc631 |
#ifdef WIN |
#include <windows.h> |
#include <winbase.h> |
#define usleep(t) Sleep((t) / 1000) |
#endif |
#define I2C_M_RD 0x01 |
/* commands via USB, must e.g. match command ids firmware */ |
#define CMD_ECHO 0 |
#define CMD_GET_FUNC 1 |
#define CMD_SET_DELAY 2 |
#define CMD_GET_STATUS 3 |
#define CMD_I2C_IO 4 |
#define CMD_I2C_BEGIN 1 // flag to I2C_IO |
#define CMD_I2C_END 2 // flag to I2C_IO |
#define STATUS_IDLE 0 |
#define STATUS_ADDRESS_ACK 1 |
#define STATUS_ADDRESS_NAK 2 |
usb_dev_handle *handle = NULL; |
/* write a set of bytes to the i2c_tiny_usb device */ |
int i2c_tiny_usb_write(int request, int value, int index){ |
if(usb_control_msg(handle, USB_CTRL_OUT, request, |
value, index, NULL, 0, 1000) < 0) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
return -1; |
} |
return 1; |
} |
/* read a set of bytes from the i2c_tiny_usb device */ |
int i2c_tiny_usb_read(unsigned char cmd, void *data, int len) { |
int nBytes; |
/* send control request and accept return value */ |
nBytes = usb_control_msg(handle,USB_CTRL_IN, cmd, 0, 0, data, len, 1000); |
if(nBytes < 0) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
return nBytes; |
} |
return 0; |
} |
/* get i2c usb interface test */ |
void i2c_tiny_usb_test(void) { |
unsigned long func; |
if(i2c_tiny_usb_read(CMD_ECHO, &func, sizeof(func)) == 0) |
printf("Functionality = %lx\n", func); |
} |
/* get i2c usb interface firmware version */ |
void i2c_tiny_usb_get_func(void) { |
unsigned long func; |
if(i2c_tiny_usb_read(CMD_GET_FUNC, &func, sizeof(func)) == 0) |
printf("Functionality = %lx\n", func); |
} |
/* set a value in the I2C_USB interface */ |
void i2c_tiny_usb_set(unsigned char cmd, int value) { |
if(usb_control_msg(handle, |
USB_TYPE_VENDOR, cmd, value, 0, |
NULL, 0, 1000) < 0) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
} |
} |
/* get the current transaction status from the i2c_tiny_usb interface */ |
int i2c_tiny_usb_get_status(void) { |
int i; |
unsigned char status; |
if((i=i2c_tiny_usb_read(CMD_GET_STATUS, &status, sizeof(status))) < 0) { |
fprintf(stderr, "Error reading status\n"); |
return i; |
} |
return status; |
} |
/* write command and read an 8 or 16 bit value from the given chip */ |
int i2c_read_with_cmd(unsigned char addr, char cmd, int length) { |
unsigned char result[2]; |
if((length < 0) || (length > sizeof(result))) { |
fprintf(stderr, "request exceeds %lu bytes\n", sizeof(result)); |
return -1; |
} |
/* write one byte register address to chip */ |
if(usb_control_msg(handle, USB_CTRL_OUT, |
CMD_I2C_IO + CMD_I2C_BEGIN |
+ ((!length)?CMD_I2C_END:0), |
0, addr, &cmd, 1, |
1000) < 1) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
return -1; |
} |
if(i2c_tiny_usb_get_status() != STATUS_ADDRESS_ACK) { |
fprintf(stderr, "write command status failed\n"); |
return -1; |
} |
// just a test? return ok |
if(!length) return 0; |
if(usb_control_msg(handle, |
USB_CTRL_IN, |
CMD_I2C_IO + CMD_I2C_END, |
I2C_M_RD, addr, (char*)result, length, |
1000) < 1) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
return -1; |
} |
if(i2c_tiny_usb_get_status() != STATUS_ADDRESS_ACK) { |
fprintf(stderr, "read data status failed\n"); |
return -1; |
} |
// return 16 bit result |
if(length == 2) |
return 256*result[0] + result[1]; |
// return 8 bit result |
return result[0]; |
} |
/* write a single byte to the i2c client */ |
int i2c_write_byte(unsigned char addr, char data) { |
/* write one byte register address to chip */ |
if(usb_control_msg(handle, USB_CTRL_OUT, |
CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END, |
0, addr, &data, 1, |
1000) < 1) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
return -1; |
} |
if(i2c_tiny_usb_get_status() != STATUS_ADDRESS_ACK) { |
fprintf(stderr, "write command status failed\n"); |
return -1; |
} |
return 0; |
} |
/* write a command byte and a single byte to the i2c client */ |
int i2c_write_cmd_and_byte(unsigned char addr, char cmd, char data) { |
char msg[2]; |
msg[0] = cmd; |
msg[1] = data; |
/* write one byte register address to chip */ |
if(usb_control_msg(handle, USB_CTRL_OUT, |
CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END, |
0, addr, msg, 2, |
1000) < 1) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
return -1; |
} |
if(i2c_tiny_usb_get_status() != STATUS_ADDRESS_ACK) { |
fprintf(stderr, "write command status failed\n"); |
return -1; |
} |
return 0; |
} |
/* write a command byte and a 16 bit value to the i2c client */ |
int i2c_write_cmd_and_word(unsigned char addr, char cmd, int data) { |
char msg[3]; |
msg[0] = cmd; |
msg[1] = data >> 8; |
msg[2] = data & 0xff; |
/* write one byte register address to chip */ |
if(usb_control_msg(handle, USB_CTRL_OUT, |
CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END, |
0, addr, msg, 3, |
1000) < 1) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
return -1; |
} |
if(i2c_tiny_usb_get_status() != STATUS_ADDRESS_ACK) { |
fprintf(stderr, "write command status failed\n"); |
return -1; |
} |
return 0; |
} |
/* read ds1621 control register */ |
void ds1621_read_control(void) { |
int result; |
do { |
result = i2c_read_with_cmd(DS1621_ADDR, 0xac, 1); |
} while(!(result & 0x80)); |
} |
/* main program process */ |
int main(int argc, char *argv[]) { |
struct usb_bus *bus; |
struct usb_device *dev; |
int i; |
#ifndef WIN |
int ret; |
#endif |
printf("-- i2c-tiny-usb test application --\n"); |
printf("-- (c) 2006 by Till Harbaum --\n"); |
printf("-- http://www.harbaum.org/till/i2c_tiny_usb --\n"); |
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 == I2C_TINY_USB_VID) && |
(dev->descriptor.idProduct == I2C_TINY_USB_PID)) { |
printf("Found i2c_tiny_usb device on bus %s device %s.\n", |
bus->dirname, dev->filename); |
/* open device */ |
if(!(handle = usb_open(dev))) |
fprintf(stderr, "Error: Cannot open the device: %s\n", |
usb_strerror()); |
break; |
} |
} |
} |
if(!handle) { |
fprintf(stderr, "Error: Could not find i2c_tiny_usb device\n"); |
#ifdef WIN |
printf("Press return to quit\n"); |
getchar(); |
#endif |
exit(-1); |
} |
#ifndef WIN |
/* Get exclusive access to interface 0. Does not work under windows. */ |
ret = usb_claim_interface(handle, 0); |
if (ret != 0) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
exit(1); |
} |
#endif |
printf("writing to adapter"); |
i2c_tiny_usb_write(0,0,0); |
/* do some testing */ |
printf("Getting adapter functionalities"); |
i2c_tiny_usb_get_func(); |
/* try to set i2c clock to 100kHz (10us), will actually result in ~50kHz */ |
/* since the software generated i2c clock isn't too exact. in fact setting */ |
/* it to 10us doesn't do anything at all since this already is the default */ |
printf("Reseting I2C clock to 100 kHz"); |
i2c_tiny_usb_set(CMD_SET_DELAY, 10); |
/* -------- begin of ds1621 client processing --------- */ |
printf("Probing for DS1621 ... "); |
/* try to access ds1621 at address DS1621_ADDR */ |
if(usb_control_msg(handle, USB_CTRL_IN, |
CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END, |
0, DS1621_ADDR, NULL, 0, |
1000) < 0) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
goto quit; |
} |
if(i2c_tiny_usb_get_status() == STATUS_ADDRESS_ACK) { |
int temp; |
printf("success at address 0x%02x\n", DS1621_ADDR); |
/* activate one shot mode */ |
if(i2c_write_cmd_and_byte(DS1621_ADDR, 0xac, 0x01) < 0) |
goto quit; |
/* wait 10ms */ |
usleep(10000); |
#if 0 |
/* write default limits */ |
/* high threshold: +15 deg celsius */ |
i2c_write_cmd_and_word(DS1621_ADDR, 0xa1, 0x0f00); /* 15 deg celsius */ |
usleep(10000); |
/* low threshold: +10 deg celsius */ |
i2c_write_cmd_and_word(DS1621_ADDR, 0xa2, 0x0a00); |
usleep(10000); |
#endif |
/* display limits */ |
temp = i2c_read_with_cmd(DS1621_ADDR, 0xa1, 2); |
printf("high temperature threshold = %d.%03d\n", |
temp>>8, 1000 * (temp & 0xff) / 256); |
temp = i2c_read_with_cmd(DS1621_ADDR, 0xa2, 2); |
printf("low temperature threshold = %d.%03d\n", |
temp>>8, 1000 * (temp & 0xff) / 256); |
printf("Getting %d temperature readings:\n", LOOPS); |
for(i=0;i<LOOPS;i++) { |
int temp; |
int counter, slope; |
/* just write command 0xee to start conversion */ |
if(i2c_read_with_cmd(DS1621_ADDR, 0xee, 0) < 0) |
goto quit; |
ds1621_read_control(); |
temp = i2c_read_with_cmd(DS1621_ADDR, 0xaa, 2); |
if(temp < 0) |
goto quit; |
/* read counter and slope values */ |
counter = i2c_read_with_cmd(DS1621_ADDR, 0xa8, 1); |
slope = i2c_read_with_cmd(DS1621_ADDR, 0xa9, 1); |
/* use counter and slope to adjust temperature (see ds1621 datasheet) */ |
temp = (temp & 0xff00) - 256/4; |
temp += 256 * (slope - counter) / slope; |
printf("temp = %d.%03d\n", temp>>8, 1000 * (temp & 0xff) / 256); |
} |
} else |
printf("failed\n"); |
/* -------- end of ds1621 client processing --------- */ |
/* -------- begin of pcf8574 client processing --------- */ |
printf("Probing for PCF8574 ... "); |
/* try to access pcf8574 at address PCF8574_ADDR */ |
if(usb_control_msg(handle, USB_CTRL_IN, |
CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END, |
0, PCF8574_ADDR, NULL, 0, |
1000) < 0) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
goto quit; |
} |
if(i2c_tiny_usb_get_status() == STATUS_ADDRESS_ACK) { |
unsigned char bit_mask = 0xfe; |
printf("success at address 0x%02x\n", PCF8574_ADDR); |
printf("Cycling 0 bit %d times.\n", LOOPS); |
/* just rotate a single 0 bit through the outputs */ |
for(i=0;i<LOOPS;i++) { |
if(i2c_write_byte(PCF8574_ADDR, bit_mask) < 0) |
goto quit; |
/* rotate the byte */ |
bit_mask = (bit_mask << 1) | 1; |
if(bit_mask == 0xff) |
bit_mask = 0xfe; |
usleep(100000); |
} |
} else |
printf("failed\n"); |
/* -------- end of pcf8574 client processing --------- */ |
quit: |
#ifndef WIN |
ret = usb_release_interface(handle, 0); |
if (ret) |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
#endif |
usb_close(handle); |
#ifdef WIN |
printf("Press return to quit\n"); |
getchar(); |
#endif |
return 0; |
} |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/PC/i2c_usb.exe |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Added: svn:executable |
+* |
\ No newline at end of property |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/PC/readme.txt |
---|
0,0 → 1,50 |
i2c-tiny-usb test application - http://www.harbaum.org/till/i2c_tiny_usb |
------------------------------------------------------------------------ |
This simple test application is meant to demonstrate libusb |
interfacing to the i2c-tiny-usb interface. |
This is no useful application, if you are only interesting in |
using the i2c-tiny-usb interface in your linux box please |
use the kernel driver. |
Linux |
----- |
This demo application has been developed under and for linux. Just |
make sure you have libusb installed. To use this program just |
compile by typing "make" and run the resulting i2c_usb. |
Be sure that the i2c-tiny-usb kernel driver is not loaded while |
running the test application. Otherwise the test application will |
fail with the follwing error message: |
USB error: could not claim interface 0: Device or resource busy |
This is due to the fact that no two drivers may access the interface |
at the same time. |
Windows |
------- |
This program can be compiled for windows. This has been tested |
under Linux using xmingw and the windows port of libusb |
(see http://libusb-win32.sourceforge.net). To install the |
driver plug the device in and install the driver from |
the win directory. Then run testapp/i2c_usb.exe |
This program may also be compiled under windows using cygwin or |
mingw (which is part of cygwin). In order to use cygwin simply |
copy usb.h win32-linusb to /cygwin/usr/include and libusb.a to |
/cygwin/lib and do a "make -f Makefile.cygwin". Don't forget to |
distribute /cygwin/bin/cygwin1.dll with your file to allow it to |
run in non-cygwin environments as well. No dll is required when using |
mingw. In that case copy usb.h to /cygwin/usr/include/mingw and |
libusb.a to /cygwin/lib/mingw. Finally do a "make -f Makefile.mingw". |
MacOS X |
------- |
The program can be compiled under MacOS as well. The fink version |
of linusb has to be installed and a simple "make -f Makefile.macos" |
will build the native MacOS X version. |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/i2c_usb.c~ |
---|
0,0 → 1,438 |
/* |
* i2c_usb.c - test application for the i2c-tiby-usb interface |
* http://www.harbaum.org/till/i2c_tiny_usb |
* |
* $Id: i2c_usb.c,v 1.5 2007/01/05 19:30:43 harbaum Exp $ |
*/ |
#include <stdio.h> |
#include <stdlib.h> |
#include <string.h> |
#include <usb.h> |
/* ds1621 chip address (A0-A2 tied low) */ |
#define DS1621_ADDR 0x48 |
/* pcf8574 chip address (A0-A2 tied low) */ |
#define PCF8574_ADDR 0x20 |
#define LOOPS 100 |
#define USB_CTRL_IN (USB_TYPE_CLASS | USB_ENDPOINT_IN) |
#define USB_CTRL_OUT (USB_TYPE_CLASS) |
/* the vendor and product id was donated by ftdi ... many thanks!*/ |
#define I2C_TINY_USB_VID 0x0403 |
#define I2C_TINY_USB_PID 0xc631 |
#ifdef WIN |
#include <windows.h> |
#include <winbase.h> |
#define usleep(t) Sleep((t) / 1000) |
#endif |
#define I2C_M_RD 0x01 |
/* commands via USB, must e.g. match command ids firmware */ |
#define CMD_ECHO 0 |
#define CMD_GET_FUNC 1 |
#define CMD_SET_DELAY 2 |
#define CMD_GET_STATUS 3 |
#define CMD_I2C_IO 4 |
#define CMD_I2C_BEGIN 1 // flag to I2C_IO |
#define CMD_I2C_END 2 // flag to I2C_IO |
#define STATUS_IDLE 0 |
#define STATUS_ADDRESS_ACK 1 |
#define STATUS_ADDRESS_NAK 2 |
usb_dev_handle *handle = NULL; |
/* write a set of bytes to the i2c_tiny_usb device */ |
int i2c_tiny_usb_write(int request, int value, int index){ |
if(usb_control_msg(handle, USB_CTRL_OUT, request, |
value, index, NULL, 0, 1000) < 0) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
return -1; |
} |
return 1; |
} |
/* read a set of bytes from the i2c_tiny_usb device */ |
int i2c_tiny_usb_read(unsigned char cmd, void *data, int len) { |
int nBytes; |
/* send control request and accept return value */ |
nBytes = usb_control_msg(handle,USB_CTRL_IN, cmd, 0, 0, &data, len, 1000); |
if(nBytes < 0) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
return nBytes; |
} |
return 0; |
} |
/* get i2c usb interface test */ |
void i2c_tiny_usb_test(void) { |
unsigned long func; |
if(i2c_tiny_usb_read(CMD_ECHO, &func, sizeof(func)) == 0) |
printf("Functionality = %lx\n", func); |
} |
/* get i2c usb interface firmware version */ |
void i2c_tiny_usb_get_func(void) { |
unsigned long func; |
if(i2c_tiny_usb_read(CMD_GET_FUNC, &func, sizeof(func)) == 0) |
printf("Functionality = %lx\n", func); |
} |
/* set a value in the I2C_USB interface */ |
void i2c_tiny_usb_set(unsigned char cmd, int value) { |
if(usb_control_msg(handle, |
USB_TYPE_VENDOR, cmd, value, 0, |
NULL, 0, 1000) < 0) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
} |
} |
/* get the current transaction status from the i2c_tiny_usb interface */ |
int i2c_tiny_usb_get_status(void) { |
int i; |
unsigned char status; |
if((i=i2c_tiny_usb_read(CMD_GET_STATUS, &status, sizeof(status))) < 0) { |
fprintf(stderr, "Error reading status\n"); |
return i; |
} |
return status; |
} |
/* write command and read an 8 or 16 bit value from the given chip */ |
int i2c_read_with_cmd(unsigned char addr, char cmd, int length) { |
unsigned char result[2]; |
if((length < 0) || (length > sizeof(result))) { |
fprintf(stderr, "request exceeds %lu bytes\n", sizeof(result)); |
return -1; |
} |
/* write one byte register address to chip */ |
if(usb_control_msg(handle, USB_CTRL_OUT, |
CMD_I2C_IO + CMD_I2C_BEGIN |
+ ((!length)?CMD_I2C_END:0), |
0, addr, &cmd, 1, |
1000) < 1) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
return -1; |
} |
if(i2c_tiny_usb_get_status() != STATUS_ADDRESS_ACK) { |
fprintf(stderr, "write command status failed\n"); |
return -1; |
} |
// just a test? return ok |
if(!length) return 0; |
if(usb_control_msg(handle, |
USB_CTRL_IN, |
CMD_I2C_IO + CMD_I2C_END, |
I2C_M_RD, addr, (char*)result, length, |
1000) < 1) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
return -1; |
} |
if(i2c_tiny_usb_get_status() != STATUS_ADDRESS_ACK) { |
fprintf(stderr, "read data status failed\n"); |
return -1; |
} |
// return 16 bit result |
if(length == 2) |
return 256*result[0] + result[1]; |
// return 8 bit result |
return result[0]; |
} |
/* write a single byte to the i2c client */ |
int i2c_write_byte(unsigned char addr, char data) { |
/* write one byte register address to chip */ |
if(usb_control_msg(handle, USB_CTRL_OUT, |
CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END, |
0, addr, &data, 1, |
1000) < 1) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
return -1; |
} |
if(i2c_tiny_usb_get_status() != STATUS_ADDRESS_ACK) { |
fprintf(stderr, "write command status failed\n"); |
return -1; |
} |
return 0; |
} |
/* write a command byte and a single byte to the i2c client */ |
int i2c_write_cmd_and_byte(unsigned char addr, char cmd, char data) { |
char msg[2]; |
msg[0] = cmd; |
msg[1] = data; |
/* write one byte register address to chip */ |
if(usb_control_msg(handle, USB_CTRL_OUT, |
CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END, |
0, addr, msg, 2, |
1000) < 1) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
return -1; |
} |
if(i2c_tiny_usb_get_status() != STATUS_ADDRESS_ACK) { |
fprintf(stderr, "write command status failed\n"); |
return -1; |
} |
return 0; |
} |
/* write a command byte and a 16 bit value to the i2c client */ |
int i2c_write_cmd_and_word(unsigned char addr, char cmd, int data) { |
char msg[3]; |
msg[0] = cmd; |
msg[1] = data >> 8; |
msg[2] = data & 0xff; |
/* write one byte register address to chip */ |
if(usb_control_msg(handle, USB_CTRL_OUT, |
CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END, |
0, addr, msg, 3, |
1000) < 1) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
return -1; |
} |
if(i2c_tiny_usb_get_status() != STATUS_ADDRESS_ACK) { |
fprintf(stderr, "write command status failed\n"); |
return -1; |
} |
return 0; |
} |
/* read ds1621 control register */ |
void ds1621_read_control(void) { |
int result; |
do { |
result = i2c_read_with_cmd(DS1621_ADDR, 0xac, 1); |
} while(!(result & 0x80)); |
} |
/* main program process */ |
int main(int argc, char *argv[]) { |
struct usb_bus *bus; |
struct usb_device *dev; |
int i; |
#ifndef WIN |
int ret; |
#endif |
printf("-- i2c-tiny-usb test application --\n"); |
printf("-- (c) 2006 by Till Harbaum --\n"); |
printf("-- http://www.harbaum.org/till/i2c_tiny_usb --\n"); |
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 == I2C_TINY_USB_VID) && |
(dev->descriptor.idProduct == I2C_TINY_USB_PID)) { |
printf("Found i2c_tiny_usb device on bus %s device %s.\n", |
bus->dirname, dev->filename); |
/* open device */ |
if(!(handle = usb_open(dev))) |
fprintf(stderr, "Error: Cannot open the device: %s\n", |
usb_strerror()); |
break; |
} |
} |
} |
if(!handle) { |
fprintf(stderr, "Error: Could not find i2c_tiny_usb device\n"); |
#ifdef WIN |
printf("Press return to quit\n"); |
getchar(); |
#endif |
exit(-1); |
} |
#ifndef WIN |
/* Get exclusive access to interface 0. Does not work under windows. */ |
ret = usb_claim_interface(handle, 0); |
if (ret != 0) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
exit(1); |
} |
#endif |
printf("writing to adapter"); |
i2c_tiny_usb_write(0,0,0); |
/* do some testing */ |
printf("Getting adapter functionalities"); |
i2c_tiny_usb_get_func(); |
/* try to set i2c clock to 100kHz (10us), will actually result in ~50kHz */ |
/* since the software generated i2c clock isn't too exact. in fact setting */ |
/* it to 10us doesn't do anything at all since this already is the default */ |
printf("Reseting I2C clock to 100 kHz"); |
i2c_tiny_usb_set(CMD_SET_DELAY, 10); |
/* -------- begin of ds1621 client processing --------- */ |
printf("Probing for DS1621 ... "); |
/* try to access ds1621 at address DS1621_ADDR */ |
if(usb_control_msg(handle, USB_CTRL_IN, |
CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END, |
0, DS1621_ADDR, NULL, 0, |
1000) < 0) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
goto quit; |
} |
if(i2c_tiny_usb_get_status() == STATUS_ADDRESS_ACK) { |
int temp; |
printf("success at address 0x%02x\n", DS1621_ADDR); |
/* activate one shot mode */ |
if(i2c_write_cmd_and_byte(DS1621_ADDR, 0xac, 0x01) < 0) |
goto quit; |
/* wait 10ms */ |
usleep(10000); |
#if 0 |
/* write default limits */ |
/* high threshold: +15 deg celsius */ |
i2c_write_cmd_and_word(DS1621_ADDR, 0xa1, 0x0f00); /* 15 deg celsius */ |
usleep(10000); |
/* low threshold: +10 deg celsius */ |
i2c_write_cmd_and_word(DS1621_ADDR, 0xa2, 0x0a00); |
usleep(10000); |
#endif |
/* display limits */ |
temp = i2c_read_with_cmd(DS1621_ADDR, 0xa1, 2); |
printf("high temperature threshold = %d.%03d\n", |
temp>>8, 1000 * (temp & 0xff) / 256); |
temp = i2c_read_with_cmd(DS1621_ADDR, 0xa2, 2); |
printf("low temperature threshold = %d.%03d\n", |
temp>>8, 1000 * (temp & 0xff) / 256); |
printf("Getting %d temperature readings:\n", LOOPS); |
for(i=0;i<LOOPS;i++) { |
int temp; |
int counter, slope; |
/* just write command 0xee to start conversion */ |
if(i2c_read_with_cmd(DS1621_ADDR, 0xee, 0) < 0) |
goto quit; |
ds1621_read_control(); |
temp = i2c_read_with_cmd(DS1621_ADDR, 0xaa, 2); |
if(temp < 0) |
goto quit; |
/* read counter and slope values */ |
counter = i2c_read_with_cmd(DS1621_ADDR, 0xa8, 1); |
slope = i2c_read_with_cmd(DS1621_ADDR, 0xa9, 1); |
/* use counter and slope to adjust temperature (see ds1621 datasheet) */ |
temp = (temp & 0xff00) - 256/4; |
temp += 256 * (slope - counter) / slope; |
printf("temp = %d.%03d\n", temp>>8, 1000 * (temp & 0xff) / 256); |
} |
} else |
printf("failed\n"); |
/* -------- end of ds1621 client processing --------- */ |
/* -------- begin of pcf8574 client processing --------- */ |
printf("Probing for PCF8574 ... "); |
/* try to access pcf8574 at address PCF8574_ADDR */ |
if(usb_control_msg(handle, USB_CTRL_IN, |
CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END, |
0, PCF8574_ADDR, NULL, 0, |
1000) < 0) { |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
goto quit; |
} |
if(i2c_tiny_usb_get_status() == STATUS_ADDRESS_ACK) { |
unsigned char bit_mask = 0xfe; |
printf("success at address 0x%02x\n", PCF8574_ADDR); |
printf("Cycling 0 bit %d times.\n", LOOPS); |
/* just rotate a single 0 bit through the outputs */ |
for(i=0;i<LOOPS;i++) { |
if(i2c_write_byte(PCF8574_ADDR, bit_mask) < 0) |
goto quit; |
/* rotate the byte */ |
bit_mask = (bit_mask << 1) | 1; |
if(bit_mask == 0xff) |
bit_mask = 0xfe; |
usleep(100000); |
} |
} else |
printf("failed\n"); |
/* -------- end of pcf8574 client processing --------- */ |
quit: |
#ifndef WIN |
ret = usb_release_interface(handle, 0); |
if (ret) |
fprintf(stderr, "USB error: %s\n", usb_strerror()); |
#endif |
usb_close(handle); |
#ifdef WIN |
printf("Press return to quit\n"); |
getchar(); |
#endif |
return 0; |
} |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/mobile/.cvsignore |
---|
0,0 → 1,0 |
*~ *.o i2c_usb |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/mobile/Makefile |
---|
0,0 → 1,21 |
# |
# Makefile |
# |
APP = i2c_usb |
all: $(APP) |
clean: |
rm -f $(APP) |
$(APP): $(APP).c |
$(CC) -o $@ $(APP).c `pkg-config gtk+-2.0 hildon-1 --cflags --libs` -lusb |
install: |
install $(APP) $(DESTDIR)/usr/bin |
install i2c_tiny_usb.rules $(DESTDIR)/etc/udev/rules.d |
install i2c_tiny_usb.desktop $(DESTDIR)/usr/share/applications/hildon |
install i2c.26.png $(DESTDIR)/usr/share/icons/hicolor/26x26/hildon/i2c_tiny_usb.png |
install i2c.40.png $(DESTDIR)/usr/share/icons/hicolor/40x40/hildon/i2c_tiny_usb.png |
install i2c.64.png $(DESTDIR)/usr/share/icons/hicolor/scalable/hildon/i2c_tiny_usb.png |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/mobile/debian/changelog |
---|
0,0 → 1,10 |
i2c-tiny-usb (0.2) unstable; urgency=low |
* Initial release (Closes: #nnnn) <nnnn is the bug number of your ITP> |
-- Till Harbaum <Till@Harbaum.org> Tue, 25 Dec 2007 21:02:30 +0100 |
* First hildonized version |
-- Till Harbaum <Till@Harbaum.org> Sat, 29 Dec 2007 12:06:43 +0100 |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/mobile/debian/compat |
---|
0,0 → 1,0 |
5 |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/mobile/debian/control |
---|
0,0 → 1,26 |
Source: i2c-tiny-usb |
Section: user/other |
Priority: extra |
Maintainer: Till Harbaum <Till@Harbaum.org> |
Build-Depends: debhelper (>= 5), libgtk2.0-dev, libusb-dev, libhildon1-dev |
Standards-Version: 3.7.2 |
Package: i2c-tiny-usb |
Architecture: any |
Depends: ${shlibs:Depends}, hildon-desktop |
Description: User space application for i2c_tiny_usb |
This simple test application for the i2c-tiny-usb interface bypasses |
the kernel driver being part of the kernel since 2.6.21. The kernel |
driver may therefore have to be unloaded before this i2c_usb is being |
run. This program currently supports the ds1621 temperature sensor |
and the pcf8574 parallel port. See www.harbaum.org/till/i2c_tiny_usb |
for details. |
XB-Maemo-Icon-26: |
iVBORw0KGgoAAAANSUhEUgAAABoAAAAaCAYAAACpSkzOAAAAF3RFWHRTb2Z0d2Fy |
ZQBYUGFpbnQgMi43LjguMT1iBU0AAAD2SURBVEiJvZZLDoQgDIbLZE7nBk5hz9Rb |
4MbrdRamhkcp4qB/YhQK/UptAMfMAACAiMfHZBGRAwBwzAyIyMuyPMGBfd+BiJxb |
19WEeO+z9rZtt2Bfa4D3vnKs9V3RxzLecdiSY6mGBsBKXWlrCRGP1LUmW2kaSSER |
2f+oBIvupNQEjTq0VmkWwygkfZfqpu4faKppK+rpNdAjqdN2k2krEuePV50FmQ6y |
1N3rLI3sdd3zaIay8yiEoA6KMWZ2aWt9mg+xVeVdOgohZH0taYGkqkBlVFcgMk4C |
0+ZWVRdjPB8N3IO15l5ekRZxarf+D8Cb1623LpA/+pCiKqwn6CcAAAAASUVORK5C |
YII= |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/mobile/debian/copyright |
---|
0,0 → 1,13 |
This package was debianized by Till Harbaum <Till@Harbaum.org> on |
Tue, 25 Dec 2007 21:02:30 +0100. |
Copyright: |
<Copyright (C) 2007 Till Harbaum> |
License: |
<GPL> |
The Debian packaging is (C) 2007, Till Harbaum <Till@Harbaum.org> and |
is licensed under the GPL, see `/usr/share/common-licenses/GPL'. |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/mobile/debian/dirs |
---|
0,0 → 1,7 |
usr/bin |
usr/sbin |
etc/udev/rules.d |
usr/share/applications/hildon |
usr/share/icons/hicolor/26x26/hildon |
usr/share/icons/hicolor/40x40/hildon |
usr/share/icons/hicolor/scalable/hildon |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/mobile/debian/docs |
---|
0,0 → 1,2 |
readme.txt |
readme.txt |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/mobile/debian/postinst |
---|
0,0 → 1,8 |
#! /bin/sh |
# The clock might be wrong and we know that we need to update the icon |
# cache so we just force it. |
gtk-update-icon-cache -f /usr/share/icons/hicolor |
exit 0 |
Property changes: |
Added: svn:executable |
+* |
\ No newline at end of property |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/mobile/debian/rules |
---|
0,0 → 1,98 |
#!/usr/bin/make -f |
# -*- makefile -*- |
# Sample debian/rules that uses debhelper. |
# This file was originally written by Joey Hess and Craig Small. |
# As a special exception, when this file is copied by dh-make into a |
# dh-make output file, you may use that output file without restriction. |
# This special exception was added by Craig Small in version 0.37 of dh-make. |
# Uncomment this to turn on verbose mode. |
#export DH_VERBOSE=1 |
CFLAGS = -Wall -g |
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS))) |
CFLAGS += -O0 |
else |
CFLAGS += -O2 |
endif |
configure: configure-stamp |
configure-stamp: |
dh_testdir |
# Add here commands to configure the package. |
touch configure-stamp |
build: build-stamp |
build-stamp: configure-stamp |
dh_testdir |
# Add here commands to compile the package. |
$(MAKE) |
#docbook-to-man debian/i2c-tiny-usb.sgml > i2c-tiny-usb.1 |
touch $@ |
clean: |
dh_testdir |
dh_testroot |
rm -f build-stamp configure-stamp |
# Add here commands to clean up after the build process. |
-$(MAKE) clean |
dh_clean |
install: build |
dh_testdir |
dh_testroot |
dh_clean -k |
dh_installdirs |
# Add here commands to install the package into debian/i2c-tiny-usb. |
$(MAKE) DESTDIR=$(CURDIR)/debian/i2c-tiny-usb install |
# Build architecture-independent files here. |
binary-indep: build install |
# We have nothing to do by default. |
# Build architecture-dependent files here. |
binary-arch: build install |
dh_testdir |
dh_testroot |
dh_installchangelogs |
dh_installdocs |
dh_installexamples |
# dh_install |
# dh_installmenu |
# dh_installdebconf |
# dh_installlogrotate |
# dh_installemacsen |
# dh_installpam |
# dh_installmime |
# dh_python |
# dh_installinit |
# dh_installcron |
# dh_installinfo |
dh_installman |
dh_link |
dh_strip |
dh_compress |
dh_fixperms |
# dh_perl |
# dh_makeshlibs |
dh_installdeb |
dh_shlibdeps |
dh_gencontrol |
dh_md5sums |
dh_builddeb |
binary: binary-indep binary-arch |
.PHONY: build clean binary-indep binary-arch binary install configure |
Property changes: |
Added: svn:executable |
+* |
\ No newline at end of property |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/mobile/i2c.26.png |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/mobile/i2c.40.png |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/mobile/i2c.64.png |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/mobile/i2c_tiny_usb.desktop |
---|
0,0 → 1,7 |
[Desktop Entry] |
Encoding=UTF-8 |
Version=1.0 |
Type=Application |
Name=I²C-Tiny-USB Demo |
Icon=i2c_tiny_usb |
Exec=/usr/bin/i2c_usb |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/mobile/i2c_tiny_usb.rules |
---|
0,0 → 1,2 |
# give user access to i2c-tiny-usb |
BUS=="usb", ACTION=="add", SUBSYSTEM=="usb_device", SYSFS{idVendor}=="0403", SYSFS{idProduct}=="c631", GROUP="users", OWNER="user" |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/mobile/i2c_usb.c |
---|
0,0 → 1,513 |
/* |
* i2c_usb.c - test application for the i2c-tiny-usb interface |
* http://www.harbaum.org/till/i2c_tiny_usb |
* |
* |
*/ |
// #define NO_USB |
/* Includes */ |
#include <hildon/hildon-program.h> |
#include <gtk/gtkmain.h> |
#include <gtk/gtklabel.h> |
#include <glib.h> |
#include <errno.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <string.h> |
#include <usb.h> |
/* ds1621 chip address (A0-A2 tied low) */ |
#define DS1621_ADDR 0x48 |
/* pcf8574 chip address (A0-A2 tied low) */ |
#define PCF8574_ADDR 0x20 |
#define LOOPS 100 |
#define USB_CTRL_IN (USB_TYPE_CLASS | USB_ENDPOINT_IN) |
#define USB_CTRL_OUT (USB_TYPE_CLASS) |
/* the vendor and product id was donated by ftdi ... many thanks!*/ |
#define I2C_TINY_USB_VID 0x0403 |
#define I2C_TINY_USB_PID 0xc631 |
#define I2C_M_RD 0x01 |
/* commands via USB, must e.g. match command ids firmware */ |
#define CMD_ECHO 0 |
#define CMD_GET_FUNC 1 |
#define CMD_SET_DELAY 2 |
#define CMD_GET_STATUS 3 |
#define CMD_I2C_IO 4 |
#define CMD_I2C_BEGIN 1 // flag to I2C_IO |
#define CMD_I2C_END 2 // flag to I2C_IO |
#define STATUS_IDLE 0 |
#define STATUS_ADDRESS_ACK 1 |
#define STATUS_ADDRESS_NAK 2 |
usb_dev_handle *handle = NULL; |
gboolean pcf8574_present = 0; |
gboolean ds1621_present = 0; |
/* global reference to main window */ |
HildonWindow *window; |
void error(char *msg, char *parm); |
/* write a set of bytes to the i2c_tiny_usb device */ |
int i2c_tiny_usb_write(int request, int value, int index) { |
if(usb_control_msg(handle, USB_CTRL_OUT, request, |
value, index, NULL, 0, 1000) < 0) { |
error("USB error: %s", usb_strerror()); |
return -1; |
} |
return 1; |
} |
/* read a set of bytes from the i2c_tiny_usb device */ |
int i2c_tiny_usb_read(unsigned char cmd, void *data, int len) { |
int nBytes; |
/* send control request and accept return value */ |
nBytes = usb_control_msg(handle, |
USB_CTRL_IN, |
cmd, 0, 0, data, len, 1000); |
if(nBytes < 0) { |
error("USB error: %s", usb_strerror()); |
return nBytes; |
} |
return 0; |
} |
/* get i2c usb interface firmware version */ |
void i2c_tiny_usb_get_func(GtkWidget *vbox) { |
unsigned long func; |
if(i2c_tiny_usb_read(CMD_GET_FUNC, &func, sizeof(func)) == 0) { |
char str[64]; |
sprintf(str, "Functionality = %lx\n", func); |
gtk_box_pack_start(GTK_BOX(vbox), gtk_label_new(str), FALSE, FALSE, 0); |
} else |
gtk_box_pack_start(GTK_BOX(vbox), |
gtk_label_new("Unable to read functionality"), |
FALSE, FALSE, 0); |
} |
/* set a value in the I2C_USB interface */ |
void i2c_tiny_usb_set(unsigned char cmd, int value) { |
if(usb_control_msg(handle, |
USB_TYPE_VENDOR, cmd, value, 0, |
NULL, 0, 1000) < 0) { |
error("USB error: %s", usb_strerror()); |
} |
} |
/* get the current transaction status from the i2c_tiny_usb interface */ |
int i2c_tiny_usb_get_status(void) { |
int i; |
unsigned char status; |
if((i=i2c_tiny_usb_read(CMD_GET_STATUS, &status, sizeof(status))) < 0) { |
error("Error reading status", NULL); |
return i; |
} |
return status; |
} |
/* write command and read an 8 or 16 bit value from the given chip */ |
int i2c_read_with_cmd(unsigned char addr, char cmd, int length) { |
unsigned char result[2]; |
if((length < 0) || (length > sizeof(result))) { |
error("Request exceeds buffer size", NULL); |
return -1; |
} |
/* write one byte register address to chip */ |
if(usb_control_msg(handle, USB_CTRL_OUT, |
CMD_I2C_IO + CMD_I2C_BEGIN |
+ ((!length)?CMD_I2C_END:0), |
0, addr, &cmd, 1, |
1000) < 1) { |
error("USB error: %s", usb_strerror()); |
return -1; |
} |
if(i2c_tiny_usb_get_status() != STATUS_ADDRESS_ACK) { |
error("Write command status failed!", NULL); |
return -1; |
} |
// just a test? return ok |
if(!length) return 0; |
if(usb_control_msg(handle, |
USB_CTRL_IN, |
CMD_I2C_IO + CMD_I2C_END, |
I2C_M_RD, addr, (char*)result, length, |
1000) < 1) { |
error("USB error: %s", usb_strerror()); |
return -1; |
} |
if(i2c_tiny_usb_get_status() != STATUS_ADDRESS_ACK) { |
error("read data status failed", NULL); |
return -1; |
} |
// return 16 bit result |
if(length == 2) |
return 256*result[0] + result[1]; |
// return 8 bit result |
return result[0]; |
} |
/* write a single byte to the i2c client */ |
int i2c_write_byte(unsigned char addr, char data) { |
/* write one byte register address to chip */ |
if(usb_control_msg(handle, USB_CTRL_OUT, |
CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END, |
0, addr, &data, 1, |
1000) < 1) { |
error("USB error: %s", usb_strerror()); |
return -1; |
} |
if(i2c_tiny_usb_get_status() != STATUS_ADDRESS_ACK) { |
error("Write command status failed", NULL); |
return -1; |
} |
return 0; |
} |
/* write a command byte and a single byte to the i2c client */ |
int i2c_write_cmd_and_byte(unsigned char addr, char cmd, char data) { |
char msg[2]; |
msg[0] = cmd; |
msg[1] = data; |
/* write one byte register address to chip */ |
if(usb_control_msg(handle, USB_CTRL_OUT, |
CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END, |
0, addr, msg, 2, |
1000) < 1) { |
error("USB error: %s", usb_strerror()); |
return -1; |
} |
if(i2c_tiny_usb_get_status() != STATUS_ADDRESS_ACK) { |
error("Write command status failed", NULL); |
return -1; |
} |
return 0; |
} |
/* write a command byte and a 16 bit value to the i2c client */ |
int i2c_write_cmd_and_word(unsigned char addr, char cmd, int data) { |
char msg[3]; |
msg[0] = cmd; |
msg[1] = data >> 8; |
msg[2] = data & 0xff; |
/* write one byte register address to chip */ |
if(usb_control_msg(handle, USB_CTRL_OUT, |
CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END, |
0, addr, msg, 3, |
1000) < 1) { |
error("USB error: %s", usb_strerror()); |
return -1; |
} |
if(i2c_tiny_usb_get_status() != STATUS_ADDRESS_ACK) { |
error("Write command status failed", NULL); |
return -1; |
} |
return 0; |
} |
/* read ds1621 control register */ |
void ds1621_read_control(void) { |
int result; |
do { |
result = i2c_read_with_cmd(DS1621_ADDR, 0xac, 1); |
} while(!(result & 0x80)); |
} |
/************************** GUI related code ************************/ |
static void |
button_clicked (GtkButton* button, gpointer data) { |
GtkWidget **checkBits = (GtkWidget**)data; |
int i, value = 0; |
for(i=0;i<8;i++) { |
value <<= 1; |
if(GTK_WIDGET_STATE(checkBits[i])) |
value |= 1; |
} |
#ifndef NO_USB |
if(pcf8574_present) |
i2c_write_byte(PCF8574_ADDR, value); |
#endif |
} |
static gboolean |
update_temperature(gpointer data) { |
GtkLabel *label = (GtkLabel*)data; |
char str[32]; |
int temp, counter, slope; |
/* just write command 0xee to start conversion */ |
if(i2c_read_with_cmd(DS1621_ADDR, 0xee, 0) < 0) |
return 0; |
ds1621_read_control(); |
temp = i2c_read_with_cmd(DS1621_ADDR, 0xaa, 2); |
if(temp < 0) return 0; |
/* read counter and slope values */ |
counter = i2c_read_with_cmd(DS1621_ADDR, 0xa8, 1); |
slope = i2c_read_with_cmd(DS1621_ADDR, 0xa9, 1); |
/* use counter and slope to adjust temperature (see ds1621 datasheet) */ |
temp = (temp & 0xff00) - 256/4; |
temp += 256 * (slope - counter) / slope; |
sprintf(str, "%d.%03d °C", temp>>8, 1000 * (temp & 0xff) / 256); |
gtk_label_set_text(label, str); |
return 1; |
} |
void error(char *msg, char *parm) { |
GtkWidget *dialog; |
dialog = gtk_message_dialog_new(GTK_WINDOW(window), |
GTK_DIALOG_DESTROY_WITH_PARENT, |
GTK_MESSAGE_ERROR, |
GTK_BUTTONS_CLOSE, |
msg, parm); |
gtk_dialog_run(GTK_DIALOG(dialog)); |
gtk_widget_destroy(dialog); |
} |
int main(int argc, char *argv[]) { |
/* Create needed variables */ |
HildonProgram *program; |
int i; |
/* pcf8574 parallel port interface */ |
GtkWidget *vbox; |
GtkWidget *ds1621Frame; |
GtkWidget *tempLabel; |
GtkWidget *pcf8574Frame; |
GtkWidget *hbox, *buttonBox; |
GtkWidget *checkBits[8]; |
GSource *temp_timer_src; |
struct usb_bus *bus; |
struct usb_device *dev; |
int ret; |
/* Initialize the GTK. */ |
gtk_init(&argc, &argv); |
/* Create the hildon program and setup the title */ |
program = HILDON_PROGRAM(hildon_program_get_instance()); |
g_set_application_name("I²C-Tiny-USB Demo"); |
/* Create HildonWindow and set it to HildonProgram */ |
window = HILDON_WINDOW(hildon_window_new()); |
hildon_program_add_window(program, window); |
/************* main view **************/ |
gtk_container_add(GTK_CONTAINER(window), |
GTK_WIDGET(vbox = gtk_vbox_new(FALSE, 0))); |
/************* hardware initialization **************/ |
#ifndef NO_USB |
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 == I2C_TINY_USB_VID) && |
(dev->descriptor.idProduct == I2C_TINY_USB_PID)) { |
char str[128]; |
sprintf(str, "\nI²C-Tiny-USB device on bus %s device %s", |
bus->dirname, dev->filename); |
gtk_box_pack_start(GTK_BOX(vbox), gtk_label_new(str), FALSE, FALSE, 0); |
/* open device */ |
if(!(handle = usb_open(dev))) { |
error("Cannot open the device: %s", usb_strerror()); |
exit(EIO); |
} |
break; |
} |
} |
} |
if(!handle) { |
error("No i2c_tiny_usb device attached", NULL); |
exit(ENODEV); |
} |
/* Get exclusive access to interface 0 */ |
ret = usb_claim_interface(handle, 0); |
if (ret != 0) { |
error("USB error: %s", usb_strerror()); |
exit(EPERM); |
} |
/* do some testing */ |
i2c_tiny_usb_get_func(vbox); |
/* try to set i2c clock to 100kHz (10us), will actually result in ~50kHz */ |
/* since the software generated i2c clock isn't too exact. in fact setting */ |
/* it to 10us doesn't do anything at all since this already is the default */ |
i2c_tiny_usb_set(CMD_SET_DELAY, 10); |
#else |
gtk_box_pack_start(GTK_BOX(vbox), gtk_label_new("\nNO_USB option set"), |
FALSE, FALSE, 0); |
gtk_box_pack_start(GTK_BOX(vbox), gtk_label_new("No functions available\n"), |
FALSE, FALSE, 0); |
#endif |
/************* create ds1621 frame **************/ |
gtk_container_add(GTK_CONTAINER(vbox), |
GTK_WIDGET(ds1621Frame = gtk_frame_new(" ds1621 "))); |
gtk_container_add(GTK_CONTAINER(ds1621Frame), |
GTK_WIDGET(hbox = gtk_hbox_new(FALSE, 0))); |
gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new("Temperature:"), |
FALSE, FALSE, 32); |
gtk_box_pack_start(GTK_BOX(hbox), tempLabel = gtk_label_new("--- °C"), |
FALSE, FALSE, 0); |
#ifndef NO_USB |
/* try to access ds1621 at address DS1621_ADDR */ |
if(usb_control_msg(handle, USB_CTRL_IN, |
CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END, |
0, DS1621_ADDR, NULL, 0, |
1000) < 0) { |
error("USB error: %s", usb_strerror()); |
goto quit; |
} |
if(i2c_tiny_usb_get_status() == STATUS_ADDRESS_ACK) { |
ds1621_present = 1; |
/* activate one shot mode */ |
if(i2c_write_cmd_and_byte(DS1621_ADDR, 0xac, 0x01) < 0) |
goto quit; |
/* wait 10ms */ |
usleep(10000); |
/* build an update timer for the temperature display */ |
temp_timer_src = g_timeout_source_new(1000); |
g_source_set_callback(temp_timer_src, update_temperature, tempLabel, NULL); |
g_source_attach (temp_timer_src, NULL); |
g_source_unref(temp_timer_src); |
} else |
gtk_frame_set_label((GtkFrame*)ds1621Frame, " ds1621 - not found "); |
#endif |
/************* create pcf8574 frame **************/ |
/* create a frame for the pcf8574 elements */ |
gtk_container_add(GTK_CONTAINER(vbox), |
GTK_WIDGET(pcf8574Frame = gtk_frame_new(" pcf8574 "))); |
gtk_container_add(GTK_CONTAINER(pcf8574Frame), |
GTK_WIDGET(hbox = gtk_hbox_new(FALSE, 0))); |
gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new("Output bits:"), |
FALSE, FALSE, 32); |
/* create a button box for the eight buttons */ |
gtk_box_pack_start(GTK_BOX(hbox), buttonBox = gtk_hbutton_box_new(), |
FALSE, FALSE, 0); |
gtk_button_box_set_child_size((GtkButtonBox*)buttonBox, 0, 0); |
/* add the eight buttons */ |
for(i=0;i<8;i++) { |
gtk_container_add(GTK_CONTAINER(buttonBox), |
GTK_WIDGET(checkBits[i] = gtk_check_button_new())); |
g_signal_connect(G_OBJECT(checkBits[i]), "clicked", |
G_CALLBACK(button_clicked), checkBits); |
} |
#ifndef NO_USB |
/* try to access pcf8574 at address PCF8574_ADDR */ |
if(usb_control_msg(handle, USB_CTRL_IN, |
CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END, |
0, PCF8574_ADDR, NULL, 0, |
1000) < 0) { |
error("USB error: %s", usb_strerror()); |
goto quit; |
} |
if(i2c_tiny_usb_get_status() == STATUS_ADDRESS_ACK) { |
pcf8574_present = 1; |
i2c_write_byte(PCF8574_ADDR, 0x00); /* default value */ |
} else |
gtk_frame_set_label((GtkFrame*)pcf8574Frame, " pcf8574 - not found "); |
#endif |
/*************************** *************************/ |
gtk_box_pack_start(GTK_BOX(vbox), |
gtk_label_new("\nhttp://www.harbaum.org/till/i2c_tiny_usb\n"), |
FALSE, FALSE, 0); |
/* begin the main application */ |
gtk_widget_show_all(GTK_WIDGET(window)); |
/* Connect signal to X in the upper corner */ |
g_signal_connect(G_OBJECT(window), "delete_event", |
G_CALLBACK(gtk_main_quit), NULL); |
gtk_main(); |
quit: |
ret = usb_release_interface(handle, 0); |
if (ret) |
error("USB error: %s\n", usb_strerror()); |
usb_close(handle); |
return 0; |
} |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/mobile/readme.txt |
---|
0,0 → 1,52 |
i2c-tiny-usb test application - http://www.harbaum.org/till/i2c_tiny_usb |
------------------------------------------------------------------------ |
This simple test application is meant to demonstrate libusb |
interfacing to the i2c-tiny-usb interface. |
This is no useful application, if you are only interesting in |
using the i2c-tiny-usb interface in your linux box please |
use the kernel driver. |
Linux |
----- |
required packages: libgtk2.0-dev |
This demo application has been developed under and for linux. Just |
make sure you have libusb installed. To use this program just |
compile by typing "make" and run the resulting i2c_usb. |
Be sure that the i2c-tiny-usb kernel driver is not loaded while |
running the test application. Otherwise the test application will |
fail with the follwing error message: |
USB error: could not claim interface 0: Device or resource busy |
This is due to the fact that no two drivers may access the interface |
at the same time. |
Windows |
------- |
This program can be compiled for windows. This has been tested |
under Linux using xmingw and the windows port of libusb |
(see http://libusb-win32.sourceforge.net). To install the |
driver plug the device in and install the driver from |
the win directory. Then run testapp/i2c_usb.exe |
This program may also be compiled under windows using cygwin or |
mingw (which is part of cygwin). In order to use cygwin simply |
copy usb.h win32-linusb to /cygwin/usr/include and libusb.a to |
/cygwin/lib and do a "make -f Makefile.cygwin". Don't forget to |
distribute /cygwin/bin/cygwin1.dll with your file to allow it to |
run in non-cygwin environments as well. No dll is required when using |
mingw. In that case copy usb.h to /cygwin/usr/include/mingw and |
libusb.a to /cygwin/lib/mingw. Finally do a "make -f Makefile.mingw". |
MacOS X |
------- |
The program can be compiled under MacOS as well. The fink version |
of linusb has to be installed and a simple "make -f Makefile.macos" |
will build the native MacOS X version. |
/Designs/Tools/i2c_Tiny_USB/SW/testapp/mobile/readme.txt~ |
---|
0,0 → 1,50 |
i2c-tiny-usb test application - http://www.harbaum.org/till/i2c_tiny_usb |
------------------------------------------------------------------------ |
This simple test application is meant to demonstrate libusb |
interfacing to the i2c-tiny-usb interface. |
This is no useful application, if you are only interesting in |
using the i2c-tiny-usb interface in your linux box please |
use the kernel driver. |
Linux |
----- |
This demo application has been developed under and for linux. Just |
make sure you have libusb installed. To use this program just |
compile by typing "make" and run the resulting i2c_usb. |
Be sure that the i2c-tiny-usb kernel driver is not loaded while |
running the test application. Otherwise the test application will |
fail with the follwing error message: |
USB error: could not claim interface 0: Device or resource busy |
This is due to the fact that no two drivers may access the interface |
at the same time. |
Windows |
------- |
This program can be compiled for windows. This has been tested |
under Linux using xmingw and the windows port of libusb |
(see http://libusb-win32.sourceforge.net). To install the |
driver plug the device in and install the driver from |
the win directory. Then run testapp/i2c_usb.exe |
This program may also be compiled under windows using cygwin or |
mingw (which is part of cygwin). In order to use cygwin simply |
copy usb.h win32-linusb to /cygwin/usr/include and libusb.a to |
/cygwin/lib and do a "make -f Makefile.cygwin". Don't forget to |
distribute /cygwin/bin/cygwin1.dll with your file to allow it to |
run in non-cygwin environments as well. No dll is required when using |
mingw. In that case copy usb.h to /cygwin/usr/include/mingw and |
libusb.a to /cygwin/lib/mingw. Finally do a "make -f Makefile.mingw". |
MacOS X |
------- |
The program can be compiled under MacOS as well. The fink version |
of linusb has to be installed and a simple "make -f Makefile.macos" |
will build the native MacOS X version. |