Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i2c: implement register access #14

Open
yegorich opened this issue Jun 16, 2014 · 6 comments
Open

i2c: implement register access #14

yegorich opened this issue Jun 16, 2014 · 6 comments

Comments

@yegorich
Copy link
Contributor

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()

@jackmitch
Copy link
Owner

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.

@nkolban
Copy link

nkolban commented Aug 2, 2016

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
}

@jackmitch
Copy link
Owner

jackmitch commented Aug 3, 2016

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 ?

@yegorich
Copy link
Contributor Author

yegorich commented Aug 3, 2016

@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.

@nkolban
Copy link

nkolban commented Aug 3, 2016

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);

@jackmitch
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants