/*
*******************************************************************************
COPYRIGHT 2002 STMicroelectronics
Source File Name : pwmart.c
Group            : IPSW,CMG-IPDF
Author           : MCD Application Team
Date First Issued: 5/03/2002    
********************************Documentation**********************************
General Purpose - This file contains source code for all the functions.
********************************RevisionHistory********************************
_______________________________________________________________________________
Date : 5/03/2002    Release : 1.0 
Date : 23/09/2003   Release : 2.0  
                    1.Added support for ST72F561 device
                    2.Removed magic numbers and placed them in pwmart_hr.h
                    3.Added indirect register access and other array based 
                      approach to optimize code size 
       01/06/2004   Release : 2.1
                    1.Changed for MISRA compliance                    
*******************************************************************************/
#include "ST7lib_config.h"                             /* Selection of device */
#include "pwmart_hr.h"                    /* PWMART registers bit definitions */
#include "pwmart.h"                   /* Declaration of the all the functions */

#if (defined  PWMART_72F521 || defined PWMART_72F561)

const static unsigned char PWMPinEnable[] = {ENABLE_OE0,ENABLE_OE1,ENABLE_OE2,
                                             ENABLE_OE3};
const static unsigned char PWMPolarity[] = {POLARITY_PWM0,POLARITY_PWM1,
                                            POLARITY_PWM2,POLARITY_PWM3}; 
#ifdef _HIWARE_
#pragma INTO_ROM
static volatile unsigned char * const PWMDCRReg[]   = {&PWMDCR0,&PWMDCR1,&PWMDCR2,
                                            &PWMDCR3};
#endif
#ifdef _COSMIC_
@tiny static unsigned char * const PWMDCRReg[] = {&PWMDCR0,&PWMDCR1,
                                                        &PWMDCR2,&PWMDCR3};
#endif 
#ifdef _RAISONANCE_
static hreg * code PWMDCRReg[]   = {&PWMDCR0,&PWMDCR1,&PWMDCR2,
                                            &PWMDCR3};
#endif
#endif

#if (defined  PWMART_72F62)

const static unsigned char PWMPinEnable[] = {ENABLE_OE0,ENABLE_OE1};
const static unsigned char PWMPolarity[] = {POLARITY_PWM0,POLARITY_PWM1};
 
#ifdef _HIWARE_
#pragma INTO_ROM
static volatile unsigned char * const PWMDCRReg[]   = {&PWMDCR0,&PWMDCR1};
#endif

#ifdef _COSMIC_
@tiny static unsigned char * const PWMDCRReg[] = {&PWMDCR0,&PWMDCR1};
#endif 

#ifdef _RAISONANCE_
static hreg * code PWMDCRReg[]   = {&PWMDCR0,&PWMDCR1};
#endif

#endif                                          
/*-----------------------------------------------------------------------------
 ROUTINE NAME : PWMART_Init
 INPUT        : InitValue: Initialization parameters
                PWMART_DEFAULT- Reset value
                PWMART_EXCLK- Enable external interrupt
                PWMART_OVF_IT_ENABLE- enable counter overflow interrupt 
 OUTPUT       : None.
 DESCRIPTION  : PWMART initialization routine.
 COMMENTS     : One or more parameters can be passed by ORing them together.
------------------------------------------------------------------------------*/
void PWMART_Init(Typ_Pwmart_InitParameter InitValue) 
{
    unsigned char Temp;

    ARTARR  = PWMART_DEFAULT;
    PWMCR   = PWMART_DEFAULT;
    ARTCSR  = (unsigned char)InitValue;   /* Change default configuration    */
    PWMDCR0 = PWMART_DEFAULT;
    PWMDCR1 = PWMART_DEFAULT;
#if (defined PWMART_72F521 || defined PWMART_72F561)
    PWMDCR2 = PWMART_DEFAULT;
    PWMDCR3 = PWMART_DEFAULT;
#endif
    ARTICCSR= PWMART_DEFAULT;
    ARTCAR  = PWMART_DEFAULT;
    Temp    = ARTICR1;                       /* Read registers to clear flags */
    Temp    = ARTICR2;
    Temp    = ARTCSR;
} 
/* ----------------------------------------------------------------------------
 ROUTINE NAME : PWMART_Counter_Enable
 INPUT        : SELECT_REG: Register to be reloaded CAR or ARR
                Counter_Data: Data to be loaded in register
                SELECT_CLK: Timer clock prescaler
 OUTPUT       : None
 DESCRIPTION  : Initialses the counter and selects counter frequency.
 COMMENTS     : None
 -----------------------------------------------------------------------------*/
