/** ###################################################################
**     THIS COMPONENT MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**     Filename  : Frekvence_filtr.C
**     Project   : Generator
**     Processor : MC9S08GT16ACFB
**     Component : TimerOut
**     Version   : Component 03.016, Driver 01.20, CPU db: 2.87.091
**     Compiler  : CodeWarrior HCS08 C Compiler
**     Date/Time : 27.3.2010, 21:03
**     Abstract  :
**         This bean implements a periodic signal generator 1:1 (Flip-Flop Output).
**         The signal alternates the "0" and "1" output values in exactly
**         regular intervals; that is why the time length of the "0" part
**         of one pulse is equal to the "1" part of one pulse.
**     Settings  :
**         Used output pin             : 
**             ----------------------------------------------------
**                Number (on package)  |    Name
**             ----------------------------------------------------
**                       20            |  PTD3_TPM2CLK_TPM2CH0
**             ----------------------------------------------------
**
**         Timer name                  : TPM2 (16-bit)
**         Compare name                : TPM20
**           Counter shared            : No
**
**         High speed mode
**             Prescaler               : divide-by-1
**             Clock                   : 10000000 Hz
**           Pulse width
**             Xtal ticks              : 20
**             microseconds            : 1
**             seconds (real)          : 0.000001
**             Hz                      : 1000000
**             kHz                     : 1000
**             MHz                     : 1
**
**         Runtime setting             : pulse width interval (continual setting)
**             ticks                   : 4 to 400 ticks
**             microseconds            : 1 to 20 microseconds
**             seconds (real)          : 0.0000002 to 0.00002 seconds
**             Hz                      : 50000 to 5000000 Hz
**             kHz                     : 50 to 5000 kHz
**             MHz                     : 1 to 5 MHz
**
**         Initialization:
**              Output level           : low
**              Timer                  : Disabled
**              Events                 : Enabled
**
**         Timer registers
**              Counter                : TPM2CNT   [$0061]
**              Mode                   : TPM2SC    [$0060]
**              Run                    : TPM2SC    [$0060]
**              Prescaler              : TPM2SC    [$0060]
**
**         Compare registers
**              Compare                : TPM2C0V   [$0066]
**
**         Flip-flop registers
**              Mode                   : TPM2C0SC  [$0065]
**     Contents  :
**         Enable        - byte Frekvence_filtr_Enable(void);
**         Disable       - byte Frekvence_filtr_Disable(void);
**         SetPeriodReal - byte Frekvence_filtr_SetPeriodReal(float Time);
**
**     Copyright : 1997 - 2009 Freescale Semiconductor, Inc. All Rights Reserved.
**     
**     http      : www.freescale.com
**     mail      : support@freescale.com
** ###################################################################*/

/* MODULE Frekvence_filtr. */

#include "PE_Error.h"
#include "Frekvence_filtr.h"

#pragma MESSAGE DISABLE C2705          /* WARNING C2705: Possible loss of data */
#pragma MESSAGE DISABLE C5919          /* WARNING C5919: Conversion of floating to unsigned integral */


static bool EnUser;                    /* Enable device by user */
static word CmpHighVal = 0x09U;        /* Compare register value for high speed CPU mode */
/* Internal method prototypes */

static void HWEnDi(void);              /* Function prototype to meet MISRA requirements */

/*
** ===================================================================
**     Method      :  HWEnDi (component TimerOut)
**
**     Description :
**         Enables or disables the peripheral(s) associated with the bean.
**         The method is called automatically as a part of the Enable and 
**         Disable methods and several internal methods.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
static void HWEnDi(void)
{
  if (EnUser) {
    /* TPM2SC: CLKSB=0,CLKSA=1 */
    clrSetReg8Bits(TPM2SC, 0x10, 0x08); /* Run counter (set CLKSB:CLKSA) */ 
  } else {
    /* TPM2SC: TOF=0,CLKSB=0,CLKSA=0 */
    clrReg8Bits(TPM2SC, 0x98);         /* Stop counter (CLKSB:CLKSA = 00) */ 
    /* TPM2CNTH: BIT15=0,BIT14=0,BIT13=0,BIT12=0,BIT11=0,BIT10=0,BIT9=0,BIT8=0 */
    setReg8(TPM2CNTH, 0x00);           /* Clear counter register */ 
  }
}

