/******************************************************************************
COPYRIGHT 2002 STMicroelectronics
Source File Name : sci.c 
Group            : IPSW,CMG-IPDF
Author           : MCD Application Team
Date First Issued: 5/3/2002
********************************Documentation**********************************
General Purpose - Contains source code for all the functions of SCI

********************************Revision History*******************************
_______________________________________________________________________________
Date :5/3/2002            Release:1.0
Date :16/12/2003        Release:2.0
                        1. Removed register definitions from sci_hr.h
                        2. Added support for ST72561 device         
Date :28/04/2004        MISRA changes  
******************************************************************************/

#include "ST7lib_Config.h"           /*Selection of device,compiler and modes*/
#include "Sci_hr.h"                        /*SCI HW registers Bit Definitions*/
#include "Sci.h"                                   /*Declaration of functions*/

unsigned char Pr_Value[4] = { 1, 3, 4,13 };
                                            /*Declaration of Global Variables*/
static volatile unsigned char SCI_Error;   

#if defined SCI_ITDRV_WITHOUTBUF_RX  
static volatile unsigned char SCI_MY_DATA;
static volatile unsigned char *PtrToSCIBuffRx;
#endif
#ifdef SCI_ITDRV_WITHOUTBUF_TX 
static const volatile unsigned char *PtrToSCIBuffTx; 
#endif

static volatile unsigned char SCI_Tx_Buff;
static volatile unsigned char SCI_Tx_String;
static volatile unsigned int SCI_Rx_Buff;
static volatile unsigned char SCI_Rx_String;
static volatile unsigned char SCI_Pol_Stat;  

#ifdef SCI_72F561                           
                                            /*Declaration of Global Variables*/
static volatile unsigned char SCI2_Error;   
static volatile unsigned char SCI2_MY_DATA;
static const volatile unsigned char *PtrToSCI2BuffTx; 
static volatile unsigned char *PtrToSCI2BuffRx;
static volatile unsigned char SCI2_Tx_Buff;
static volatile unsigned char SCI2_Tx_String;
static volatile unsigned int SCI2_Rx_Buff;
static volatile unsigned char SCI2_Rx_String;
static volatile unsigned char SCI2_Pol_Stat;
#endif

/*-----------------------------------------------------------------------------
ROUTINE NAME : SCI_Init
INPUT        : 1.SCI_DEFAULT_PARAM1, SCI_WORDLENGTH_9,SCI_ODPARITY_SELECT,
                 SCI_EVPARITY_SELECT or SCI_WAKEUP_ADDR
               2.SCI_MUTE_ENABLE,SCI_BREAK_ENABLE,SCI_DEFAULT_PARAM2
OUTPUT       : None              
DESCRIPTION  : Initializes the SCI in the required mode
COMMENTS     : Must be called before starting any sci operation
-----------------------------------------------------------------------------*/
void SCI_Init ( SCI_Type_Param1  Init_Value1, SCI_Type_Param2  Init_Value2)
{   
    unsigned char temp;    
    
    SCICR1 = SCI_DEFAULT_PARAM1;                         /*Load Default value*/
    SCICR2 = SCI_DEFAULT_PARAM2;
    temp = SCISR;                    /*Clear all the flags in Status register*/
    temp = SCIDR;
    SCIDR = SCI_DEFAULT_PARAM1;
    SCICR1 |= (unsigned char)Init_Value1;/*Sets SCI in the required mode*/
    SCICR2 |= (unsigned char)Init_Value2;
    SCI_Pol_Stat = SCI_Error = 0;              /*Initializes global variables*/
    SCI_Tx_Buff = 0;
    SCI_Tx_String = 0;
    SCI_Rx_Buff = SCI_Rx_String = 0;         

#ifdef SCI_72F561
    SCICR3 = 0x00;
#endif    
}        
//for SCI2 of 561 device
#ifdef SCI_72F561
void SCI2_Init ( SCI_Type_Param1  Init_Value1, SCI_Type_Param2  Init_Value2)
{   
    unsigned char temp;    
    
    SCI2CR1 = SCI_DEFAULT_PARAM1;                        /*Load Default value*/
    SCI2CR2 = SCI_DEFAULT_PARAM2;
    temp = SCI2SR;                   /*Clear all the flags in Status register*/
    temp = SCI2DR;
    SCI2DR = SCI_DEFAULT_PARAM1;
    SCI2CR1 |= (unsigned char)Init_Value1;    /*Sets SCI in the required mode*/
    SCI2CR2 |= (unsigned char)Init_Value2;
    SCI2_Pol_Stat = SCI2_Error = 0;            /*Initializes global variables*/
    SCI2_Tx_Buff = 0;
    SCI2_Tx_String = 0;
    SCI2_Rx_Buff = SCI2_Rx_String = 0;         
    SCI2CR3 = 0x00;
}
#endif

/*-----------------------------------------------------------------------------
ROUTINE NAME : SCI_Forced_Clear_Flag
INPUT        : None
OUTPUT       : None      
DESCRIPTION  : Clears all status flag performs both read and write operation 
COMMENTS     : Can be used if clearing of all status flag is needed
-----------------------------------------------------------------------------*/
void SCI_Forced_Clear_Flag( void )
{
    unsigned char Temp_Variable,temp;     
    
    Temp_Variable = SCICR2; 
    SCICR2 = 0x00;      
    temp = SCISR;                       /*Clears all flags in status register*/
    SCIDR = 0x00; 
    SCICR2 = Temp_Variable;
}  

#ifdef SCI_72F561
void SCI2_Forced_Clear_Flag( void )
{
    unsigned char Temp_Variable,temp;     
    
    Temp_Variable = SCI2CR2; 
    SCI2CR2 = 0x00;      
    temp = SCI2SR;                      /*Clears all flags in status register*/
    SCI2DR = 0x00; 
    SCI2CR2 = Temp_Variable;
}                
#endif
                                                                               
/*-----------------------------------------------------------------------------
ROUTINE NAME : SCI_Compute_Baudrate
INPUT        : BaudRate_Tx,BaudRate_Rx.
OUTPUT       : None
DESCRIPTION  : Selects any possible  baudrate.
COMMENTS     : User needs to select proper value of function argument.
-----------------------------------------------------------------------------*/
/* RBuilder can override this function by adding 
                                            #define REM_SCI_COMPUTE_BAUDRATE */
#ifndef REM_SCI_COMPUTE_BAUDRATE

