/*
*******************************************************************************
COPYRIGHT 2003 STMicroelectronics
Source File Name : lt.c
Group            : IPSW,CMG-IPDF
Author           : MCD Application Team
Date First Issued: 8/3/2002    
********************************Documentation**********************************
General Purpose - This file contains source code for all the LT functions.
********************************RevisionHistory********************************
_______________________________________________________________________________
Date:8/3/2002       Release:1.0
Date :23/9/2003     Release:2.0 	       
                    1. Included necessary comments.
                    2. Removed the description of the functions which are 
                       changed to Macros.
                    3. Changed the magic numbers to Constants.
                    4. Modified the code to support full LITE family & DALI.
Date :28/04/04      MISRA changes
******************************************************************************/
#include "ST7lib_config.h"                      /*  Selection of device      */
#include "lt_hr.h"             /* Declaration of constants used in the code  */
#include "lt.h"                      /* Declaration of the all the functions */

/*-----------------------------------------------------------------------------
 ROUTINE NAME : LT_Init
 INPUT        : LT_WDG_ENABLE (Enables the Watchdog)
 		LT_ICAP_IT_ENABLE (Enables the Input capture Interrupt)
 		LT_TB_IT_ENABLE (Enables the Timebase Interrupt)
 		LT_TB1_IT_ENABLE (Enables the Timebase1 Interrupt)
 		LT_TB2_IT_ENABLE (Enables the Timebase2 Interrupt)
 		LT_DEFAULT (Configures the LT as default)
 OUTPUT       : None.
 DESCRIPTION  : LT initialisation routine.
 COMMENTS     : One or more parameters can be passed by  ORing them together.
-----------------------------------------------------------------------------*/
 void LT_Init(Lt_InitParameter InitValue) 
{      
    unsigned char  Temp; 
    
#if (defined LT_LITE0||defined LT_LITES2_5 || defined LT_LITES0)
    /* Initialise the C/S Register with Init Value*/
    LTCSR   = (unsigned char)InitValue;  
#endif
#if (defined LT_LITE1||defined LT_LITE2||defined LT_LITE3||defined LT_DALI)
    LTCSR2  = LT_DEFAULT;                        /* Clear the C/S Register 2 */
    if(InitValue & LT_TB2_IT_SET)
        LTCSR2 |= LT_TB2_IT_ENABLE;      /* Enable the Time Base 2 Interrupt */
    InitValue &= LT_TB2_IT_RESET;
    LTCSR1  = (unsigned char)InitValue;                /* Initialise the C/S Register 1 */
#endif   
#ifndef LT_LITES0                           
    Temp    = LTICR;                                   /* Clear the ICF flag */
#endif
} 
/* ----------------------------------------------------------------------------
 ROUTINE NAME : LT_TB
 INPUT        : LT_SET_TB_1 (Sets the Timebase 1)
 				LT_SET_TB_2 (Sets the Timebase 2)
 OUTPUT       : None
 DESCRIPTION  : Selects the Timebase
 COMMENTS     : None
 ----------------------------------------------------------------------------*/
void LT_TB(Lt_TB_Param TBValue)                                                            
{
    if ( TBValue == LT_SET_TB_1)
    {
        #if (defined LT_LITE0 || defined LT_LITES2_5)
            LTCSR  &= LT_TB_1_SET;               /* Set Timebase 1ms   */
        #endif
        #if (defined LT_LITE1 || defined LT_LITE2 || defined LT_LITE3||\
        defined LT_DALI)
            LTCSR1 &= LT_TB_1_SET;
        #endif
    }
    else
    {
        #if (defined LT_LITE0 || defined LT_LITES2_5)      
            LTCSR  |= LT_TB_2_SET;                /* Set Timebase 1ms   */
        #endif
        #if (defined LT_LITE1 || defined LT_LITE2 || defined LT_LITE3||\
        defined LT_DALI)
            LTCSR1 |= LT_TB_2_SET;
        #endif                
    }
}   
/* ----------------------------------------------------------------------------
 ROUTINE NAME : LT_Disable
 INPUT        : LT_WDG_DISABLE (Disables the Watchdog)
 		LT_ICAP_IT_DISABLE (Disables the interrupt for input capture)
 		LT_TB_IT_DISABLE (Disables the interrupt for timebase)
 		LT_TB1_IT_DISABLE (Disables the interrupt for timebase1)
 		LT_TB2_IT_DISABLE (Disables the interrupt for timebase2)
 OUTPUT       : None
 DESCRIPTION  : Disables ICAP,TB,TB1,TB2 interrupts or watchdog 
 COMMENTS     : Morte than one input paramters can be paassed  by logically
                ORing them together.
 ----------------------------------------------------------------------------*/
void LT_Disable(Lt_Disable_Param DValue)
{   
#if (defined LT_LITE0 || defined LT_LITES0 || defined LT_LITES2_5)
    DValue = (unsigned char)~(unsigned char)DValue;
    LTCSR  &= (unsigned char)DValue;/* Disable ICAP,TB interrupts or watchdog */
#endif
#if (defined LT_LITE1 || defined LT_LITE2 || defined LT_LITE3||\
defined LT_DALI)
    if(DValue & LT_TB2_IT_SET)
    {
        LTCSR2 &= LT_TB2_IT_RESET;
        DValue &= LT_TB2_IT_RESET;
    }   
    LTCSR1 &= (unsigned char)~DValue;