/*
** ===================================================================
**     Method      :  Frekvence_filtr_Enable (component TimerOut)
**
**     Description :
**         This method enables the component - It starts the signal
**         generation. Events may be generated (<DisableEvent>
**         /<EnableEvent>).
**     Parameters  : None
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte Frekvence_filtr_Enable(void)
{
  if (!EnUser) {                       /* Is the device disabled by user? */
    EnUser = TRUE;                     /* If yes then set the flag "device enabled" */
    HWEnDi();                          /* Enable the device */
  }
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  Frekvence_filtr_Disable (component TimerOut)
**
**     Description :
**         This method disables the timer - it stops signal generation
**         and events calling. When the timer is disabled, it is
**         possible to call methods <ClrValue> and <SetValue>.
**     Parameters  : None
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte Frekvence_filtr_Disable(void)
{
  if (EnUser) {                        /* Is the device enabled by user? */
    EnUser = FALSE;                    /* If yes then set the flag "device disabled" */
    HWEnDi();                          /* Disable the device */
  }
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  Frekvence_filtr_SetPeriodReal (component TimerOut)
**
**     Description :
**         This method sets the new period of the output signal. The
**         period is expressed in seconds as a real number.
**         This method is available only if the runtime setting type
**         'from interval' is selected in the Timing dialog box in
**         the Runtime setting area.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Time            - Period to set [in seconds]
**                      (0.0000002 to 0.00002 seconds)
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_MATH - Overflow during evaluation
**                           ERR_RANGE - Parameter out of range
** ===================================================================
*/
byte Frekvence_filtr_SetPeriodReal(float Time)
{
  float rtreal;                        /* Result of multiplication */

  if ((Time > 0.00002F) || (Time < 0.0000002F)) { /* Is the given value out of range? */
    return ERR_RANGE;                  /* If yes then error */
  }
  rtreal = (Time * 10000000.0F) + 0.5F; /* Multiply given value and high speed CPU mode coefficient, add 0.5 for correct round */
  CmpHighVal = (word)((word)rtreal - 1U); /* Store result (compare register value for high speed CPU mode) to the variable CmpHighVal */
  Frekvence_filtr_SetCV(CmpHighVal);   /* Store appropriate value to the compare register according to the selected high speed CPU mode */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  Frekvence_filtr_InitTO (component TimerOut)
**
**     Description :
**         Initializes the associated peripheral(s) and the bean internal 
**         variables. The method is called automatically as a part of the 
**         application initialization code.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
void Frekvence_filtr_InitTO(void)
{
  /* TPM2SC: TOF=0,TOIE=0,CPWMS=0,CLKSB=0,CLKSA=0,PS2=0,PS1=0,PS0=0 */
  setReg8(TPM2SC, 0x00);               /* Stop HW; disable overflow interrupt and set prescaler to 0 */ 
  /* TPM2C0SC: CH0F=0,CH0IE=0,MS0B=0,MS0A=1,ELS0B=0,ELS0A=1,??=0,??=0 */
  setReg8(TPM2C0SC, 0x14);             /* Set output comp. mode (tgg. on output); disable. comp. int. */ 
  EnUser=FALSE;                        /* Disable device */
  Frekvence_filtr_SetCV(CmpHighVal);   /* Store appropriate value to the compare register if selected High speed CPU mode */
  Frekvence_filtr_SetPV((byte)0x00);   /* Set prescaler */
  HWEnDi();
}

/*
** ===================================================================
**     Method      :  SetCV (component TimerOut)
**
**     Description :
**         The method computes and sets compare eventually modulo value 
**         for time measuring.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
static void Frekvence_filtr_SetCV(word Val)
{
  TPM2C0V = Val;                       /* Store given value to the compare register */
  TPM2MOD = Val;                       /* and to the modulo register */
}


/* END Frekvence_filtr. */

/*
** ###################################################################
**
**     This file was created by Processor Expert 3.07 [04.34]
**     for the Freescale HCS08 series of microcontrollers.
**
** ###################################################################
*/
