/*
*******************************************************************************
COPYRIGHT 2003 STMicroelectronics
Source File Name : lart.c
Group            : IPSW,CMG-IPDF
Author           : MCD Application Team
Date First Issued: 15/03/2002 
********************************Documentation**********************************
General Purpose - This file contains source code for all the functions.
********************************RevisionHistory********************************
_______________________________________________________________________________
Date :15/3/2002       Release:1.0 	            
Date :23/9/2003       Release:2.0
                      1. Added new functions for Input Capture, Break generate
                         for Lite1,Lite2,Lite3 and DALI
                      2. Added new function for counter 2 use, Long input 
                         capture and deadtime generation for Lite3
Date : 27/05/2004     Release:2.1 
                      1.Changed for MISRA compliance
******************************************************************************/
#include "ST7lib_config.h"                      /*  selection of device used */
#include "lart_hr.h"                   /* Declaration of the Bit definitions */
#include "lart.h"                    /* Declaration of the all the functions */

                           /* Declare array for indirect accessing registers */
#if(defined LART_LITE1 || defined LART_LITE2 || defined LART_LITE3 || \
                                                             defined LART_DALI)
const static unsigned char PWMPinEnable[] = {PWM0_OUT_ENB, PWM1_OUT_ENB,
                                             PWM2_OUT_ENB, PWM3_OUT_ENB};

#ifdef _HIWARE_
#pragma INTO_ROM
static volatile unsigned char * const PWMPolarityReg[] = {&PWM0CSR, &PWM1CSR, 
                                                            &PWM2CSR,&PWM3CSR}; 
#pragma INTO_ROM
static volatile unsigned int * const PWMDCRReg[]   = {&DCR0, &DCR1, &DCR2, &DCR3}; 
#endif

#ifdef _COSMIC_
@tiny static unsigned char * const PWMPolarityReg[] = {&PWM0CSR,&PWM1CSR, 
                                                          &PWM2CSR, &PWM3CSR}; 
@tiny static unsigned int * const PWMDCRReg[]   = {&DCR0, &DCR1, &DCR2, 
                                                        &DCR3}; 
#endif

#ifdef _RAISONANCE_
static hreg * code PWMPolarityReg[] = {&PWM0CSR, &PWM1CSR, 
                                                            &PWM2CSR,&PWM3CSR}; 
static hreg16 * code PWMDCRReg[]   = {&DCR0, &DCR1, &DCR2, &DCR3}; 
#endif

#endif
                                            
/*-----------------------------------------------------------------------------
 ROUTINE NAME : LART_Init
 INPUT        : InitValue : Initialization mode
                LART_COUNTER_CLK_FLT,LART_COUNTER_CLK_FCPU,LART_OCMP_IT_ENABLE,
                LART_OVF_IT_ENABLE,LART_DEFAULT
 OUTPUT       : None.
 DESCRIPTION  : LART initialisation routine.
 COMMENTS     : One or more parameters can be passed by  ORing them together.
-----------------------------------------------------------------------------*/
void LART_Init(Lart_InitParameter InitValue) 
{
    unsigned char Temp;
    
    PWM0CSR = LART_DEFAULT;
//    DCR0H   = LART_DEFAULT;     

//    DCR0L   = LART_DEFAULT;     
//    PWMCR   = LART_DEFAULT;        
Nop
Nop
Nop
    ATCSR   = (unsigned char)InitValue;     /* change default configuration  */
    Temp    = ATCSR;                                         /* Clear flags  */
    Temp    = PWM0CSR;

#ifdef LART_LITE3
    ATR1H    = LART_DEFAULT;
    ATR1L    = LART_DEFAULT;
    ATR2H    = LART_DEFAULT;
    ATR2L    = LART_DEFAULT;
#else
    ATRH    = LART_DEFAULT;
    ATRL    = LART_DEFAULT;
#endif /* LART_LITE3 */

/* Initialize registers available on other devices */
#if(defined LART_LITE1 || defined LART_LITE2 || defined LART_LITE3 || \
                                                            defined LART_DALI)
    PWM1CSR = LART_DEFAULT;
    PWM2CSR = LART_DEFAULT;
    PWM3CSR = LART_DEFAULT;
    DCR1H   = LART_DEFAULT;  
    DCR1L   = LART_DEFAULT;
    DCR2H   = LART_DEFAULT;
    DCR2L   = LART_DEFAULT;
    DCR3H   = LART_DEFAULT;
    DCR3L   = LART_DEFAULT;
    BREAKCR = LART_DEFAULT;    
    Temp    = ATICRL;                        /* Clear the Input Capture Flag */
                                                   /*Clear PWM compare flags */
    Temp    = PWM1CSR;
    Temp    = PWM2CSR;
    Temp    = PWM3CSR;