void SCI_Compute_Baudrate ( unsigned int BaudRate_Tx, unsigned int BaudRate_Rx)
{
    unsigned char  Presc_Value,TPR1, TPR2, RPR1, RPR2,i;
    unsigned int  Tx_Value, Rx_Value,temp1;
    unsigned int TR1, TR2, RR1, RR2,Presc_Fact,j,count; 
    unsigned long temp; 
           
    TPR1 = 0;
    TPR2 = 0;
    RPR1 = 0;
    RPR2 = 0; 
    temp = 0; 
    
    temp = Fcpu/BaudRate_Tx;                       
    for (temp1 = 0,count = 1;temp1 < 1;count++)    
    {    
        Presc_Fact = ((unsigned int)(temp/16)) ;
        for (i = 0; i != 4; i++)
        {
            for (j = 1; j <= 128; j = j*2)
            {
                if ( ((Pr_Value[i]*j) == Presc_Fact) && (TPR1 == 0) )
                {
                    TPR1 = Pr_Value[i];     /*Prescalar value for Transmitter*/
                    TR1  = j;                      /*Transmitter rate divisor*/
                    temp1++;
                }
                else if ((Pr_Value[i]*j) == Presc_Fact)
                {
                    TPR2 = Pr_Value[i]; 
                                    /*Another Prescalar value for Transmitter*/
                    TR2 = j;               /*Another Transmitter rate divisor*/
                    temp1++;
                }
            }
        }   
        if(count%2 == 0)      /*Changes the temp value if no prescalar factor
                                                                     is found*/
        {         
            temp = temp + count;                           
        }
        else
        {         
            temp = temp - count;
        }    
    }               
    temp = Fcpu/BaudRate_Rx; 
    for (temp1 = 0,count = 1;temp1 < 1;count++)
    {   
        Presc_Fact = (unsigned int)(temp/16); 
        for (i = 0; i != 4; i++)
        {
            for (j = 1; j <= 128; j = j*2)
            {
                if (((Pr_Value[i]*j) == Presc_Fact) && (RPR1 == 0))
                {
                    RPR1 = Pr_Value[i];        /*Prescalar value for Receiver*/
                    RR1    = j;                       /*Receiver rate divisor*/
                    temp1++;
                }
                else if ((Pr_Value[i]*j) == Presc_Fact)
                {
                    RPR2 = Pr_Value[i];
                                       /*Another Prescalar value for Receiver*/
                    RR2    = j;                /*Another Receiver rate divisor*/
                    temp1++;
                }
            }
        }
        if(count%2 == 0)
                     /*Changes the temp value if no prescalar factor is found*/                                                        
        {     
            temp = temp + count;
        }
        else
        {     
            temp = temp - count;
        }
    } 
                /*Searches common Prescalar factor for transmitter & receiver*/
    if (TPR1 == RPR1)    
    {
        Presc_Value = TPR1;
        Tx_Value    = TR1;
        Rx_Value    = RR1;
    }
    else if ( TPR1 == RPR2)
    {
        Presc_Value = TPR1;
        Tx_Value    = TR1;
        Rx_Value    = RR2;
    }
    else if ( TPR2 == RPR1)
    {
        Presc_Value = TPR2;
        Tx_Value    = TR2;
        Rx_Value    = RR1;
    }
    else if ( TPR2 == RPR2)
    {
        Presc_Value = TPR2;
        Tx_Value    = TR2;
        Rx_Value    = RR2;
    }   
    else                    /*If no common prescalar division factor is found*/
    {
        for (temp1=0,count=1;temp1<1;count++)
        {
            for (j=1; j<=128; j= j*2)
            {
                if ( (TPR1*j) == Presc_Fact)
                {
                    Presc_Value = TPR1; 
                    Tx_Value = TR1; 
                    Rx_Value = j;
                    temp1++; 
                }
            }
            if(count%2 == 0)
            {     
                Presc_Fact = Presc_Fact+count;
            }
            else
            {     
                Presc_Fact = Presc_Fact-count;
            }
        }
    }    
    if (Presc_Value == 1)
    {
        Presc_Value = 0;
    }
    else if (Presc_Value == 3)
    {
        Presc_Value = 1;
    }
    else if (Presc_Value == 4)
    {
        Presc_Value = 2;
    }
    else 
    {
        Presc_Value = 3;
    }
    for (i=0,j=1; j<=128; j=j*2)
    {
        if ( j == Tx_Value )
        {
            Tx_Value = i;
        }
        i++;
    }
    for (i=0,j=1; j<=128; j=j*2)
    {
    if ( j == Rx_Value )
        {    
            Rx_Value = i;
        }
        i++;
    }  
    Presc_Value <<= 6 ;
    SCIBRR &= SCI_PR ;  
    SCIBRR |= Presc_Value ;                             /*Loads the prescalar*/

    Tx_Value <<= 3 ;                     /*Loads the transmitter rate divisor*/
    SCIBRR &= SCI_TR ;
    SCIBRR |= (unsigned char)Tx_Value ;

    SCIBRR &= SCI_RR ;                      /*Loads the receiver rate divisor*/
    SCIBRR |= (unsigned char)Rx_Value ;
} 

