cancel
Showing results for 
Search instead for 
Did you mean: 

ST7_LIB2 beta library I2C routines

md1
Associate II
Posted on November 09, 2004 at 07:11

ST7_LIB2 beta library I2C routines

4 REPLIES 4
md1
Associate II
Posted on October 22, 2004 at 12:41

Hi,

I am having a problem with the I2C routines in ST's beta libraries.

The following code is derived from ST's example:

void LCD_WriteBytes(BYTE addr, BYTE count, void *buf)

{

BYTE tmp1, tmp2;

I2C_TxErrCode_t err;

BYTE *data;

data = (BYTE*)buf;

I2C_Generate_Start(); // Generate start bit

while(!(I2C_IsTransmitCompleted()== I2C_START_OK));

/*================================================

Transmission through ‘Polling’ mode

=================================================*/

/* Communication mode defined as POLLING_TX in ST7lib_config.h */

I2C_Load_Address (addr, I2C_TX_MODE);

tmp1 = I2C_IsTransmitCompleted();

while ((User_Timeout_Function()) && (tmp1 != I2C_ADD_TX_OK))

{

tmp1 = I2C_IsTransmitCompleted();

}

switch (tmp1) /* To check transmission status */

{

case I2C_TX_AF:

User_Function(tmp1);

break;

default:

break;

}

// Write the data

err = I2C_PutBuffer(data, count);

switch (err) {

case I2C_TX_AF:

case I2C_TX_ARLO:

case I2C_TX_BERR:

User_Function(err); /* Error Management */

break;

case I2C_DATA_TX_OK:

break;

default: /* None of the above condition is true */

User_Function(err);

break;

}

I2C_Generate_Stop(); /* Transmission Stopped */

while (!(I2C_IsStopGen()));

}

This code works fine if I single step through, but hangs for ever if I run on the DVP3 or program a micro and run it.

I am not sure it is down to a timing issue, but the code hangs in this function...

I2C_TxErrCode_t I2C_PutBuffer (const unsigned char *PtrToBuffer,

unsigned char NbOfBytes)

{

unsigned char Temp, Temp1, Temp2 ;

I2C_Status = 0 ;

Temp2 = I2CSR1 ;

while (!( Temp2 & BTF))

{ /* Waiting for address or data byte transmission completion */

Temp2 = I2CSR1 ;

}

...in the while loop above.

The state of Temp2 when I single step is 10111010 indicating successfull completion of writing out the address byte.

The state of Temp2 when I run in real time is 10010010, and never changes.

Any ideas?

Cheers,

Martin.
nikhil2
Associate
Posted on October 25, 2004 at 08:23

Hi,

From the description it seems that software is not able to clear the EVF bit which is set just after the Address reception (10010010).

This might be the case if any I2C interrupt is remained enabled. Please check that if any I2C interrupt is remianed enabled.

Two suggestion from my side are-

1) Always intialize the I2C with I2C_Init() function with proper arguments.

2) At what speed you are operating the I2C.

Initializing it using the I2C_Select_Speed() function might also be useful.

Cheers,

Nikhil
md1
Associate II
Posted on October 27, 2004 at 12:27

Hi Nikhil,

Thanks for you reply.

The I2C_Init function is being called like this:

I2C_Init(I2C_ENABLE_ACK);

I2C_MultiMaster_Config(); // Configure I2C as multimaster I2C device

I2C_Select_Speed (I2C_DEFAULT_PARAM2, (unsigned int)100); // Selects fast speed mode, Speed is 100KHz

Which is basically straight from the example.

I have got around the problem for now by putting delays in the code. Please see attached example.

I now have no problems with the I2C. I will investigate further when I get chance but you know what its like when you have a live project. I definately need to fix this before I release product though.

Many thanks for your help again...

Martin.

________________

Attachments :

i2c_write_bytes.txt : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HzVa&d=%2Fa%2F0X0000000bUR%2FU4ZPkcVm8MfihNPfBGKoOg3plZl0BsJvIdPYGevb0vo&asPdf=false
nikhil2
Associate
Posted on November 09, 2004 at 07:11

Hi Martin,

As I mentioned earlier, that the software is not able to clear EVF just after the load the address as mentioned below-

I2C_Load_Address (addr, I2C_TX_MODE);

// This is a work-around for now, take this up with ST

delay();

tmp1 = I2C_IsTransmitCompleted();

while ((User_Timeout_Function()) && (tmp1 != I2C_ADD_TX_OK))

{

tmp1 = I2C_IsTransmitCompleted();

}

My doubt here is that when we are not using the delay() function. Actually the control is coming out from the above while loop for checking the I2C_ADD_TX_OK is not checking it correctly.

My suggestion here is that, Instead of putting delay() at every section. You either remove the UserTimeOut() from above while loop. Or increase the delay in this function.

I think it should work then as I dont see any other problem.

Cheers,

Nikhil