void PWMART_Counter_Enable(Pwmart_Counter SELECT_REG,unsigned char Counter_Data,
                                                        Pwmart_Clock SELECT_CLK)
{ 
    ARTCSR &= PRES_MASK;                                   /* Clear prescalar */ 
    
    if(SELECT_REG == PWMART_REG_CAR)
    {
        ARTCAR = Counter_Data;                 /* Load counter value in CAR   */
    }
    else
    {         
        ARTARR = Counter_Data;                  /* Load counter value in ARR  */
        ARTCSR |= FORCE_RELOAD;                                   /* Set FCRL */
    }
    
    ARTCSR |= (unsigned char)SELECT_CLK;               /* Set Clock prescalar */

    ARTCSR |= COUNTER_ENABLE;                    /* Set TCE to enable counter */
}


/* ----------------------------------------------------------------------------
 ROUTINE NAME : PWMART_OCMP_Mode
 INPUT        : OCMP: output compare register to be used
                POLARITY: Polarity of OCMP pin
                Compare_Data: Data to be loaded in DCR register 
 OUTPUT       : None
 DESCRIPTION  : configures PWMART in output compare mode 
 COMMENTS     : User has to call this function more than once to enable 
                several output compare
 -----------------------------------------------------------------------------*/
void PWMART_OCMP_Mode(Pwmart_Compare OCMP,Pwmart_Output POLARITY,
                                                   unsigned char Compare_Data)
{
    volatile unsigned char *DCR;
        
    DCR = PWMDCRReg[OCMP];
    *DCR = Compare_Data;                                /* Load DCRx register */
    
    PWMCR |= PWMPinEnable[OCMP];                                /* Enable OEx */
    if(POLARITY == PWMART_POLARITY_0)                  /* Select polarity OPx */
    {
        PWMCR &= (unsigned char)~PWMPolarity[OCMP];
    }
    else
    {
        PWMCR |= PWMPolarity[OCMP];	 	
    }
}


/*----------------------------------------------------------------------------
 ROUTINE NAME : PWMART_OCMP_Timebase
 INPUT        : Tinput: value in nanosecond 1/fCPU
                Time: time in nanosecond
                SELECT_CLK: Timer clock prescaler
 OUTPUT       : None
 DESCRIPTION  : output compare is used as time base interrupt
 COMMENTS     : Time and Tinput should be passed in nanosecond.
 -----------------------------------------------------------------------------*/
void PWMART_OCMP_Timebase(double Tinput,double Time,Pwmart_Clock SELECT_CLK)
{
    double Tcounter;
    int Temp, ClockPresc;
    
                                                 /* Calculate Prescalar value */
    switch( SELECT_CLK )
    {
        case PWMART_CLK_1:                               /* fcounter = finput */
             ClockPresc = 1;   
             break;
        case PWMART_CLK_2:                             /* fcounter = finput/2 */
             ClockPresc = 2;
             break;
        case PWMART_CLK_4:                             /* fcounter = finput/4 */
             ClockPresc = 4;
             break;
        case PWMART_CLK_8:                             /* fcounter = finput/8 */
             ClockPresc = 8;
             break;
        case PWMART_CLK_16:                           /* fcounter = finput/16 */
             ClockPresc = 16;
             break;
        case PWMART_CLK_32:                           /* fcounter = finput/32 */
             ClockPresc = 32;
             break;
        case PWMART_CLK_64:                           /* fcounter = finput/64 */
             ClockPresc = 64;
             break;
        case PWMART_CLK_128:                         /* fcounter = finput/128 */
             ClockPresc = 128;
             break;
	default:	
	         /* error case: same as PWMART_CLK_1 */
             ClockPresc = 1;
             break;
    }
        
    Tcounter = Tinput * ClockPresc;           /* Calculate timer clock period */
                                                /* Load ARTARR register value */
    Temp     =   (int)(255 - (int)(Time / Tcounter));       
    ARTARR   =   (unsigned char)Temp;

    ARTCSR  |= FORCE_RELOAD;                                      /* Set FCRL */ 
    ARTCSR  &= PRES_MASK;                                  /* Clear prescalar */

    ARTCSR |= (unsigned char)SELECT_CLK;            /* Select Clock Prescalar */

                                     /*  Enable counter & overflow interrupt  */  
    ARTCSR |= (((unsigned char)COUNTER_ENABLE | ((unsigned char)
                                                          OVERFLOW_INTERRUPT))); 
}