#ifdef SCI_72F561
void SCI2_Compute_Baudrate ( unsigned int BaudRate_Tx,unsigned int BaudRate_Rx)
{
    unsigned char  Presc_Value,TPR1, TPR2, RPR1, RPR2,i;
    unsigned int  Tx_Value, Rx_Value,temp1;
    unsigned int TR1, TR2, RR1, RR2,Presc_Fact,j,count;
    unsigned long temp;        
    TPR1 = 0;
    TPR2 = 0;
    RPR1 = 0;
    RPR2 = 0; 
    temp = 0; 
    
    temp = Fcpu/BaudRate_Tx;                       
    for (temp1 = 0,count = 1;temp1 < 1;count++)    
    {    
        Presc_Fact = ((unsigned int)(temp/16)) ;
        for (i = 0; i != 4; i++)
        {
            for (j = 1; j <= 128; j = j*2)
            {
                if ( ((Pr_Value[i]*j) == Presc_Fact) && (TPR1 == 0) )
                {
                    TPR1 = Pr_Value[i];     /*Prescalar value for Transmitter*/
                    TR1  = j;                      /*Transmitter rate divisor*/
                    temp1++;
                }
                else if ((Pr_Value[i]*j) == Presc_Fact)
                {
                    TPR2 = Pr_Value[i]; 
                                    /*Another Prescalar value for Transmitter*/
                    TR2 = j;               /*Another Transmitter rate divisor*/
                    temp1++;
                }
            }
        }   
        if(count%2 == 0)      /*Changes the temp value if no prescalar factor
                                                                     is found*/
        {         
            temp = temp + count;                           
        }
        else
        {         
            temp = temp - count;
        }    
    }               
    temp = Fcpu/BaudRate_Rx; 
    for (temp1 = 0,count = 1;temp1 < 1;count++)
    {   
        Presc_Fact = (unsigned int)(temp/16); 
        for (i = 0; i != 4; i++)
        {
            for (j = 1; j <= 128; j = j*2)
            {
                if (((Pr_Value[i]*j) == Presc_Fact) && (RPR1 == 0))
                {
                    RPR1 = Pr_Value[i];        /*Prescalar value for Receiver*/
                    RR1    = j;                        /*Receiver rate divisor*/
                    temp1++;
                }
                else if ((Pr_Value[i]*j) == Presc_Fact)
                {
                    RPR2 = Pr_Value[i];
                                       /*Another Prescalar value for Receiver*/
                    RR2    = j;               /*Another Receiver rate divisor*/
                    temp1++;
                }
            }
        }
        if(count%2 == 0)
                     /*Changes the temp value if no prescalar factor is found*/                                                        
        {     
            temp = temp + count;
        }
        else
        {     
            temp = temp - count;
        }
    } 
                /*Searches common Prescalar factor for transmitter & receiver*/
    if (TPR1 == RPR1)    
    {
        Presc_Value = TPR1;
        Tx_Value    = TR1;
        Rx_Value    = RR1;
    }
    else if ( TPR1 == RPR2)
    {
        Presc_Value = TPR1;
        Tx_Value    = TR1;
        Rx_Value    = RR2;
    }
    else if ( TPR2 == RPR1)
    {
        Presc_Value = TPR2;
        Tx_Value    = TR2;
        Rx_Value    = RR1;
    }
    else if ( TPR2 == RPR2)
    {
        Presc_Value = TPR2;
        Tx_Value    = TR2;
        Rx_Value    = RR2;
    }   
    else                    /*If no common prescalar division factor is found*/
    {
        for (temp1=0,count=1;temp1<1;count++)
        {
            for (j=1; j<=128; j= j*2)
            {
                if ( (TPR1*j) == Presc_Fact)
                {
                    Presc_Value = TPR1; 
                    Tx_Value = TR1; 
                    Rx_Value = j;
                    temp1++; 
                }
            }
            if(count%2 == 0)
            {     
                Presc_Fact = Presc_Fact+count;
            }
            else
            {     
                Presc_Fact = Presc_Fact-count;
            }
        }
    }    
    if (Presc_Value == 1)
    {
        Presc_Value = 0;
    }
    else if (Presc_Value == 3)
    {
        Presc_Value = 1;
    }
    else if (Presc_Value == 4)
    {
        Presc_Value = 2;
    }
    else 
    {
        Presc_Value = 3;
    }
    for (i=0,j=1; j<=128; j=j*2)
    {
        if ( j == Tx_Value )
        {
            Tx_Value = i;
        }
        i++;
    }
    for (i=0,j=1; j<=128; j=j*2)
    {
    if ( j == Rx_Value )
        {    
            Rx_Value = i;
        }
        i++;
    }  
    Presc_Value <<= 6 ;
    SCI2BRR &= SCI_PR ;  
    SCI2BRR |= Presc_Value ;                            /*Loads the prescalar*/

    Tx_Value <<= 3 ;                     /*Loads the transmitter rate divisor*/
    SCI2BRR &= SCI_TR ;
    SCI2BRR |= (unsigned char)Tx_Value ;

    SCI2BRR &= SCI_RR ;                     /*Loads the receiver rate divisor*/
    SCI2BRR |= (unsigned char)Rx_Value ;
}       
#endif
#endif /* REM_SCI_COMPUTE_BAUDRATE */

/*-----------------------------------------------------------------------------
ROUTINE NAME    : SCI_Extend_BaudRate
INPUT           : SCI_PR_X+SCI_TR_Y+RR_Z,ETPR,ERPR 
OUTPUT          : None
DESCRIPTION     : Selects the possible baudrate with extended prescalar    
COMMENTS        : Values are to be precalculated by the user
-----------------------------------------------------------------------------*/
#if (defined SCI_72F521 || defined SCI_72F62 || defined SCI_LITE3 || \
     defined SCI_72F264 || defined SCI_72F561 ||defined SCI_72F34x )
void SCI_Extend_Baudrate (SCI_Baudrate_Type Baudrate_Presc,
                                         unsigned char ETPR,unsigned char ERPR)
{
    SCIBRR = 0x00;
    SCIBRR |= (unsigned char)Baudrate_Presc; 
                                            /*Loads the required value SCIBRR*/
    SCIETPR = ETPR;                    /*loads the required value in SCIETPR */
    SCIERPR = ERPR;                    /*loads the required value in SCIERPR */
}  
#endif

#ifdef SCI_72F561
void SCI2_Extend_Baudrate (SCI_Baudrate_Type Baudrate_Presc,
                                         unsigned char ETPR,unsigned char ERPR)
{
    SCI2BRR = 0x00;
    SCI2BRR |= (unsigned char)Baudrate_Presc;
                                           /*Loads the required value SCIBRR*/
    SCI2ETPR = ETPR;                   /*loads the required value in SCIETPR */
    SCI2ERPR = ERPR;                   /*loads the required value in SCIERPR */
}  
#endif

/*-----------------------------------------------------------------------------
ROUTINE NAME    : SCI_PutByte
INPUT           : Tx_DATA
OUTPUT          : None        
DESCRIPTION     : Transmits a single data byte
COMMENTS        : None
-----------------------------------------------------------------------------*/
void SCI_PutByte (unsigned char Tx_DATA)
{  
    unsigned char temp;
    
#ifdef SCI_POLLING_TX
    temp = SCISR;                          /*Loads the byte to be transmitted*/
    SCIDR = Tx_DATA;
#endif

#ifdef SCI_ITDRV_WITHOUTBUF_TX    
    SCI_Tx_Buff = 1;                               /*Loads the default value */
    SCI_Tx_String = 0;
    temp = SCISR; 
    SCIDR = Tx_DATA;                       /*Loads the byte to be transmitted*/
    SCICR2 |= (unsigned char)SCI_TDRE;                   /* Enable Interrupt */
#endif
}       

#ifdef SCI_72F561
void SCI2_PutByte (unsigned char Tx_DATA)
{  
    unsigned char temp;
    
#ifdef SCI2_POLLING_TX
    temp = SCI2SR;                         /*Loads the byte to be transmitted*/
    SCI2DR = Tx_DATA;
#endif

#ifdef SCI2_ITDRV_WITHOUTBUF_TX    
    SCI2_Tx_Buff = 1;                              /*Loads the default value */
    SCI2_Tx_String = 0;
    temp = SCI2SR; 
    SCI2DR = Tx_DATA;                      /*Loads the byte to be transmitted*/
    SCI2CR2 |= (unsigned char)SCI_TDRE;                    /*Enable Interrupt*/
#endif
}
#endif
/*-----------------------------------------------------------------------------
ROUTINE NAME    : SCI_IsTransmitCompleted
INPUT           : None    
OUTPUT          : TRUE or FALSE    
DESCRIPTION     : Checks if the transmission is completed or not
COMMENTS        : Must be called after Put_Byte or Put_Buffer or Put_String
-----------------------------------------------------------------------------*/
BOOL SCI_IsTransmitCompleted(void)
{
#if defined SCI_ITDRV_WITHOUTBUF_TX 
unsigned char Temp;
#endif
#if defined SCI_POLLING_TX
    if(SCISR & SCI_TC)                  /*Checks if transmission is completed*/
    {
        return (TRUE);
    }
    else 
    {
        return(FALSE);
    }
#else
#if defined SCI_ITDRV_WITHOUTBUF_TX 
    if (SCI_Tx_String == 1)
    {
    Temp = *PtrToSCIBuffTx ;
        if( (SCISR & SCI_TDRE)&& (Temp == '\0')) 
        {
            return (TRUE);
        }
        else
        {
            return (FALSE);
        }
    }
    else if (SCI_Tx_Buff == 0)
    {
        return (TRUE);
    }
    else
    {
        return (FALSE);
    }

#else
    return(TRUE);                                    /*Dummy return statement*/
#endif    
#endif
}