#ifdef LART_LITE3
    ATCSR2  = ((unsigned char)TRAN1_ENB | (unsigned char)TRAN2_ENB);
    DTGR    = LART_DEFAULT;
    Temp    = ATCSR2;                                     /* Clear OVF2 flag */   
#else
    TRANCR  = TRAN_ENB;
#endif

#endif  
  
} 
/* ----------------------------------------------------------------------------
 ROUTINE NAME : LART_Disable
 INPUT        : DValue: Feature to be disabled
                LART_CMPF0_IT_DISABLE,LART_OVF_IT_DISABLE,LART_COUNTER_CLK_OFF 
                LART_PWM_Disable
                For devices ST7FLite1,ST7FLite2, ST7FLite3 and ST7DALI
                LART_PWM0_Disable,LART_PWM1_DISABLE,LART_PWM2_DISABLE,
                LART_PWM3_DISABLE,LART_ICAP_IT_DISABLE                                  
                For ST7FLite3
                LART_CTR2_DISABLE,LART_OVF2_IT_DISABLE,LART_LONG_ICAP_DISABLE
 OUTPUT       : None
 DESCRIPTION  : Disables ICAP,OCMP,OVF interrupts, PWM or turns off counter 
                clock 
 COMMENTS     : Using LART_LONG_ICAP_DISABLE may affect functionality of LT 
                Input Capture as it disables the ICAP interrupt temporarily and
                clears the ICF flag
 ----------------------------------------------------------------------------*/
void LART_Disable(Lart_Disable_Param DValue)
{   
    unsigned char ITShadow = 0x00,Temp;
  
    switch(DValue)
    {
        case LART_OVF_IT_DISABLE:
             ATCSR &= (unsigned char)~(unsigned char)OVF_IT_ENB;
             break;  
        case LART_OCMP_IT_DISABLE:
             
             ATCSR &= (unsigned char)~(unsigned char)CMP_IT_ENB;
             break;
        case LART_COUNTER_CLK_OFF:
             ATCSR &= (unsigned char)~(unsigned char)CLK_MASK;
             break;
                            /* This option is used for backward compatibelity*/
        case LART_PWM_DISABLE:   
        case LART_PWM0_DISABLE:
             PWMCR &= (unsigned char)~(unsigned char)PWM0_OUT_ENB;
             break;
        /* check for devices with additional PWMs and Input Capture function */
#if(defined LART_LITE1 || defined LART_LITE2 || defined LART_LITE3 || \
                                                             defined LART_DALI)
        case LART_PWM1_DISABLE:
             PWMCR &= (unsigned char)~(unsigned char)PWM1_OUT_ENB;
             break;
        case LART_PWM2_DISABLE:
             PWMCR &= (unsigned char)~(unsigned char)PWM2_OUT_ENB;
             break;
        case LART_PWM3_DISABLE:
             PWMCR &= (unsigned char)~(unsigned char)PWM3_OUT_ENB;
             break;                     
        case LART_ICAP_IT_DISABLE:
             ATCSR &= (unsigned char)~(unsigned char)ICAP_IT_ENB;
             break;                 
#endif

#ifdef LART_LITE3        
        case LART_CTR2_DISABLE:
             ATCSR2 &= (unsigned char)~(unsigned char)CTR2_ENABLE;
             break;
        case LART_OVF2_IT_DISABLE:
             ATCSR2 &= (unsigned char)~(unsigned char)OVF2_IT_ENB;
             break;
        case LART_LONG_ICAP_DISABLE:
        
        /* Disable Input Capture interrupts and copy them in ITShadow to enable 
           later */
             if(ATCSR & ICAP_IT_ENB)
             {
                 ATCSR &= (unsigned char)~(unsigned char)ICAP_IT_ENB;
                 ITShadow |= ICAP_IT_ENB;       
             }
             if(LTCSR1 & LT_ICAP_IT_ENB)
             {
                 LTCSR1 &= (unsigned char)~(unsigned char)LT_ICAP_IT_ENB;
                 ITShadow |= LT_ICAP_IT_ENB;
             }
                 /* Select Input capture source as LART input capture source */
             ATCSR2 &= (unsigned char)~(unsigned char)ICS_ENABLE;
        
                                                      /* Clear the ICF flags */
             Temp = ATICRL;    
             Temp = LTICR;

                                                 /* Re-enable the interrupts */
             if(ITShadow & ICAP_IT_ENB)
             {
                 ATCSR |= ICAP_IT_ENB;
             }
             if(ITShadow & LT_ICAP_IT_ENB)
             {
                 LTCSR1 |= LT_ICAP_IT_ENB;
             }       
             break;
	
           
#endif /* LART_LITE3 */

	default:	
	     break;	
    } /* switch */           
  
}                                                                                 
/*----------------------------------------------------------------------------
 ROUTINE NAME : LART_ConfigurePWM
 INPUT        : PWMChannel: PWM channel to be configured
                Autoreload_Value: Value to be loaded in ATR register
                POLARITY: Polarity of PWM output
                Dutycycle_Data: Value to be loaded in duty cycle register
 OUTPUT       : None
 DESCRIPTION  : Generates PWM signal on PWMx pin
 COMMENTS     : Frequency and dutycycle of PWM signal depends upon ATR register
                value and DCR0 register value  respectively.
 ----------------------------------------------------------------------------*/
