Content originally posted in LPCWare by jherrera8 on Fri Oct 07 18:31:10 MST 2011
Thanks for the reply, I will definitely look at that. Also, I have an idea how it works, I'm just a bit uneasy about the code. Could somebody explain a bit more about what is going on in this example code. Thanks guys,
void I2C0_IRQHandler(void)
{
uint8_t StatValue;
timeout[0] = 0;
/* this handler deals with master read and master write only */
StatValue = LPC_I2C0->STAT;
switch ( StatValue )
{
case 0x08:/* A Start condition is issued. */
WrIndex0 = 0;
LPC_I2C0->DAT = I2CMasterBuffer[0][WrIndex0++];
LPC_I2C0->CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
break;
case 0x10:/* A repeated started is issued */
RdIndex0 = 0;
/* Send SLA with R bit set, */
LPC_I2C0->DAT = I2CMasterBuffer[0][WrIndex0++];
LPC_I2C0->CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
break;
case 0x18:/* Regardless, it's a ACK */
if ( I2CWriteLength[0] == 1 )
{
LPC_I2C0->CONSET = I2CONSET_STO; /* Set Stop flag */
I2CMasterState[0] = I2C_NO_DATA;
}
else
{
LPC_I2C0->DAT = I2CMasterBuffer[0][WrIndex0++];
}
LPC_I2C0->CONCLR = I2CONCLR_SIC;
break;
case 0x28:/* Data byte has been transmitted, regardless ACK or NACK */
if ( WrIndex0 < I2CWriteLength[0] )
{
LPC_I2C0->DAT = I2CMasterBuffer[0][WrIndex0++]; /* this should be the last one */
}
else
{
if ( I2CReadLength[0] != 0 )
{
LPC_I2C0->CONSET = I2CONSET_STA;/* Set Repeated-start flag */
}
else
{
LPC_I2C0->CONSET = I2CONSET_STO; /* Set Stop flag */
I2CMasterState[0] = I2C_OK;
}
}
LPC_I2C0->CONCLR = I2CONCLR_SIC;
break;
case 0x30:
LPC_I2C0->CONSET = I2CONSET_STO; /* Set Stop flag */
I2CMasterState[0] = I2C_NACK_ON_DATA;
LPC_I2C0->CONCLR = I2CONCLR_SIC;
break;
case 0x40:/* Master Receive, SLA_R has been sent */
if ( (RdIndex0 + 1) < I2CReadLength[0] )
{
/* Will go to State 0x50 */
LPC_I2C0->CONSET = I2CONSET_AA;/* assert ACK after data is received */
}
else
{
/* Will go to State 0x58 */
LPC_I2C0->CONCLR = I2CONCLR_AAC;/* assert NACK after data is received */
}
LPC_I2C0->CONCLR = I2CONCLR_SIC;
break;
case 0x50:/* Data byte has been received, regardless following ACK or NACK */
I2CSlaveBuffer[0][RdIndex0++] = LPC_I2C0->DAT;
if ( (RdIndex0 + 1) < I2CReadLength[0] )
{
LPC_I2C0->CONSET = I2CONSET_AA;/* assert ACK after data is received */
}
else
{
LPC_I2C0->CONCLR = I2CONCLR_AAC;/* assert NACK on last byte */
}
LPC_I2C0->CONCLR = I2CONCLR_SIC;
break;
case 0x58:
I2CSlaveBuffer[0][RdIndex0++] = LPC_I2C0->DAT;
I2CMasterState[0] = I2C_OK;
LPC_I2C0->CONSET = I2CONSET_STO;/* Set Stop flag */
LPC_I2C0->CONCLR = I2CONCLR_SIC;/* Clear SI flag */
break;
case 0x20:/* regardless, it's a NACK */
case 0x48:
LPC_I2C0->CONSET = I2CONSET_STO; /* Set Stop flag */
I2CMasterState[0] = I2C_NACK_ON_ADDRESS;
LPC_I2C0->CONCLR = I2CONCLR_SIC;
break;
case 0x38:/* Arbitration lost, in this example, we don't
deal with multiple master situation */
default:
I2CMasterState[0] = I2C_ARBITRATION_LOST;
LPC_I2C0->CONCLR = I2CONCLR_SIC;
break;
}
return;
}