/**
******************************************************************************
* @file stm8l10x_tim2.c
* @author MCD Application Team
* @version V1.2.0
* @date 01-June-2012
* @brief This file contains all the functions for the TIM2 peripheral.
******************************************************************************
* @attention
*
* <h2><center>© 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.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm8l10x_tim2.h"
/** @addtogroup STM8L10x_StdPeriph_Driver
* @{
*/
/** @defgroup TIM2
* @brief TIM2 driver modules
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void TI1_Config(TIM2_ICPolarity_TypeDef TIM2_ICPolarity,
TIM2_ICSelection_TypeDef TIM2_ICSelection,
uint8_t TIM2_ICFilter);
static void TI2_Config(TIM2_ICPolarity_TypeDef TIM2_ICPolarity,
TIM2_ICSelection_TypeDef TIM2_ICSelection,
uint8_t TIM2_ICFilter);
/**
* @addtogroup TIM2_Private_Functions
* @{
*/
/**
* @brief Deinitializes the TIM2 peripheral registers to their default reset values.
* @param None
* @retval None
*/
void TIM2_DeInit(void)
{
TIM2->CR1 = TIM_CR1_RESET_VALUE;
TIM2->CR2 = TIM_CR2_RESET_VALUE;
TIM2->SMCR = TIM_SMCR_RESET_VALUE;
TIM2->ETR = TIM_ETR_RESET_VALUE;
TIM2->IER = TIM_IER_RESET_VALUE;
TIM2->SR2 = TIM_SR2_RESET_VALUE;
/* Disable channels */
TIM2->CCER1 = TIM_CCER1_RESET_VALUE;
/* Configure channels as inputs: it is necessary if lock level is equal to 2 or 3 */
TIM2->CCMR1 = 0x01;/*TIM2_ICxSource_TIxFPx */
TIM2->CCMR2 = 0x01;/*TIM2_ICxSource_TIxFPx */
/* Then reset channel registers: it also works if lock level is equal to 2 or 3 */
TIM2->CCER1 = TIM_CCER1_RESET_VALUE;
TIM2->CCMR1 = TIM_CCMR1_RESET_VALUE;
TIM2->CCMR2 = TIM_CCMR2_RESET_VALUE;
TIM2->CNTRH = TIM_CNTRH_RESET_VALUE;
TIM2->CNTRL = TIM_CNTRL_RESET_VALUE;
TIM2->PSCR = TIM_PSCR_RESET_VALUE;
TIM2->ARRH = TIM_ARRH_RESET_VALUE;
TIM2->ARRL = TIM_ARRL_RESET_VALUE;
TIM2->CCR1H = TIM_CCR1H_RESET_VALUE;
TIM2->CCR1L = TIM_CCR1L_RESET_VALUE;
TIM2->CCR2H = TIM_CCR2H_RESET_VALUE;
TIM2->CCR2L = TIM_CCR2L_RESET_VALUE;
TIM2->OISR = TIM_OISR_RESET_VALUE;
TIM2->EGR = 0x01;/*TIM_EGR_UG;*/
TIM2->BKR = TIM_BKR_RESET_VALUE;
TIM2->SR1 = TIM_SR1_RESET_VALUE;
}
/**
* @brief Initializes the TIM2 Time Base Unit according to the specified parameters.
* @param TIM2_Prescaler: Prescaler
* This parameter can be one of the following values:
* @arg TIM2_Prescaler_1: Time base Prescaler = 1 (No effect)
* @arg TIM2_Prescaler_2: Time base Prescaler = 2
* @arg TIM2_Prescaler_4: Time base Prescaler = 4
* @arg TIM2_Prescaler_8: Time base Prescaler = 8
* @arg TIM2_Prescaler_16: Time base Prescaler = 16
* @arg TIM2_Prescaler_32: Time base Prescaler = 32
* @arg TIM2_Prescaler_64: Time base Prescaler = 64
* @arg TIM2_Prescaler_128: Time base Prescaler = 128
* @param TIM2_CounterMode: Counter mode
* This parameter can be one of the following values:
* @arg TIM2_CounterMode_Up: Counter Up Mode
* @arg TIM2_CounterMode_Down: Counter Down Mode
* @arg TIM2_CounterMode_CenterAligned1: Counter Central aligned Mode 1
* @arg TIM2_CounterMode_CenterAligned2: Counter Central aligned Mode 2
* @arg TIM2_CounterMode_CenterAligned3: Counter Central aligned Mode 3
* @param TIM2_Period: This parameter must be a value between 0x0000 and 0xFFFF.
* @retval None
*/
void TIM2_TimeBaseInit(TIM2_Prescaler_TypeDef TIM2_Prescaler,
TIM2_CounterMode_TypeDef TIM2_CounterMode,
uint16_t TIM2_Period)
{
assert_param(IS_TIM2_PRESCALER(TIM2_Prescaler));
assert_param(IS_TIM2_COUNTER_MODE(TIM2_CounterMode));
/* Set the Autoreload value */
TIM2->ARRH = (uint8_t)(TIM2_Period >> 8) ;
TIM2->ARRL = (uint8_t)(TIM2_Period);
/* Set the Prescaler value */
TIM2->PSCR = (uint8_t)(TIM2_Prescaler);
/* Select the Counter Mode */
TIM2->CR1 &= (uint8_t)((uint8_t)(~TIM_CR1_CMS)) & ((uint8_t)(~TIM_CR1_DIR));
TIM2->CR1 |= (uint8_t)(TIM2_CounterMode);
}
/**
* @brief Initializes the TIM2 Channel1 according to the specified parameters.
* @param TIM2_OCMode: Output Compare Mode
* This parameter can be one of the following values:
* @arg TIM2_OCMode_Timing: Timing (Frozen) Mode
* @arg TIM2_OCMode_Active: Active Mode
* @arg TIM2_OCMode_Inactive: Inactive Mode
* @arg TIM2_OCMode_Toggle: Toggle Mode
* @arg TIM2_OCMode_PWM1: PWM Mode 1
* @arg TIM2_OCMode_PWM2: PWM Mode 2
* @param TIM2_OutputState: Output state
* This parameter can be one of the following values:
* @arg TIM2_OutputState_Disable: Output compare State disabled (channel output disabled)
* @arg TIM2_OutputState_Enable: Output compare State enabled (channel output enabled)
* @param TIM2_Pulse: This parameter must be a value between 0x0000 and 0xFFFF.
* @param TIM2_OCPolarity: Polarity
* This parameter can be one of the following values:
* @arg TIM2_OCPolarity_High: Output compare polarity = High
* @arg TIM2_OCPolarity_Low: Output compare polarity = Low
* @param TIM2_OCIdleState: Output Compare Idle State
* This parameter can be one of the following values:
* @arg TIM2_OCIdleState_Reset: Output Compare Idle state = Reset
* @arg TIM2_OCIdleState_Set: Output Compare Idle state = Set
* @retval None
*/
void TIM2_OC1Init(TIM2_OCMode_TypeDef TIM2_OCMode,
TIM2_OutputState_TypeDef TIM2_OutputState,
uint16_t TIM2_Pulse,
TIM2_OCPolarity_TypeDef TIM2_OCPolarity,
TIM2_OCIdleState_TypeDef TIM2_OCIdleState)
{
uint8_t tmpccmr1 = 0;
/* Check the parameters */
assert_param(IS_TIM2_OC_MODE(TIM2_OCMode));
assert_param(IS_TIM2_OUTPUT_STATE(TIM2_OutputState));
assert_param(IS_TIM2_OC_POLARITY(TIM2_OCPolarity));
assert_param(IS_TIM2_OCIDLE_STATE(TIM2_OCIdleState));
tmpccmr1 = TIM2->CCMR1;
/* Disable the Channel 1: Reset the CCE Bit */
TIM2->CCER1 &= (uint8_t)(~TIM_CCER1_CC1E);
/* Reset the Output Compare Bits */
tmpccmr1 &= (uint8_t)(~TIM_CCMR_OCM);
/* Set the Ouput Compare Mode */
tmpccmr1 |= (uint8_t)TIM2_OCMode;
TIM2->CCMR1 = tmpccmr1;
/* Set the Output State */
if (TIM2_OutputState ==