#ifdef SCI_72F561
BOOL SCI2_IsTransmitCompleted(void)
{
#if defined SCI2_POLLING_TX
    if(SCI2SR & SCI_TC)                 /*Checks if transmission is completed*/
    {
        return (TRUE);
    }
    else 
    {
        return(FALSE);
    }
#else
#if defined SCI2_ITDRV_WITHOUTBUF_TX 
    unsigned char Temp;                 
    if (SCI2_Tx_String == 1)
    {    
        Temp = *PtrToSCI2BuffTx;
        if( (SCI2SR & SCI_TDRE) && (Temp == '\0')) 
        {
            return (TRUE);
        }
        else
        {
           return (FALSE);
        }
    }
    else if (SCI2_Tx_Buff == 0)
    {
        return (TRUE);
    }
    else
    {
        return (FALSE);
    }

#else
    return(TRUE);                                 /*Dummy return statement*/
#endif    
#endif
}               
#endif
/*-----------------------------------------------------------------------------
ROUTINE NAME    : SCI_IT_Disable
INPUT           : SCI_IDLE_LINE,SCI_RECEIVE_OVERRUN,SCI_TRANSMIT_REGISTER_READY
                 ,SCI_FRAME_TRANSMITTED,SCI_PARITY_ERROR
OUTPUT          : None
DESCRIPTION     : Disables the interrupt due to a particular flag
COMMENTS        : None
-----------------------------------------------------------------------------*/
void SCI_IT_Disable(SCI_IT_Type SCI_IT_Param)
{ 
   SCICR2 &= (unsigned char)(~(unsigned char)((unsigned char)SCI_IT_Param & 
                                        (~(unsigned char)(SCI_PARITY_ERROR))));
                                            /*disables the selected interrupt*/
   SCICR1 &= (unsigned char)(~(unsigned char)((unsigned char)SCI_IT_Param & 
                                             (unsigned char)SCI_PARITY_ERROR));    
} 

#ifdef SCI_72F561
void SCI2_IT_Disable(SCI_IT_Type SCI_IT_Param)
{ 
   SCI2CR2 &= (unsigned char)(~(unsigned char)((unsigned char)SCI_IT_Param & 
                                          (~(unsigned char)SCI_PARITY_ERROR)));  
                                            /*disables the selected interrupt*/
   SCI2CR1 &= (unsigned char)(~(unsigned char)((unsigned char)SCI_IT_Param & 
                                             (unsigned char)SCI_PARITY_ERROR));     
}
#endif

/*-----------------------------------------------------------------------------
ROUTINE NAME    : SCI_PutBuffer
INPUT           : *PtrtoBuffer,NbOfBytes
OUTPUT          : None
DESCRIPTION     : Transmits  data from the user buffer in polling mode and 
                  starts transmission in interrupt driven with buffer mode
COMMENTS        : None
-----------------------------------------------------------------------------*/
void SCI_PutBuffer (const unsigned char *PtrtoBuffer, unsigned char NbOfBytes)
{  
    unsigned char temp;
#ifdef SCI_POLLING_TX
    for (; NbOfBytes > 0 ; NbOfBytes--)
    { 
           temp = SCISR;
        SCIDR = *PtrtoBuffer;              /*Loads the byte to be transmitted*/
        while (!(SCI_IsTransmitCompleted()));  
                                   /*Waits for the completion of transmission*/
        PtrtoBuffer++;               /*Moves the pointer to the next location*/
    } 
#endif

#ifdef SCI_ITDRV_WITHOUTBUF_TX  
    PtrToSCIBuffTx  = PtrtoBuffer; /*copies the user data to global variables*/    
    SCI_Tx_Buff = NbOfBytes;
    SCI_Tx_String = 0;
    temp = SCISR;
    SCIDR = *PtrToSCIBuffTx;        /*First byte is transmitted to generate the
                                                                    interrupt*/
    SCICR2 |= (unsigned char)SCI_TDRE;                     /*Enable Interrupt*/
#endif
}      

#ifdef SCI_72F561
void SCI2_PutBuffer (const unsigned char *PtrtoBuffer, unsigned char NbOfBytes)
{  
    unsigned char temp;
#ifdef SCI2_POLLING_TX
    for (; NbOfBytes > 0 ; NbOfBytes--)
    { 
           temp = SCI2SR;
        SCI2DR = *PtrtoBuffer;             /*Loads the byte to be transmitted*/
        while (!(SCI2_IsTransmitCompleted()));  
                                   /*Waits for the completion of transmission*/
        PtrtoBuffer++;               /*Moves the pointer to the next location*/
    } 
#endif

#ifdef SCI2_ITDRV_WITHOUTBUF_TX  
    PtrToSCI2BuffTx  = PtrtoBuffer;/*copies the user data to global variables*/    
    SCI2_Tx_Buff = NbOfBytes;
    SCI2_Tx_String = 0;
    temp = SCI2SR;
    SCI2DR = *PtrToSCI2BuffTx;      /*First byte is transmitted to generate the
                                                                    interrupt*/
    SCI2CR2 |= (unsigned char)SCI_TDRE;                    /*Enable Interrupt*/
#endif
}
#endif
/*-----------------------------------------------------------------------------
ROUTINE NAME    : SCI_PutString
INPUT           : *PtrtoString
OUTPUT          : None
DESCRIPTION     : Transmits the user string in polling mode and starts 
                  transmission in interrupt driven mode
COMMENTS        : None
-----------------------------------------------------------------------------*/
void SCI_PutString(const unsigned char *PtrtoString)
{   
    unsigned char temp;

#ifdef SCI_POLLING_TX 
    unsigned char Val = *PtrtoString;
    while (Val != '\0')                        /*Checks for the end of string*/
    {
        Val= *PtrtoString;                    
        temp = SCISR;        
        SCIDR = *PtrtoString;        /*Copies the character to be transmitted*/
        while(!(SCI_IsTransmitCompleted()));   
                               /*waits for the completion of the transmission*/
        PtrtoString++;               /*Moves the pointer to the next location*/
    }    
#endif

#ifdef SCI_ITDRV_WITHOUTBUF_TX 
                /*Copy the adress of the user string into the global variable*/
    PtrToSCIBuffTx = PtrtoString;   
   /*copy default value to recognize string transmission in interrupt routine*/
    SCI_Tx_String = 1;               
    SCI_Tx_Buff = 0;                                     
    temp = SCISR;
    SCIDR = *PtrToSCIBuffTx;/*transmits first character to generate interrupt*/
    SCICR2 |= (unsigned char)SCI_TDRE;                     /*Enable Interrupt*/
#endif
} 

