I2c

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

I2c

506 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by affonpign on Thu Oct 06 02:04:29 MST 2011
Hello everybody,

I use the Lib_MCU, which is a interface driver library Embedded Artists provides. I use the I2C driver. The I2C drivers can be found in the lpc17xx_i2c.c file. I copied the I2C_SendByte function right bellow:

/***********************************************************************
* Function: I2C_SendByte
* Purpose: Send a byte
* Parameters:
*     I2Cx: Pointer to I2C register
* Returns: value of I2C status register after sending
**********************************************************************/
static uint32_t I2C_SendByte (LPC_I2C_TypeDef *I2Cx, uint8_t databyte)
{
    /* Make sure start bit is not active */
    if (I2Cx->I2CONSET & I2C_I2CONSET_STA)
    {
        I2Cx->I2CONCLR = I2C_I2CONCLR_STAC;
    }
    I2Cx->I2DAT = databyte & I2C_I2DAT_BITMASK;
    I2Cx->I2CONCLR = I2C_I2CONCLR_SIC;

    while (!(I2Cx->I2CONSET & I2C_I2CONSET_SI));
    return (I2Cx->I2STAT & I2C_STAT_CODE_BITMASK);
}


The following command should clear the interrupt flag:

I2Cx->I2CONCLR = I2C_I2CONCLR_SIC;

But then the following line waits in the while loop until the interrupt flag SI is set. I do not understand that because we just cleared the interrupt flag.

If I check the status of the control register I see that the interrupt flag is still set after it is cleared by the stated command.

Does anyone can explain that behavior?

Regards,
Lucas
0 Kudos
Reply
4 Replies

453 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by affonpign on Wed Oct 12 02:22:42 MST 2011
The code and the hardware work fine. I just did not understand the MCU library code.

The SI is cleared but just after clearing it is set again because of the slave acknowledgement. I understand now, I was just surprised that it happen so fast.

Thanks for your help.

Regards,
affonpign
0 Kudos
Reply

453 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ToBeFrank on Fri Oct 07 07:57:33 MST 2011

Quote: affonpign
The important question is, why does the SI remains set after clearing.



It doesn't.


Quote:
CONSET = 0x40



It is correctly cleared.

Your code then waits for it to be set again. After you send a byte, the slave should acknowledge it, which changes the I2C state and generates an interrupt. Are you sure you have the I2C initialized correctly and your hardware is ok?
0 Kudos
Reply

453 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by affonpign on Thu Oct 06 23:47:22 MST 2011
The important question is, why does the SI remains set after clearing. That makes no sense.

CONSET = 0x40
STAT = 0x18 or 0x28 depending if the slave address or a data byte was sent.

Regards,
affonpign
0 Kudos
Reply

453 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ToBeFrank on Thu Oct 06 12:21:30 MST 2011
For one thing, this code is not thread/interrupt safe, but that's likely not your problem. What are the values of STAT and CONSET after the line that clears SI?
0 Kudos
Reply