/* ----------------------------------------------------------------------------
 ROUTINE NAME : PWMART_PWM_Mode
 INPUT        : SELECT_Pin: PWM port pin
                POLARITY: Polarity of PWM pin
                Dutycycle_Data: data to be loaded in corresponding OCR register 
 OUTPUT       : None
 DESCRIPTION  : configures PWMART in PWM mode 
 COMMENTS     : User has to call this function more than once to generate 
                several PWM signals
 -----------------------------------------------------------------------------*/
void PWMART_PWM_Mode(Pwmart_Pwm SELECT_Pin,Pwmart_Output POLARITY,
                                                unsigned char Dutycycle_Data)
{
    volatile unsigned char *DCR;
    
    DCR = PWMDCRReg[SELECT_Pin];
    *DCR = Dutycycle_Data;                              /* Load DCRx register */
    
    PWMCR |= PWMPinEnable[SELECT_Pin];                          /* Enable OEx */
    if(POLARITY == PWMART_POLARITY_0)                  /* Select polarity OPx */
    {
        PWMCR &= (unsigned char)~PWMPolarity[SELECT_Pin];
    }
    else
    {
        PWMCR |= PWMPolarity[SELECT_Pin];	 	
    }
}


/* ----------------------------------------------------------------------------
 ROUTINE NAME : PWMART_ICAP_Mode
 INPUT        : ICAP_I: Input capture channel
                SENS_IT_Value: Input capture sensitivity
 OUTPUT       : None
 DESCRIPTION  : Configures PWMART in input capture mode
 COMMENTS     : function should be called twice for ICAP1 and ICAP2 both
 -----------------------------------------------------------------------------*/
void PWMART_ICAP_Mode(Pwmart_Icap ICAP_I,Pwmart_Sens_IT_Param SENS_IT_Value)
{   
    unsigned char Temp ;
    
    Temp = (unsigned char)SENS_IT_Value;
    
    if( ICAP_I == PWMART_ICAP_1 )
    {                   
        ARTICCSR &= (unsigned char)~(unsigned char)SENS_ICAP1;/* CS1 = 0 */
        ARTICCSR |= (unsigned char)SENS_IT_Value;    /* Define sensitivity */
    }
    else
    {   
        Temp<<=1;
        ARTICCSR &= (unsigned char)~(unsigned char)SENS_ICAP2;/* CS2 = 0 */

        ARTICCSR |= (unsigned char)Temp;             /* Define sensitivity */
    }
    
}
    	
/* ---------------------------------------------------------------------------
 ROUTINE NAME : PWMART_ICAP_Getvalue
 INPUT        : ICAP_I: Input capture channel
 OUTPUT       : returns capture register value as unsigned char										
 DESCRIPTION  : returns input capture1 or input capture2 register value   
 COMMENTS     : None
 -----------------------------------------------------------------------------*/
unsigned char PWMART_ICAP_Getvalue(Pwmart_Icap ICAP_I)
{
    if(ICAP_I == PWMART_ICAP_1)
    {
        return ARTICR1;                                /* Read ICAP1 register */ 
    }
    else
    {         
        return  ARTICR2;                               /* Read ICAP2 register */
    }
}

