/**
  ******************************************************************************
  * @file    stm8l10x_tim4.h
  * @author  MCD Application Team
  * @version V1.2.0
  * @date    01-June-2012
  * @brief   This file contains all functions prototypes and macros for the TIM4
  *          peripheral.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM8L10X_TIM4_H
#define __STM8L10X_TIM4_H

/* Includes ------------------------------------------------------------------*/
#include "stm8l10x.h"

/* Exported variables ------------------------------------------------------- */
/* Exported types ------------------------------------------------------------*/

/** @addtogroup TIM4_Exported_Types
  * @{
  */

/**
  * @brief TIM4 Prescaler
  */
typedef enum
{
  TIM4_Prescaler_1    = ((uint8_t)0x00), /*!< Time base Prescaler = 1 (No effect)*/
  TIM4_Prescaler_2     = ((uint8_t)0x01),  /*!< Time base Prescaler = 2 */
  TIM4_Prescaler_4     = ((uint8_t)0x02),  /*!< Time base Prescaler = 4 */
  TIM4_Prescaler_8    = ((uint8_t)0x03),  /*!< Time base Prescaler = 8 */
  TIM4_Prescaler_16    = ((uint8_t)0x04),  /*!< Time base Prescaler = 16 */
  TIM4_Prescaler_32    = ((uint8_t)0x05),  /*!< Time base Prescaler = 32 */
  TIM4_Prescaler_64    = ((uint8_t)0x06),  /*!< Time base Prescaler = 64 */
  TIM4_Prescaler_128   = ((uint8_t)0x07),  /*!< Time base Prescaler = 128 */
  TIM4_Prescaler_256   = ((uint8_t)0x08),  /*!< Time base Prescaler = 256 */
  TIM4_Prescaler_512   = ((uint8_t)0x09),  /*!< Time base Prescaler = 512 */
  TIM4_Prescaler_1024  = ((uint8_t)0x0A),  /*!< Time base Prescaler = 1024 */
  TIM4_Prescaler_2048  = ((uint8_t)0x0B),  /*!< Time base Prescaler = 2048 */
  TIM4_Prescaler_4096  = ((uint8_t)0x0C),  /*!< Time base Prescaler = 4096 */
  TIM4_Prescaler_8192  = ((uint8_t)0x0D),  /*!< Time base Prescaler = 8196 */
  TIM4_Prescaler_16384 = ((uint8_t)0x0E),  /*!< Time base Prescaler = 16384 */
  TIM4_Prescaler_32768 = ((uint8_t)0x0F)   /*!< Time base Prescaler = 32768 */
}TIM4_Prescaler_TypeDef;

/**
  * @brief TIM4 One Pulse Mode
  */
typedef enum
{
  TIM4_OPMode_Single    = ((uint8_t)0x01),  /*!< Single one Pulse mode (OPM Active) */
  TIM4_OPMode_Repetitive  = ((uint8_t)0x00)   /*!< Repetitive Pulse mode (OPM inactive) */
}TIM4_OPMode_TypeDef;

/**
  * @brief TIM4 Prescaler Reload Mode
  */
typedef enum
{
  TIM4_PSCReloadMode_Update     = ((uint8_t)0x00), /*!< Prescaler value is reloaded at every update*/
  TIM4_PSCReloadMode_Immediate = ((uint8_t)0x01)  /*!< Prescaler value is reloaded immediatly*/
}TIM4_PSCReloadMode_TypeDef;

/**
  * @brief TIM4 Update Source
  */
typedef enum
{
  TIM4_UpdateSource_Global = ((uint8_t)0x00), /*!< Global Update request source */
  TIM4_UpdateSource_Regular = ((uint8_t)0x01)  /*!< Regular Update request source */
}TIM4_UpdateSource_TypeDef;

/**
  * @brief TIM4 Event Source
  */
typedef enum
{
  TIM4_EventSource_Update  = ((uint8_t)0x01),  /*!< Update Event*/
  TIM4_EventSource_Trigger = ((uint8_t)0x40)  /*!< Trigger Event*/
}TIM4_EventSource_TypeDef;

/**
  * @brief TIM4 Trigger Output Source
  */
typedef enum
{
  TIM4_TRGOSource_Reset  = ((uint8_t)0x00),  /*!< Trigger Output source = Reset*/
  TIM4_TRGOSource_Enable = ((uint8_t)0x10),  /*!< Trigger Output source = TIM2 is enabled*/
  TIM4_TRGOSource_Update = ((uint8_t)0x20)  /*!< Trigger Output source = Update event*/
}TIM4_TRGOSource_TypeDef;