#endif      
}
/*----------------------------------------------------------------------------
 ROUTINE NAME : LT_WDG_Reset
 INPUT        : LT_DELAY_WDG_RESET (Delays the Watchdog Reset)
 		LT_FORCD_WDG_RESET (Forces the Watchdog to be reset)
 OUTPUT       : None
 DESCRIPTION  : Delay WDG reset or force WDG reset.
 COMMENTS     : None
 ----------------------------------------------------------------------------*/
#if (defined LT_LITE0 || defined LT_LITES0 || defined LT_LITES2_5)
void LT_WDG_Reset(Lt_Reset_Param WDGValue)
{
    if( WDGValue == LT_DELAY_WDG_RESET)
    {
        LTCSR |= LT_WDG_DELAY_RESET;                   /* Delay WDG reset   */
    }
    else
    {
        LTCSR |= LT_ENABLE_WDG;                         /* Force WDH reset  */
        LTCSR |= LT_WDG_FORCD_RESET;      
        Nop
        Nop                   
    }        
} 
#endif 
/* ---------------------------------------------------------------------------
 ROUTINE NAME : LT_Status_Flag
 INPUT        : LT_FLAG_TBF (Timebase Flag)
 				LT_FLAG_TB1F (Timebase1 Flag)
 				LT_FLAG_TB2F (Timebase2 Flag)
 				LT_FLAG_ICF (Input Capture Flag)
 				LT_FLAG_WDGRF (Watchdog reset Flag)
 OUTPUT       : TRUE or FALSE
 DESCRIPTION  : Checks the status of a flag.
 COMMENTS     : this function can be looped till the convertion is complete.
 ----------------------------------------------------------------------------*/
BOOL LT_Status_Flag( Lt_Flag FLAG_F)
{           
    switch( FLAG_F )
    {
        #if (defined LT_LITE0 || defined LT_LITES2_5)
        case LT_FLAG_TBF:
             if(LTCSR & LT_TBF_FLAG)                  /* Check TBF */
             {
                 return(TRUE);  
             }
             else
             {
                 return(FALSE);
             }
        break;
        #endif
        #if (defined LT_LITE1 || defined LT_LITE2 || defined LT_LITE3||\
        defined LT_DALI)
        case LT_FLAG_TB1F:
             if(LTCSR1 & LT_TB1F_FLAG)		       /* Check TB1F */
             {
                 return(TRUE);
             }
             else
             {
                 return(FALSE);
             }
        break;
        #endif        
        #if (defined LT_LITE1 || defined LT_LITE2 || defined LT_LITE3||\
        defined LT_DALI)      
        case LT_FLAG_TB2F:    
	         if(LTCSR2 & LT_TB2F_FLAG)						   /* Check TB2F */
             {
                 return(TRUE);
             }
             else
             {
                 return(FALSE);
             }               
        break;
        #endif
        #if (defined LT_LITE0||defined LT_LITES2_5)
        case LT_FLAG_ICF:                                    /* Check ICF */
             if(LTCSR & LT_ICF_FLAG) 
             {
                 return(TRUE);  
             }
             else
             {
                 return(FALSE);			   	  
             }
             break;
        #endif
        #if (defined LT_LITE1||defined LT_LITE2 ||defined LT_LITE3||\
                                                            defined LT_DALI)
        case LT_FLAG_ICF:
             if(LTCSR1 & LT_ICF_FLAG)
             {
                 return(TRUE);
             }
             else
             {
                 return(FALSE);
             }
             break;
        #endif 
        #if (defined LT_LITE0 || defined LT_LITES2_5 || defined LT_LITES0)
        case LT_FLAG_WDGRF:
             if(LTCSR & LT_WDGRF_FLAG)                        /* Check WDGRF */
             {
                 return(TRUE);  
             }
             else
             {
                 return(FALSE);
             }
             break;      
        #endif
	default:	
	     break;	
    }
             return(TRUE);   
}
/* ----------------------------------------------------------------------------
 ROUTINE NAME : LT_Clear_Flag
 INPUT        : FLAG_TBF (Timebase Flag)
				FLAG_TB1F (Timebase1 Flag)
				FLAG_TB2F (Timebase2 Flag) 
 				FLAG_ICF (Input capture Flag)
 				FLAG_WDGRF (Watchdog Reset Flag)
 OUTPUT       : None
 DESCRIPTION  : clears the status flag
 COMMENTS     : flag is cleared depending upon the INPUT parameter
 ----------------------------------------------------------------------------*/
void LT_Clear_Flag(Lt_Flag FLAG_F)
{
    unsigned char Temp; 
      
    switch( FLAG_F )
	{
        #if (defined LT_LITE0 || defined LT_LITES2_5)
        case LT_FLAG_TBF:                                       /* Clear TBF */
             Temp = LTCSR;            
             break;                                
        #endif
        #if (defined LT_LITE1||defined LT_LITE2 ||defined LT_LITE3||\
        defined LT_DALI)
        case LT_FLAG_TB1F:									   /* Clear TB1F */
             Temp = LTCSR1;    
             break;                                                         
        #endif
        #if (defined LT_LITE1||defined LT_LITE2 ||defined LT_LITE3||\
        defined LT_DALI)
        case LT_FLAG_TB2F:									   /* Clear TB2F */
             Temp = LTCSR2;
            break;
        #endif            
        #ifndef LT_LITES0
        case LT_FLAG_ICF:                                       /* Clear ICF */
             Temp = LTICR;			
             break;
        #endif
        #if (defined LT_LITE0 || defined LT_LITES2_5 || defined LT_LITES0)
        case LT_FLAG_WDGRF:                                   /* Clear WDGRF */
             Temp = LTCSR;
             break;      
        #endif  
	default:	
	     break;     
    }
}  

/************** (c) 2003  ST Microelectronics ***************** END OF FILE **/
