keil_examples/I2C/i2c.c
/*****************************************************************************
** Function name:		I2C_IRQHandler
**
** Descriptions:		I2C interrupt handler, deal with master mode only.
**
** parameters:			None
** Returned value:		None
** 
*****************************************************************************/
Function name:         I2C_IRQHandler
Descriptions:          I2C interrupt handler, deal with master mode only.

None
Returned value:                None
Function name : I2C_IRQHandler Descriptions : I2C interrupt handler deal with master mode only . None Returned value : None
void I2C1_IRQHandler(void) 
{
  uint8_t StatValue;
  timeout[1] = 0;
  StatValue = LPC_I2C1->STAT;
  switch ( StatValue )
  {
	case 0x08:			
	WrIndex1 = 0;
	LPC_I2C1->DAT = I2CMasterBuffer[1][WrIndex1++];
	LPC_I2C1->CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
	break;
	case 0x10:			
	RdIndex1 = 0;
	LPC_I2C1->DAT = I2CMasterBuffer[1][WrIndex1++];
	LPC_I2C1->CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
	break;
	case 0x18:			
	if ( I2CWriteLength[1] == 1 )
	{
	  LPC_I2C1->CONSET = I2CONSET_STO;      
	  I2CMasterState[1] = I2C_NO_DATA;
	}
	else
	{
	  LPC_I2C1->DAT = I2CMasterBuffer[1][WrIndex1++];
	}
	LPC_I2C1->CONCLR = I2CONCLR_SIC;
	break;
	case 0x28:	
	if ( WrIndex1 < I2CWriteLength[1] )
	{   
	  LPC_I2C1->DAT = I2CMasterBuffer[1][WrIndex1++]; 
	}
	else
	{
	  if ( I2CReadLength[1] != 0 )
	  {
		LPC_I2C1->CONSET = I2CONSET_STA;	
	  }
	  else
	  {
		LPC_I2C1->CONSET = I2CONSET_STO;      
		I2CMasterState[1] = I2C_OK;
	  }
	}
	LPC_I2C1->CONCLR = I2CONCLR_SIC;
	break;
	case 0x30:
	LPC_I2C1->CONSET = I2CONSET_STO;      
	I2CMasterState[1] = I2C_NACK_ON_DATA;
	LPC_I2C1->CONCLR = I2CONCLR_SIC;
	break;
	case 0x40:	
	if ( (RdIndex1 + 1) < I2CReadLength[1] )
	{
	  LPC_I2C1->CONSET = I2CONSET_AA;	
	}
	else
	{
	  LPC_I2C1->CONCLR = I2CONCLR_AAC;	
	}
	LPC_I2C1->CONCLR = I2CONCLR_SIC;
	break;
	case 0x50:	
	I2CSlaveBuffer[1][RdIndex1++] = LPC_I2C1->DAT;
	if ( (RdIndex1 + 1) < I2CReadLength[1] )
	{   
	  LPC_I2C1->CONSET = I2CONSET_AA;	
	}
	else
	{
	  LPC_I2C1->CONCLR = I2CONCLR_AAC;	
	}
	LPC_I2C1->CONCLR = I2CONCLR_SIC;
	break;
	case 0x58:
	I2CSlaveBuffer[1][RdIndex1++] = LPC_I2C1->DAT;
	I2CMasterState[1] = I2C_OK;
	LPC_I2C1->CONSET = I2CONSET_STO;	 
	LPC_I2C1->CONCLR = I2CONCLR_SIC;	
	break;
	case 0x20:		
	case 0x48:
	LPC_I2C1->CONSET = I2CONSET_STO;      
	I2CMasterState[1] = I2C_NACK_ON_ADDRESS;
	LPC_I2C1->CONCLR = I2CONCLR_SIC;
	break;
	case 0x38:		
	default:
	I2CMasterState[1] = I2C_ARBITRATION_LOST;
	LPC_I2C1->CONCLR = I2CONCLR_SIC;	
	break;
  }
  return;
}
void I2C1_IRQHandler ( void ) { uint8_t StatValue ; timeout [ 1 ] = 0 ; StatValue = LPC_I2C1 -> STAT ; switch ( StatValue ) { case 0x08 : WrIndex1 = 0 ; LPC_I2C1 -> DAT = I2CMasterBuffer [ 1 ] [ WrIndex1 ++ ] ; LPC_I2C1 -> CONCLR = ( I2CONCLR_SIC | I2CONCLR_STAC ) ; break ; case 0x10 : RdIndex1 = 0 ; LPC_I2C1 -> DAT = I2CMasterBuffer [ 1 ] [ WrIndex1 ++ ] ; LPC_I2C1 -> CONCLR = ( I2CONCLR_SIC | I2CONCLR_STAC ) ; break ; case 0x18 : if ( I2CWriteLength [ 1 ] == 1 ) { LPC_I2C1 -> CONSET = I2CONSET_STO ; I2CMasterState [ 1 ] = I2C_NO_DATA ; } else { LPC_I2C1 -> DAT = I2CMasterBuffer [ 1 ] [ WrIndex1 ++ ] ; } LPC_I2C1 -> CONCLR = I2CONCLR_SIC ; break ; case 0x28 : if ( WrIndex1 < I2CWriteLength [ 1 ] ) { LPC_I2C1 -> DAT = I2CMasterBuffer [ 1 ] [ WrIndex1 ++ ] ; } else { if ( I2CReadLength [ 1 ] != 0 ) { LPC_I2C1 -> CONSET = I2CONSET_STA ; } else { LPC_I2C1 -> CONSET = I2CONSET_STO ; I2CMasterState [ 1 ] = I2C_OK ; } } LPC_I2C1 -> CONCLR = I2CONCLR_SIC ; break ; case 0x30 : LPC_I2C1 -> CONSET = I2CONSET_STO ; I2CMasterState [ 1 ] = I2C_NACK_ON_DATA ; LPC_I2C1 -> CONCLR = I2CONCLR_SIC ; break ; case 0x40 : if ( ( RdIndex1 + 1 ) < I2CReadLength [ 1 ] ) { LPC_I2C1 -> CONSET = I2CONSET_AA ; } else { LPC_I2C1 -> CONCLR = I2CONCLR_AAC ; } LPC_I2C1 -> CONCLR = I2CONCLR_SIC ; break ; case 0x50 : I2CSlaveBuffer [ 1 ] [ RdIndex1 ++ ] = LPC_I2C1 -> DAT ; if ( ( RdIndex1 + 1 ) < I2CReadLength [ 1 ] ) { LPC_I2C1 -> CONSET = I2CONSET_AA ; } else { LPC_I2C1 -> CONCLR = I2CONCLR_AAC ; } LPC_I2C1 -> CONCLR = I2CONCLR_SIC ; break ; case 0x58 : I2CSlaveBuffer [ 1 ] [ RdIndex1 ++ ] = LPC_I2C1 -> DAT ; I2CMasterState [ 1 ] = I2C_OK ; LPC_I2C1 -> CONSET = I2CONSET_STO ; LPC_I2C1 -> CONCLR = I2CONCLR_SIC ; break ; case 0x20 : case 0x48 : LPC_I2C1 -> CONSET = I2CONSET_STO ; I2CMasterState [ 1 ] = I2C_NACK_ON_ADDRESS ; LPC_I2C1 -> CONCLR = I2CONCLR_SIC ; break ; case 0x38 : default : I2CMasterState [ 1 ] = I2C_ARBITRATION_LOST ; LPC_I2C1 -> CONCLR = I2CONCLR_SIC ; break ; } return ; }
Function name:         I2C_IRQHandler
Descriptions:          I2C interrupt handler, deal with master mode only.
Function name : I2C_IRQHandler Descriptions : I2C interrupt handler deal with master mode only .
/* this handler deals with master read and master write only */ /* A Start condition is issued. */ /* A repeated started is issued */ /* Send SLA with R bit set, */ /* Regardless, it's a ACK */ /* Set Stop flag */ /* Data byte has been transmitted, regardless ACK or NACK */ /* this should be the last one */ /* Set Repeated-start flag */ /* Set Stop flag */ /* Set Stop flag */ /* Master Receive, SLA_R has been sent */ /* Will go to State 0x50 */ /* assert ACK after data is received */ /* Will go to State 0x58 */ /* assert NACK after data is received */ /* Data byte has been received, regardless following ACK or NACK */ /* assert ACK after data is received */ /* assert NACK on last byte */ /* Set Stop flag */ /* Clear SI flag */ /* regardless, it's a NACK */ /* Set Stop flag */ /* Arbitration lost, in this example, we don't
					deal with multiple master situation */