Kinetis SDK v.1.3 API Reference Manual  Rev. 0
Freescale Semiconductor, Inc.
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
FlexTimer Peripheral Driver

Overview

This section describes the programming interface of the FlexTimer Peripheral driver.

FlexTimer Overview

The FlexTimer module is a timer that supports input capture, output compare, and generation of PWM signals. The current Kinetis SDK driver only supports the generation of PWM signals. The input capture and output compare will be supported in upcoming Kinetis SDK releases.

FlexTimer Initialization

  1. To initialize the FlexTimer driver, call the FTM_DRV_Init() function and pass the instance number of the relevant FTM. For example, to use FTM0, pass a value of 0 to the initialization function.
  2. Pass a user configuration structure ftm_user_config_t, as shown here:
// FTM configuration structure
typedef struct FtmUserConfig {
uint8_t tofFrequency;
bool isFTMMode;
uint8_t BDMMode;
bool isWriteProtection;
bool isTimerOverFlowInterrupt;
bool isFaultInterrupt;

FlexTimer Generate a PWM signal

FTM calls the FTM_DRV_PwmStart() function to generate a PWM signal. Use this structure to configure different parameters for the PWM signal.

typedef struct FtmPwmParam
{
uint32_t uFrequencyHZ;
uint32_t uDutyCyclePercent;
0 = inactive signal(0% duty cycle)...
100 = active signal (100% duty cycle). //
uint16_t uFirstEdgeDelayPercent;
Specifies the delay to the first edge in a PWM period.
If unsure leave as 0. Should be specified as
percentage of the PWM period//

The mode options are kFtmEdgeAlignedPWM, kFtmCenterAlignedPWM, and kFtmCombinedPWM. For edge mode, the options available are kFtmHighTrue and kFtmLowTrue. Specify the PWM signal frequency in Hz and the duty cycle percentage (value between 0-100). If the PWM mode is kFtmCombinedPWM and if the user chooses to specify a value for the uFirstEdgeDelayPercent, start of the PWM pulse is delayed.

Data Structures

struct  ftm_user_config_t
 Configuration structure that the user needs to set. More...
 

Functions

ftm_status_t FTM_DRV_Init (uint32_t instance, const ftm_user_config_t *info)
 Initializes the FTM driver. More...
 
void FTM_DRV_Deinit (uint32_t instance)
 Shuts down the FTM driver. More...
 
void FTM_DRV_PwmStop (uint32_t instance, ftm_pwm_param_t *param, uint8_t channel)
 Stops the channel PWM. More...
 
ftm_status_t FTM_DRV_PwmStart (uint32_t instance, ftm_pwm_param_t *param, uint8_t channel)
 Configures the duty cycle and frequency and starts outputting the PWM on a specified channel . More...
 
void FTM_DRV_QuadDecodeStart (uint32_t instance, ftm_phase_params_t *phaseAParams, ftm_phase_params_t *phaseBParams, ftm_quad_decode_mode_t quadMode)
 Configures the parameters and activates the quadrature decode mode. More...
 
void FTM_DRV_QuadDecodeStop (uint32_t instance)
 Deactivates the quadrature decode mode. More...
 
void FTM_DRV_CounterStart (uint32_t instance, ftm_counting_mode_t countMode, uint32_t countStartVal, uint32_t countFinalVal, bool enableOverflowInt)
 Starts the FTM counter. More...
 
void FTM_DRV_CounterStop (uint32_t instance)
 Stops the FTM counter. More...
 
uint32_t FTM_DRV_CounterRead (uint32_t instance)
 Reads back the current value of the FTM counter. More...
 
void FTM_DRV_SetClock (uint8_t instance, ftm_clock_source_t clock, ftm_clock_ps_t clockPs)
 Sets the FTM clock source. More...
 
uint32_t FTM_DRV_GetClock (uint8_t instance)
 Retrieves the frequency of the clock source feeding the FTM counter. More...
 
void FTM_DRV_SetTimeOverflowIntCmd (uint32_t instance, bool overflowEnable)
 Enables or disables the timer overflow interrupt. More...
 
void FTM_DRV_SetFaultIntCmd (uint32_t instance, bool faultEnable)
 Enables or disables the fault interrupt. More...
 
void FTM_DRV_SetupChnInputCapture (uint32_t instance, ftm_input_capture_edge_mode_t captureMode, uint8_t channel, uint8_t filterVal)
 Enables capturing an input signal on the channel using the function parameters. More...
 
void FTM_DRV_SetupChnOutputCompare (uint32_t instance, ftm_output_compare_edge_mode_t compareMode, uint8_t channel, uint32_t compareVal)
 Configures the FTM to generate timed pulses. More...
 
void FTM_DRV_SetupChnDualEdgeCapture (uint32_t instance, ftm_dual_edge_capture_param_t *param, uint8_t channel, uint8_t filterVal)
 Configures the dual edge capture mode of the FTM. More...
 
void FTM_DRV_IRQHandler (uint32_t instance)
 Action to take when an FTM interrupt is triggered. More...
 

Variables

FTM_Type *const g_ftmBase [FTM_INSTANCE_COUNT]
 Table of base addresses for FTM instances. More...
 
const IRQn_Type g_ftmIrqId [FTM_INSTANCE_COUNT]
 Table to save FTM IRQ enumeration numbers defined in the CMSIS header file. More...
 

Data Structure Documentation

struct ftm_user_config_t

Data Fields

uint8_t tofFrequency
 Select ratio between number of overflows to times TOF is set.
 
ftm_bdm_mode_t BDMMode
 Select FTM behavior in BDM mode.
 
bool isWriteProtection
 true: enable write protection, false: write protection is disabled
 
uint32_t syncMethod
 Register synch options available in the ftm_sync_method_t enumeration.
 

Function Documentation

ftm_status_t FTM_DRV_Init ( uint32_t  instance,
const ftm_user_config_t info 
)
Parameters
instanceThe FTM peripheral instance number.
infoThe FTM user configuration structure, see ftm_user_config_t.
Returns
kStatusFtmSuccess means success. Otherwise means failure.
void FTM_DRV_Deinit ( uint32_t  instance)
Parameters
instanceThe FTM peripheral instance number.
void FTM_DRV_PwmStop ( uint32_t  instance,
ftm_pwm_param_t param,
uint8_t  channel 
)
Parameters
instanceThe FTM peripheral instance number.
paramFTM driver PWM parameter to configure PWM options
channelThe channel number. In combined mode, the code finds the channel pair associated with the channel number passed in.
ftm_status_t FTM_DRV_PwmStart ( uint32_t  instance,
ftm_pwm_param_t param,
uint8_t  channel 
)
Parameters
instanceThe FTM peripheral instance number.
paramFTM driver PWM parameter to configure PWM options
channelThe channel number. In combined mode, the code finds the channel pair associated with the channel number passed in.
Returns
kStatusFtmSuccess if the PWM setup was successful, kStatusFtmError on failure as the PWM counter is disabled
void FTM_DRV_QuadDecodeStart ( uint32_t  instance,
ftm_phase_params_t phaseAParams,
ftm_phase_params_t phaseBParams,
ftm_quad_decode_mode_t  quadMode 
)
Parameters
instanceInstance number of the FTM module.
phaseAParamsPhase A configuration parameters
phaseBParamsPhase B configuration parameters
quadModeSelects encoding mode used in quadrature decoder mode
void FTM_DRV_QuadDecodeStop ( uint32_t  instance)
Parameters
instanceInstance number of the FTM module.
void FTM_DRV_CounterStart ( uint32_t  instance,
ftm_counting_mode_t  countMode,
uint32_t  countStartVal,
uint32_t  countFinalVal,
bool  enableOverflowInt 
)

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 the up-counting option and provide 0x0 for the countStartVal and 0xFFFF for countFinalVal.

Parameters
instanceThe FTM peripheral instance number.
countModeThe FTM counter mode defined by ftm_counting_mode_t.
countStartValThe starting value that is stored in the CNTIN register.
countFinalValThe final value that is stored in the MOD register.
enableOverflowInttrue: enable timer overflow interrupt; false: disable
void FTM_DRV_CounterStop ( uint32_t  instance)
Parameters
instanceThe FTM peripheral instance number.
uint32_t FTM_DRV_CounterRead ( uint32_t  instance)
Parameters
instanceThe FTM peripheral instance number.
Returns
The counter value.
void FTM_DRV_SetClock ( uint8_t  instance,
ftm_clock_source_t  clock,
ftm_clock_ps_t  clockPs 
)

This function saves the user's clock source selection in the driver to set the clock source and use features provided by the driver-like counter, PWM generation, and so on. This function also sets the clock divider.

Parameters
instanceThe FTM peripheral instance number.
clockThe clock source to use, cannot pick None.
clockPsThe clock divider value.
uint32_t FTM_DRV_GetClock ( uint8_t  instance)

This function returns zero if no clock source is selected and the FTM counter is disabled.

Parameters
instanceThe FTM peripheral instance number.
Returns
The frequency of the clock source running the FTM counter, returns 0 if counter is disabled
void FTM_DRV_SetTimeOverflowIntCmd ( uint32_t  instance,
bool  overflowEnable 
)
Parameters
instanceThe FTM peripheral instance number.
overflowEnabletrue: enable the timer overflow interrupt, false: disable
void FTM_DRV_SetFaultIntCmd ( uint32_t  instance,
bool  faultEnable 
)
Parameters
instanceThe FTM peripheral instance number.
faultEnabletrue: enable the fault interrupt, false: disable
void FTM_DRV_SetupChnInputCapture ( uint32_t  instance,
ftm_input_capture_edge_mode_t  captureMode,
uint8_t  channel,
uint8_t  filterVal 
)

When the edge specified in the captureMode argument occurs on the channel, the FTM counter is captured into the CnV register. The user has to read the CnV register separately to get this value. The filter function is disabled if the filterVal argument passed in is 0. The filter function is available only for 0, 1, 2, 3 channels.

Parameters
instanceThe FTM peripheral instance number
captureModeSpecifies which edge to capture
channelThe channel number
filterValFilter value to be used, specify 0 to disable filter. Available only for channels 0-3
void FTM_DRV_SetupChnOutputCompare ( uint32_t  instance,
ftm_output_compare_edge_mode_t  compareMode,
uint8_t  channel,
uint32_t  compareVal 
)

When the FTM counter matches the value of compareVal argument (this is written into CnV reg), the channel output is changed based on what is specified in the compareMode argument.

Parameters
instanceThe FTM peripheral instance number.
compareModeAction to take on the channel output when the compare condition is met
channelThe channel number
compareValValue to be programmed in the CnV register.
void FTM_DRV_SetupChnDualEdgeCapture ( uint32_t  instance,
ftm_dual_edge_capture_param_t param,
uint8_t  channel,
uint8_t  filterVal 
)

This function sets up the dual edge capture mode on a channel pair. The capture edge for the channel pair and the capture mode (one-shot or continuous) is specified in the parameter argument. The filter function is disabled if the filterVal argument passed is zero. The filter function is available only on channels 0 and 2. The user has to read the channel CnV registers separately to get the capture values.

Parameters
instanceThe FTM peripheral instance number.
paramControls the dual edge capture function
channelThe channel number, the code finds the channel pair associated with the channel number passed in.
filterValFilter value to be used, specify 0 to disable filter. Available only for channels 0, 2
void FTM_DRV_IRQHandler ( uint32_t  instance)

The timer overflow flag is checked and cleared if set.

Parameters
instanceInstance number of the FTM module.

Variable Documentation

FTM_Type* const g_ftmBase[FTM_INSTANCE_COUNT]
const IRQn_Type g_ftmIrqId[FTM_INSTANCE_COUNT]