/**
  * @brief TIM4 Slave Mode
  */
typedef enum
{
  TIM4_SLAVEMODE_DISABLE  = ((uint8_t)0x00),  /*!< Disable slave mode to clock the prescaler directly with the internal clock */
  TIM4_SlaveMode_Reset   = ((uint8_t)0x04),  /*!< Slave Mode Selection  = Reset*/
  TIM4_SlaveMode_Gated   = ((uint8_t)0x05),  /*!< Slave Mode Selection  = Gated*/
  TIM4_SlaveMode_Trigger  = ((uint8_t)0x06),  /*!< Slave Mode Selection  = Trigger*/
  TIM4_SlaveMode_External1 = ((uint8_t)0x07)  /*!< Slave Mode Selection  = External 1*/
}TIM4_SlaveMode_TypeDef;

/**
  * @brief TIM4 Flags
  */
typedef enum
{
  TIM4_FLAG_Update = ((uint8_t)0x01),  /*!< Update Flag */
  TIM4_FLAG_Trigger = ((uint8_t)0x40)  /*!< Trigger Flag */
}TIM4_FLAG_TypeDef;

/**
  * @brief TIM4 interrupt sources
  */
typedef enum
{
  TIM4_IT_Update = ((uint8_t)0x01),  /*!< Update Interrupt*/
  TIM4_IT_Trigger = ((uint8_t)0x40)  /*!< Trigger  Interrupt*/
}TIM4_IT_TypeDef;

/**
  * @brief TIM4 Internal Trigger Selection
  */
typedef enum
{
  TIM4_TRGSelection_TIM3 = ((uint8_t)0x20),/*!< TRIG Input source =  TIM3 TRIG Output  */
  TIM4_TRGSelection_TIM2 = ((uint8_t)0x30) /*!< TRIG Input source =  TIM2 TRIG Output  */
}TIM4_TRGSelection_TypeDef;

/**
  * @}
  */

/* Exported constants --------------------------------------------------------*/
/* Exported macros -----------------------------------------------------------*/
/* Private macros ------------------------------------------------------------*/

/** @addtogroup TIM4_Private_Macros
  * @{
  */

/**
  * @brief Macro used by the assert function to check the different functions parameters.
  */

/**
  * @brief Macro TIM4 Prescaler
  */
#define IS_TIM4_PRESCALER(PRESCALER)  \
  (((PRESCALER) == TIM4_Prescaler_1)    || \
   ((PRESCALER) == TIM4_Prescaler_2)    || \
   ((PRESCALER) == TIM4_Prescaler_4)    || \
   ((PRESCALER) == TIM4_Prescaler_8)    || \
   ((PRESCALER) == TIM4_Prescaler_16)   || \
   ((PRESCALER) == TIM4_Prescaler_32)   || \
   ((PRESCALER) == TIM4_Prescaler_64)   || \
   ((PRESCALER) == TIM4_Prescaler_128)  || \
   ((PRESCALER) == TIM4_Prescaler_256)  || \
   ((PRESCALER) == TIM4_Prescaler_512)  || \
   ((PRESCALER) == TIM4_Prescaler_1024) || \
   ((PRESCALER) == TIM4_Prescaler_2048) || \
   ((PRESCALER) == TIM4_Prescaler_4096) || \
   ((PRESCALER) == TIM4_Prescaler_8192) || \
   ((PRESCALER) == TIM4_Prescaler_16384)|| \
   ((PRESCALER) == TIM4_Prescaler_32768))
/**
  * @brief Macro TIM4 One Pulse Mode
  */
#define IS_TIM4_OPM_MODE(MODE)  \
  (((MODE) == TIM4_OPMode_Single) || \
   ((MODE) == TIM4_OPMode_Repetitive))

/**
  * @brief Macro TIM4 Prescaler reload
  */
#define IS_TIM4_PRESCALER_RELOAD(RELOAD) \
  (((RELOAD) == TIM4_PSCReloadMode_Update) || \
   ((RELOAD) == TIM4_PSCReloadMode_Immediate))
/**
  * @brief Macro TIM4 Update source
  */
#define IS_TIM4_UPDATE_SOURCE(SOURCE) \
  (((SOURCE) == TIM4_UpdateSource_Global) || \
   ((SOURCE) == TIM4_UpdateSource_Regular))
/**
  * @brief Macro TIM4 Event source
  */
