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:
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.
Here is the alternative include file for software I2C. Full instructions are included within it.
Nice work Thomas,
Your effort is appreciated.
I am doing a similar thing with software serial.
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.
Good work. Thanks for posting.
Initial code review. I will test after these mods have been made.
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.
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.
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.
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.