/*****************************************************************************
COPYRIGHT 2003 STMicroelectronics
Source File Name :  i2cs.c
Group            : IPSW,CMG
Author           : MCD Application Team
Date First Issued: 14/10/2003
********************************Documentation**********************************
General Purpose - Contains source code for all the functions of i2cs

********************************Revision History*******************************
_______________________________________________________________________________
Date :07/03/2002                 Release:1.0
******************************************************************************/

#include "ST7lib_config.h"                            /* Selection of device */
#include "i2cs_hr.h"                     /* Declaration of i2cs HW registers */
#include "i2cs.h"         /* Prototype definitions of i2cs library functions */

/*---------------------------------------------------------------------------*/
                                          /* Declaration of Global Variables */
#if (defined I2C_ITDRV_WITHOUTBUF_RX ||defined I2C_ITDRV_WITHOUTBUF_TX )
/* Global address variable for Interrupt Mode                                */
static unsigned char* I2Cs_GlobalBuff ;
    #endif

/* Transmitter buffer count                                                  */
static unsigned char I2Cs_IntrCounter;

/* Error variable for transmission and reception                             */
static unsigned char I2CST1;
static unsigned char I2CST2;

/* Stores the maximum size of the buffer                                     */
static unsigned char I2Cs_MaxSize;

/* Global variable for storing the communication mode                        */
static I2Cs_Mode I2Cs_Com_Mode = I2Cs_DEFAULT;

static BOOL I2Cs_StartStatus = FALSE;

static BOOL I2Cs_Reception = FALSE;

static BOOL I2Cs_Transmission = FALSE;

static BOOL FirstByte = FALSE;

/*-----------------------------------------------------------------------------
ROUTINE NAME : I2Cs_Init
INPUT 1      : I2Cs_DEFAULT_PARAM - Default value
               I2Cs_ENABLE_ACK - Enable the Ack bit
               I2Cs_ENABLE_ENGC - Enable the ENGC bit
INPUT 2      : I2Cs_OAR1Value - Lower address byte
INPUT 3      : I2Cs_OAR2Value - Higher address byte
OUTPUT       : None.
DESCRIPTION  : Initialize the control register and status register. Configure
               polarity,phase and baudrate of i2cs clock. Master/ slave in
               software/ hardware mode can be selected and Interrupt and i2cs
               can be enabled.
COMMENTS     : Must be called before starting any i2cs operation.
-----------------------------------------------------------------------------*/
void I2Cs_Init( I2Cs_Control_Param InitParam,unsigned char I2Cs_OAR1Value,
               unsigned char I2Cs_OAR2Value)
{
    /* Clear past request and clear all register                             */
    I2CDR = I2Cs_DEFAULT_PARAM;
    I2CCR = I2Cs_DEFAULT_PARAM;

    #if (defined I2C_72F63)
    I2COAR = I2Cs_OAR1Value;
    #endif

    #if (defined I2C_72F521 || defined I2C_72F264 || defined I2C_72F34x)
    I2COAR1 = I2Cs_OAR1Value;
    if ( Fcpu >= 6000000)
        I2COAR2 = 0x40;
    else
        I2COAR2 = 0x00;
    /* Higher byte and FRi bits                                              */
    I2COAR2 |= (unsigned char)(I2Cs_OAR2Value & 0x3F);
    #endif
    /* Enable peripheral                                                     */
    I2CCR |= I2Cs_PE;
      /* Sets the control bits depending on the settings                     */
    //I2CCR |= (unsigned char)( InitParam | I2Cs_PE );
    I2CCR |= (unsigned char)(((unsigned char)InitParam |(unsigned char)I2Cs_PE));
                                               //InitParam & I2Cs_Param typecasted
    I2Cs_MaxSize = 0;
    I2Cs_Com_Mode = I2Cs_DEFAULT;
    #if (defined I2C_ITDRV_WITHOUTBUF_RX ||defined I2C_ITDRV_WITHOUTBUF_TX )
    I2Cs_IntrCounter = 0;
    I2CST1 = 0;
    I2CST2 = 0;
    I2Cs_StartStatus = FALSE;
    /* Enable Interrupt for Interrupt mode I2C Slave                         */
    I2CCR |= I2Cs_ITE;
    #endif

}


