Jump to content
43oh

SOLVED — How to Implement Clock Stretching for I²C on Tiva C?


Recommended Posts

  • 1 month later...

Here's the solution that I've tested on port I²C (3) of the Stellaris LM4F120 and Tiva C TM4C123 LaunchPads.


 


Libraries to include with #include "Wire.h"



#include "driverlib/i2c.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"

This routine initialise the SCL pin. Not sure it is really useful.



void stretchInit()
{
SysCtlPeripheralEnable(I2C_SCL3_PERIPH);
}

This routine configures the SCL line as a standard output and sets it LOW. Not sure about the 4mA parameter.



void stretchPause()
{
// Standard output
GPIOPadConfigSet(I2C_SCL3_BASE, I2C_SCL3_PIN, GPIO_STRENGTH_4MA, GPIO_PIN_TYPE_STD);
GPIODirModeSet(I2C_SCL3_BASE, I2C_SCL3_PIN, GPIO_DIR_MODE_OUT);
// Set LOW
GPIOPinWrite(I2C_SCL3_BASE, I2C_SCL3_PIN, 0);
}

This routine configures the SCL line back to normal. I tried another option but it didn't work.



void stretchResume()
{
// Reconfigure for I2C
ROM_GPIOPinConfigure(GPIO_PD0_I2C3SCL);
ROM_GPIOPinTypeI2CSCL(I2C_SCL3_BASE, I2C_SCL3_PIN);

// Doesn't work
// HIGH for floating?
// GPIOPinWrite(I2C_SCL3_BASE, I2C_SCL3_PIN, I2C_SCL3_PIN);
// Open drain
// GPIOPadConfigSet(I2C_SCL3_BASE, I2C_SCL3_PIN, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD);
// GPIODirModeSet(I2C_SCL3_BASE, I2C_SCL3_PIN, GPIO_DIR_MODE_HW);
}

Typical usage requires


  • adding stretchInit(); in setup()
  • including stretchPause(); on the first line of void ReceiveEvent(int howMany) as the I²C sequence is buffered
  • placing stretchResume(); on the last line of the routine in charge of executing orders from I²C.

Questions:


  • How to make it universal for the 4 I²C ports available in the LM4F and TM4C MCUs?
  • How to integrate the routines directly into the Wire.h library?
  • How to check the whole process is safe —which values for pull-up resistors, which parameter for mA, no shorts, no damage for the MCU?

Thanks!


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...