|
|
|
Re: I2C again | |
| [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
|
|
On Saturday 29 November 2008, viper@xxxxxxxxxxx wrote:> Hi all,>> I’m trying to use a Texas Instruments TMP175 temprature sensor on an> AT91RM9200 system ( i2c-gpio).>> The code (part of it):>> //global vars> int i2c_handler;>> int main( int argc, char *argv[] ) {> int i=0;> int value=0;> int adapter_nr;> int err;> int funcs;> char buf[8];>> //we don't have to initialize the I2C, since it's done by the driver>> //opening the device> printf("Opening /dev/i2c\n");> if ( ( err = ( i2c_handler = open("/dev/i2c-0",O_RDWR) ) ) < 0 ) {> printf("Error opening /dev/i2c-0! Exiting (%d -> %s).\n",err,strerror(errno));> exit(1);> }>> // check for req funcs> printf("Getting available IOCTLs\n");> // get funcs list> if((err = ioctl(i2c_handler, I2C_FUNCS, &funcs) < 0)) {> printf( "Error eeprom_open: %s\n", strerror(err));> exit(1);> }> CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE );> CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE );> CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE_DATA );> CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE_DATA );> CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_WORD_DATA );> CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_WORD_DATA );>> printf("Setting slave addr to 0x79\n");> if ( ( err = ioctl(i2c_handler,I2C_SLAVE,0x79) ) < 0 ) {> printf("Error setting SLAVE address! Exiting (%d).\n",err);> exit(1);> }>> value = i2c_smbus_read_byte(i2c_handler);> if ( value < 0 )> fprintf( stderr, "Failed to read from I2C device: %m\n" );>> printf("Reading with read\n");> if ( read(i2c_handler,buf,1) != 1 ) {> printf("Error reading byte! (%s)\n",buf);> }>> printf("Reading with i2c_read\n");> if ( ( value = i2c_smbus_read_byte(i2c_handler)) < 0 ) {> printf("Error readiny byte! (%d)\n",value);> }>> printf("Trying direct write!\n");> buf[0] = 0;>> if ( write(i2c_handler,buf,1) != 1 ) {> printf("Error writing byte! (%s)\n",buf);> return -1;> }>> The result:> root@(none):/mnt/tmp175# ./tmp175> Opening /dev/i2c> Getting available IOCTLs> Setting slave addr to 0x79> Failed to read from I2C device: Operation not permitted> Reading with read> Error reading byte! (X†)> Reading with i2c_read> Error readiny byte! (-1)> Trying direct write!> Error writing byte! ()>> The ’dmesg’ output, since debug messages are turned on:> i2c-adapter i2c-0: ioctl, cmd=0x705, arg=0xbee5fde4> i2c-adapter i2c-0: ioctl, cmd=0x703, arg=0x79> i2c-adapter i2c-0: ioctl, cmd=0x720, arg=0xbee5fd74> i2c-adapter i2c-0: master_xfer[0] R, addr=0x79, len=1> i2c-adapter i2c-0: NAK from device addr 0x79 msg #0> i2c-dev: i2c-0 reading 1 bytes.> i2c-adapter i2c-0: master_xfer[0] R, addr=0x79, len=1> i2c-adapter i2c-0: NAK from device addr 0x79 msg #0> i2c-adapter i2c-0: ioctl, cmd=0x720, arg=0xbee5fd74> i2c-adapter i2c-0: master_xfer[0] R, addr=0x79, len=1> i2c-adapter i2c-0: NAK from device addr 0x79 msg #0> i2c-dev: i2c-0 writing 1 bytes.> i2c-adapter i2c-0: master_xfer[0] W, addr=0x79, len=1> i2c-adapter i2c-0: NAK from device addr 0x79 msg #0>> What’s wrong? Am I just lame, or it’s something with the device?>> Thanks,>> Tamas
I suspect your slave address is wrong. For example, if the docs say your device's slave address is 0x26, you need to use a value of 0x13 (shift 1 >> or divide by 2) in the ioctl operations.
-Jeff-- Secure your email with Gnu Privacy Guard and Thunderbird, the open sourcecross platform solution for Windows, Linux, and Mac OS X.See http://enigmail.mozdev.org/
-------------------------------------------------------------------List admin: http://lists.arm.linux.org.uk/mailman/listinfo/linux-armFAQ: http://www.arm.linux.org.uk/mailinglists/faq.phpEtiquette: http://www.arm.linux.org.uk/mailinglists/etiquette.php
[Linux ARM] [Linux ARM MSM] [Linux ARM Kernel] [Fedora ARM] [IETF Annouce] [Security] [Bugtraq] [Linux] [Linux OMAP] [Linux MIPS] [ECOS] [Asterisk Internet PBX] [Linux API]
![]() |
![]() |