static void ResetFTM(uint32_t instance) { FTM_Type *ftmBase = g_ftmBase[instance]; uint8_t channel; /* reset all values */ FTM_HAL_SetCounter(ftmBase, 0); /* reset FTM counter */ FTM_HAL_ClearTimerOverflow(ftmBase); /* clear timer overflow flag (if any) */ for(channel=0; channel<NOF_FTM_CHANNELS; channel++) { FTM_HAL_ClearChnEventFlag(ftmBase, channel); /* clear channel flag */ FTM_HAL_SetChnDmaCmd(ftmBase, channel, true); /* enable DMA request */ FTM_HAL_EnableChnInt(ftmBase, channel); /* enable channel interrupt: need to have both DMA and CHnIE set for DMA transfers! See RM 40.4.23 */ } }
static void StopFTMDMA(uint32_t instance) { FTM_Type *ftmBase = g_ftmBase[instance]; uint8_t channel; StartStopFTM(instance, false); /* stop FTM timer */ /* reset all values */ FTM_HAL_SetCounter(ftmBase, 0); /* reset FTM counter */ FTM_HAL_ClearTimerOverflow(ftmBase); /* clear timer overflow flag (if any) */ for(channel=0; channel<NOF_FTM_CHANNELS; channel++) { FTM_HAL_DisableChnInt(ftmBase, channel); /* disable channel interrupt */ FTM_HAL_SetChnDmaCmd(ftmBase, channel, false); /* disable DMA request */ FTM_HAL_ClearChnEventFlag(ftmBase, channel); /* clear channel flag */ } }
/*FUNCTION********************************************************************** * * Function Name : FTM_DRV_PwmStop * Description : Stops channel PWM. * *END**************************************************************************/ void FTM_DRV_PwmStop(uint32_t instance, ftm_pwm_param_t *param, uint8_t channel) { assert((param->mode == kFtmEdgeAlignedPWM) || (param->mode == kFtmCenterAlignedPWM) || (param->mode == kFtmCombinedPWM)); assert(instance < FTM_INSTANCE_COUNT); assert(channel < g_ftmChannelCount[instance]); FTM_Type *ftmBase = g_ftmBase[instance]; /* Stop the FTM counter */ FTM_HAL_SetClockSource(ftmBase, kClock_source_FTM_None); FTM_HAL_DisablePwmMode(ftmBase, param, channel); /* Clear out the registers */ FTM_HAL_SetMod(ftmBase, 0); FTM_HAL_SetCounter(ftmBase, 0); }
/*See fsl_ftm_driver.h for documentation of this function.*/ void FTM_DRV_PwmStop(uint8_t instance, ftm_pwm_param_t *param, uint8_t channel) { assert((param->mode == kFtmEdgeAlignedPWM) || (param->mode == kFtmCenterAlignedPWM) || (param->mode == kFtmCombinedPWM)); assert(instance < HW_FTM_INSTANCE_COUNT); assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNTn(instance)); uint32_t ftmBaseAddr = g_ftmBaseAddr[instance]; /* Stop the FTM counter */ FTM_HAL_SetClockSource(ftmBaseAddr, kClock_source_FTM_None); FTM_HAL_DisablePwmMode(ftmBaseAddr, param, channel); /* Clear out the registers */ FTM_HAL_SetMod(ftmBaseAddr, 0); FTM_HAL_SetCounter(ftmBaseAddr, 0); }
/*FUNCTION********************************************************************** * * Function Name : FTM_DRV_CounterStart * Description : Starts the FTM counter. This function provides access to the * FTM counter. The counter can be run in Up-counting and Up-down counting modes. * To run the counter in Free running mode, choose Up-counting option and provide * 0x0 for the countStartVal and 0xFFFF for countFinalVal. * *END**************************************************************************/ void FTM_DRV_CounterStart(uint32_t instance, ftm_counting_mode_t countMode, uint32_t countStartVal, uint32_t countFinalVal, bool enableOverflowInt) { assert(instance < FTM_INSTANCE_COUNT); FTM_Type *ftmBase = g_ftmBase[instance]; uint32_t channel = 0; /* Clear the overflow flag */ FTM_HAL_ClearTimerOverflow(ftmBase); FTM_HAL_SetCounterInitVal(ftmBase, countStartVal); FTM_HAL_SetMod(ftmBase, countFinalVal); FTM_HAL_SetCounter(ftmBase, 0); /* Use FTM as counter, disable all the channels */ for (channel = 0; channel < g_ftmChannelCount[instance]; channel++) { FTM_HAL_SetChnEdgeLevel(ftmBase, channel, 0); } if (countMode == kCounting_FTM_UP) { FTM_HAL_SetQuadDecoderCmd(ftmBase, false); FTM_HAL_SetCpwms(ftmBase, 0); } else if (countMode == kCounting_FTM_UpDown) { FTM_HAL_SetQuadDecoderCmd(ftmBase, false); FTM_HAL_SetCpwms(ftmBase, 1); } /* Activate interrupts if required */ FTM_DRV_SetTimeOverflowIntCmd(instance, enableOverflowInt); /* Set clock source to start the counter */ FTM_HAL_SetClockSource(ftmBase, s_ftmClockSource); }
void FTM_DRV_CounterStart(uint8_t instance, ftm_counting_mode_t countMode, uint32_t countStartVal, uint32_t countFinalVal, bool enableOverflowInt) { assert(instance < HW_FTM_INSTANCE_COUNT); uint32_t ftmBaseAddr = g_ftmBaseAddr[instance]; uint32_t channel = 0; /* Clear the overflow flag */ FTM_HAL_ClearTimerOverflow(ftmBaseAddr); FTM_HAL_SetCounterInitVal(ftmBaseAddr, countStartVal); FTM_HAL_SetMod(ftmBaseAddr, countFinalVal); FTM_HAL_SetCounter(ftmBaseAddr, 0); /* Use FTM as counter, disable all the channels */ for (channel = 0; channel < FSL_FEATURE_FTM_CHANNEL_COUNTn(instance); channel++) { FTM_HAL_SetChnEdgeLevel(ftmBaseAddr, channel, 0); } if (countMode == kCounting_FTM_UP) { FTM_HAL_SetQuadDecoderCmd(ftmBaseAddr, false); FTM_HAL_SetCpwms(ftmBaseAddr, 0); } else if (countMode == kCounting_FTM_UpDown) { FTM_HAL_SetQuadDecoderCmd(ftmBaseAddr, false); FTM_HAL_SetCpwms(ftmBaseAddr, 1); } /* Activate interrupts if required */ FTM_DRV_SetTimeOverflowIntCmd(instance, enableOverflowInt); /* Set clock source to start the counter */ FTM_HAL_SetClockSource(ftmBaseAddr, kClock_source_FTM_SystemClk); }
/************************************************************************* * Function Name: FTM0_init * Parameters: none * Return: none * Description: FlexTimer 0 initialization *************************************************************************/ void FTM0_init(void) { FTM_HAL_SetWriteProtectionCmd(FTM0_BASE_PTR, false);//false: Write-protection is disabled FTM_HAL_Enable(FTM0_BASE_PTR, true);//true: all registers including FTM-specific registers are available FTM_HAL_SetCounterInitVal(FTM0_BASE_PTR, (uint16_t)(-PWM_MODULO/2)); FTM_HAL_SetMod(FTM0_BASE_PTR, (uint16_t)PWM_MODULO/2-1); // 20 kHz FTM_HAL_SetChnEdgeLevel(FTM0_BASE_PTR, 0, 2); FTM_HAL_SetChnEdgeLevel(FTM0_BASE_PTR, 1, 2); FTM_HAL_SetChnEdgeLevel(FTM0_BASE_PTR, 2, 2); FTM_HAL_SetChnEdgeLevel(FTM0_BASE_PTR, 3, 2); FTM_HAL_SetChnEdgeLevel(FTM0_BASE_PTR, 4, 2); FTM_HAL_SetChnEdgeLevel(FTM0_BASE_PTR, 5, 2); // output mask updated on PWM synchronization (not on rising edge of clock) FTM_HAL_SetMaxLoadingCmd(FTM0_BASE_PTR, true);//True to enable minimum loading point FTM_HAL_SetOutmaskPwmSyncModeCmd(FTM0_BASE_PTR, 1);//true if OUTMASK register is updated only by PWM sync\n FTM_HAL_SetDualChnPwmSyncCmd(FTM0_BASE_PTR, 0, true);//True to enable PWM synchronization FTM_HAL_SetDualChnPwmSyncCmd(FTM0_BASE_PTR, 1, true); FTM_HAL_SetDualChnPwmSyncCmd(FTM0_BASE_PTR, 2, true); FTM_HAL_SetDualChnDeadtimeCmd(FTM0_BASE_PTR, 0, true);//True to enable deadtime insertion, false to disable FTM_HAL_SetDualChnDeadtimeCmd(FTM0_BASE_PTR, 1, true); FTM_HAL_SetDualChnDeadtimeCmd(FTM0_BASE_PTR, 2, true); FTM_HAL_SetDualChnCompCmd(FTM0_BASE_PTR, 0, true);//True to enable complementary mode, false to disable FTM_HAL_SetDualChnCompCmd(FTM0_BASE_PTR, 1, true); FTM_HAL_SetDualChnCompCmd(FTM0_BASE_PTR, 2, true); FTM_HAL_SetDualChnCombineCmd(FTM0_BASE_PTR, 0, true);// True to enable channel pair to combine, false to disable FTM_HAL_SetDualChnCombineCmd(FTM0_BASE_PTR, 1, true); FTM_HAL_SetDualChnCombineCmd(FTM0_BASE_PTR, 2, true); // High transistors have negative polarity (MC33927/37) FTM_HAL_SetDeadtimePrescale(FTM0_BASE_PTR, kFtmDivided1); FTM_HAL_SetDeadtimeCount(FTM0_BASE_PTR, FTM_DEADTIME_DTVAL(63)); // DTVAL - deadtime value (0-63): deadtime period = DTPS x DTVAL FTM_HAL_SetInitTriggerCmd(FTM0_BASE_PTR, true);//True to enable, false to disable FTM_HAL_SetChnOutputPolarityCmd(FTM0_BASE_PTR, 0, 1); FTM_HAL_SetChnOutputPolarityCmd(FTM0_BASE_PTR, 2, 1); FTM_HAL_SetChnOutputPolarityCmd(FTM0_BASE_PTR, 4, 1); /* Following line configures: - enhanced PWM synchronization, FTM counter reset on SW sync - output SW control / polarity registers updated on PWM synchronization (not on rising edge of clock) - output SW control/inverting(swap)/mask registers updated from buffers on SW synchronization */ FTM_HAL_SetPwmSyncModeCmd(FTM0_BASE_PTR, true);// true means use Enhanced PWM synchronization\n FTM_HAL_SetCounterSoftwareSyncModeCmd(FTM0_BASE_PTR, true);//true means software trigger activates register sync\n FTM_HAL_SetSwoctrlPwmSyncModeCmd(FTM0_BASE_PTR, true);//true means SWOCTRL register is updated by PWM synch\n FTM_HAL_SetInvctrlPwmSyncModeCmd(FTM0_BASE_PTR, true);//true means INVCTRL register is updated by PWM synch\n FTM_HAL_SetSwoctrlSoftwareSyncModeCmd(FTM0_BASE_PTR, true);//true means software trigger activates register sync\n FTM_HAL_SetInvctrlSoftwareSyncModeCmd(FTM0_BASE_PTR, true);//true means software trigger activates register sync\n FTM_HAL_SetOutmaskSoftwareSyncModeCmd(FTM0_BASE_PTR, true);//true means software FTM_HAL_SetChnOutputMask(FTM0_BASE_PTR, 0, 1);//Sets the FTM peripheral timer channel output mask. FTM_HAL_SetChnOutputMask(FTM0_BASE_PTR, 1, 1); FTM_HAL_SetChnOutputMask(FTM0_BASE_PTR, 2, 1); FTM_HAL_SetChnOutputMask(FTM0_BASE_PTR, 3, 1); FTM_HAL_SetChnOutputMask(FTM0_BASE_PTR, 4, 1); FTM_HAL_SetChnOutputMask(FTM0_BASE_PTR, 5, 1); FTM_HAL_SetCounter(FTM0_BASE_PTR, 1U); // update of FTM settings // no ISR, counting up, system clock, divide by 1 FTM_HAL_SetClockSource(FTM0_BASE_PTR, kClock_source_FTM_SystemClk); FTM_HAL_SetChnCountVal(FTM0_BASE_PTR, 0, (uint16_t)(-PWM_MODULO/4)); FTM_HAL_SetChnCountVal(FTM0_BASE_PTR, 1,(uint16_t) PWM_MODULO/4); FTM_HAL_SetChnCountVal(FTM0_BASE_PTR, 2, (uint16_t)(-PWM_MODULO/4)); FTM_HAL_SetChnCountVal(FTM0_BASE_PTR, 3,(uint16_t) PWM_MODULO/4); FTM_HAL_SetChnCountVal(FTM0_BASE_PTR, 4, (uint16_t)(-PWM_MODULO/4)); FTM_HAL_SetChnCountVal(FTM0_BASE_PTR, 5,(uint16_t) PWM_MODULO/4); FTM_HAL_SetSoftwareTriggerCmd(FTM0_BASE_PTR, 1); FTM_HAL_SetPwmLoadCmd(FTM0_BASE_PTR, 1); // FTM0 PWM output pins PORT_HAL_SetMuxMode(PORTC_BASE_PTR, 1, kPortMuxAlt4); PORT_HAL_SetMuxMode(PORTC_BASE_PTR, 3, kPortMuxAlt4); PORT_HAL_SetMuxMode(PORTC_BASE_PTR, 4, kPortMuxAlt4); PORT_HAL_SetMuxMode(PORTD_BASE_PTR, 4, kPortMuxAlt4); PORT_HAL_SetMuxMode(PORTD_BASE_PTR, 5, kPortMuxAlt4); #if defined(KV10Z7_SERIES) PORT_HAL_SetMuxMode(PORTE_BASE_PTR, 25, kPortMuxAlt3); #elif (defined(KV10Z1287_SERIES) || defined(KV11Z7_SERIES)) PORT_HAL_SetMuxMode(PORTC_BASE_PTR, 2, kPortMuxAlt4); #endif GPIO_HAL_SetPinDir(GPIOC_BASE_PTR, 1, kGpioDigitalOutput); GPIO_HAL_SetPinDir(GPIOC_BASE_PTR, 3, kGpioDigitalOutput); GPIO_HAL_SetPinDir(GPIOC_BASE_PTR, 4, kGpioDigitalOutput); GPIO_HAL_SetPinDir(GPIOD_BASE_PTR, 4, kGpioDigitalOutput); GPIO_HAL_SetPinDir(GPIOD_BASE_PTR, 5, kGpioDigitalOutput); #if defined(KV10Z7_SERIES) GPIO_HAL_SetPinDir(GPIOE_BASE_PTR, 25, kGpioDigitalOutput); #elif (defined(KV10Z1287_SERIES) || defined(KV11Z7_SERIES)) GPIO_HAL_SetPinDir(GPIOC_BASE_PTR, 2, kGpioDigitalOutput); #endif }