Timing of TegI2c_xxx class library

Hello,
I have 2 problems in my I2c driver testing software:

  • the light one is that I set a 400.000 Hz speed and I see on the oscilloscope a 100.000 Hz (on SCL pin, I2C2 channel, follows more detailed listing)
  • the second problem is about a thread managing of the driver: I set a VERY low priority of my thread-task (even up to 10) but sometimes ( easy replicable opening Internet Explorer) my task is delayed-stretched from another process… follow the code.

Obviously, this is only a performance test to know the limit.

#define MAX_THREADS 3

int _tmain(int argc, _TCHAR* argv[])
{
pWork   pDataArray[MAX_THREADS];
DWORD   dwThreadIdArray[MAX_THREADS];
HANDLE  hThreadArray[MAX_THREADS]; 
HANDLE handleI2C;

HANDLE h= GetCurrentThread();
CeSetThreadPriority( h,50);

...

handleI2C= TegI2c_Init( L"I2C2");
TegI2c_SetConfigInt( handleI2C, L"SlaveAddrSize", 7, StoreVolatile);
TegI2c_SetConfigInt( handleI2C, L"RegisterAddrSize", 0, StoreVolatile);
TegI2c_SetConfigInt( handleI2C, L"BitRateHz", 400000, StoreVolatile);
TegI2c_Open( handleI2C);

pDataArray[0].handleI2C= handleI2C;

hThreadArray[0] = CreateThread( 
    NULL,                   // default security attributes
    0,                      // use default stack size  
    SlotThread,       		// thread function name
    pDataArray[0],         // argument to thread function 
    0,                      // use default creation flags 
    &dwThreadIdArray[0]);   // returns the thread identifier 

    while( true)
        Sleep(500);

return 0;
}
  	  
	  
	  
DWORD WINAPI SlotThread( LPVOID lpParam ) 
{ 
    pWork  *pDataArray;
BYTE i2cDataWrite[10]= {0};

HANDLE h= GetCurrentThread();
CeSetThreadPriority( h, 50);

......

pDataArray= (pWork*)lpParam;

while( true)
{
	i2cDataWrite[0]= 0x01;
	retValue = TegI2c_Write(pDataArray->handleI2C, (DWORD*)i2cDataWrite, 1);
	retValue = TegI2c_Read(pDataArray->handleI2C, (DWORD*)i2cDataRead, 10);

	Sleep(1);
}

return 0;

}

so the questions are:

  • why 400.000 Hz don’t run?
  • why despite a very low task-thread priority can’t I have a repeatable timing?

thanks. Davide.

Dear @ddr,

Thank you for contacting support.

why 400 KHx don’t run?

  • Is there multiple i2c communication threads on the application?

  • Could you try to reproduce the issue with our I2cDemo application? which will be inside the Toradex CE libraries. You need to do one modification, I2cSpeed must be set before calling I2cOpen.

    I2c_SetConfigInt(i2c, L"BitRateHz", 400000, StoreVolatile); // Set I2C speed to 400KHz
    // Open the i2c port
    I2c_Open(i2c);

  • We recommend to write I2c_close should be called after I2c_Write and I2c_Read to release the locked resource and other threads can access the i2c channel.

     Success = I2c_Open(i2c);
     I2c_Read(i2c, &tmpBuf, 1);
     I2c_Close(i2c);
    

I2c_Open is locking a resource and it will be unlocked when you call the I2c_close otherwise other threads can’t get the resource to use the I2c channel.

why despite a very low task-thread priority can’t I have a repeatable timing