/* ---------------------------------------------------------------------------
 ROUTINE NAME : PWMART_Status_Flag
 INPUT        : FLAG_F: flag to be checked                
 OUTPUT       : TRUE or FALSE
 DESCRIPTION  : Checks the status of a flag.
 COMMENTS     : this function can be looped till the convertion is complete.
 -----------------------------------------------------------------------------*/
BOOL PWMART_Status_Flag( Pwmart_Flag FLAG_F)
{           
    switch( FLAG_F )
    {
        case PWMART_FLAG_OVF:
             if(ARTCSR & OVF_FLAG)                               /* Check OVF */ 
             {
                 return(TRUE);  
             }
             else
             {
                 return(FALSE);
             }
	break;
        case PWMART_FLAG_ICF1:                                  /* Check ICF1 */
             if(ARTICCSR & ICAP1_FLAG) 
             {
                 return(TRUE);  
             }
             else
             {
                 return(FALSE);			   	  
             }
	break;
        case PWMART_FLAG_ICF2:
             if(ARTICCSR & ICAP2_FLAG)                          /* Check ICF2 */
             {
                 return(TRUE);  
             }
             else
             {
                 return(FALSE);			   	  
             }
	break;
	default:	
	     break;
    }
  
    return(TRUE);
}
/* ----------------------------------------------------------------------------
 ROUTINE NAME : PWMART_Clear_Flag
 INPUT        : FLAG_F: Flag to be cleared
 OUTPUT       : None
 DESCRIPTION  : clears the status flag
 COMMENTS     : flag is cleared depending upon the INPUT parameter
 -----------------------------------------------------------------------------*/
void PWMART_Clear_Flag(Pwmart_Flag FLAG_F)
{
    unsigned char Temp;
  
    switch( FLAG_F )
    {
        case PWMART_FLAG_ICF1:                                  /* Clear ICF1 */
             Temp = ARTICR1;
             break;
        case PWMART_FLAG_ICF2:                                  /* Clear ICF2 */
             Temp = ARTICR2;			
             break;
        case PWMART_FLAG_OVF:                                    /* Clear OVF */
             Temp = ARTCSR;
             break;
	default:	
	     break;
    }
}
/* ----------------------------------------------------------------------------
 ROUTINE NAME : PWMART_Mode_Disable
 INPUT        : MODE: PWMART mode to be disabled	
 OUTPUT       : None					
 DESCRIPTION  : Disbles the counter Mode depending upon the input parameter    
 COMMENTS     : None
 -----------------------------------------------------------------------------*/
void PWMART_Mode_Disable(Pwmart_Dparam MODE)
{
    switch(MODE)
    {
        case PWMART_OVF_IT_DISABLE:                  /* Disable OVF interrupt */
             ARTCSR &= (unsigned char)~(unsigned char)OVERFLOW_INTERRUPT;
             break;
        case PWMART_ICAP1_IT_DISABLE:              /* Disable ICAP1 interrupt */
             ARTICCSR &= (unsigned char)~(unsigned char)ICAP1_INTERRUPT;
             break;
        case PWMART_ICAP2_IT_DISABLE:              /* Disable ICAP2 interrupt */
             ARTICCSR &= (unsigned char)~(unsigned char)ICAP2_INTERRUPT;
             break;
        case PWMART_COUNTER_DISABLE:                /* Disable PWMART counter */
             ARTCSR &= (unsigned char)~(unsigned char)COUNTER_ENABLE;
             break;
        case PWMART_PWM0_DISABLE:                      /* Disable PWM0 output */
             PWMCR &= (unsigned char)~(unsigned char)ENABLE_OE0;
             break;  
        case PWMART_PWM1_DISABLE:                      /* Disable PWM1 output */
             PWMCR &= (unsigned char)~(unsigned char)ENABLE_OE1;
             break;
#if (defined PWMART_72F521 || defined PWMART_72F561)
        case PWMART_PWM2_DISABLE:                      /* Disable PWM2 output */
             PWMCR &= (unsigned char)~(unsigned char)ENABLE_OE2;
             break;           
        case PWMART_PWM3_DISABLE:                      /* Disable PWM3 output */
             PWMCR &= (unsigned char)~(unsigned char)ENABLE_OE3;
             break; 
#endif
        default:
             break;
    }
} 

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