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

********************************Revision History*******************************
_______________________________________________________________________________
Date :5/3/2002          Release:1.0
Date :16/12/2003        Release 2.0 
Date :28/04/2004        MISRA changes                             
******************************************************************************/
#ifndef SCI_H
#define SCI_H  

#include "ST7lib_Config.h" 

                 /* Description of all the functions defined in this module. */
                                           /*Parameters for Control Register1*/

#define SCI_ErrValue 0x0f                         /*Error bits in SR register*/

typedef enum{
    SCI_DEFAULT_PARAM1      = (unsigned char) 0x00,     
    SCI_WORDLENGTH_9        = (unsigned char) 0x10,
    SCI_ODPARITY_SELECT     = (unsigned char) 0x06,
    SCI_EVPARITY_SELECT     = (unsigned char) 0x04,
    SCI_WAKEUP_ADDR         = (unsigned char) 0x08
    }SCI_Type_Param1;

typedef enum {                             /*Parameters for Control Register2*/
    SCI_MUTE_ENABLE            = (unsigned char) 0x02, 
    SCI_BREAK_ENABLE        = (unsigned char) 0x01,
    SCI_DEFAULT_PARAM2      = (unsigned char) 0x00
    }SCI_Type_Param2;

typedef enum {
    SCI_PR_1    = (unsigned char) 0x00,    /*parameters for baudrate
                                                                     register*/
    SCI_PR_3    = (unsigned char) 0x40,    
    SCI_PR_4    = (unsigned char) 0x80,
    SCI_PR_13    = (unsigned char) 0xc0,
    SCI_TR_1    = (unsigned char) 0x00,
    SCI_TR_2    = (unsigned char) 0x08,
    SCI_TR_4    = (unsigned char) 0x10,
    SCI_TR_8    = (unsigned char) 0x18,
    SCI_TR_16    = (unsigned char) 0x20,
    SCI_TR_32    = (unsigned char) 0x28,
    SCI_TR_64    = (unsigned char) 0x30,
    SCI_TR_128  = (unsigned char) 0x38,
    SCI_RR_1    = (unsigned char) 0x00,
    SCI_RR_2    = (unsigned char) 0x01,
    SCI_RR_4    = (unsigned char) 0x02,
    SCI_RR_8    = (unsigned char) 0x03,
    SCI_RR_16    = (unsigned char) 0x04,
    SCI_RR_32    = (unsigned char) 0x05,
    SCI_RR_64    = (unsigned char) 0x06,
    SCI_RR_128  = (unsigned char) 0x07
    }SCI_Baudrate_Type;
                   
typedef enum {                           /*Parameters for enabling interrupts*/
        SCI_IDLE_LINE               = (unsigned char) 0x10,
        SCI_RECEIVE_OVERRUN         = (unsigned char) 0x20,
        SCI_TRANSMIT_REGISTER_READY = (unsigned char) 0x80,
        SCI_FRAME_TRANSMITTED       = (unsigned char) 0x40,
        SCI_PARITY_ERROR            = (unsigned char) 0x01
    }SCI_IT_Type;

typedef enum {                                 /*Parameters for enabling mode*/
    SCI_TX_ENABLE        = (unsigned char) 0x08,
    SCI_RX_ENABLE        = (unsigned char) 0x04
         }SCI_Mode_Type;

typedef enum {                                                  /*Error Types*/
    SCI_BUFFER_ONGOING      = (unsigned char) 0xff,
    SCI_STRING_ONGOING      = (unsigned char) 0xfe,
    SCI_NOISE_ERR           = (unsigned char) 0x04,
    SCI_OVERRUN_ERR         = (unsigned char) 0x08,
    SCI_FRAMING_ERR         = (unsigned char) 0x02,
    SCI_PARITY_ERR          = (unsigned char) 0x01,
    SCI_RECEIVE_OK          = (unsigned char) 0x00,
    SCI_RX_DATA_EMPTY       = (unsigned char) 0xfd
        }SCI_RxError_t;

#ifdef SCI_72F561     
/*  Parameters for configuring Polarity and Phase for CLK out in 561 device  */
typedef enum {        
        SCI_PO_LOW_PH_LOW   = (unsigned char)0x00,
        SCI_PO_LOW_PH_HIGH  = (unsigned char)0x02,
        SCI_PO_HIGH_PH_LOW  = (unsigned char)0x04,
        SCI_PO_HIGH_PH_HIGH = (unsigned char)0x06
        }SCI_PO_PH_t;

/********************** Parameters for enabling CLK at last bit ***************/
typedef enum {
        SCI_LBCL_DISABLE = (unsigned char)0x00,
        SCI_LBCL_ENABLE  = (unsigned char)0x01
        }SCI_LBCL_t;
        
#endif 

/*****************************MACRO definitions********************************/       

/*****************For selecting Transmitter or Receiver mode ******************/
#define SCI_Mode(SCI_Mode_Param)  ((SCICR2) |= (SCI_Mode_Param));

/************************* For enabling SCI interrupts ***********************/
#define SCI_IT_Enable(SCI_IT_Param) \
(SCICR1 |= (unsigned char)((SCI_IT_Param)&(SCI_PARITY_ERROR)));   \
(SCICR2 |= (unsigned char)((SCI_IT_Param)&(~(unsigned char)SCI_PARITY_ERROR))); 

/*************************** For Selecting the Baudrate **********************/
#define SCI_Select_Baudrate(Baudrate_Presc)  (SCIBRR =0x00);\
(SCIBRR) |= (Baudrate_Presc);

#ifdef SCI_72F561

#define SCI2_Mode(SCI_Mode_Param)  ((SCI2CR2) |= (SCI_Mode_Param);)  


