libsoc icon indicating copy to clipboard operation
libsoc copied to clipboard

i2c: implement register access

Open yegorich opened this issue 11 years ago • 6 comments

Allow to pass register address to the read function in order to read one byte from supplied register address. See an example in this blog (http://bunniestudios.com/blog/images/infocast_i2c.c) function get_i2c_register()

yegorich avatar Jun 16 '14 15:06 yegorich

I could get on board with this. I'll add it to the TODO list, hoping to have some time this week/weekend to spend a bit of time catching up with the backlog.

jackmitch avatar Jun 16 '14 21:06 jackmitch

Jack, I have a need for this too. Are you open to collaborators? If I made changes and issued a pull-request, are you open to that?

From a design perspective, I would envision a new API called:

libsoc_i2c_transaction(
   i2c *i2c,
   i2c_transaction *transactions,
   uint16_t numTransactions)

This API would take an array of transactions and perform them as a unit. The i2c_transaction data type would be a structure composed of:

struct i2c_transaction {
   uint8_t  *buffer;
   uint16_t len;
   uint8_t  write; // 0 for read, non zero for write
}

nkolban avatar Aug 02 '16 18:08 nkolban

Your example is very similar to the current libsoc_i2c_write function. I think what @yegorich was implying was something more akin to:

int libsoc_i2c_write_register(i2c *i2c, u16 regnum, u16 value);
u16 libsoc_i2c_read_register(i2c *i2c, u16 regnum);

which would be an easy way to encapsulate a single read/write over i2c without having to mess about with buffers and lengths.

Care to comment @yegorich ?

jackmitch avatar Aug 03 '16 11:08 jackmitch

@jackmitch that's what I mean. A call, that accepts register number. @nkolban see the source file I was referring to in my initial post.

yegorich avatar Aug 03 '16 13:08 yegorich

I understand. I didn't like the "consumability" of the option I proposed but it would give full flexibility for scenarios we hadn't anticipated. If we look at wiringPi here ...

http://wiringpi.com/reference/i2c-library/

We see the following APIs:

int wiringPiI2CRead (int fd)
int wiringPiI2CWrite (int fd, int data)
int wiringPiI2CWriteReg8 (int fd, int reg, int data) 
int wiringPiI2CWriteReg16 (int fd, int reg, int data)
int wiringPiI2CReadReg8 (int fd, int reg) ;
int wiringPiI2CReadReg16 (int fd, int reg)

Some I2C bytes when reading or writing a register want:

  • 8 bit data
  • 16 bit data
  • variable length data

If we provided only:

int libsoc_i2c_write_register(i2c *i2c, u16 regnum, u16 value);
u16 libsoc_i2c_read_register(i2c *i2c, u16 regnum);

Then what about the 8 bit register data read and write?

Maybe the right answer would be:

int libsoc_i2c_write_register8(i2c *i2c, u16 regnum, u8 value);
u8 libsoc_i2c_read_register8(i2c *i2c, u16 regnum);
int libsoc_i2c_write_register16(i2c *i2c, u16 regnum, u16 value);
u16 libsoc_i2c_read_register16(i2c *i2c, u16 regnum);

nkolban avatar Aug 03 '16 16:08 nkolban

I would be happy to consider a pull request which implemented the above functions.

jackmitch avatar Aug 15 '16 12:08 jackmitch