435 lines
12 KiB
C
435 lines
12 KiB
C
/* USER CODE BEGIN Header */
|
|
/**
|
|
******************************************************************************
|
|
* @file timer.c
|
|
* @brief This file provides code for the configuration
|
|
* of the Periodic Timer , PWM and Input Capture.
|
|
* @data 1-feb-2024
|
|
* @Author Vecmocon Technology
|
|
******************************************************************************
|
|
*/
|
|
/* USER CODE END Header */
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include <..\Core\Include\ivec_mcal_timer.h>
|
|
|
|
static volatile bool b_TimerInitFlag = 0; /*!< Timer initialization flag */
|
|
static volatile bool b_TimerCounterFlag = 0; /*!< Timer Counter flag */
|
|
static volatile bool b_PwmInitflag = 0; /*!< PWM initialization flag */
|
|
static volatile bool b_Pwmstartflag = 0; /*!< PWM Start flag */
|
|
static volatile bool b_InputCaputerflag =0; /*!< Input Capture initialization flag */
|
|
|
|
|
|
static volatile uint32_t g_u32CapturePeriod; /*!<To get the first capture period */
|
|
static volatile uint32_t g_u32CaptureValue0; /*!<To get the first capture value */
|
|
static volatile uint32_t g_u32CaptureValue1; /*!<To get the second capture value */
|
|
static volatile uint32_t g_u32LoadValue; /*!<To get timer loaded value */
|
|
float f_frequency =0; /*!<To get calculated frequency */
|
|
|
|
/**
|
|
* @brief Configuration for Input capture timer.
|
|
*/
|
|
static const DL_TimerA_ClockConfig gCAPTURE_0ClockConfig = {
|
|
.clockSel = DL_TIMER_CLOCK_BUSCLK,
|
|
.divideRatio = DL_TIMER_CLOCK_DIVIDE_1,
|
|
.prescale = 32U
|
|
};
|
|
|
|
/**
|
|
* @brief Configuration for Input capture mode channel.
|
|
*/
|
|
static const DL_TimerA_CaptureCombinedConfig gCAPTURE_0CaptureConfig = {
|
|
.captureMode = DL_TIMER_CAPTURE_COMBINED_MODE_PULSE_WIDTH_AND_PERIOD,
|
|
.period = CAPTURE_0_INST_LOAD_VALUE,
|
|
.startTimer = DL_TIMER_STOP,
|
|
.inputChan = DL_TIMER_INPUT_CHAN_0,
|
|
.inputInvMode = DL_TIMER_CC_INPUT_INV_NOINVERT,
|
|
};
|
|
|
|
/**
|
|
* @brief Configuration for Periodic Timer.
|
|
*/
|
|
static const DL_TimerG_ClockConfig gTIMER_0ClockConfig = {
|
|
.clockSel = DL_TIMER_CLOCK_LFCLK,
|
|
.divideRatio = DL_TIMER_CLOCK_DIVIDE_1,
|
|
.prescale = 32U,
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief Configuration for Periodic timer Mode and Period.
|
|
*/
|
|
static const DL_TimerG_TimerConfig gTIMER_0TimerConfig = {
|
|
.period = TIMER_0_INST_LOAD_VALUE,
|
|
.timerMode = DL_TIMER_TIMER_MODE_PERIODIC,
|
|
.startTimer = DL_TIMER_STOP,
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief Configuration for PWM timer.
|
|
*/
|
|
static const DL_TimerG_ClockConfig gPWM_0ClockConfig = {
|
|
.clockSel = DL_TIMER_CLOCK_BUSCLK,
|
|
.divideRatio = DL_TIMER_CLOCK_DIVIDE_1,
|
|
.prescale = 31U
|
|
};
|
|
|
|
/**
|
|
* @brief Configuration for PWM time MODE and Periode.
|
|
*/
|
|
static const DL_TimerG_PWMConfig gPWM_0Config = {
|
|
.pwmMode = DL_TIMER_PWM_MODE_EDGE_ALIGN,
|
|
.period = 1000,
|
|
.startTimer = DL_TIMER_STOP,
|
|
};
|
|
|
|
|
|
__attribute__((weak)) void vMCAL_TimerCallback(void)
|
|
{
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Initlization Periodic timer
|
|
* @note Interruptions enabled in this function
|
|
* @param pTimer_inst Timer handle
|
|
* @retval IVEC MCAL status
|
|
*/
|
|
IVEC_McalStatus_e xMCAL_TimerInit(GPTIMER_Regs* const pTimer_inst)
|
|
{
|
|
assert(pTimer_inst == TIMG0);
|
|
if(b_TimerInitFlag==0)
|
|
{
|
|
DL_TimerG_setClockConfig(TIMG0, (DL_TimerG_ClockConfig *) &gTIMER_0ClockConfig);
|
|
DL_TimerG_enableClock(TIMG0);
|
|
DL_TimerG_initTimerMode(TIMG0,(DL_TimerG_TimerConfig *) &gTIMER_0TimerConfig);
|
|
/* Configure Interrupts */
|
|
DL_TimerG_enableInterrupt(TIMG0 , GPTIMER_CPU_INT_IMASK_Z_SET);
|
|
NVIC_ClearPendingIRQ(TIMG0_INT_IRQn);
|
|
NVIC_EnableIRQ(TIMG0_INT_IRQn);
|
|
b_TimerInitFlag = 1;
|
|
return IVEC_MCAL_STATUS_SUCCESS;
|
|
}
|
|
else
|
|
return IVEC_MCAL_STATUS_INIT_FAIL;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief De-Initialize the Timer peripheral to their default reset values
|
|
* @param pTimer_inst Timer handle
|
|
* @retval IVEC MCAL status
|
|
*/
|
|
IVEC_McalStatus_e xMCAL_TimerDeInit(GPTIMER_Regs* const pTimer_inst)
|
|
{
|
|
assert(pTimer_inst == TIMG0);
|
|
if(b_TimerInitFlag == 1 )
|
|
{
|
|
b_TimerInitFlag = 0;
|
|
return IVEC_MCAL_STATUS_SUCCESS;
|
|
}
|
|
else
|
|
return IVEC_MCAL_STATUS_INIT_FAIL;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief To get the timer counts.
|
|
* @param p_u32temp to get the timer counts
|
|
* @retval IVEC MCAL status
|
|
*/
|
|
IVEC_McalStatus_e xMCAL_TimergetCount(uint32_t* p_u32temp)
|
|
{
|
|
if(DL_Timer_isRunning(TIMG0) == 1)
|
|
{
|
|
*p_u32temp = DL_Timer_getTimerCount(TIMG0);
|
|
return IVEC_MCAL_STATUS_SUCCESS;
|
|
}
|
|
return IVEC_MCAL_STATUS_ERROR;
|
|
}
|
|
//IVEC_McalStatus_e xMCAL_TimergetCount(uint32_t* p_u32temp)
|
|
//{
|
|
// assert(DL_Timer_isRunning(TIMG0) == 1);
|
|
// *p_u32temp = DL_Timer_getTimerCount(TIMG0);
|
|
// return IVEC_MCAL_STATUS_SUCCESS;
|
|
//}
|
|
|
|
|
|
/**
|
|
* @brief Start the Timer counter.
|
|
* @retval IVEC MCAL status
|
|
*/
|
|
IVEC_McalStatus_e xMCAL_TimerstartCounter(void)
|
|
{
|
|
if(b_TimerCounterFlag == 0)
|
|
{
|
|
DL_TimerG_startCounter(TIMG0);
|
|
b_TimerCounterFlag = 1;
|
|
return IVEC_MCAL_STATUS_TRUE;
|
|
}
|
|
else
|
|
return IVEC_MCAL_STATUS_FALSE;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Stop the Timer stop.
|
|
* @retval IVEC MCAL status
|
|
*/
|
|
IVEC_McalStatus_e xMCAL_TimerstopCounter(void)
|
|
{
|
|
if(b_TimerCounterFlag == 1)
|
|
{
|
|
DL_TimerG_stopCounter(TIMG0);
|
|
b_TimerCounterFlag = 0;
|
|
return IVEC_MCAL_STATUS_TRUE;
|
|
}
|
|
else
|
|
return IVEC_MCAL_STATUS_FALSE;
|
|
}
|
|
|
|
/**
|
|
* @brief Function use to set load value of timer
|
|
* @param pTimer_inst Timer handle
|
|
* @param u8Channel use to pass the load value
|
|
* @param p_u32temp use to get loaded value
|
|
* @retval IVEC MCAL status
|
|
*/
|
|
IVEC_McalStatus_e xMCAL_TimersetLoadValue(GPTIMER_Regs* const pTimer_inst, uint32_t count,uint32_t* p_u32temp)
|
|
{
|
|
assert(pTimer_inst == TIMG0);
|
|
DL_Timer_setLoadValue(TIMG0, count);
|
|
*p_u32temp = DL_Timer_getLoadValue(TIMG0);
|
|
return IVEC_MCAL_STATUS_TRUE;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Timer call back function
|
|
* @note Interruptions enabled in this function
|
|
*/
|
|
void _prv_timGcallback()
|
|
{
|
|
switch (DL_TimerG_getPendingInterrupt(TIMG0))
|
|
{
|
|
case DL_TIMER_IIDX_ZERO:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Timer IRQ handler
|
|
*/
|
|
void TIMG0_IRQHandler()
|
|
{
|
|
_prv_timGcallback();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
* @brief Initlization PWM timer
|
|
* @note Interruptions enabled in this function
|
|
* @param Tim_inst Timer handle
|
|
* @retval IVEC MCAL status
|
|
*/
|
|
IVEC_McalStatus_e xMCAL_PwmInit(GPTIMER_Regs* const Tim_inst)
|
|
{
|
|
assert(Tim_inst==TIMG0);
|
|
if(b_PwmInitflag ==0)
|
|
{
|
|
DL_TimerG_setClockConfig(TIMG7, (DL_TimerG_ClockConfig *) &gPWM_0ClockConfig);
|
|
DL_TimerG_initPWMMode(TIMG7, (DL_TimerG_PWMConfig *) &gPWM_0Config);
|
|
DL_TimerG_setCaptureCompareValue(TIMG7, 500, DL_TIMER_CC_0_INDEX);
|
|
DL_TimerG_setCaptureCompareOutCtl(TIMG7, GPTIMER_OCTL_01_CCPIV_LOW,
|
|
GPTIMER_OCTL_01_CCPOINV_NOINV, GPTIMER_OCTL_01_CCPO_FUNCVAL,DL_TIMER_CC_0_INDEX);
|
|
DL_TimerG_setCaptCompUpdateMethod(TIMG7, DL_TIMER_CC_UPDATE_METHOD_IMMEDIATE, DL_TIMER_CC_0_INDEX);
|
|
DL_TimerG_enableClock(TIMG7);
|
|
DL_TimerG_setCCPDirection(TIMG7 , GPTIMER_CCPD_C0CCP0_OUTPUT );
|
|
DL_GPIO_initPeripheralOutputFunction(IOMUX_PINCM32,IOMUX_PINCM32_PF_TIMG7_CCP0);
|
|
DL_GPIO_enableOutput(GPIOB, DL_GPIO_PIN_15);
|
|
b_PwmInitflag =1;
|
|
return IVEC_MCAL_STATUS_SUCCESS;
|
|
}
|
|
else
|
|
return IVEC_MCAL_STATUS_INIT_FAIL;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief De-Initlization PWM timer
|
|
* @param Tim_inst Timer handle
|
|
* @retval IVEC MCAL status
|
|
*/
|
|
IVEC_McalStatus_e xMCAL_PwmDeInit(GPTIMER_Regs* const Tim_inst)
|
|
{
|
|
assert(Tim_inst==TIMG0);
|
|
if(b_PwmInitflag ==1)
|
|
{
|
|
b_PwmInitflag =0;
|
|
return IVEC_MCAL_STATUS_SUCCESS;
|
|
}
|
|
else
|
|
return IVEC_MCAL_STATUS_INIT_FAIL;
|
|
}
|
|
|
|
/**
|
|
* @brief Enable PWM Timer.
|
|
* @retval IVEC MCAL status
|
|
*/
|
|
IVEC_McalStatus_e xMCAL_PwmStart(void)
|
|
{
|
|
if (b_Pwmstartflag == 0)
|
|
{
|
|
DL_TimerA_setCoreHaltBehavior(TIMG7, DL_TIMER_CORE_HALT_IMMEDIATE);
|
|
DL_TimerG_startCounter(TIMG7);
|
|
b_Pwmstartflag =1;
|
|
return IVEC_MCAL_STATUS_TRUE;
|
|
}
|
|
else
|
|
return IVEC_MCAL_STATUS_FALSE;
|
|
}
|
|
|
|
/**
|
|
* @brief Disable PWM Timer.
|
|
* @retval IVEC MCAL status
|
|
*/
|
|
IVEC_McalStatus_e xMCAL_PwmStop(void)
|
|
{
|
|
if (b_Pwmstartflag == 1)
|
|
{
|
|
DL_TimerA_setCoreHaltBehavior(TIMG7, DL_TIMER_CORE_HALT_IMMEDIATE);
|
|
DL_TimerG_stopCounter(TIMG7);
|
|
b_Pwmstartflag = 0;
|
|
return IVEC_MCAL_STATUS_TRUE;
|
|
}
|
|
else
|
|
return IVEC_MCAL_STATUS_FALSE;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Initlization Input Capture timer
|
|
* @note Interruptions enabled in this function
|
|
* @param Tim_inst Timer handle
|
|
* @retval IVEC MCAL status
|
|
*/
|
|
IVEC_McalStatus_e xMCAL_InputCaptureInit(GPTIMER_Regs* const Tim_inst)
|
|
{
|
|
assert(Tim_inst==TIMA0);
|
|
if(b_InputCaputerflag ==0)
|
|
{
|
|
DL_TimerA_setClockConfig(TIMA0,(DL_TimerA_ClockConfig *) &gCAPTURE_0ClockConfig);
|
|
DL_TimerA_initCaptureCombinedMode(TIMA0,(DL_TimerA_CaptureCombinedConfig *) &gCAPTURE_0CaptureConfig);
|
|
DL_TimerA_enableInterrupt(TIMA0 , DL_TIMERA_INTERRUPT_CC1_DN_EVENT | DL_TIMERA_INTERRUPT_ZERO_EVENT);
|
|
DL_TimerA_enableClock(TIMA0);
|
|
DL_GPIO_initPeripheralInputFunction(GPIO_CAPTURE_0_C0_IOMUX,GPIO_CAPTURE_0_C0_IOMUX_FUNC);
|
|
b_InputCaputerflag = 1;
|
|
return IVEC_MCAL_STATUS_SUCCESS;
|
|
}
|
|
else
|
|
return IVEC_MCAL_STATUS_INIT_FAIL;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief De-Initlization Input Capture timer
|
|
* @param Tim_inst Timer handle
|
|
* @retval IVEC MCAL status
|
|
*/
|
|
IVEC_McalStatus_e xMCAL_InputCaptureDeInit(GPTIMER_Regs* const Tim_inst)
|
|
{
|
|
assert(Tim_inst==TIMA0);
|
|
if(b_InputCaputerflag ==1)
|
|
{
|
|
b_InputCaputerflag = 0;
|
|
return IVEC_MCAL_STATUS_SUCCESS;
|
|
}
|
|
else
|
|
return IVEC_MCAL_STATUS_INIT_FAIL;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Enable IC timer.
|
|
* @retval IVEC MCAL status
|
|
*/
|
|
IVEC_McalStatus_e xMCAL_InputCaptureStart(void)
|
|
{
|
|
assert(b_InputCaputerflag==1);
|
|
DL_TimerG_setCoreHaltBehavior(TIMA0, DL_TIMER_CORE_HALT_IMMEDIATE);
|
|
NVIC_EnableIRQ(TIMA0_INT_IRQn);
|
|
DL_TimerA_startCounter(TIMA0);
|
|
return IVEC_MCAL_STATUS_TRUE;
|
|
}
|
|
|
|
/**
|
|
* @brief Disable IC timer.
|
|
* @retval IVEC MCAL status
|
|
*/
|
|
IVEC_McalStatus_e xMCAL_InputCaptureStop(void)
|
|
{
|
|
assert(b_InputCaputerflag==1);
|
|
DL_TimerG_setCoreHaltBehavior(TIMA0, DL_TIMER_CORE_HALT_IMMEDIATE);
|
|
NVIC_EnableIRQ(TIMA0_INT_IRQn);
|
|
DL_TimerA_stopCounter(TIMA0);
|
|
return IVEC_MCAL_STATUS_SUCCESS;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Calculate the frequency of input PWM signal.
|
|
* @param f_frequency to get Calculate frequency
|
|
* @retval IVEC MCAL status
|
|
*/
|
|
void vMCAL_Calcfrequency(float* f_frequency){
|
|
|
|
g_u32LoadValue = DL_Timer_getLoadValue(TIMA0);
|
|
g_u32CaptureValue1 = g_u32CaptureValue0;
|
|
g_u32CaptureValue0 = DL_Timer_getCaptureCompareValue(TIMA0, DL_TIMER_CC_1_INDEX);
|
|
|
|
if(g_u32CaptureValue1 > g_u32CaptureValue0)
|
|
{
|
|
g_u32CapturePeriod = g_u32CaptureValue1 - g_u32CaptureValue0;
|
|
}
|
|
else
|
|
{
|
|
g_u32CapturePeriod = g_u32LoadValue + g_u32CaptureValue1 - g_u32CaptureValue0;
|
|
}
|
|
*f_frequency = (timerCAPTURE_FREQ_i32)/(g_u32CapturePeriod + 1) *10;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Timer call back function
|
|
* @note f_frequency: get the calculated frequency
|
|
*/
|
|
void _prv_timAcallback()
|
|
{
|
|
vMCAL_Calcfrequency(&f_frequency);
|
|
}
|
|
|
|
/**
|
|
* @brief Timer IRQ handler
|
|
*/
|
|
void TIMA0_IRQHandler()
|
|
{
|
|
// _prv_timAcallback();
|
|
switch( DL_TimerA_getPendingInterrupt(TIMA0) )
|
|
{
|
|
case DL_TIMER_IIDX_ZERO:
|
|
vMCAL_TimerCallback();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|