/**************************** For enabling SCI interrupts ********************/
#define SCI2_IT_Enable(SCI_IT_Param)\
(SCI2CR1 |= (unsigned char)((SCI_IT_Param)&(SCI_PARITY_ERROR)));   \
(SCI2CR2 |= (unsigned char)((SCI_IT_Param)&(~(unsigned char)SCI_PARITY_ERROR)));


/***************************** For Selecting the Baudrate ********************/
#define SCI2_Select_Baudrate(Baudrate_Presc)\
    (SCI2BRR =0x00);\
    ((SCI2BRR) |= (Baudrate_Presc));  


#define SCI2_Clkout_Enable() (SCI2CR3 | 0x80) 

/********************For configuring the CLK output in 561 device*************/
#define SCI2_ClkConfigure(SCI_PO_PH_Param, SCI_LBCL_Param)\
    (SCI2CR3 |= ((SCI_PO_PH_Param) | (SCI_LBCL_Param)));

       

#endif
/***************************Function declarations*****************************/
void SCI_Init (SCI_Type_Param1,SCI_Type_Param2);      /*Initialises the SCI*/

void SCI_Forced_Clear_Flag(void);                      /*Clears all the flags*/

/* 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);
                                              /*Computes the desired Baudrate*/
#endif /* REM_SCI_COMPUTE_BAUDRATE */

#if (defined SCI_72F521 || defined SCI_72F62 || defined SCI_LITE3 || \
                                       defined SCI_72F264 || defined SCI_72F561)
void SCI_Extend_Baudrate(SCI_Baudrate_Type,unsigned char,unsigned char); 
              /*Selects the desired baudrate with extended prescalar register*/
#endif


void SCI_IT_Disable ( SCI_IT_Type );    /*Disables the desired the interrupt */

void SCI_PutByte(unsigned char);               /*Transmits a single data byte*/

BOOL SCI_IsTransmitCompleted( void);    /*Checks if transmission is completed*/

void SCI_PutBuffer(const unsigned char*,unsigned char); 
                                                    /*Transmits a user buffer*/

void SCI_PutString(const unsigned char*);                /*Transmits a string*/

BOOL SCI_9thBit_TxRx(BOOL);                   /*Transmits or receives 9th bit*/

unsigned char SCI_GetByte( void);               /*Receives a single data byte*/

#ifdef SCI_POLLING_RX
SCI_RxError_t SCI_GetBuffer(unsigned char*,unsigned char);    
    /*Receives continuous data into the user buffer and returns error status */
SCI_RxError_t SCI_GetString ( unsigned char*);    
  /*Receives a string in the user buffer and returns error status to the user*/
#endif

#ifdef SCI_ITDRV_WITHOUTBUF_RX
void SCI_GetBuffer (unsigned char*,unsigned char);        
            /*Starts the reception in user buffer in the interrupt subroutine*/
void SCI_GetString (unsigned char*);                 
   /*Starts reception of a string  in user buffer in the interrupt subroutine*/
#endif

SCI_RxError_t SCI_IsReceptionCompleted(void);    
              /*Checks if reception is completed and returns the error status*/

void SCI_IT_Function (void);
                         /*Carries out communication in Interrupt Driven mode*/


/**********************Function for SCI2 in 72F561****************************/
#ifdef SCI_72F561

void SCI2_Init ( SCI_Type_Param1,SCI_Type_Param2 );     /*Initialises the SCI*/

void SCI2_Forced_Clear_Flag(void);                     /*Clears all the flags*/

/* RBuilder can override this function by adding 
                                            #define REM_SCI_COMPUTE_BAUDRATE */
#ifndef REM_SCI_COMPUTE_BAUDRATE
void SCI2_Compute_Baudrate(unsigned int BaudRate_Tx,unsigned int BaudRate_Rx);
                                              /*Computes the desired Baudrate*/
#endif /* REM_SCI_COMPUTE_BAUDRATE */

void SCI2_Extend_Baudrate(SCI_Baudrate_Type,unsigned char,unsigned char); 
              /*Selects the desired baudrate with extended prescalar register*/

void SCI2_IT_Disable ( SCI_IT_Type );    /*Disables the desired the interrupt*/
                                         

void SCI2_PutByte(unsigned char);              /*Transmits a single data byte*/

BOOL SCI2_IsTransmitCompleted( void);   /*Checks if transmission is completed*/

void SCI2_PutBuffer(const unsigned char*,unsigned char); 
                                                    /*Transmits a user buffer*/

void SCI2_PutString(const unsigned char*);               /*Transmits a string*/

BOOL SCI2_9thBit_TxRx(BOOL);                  /*Transmits or receives 9th bit*/

unsigned char SCI2_GetByte( void);              /*Receives a single data byte*/

#ifdef SCI2_POLLING_RX
SCI_RxError_t SCI2_GetBuffer(unsigned char*,unsigned char);    
    /*Receives continuous data into the user buffer and returns error status */
SCI_RxError_t SCI2_GetString ( unsigned char*);    
  /*Receives a string in the user buffer and returns error status to the user*/
#endif

#ifdef SCI2_ITDRV_WITHOUTBUF_RX
void SCI2_GetBuffer (unsigned char*,unsigned char);        
            /*Starts the reception in user buffer in the interrupt subroutine*/
void SCI2_GetString (unsigned char*);                 
   /*Starts reception of a string  in user buffer in the interrupt subroutine*/
#endif

SCI_RxError_t SCI2_IsReceptionCompleted(void);    
              /*Checks if reception is completed and returns the error status*/

void SCI2_IT_Function (void);
                         /*Carries out communication in Interrupt Driven mode*/
#endif                         
#endif

/**** (c) 2002   STMicroelectronics *************************** END OF FILE **/
