i2c programming in C/C++



  • Hi,

    is there any example about how to control the I2C bus from a C program?

    a basic approach would be to call the i2ctools commands, but it would be very heavy and inelegant, so I would like to avoid it.

    I would also like to avoid to do basic GPIO 20&21 control.

    If the code of the i2ctools is publicly available I guess it would be a good starting point for me.

    Thanks in advance for any help!



  • @Frederic-Baumann I want this too. I will probably be attempting to write a C/C++ library for I2C control. I have yet to find some suitable guidelines to base my code off.
    As soon as I have done it I will be posting it on the Onion Community



  • Thanks for your answer.

    Right now I am going to start playing with your cool-looking GPIO C++ API :-)



  • @Frederic-Baumann Thanks for your kind comment.
    I will soon be adding GPIO interrupt handling to the C++ code as well.
    Additionally, based off that code, I have in progress Java wrappers for it that run well under jamvm though you need to provide more space on the Omega with USB storage and the pivot-overlay or pivot-root methods described in https://wiki.onion.io/Tutorials/Using-USB-Storage-as-Rootfs


  • administrators



  • @Lazar-Demin Thanks for the reference - I must have been blind not to see that! :-(



  • Thanks, actually this afternoon I was "googling" "i2c onion", and found it.

    Then, I have seen that the .so was present on the Onion, however I had a hard time to:

    • find the header files either on the Onion or in the Kubuntu development environment
    • make the linking work on the Kubuntu env after having copied the .so from the Onion to Kubuntu (no symbols found in it according to nm).

    So finally, I have found this link: https://github.com/OnionIoT/i2c-exp-driver showing source code.

    I could copy 2 .h and 2 .c files to my Kubuntu, and compile+link them directly in my app (not using the lib).

    And the good news and happy end is that it works pretty fine :-)

    Indeed, it is not very clear to me who is doing what on the Onion. For instance, who has developed or porter this I2C lib to the Onion. Why isn't it downloadable (with the header files) directly from the Onion.io website?

    Thanks in any case, it's cool to do stuff on the Onion!
    Frédéric



  • I have been looking at the code in https://github.com/OnionIoT/i2c-exp-driver - it all seems to make pretty good sense to me.
    However, I have a couple of small questions:

    1. As far as I can see, the Omega can only ever act as an i2c master not as an i2c slave
      Is this correct? If I am mistaken can you point me at how one could use it as a slave
    2. As far as I can see, the int devNum (and similar parameters) to the various routines should always be set to 0 for the Omega since there is aonly one i2c device
      Is this correct?

    Thanks in advance for any feedback :-)


  • administrators

    Hi @Kit-Bishop,

    1. Omega can act as both the master and the slave, however, we have only implemented the master logic in the driver right now because 99% of the time the Omega is used as a master. The logic for the slave has also been planned, we just haven't had a chance to implement it yet!

    2. You can actually configure two other GPIO pins as another I2C bus. In that case you will can set it to 0 or 1. But by default, we have only one I2C bus (pin 20 and 21), so in most cases you will be setting the devNum to 0.



  • @Boken-Lin Thanks for the info.

    I don't have an immediate need for these additional features but was just generally interested.
    In the past, I have implemented code for 2 Arduino devices to communicate with each other via I2C - one as master, one as slave. Particularly handy with the Teensy 3 which has 2 I2C devices built in so on each, one can be a master and one a slave making 2 way communication easier.

    However, I suspect that as/when I need two (or more) Omegas to communicate with each other, there will be better ways than I2C


  • administrators

    @Kit-Bishop Yup, it's much easier to use tcp socket or even HTTP for that.



  • @Boken-Lin That's the kind of thing I was thinking of :-) Arduino has no where near the capabilities to do that so I had to come up with other methods :-)


Log in to reply
 

Looks like your connection to Onion Community was lost, please wait while we try to reconnect.