I2C Routines (Spin2/Inline PASM2)
JonnyMac
Posts: 9,104
I translated my jm_i2c.spin to Spin2 and found that it worked, but actually ran faster that 400kHz which is not good with some devices. Where I knew I was going to go, anyway, was converting the routines to inline PASM2 so that the bus clock speed could be controlled. It works. That said, it's version 1, so if you find something wrong please let me know.
I've attached a little demo that is a bus scanner. You can see it found two devices. The device at $AE is a 24C32N EEPROM. The device at $D0 is a DS3231 RTC. Both devices are on a little module that I picked up on Amazon. I tested the I2C methods by setting the clock and reading it back, watching the time change every second.
The updated (March 18) version allows you to specify the internal pull-up strength or none at all (e.g., external pull-ups are used). Block methods have been added, too.
March 19: Updated to use new constants for pin modes in PNut v34o.
I've attached a little demo that is a bus scanner. You can see it found two devices. The device at $AE is a 24C32N EEPROM. The device at $D0 is a DS3231 RTC. Both devices are on a little module that I picked up on Amazon. I tested the I2C methods by setting the clock and reading it back, watching the time change every second.
The updated (March 18) version allows you to specify the internal pull-up strength or none at all (e.g., external pull-ups are used). Block methods have been added, too.
March 19: Updated to use new constants for pin modes in PNut v34o.
Comments
Thanks, JonnyMac!
dgately
I think there was a downside to 1.5k, trying to remember what it was...
Maybe it was the response of the device being slower with a lower value resistance...
It's probably the best choice though.
There are potential issues when mixing high speed and low speed devices on the same bus...
15k is probably better for low speed, 1.5k for high speed...
Will that work with multiple devices on the same bus?
bytecode would be more compact and I2C does't really need assembly.
My driver only has 3 lines of ASM to set the pullup strength...
I just left the rest as Spin. You don't save anything with Fastspin, but didn't seem worth the bother to switch to assembly...
Here's an example of what I'm using for comparison.
My Spin2 code -- where I started -- is attached. I'll have a look at your code, too.
I am only using PNut; none of my code has tested with FastSpin.
So, maybe you do need assembly...
Is that at the same clock rate? I haven't done any measurements, myself, yet.
-- added pull-up configuration
-- added wr_block() and rd_block() methods
Thanks, John. Considering we have a 2x improvement at the PASM level, that means the XBYTE is yielding an 8x improvement.
Yes, it is only slightly different from a 3.3k pullup.
The resistor has dropped to 0.5mA at 50% Vcc, so you get the classic exponential rise, whilst a current source will still be 1mA at 50%.
That means a constant current pin will have a bit faster rise time for the same power drain.
A current source will tail off very close to Vcc.
Does anyone have the knee voltage of the P2 in current mode (guess ~ 500~700mV below Vcc?) or any curve tracer plots ?
This is an LCD demo for a PCF8574 LCD backpack and 2x16 LCD. Tested with PNut v34o.