/*-----------------------------------------------------------------------------
ROUTINE NAME : I2Cs_GetCommMode
INPUT        : None
OUTPUT       : I2Cs_Mode
DESCRIPTION  : Data transmission and reception takes place in interrupt
               subroutine for Interrupt driven without buffer mode.
COMMENTS     : None
-----------------------------------------------------------------------------*/
I2Cs_Mode I2Cs_GetCommMode(void)
{
    #if (defined I2C_POLLING_TX || defined I2C_POLLING_RX )
    unsigned char Temp, Temp2;
    #endif
    /* For Interrupt Mode                                                    */
    #if (defined I2C_ITDRV_WITHOUTBUF_TX || defined I2C_ITDRV_WITHOUTBUF_RX )
    if( I2Cs_StartStatus )
    {
        I2Cs_StartStatus = FALSE;
        I2CCR |= I2Cs_ITE;
    }
    else if ( I2CST2 & I2Cs_ERR )
    {
        I2CCR |= I2Cs_ITE;
    }
    return I2Cs_Com_Mode;

    #endif

    /* For Polling Mode                                                      */
    #if (defined I2C_POLLING_TX || defined I2C_POLLING_RX )
    Temp = I2CSR1;
    I2CST2 = I2CSR2;
    Temp2 = (unsigned char)(I2CST2 & I2Cs_ERR);
    //if ( (Temp & I2Cs_BTF) && !(Temp2 & I2CSR2))
    if ( (!(Temp2 & I2CSR2)) && (Temp & I2Cs_BTF))//newly added,previous stmt commented above
    {
        if( Temp & I2Cs_TRA)
        {
            /* Transmitter                                                */
            return I2Cs_TX_MODE;
        }
        else
        {
             /* Receicver                                                    */
            return I2Cs_RX_MODE;
        }
    }
    else
   {//add brace
        return I2Cs_DEFAULT;
   }
    #endif

}


/*-----------------------------------------------------------------------------
ROUTINE NAME : I2Cs_PutByte
INPUT        : None
OUTPUT       : Received data byte
DESCRIPTION  : Reads the data from i2csDR register and return the received
               data byte.
COMMENTS     : I2Cs_IsReceptionCompleted is called to check whether data byte is
               received.
-----------------------------------------------------------------------------*/
void I2Cs_PutByte(unsigned char Tx_Byte)
{
    I2CDR = Tx_Byte;
    /* For Interrupt Mode                                                    */
    I2Cs_MaxSize = 1;
    #ifdef I2C_ITDRV_WITHOUTBUF_TX
     /* No meaning in single byte case                                       */
    I2Cs_IntrCounter = 0;
     /* Enable i2c interrupt                                                 */
    I2CCR |= I2Cs_ITE;
    #endif
}



#ifdef I2C_POLLING_TX
/*-----------------------------------------------------------------------------
ROUTINE NAME : I2Cs_PutBuffer
INPUT 1      : *PtrToBuffer - Starting address of the user buffer.
INPUT 2      : NbOfBytes - Number of data bytes to be transmitted.
OUTPUT       : Transmission status
DESCRIPTION  : In polling mode, Transmits data bytes continuously from user
               buffer and returns the transmission status
COMMENTS     : None
-----------------------------------------------------------------------------*/
I2Cs_ErrCode_t I2Cs_PutBuffer(unsigned char *PtrToBuffer, unsigned char MaxSize)
{
    unsigned char Temp1, Temp2;
    unsigned char counter = 0;
    volatile unsigned char temp =0;

     /* Check whether BTF is set or not                                      */
    while (!(I2CSR1 & I2Cs_BTF));

    /* Write handling for  MaxSize = 0                                       */
    if ( MaxSize )
   {//add brace
        I2CDR = *(PtrToBuffer + counter);
   }
    else
   {//add brace
        I2CDR = 0xFF; /* Dummy Bytes   */
   }
    counter++;
    /* Communication routine                                                 */
    while(1)
    {
        Temp1 = I2CSR1;
        /* Wait for an event to occured                                      */
        while (!( Temp1 & I2Cs_EVF) )
        {
            Temp1 = I2CSR1;
        }
        /* Check for errors                                                  */
        Temp2 = I2CSR2;
        if( Temp2 & I2Cs_ERR)
        {
            if ( Temp2 & I2Cs_BRR)
            {
                return I2Cs_BERR;
            }
            else if (Temp2 & I2Cs_AF)
            {
                while(!(I2CSR1 & I2Cs_BTF));
                /* Release the SCL and SDA lines                             */
                I2CDR = 0xFF;
            }
            else if ( Temp2 & I2Cs_STOPF )
            {
                if ( counter <= MaxSize)
          {//add brace
                    return I2Cs_TX_DATA_OK;
          }
                else
          {//add brace
                    return I2Cs_OVERFLOW_TX;
          }
            }
        }
        else
        {
            if ( Temp1 & I2Cs_ADSL )
            {   /* Start condition need to check whether Tx or Rx.           */
                if ( Temp2 & I2Cs_GCAL )
          {//add brace
                    return I2Cs_GENERAL_CALL;
          }
                else
          {//add brace
                    return I2Cs_ADDRESS_DETECTED;
          }
            }
            else if ((Temp1 & I2Cs_BTF) && ( Temp1 & I2Cs_TRA))
            {
                if ( counter < MaxSize)
          {//add brace
                    I2CDR = *(PtrToBuffer + counter);
          }
                else
          {//add brace
                    I2CDR = 0xFF; /* Dummy bytes due to Overflow comm        */
          }
                counter++;
            }

        }  /* End of  'Temp2' if loop                                        */

    }  /* End of while loop                                                  */

} /* End of the function                                                     */
#endif