#ifdef SCI_72F561
void SCI2_PutString(const unsigned char *PtrtoString)
{   
    unsigned char temp;

#ifdef SCI2_POLLING_TX 
    unsigned char Val = *PtrtoString;
    while (Val != '\0')                        /*Checks for the end of string*/
    {
        Val= *PtrtoString;                    
        temp = SCI2SR;        
        SCI2DR = *PtrtoString;       /*Copies the character to be transmitted*/
        while(!(SCI2_IsTransmitCompleted()));   
                               /*waits for the completion of the transmission*/
        PtrtoString++;               /*Moves the pointer to the next location*/
    }    
#endif

#ifdef SCI2_ITDRV_WITHOUTBUF_TX 
                /*Copy the adress of the user string into the global variable*/    
    PtrToSCI2BuffTx = PtrtoString;  
/*copy default value to recognize string transmission in interrupt subroutine*/
    SCI2_Tx_String = 1;              
    SCI2_Tx_Buff = 0;                                     
    temp = SCI2SR;
                            /*transmits first character to generate interrupt*/
    SCI2DR = *PtrToSCI2BuffTx;
    SCI2CR2 |= (unsigned char)SCI_TDRE;                   /*Enable Interrupt */
#endif
}
#endif
/*-----------------------------------------------------------------------------
ROUTINE NAME    : SCI_9thBit_TxRx
INPUT           : TRUE or FALSE
OUTPUT          : TRUE or FALSE
DESCRIPTION     : Writes the 9th bit to be transmitted in the SCICR1 register 
                  and returns the 9th bit received
COMMENTS        : 9 bit mode must be enabled.
-----------------------------------------------------------------------------*/
BOOL SCI_9thBit_TxRx(BOOL Bit9_Val)
{
    if (Bit9_Val == TRUE)              /*If 9th bit is to be transmittes as 1*/
    {
        SCICR1 |= SCI_TX_9TH;
    }
    else
    {
        SCICR1 &= (unsigned char)~(unsigned char)SCI_TX_9TH;
    }
    if (SCICR1 & SCI_RX_9TH)                /*If the 9th bit is received as 1*/
    {
        return (TRUE);
    }
    else
    {
        return (FALSE);
    }
}    

#ifdef SCI_72F561
BOOL SCI2_9thBit_TxRx(BOOL Bit9_Val)
{
    if (Bit9_Val == TRUE)              /*If 9th bit is to be transmittes as 1*/
    {
        SCI2CR1 |= SCI_TX_9TH;
    }
    else
    {
        SCI2CR1 &= (unsigned char)~(unsigned char)SCI_TX_9TH;
    }
    if (SCI2CR1 & SCI_RX_9TH)               /*If the 9th bit is received as 1*/
    {
        return (TRUE);
    }
    else
    {
        return (FALSE);
    }
}
#endif                
/*-----------------------------------------------------------------------------
ROUTINE NAME    : SCI_GetByte
INPUT           : None
OUTPUT          : Received Data
DESCRIPTION     : Retunrs a data received to the user
COMMENTS        : None
-----------------------------------------------------------------------------*/
unsigned char SCI_GetByte(void)
{  
#if defined SCI_POLLING_RX     
    return(SCIDR);                     /*Returns the current value of the Data 
                                                         register to the user*/
#else

#if defined SCI_ITDRV_WITHOUTBUF_RX 
    SCI_Error = 0x00;
    return(SCI_MY_DATA);       
#else    
    return (SCIDR);           
#endif
#endif
}

#ifdef SCI_72F561
unsigned char SCI2_GetByte(void)
{  
#if defined SCI2_POLLING_RX     
    return(SCI2DR);                     /*Returns the current value of the Data 
                                                         register to the user*/
#else

#if defined SCI2_ITDRV_WITHOUTBUF_RX 
    SCI2_Error = 0x00;
    return(SCI2_MY_DATA);       
#else    
    return (SCI2DR);           
#endif
#endif
}
#endif

/*-----------------------------------------------------------------------------
ROUTINE NAME    : SCI_GetBuffer
INPUT           : *PtrtoBuffer,NbOfBytes
OUTPUT          :  Error Status
DESCRIPTION     : Receives a continuous data,stores it into the user buffer and
                  returns the error status to the user
COMMENTS        : Reception stops as soon as an error occurs
-----------------------------------------------------------------------------*/
#ifdef SCI_POLLING_RX
SCI_RxError_t SCI_GetBuffer(unsigned char *PtrtoBuffer,unsigned char NbOfBytes)
{
    unsigned char Temp = 0x00 ;
       
    for (;((NbOfBytes > 0)&& (!(Temp))); --NbOfBytes)           
                                    /*Checks if all the bytes are transmitted*/
    {   
        Temp |= (unsigned char)(SCISR & SCI_ErrValue);
        while (!(SCISR & SCI_RDRF)); 
        Temp |= (unsigned char)(SCISR & SCI_ErrValue);
                                           /*waits for the next received byte*/
        *PtrtoBuffer = SCIDR;  
                              /*copies the received byte into the user buffer*/
        PtrtoBuffer++;               /*Moves the pointer to the next location*/
    } 
    return((SCI_RxError_t)Temp);
}
#endif 


