Alternative Software I2C Include File

2014-07-17
2014-08-25
  • Thomas Henry
    Thomas Henry
    2014-07-17

    Hi gang,

    I hope this doesn't upset anyone, but I just had to rework the software I2C stuff. You see, I had spent over three weeks trying to get the old code working and just wasn't getting anywhere. Worse, I also couldn't understand the code. So, I decided to go back to Hugh's original include file and comment the daylights out of it, so I could understand it. I've added over 300 comments!

    Once I did that and knew what was going on, the next step was to simplify everything like crazy. I felt like I could get by with just the basic commands, rather than have the thing blossom with all sorts of new commands to handle exceptions. Moreover, I reworked things to be as easy as possible for I2C newcomers (which included me last month). And there's lots of error detection should you need it. (Or you can ignore it and just get down to brass tacks). The only commands needed now are:

    I2CStart()
    I2CStop()
    I2CAckPoll()
    I2CReceive()
    I2CSend()

    Both Master and Slave operation are supported. I have tested this on Eeproms, real-time clocks, and homebrew Slaves and so far everything has been very predictable and reliable. I've also tested for several deliberately caused error conditions, to see if it catches them. It would be great if someone else put it through the wringer for confirmation. Bus speed is approximately 75 kHz. I've tested on two different PICs, with and without external resonators.

    In addition to the thorough commenting throughout, at the start of the source code you'll find a complete set of instructions for the five commands, both as Masters or Slaves.

    I'll attach the alternative file in the next post. Please forgive me for being so brash as to rework Hugh's code. I just needed something I could use in my own work and could understand. If this bothers the moderators, feel free to delete the file.

    Thomas Henry

     
  • Thomas Henry
    Thomas Henry
    2014-07-17

    Here is the alternative include file for software I2C. Full instructions are included within it.

     
    Attachments
  • William Roth
    William Roth
    2014-07-18

    Nice work Thomas,

    Your effort is appreciated.

    I am doing a similar thing with software serial.

     
  • Hugh Considine
    Hugh Considine
    2014-07-18

    Please feel free to rework any code I've written - there are plenty of bits that need improvement! As long as you add a comment at the top of the file to say what has changed, and try to make sure that the changed library is compatible with programs that use the original, all is well!

    Thomas, I'll try out that code soon and see how it goes.

    William, I apologise for the mess in the software serial routines! That would be one of the oldest libraries in GCBASIC, I think only eeprom.h is older. Any improvements are welcome! The timings may have originally been ok, but changes to the compiler may have since resulted in more or less assembly code being generated for the same BASIC, which would throw things out.

     
  • Anobium
    Anobium
    2014-07-19

    Good work. Thanks for posting.

    Initial code review. I will test after these mods have been made.

    1. Please restore I2CStartOccurred. This was in the original code and is documented in the examples and help file.
    2. Please restore I2CRESTART. You may be able to reference to I2CSTART if the code is the same but this makes it easier to follow the exact protocol as described in many datasheets.
    3. Please restore I2CRESET. This is required when using the SDA/SCL lines in multi use mode ie when used as Serial and I2C and you need to 'shake' the I2C bus.
    4. I2CACKPOLL no longer returns I2CAckPollState. This is required for I2CDiscovery. What other globals have been removed from the currently release code?
    5. The copyright should be Hugh 2009, myself 2013 and yourself 2014. I stand to be corrected but this is not correct in the file as posted.

    Can you impact the changes to documentation after you have made the changes above? Are there are new global variables, some variable names have been changed etc. Please use the web site HTM as the baseline as this is at the same release level as the CHM help file. I will edit/merge when I get your revised copy.

    I will replace the existing I2C.h file in Sourceforge after we have the help file sorted.

     
    • Anobium
      Anobium
      2014-07-19

      If you can make these few changes I will do the functional testing as at the moment I am getting errors with missing methods and variables.

      Thank you.

       
  • Thomas Henry
    Thomas Henry
    2014-07-19

    Whoa!

    As mentioned, I went back and worked strictly from Hugh's 2009 code, mainly just to comment it so I could understand how it worked. Hugh's copyright notice appears, as indeed it should. As far as I know, since I was working from the 2009 include file, none of Anobium's code was used, unless Hugh didn't note it as such. Any comments I added are unworthy of a copyright. Hugh was the writer, I merely looked over his shoulder and tried to explain things.

    I simply offered this file as an alternative since I found it useful, not a replacement. My concern was that later files were simply grandfathering in things that weren't quite right, just for the sake of "backwards compatibility" and that fundamental issues weren't being addressed before we built upon them.

    In any event, I want nothing to do with copyright issues and such, so am bowing out. Feel free to continue using anything I've contributed these past few years if it's useful to others.

    TH

     
  • Anobium
    Anobium
    2014-08-25

    Uploaded to the released code section of SourceForge, see https://sourceforge.net/p/gcbasic/code/HEAD/tree/GCBASIC/trunk/include/lowlevel/i2c.h

    I have updated header for copyright etc. This is now destined for general release. I will update the Help File as soon as practical.

    This is an extension revision of the software I2C code. Testing has been completed for backwards compatibility, use as a Master and Slave. This is a stability release and a merger of all the good work over the last few years.

    Thank you!!