2009-04-30 02:51:38     i2c sending commands

Document created by Aaronwu Employee on Aug 15, 2013
Version 1Show Document
  • View in full screen mode

2009-04-30 02:51:38     i2c sending commands

Ralph Bertelsmann (GERMANY)

Message: 73459   

 

Hello everybody,

 

i have an i2c question.

 

 

 

I connected two i2c devices to my bf board running svn trunk.

 

They both show up:

 

root:~> i2cdetect 0

i2cdetect 0

WARNING! This program can confuse your I2C bus, cause data loss and worse!

I will probe file /dev/i2c-0.

I will probe address range 0x03-0x77.

Continue? [Y/n]

 

 

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f

00:          -- -- -- -- -- -- -- -- -- -- -- -- --

10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

40: -- -- 42 -- -- -- -- -- -- -- -- -- -- -- -- --

50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

70: -- -- -- -- -- -- -- --                       

 

Ok, the first device (0x60) is a compass. I can use it by reading out its registers, using i2c_read_register() provided in  ppifcd-test-orig by Michael Hennerich. No problem here.

 

 

 

But the second device doesn't work this way (0x42, rfid reader).

 

The manual says that i have to send certain control commands to the device.

 

Example Code for a PIC processor says:

 

//--------------------------------------------------------------------

        //----->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

        //*****               Read Firmware Version Command                  ******

        //----->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

 

         printf("Firmware:");

 

         i2c_start();

         i2c_write(0x84); //42h (7 bit addressing) (left shift)--> (84h when writing) (85h when reading)

         i2c_write(0x01); //  Length of Data including Command ID, excluding Checksum byte

         i2c_write(0x81); //  Read Firmware Version Command

         i2c_write(0x82); //  Checksum = Length + Data

         i2c_stop();

         Count=get_response(10);

            if (Count>0)

            {

                        //Firmware version string will be in Buffer

                     for(i=0;i<=Count+1;i++)

                  printf("%x",Buffer[i]);

 

 

            }

 

I can use the reader via UART and a windows program, and the log tells me the same:

 

TX > FF 00 01 81 82                                      (Read Firmware Version)

RX <FF 00 08 81 49 32 43 20 32 2E 38 FF                 I2C 2.8            FIRMWARE VERSION

 

 

 

So how can i send a command like 01 81 82 (the read firmware command for example) to the i2c device?

 

Is there a function?

 

Any tips where i can look for it?

 

 

 

Thanks in advance & greetings!

TranslateQuoteReplyEditDelete

 

 

2009-04-30 03:06:30     Re: i2c sending commands

Mike Frysinger (UNITED STATES)

Message: 73462   

 

if you dont know how to use I2C, then read the I2C docs:

http://docs.blackfin.uclinux.org/doku.php?id=i2c#linux_kernel_framework

QuoteReplyEditDelete

 

 

2009-04-30 04:51:54     Re: i2c sending commands

Ralph Bertelsmann (GERMANY)

Message: 73480   

 

Ah ok,

 

i browsed over the I2C docs and changed Michaels Code a bit:

 

 

 

int myi2c_send(char * device, unsigned char client, unsigned int value, int length)

{

    int    addr = I2C_SLAVE_ADDR;

    char   msg_data[32];

    struct i2c_msg msg = { addr, 0, 0, msg_data };

    struct i2c_rdwr_ioctl_data rdwr = { &msg, 1 };

  

    int fd,i;

 

  if ( (fd = open( device, O_RDWR ) ) < 0 ) {

    fprintf(stderr, "Error: could not open %s\n", device);

    exit( 1 );

  }

 

  if ( ioctl( fd, I2C_SLAVE, addr ) < 0 ) {

    fprintf(stderr, "Error: could not bind address %x \n", addr );

  }

 

    msg.len   = length;

    msg.flags = 0;

    for (i = length-1; i >= 0; i--){

    msg_data[i] = (0xFF & value);

    value = value >> 8;  

    }

    msg.addr = client;

 

  if ( ioctl( fd, I2C_RDWR, &rdwr ) < 0 ) {

    fprintf(stderr, "Error: could not write \n");

  }

 

  close( fd );

  return 0;

}

 

And

 

myi2c_send(I2C_DEVICE,I2C_DEVID, 0x018182, 3);

 

sends the command mentioned above.

 

 

 

Works for me, thank you Mike.

 

Greetings

Attachments

    Outcomes