#define TIMER_CLOCK 72000000
#define PRESCALER 1
uint16_t channel0_val=0;
uint16_t channel1_val=0;
float frequency;
void ftm_init()
{
ftm_dual_edge_capture_param_t param;
ftm_user_config_t user_info;
FTM_Type *ftmBase = g_ftmBase[BOARD_FTM_INSTANCE];
FTM_HAL_ClearChnEventFlag(ftmBase, BOARD_FTM_X_CHANNEL); //should be set zero initially
FTM_HAL_ClearChnEventFlag(ftmBase, (BOARD_FTM_X_CHANNEL+1)); //should be set zero initially
user_info.tofFrequency=0; /*!< Select ratio between number of overflows to times TOF is set @internal gui name="Overflow frequency" id="OvFrequency" */
user_info.BDMMode= kFtmBdmMode_11; /*!< Select FTM behavior in BDM mode @internal gui name="BDM mode" id="BdmMode" */
user_info.isWriteProtection=false; /*!< true: enable write protection, false: write protection is disabled @internal gui name="Write protection" id="WriteProtection" */ user_info.syncMethod=kFtmUseSoftwareTrig;
FTM_DRV_Init(BOARD_FTM_INSTANCE, &user_info);
FTM_DRV_SetClock(BOARD_FTM_INSTANCE, kClock_source_FTM_SystemClk, kFtmDividedBy1);
param.mode=kFtmContinuous;
param.currChanEdgeMode=kFtmRisingEdge;
param.nextChanEdgeMode=kFtmRisingEdge;
FTM_DRV_SetupChnDualEdgeCapture(BOARD_FTM_INSTANCE, ¶m,BOARD_FTM_X_CHANNEL,0);
}
void ftm_channel_data()
{
FTM_Type *ftmBase = g_ftmBase[BOARD_FTM_INSTANCE];
if ((FTM_HAL_HasChnEventOccurred(ftmBase,BOARD_FTM_X_CHANNEL)== 1)&&(FTM_HAL_HasChnEventOccurred(ftmBase,(BOARD_FTM_X_CHANNEL+1))== 1))
{
channel0_val=FTM_HAL_GetChnCountVal(ftmBase,BOARD_FTM_X_CHANNEL); //check this
channel1_val=FTM_HAL_GetChnCountVal(ftmBase,(BOARD_FTM_X_CHANNEL+1)); //check this
FTM_HAL_ClearChnEventFlag(ftmBase, BOARD_FTM_X_CHANNEL);
FTM_HAL_ClearChnEventFlag(ftmBase, (BOARD_FTM_X_CHANNEL+1));
}
}
void frequency_calculation()
{
uint16_t capture=0;
if (channel1_val>channel0_val)
{
capture=channel1_val - channel0_val;
}
else
{
capture=65535 - channel1_val + channel0_val;
}
frequency=(float)(TIMER_CLOCK / (PRESCALER+1)) / capture;
}