void LART_ConfigurePWM(Lart_PWMChannel PWMChannel,unsigned int Autoreload_Value,
                     Lart_Output POLARITY, unsigned int Dutycycle_Data)
{
    volatile unsigned int *DCR;
    volatile unsigned char *CSR;
        
#ifdef LART_LITE3
    ATR1 = Autoreload_Value;                                     /* load ATR */
#else
    ATR = Autoreload_Value;                                      /* load ATR */    
#endif /* LART_LITE3 */  

#if(defined LART_LITE1 || defined LART_LITE2 || defined LART_LITE3 || \
                                                             defined LART_DALI)
                                       /* Configure the PWM Channel selected */    
    DCR = PWMDCRReg[PWMChannel];
    *DCR = Dutycycle_Data;                       /* load Duty cycle register */
    PWMCR |= (unsigned char)PWMPinEnable[PWMChannel];    /* PWM mode Enable  */
    
    CSR = PWMPolarityReg[PWMChannel];
    if(POLARITY == LART_POLARITY_0)
    {   
                                              /* PWMx signal is not inverted */
        *CSR &= (unsigned char)~(unsigned char)PWM_POLARITY; 
    }
    else
    {   
        *CSR |= PWM_POLARITY;                    /* PWMx signal is  inverted */                
    } 
    

/* Enable the transfer bit to copy the Duty cycle register to shadow register*/
#ifdef LART_LITE3
    ATCSR2  = ((unsigned char)TRAN1_ENB | (unsigned char)TRAN2_ENB);
#else
    TRANCR  = TRAN_ENB;
#endif /* LART_LITE3 */
  
#else                    /* Process for ST7FLite0 or Superlite or Superlite2, Superlite 5 */

          /*ST7FLite0 and Superlite2, Superlite 5 have only one PWM channel  */
    DCR0 =  Dutycycle_Data;                      /* load Duty cycle register */
    DCR0L |= DCR0L;                 
    PWMCR |= PWM0_OUT_ENB;                                /* PWM mode Enable */
    if(POLARITY == LART_POLARITY_0)
    {   
                                              /* PWM0 signal is not inverted */
        PWM0CSR &= (unsigned char)~(unsigned char)PWM_POLARITY; 
    }
    else
    {   
        PWM0CSR |= PWM_POLARITY;                 /* PWM0 signal is  inverted */                
    }
#endif  
  
}                                                                               


/*----------------------------------------------------------------------------
 ROUTINE NAME : LART_ConfigureOCMP
 INPUT        : OCMPChannel: OCMP channel to be configured
                Dutycycle_Data: Data to be loaded in Duty cycle register
 OUTPUT       : None
 DESCRIPTION  : Enables Output compare mode
 COMMENTS     : None
 ----------------------------------------------------------------------------*/