#ifdef SCI_72F561
#ifdef SCI2_POLLING_RX
SCI_RxError_t SCI2_GetBuffer(unsigned char *PtrtoBuffer,unsigned char NbOfBytes)
{
    unsigned char Temp = 0x00 ;
       
    for (;((NbOfBytes > 0)&& (!(Temp))); --NbOfBytes)           
                                    /*Checks if all the bytes are transmitted*/
    {   
        Temp |= (unsigned char)(SCI2SR & SCI_ErrValue);
        while (!(SCI2SR & SCI_RDRF)); 
        Temp |= (unsigned char)(SCI2SR & SCI_ErrValue);
                                           /*waits for the next received byte*/
        *PtrtoBuffer = SCI2DR;  
                              /*copies the received byte into the user buffer*/
        PtrtoBuffer++;               /*Moves the pointer to the next location*/
    } 
    return((SCI_RxError_t)Temp);
}
#endif
#endif
/*-----------------------------------------------------------------------------
ROUTINE NAME    : SCI_GetString
INPUT           : *PtrtoString
OUTPUT          : Error Status
DESCRIPTION     : Receives a string,Stores it in the user buffer and returns
                  the error status to the user
COMMENTS        : Reception stops as soon as an error occurs
-----------------------------------------------------------------------------*/
#ifdef SCI_POLLING_RX
SCI_RxError_t SCI_GetString(unsigned char *PtrtoString)
{
    unsigned char Temp = 0x00, Val = 0xff; 
 
    for (;((Val != '\0')&&(Temp == 0));PtrtoString++)
    {    
        Temp |= (unsigned char)(SCISR & SCI_ErrValue);   
        while (!(SCISR & SCI_RDRF));            /*waits for the received byte*/
        Temp |= (unsigned char)(SCISR & SCI_ErrValue);         
                   /*Temp stores any error that occurred during the reception*/
        *PtrtoString = SCIDR; 
                         /*Copies the received character into the user string*/
        Val = *PtrtoString;
    }  
    return((SCI_RxError_t)Temp);       /*Returns the error status to the user*/
}
#endif

#ifdef SCI_72F561
#ifdef SCI2_POLLING_RX
SCI_RxError_t SCI2_GetString(unsigned char *PtrtoString)
{
    unsigned char Temp = 0x00, Val = 0xff; 
 
    for (;((Val != '\0')&&(Temp == 0));PtrtoString++)
    {    
        Temp |= (unsigned char)(SCI2SR & SCI_ErrValue);   
        while (!(SCI2SR & SCI_RDRF));           /*waits for the received byte*/
        Temp |= (unsigned char)(SCI2SR & SCI_ErrValue);          
                   /*Temp stores any error that occurred during the reception*/
        *PtrtoString = SCI2DR; 
                         /*Copies the received character into the user string*/
        Val = *PtrtoString;
    }  
    return((SCI_RxError_t)Temp);      /*Returns the error status to the user*/
}
#endif           
#endif

/*-----------------------------------------------------------------------------
ROUTINE NAME    : SCI_GetBuffer
INPUT           : *PtrtoBuffer, NbOfBytes 
OUTPUT          : None    
DESCRIPTION     : Starts the reception in the interrupt subroutine
COMMENTS        : Any data received before calling this function is ignored
-----------------------------------------------------------------------------*/
#ifdef SCI_ITDRV_WITHOUTBUF_RX
void SCI_GetBuffer(unsigned char *PtrtoBuffer,unsigned char NbOfBytes) 
{
    PtrToSCIBuffRx = PtrtoBuffer;/*Copies user buffer to the global variable */
    SCI_Rx_Buff = NbOfBytes;
    SCI_Rx_Buff++;
    SCI_Error = 0x00;                                     
    SCICR2 |= (unsigned char)SCI_RDRF;                     /*Enable Interrupt*/    
}                                                         

/*-----------------------------------------------------------------------------
ROUTINE NAME    : SCI_GetString
INPUT           : *PtrtoString  
OUTPUT          : None 
DESCRIPTION     : Starts the reception of a string in the interrupt subroutine
COMMENTS        : Any data received before calling this function is ignored
-----------------------------------------------------------------------------*/
void SCI_GetString(unsigned char *PtrtoString) 
{
    PtrToSCIBuffRx = (PtrtoString); /*Copies the user buffer to global buffer*/
    SCI_Rx_String = 1;                        /*For String recognition in ISR*/ 
    SCI_Error = 0x00;
    SCICR2 |= (unsigned char)SCI_RDRF;                     /*Enable Interrupt*/    
}
#endif     

#ifdef SCI_72F561
#ifdef SCI2_ITDRV_WITHOUTBUF_RX
void SCI2_GetBuffer(unsigned char *PtrtoBuffer,unsigned char NbOfBytes) 
{
    PtrToSCI2BuffRx = PtrtoBuffer;/*Copies user buffer to the global variable*/
    SCI2_Rx_Buff = NbOfBytes;
    SCI2_Rx_Buff++;
    SCI2_Error = 0x00;                                     
    SCI2CR2 |= (unsigned char)SCI_RDRF;                    /*Enable Interrupt*/    
}                                                         

void SCI2_GetString(unsigned char *PtrtoString) 
{
    PtrToSCI2BuffRx = (PtrtoString);/*Copies the user buffer to global buffer*/
    SCI2_Rx_String = 1;                       /*For String recognition in ISR*/ 
    SCI2_Error = 0x00;
    SCI2CR2 |= (unsigned char)SCI_RDRF;                    /*Enable Interrupt*/    
}
#endif
#endif
/*-----------------------------------------------------------------------------
ROUTINE NAME    : SCI_IsReceptionCompleted
INPUT           : None
OUTPUT          : Error Status
DESCRIPTION     : Returns the error status occurred during the reception in the
                  interrupt subroutine if the reception is completed.
COMMENTS        : Must be called after SCI_GetBuffer and SCI_GetString in 
                  Interrupt Driven mode to get the reception status.
                  Must be called before SCI_GetByte in interrupt Driven mode.
-----------------------------------------------------------------------------*/
SCI_RxError_t SCI_IsReceptionCompleted (void)  
{   
#if defined SCI_ITDRV_WITHOUTBUF_RX   
    unsigned char Temp_Error,Temp_BuffRx, Temp_PolStat, Temp_RxString ;
    SCI_Pol_Stat = 0;          
    Temp_Error = SCI_Error ;
    Temp_BuffRx = *(PtrToSCIBuffRx);
    Temp_PolStat =  SCI_Pol_Stat ;
    Temp_RxString = SCI_Rx_String;

    if ((SCI_Rx_String == 1) && ((Temp_BuffRx != '\0') &&(Temp_Error == 0)))
                                        /*String reception request undergoing*/
    {
        return(SCI_STRING_ONGOING);
    }  
    else if ((SCI_Rx_Buff > 1) && (Temp_Error == 0) ) 
                                        /*Buffer Reception request undergoing*/
    {   
        return(SCI_BUFFER_ONGOING) ;
    }
    else if ((SCI_Rx_Buff == 0) && ((Temp_RxString == 0) && (Temp_PolStat == 0)))    
                                                   /*Single Byte not received*/
    {
        return (SCI_RX_DATA_EMPTY);    
    }
    else                                             /*Reception is completed*/
    {       
        SCI_Pol_Stat = 0; 
        SCI_Rx_Buff = 0;
        SCI_Rx_String = 0;
        return ((SCI_RxError_t)SCI_Error);
    }           
#else
#if defined SCI_POLLING_RX
    if(SCISR & SCI_RDRF)
    {
        return ((SCI_RxError_t)(SCISR & SCI_ErrValue)); 
    }
    else
    {
        return (SCI_RX_DATA_EMPTY);    /*Returns the error status to the user*/
    }
#else 
        return (SCI_RX_DATA_EMPTY);                  /*Dummy return statement*/
#endif
#endif        
}     