#define IS_TIM4_EVENT_SOURCE(SOURCE) \
  (((SOURCE & (uint8_t)0xBE) == 0x00) && \
   ((SOURCE) != 0x00))

/**
  * @brief Macro TIM4 TRGO source
  */
#define IS_TIM4_TRGO_SOURCE(SOURCE) \
  (((SOURCE) == TIM4_TRGOSource_Reset) || \
   ((SOURCE) == TIM4_TRGOSource_Enable)|| \
   ((SOURCE) == TIM4_TRGOSource_Update))
/**
  * @brief Macro TIM4 Slave mode
  */
#define IS_TIM4_SLAVE_MODE(MODE) \
  (((MODE) == TIM4_SLAVEMODE_DISABLE) || \
   ((MODE) == TIM4_SlaveMode_Reset)   || \
   ((MODE) == TIM4_SlaveMode_Gated)   || \
   ((MODE) == TIM4_SlaveMode_Trigger) || \
   ((MODE) == TIM4_SlaveMode_External1))
/**
  * @brief Macro TIM4 Flags
  */
#define IS_TIM4_GET_FLAG(FLAG) \
  (((FLAG) == TIM4_FLAG_Update) || \
   ((FLAG) == TIM4_FLAG_Trigger))

#define IS_TIM4_CLEAR_FLAG(FLAG) \
  (((FLAG & (uint8_t)0xBE) == 0x00) && (FLAG != 0x00))
/**
  * @brief Macro TIM4 interrupts
  */
#define IS_TIM4_IT(IT) \
  (((IT) != 0x00) && \
   (((uint8_t)IT & (uint8_t)(~(uint8_t)(0x41)))== 0x00))

#define IS_TIM4_GET_IT(IT) \
  (((IT) == TIM4_IT_Update) || \
   ((IT) == TIM4_IT_Trigger))
/**
  * @brief Macro TIM4 Trigger selection
  */
#define IS_TIM4_TRIGGER_SELECTION(SELECTION) \
  (((SELECTION) == TIM4_TRGSelection_TIM2) || \
   ((SELECTION) == TIM4_TRGSelection_TIM3))
/**
 * @}
 */

/* Exported functions --------------------------------------------------------*/

/** @addtogroup TIM4_Exported_Functions
  * @{
  */

void TIM4_DeInit(void);
void TIM4_TimeBaseInit(TIM4_Prescaler_TypeDef TIM4_Prescaler,
                       uint8_t TIM4_Period);
void TIM4_Cmd(FunctionalState NewState);
void TIM4_ITConfig(TIM4_IT_TypeDef TIM4_IT,
                   FunctionalState NewState);
void TIM4_InternalClockConfig(void);
void TIM4_SelectInputTrigger(TIM4_TRGSelection_TypeDef TIM4_InputTriggerSource);
void TIM4_UpdateDisableConfig(FunctionalState Newstate);
void TIM4_UpdateRequestConfig(TIM4_UpdateSource_TypeDef TIM4_UpdateSource);
void TIM4_SelectOnePulseMode(TIM4_OPMode_TypeDef TIM4_OPMode);
void TIM4_SelectOutputTrigger(TIM4_TRGOSource_TypeDef TIM4_TRGOSource);
void TIM4_SelectSlaveMode(TIM4_SlaveMode_TypeDef TIM4_SlaveMode);
void TIM4_SelectMasterSlaveMode(FunctionalState NewState);
void TIM4_PrescalerConfig(TIM4_Prescaler_TypeDef Prescaler,
                          TIM4_PSCReloadMode_TypeDef TIM4_PSCReloadMode);
void TIM4_ARRPreloadConfig(FunctionalState Newstate);
void TIM4_GenerateEvent(TIM4_EventSource_TypeDef TIM4_EventSource);
void TIM4_SetCounter(uint8_t TIM4_Counter);
void TIM4_SetAutoreload(uint8_t TIM4_Autoreload);
uint8_t TIM4_GetCounter(void);
TIM4_Prescaler_TypeDef TIM4_GetPrescaler(void);
FunctionalState TIM4_GetStatus(void);
FlagStatus TIM4_GetFlagStatus(TIM4_FLAG_TypeDef TIM4_FLAG);
void TIM4_ClearFlag(TIM4_FLAG_TypeDef TIM4_FLAG);
ITStatus TIM4_GetITStatus(TIM4_IT_TypeDef TIM4_IT);
void TIM4_ClearITPendingBit(TIM4_IT_TypeDef TIM4_IT);

/**
 * @}
 */

#endif /* __STM8L10X_TIM4_H */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