void LART_ConfigureOCMP(Lart_OCMPChannel OCMPChannel,
                                                   unsigned int Dutycycle_Data)
{   
    volatile unsigned int *DCR;
    
#if(defined LART_LITE1 || defined LART_LITE2 || defined LART_LITE3 || \
                                                             defined LART_DALI)
    DCR = PWMDCRReg[OCMPChannel];
    *DCR = Dutycycle_Data;                       /* load Duty cycle register */
    PWMCR &= (unsigned char)~PWMPinEnable[OCMPChannel];  /* OCMP mode Enable */
         
#else /* ST7Lite0 or Superlite or ST7Superlite2 or Superlite5 */

     /* ST7Lite0 or Superlite Or ST7Superlite2 or Superlite5 have only one output compare */ 
    DCR0 =  Dutycycle_Data;                      /* Load Duty cycle register */
    DCR0L |= DCR0L;
                   
    PWMCR &= (unsigned char)~(unsigned char)PWM0_OUT_ENB;/* OCMP mode Enable */
#endif
}


/* ---------------------------------------------------------------------------
 ROUTINE NAME : LART_Status_Flag
 INPUT        : FLAG_F: flag to be checked
                LART_FLAG_OVF,LART_FLAG_CMPF0,LART_FLAG_CMPF1,
                LART_FLAG_CMPF2,LART_FLAG_CMPF3,LART_FLAG_ICF,LART_FLAG_OVF2
 OUTPUT       : TRUE or FALSE
 DESCRIPTION  : Checks the status of a flag.
 COMMENTS     : this function can be looped till flag status changes.
 ----------------------------------------------------------------------------*/
BOOL LART_Status_Flag( Lart_Flag FLAG_F)
{           
    switch( FLAG_F )
    {
        case LART_FLAG_OVF:
             if(ATCSR & OVF_FLAG)                               /* Check OVF */
             {
                 return(TRUE);  
             }
	         else
             {
	             return(FALSE);
             }
             break;
        case LART_FLAG_CMPF0:                                 /* Check CMPF0 */
             if(PWM0CSR & CMP_FLAG) 
             {
                 return(TRUE);  
             }             
	         else
             {
	             return(FALSE);			   	  
             }
             break;
#if(defined LART_LITE1 || defined LART_LITE2 || defined LART_LITE3 || \
                                                             defined LART_DALI)
        case LART_FLAG_CMPF1:                                 /* Check CMPF1 */
             if(PWM1CSR & CMP_FLAG) 
             {
                 return(TRUE);  
             }             
	         else
             {
	             return(FALSE);			   	  
             }
             break;
        case LART_FLAG_CMPF2:                                 /* Check CMPF2 */
             if(PWM2CSR & CMP_FLAG) 
             {
                 return(TRUE);  
             }             
	         else
             {
	             return(FALSE);			   	  
             }
             break;
        case LART_FLAG_CMPF3:                                 /* Check CMPF3 */
             if(PWM3CSR & CMP_FLAG) 
             {
                 return(TRUE);  
             }             
	         else
             {
	             return(FALSE);			   	  
             }
             break;
        case LART_FLAG_ICF:                      /* Check Input Capture Flag */
             if(ATCSR & ICF_FLAG) 
             {
                 return(TRUE);  
             }             
	         else
             {
	             return(FALSE);			   	  
             }
             break; 
#endif              
#ifdef LART_LITE3
        case LART_FLAG_OVF2:                     /* Check counter 2 OVF flag */
             if(ATCSR2 & OVF2_FLAG) 
             {
                 return(TRUE);  
             }             
	         else
             {
	             return(FALSE);			   	  
             }
             break; 
#endif        
	default:	
	     break;	

    } /* switch */
    return(TRUE); 
}
/* ----------------------------------------------------------------------------
 ROUTINE NAME : LART_Clear_Flag
 INPUT        : FLAG_F: flag to be cleared
                LART_FLAG_OVF,LART_FLAG_CMPF0,LART_FLAG_CMPF1,LART_FLAG_CMPF2,
                LART_FLAG_CMPF3,LART_FLAG_ICF,LART_FLAG_OVF2 
 OUTPUT       : None
 DESCRIPTION  : clears the status flag
 COMMENTS     : flag is cleared depending upon the INPUT parameter
 ----------------------------------------------------------------------------*/