#ifdef SCI_72F561
SCI_RxError_t SCI2_IsReceptionCompleted (void)  
{   
#if defined SCI2_ITDRV_WITHOUTBUF_RX          
    unsigned char Temp_BuffRx, Temp_PolStat, Temp_RxString, Temp_Error;
    SCI2_Pol_Stat = 0;          

    Temp_Error = SCI2_Error ;
    Temp_BuffRx = *(PtrToSCI2BuffRx);
    Temp_PolStat =  SCI2_Pol_Stat ;
    Temp_RxString = SCI2_Rx_String;
    if((SCI2_Rx_String == 1)&&((Temp_BuffRx != '\0')&&(Temp_Error == 0)))
                                        /*String reception request undergoing*/
    {
        return(SCI_STRING_ONGOING);
    }  
    else if ( (SCI2_Rx_Buff > 1) && (Temp_Error == 0)) 
                                        /*Buffer Reception request undergoing*/
    {   
        return(SCI_BUFFER_ONGOING) ;
    }
    else if ((SCI2_Rx_Buff == 0)&&((Temp_RxString == 0)&&(Temp_PolStat == 0)))    
                                                   /*Single Byte not received*/
    {
        return (SCI_RX_DATA_EMPTY);    
    }
    else                                             /*Reception is completed*/
    {       
        SCI2_Pol_Stat = 0; 
        SCI2_Rx_Buff = 0;
        SCI2_Rx_String = 0;
        return ((SCI_RxError_t)SCI2_Error);
    }           
#else
#if defined SCI2_POLLING_RX
    if(SCI2SR & SCI_RDRF)
    {
        return ((SCI_RxError_t)(SCI2SR & SCI_ErrValue)); 
    }
    else
    {
        return (SCI_RX_DATA_EMPTY);    /*Returns the error status to the user*/
    }
#else 
        return (SCI_RX_DATA_EMPTY);                  /*Dummy return statement*/
#endif
#endif        
}
#endif
/*-----------------------------------------------------------------------------
ROUTINE NAME    : SCI_IT_Function
INPUT           : None
OUTPUT          : None
DESCRIPTION     : Carries out communiction in Interrupt driven mode.
COMMENTS        : This function must be called inside the SCI interrupt service
                  routine for Interrupt driven communication
-----------------------------------------------------------------------------*/
void  SCI_IT_Function (void)
{                                                 
/******************************************************************************
Reception in Interrupt Driven Without Buffer mode
******************************************************************************/
unsigned char Temp_Buff;
#ifdef SCI_ITDRV_WITHOUTBUF_RX   
    unsigned char Temp_Error,Temp_RxString,temp;
    if(SCISR & SCI_ORE)      /*If Interrupt is generated due to overrun error*/
    {
        SCI_Error = (unsigned char)(SCISR & SCI_ErrValue);
        temp = SCIDR;    
        SCI_Pol_Stat = 2;
    } 
    else if (SCISR & SCI_RDRF)
    {   
        Temp_Buff = *PtrToSCIBuffRx ;
        Temp_Error = SCI_Error ;
        Temp_RxString = SCI_Rx_String ;     
        if((SCI_Rx_String == 1)&&((Temp_Buff != '\0')&&(Temp_Error == 0)))     
                                                        /*If String Reception*/
        {       
            SCI_Error = (unsigned char)(SCISR & SCI_ErrValue);
            *PtrToSCIBuffRx = SCIDR;      
            if (*PtrToSCIBuffRx != '\0')
            {
                PtrToSCIBuffRx++;
            }             
            else
            {
                SCICR2 &= (unsigned char)(~(unsigned char)SCI_RDRF);    
                                                         /*Disable interrupt */
            }    
            SCI_Pol_Stat = 2;
        }
        else if ((SCI_Rx_Buff > 1) && (Temp_Error == 0))
        {              
            SCI_Error = (unsigned char)(SCISR & SCI_ErrValue) ;  
            *PtrToSCIBuffRx = SCIDR;
            SCI_Rx_Buff--;  
            SCI_Pol_Stat = 2;                       /*If a buffer is received*/
            PtrToSCIBuffRx++;  
            if(SCI_Rx_Buff == 1)
            { 
                SCICR2 &= (unsigned char)(~(unsigned char)SCI_RDRF);   
                                                         /*Disable interrupt */
            }    
        }
        else if ((SCI_Rx_Buff == 0) && (Temp_RxString== 0))  
       /*If interrupt is generated before calling SCI_GetBuffer/SCI_GetString*/
        {  
            SCI_Error = (unsigned char)(SCISR & SCI_ErrValue) ;  
            SCI_MY_DATA = SCIDR;
            SCI_Pol_Stat = 1;
        }                    
        else
        {   
            SCI_Pol_Stat = 1;
            temp = SCIDR;
        }                
    }    
#endif 

/******************************************************************************
Transmission in Interrupt Driven without Buffer mode
******************************************************************************/

#ifdef SCI_ITDRV_WITHOUTBUF_TX 
  {
    unsigned char Temp_CR2;
    Temp_CR2 = SCICR2 ;
                                /*Checks if transmit complete flag is set*/
    if((SCISR & SCI_TC)&& (Temp_CR2 & SCI_TC))
    {   Temp_Buff = *PtrToSCIBuffTx  ;   
        if ((SCI_Tx_String == 1) && (Temp_Buff != '\0')) 
                                   /*Checks if complete string is transmitted*/
        {   
            PtrToSCIBuffTx++;
            SCIDR = *PtrToSCIBuffTx;
            if (*PtrToSCIBuffTx == '\0')
            {
                SCICR2 &= (unsigned char)(~(unsigned char)SCI_TC);  
                               /*Disable IT if complete string is transmitted*/
            } 
        } 
        else if (SCI_Tx_Buff > 1)   /*Checks if all the bytes are transmitted*/
        {
            SCI_Tx_Buff--; 
            PtrToSCIBuffTx++;            /*Moves the pointer to next location*/
            SCIDR = *PtrToSCIBuffTx;                /*Transmits a single byte*/
        }                          
        else if(SCI_Tx_Buff == 1)
        {
            SCI_Tx_Buff--;
            SCICR2 &= (unsigned char)(~(unsigned char)SCI_TC);     
                               /*Disable IT if complete string is transmitted*/
        }
    }                                              
                                                 /*Checks if TDRE Flag is set*/
    else if ((SCISR & SCI_TDRE)&&(Temp_CR2 & SCI_TDRE))  
    {           
        Temp_Buff = *PtrToSCIBuffTx ;
                           /*Checks if complete string is transmitted*/
        if ((SCI_Tx_String == 1)&&(Temp_Buff != '\0')) 
        {   
           PtrToSCIBuffTx++;
           SCIDR = *PtrToSCIBuffTx;
           if (*PtrToSCIBuffTx == '\0')
           {    
               Nop
               SCICR2 &= (unsigned char)(~(unsigned char)SCI_TDRE);
                               /*Disable IT if complete string is transmitted*/
               Nop
           }    

        }
        else if (SCI_Tx_Buff > 1)   /*Checks if all the bytes are transmitted*/
        {
            SCI_Tx_Buff--; 
            PtrToSCIBuffTx++;            /*Moves the pointer to next location*/
            SCIDR = *PtrToSCIBuffTx;                /*Transmits a single byte*/
        }
        else if(SCI_Tx_Buff == 1)
        {
            SCI_Tx_Buff--;
            SCICR2 &= (unsigned char)(~(unsigned char)SCI_TDRE);
                               /*Disable IT if complete string is transmitted*/
        } 
    }
  }    
#endif
}