#ifdef I2C_ITDRV_WITHOUTBUF_TX
/*-----------------------------------------------------------------------------
ROUTINE NAME : I2Cs_PutBuffer
INPUT 1      : *PtrToBuffer - Starting address of the user buffer
INPUT 2      : NbOfBytes - Number of data bytes to be transmitted
OUTPUT       : None
DESCRIPTION  : In interrupt driven mode, starts continuous data bytes
               transmission in ISR
COMMENTS     : None
-----------------------------------------------------------------------------*/
void  I2Cs_PutBuffer(unsigned char *PtrToBuffer,unsigned char MaxSize )
{
    I2Cs_IntrCounter = 0;
    I2Cs_GlobalBuff = PtrToBuffer;
    I2Cs_MaxSize = MaxSize;

    if (MaxSize)
    {   // add braces
        I2CDR =*(I2Cs_GlobalBuff + I2Cs_IntrCounter ); /*Transmit first byte */
    }
    else
    {   // add braces
        I2CDR = 0xFF; /* Dummy Bytes                                         */
    }
    I2Cs_IntrCounter++;
    /* Enable the interrupt                                                  */
    I2CCR |= I2Cs_ITE;
}
#endif


/*-----------------------------------------------------------------------------
ROUTINE NAME : I2Cs_IsTransmitCompleted
INPUT        : None
OUTPUT       : Returns the transmission status
DESCRIPTION  : Returns the transmission status and checks the pending request.
COMMENTS     : Must be called after transmission function in Interrupt driven
               mode.
               In polling mode this function is called only after I2Cs_PutByte.
-----------------------------------------------------------------------------*/
I2Cs_ErrCode_t I2Cs_IsTransmitCompleted(void)
{
    unsigned char Temp1, Temp2;

    /* If no data has been transmitted                                       */
    Temp1 = I2CSR1;
    Temp2 =(unsigned char)(I2CSR2 | I2CST2);

    /* Here are start and stop condition are checked to make sure they are
       checked even if no bytes are transfered                               */

    /* To detect the error                                                   */
    if ( Temp2 & I2Cs_ERR )
    {
        /* Clear the error bits to protect any false detection               */
        I2CST2 = 0;

        if ( Temp2 & I2Cs_BRR )
        {
            #if (defined I2C_ITDRV_WITHOUTBUF_RX ||defined I2C_ITDRV_WITHOUTBUF_TX )
            I2CCR |= I2Cs_ITE;
            #endif
            return I2Cs_BERR;
        }
        else if ( Temp2 & I2Cs_STOPF )
        {
            #if (defined I2C_ITDRV_WITHOUTBUF_RX ||defined I2C_ITDRV_WITHOUTBUF_TX )
            I2CCR |= I2Cs_ITE;
            #endif
            if( I2Cs_IntrCounter == 0)
      {//add brace
                return I2Cs_STOP_DETECTED;   /* Single Byte  or no byte      */
      }
            else
            {
                if ( I2Cs_IntrCounter <= I2Cs_MaxSize )
          {//add brace
                    return I2Cs_TX_DATA_OK;    /* Buffer Tx                  */
          }
                else
          {//add brace
                    return I2Cs_OVERFLOW_TX;
          }
            }
        }
        else if( Temp2 & I2Cs_AF) /* The Acknowledge failure error           */
        {
            I2CDR = 0xFF;
            #ifdef I2C_ITDRV_WITHOUTBUF_TX
            I2CCR |= I2Cs_ITE;
            #endif
            return I2Cs_TX_AF;

        }
        else
        {
            #if (defined I2C_ITDRV_WITHOUTBUF_RX ||defined I2C_ITDRV_WITHOUTBUF_TX )
            I2CCR |= I2Cs_ITE;
            #endif
            return I2Cs_DEFAULT_STATUS;
        }
    }
    #if (defined I2C_POLLING_RX ||defined I2C_POLLING_TX )
    /* Here both macro is enabled as Start condition is independent of Mod   */
    else if ((Temp1 &  I2Cs_ADSL) && (!(Temp2 & I2Cs_BRR)) )//right side operand put in parantheses
    {
        I2Cs_IntrCounter = 0;
        I2Cs_MaxSize = 0;
        if ( Temp2 & I2Cs_GCAL )
       {//add brace
            return I2Cs_GENERAL_CALL;
       }
        else
       {//add brace
            return I2Cs_ADDRESS_DETECTED;
       }
    }
    #endif
    #if (defined I2C_ITDRV_WITHOUTBUF_RX ||defined I2C_ITDRV_WITHOUTBUF_TX )
    else if ( I2Cs_StartStatus )
    {
        I2Cs_StartStatus = FALSE;
        I2Cs_IntrCounter = I2Cs_MaxSize = 0;
        I2CCR |= I2Cs_ITE;
        if ( Temp2 & I2Cs_GCAL )
        {   //add brace
            return I2Cs_GENERAL_CALL;
        }
        else
        {   //add brace
            return I2Cs_ADDRESS_DETECTED;
        }
    }
    #endif
    #ifdef I2C_POLLING_TX
    /* Only for I2Cs_PutByte and detecting start, stop condition             */
    else if ((Temp1 & I2Cs_BTF) && (Temp1 & I2Cs_TRA))
   {//add brace
        return I2Cs_TX_DATA_OK;
   }

    #endif
    #ifdef I2C_ITDRV_WITHOUTBUF_TX
    else if ( I2Cs_Transmission)
    {
        I2Cs_Transmission = FALSE;
        /*Single Byte  Transmission                                          */
        if(( I2Cs_MaxSize == 1 ) && ( I2Cs_IntrCounter == 0) )
        {   //add brace
            return I2Cs_TX_DATA_OK;
        }
        else /* Buffer Transmission                                          */
        {   //add brace
            return I2Cs_BUFF_TX_ONGOING;
        }
    }
    #endif
    else
   {//add brace
        return I2Cs_DEFAULT_STATUS;
   }

}


