Add clock stretching to I2C component
Moderator: Benj
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Add clock stretching to I2C component
It would be much better if I2C component has clock stretching added which pauses master until slave is ready.
Then Loops or delays to wait for slave to be ready won't be required.
Would simplify flowchart so less ROM is used n target device.
Martin
Then Loops or delays to wait for slave to be ready won't be required.
Would simplify flowchart so less ROM is used n target device.
Martin
Martin
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Add clock stretching to I2C component
Hello,
I'm looking into this and I'm probably missing something.
I'm looking at the 24EPxxxGU datasheet at the I2C control registers.
There are clock stretching control bits however they all seem to relate to the I2C being in slave mode. Is clock stretching a function of the master or the slave? I feel I had better do a bit more research to understand what clock stretching does.
I'm looking into this and I'm probably missing something.
I'm looking at the 24EPxxxGU datasheet at the I2C control registers.
There are clock stretching control bits however they all seem to relate to the I2C being in slave mode. Is clock stretching a function of the master or the slave? I feel I had better do a bit more research to understand what clock stretching does.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Add clock stretching to I2C component
Right I found this page which has some good info.
http://stackoverflow.com/questions/2486 ... stretching
Looks like we need to revisit our software I2C layer to add the ability to wait for the SCL to float high after we release it.
I'm hoping that hardware I2C master will already do this automatically?
Here are the modded CAL files for v7 which hopefully add clock stretching support for the software channels in master mode.
http://stackoverflow.com/questions/2486 ... stretching
Looks like we need to revisit our software I2C layer to add the ability to wait for the SCL to float high after we release it.
I'm hoping that hardware I2C master will already do this automatically?
Here are the modded CAL files for v7 which hopefully add clock stretching support for the software channels in master mode.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: Add clock stretching to I2C component
Thanks Ben,
Master pauses until slave releases the line.
I will have a play and let you know how I get on.
That's how how i believed it worked.Benj wrote:Looks like we need to revisit our software I2C layer to add the ability to wait for the SCL to float high after we release it.
Master pauses until slave releases the line.
I will have a play and let you know how I get on.
Martin
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: Add clock stretching to I2C component
Hi Ben,
Unfortunately the update (PIC) has stopped i2c from working.
I not not yet modified it for clock stretching.
Martin
Unfortunately the update (PIC) has stopped i2c from working.
I not not yet modified it for clock stretching.
Martin
Martin
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Add clock stretching to I2C component
In the I2C CAL File I basically changed these lines in the master transmit byte and receive byte functions.
To these lines.
Maybe the while is having a negative effect?
Code: Select all
MX_I2C_DELAY;
FC_CAL_Bit_In_DDR(MX_I2C_SCL_PORT_X, MX_I2C_SCL_TRIS_X, MX_I2C_SCL_PIN_X); //Set SCL High
MX_I2C_DELAY;
FC_CAL_Bit_Low_DDR(MX_I2C_SCL_PORT_X, MX_I2C_SCL_TRIS_X, MX_I2C_SCL_PIN_X); //Set SCL Low
Code: Select all
MX_I2C_DELAY;
FC_CAL_Bit_In_DDR(MX_I2C_SCL_PORT_X, MX_I2C_SCL_TRIS_X, MX_I2C_SCL_PIN_X); //Set SCL High
MX_I2C_DELAY;
while (FC_CAL_Bit_In_DDR(MX_I2C_SCL_PORT_X, MX_I2C_SCL_TRIS_X, MX_I2C_SCL_PIN_X) == 0); //Wait for the clock to go high - Clock stretching.
FC_CAL_Bit_Low_DDR(MX_I2C_SCL_PORT_X, MX_I2C_SCL_TRIS_X, MX_I2C_SCL_PIN_X); //Set SCL Low
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Add clock stretching to I2C component
Aha,
FC_CAL_Bit_In_DDR doesn't return a value. You have to instead use FC_CAL_Bit_In.
Hopefully this will work better.
Now looks like this.
FC_CAL_Bit_In_DDR doesn't return a value. You have to instead use FC_CAL_Bit_In.
Hopefully this will work better.
Now looks like this.
Code: Select all
MX_I2C_DELAY;
FC_CAL_Bit_In_DDR(MX_I2C_SCL_PORT_X, MX_I2C_SCL_TRIS_X, MX_I2C_SCL_PIN_X); //Set SCL High
MX_I2C_DELAY;
while (FC_CAL_Bit_In(MX_I2C_SCL_PORT_X, MX_I2C_SCL_PIN_X) == 0); //Wait for the clock to go high - Clock stretching.
FC_CAL_Bit_Low_DDR(MX_I2C_SCL_PORT_X, MX_I2C_SCL_TRIS_X, MX_I2C_SCL_PIN_X); //Set SCL Low
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Add clock stretching to I2C component
At the moment in the new CAL file clock stretching is enforced i.e. always on.
Would it be better if there was a property in the component to turn it back off for software mode or should it always be enforced to better match what the hardware mode does?
Would it be better if there was a property in the component to turn it back off for software mode or should it always be enforced to better match what the hardware mode does?
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: Add clock stretching to I2C component
Hi Ben,
Works really well in deed.
Removed all the delays so just got: Before changing sensor command from No hold master to hold master, the reading without delays are totally wrong.
Changing to hold master then recompile and got: Now compare with SHT21 component: Conclusion is using clock stretching there are less errors so readings are more stable.
My guess is leaving it on then if not clock stretching supported then delays or loops can be added.
Martin
Works really well in deed.
Removed all the delays so just got: Before changing sensor command from No hold master to hold master, the reading without delays are totally wrong.
Changing to hold master then recompile and got: Now compare with SHT21 component: Conclusion is using clock stretching there are less errors so readings are more stable.
I'm not sure.Benj wrote:Would it be better if there was a property in the component to turn it back off for software mode or should it always be enforced to better match what the hardware mode does?
My guess is leaving it on then if not clock stretching supported then delays or loops can be added.
Martin
Martin
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Add clock stretching to I2C component
Thanks Martin,
I've rolled out the change so clock stretching is now active for software channels on all of the CALs.
I've rolled out the change so clock stretching is now active for software channels on all of the CALs.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: Add clock stretching to I2C component
Your welcome.
Thank you for implementing so fast.
Martin
Thank you for implementing so fast.
Martin
Martin