/*******************************************************************************
COPYRIGHT 2002 STMicroelectronics
Source File Name : tbu.c
Group            : IPSW, CMG - IPDF
Author           : MCD Application Team
Date First Issued: 21/03/2002
********************************Documentation***********************************
General Purpose - This file conatains the functions for TBU. 
********************************RevisionHistory*********************************
Date : 21/03/2002                  Release:1.0  
Date : 23/09/2003               Release:2.0
Date : 28/04/04                 MISRA changes  
________________________________________________________________________________

1. Included necessary comments .
2. Changed the magic numbers to constant definitions present in tbu_hr.h.
3. Removed the functions which are changed to Macros.
4. Updated the file for release 2.0.
*******************************************************************************/

#include "ST7lib_Config.h"                                /*Configuration File*/
#include "tbu_hr.h"         /* Declaration of TBU HW registers BIT definitions*/
#include "tbu.h"                                /*Declaration of TBU functions*/   

/*------------------------------------------------------------------------------
ROUTINE NAME : TBU_Init
INPUT        : TBU_DEFAULT: Configures the TBU in default mode.
               TBU_IT_ENABLE: Interrupt is enabled
               TBU_ART_CASCADE: Used along with the ART as a 16 bit timer.
OUTPUT       : None
DESCRIPTION  : Initialises the TBU in the required mode
COMMENTS     : None
------------------------------------------------------------------------------*/
void TBU_Init ( TBU_Init_Param Init_Value)
{
    unsigned char temp;     
    TBUCV = DEFAULT_CV;           /*Sets TBU counter register to default value*/
    TBUCSR = (unsigned char)Init_Value; /* Sets the TBU as per the Input 
                                                                    parameter */
    temp = TBUCSR;
}
/*------------------------------------------------------------------------------
ROUTINE NAME : TBU_SetPrescCount8
INPUT        : 1.Prescvalue:Gives the factor by which the counter speed
                                is scaled.
               2.Counter_Value: Value from which the counter starts counting.
OUTPUT       : None
DESCRIPTION  : Sets the prescaler & Puts the value in the Counter register
COMMENTS     : None
------------------------------------------------------------------------------*/ 
void TBU_SetPrescCount8( TBU_Presc PrescValue,unsigned char Counter_Value)
{
    TBUCSR &= RESET_PRESC8 ;
    TBUCSR |= (unsigned char)PrescValue;            /* Sets the TBU prescaler */
    TBUCV = Counter_Value;                            /*Sets the Counter value*/
}
/*------------------------------------------------------------------------------
ROUTINE NAME : TBU_SetPrescCount16
INPUT        : 1.CasPrescvalue:Gives the factor by which the counter speed
              is scaled.
               2.Counter_Value: Value from which the counter starts counting.
OUTPUT       : None
DESCRIPTION  : Sets the prescaler & Puts the value in the TBU Counter register
               and the ART counter register
COMMENTS     : None
------------------------------------------------------------------------------*/ 
void TBU_SetPrescCount16(TBU_CasPresc CasPrescvalue,unsigned int Counter_value)
{  
    unsigned char temp;     

    ARTCSR &= RESET_PRESC16;
    ARTCSR |= (unsigned char)CasPrescvalue; /*Sets the PWMART prescaler value*/ 
    temp = (unsigned char)((unsigned int)0xff & Counter_value); 
                                                  /*Sets the ART counter value*/
    Counter_value = Counter_value >> 8;    
                                                  /*Sets the TBU counter value*/
    TBUCV = (unsigned char)(Counter_value & 0x00ff);
                                           
    ARTCSR |= INTERRUPT_ENABLE;         
    
    /* Minimum Counter Values to be stored in the ART register for  
        a given speed is calculated thro testing and stored.
        If the values exceed that,it is rounded off  to the minimum limit */
     
    if ((((ARTCSR & CHECK_PRESCALER )== SPEED_16) && (temp > 0xfc)))
    {
        temp = 0xfc;
    }
    else if (((ARTCSR & CHECK_PRESCALER )== SPEED_8)&& (temp > 0xfa))
    {
        temp = 0xfa;
    }          
    else if (((ARTCSR & CHECK_PRESCALER )== SPEED_4) && (temp > 0xf5))
    {
        temp = 0xf5;
    }
    else if (((ARTCSR & CHECK_PRESCALER )== SPEED_2) && (temp > 0xf0))
    {
        temp = 0xf0;
    }            
    else if ((!(ARTCSR & CHECK_PRESCALER )) && (temp > 0xe8))
    {
        temp = 0xe8;
    }
    else if (((ARTCSR & CHECK_PRESCALER ) > SPEED_16) && (temp > 0xfe))
    {
        temp = 0xfe;
    }

    ARTCAR = temp;
} 
/**** (c) 2003   STMicroelectronics *************************** END OF FILE  **/