void LART_Clear_Flag(Lart_Flag FLAG_F)
{
    unsigned char Temp;
    switch( FLAG_F )
    {
        case LART_FLAG_OVF:                                      /* clear OVF*/
	         Temp = ATCSR;
	         break;
        case LART_FLAG_CMPF0:                                  /* clear CMPF0*/
	         Temp = PWM0CSR;			
	         break;
#if(defined LART_LITE1 || defined LART_LITE2 || defined LART_LITE3 || \
                                                              defined LART_DALI)
        case LART_FLAG_CMPF1:                                  /* clear CMPF1*/
	         Temp = PWM1CSR;			
	         break;
        case LART_FLAG_CMPF2:                                  /* clear CMPF2*/
	         Temp = PWM2CSR;			
	         break;
        case LART_FLAG_CMPF3:                                  /* clear CMPF3*/
	         Temp = PWM3CSR;			
	         break;
        case LART_FLAG_ICF:                                      /* clear ICF*/
	         Temp = ATICRL;			
	         break;     
                  
#endif  
#ifdef LART_LITE3
        case LART_FLAG_OVF2:                       /* Check couter 2 OVF flag*/
             Temp = ATCSR2; 
             break; 
#endif  
	default:	
	     break;	

    } /* switch */
}

 /* Following functions are not available in Lite0 & superlite, Superlite 2, Superlite5 */

#if(defined LART_LITE1 || defined LART_LITE2 || defined LART_LITE3 || \
                                                             defined LART_DALI)
/* ----------------------------------------------------------------------------
 ROUTINE NAME : LART_ICAPMode
 INPUT        : InitParams: Intialization parameters for Input Capture
                LART_ICAP_DEFAULT: Input Capture flag is cleared, input capture
                                   interrupt not enabled
                LART_ICAP_IT_ENABLE: Input Capture flag is cleared,input capture
                                   interrupt is enabled
 OUTPUT       : None
 DESCRIPTION  : This function clears ICF flag, This fucntion also enables Input
                Capture interrupt if  LART_ICAP_IT_ENABLE is passed
 COMMENTS     : Calling this function may clear OVF flag
 ----------------------------------------------------------------------------*/
void LART_ICAPMode(Lart_ICAPInitParams InitParams)
{
    unsigned char Temp;
  
    if(InitParams == LART_ICAP_IT_ENABLE)
    {
        ATCSR |= ICAP_IT_ENB;
    }
    
    Temp = ATICRL;                                     /* Clear the ICF flag */ 
}
 
/* ----------------------------------------------------------------------------
 ROUTINE NAME : LART_ConfigureBREAK
 INPUT        : Lart_BREAKPinState: Enable or disable BREAK pin fucntion
                PWMPattern: Pattern to be generated on PWM pins in BREAK state
 OUTPUT       : None
 DESCRIPTION  : This function enables/disables the BREAK pin fucntion and loads 
                the PWM bit pattern to be generared in BREAK state
 COMMENTS     : BREAK condition can be connecting a low level to BREAK pin when 
                its function is enabled or through software
 ----------------------------------------------------------------------------*/
void LART_ConfigureBREAK(Lart_BREAKPinState BREAKPinState,Lart_BREAK_PWMPattern 
                                                                    PWMPattern)
{
                  /* Load the PWM bit pattern to be generated in BREAK state */
    BREAKCR &= (unsigned char)~ (unsigned char)BREAK_PWM_MASK;
    BREAKCR = (unsigned char)((unsigned char)PWMPattern & BREAK_PWM_MASK);
  
                                     /* Enable or disable break pin function */
    if(BREAKPinState == LART_BREAK_ENABLE)
    {
        BREAKCR |= BREAK_PIN_ENABLE;    
    }
    else
    {
        BREAKCR &= (unsigned char)~(unsigned char)BREAK_PIN_ENABLE;
    }
}                                                                      
#endif

/* Following functions are available only in ST7FLite3                       */

