/*!
* @file apm32f4xx_tmr.c
*
* @brief This file provides all the TMR firmware functions.
*
* @version V1.0.2
*
* @date 2022-06-23
*
* @attention
*
* Copyright (C) 2021-2022 Geehy Semiconductor
*
* You may not use this file except in compliance with the
* GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
*
* The program is only for reference, which is distributed in the hope
* that it will be usefull and instructional for customers to develop
* their software. Unless required by applicable law or agreed to in
* writing, the program is distributed on an "AS IS" BASIS, WITHOUT
* ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
* See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
* and limitations under the License.
*/
#include "apm32f4xx_tmr.h"
#include "apm32f4xx_rcm.h"
/** @addtogroup APM32F4xx_StdPeriphDriver
@{
*/
/** @defgroup TMR_Driver
* @brief TMR driver modules
@{
*/
/** @defgroup TMR_Functions
@{
*/
static void TI1Config(TMR_T* tmr, uint16_t ICpolarity, uint16_t ICselection, uint16_t ICfilter);
static void TI2Config(TMR_T* tmr, uint16_t ICpolarity, uint16_t ICselection, uint16_t ICfilter);
static void TI3Config(TMR_T* tmr, uint16_t ICpolarity, uint16_t ICselection, uint16_t ICfilter);
static void TI4Config(TMR_T* tmr, uint16_t ICpolarity, uint16_t ICselection, uint16_t ICfilter);
/*!
* @brief Deinitializes the TMRx peripheral registers to their default reset values.
*
* @param tmr: Select TMRx peripheral, The x can be from 1 to 14.
*
* @retval None
*
* @note
*/
void TMR_Reset(TMR_T* tmr)
{
if (tmr == TMR1)
{
RCM_EnableAPB2PeriphReset(RCM_APB2_PERIPH_TMR1);
RCM_DisableAPB2PeriphReset(RCM_APB2_PERIPH_TMR1);
}
else if (tmr == TMR2)
{
RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_TMR2);
RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_TMR2);
}
else if (tmr == TMR3)
{
RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_TMR3);
RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_TMR3);
}
else if (tmr == TMR4)
{
RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_TMR4);
RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_TMR4);
}
else if (tmr == TMR5)
{
RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_TMR5);
RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_TMR5);
}
else if (tmr == TMR6)
{
RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_TMR6);
RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_TMR6);
}
else if (tmr == TMR7)
{
RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_TMR7);
RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_TMR7);
}
else if (tmr == TMR8)
{
RCM_EnableAPB2PeriphReset(RCM_APB2_PERIPH_TMR8);
RCM_DisableAPB2PeriphReset(RCM_APB2_PERIPH_TMR8);
}
else if (tmr == TMR9)
{
RCM_EnableAPB2PeriphReset(RCM_APB2_PERIPH_TMR9);
RCM_DisableAPB2PeriphReset(RCM_APB2_PERIPH_TMR9);
}
else if (tmr == TMR10)
{
RCM_EnableAPB2PeriphReset(RCM_APB2_PERIPH_TMR10);
RCM_DisableAPB2PeriphReset(RCM_APB2_PERIPH_TMR10);
}
else if (tmr == TMR11)
{
RCM_EnableAPB2PeriphReset(RCM_APB2_PERIPH_TMR11);
RCM_DisableAPB2PeriphReset(RCM_APB2_PERIPH_TMR11);
}
else if (tmr == TMR12)
{
RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_TMR12);
RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_TMR12);
}
else if (tmr == TMR13)
{
RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_TMR13);
RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_TMR13);
}
else if (tmr == TMR14)
{
RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_TMR14);
RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_TMR14);
}
}
/*!
* @brief Config the base timer through the structure
*
* @param tmr: Select TMRx peripheral, The x can be 1 to 14
*
* @param baseConfig: Pointer to a TMR_BaseConfig_T structure
*
* @retval None
*/
void TMR_ConfigTimeBase(TMR_T* tmr, TMR_BaseConfig_T* baseConfig)
{
if ((tmr == TMR1) || (tmr == TMR2) || \
(tmr == TMR3) || (tmr == TMR4) || \
(tmr == TMR5) || (tmr == TMR8))
{
/** Count Direction */
tmr->CTRL1_B.CNTDIR = baseConfig->countMode & 0x01;
/** Aligned mode */
tmr->CTRL1_B.CAMSEL = baseConfig->countMode >> 4;
}
if ((tmr != TMR6) && (tmr != TMR7))
{
tmr->CTRL1_B.CLKDIV = baseConfig->clockDivision;
}
tmr->AUTORLD = baseConfig->period ;
tmr->PSC = baseConfig->division;
if ((tmr == TMR1) || (tmr == TMR8))
{
tmr->REPCNT = baseConfig->repetitionCounter;
}
tmr->CEG_B.UEG = TMR_PSC_RELOAD_IMMEDIATE;
}
/*!
* @brief Config the Base timer with its default value.
*
* @param baseConfig: pointer to a TMR_BaseConfig_T
*
* @retval None
*/
void TMR_ConfigTimeBaseStructInit(TMR_BaseConfig_T* baseConfig)
{
baseConfig->countMode = TMR_COUNTER_MODE_UP;
baseConfig->clockDivision = TMR_CLOCK_DIV_1;
baseConfig->period = 0xFFFF;
baseConfig->division = 0x0000;
baseConfig->repetitionCounter = 0x0000;
}
/*!
* @brief Configures the TMRx Prescaler.
*
* @param tmr: Select TMRx peripheral, The x can be 1 to 14
*
* @param psc: specifies the Prescaler Register value
*
* @param reload: specifies the TMR Prescaler Reload mode
* The parameter can be one of following values:
* @arg TMR_PSC_RELOAD_UPDATE: The Prescaler reload at the update event
* @arg TMR_PSC_RELOAD_IMMEDIATE: The Prescaler reload immediately
*
* @retval None
*/
void TMR_ConfigPrescaler(TMR_T* tmr, uint16_t psc, TMR_PSC_RELOAD_T reload)
{
tmr->PSC_B.PSC = psc;
tmr->CEG_B.UEG = reload;
}
/*!
* @brief Config counter mode.
*
* @param tmr: The TMRx can be 1 to 8 except 6 and 7
*
* @param countMode: specifies the Counter Mode to be used
* The parameter can be one of following values:
* @arg TMR_COUNTER_MODE_UP: Timer Up Counting Mode
* @arg TMR_COUNTER_MODE_DOWN: Timer Down Counting Mode
* @arg TMR_COUNTER_MODE_CENTER_ALIGNED1: Timer Center Aligned Mode1
* @arg TMR_COUNTER_MODE_CENTER_ALIGNED2: Timer Center Aligned Mode2
* @arg TMR_COUNTER_MODE_CENTER_ALIGNED3: Timer Center Aligned Mode3
*
* @retval None
*/
void TMR_ConfigCounterMode(TMR_T* tmr, TMR_COUNTER_MODE_T countMode)
{
tmr->CTRL1_B.CNTDIR = countMode & 0x01;
tmr->CTRL1_B.CAMSEL = countMode >> 4;
}
/*!
* @brief Configs the Counter Register value
*
* @param tmr: The TMRx can be 1 to 14
*
* @param counter: Counter register new value
*
* @retval None
*/
void TMR_ConfigCounter(TMR_T* tmr, uint16_t counter)
{
tmr->CNT = counter;
}
/*!
* @brief Configs the value of AutoReload Register.
*
* @param tmr: The TMRx can be 1 to 14
*
* @param autoReload: autoReload register new value
*
* @retval None
*/
void TMR_ConfigAutoreload(TMR_T* tmr, uint16_t autoReload)
{
tmr->AUTORLD_B.AUTORLD = autoReload;
}
/*!
* @brief Read the TMRx Counter value.
*
* @param tmr: The TMRx can be 1 to 14
*
* @retval Counter Register value.
*/
uint16_t TMR_ReadCounter(TMR_T* tmr)
{
return tmr->CNT;
}
/*!
* @brief Read the TMRx Prescaler value.
*
* @param tmr: The TMRx can be 1 to 14
*
* @retval Prescaler Register value.
*/
uint16_t TMR_ReadPrescaler(TMR_T* tmr)
{
return tmr->PSC;
}
/*!
* @brief Enable the TMRx update event
*
* @param t