Shop OBEX P1 Docs P2 Docs Learn Events
Can simpletools i2c_in() and i2c_out() be used for register-based devices or only memory devices? — Parallax Forums

Can simpletools i2c_in() and i2c_out() be used for register-based devices or only memory devices?

I'm working with a clock generator (Si5351) that has registers and I'm not clear on how to properly use i2c_in() and i2c_out() parameters to select and read/write registers. I've read i2c_in.c, i2c_out.c, simplei2c.c.

int  i2c_in(i2c *busID, int i2cAddr, 
                     int memAddr, int memAddrCount, 
                     unsigned char *data, int dataCount)

i2cAddr is the device address from the datasheet (0x60), but what about memAddr? Is that the register?
memAddrCount is the number of bytes to read or is dataCount ?

The Si5351 has 8-bit registers - Is the memAddr the register number?
I'm only reading one byte, right?

Any help would be appreciated.

Comments

  • JonnyMacJonnyMac Posts: 9,229

    Look at the library called simplei2c.c -- it has the low-level functions that you need to direct access to any I2C device.

    On my computer the library is in this folder:
    -- C:\Users\jmcph\Documents\SimpleIDE\Learn\Simple Libraries\Protocol\libsimplei2c

  • Thanks for the reply, @JonnyMac.

    I've pored over that and simplei2c.c The low-level functions in libsimplei2c.c and the higher-level functions in i2c_in and i2c_out all assume memory devices with large multi-byte addresses. They endianSwap or shift right by 8 bits whatever address is sent in the address field and send multiple bytes for the address which doesn't make sense if the address is a one-byte register address. The data sheet for the clock generator shows only single byte addresses for register queries using i2c. I don't need to send multiple bytes for the register address.
    I kinda suspected trouble when I saw all those functions where prefixed with ee_ (eeprom functions)

    I think I'll need to just pick out the appropriate elements and build my own functions for this device.

  • JonnyMacJonnyMac Posts: 9,229

    I think I'll need to just pick out the appropriate elements and build my own functions for this device.

    That how I do it (in Spin). I have a basic I2C library that does start(), write(), read(), and stop(), etc. -- individual component libraries are built on top of that. I've attached an example. Again, this is Spin, but you can see how the functions/methods work. I like I2C; if you have good documentation it's pretty easy to build a component library.

  • @JonnyMac said:

    I think I'll need to just pick out the appropriate elements and build my own functions for this device.

    That how I do it (in Spin). I have a basic I2C library that does start(), write(), read(), and stop(), etc. -- individual component libraries are built on top of that. I've attached an example. Again, this is Spin, but you can see how the functions/methods work. I like I2C; if you have good documentation it's pretty easy to build a component library.

    Even though I'm not versed in Spin, I can understand most of it - Some really helpful code in those documents.

    The wr_reg(), and rd_reg() functions are spot on for what I'm trying to accomplish. The read() and write() boil it down to bytes which is what I was trying to accomplish. The readByte() and writeByte() from the simplei2c.c mirror your code including the clock stretching - I'm fairly sure this is exactly what I needed.

    Thank you.

    I'll keep at it and let you know how it goes.

  • JonnyMacJonnyMac Posts: 9,229

    Glad I could help.

Sign In or Register to comment.