/******************************************************************************
COPYRIGHT 2002 STMicroelectronics
Source File Name :  main.c  
Group            :  IPSW,CMG-IPDF
Author           : MCD Application Team
Date First Issued:  5/3/2002
********************************Documentation**********************************
General Purpose - It is application file which calls the functions in sci.c. 
This example carries out the single byte transmission and continuous 
transmission in polling mode or in interrupt driven without buffer mode as per 
the selection made in the ST7lib_Config.h. Also it carries out the reception of
a single byte and a buffer of data in polling mode or in interrupt driven 
without buffer mode.The transmission and reception baudrate with extended 
prescalar register are selected as 9600.
Also for the transmission in interrupt driven case transmit data register ready
flag is enabled for generating the interrupt.      
********************************Revision History*******************************
_______________________________________________________________________________
Date : 5/3/2002             Release:1.0  
Date : 28/04/04             MISRA changes

--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**/
#include "ST7lib_Config.h"   /*List of all ST7 devices and communication mode*/
#include "main.h"           /*Declaration of all the functions used in main.c*/

#define Buf_Size ((unsigned char)20)

void main(void);
static unsigned int Timeoutcount;
unsigned char Buff_Rcv[20]= {"                  "} ; 

void main (void)
{ 
    unsigned char Rx_Data = 0;
    unsigned char NbOfBytes = 20;
    unsigned char Tx_Data = 51;
    unsigned int BaudRate_Tx = 9600;
    unsigned int BaudRate_Rx = 9600;
    SCI_RxError_t Err = 0;
    unsigned char Buff[Buf_Size]= "SCI DRIVERS TESTING"; /*user buffer*/ 
    unsigned char new[] = "\n\r";
    BOOL Bool_Temp1 ;
    BOOL Bool_Temp2 ;
    Timeoutcount = 0;  
    SCI_Init(SCI_DEFAULT_PARAM1, SCI_DEFAULT_PARAM2); 
    SCI_Compute_Baudrate(BaudRate_Tx,BaudRate_Rx);
                        /*Selects the transmission reception baudrate as 9600*/
//    SCI_Extend_Baudrate (SCI_PR_13+SCI_TR_2+SCI_RR_2,0x02,0x02);  
/*                            Selects transmission reception baudrate as 9600*/
    EnableInterrupts 
/*-----------------------------------------------------------------------------
Transmission through Polling mechanism
-----------------------------------------------------------------------------*/
#ifdef SCI_POLLING_TX                 /*Selects polling mode for transmission*/
    SCI_Mode(SCI_TX_ENABLE);                                                  
                               
    SCI_PutByte(Tx_Data);/*Single Byte transmission*/
    Bool_Temp1 = (SCI_IsTransmitCompleted()); /* this function causes a 
    volatile variable to change,hence it cannot be put as a right side operand 
                                                      in a conditional stmt */
                                
    while ( (User_Timeout_Function()) && (!(Bool_Temp1)));
    SCI_PutBuffer( Buff,NbOfBytes);/*Continuous buffer transmission*/

#endif
/*-----------------------------------------------------------------------------
Reception Through Polling mechanism
-----------------------------------------------------------------------------*/
#ifdef SCI_POLLING_RX                   /*Selects polling mode for  reception*/
    SCI_Mode(SCI_RX_ENABLE);               /* Enable the receiver mode of SCI*/
    
/*-----------------------------Single Byte Reception-------------------------*/

    do                                              /*Wait for data reception*/
    {
        Err = SCI_IsReceptionCompleted();
    }while ((User_Timeout_Function()) && (Err == SCI_RX_DATA_EMPTY) ); 
     
    if(User_Timeout_Function())               /*Byte received before Timeout */    
    { 
        if(Err == SCI_RECEIVE_OK)
        {            
            Rx_Data = SCI_GetByte();             /*Correct Data Byte received*/     
            Nop 
        }    
        else
        {
            if((unsigned char)Err & SCI_NOISE_ERR)
            {
                Userfunction ();
            }
            if((unsigned char)Err & SCI_OVERRUN_ERR)
            {
                Userfunction ();
            }
            if((unsigned char)Err & SCI_FRAMING_ERR)
            {
                Userfunction ();
            }
            if((unsigned char)Err & SCI_PARITY_ERR)
            {
                Userfunction ();
            } 
            Rx_Data = SCI_GetByte();           /*Corrupted Data Byte received*/
        } 
    }                                                       /*Timeout elapsed*/
    else
    {
        while(1);                   /*Transmitter or Receiver having problem */
    }                                                                          

/*----------------------------Buffer Reception-------------------------------*/

    Err = (SCI_GetBuffer(Buff_Rcv,(unsigned char)19));  /*Reception of data 
                                                               in user buffer*/
                                                    
    if(Err == SCI_RECEIVE_OK)                       /*Checks the error status*/
    {
        Nop                                                 /* Reception OK*/
    }                                                         
    else                                     /*Error Ocurred during reception*/
    {
        if((unsigned char)Err & SCI_NOISE_ERR)
        {
            Userfunction ();
        }
        if((unsigned char)Err & SCI_OVERRUN_ERR)
        {
            Userfunction ();
        }
        if((unsigned char)Err & SCI_FRAMING_ERR)
        {
            Userfunction ();
        }
        if((unsigned char)Err & SCI_PARITY_ERR)
        {
            Userfunction ();
        }
    }   
#endif 
/*-----------------------------------------------------------------------------
Transmission through Interrupt Driven without Buffer mode
-----------------------------------------------------------------------------*/ 
#ifdef SCI_ITDRV_WITHOUTBUF_TX      /*Selects interrupt mode for transmission*/
                                    /*Interrupt enable when TDRE flag is set */
    SCI_Mode(SCI_TX_ENABLE);   
    if(SCI_IsTransmitCompleted())
    {         
        SCI_PutByte((unsigned char)55);  
        while (!(SCI_IsTransmitCompleted())); 
        SCI_PutString(Buff);/*user pointer is copied to the global pointer*/
/* Here,user can perform other tasks or operations except transmission till the
time transmission is complete,after which user can perform transmission again*/        
        
        while (!(SCI_IsTransmitCompleted())); 
    }           
#endif
/*-----------------------------------------------------------------------------
Reception through Interrupt driven without Buffer mechanism
-----------------------------------------------------------------------------*/
#ifdef SCI_ITDRV_WITHOUTBUF_RX         /*Selects interrupt mode for reception*/
                        /*Interrupt enable when RDR register is ready to read*/
    SCI_IT_Enable((unsigned char)SCI_RECEIVE_OVERRUN);            
    SCI_Mode(SCI_RX_ENABLE); 
             
/*----------------------------Single Byte reception--------------------------*/   
    do
    {
        Err = SCI_IsReceptionCompleted();
    }while ((User_Timeout_Function()) && (Err == SCI_RX_DATA_EMPTY) );
                    /* Wait for the completion of current data byte reception*/ 
     
    if(User_Timeout_Function())      
    {   
       if(Err == SCI_RECEIVE_OK)
        {/*Reception OK                                         */
          Nop
            Rx_Data = SCI_GetByte(); 
            Nop
        }
        else
        {
            if((unsigned char)Err & SCI_NOISE_ERR)
            {
                Userfunction ();
            }
            if((unsigned char)Err & SCI_OVERRUN_ERR)
            {
                Userfunction ();
            }
            if((unsigned char)Err & SCI_FRAMING_ERR)
            {
                Userfunction ();
            }
            if((unsigned char)Err & SCI_PARITY_ERR)
            {
                Userfunction ();
            }
            Rx_Data = SCI_GetByte();  /*User will receive the corrupted data */
        }
    }    
    else
    {
        while(1);                   /*Transmitter or Receiver having problem */
    }             

/*------------------------------Buffer Reception-----------------------------*/    

    SCI_GetBuffer(Buff_Rcv,(unsigned char)19);  
                 /* Any data received before calling this function is ignored*/
/* Here, user can perform other tasks or operations except reception till the
     time the function SCI_IsReceptionCompleted() returns RECEIVE_OK, 
     after which user can perform reception again                            */
 
    do
    {
        Err = SCI_IsReceptionCompleted();
    }while (Err == SCI_BUFFER_ONGOING); 
       /* To be sure that the communication by this point has been completed */

    if(Err == SCI_RECEIVE_OK) 
    {
        Nop                                                  /*Reception OK */  
    }
    else 
    {
        if((unsigned char)Err & SCI_NOISE_ERR)
        {
            Userfunction ();
        }
        if((unsigned char)Err & SCI_OVERRUN_ERR)
        {
            Userfunction ();
        }
        if((unsigned char)Err & SCI_FRAMING_ERR)
        {
            Userfunction ();
        }
        if((unsigned char)Err & SCI_PARITY_ERR)
        {
            Userfunction ();
        }
    }                                                                              

#endif  
 Nop                                      
}
/******************************************************************************
Interrupt Subroutine
******************************************************************************/

#ifdef _HIWARE_                                  /* Test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS            /* Additional registers will be saved */
#else
#ifdef _COSMIC_                                  /* Test for Cosmic Compiler */
@interrupt                                      /* Cosmic interrupt handling */
#else
#error"Unsupported Compiler!"                 /* Compiler Defines not found! */
#endif
#endif
void sci_rt (void)
{
    SCI_IT_Function();                    /*Interrupt function of the library*/
}

/*******************************End OF ISR************************************/
 
/*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**/
void Userfunction(void)
{
    SCI_Forced_Clear_Flag();
}

BOOL User_Timeout_Function(void)
{    
    while(Timeoutcount < 50000)
    {
        Timeoutcount++ ;
        return (TRUE);        
    } 
    return (FALSE)  ;
}            
    
/**** (c) 2002   STMicroelectronics *************************** END OF FILE **/