#ifdef SCI_72F561
void  SCI2_IT_Function (void)
{                                                 
/******************************************************************************
Reception in Interrupt Driven Without Buffer mode
******************************************************************************/

#ifdef SCI2_ITDRV_WITHOUTBUF_RX 
    unsigned char temp, Temp_Error, Temp_Buff, Temp_RxString, Temp_CR2;  
    if(SCI2SR & SCI_ORE)     /*If Interrupt is generated due to overrun error*/
    {
        SCI2_Error = (unsigned char)(SCI2SR & SCI_ErrValue);
        temp = SCI2DR;    
        SCI2_Pol_Stat = 2;
    } 
    else if (SCI2SR & SCI_RDRF)
    {   
        Temp_Buff = *PtrToSCI2BuffRx ;
        Temp_Error = SCI2_Error ;
        Temp_RxString = SCI2_Rx_String ;     
        if((SCI2_Rx_String == 1)&&((Temp_Buff != '\0')&&(Temp_Error == 0)))
                                                        /*If String Reception*/
        {       
            SCI2_Error = (unsigned char)(SCI2SR & SCI_ErrValue);
            *PtrToSCI2BuffRx = SCI2DR;      
            if (*PtrToSCI2BuffRx != '\0')
            {
                PtrToSCI2BuffRx++;
            }             
            else
            {
                SCI2CR2 &= (unsigned char)(~(unsigned char)SCI_RDRF);  
                                                         /*Disable interrupt */
            }    
            SCI2_Pol_Stat = 2;
        }
        else if ((SCI2_Rx_Buff > 1) && (Temp_Error == 0))
        {              
            SCI2_Error = (unsigned char)(SCI2SR & SCI_ErrValue) ;  
            *PtrToSCI2BuffRx = SCI2DR;
            SCI2_Rx_Buff--;  
            SCI2_Pol_Stat = 2;                     /*If a buffer is received*/
            PtrToSCI2BuffRx++;  
            if(SCI2_Rx_Buff == 1)
            { 
                SCI2CR2 &= (unsigned char)(~(unsigned char)SCI_RDRF);  
                                                        /*Disable interrupt */
            }    
        }
        else if ((SCI2_Rx_Buff == 0) && (Temp_RxString == 0))  
       /*If interrupt is generated before calling SCI_GetBuffer/SCI_GetString*/
        {  
            SCI2_Error = (unsigned char)(SCI2SR & SCI_ErrValue) ;  
            SCI2_MY_DATA = SCI2DR;
            SCI2_Pol_Stat = 1;
        }                    
        else
        {   
            SCI2_Pol_Stat = 1;
            temp = SCI2DR;
        }                
    }    
#endif 

/******************************************************************************
Transmission in Interrupt Driven without Buffer mode
******************************************************************************/

#ifdef SCI2_ITDRV_WITHOUTBUF_TX 
    unsigned char temp, Temp_Error, Temp_Buff, Temp_RxString, Temp_CR2;  
                                    /*Checks if transmit complete flag is set*/
    Temp_CR2 = SCI2CR2 ;
    if((SCI2SR & SCI_TC)&& (Temp_CR2 & SCI_TC))
    {   Temp_Buff = *PtrToSCI2BuffTx  ;   
        if ((SCI2_Tx_String == 1) && (Temp_Buff != '\0')) 
                                   /*Checks if complete string is transmitted*/
        {   
            PtrToSCI2BuffTx++;
            SCI2DR = *PtrToSCI2BuffTx;
            if (*PtrToSCI2BuffTx == '\0')
            {
                SCI2CR2 &= (unsigned char)(~(unsigned char)SCI_TC);  
                              /*Disable IT if complete string is transmitted*/
            } 
        } 
        else if (SCI2_Tx_Buff > 1)  /*Checks if all the bytes are transmitted*/
        {
            SCI2_Tx_Buff--; 
            PtrToSCI2BuffTx++;           /*Moves the pointer to next location*/
            SCI2DR = *PtrToSCI2BuffTx;              /*Transmits a single byte*/
        }                          
        else if(SCI2_Tx_Buff == 1)
        {
            SCI2_Tx_Buff--;
            SCI2CR2 &= (unsigned char)(~(unsigned char)SCI_TC);     
                               /*Disable IT if complete string is transmitted*/
        }
    }                                            
                                                 /*Checks if TDRE Flag is set*/
    else if ((SCI2SR & SCI_TDRE)&&(Temp_CR2 & SCI_TDRE))  
    {           
        Temp_Buff = *PtrToSCI2BuffTx  ;/*Checks if complete string is transmitted*/
        if ((SCI2_Tx_String == 1)&&(Temp_Buff != '\0')) 
        {   
           PtrToSCI2BuffTx++;
           SCI2DR = *PtrToSCI2BuffTx;
           if (*PtrToSCI2BuffTx == '\0')
           {    
               Nop
               SCI2CR2 &= (unsigned char)(~(unsigned char)SCI_TDRE);
                               /*Disable IT if complete string is transmitted*/
               Nop
           }    

        }
        else if (SCI2_Tx_Buff > 1)  /*Checks if all the bytes are transmitted*/
        {
            SCI2_Tx_Buff--; 
            PtrToSCI2BuffTx++;           /*Moves the pointer to next location*/
            SCI2DR = *PtrToSCI2BuffTx;              /*Transmits a single byte*/
        }
        else if(SCI2_Tx_Buff == 1)
        {
            SCI2_Tx_Buff--;
            SCI2CR2 &= (unsigned char)(~(unsigned char)SCI_TDRE);
                               /*Disable IT if complete string is transmitted*/
        } 
    }    
#endif
}
#endif
/**** (c) 2002   STMicroelectronics *************************** END OF FILE **/ 