#ifdef LART_LITE3
/* ----------------------------------------------------------------------------
 ROUTINE NAME : LART_Counter2Init
 INPUT        : InitParams: Enable or disable counter 2 overflow interrupt
                ATR2Value: Value to be loaded in autoreload register 2 which 
                will be used for counter 2
 OUTPUT       : None
 DESCRIPTION  : This function enables counter 2 which will be used for PWM2, 
                PWM3. This also enables counter 2 overflow interrupt depending 
                upon InitParams and loads the autoreload register 2
 COMMENTS     : 
 ----------------------------------------------------------------------------*/
void LART_Counter2Init(Lart_CTR2InitParams InitParams, unsigned int ATR2Value)
{
    
    ATR2 = ATR2Value;                    /* Load Autoreload register 2 value */
  
    if(InitParams == LART_CTR2_OVF_IT_ENABLE)
    {
        ATCSR2 |= ((unsigned char)CTR2_ENABLE |(unsigned char)OVF2_IT_ENB);   
    }
    else
    {                                                /* disable OVF interrupt */
        ATCSR2 &= (unsigned char)~(unsigned char)OVF2_IT_ENB;      
        ATCSR2 |= CTR2_ENABLE;
    }
}

/* ----------------------------------------------------------------------------
 ROUTINE NAME : LART_LongICAPMode
 INPUT        : InitParams: Selects which ICAP interrupt is used for long input
                capture.
                LART_SELF_ICAP_IT_ENABLE- LART ICAP interrupt is used
                LART_LT_ICAP_IT_ENABLE- LT ICAP interrupt is used
                LART_ICAP_NO_IT_ENABLE- NO interrupt is used for ICAP
 OUTPUT       : None
 DESCRIPTION  : This fucntion enables the long input capture function of LART. 
                This function selects clock source as LT output, Input capture 
                source as LT input capture and interrupt source as specified in 
                InitParams.
 COMMENTS     : LT Input Capture mode can't be used independently when this 
		mode  is used. The clock source of LART is also changed which 
				may affect other functionalities of LART module
 ----------------------------------------------------------------------------*/
void LART_LongICAPMode(Lart_LongICAPInitParams InitParams)
{
    unsigned char Temp;
  
                              /* Change the clock input to Lite timer output */
    ATCSR &= (unsigned char)~(unsigned char)CLK_MASK;
    ATCSR |= LART_COUNTER_CLK_FLT;
                                     /* Disable LART Input Capture interrupt */
    ATCSR &= (unsigned char)~(unsigned char)ICAP_IT_ENB;       
                                       /* Disable LT Input Capture interrupt */
    LTCSR1 &= (unsigned char)~(unsigned char)LT_ICAP_IT_ENB;
  
                   /* Select Input capture source as LT input capture source */
    ATCSR2 |= ICS_ENABLE;
  
    /* Clear the ICF flag on both LT and LART because a spurious signal may be 
    generated when selecting the input capture source using ICS bit */
    Temp = ATICRL;
    Temp = LTICR;
  
                                           /* Enable required ICAP interrupt */
    if(InitParams == LART_SELF_ICAP_IT_ENABLE)
    {
        ATCSR |= ICAP_IT_ENB;    
    }
    else
        if(InitParams == LART_LT_ICAP_IT_ENABLE)
        {
            LTCSR1 |= LT_ICAP_IT_ENB;
        }
  
     
}

/* ----------------------------------------------------------------------------
 ROUTINE NAME : LART_LongICAPGetValue
 INPUT        : None Parameters are passed as reference to get the output 
                results
 OUTPUT       : LTValue: This parameter contain value of Lite Timer Input 
                         capture register
                LARTValue: This parameter contain value of LART input capture
                         register         
 DESCRIPTION  : This fucntion is used to read input capture regisres of LT and
                LART in long input capture mode.
 COMMENTS     : The parameters are passed as pointer and result is returned 
                through arguments of function.
                ICF flags will be cleared
 ----------------------------------------------------------------------------*/
void LART_LongICAPGetValue(unsigned char *LTValue,unsigned int *LARTValue)
{
    *LTValue = LTICR;
    *LARTValue = ATICR;
}

#endif /* LART_LITE3 */
/************** (c) 2002  ST Microelectronics ***************** END OF FILE **/




