/*********************************************************************************//*!
*
* @用ETM完成捕捉
*
* @ 输入 pETM 指向三个ETM定时器其中一个的基址
* @输入 Channel 必须完成配置通道即通道号
* @ 输入 CompareMode 选择模式:翻转(01)、置位(11)、清0(10)
*
* @无返回
*
*********************************************************************************/
void ETM_OutputCompareInit(ETM_Type *pETM, uint8_t u8ETM_Channel, uint8_t u8CompareMode)
{
ASSERT(((ETM0 == pETM) && (u8ETM_Channel < 2)) ||
((ETM1 == pETM) && (u8ETM_Channel < 2)) ||
((ETM2 == pETM) && (u8ETM_Channel < 6))
);
/* open the clock gate */
if(ETM0 == pETM)
{
SIM->SCGC |= SIM_SCGC_ETM0_MASK;
}
else if(ETM1 == pETM)
{
SIM->SCGC |= SIM_SCGC_ETM1_MASK;
}
else
{
SIM->SCGC |= SIM_SCGC_ETM2_MASK;
}
pETM->SC = 0x0; //关闭计数器
pETM->MOD = ETM_MOD_INIT;
pETM->CONTROLS[u8ETM_Channel].CnSC=(ETM_CnSC_MSA_MASK|(u8CompareMode<<2));
pETM->CONTROLS[u8ETM_Channel].CnV = ETM_C0V_INIT;
}
/*****************************************************************************
*
* @配置寄存器 ETMx_SYNCONF,其中里面包含了软件输出的控制是否由硬件触发HW或是否有软件出发SW
* @param[in] pETM 指向三个ETM定时器其中一个的基址
* @param[in] u32ConfigValue ETMx_SYNCONF这个寄存器的值
*
* @无返回
*
*****************************************************************************/
void ETM_SyncConfigDeactivate(ETM_Type *pETM, uint32_t u32ConfigValue)
{
ASSERT((ETM2 == pETM));
pETM->SYNCONF &= ~u32ConfigValue;
}
/*********************************************************************************
* @ETM中配置ETMx_SYNC 寄存器来保证软件同步
*
* @输入 pETM 指向三个ETM定时器其中一个的基址
* @输入 u8TriggerN 选择硬件触发资源
*
* @无返回
*
*********************************************************************************/
void ETM_HardwareSync(ETM_Type *pETM, uint8_t u8TriggerN)
{
ASSERT(ETM2 == pETM);
pETM->SYNCONF |= ETM_SYNCONF_SYNCMODE_MASK;
switch(u8TriggerN)
{
case ETM_SYNC_TRIGGER_TRIGGER2:
pETM->SYNC |= ETM_SYNC_TRIG2_MASK;
break;
case ETM_SYNC_TRIGGER_TRIGGER1:
pETM->SYNC |= ETM_SYNC_TRIG1_MASK;
break;
case ETM_SYNC_TRIGGER_TRIGGER0:
pETM->SYNC |= ETM_SYNC_TRIG0_MASK;
break;
default:
break;
}
}
/*********************************************************************************//*!
*
* @配置软件输出控制SWOCTRL寄存器的同步是否由软件触发
*
* @输入 pETM 指向三个ETM定时器其中一个的基址
* @输入 Channel PWM波的通道选择
* @输入 ChannelValue 0或1,0不触发;1触发
*
* @无返回
*
*********************************************************************************/
void ETM_SWOutputControlSet(ETM_Type *pETM, uint8_t u8ETM_Channel, uint8_t u8ChannelValue)
{
ASSERT((ETM2 == pETM) && (u8ETM_Channel < 6));
if(ETM_SWOCTRL_HIGH == u8ChannelValue)
{
pETM->SWOCTRL |= (0x0101 << u8ETM_Channel);
}
else if(ETM_SWOCTRL_LOW == u8ChannelValue)
{
pETM->SWOCTRL |= (1 << u8ETM_Channel);
pETM->SWOCTRL &= ~(0x100 << u8ETM_Channel);
}
if(pETM->SYNCONF & ETM_SYNCONF_SWOC_MASK) /* if PWM sync is needed */
{
pETM->SYNCONF |= ETM_SYNCONF_SYNCMODE_MASK; /* recommend enhanced sync mode */
if(pETM->SYNCONF & ETM_SYNCONF_SWSOC_MASK) /* if software sync is needed*/
{
pETM->SYNC |= ETM_SYNC_SWSYNC_MASK; /* software sync */
}
else if(pETM->SYNCONF & ETM_SYNCONF_HWSOC_MASK) /* if hardware sync is needed*/
{
pETM->SYNC |= ETM_SYNC_TRIG2_MASK;
#if defined(CPU_NV32)
SIM->SOPT |= SIM_SOPT_ETMSYNC_MASK; /* hardware sync */
#elif defined(CPU_NV32M3)
SIM->SOPT |= SIM_SOPT_ETMSYNC_MASK; /* hardware sync */
#elif defined(CPU_NV32M4)
SIM->SOPT0 |= SIM_SOPT0_ETMSYNC_MASK; /* hardware sync */
#endif
}
}
else /* no need to sync, update on the next rising edge of system clock */
{
}
}
/*********************************************************************************//*!
*
* @通过配置ETM保证硬件同步,产生触发
*
* @输入 pETM 指向三个ETM定时器其中一个的基址
*@输入 u8TriggerMask 选择硬件触发资源. combine TRIG0~TREG2.(x000xxxx~x111xxxx)
*
* @无返回.
*
*********************************************************************************/
void ETM_HardwareSyncCombine(ETM_Type *pETM, uint8_t u8TriggerMask)
{
ASSERT(ETM2 == pETM);
pETM->SYNCONF |= ETM_SYNCONF_SYNCMODE_MASK;
pETM->SYNC &= 0x8F;
pETM->SYNC |= (u8TriggerMask & 0x70);
}
/*****************************************************************************//*!
*
* @ETM初始化函数
*
* @输入 pETM 指向三个ETM定时器其中一个的基址
* @输入 pConfig 指向ETM的一些基本参数
* @无返回值
*
*****************************************************************************/
void ETM_Init(ETM_Type *pETM, ETM_ConfigType *pConfig)
{
ASSERT((ETM0 == pETM) || (ETM1 == pETM) || (ETM2 == pETM));
if(ETM0 == pETM)
{
SIM->SCGC |= SIM_SCGC_ETM0_MASK;
}
else
{
SIM->SCGC |= SIM_SCGC_ETM2_MASK;
}
/* diable counter */
pETM->SC = 0;
pETM->MODE = pConfig->mode;
pETM->MOD = pConfig->modulo;
pETM->CNT = pConfig->cnt;
if( pETM->MODE & ETM_MODE_ETMEN_MASK )
{
/* when ETMEN = 1, all other registers can be written */
pETM->COMBINE = pConfig->combine;
pETM->CNTIN = pConfig->cntin;
pETM->SYNC = pConfig->sync;
pETM->OUTINIT = pConfig->outinit;
pETM->OUTMASK = pConfig->outmask;
pETM->DEADETME = pConfig->deadETMe;
pETM->EXTTRIG = pConfig->exttrig;
pETM->POL = pConfig->pol;
pETM->FMS = pConfig->fms;
pETM->FILTER = pConfig->filter;
pETM->FLTCTRL = pConfig->fltctrl;
pETM->FLTPOL = pConfig->fltpol;
pETM->CONF = pConfig->conf;
pETM->SYNCONF = pConfig->synconf;
pETM->SWOCTRL = pConfig->swoctrl;
pETM->PWMLOAD = pConfig->pwmload;
}
/* write SC to enable clock */
pETM->SC = pConfig->sc;
}
/*****************************************************************************
关闭相应的ETM功能组件函数
*****************************************************************************/
void ETM_DeInit(ETM_Type *pETM)
{
ASSERT((ETM0 == pETM) || (ETM1 == pETM) || (ETM2 == pETM));
pETM->SC = 0;
pETM->MOD = 0;
pETM->CNT = 0;
if(ETM2 == pETM)
{ pETM->MODE = 0x4;
pETM->COMBINE = 0;
pETM->CNTIN = 0;
pETM->SYNC = 0;
pETM->OUTINIT = 0;
pETM->OUTMASK = 0;
pETM->DEADETME = 0;
pETM->EXTTRIG = 0;
pETM->POL = 0;
pETM->FMS = 0;
pETM->FILTER = 0;
pETM->FLTCTRL = 0;
pETM->FLTPOL = 0;
pETM->CONF = 0;
pETM->SYNCONF = 0;
pETM->SWOCTRL = 0;
pETM->PWMLOAD = 0;
}
if (ETM0 == pETM)
{
SIM->SCGC &= ~SIM_SCGC_ETM0_MASK;
NVIC_DisableIRQ(ETM0_IRQn);
}
else if (ETM2 == pETM)
SIM->SCGC &= ~SIM_SCGC_ETM2_MASK;
NVIC_DisableIRQ(ETM2_IRQn);
}
/*********************************************************************************//*!
*
* @运用ETM2来实现软件同步
*
* @输入 pETM 指向三个ETM定时器其中一个的基址
*
* @无返回
*
*********************************************************************************/
void ETM_SoftwareSync(ETM_Type *pETM)
{
ASSERT(ETM2 == pETM);
pETM->SYNCONF |= ETM_SYNCONF_SYNCMODE_MASK;
pETM->SYNC |= ETM_SYNC_SWSYNC_MASK;
}
/**********************************************************************************
*
* @产生ETM2硬件触发,Note: please call ETM_HardwareSyncCombine first.
*
* @输入 pETM 指向三个ETM定时器其中一个的基址
*
* @无返回
*
*********************************************************************************/
void ETM_GenerateTrig2(ETM_Type *pETM)
{
ASSERT(ETM2 == pETM);
if(pETM->SYNC & ETM_SYNC_TRIG2_MASK)
{
#if defined(CPU_NV32)
SIM->SOPT |= SIM_SOPT_ETMSYNC_MASK;
#endif
}
}
/*********************************************************************************
*
* @选择BDM模式下的ETM行为
*
* @输入 pETM 指向三个ETM定时器其中一个的基址
* @输入 u8DebugMode debug 的模式从00-11之间选择
*
* @无返回
*
*********************************************************************************/
void ETM_SetDebugModeBehavior(ETM_Type *pETM, uint8_t u8DebugMode)
{
ASSERT((ETM2 == pETM));
pETM->CONF &= ~ETM_CONF_BDMMODE_MASK;
pETM->CONF |= ETM_CONF_BDMMODE(u8DebugMode);
}
/*********************************************************************************
* @交换通道CH(n)和通道CH(n+1)的输出结果
* @输入 pETM 其中一个ETM定时器的基址
* @输入 ChannelPair 要被交换的通道数号,即n可为0,1,2,
*********************************************************************************/
void ETM_InvertChannel(ETM_Type *pETM, uint8_t u8ChannelPair)
{
ASSERT((ETM2 == pETM) && u8ChannelPair <= 2);
pETM->INVCTRL |= 1<<u8ChannelPair;
if(pETM->SYNCONF & ETM_SYNCONF_INVC_MASK) /* if PWM sync is needed */
{
pETM->SYNCONF |= ETM_SYNCONF_SYNCMODE_MASK; /* recommend enhanced sync mode */
if(pETM->SYNCONF & ETM_SYNCONF_SWINVC_MASK) /* if software sync is needed*/
{
pETM->SYNC |= ETM_SYNC_SWSYNC_MASK; /* software sync */
}
else if(pETM->SYNCONF & ETM_SYNCONF_HWINVC_MASK) /* if hardware sync is needed*/
{
pETM->SYNC |= ETM_SYNC_TRIG2_MASK;
#if defined(CPU_NV32)
SIM->SOPT |= SIM_SOPT_ETMSYNC_MASK; /* hardware sync */
#endif
}
}
else
{
}
}
/*****************************************************************************//*!
*
* @本函数用来配置ETM通道, 包括通道状态及控制寄存器CnSC和通道计数值寄存器CnV
* @输入 pETM 指向三个ETM定时器其中一个的基址
* @输入 ETM_Channel ETM的通道号
* @输入 pTETMCH_Params 指向ETM通道一般参数的指针
*
* @无返回值
*
*****************************************************************************/
void ETM_ChannelInit(ETM_Type *pETM, uint8_t u8ETM_Channel, ETM_ChParamsType *pTETMCH_Params)
{
ASSERT((ETM0 == pETM) || (ETM1 == pETM) || (ETM2 == pETM)); //断言检测定时器号是否正确
if (ETM0 == pETM)
{
ASSERT(u8ETM_Channel < 2);
SIM->SCGC |= SIM_SCGC_ETM0_MASK;
}
else
{
ASSERT(u8ETM_Channel < 6);
SIM->SCGC |= SIM_SCGC_ETM2_MASK;
}
pETM->CONTROLS[u8ETM_Channel].CnSC = pTETMCH_Params->u8CnSC;
pETM->CONTROLS[u8ETM_Channel].CnV = pTETMCH_Params->u16CnV;
return;
}
/*****************************************************************************//*!
*
* @brief This function sets the callback function.
*
* @param[in] pETM pointer to one of three ETM base register address.
* @param[in] pfnCallback functon address.
*
* @return none.
*
* @ Pass/ Fail criteria: none
*
*****************************************************************************/
void ETM_SetCallback(ETM_Type *pETM, ETM_CallbackPtr pfnCallback)
{
ETM_Callback[((uint32_t)pETM - (uint32_t)ETM0_BASE)>>12] = pfnCallback;
}
/*! @} End of ETM_api_list */
/*****************************************************************************//*!
*
* @brief ETM0_Isr interrupt service routine.
*
* @param none.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*
*****************************************************************************/
void ETM0_Isr(void)
{
if(ETM_Callback[0])
{
ETM_Callback[0]();
}
}
/*****************************************************************************//*!
*
* @brief ETM1_Isr interrupt service routine.
*
* @param none.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*
*****************************************************************************/
void ETM1_Isr(void)
{
if(ETM_Callback[1])
{
ETM_Callback[1]();
}
}
/*****************************************************************************//*!
*
* @brief ETM2_Isr interrupt service routine.
*
* @param none.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*
*****************************************************************************/
void ETM2_Isr(void)
{
if(ETM_Callback[2])
{
ETM_Callback[2]();
}
}
|