/*-----------------------------------------------------------------------------
ROUTINE NAME : I2Cs_GetByte
INPUT        : None
OUTPUT       : Received data byte
DESCRIPTION  : Reads the data from i2csDR register and return the received
               data byte.
COMMENTS     : I2Cs_IsReceptionCompleted is called to check whether data byte
               is received.
-----------------------------------------------------------------------------*/
unsigned char I2Cs_GetByte(void)
{
    unsigned char Temp;
    I2Cs_MaxSize = 1;
    #ifdef I2C_ITDRV_WITHOUTBUF_RX
    I2Cs_IntrCounter = 0;
    FirstByte = FALSE;
    Temp =  I2CDR;
    I2CCR |=I2Cs_ITE;
    return Temp;
    #endif

    #ifdef I2C_POLLING_RX
    Temp =  I2CDR;
    return Temp;
    #endif
}


#ifdef I2C_POLLING_RX
/*-----------------------------------------------------------------------------
ROUTINE NAME : I2Cs_GetBuffer
INPUT 1      : *PtrToBuffer - Starting address of the user buffer
INPUT 2      : NbOfBytes - Number of data bytes to be received
OUTPUT       : Returns the reception status
DESCRIPTION  : In polling mode, Receives data bytes continuously and store them
               in user buffer. Also returns the reception status.
COMMENTS     : None
-----------------------------------------------------------------------------*/
I2Cs_ErrCode_t I2Cs_GetBuffer(unsigned char *PtrToBuffer, unsigned
char MaxSize )
{
    unsigned char Temp, Temp1, Temp2, counter = 0;
     /* Check whether BTF is set or not                                      */
    while (!(I2CSR1 & I2Cs_BTF));
    if (!MaxSize )
   {//add brace
        Temp = I2CDR; /* temporary register                                  */
   }
    else
   {//add brace
        *(PtrToBuffer + counter) = I2CDR;
   }


    counter++;

    while(1)
    {
        /* Wait for an event to occured                                      */
        Temp1 = I2CSR1;
        while (!( Temp1 & I2Cs_EVF) )
        {
            Temp1 = I2CSR1;
        }

        Temp2 = I2CSR2;
        /* check for the error                                               */
        if ( Temp2 &  I2Cs_ERR )
        {
            if ( Temp2 & I2Cs_BRR)
      {//add brace
                return I2Cs_BERR;
      }
            else if ( Temp2 & I2Cs_STOPF )
            {
                if ( counter <= MaxSize )
          {//add brace
                    return I2Cs_RX_DATA_OK;
          }
                else
          {//add brace
                    return I2Cs_OVERFLOW_RX;
          }
            }
        }
        else if ( Temp1 & I2Cs_ADSL )
        {
            /* Check for start or repeated start condtion                    */
            if ( Temp2 & I2Cs_GCAL )
      {//add brace
                return I2Cs_GENERAL_CALL;
      }
            else
      {//add brace
                return I2Cs_ADDRESS_DETECTED;
      }
        }
        else if ( (Temp1 & I2Cs_BTF) && (!( Temp1 & I2Cs_TRA)) )//right side operand put in parantheses
        {
            if ( counter < MaxSize)
      {//add brace
                *(PtrToBuffer + counter)  = I2CDR;
      }
            else
      {//add brace
                Temp = I2CDR;
      }
            counter++;
        }
    } /* End while                                                           */

}
#endif


#ifdef I2C_ITDRV_WITHOUTBUF_RX
/*-----------------------------------------------------------------------------
ROUTINE NAME : I2Cs_GetBuffer
INPUT 1      : *PtrToBuffer - Starting address of the user buffer
INPUT 2      : NbOfBytes - Number of data bytes to be received
OUTPUT       : None
DESCRIPTION  : In interrupt driven mode, starts data bytes reception in ISR
COMMENTS     : None
-----------------------------------------------------------------------------*/
void I2Cs_GetBuffer(unsigned char *PtrToBuffer,unsigned char MaxSize )
{
    unsigned char Temp;
    I2Cs_IntrCounter = 0;

    I2Cs_GlobalBuff = PtrToBuffer;

    I2Cs_MaxSize =  MaxSize;

    if(FirstByte)
    {
        FirstByte = FALSE;
        if (!MaxSize)
        {   //add brace
            Temp = I2CDR; /* temporary register to neglect the byte          */
        }
        else
        {   //add brace
            *(I2Cs_GlobalBuff + I2Cs_IntrCounter ) = I2CDR;
        }
        I2Cs_IntrCounter++;
    }
    /* Enable the interrupt                                                  */
    I2CCR |= I2Cs_ITE;

}
#endif


/*-----------------------------------------------------------------------------
ROUTINE NAME : I2Cs_IsReceptionCompleted
INPUT        : None
OUTPUT       : Returns the reception status
DESCRIPTION  : Returns the reception status and checks the pending request.
COMMENTS     : Must be called after reception function in Interrupt driven
               mode.In polling mode this function is called only after
               I2Cs_GetByte.
-----------------------------------------------------------------------------*/
I2Cs_ErrCode_t I2Cs_IsReceptionCompleted(void)
{
    unsigned char Temp1, Temp2;
    Temp1 = I2CSR1;
    Temp2 =(unsigned char)(I2CSR2 | I2CST2);
    /* Check error condition                                                 */
    if ( Temp2 & I2Cs_ERR)
    {
        /* Clear the error bits to protect any false detection               */
        I2CST2 = 0;
        if (Temp2 & I2Cs_BRR)
        {
            #if (defined I2C_ITDRV_WITHOUTBUF_RX ||defined I2C_ITDRV_WITHOUTBUF_TX )
            I2CCR |= I2Cs_ITE;
            #endif
            return I2Cs_BERR;
        }
        else if ( Temp2 & I2Cs_STOPF )
        {
            I2Cs_Reception = FALSE;
            #if (defined I2C_ITDRV_WITHOUTBUF_RX ||defined I2C_ITDRV_WITHOUTBUF_TX )
            I2CCR |= I2Cs_ITE;
            #endif
            if( I2Cs_IntrCounter == 0 )
       {//added braces
                return I2Cs_STOP_DETECTED;   /* Single Byte or no byte       */
       }
            else
            {
                if ( I2Cs_IntrCounter <= I2Cs_MaxSize )
      {//added braces
                    return I2Cs_RX_DATA_OK;    /* Buffer Tx                  */
      }
                else
      {//added braces
                    return I2Cs_OVERFLOW_RX;
      }
            }
        }
        else
        {
            #if (defined I2C_ITDRV_WITHOUTBUF_RX ||defined I2C_ITDRV_WITHOUTBUF_TX )
            I2CCR |= I2Cs_ITE;
            #endif
            return I2Cs_DEFAULT_STATUS;
        }
    }
    #if (defined I2C_POLLING_RX ||defined I2C_POLLING_TX )
    /* Here both macro is enabled as Start condition is independent of Mod   */
    //else if ((Temp1 &  I2Cs_ADSL) && !(Temp2 & I2Cs_BRR) )
    else if ((Temp1 &  I2Cs_ADSL) && (!(Temp2 & I2Cs_BRR)) )
    {
        I2Cs_IntrCounter = I2Cs_MaxSize = 0;
        if ( Temp2 & I2Cs_GCAL )
   {//added braces
            return I2Cs_GENERAL_CALL;
   }
        else
   {//added braces
            return I2Cs_ADDRESS_DETECTED;
   }
    }
    #endif

    #if (defined I2C_ITDRV_WITHOUTBUF_RX ||defined I2C_ITDRV_WITHOUTBUF_TX )
    else if ( I2Cs_StartStatus )
    {
        I2Cs_StartStatus = FALSE;
        I2Cs_IntrCounter = I2Cs_MaxSize = 0;
        I2CCR |= I2Cs_ITE;
        if ( Temp2 & I2Cs_GCAL )
        {   //add brace
            return I2Cs_GENERAL_CALL;
        }
        else
        {   //add brace
            return I2Cs_ADDRESS_DETECTED;
        }
    }
    #endif

    #ifdef I2C_POLLING_RX
    /* Only for I2Cs_PutByte and detecting start, stop condition             */
    else if ((Temp1 & I2Cs_BTF) && (!(Temp1 & I2Cs_TRA)))//right operand put in parantheses
   {//added braces
        return I2Cs_RX_DATA_OK;
   }
    #endif

    #ifdef I2C_ITDRV_WITHOUTBUF_RX
    else if ( I2Cs_Reception )
    {
        if(!FirstByte)
        {   //add brace
            I2Cs_Reception = FALSE;
        }
        if( FirstByte == TRUE )
        {   //add brace
            return I2Cs_RX_DATA_OK;
        }
         /* Check whether Single Byte or Buffer reception                    */
        if((I2Cs_MaxSize == 1 ) && ( I2Cs_IntrCounter == 0 ))
        {   //add brace
            return I2Cs_RX_DATA_OK;
        }
        else
        {   //add brace
            return I2Cs_BUFF_RX_ONGOING;
        }

    }
    #endif
    else if (( I2Cs_IntrCounter == 0) && ( I2Cs_MaxSize == 0 ))
   {//added braces
        return I2Cs_EMPTY;
   }
    else
   {//added braces
        return I2Cs_DEFAULT_STATUS;
   }

}


/*-----------------------------------------------------------------------------
ROUTINE NAME : I2Cs_ErrorClear
INPUT        : None
OUTPUT       : None
DESCRIPTION  : Data transmission and reception takes place in interrupt
               subroutine for Interrupt driven without buffer mode.
COMMENTS     : None
-----------------------------------------------------------------------------*/
void I2Cs_ErrorClear(void )
{
    unsigned char Temp;
    Temp = I2CSR1;
    Temp = I2CSR2;
    I2CST2 = I2CST1 = 0;
}


/*-----------------------------------------------------------------------------
ROUTINE NAME : I2Cs_ITFunction
INPUT        : None
OUTPUT       : None
DESCRIPTION  : Data transmission and reception takes place in interrupt
               subroutine for Interrupt driven without buffer mode.
COMMENTS     : None
-----------------------------------------------------------------------------*/
void I2Cs_ITFunction(void)
{
    #if (defined I2C_ITDRV_WITHOUTBUF_RX ||defined I2C_ITDRV_WITHOUTBUF_TX )

    unsigned char Temp;
    I2CST1 = I2CSR1;
    /* Copy the error variable                                               */
    I2CST2 = I2CSR2;
    /* Below will be the code for communication and also the control for this
       has been given                                                        */

    /* Check whether it should be done in ISR or in main                     */
    if ( I2CST2 & I2Cs_ERR)
    {
        if((I2CST2 & I2Cs_STOPF) || (I2CST2 & I2Cs_BRR))
        {   //add brace
            I2Cs_Com_Mode = I2Cs_DEFAULT;
        }
        I2CCR &= (unsigned char)(~(unsigned char)(I2Cs_ITE));/* Disable interrupt             */
    }
    else if ( I2CST1 & I2Cs_ADSL ) /* EV1 address detected                   */
    {
        I2CCR &= (unsigned char)(~(unsigned char)(I2Cs_ITE));/* Disable interrupt             */
        I2Cs_Com_Mode = I2Cs_DEFAULT;
        I2Cs_StartStatus = TRUE;

    }
    else if ((I2CST1 & I2Cs_BTF) && (!( I2CST2 & I2Cs_AF )))
    {
        if ( I2Cs_Com_Mode == I2Cs_DEFAULT )
        {
            if ( I2CST1 & I2Cs_TRA )
            {
                /* Slave is in transmitter mode                              */
                I2Cs_Com_Mode = I2Cs_TX_MODE;
                /* Disable the interrupt                                     */
                I2CCR &= (unsigned char)(~(unsigned char)(I2Cs_ITE));

            }
            else
            {   /* Slave is in receiver mode                                 */
                I2Cs_Com_Mode = I2Cs_RX_MODE;
                FirstByte = TRUE;
                I2Cs_Reception = TRUE;
                I2CCR &= (unsigned char)(~(unsigned char)(I2Cs_ITE));
                /* Disable the interrupt                                     */
            }
        }
        #ifdef I2C_ITDRV_WITHOUTBUF_TX
        else if ( I2Cs_Com_Mode == I2Cs_TX_MODE)
        {
            /*  Confirming for right communication mode                      */
            if ( I2CST1 & I2Cs_TRA )
            {
                I2Cs_Transmission = TRUE;
                /* Check whether Single byte or Buffer transmission          */
                if ((I2Cs_MaxSize == 1) && (I2Cs_IntrCounter == 0))
                {   //add brace
                    I2CCR &= (unsigned char )(~(unsigned char)(I2Cs_ITE));
                }
                else
                {
                    if ( I2Cs_IntrCounter < I2Cs_MaxSize)
                    {   //add brace
                        I2CDR = *(I2Cs_GlobalBuff + I2Cs_IntrCounter );
                    }
                    else
                    {   //add brace
                        I2CDR = 0xFF; /* Dummy byte                          */
                    }
                    I2Cs_IntrCounter++;
                }
            }

        }
        #endif

        #ifdef I2C_ITDRV_WITHOUTBUF_RX
        else if ( I2Cs_Com_Mode == I2Cs_RX_MODE )
        {
            /*  Confirming for right communication mode                      */
            if (!(I2CST1 & I2Cs_TRA))
            {
                I2Cs_Reception = TRUE;
                /* Check whether Single byte or Buffer reception             */
                if ((I2Cs_MaxSize == 1) && (I2Cs_IntrCounter == 0))
                {   //add brace
                    I2CCR &= (unsigned char)(~(unsigned char)(I2Cs_ITE));
                }
                else
                {
                    if ( I2Cs_IntrCounter < I2Cs_MaxSize)
                    {   //add brace
                        *(I2Cs_GlobalBuff + I2Cs_IntrCounter ) = I2CDR;
                    }
                    else
                    {   //add brace
                        Temp = I2CDR; /* Dummy byte                          */
                    }
                    I2Cs_IntrCounter++;
                }
            }
        }  /* End 'I2Cs_RX_MODE' if loop                                     */
        #endif
    }  /* End 'BTF' if loop                                                  */
    #endif
} /* End 'Interrupt'  loop                                                   */


