FlexTimer Output Compare Driver (FTM_OC)

Detailed Description

FlexTimer Output Compare Peripheral Driver.

Hardware background

The FTM of the S32K1xx is based on a 16 bits counter and supports: input capture, output compare, PWM and some instances include quadrature decoder.

How to use FTM driver in your application

For all operation modes (without Quadrature Decoder mode) the user need to configure ftm_user_config_t. This structure will be used for initialization (FTM_DRV_Init). The next functions used are specific for each operation mode.

Output compare mode

For this mode the user needs to configure maximum counter value, number of channels used and output mode for each channel (toggle/clear/set on match). This information is stored in ftm_output_cmp_param_t structure type and are used in FTM_DRV_InitOutputCompare function. Next step is to set a value for comparison with the FTM_DRV_UpdateOutputCompareChannel function.

Example:

/* The state structure of instance in the output compare mode */
ftm_state_t stateOutputCompare;
#define FTM_OUTPUT_COMPARE_INSTANCE 1UL
/* Channels configuration structure for PWM output compare */
ftm_output_cmp_ch_param_t PWM_OutputCompareChannelConfig[2] =
{
{
0U, /* Channel id */
FTM_TOGGLE_ON_MATCH, /* Output mode */
10000U, /* Compared value */
false, /* External Trigger */
},
{
1U, /* Channel id */
FTM_TOGGLE_ON_MATCH, /* Output mode */
20000U, /* Compared value */
false, /* External Trigger */
}
};
/* Output compare configuration for PWM */
ftm_output_cmp_param_t PWM_OutputCompareConfig =
{
2U, /* Number of channels */
FTM_MODE_OUTPUT_COMPARE, /* FTM mode */
40000U, /* Maximum count value */
PWM_OutputCompareChannelConfig /* Channels configuration */
};
/* Timer mode configuration for PWM */
/* Global configuration of PWM */
ftm_user_config_t PWM_InitConfig =
{
{
true, /* Software trigger state */
false, /* Hardware trigger 1 state */
false, /* Hardware trigger 2 state */
false, /* Hardware trigger 3 state */
true, /* Maximum loading point state */
true, /* Min loading point state */
FTM_SYSTEM_CLOCK, /* Update mode for INVCTRL register */
FTM_SYSTEM_CLOCK, /* Update mode for SWOCTRL register */
FTM_SYSTEM_CLOCK, /* Update mode for OUTMASK register */
FTM_SYSTEM_CLOCK, /* Update mode for CNTIN register */
false, /* Auto clear trigger state for hardware trigger */
FTM_UPDATE_NOW, /* select synchronization method */
},
FTM_MODE_OUTPUT_COMPARE, /* Mode of operation for FTM */
FTM_CLOCK_DIVID_BY_4, /* FTM clock pre-scaler */
FTM_CLOCK_SOURCE_SYSTEMCLK, /* FTM clock source */
FTM_BDM_MODE_11, /* FTM debug mode */
false, /* Interrupt state */
false /* Initialization trigger */
};
FTM_DRV_Init(FTM_OUTPUT_COMPARE_INSTANCE, &PWM_InitConfig, &stateOutputCompare);
FTM_DRV_InitOutputCompare(FTM_OUTPUT_COMPARE_INSTANCE, &PWM_OutputCompareConfig);
/* If you want to change compared value */
FTM_DRV_UpdateOutputCompareChannel(FTM_OUTPUT_COMPARE_INSTANCE, 0UL, 15000U );

Data Structures

struct  ftm_output_cmp_ch_param_t
 FlexTimer driver PWM parameters. More...
 
struct  ftm_output_cmp_param_t
 FlexTimer driver PWM parameters. More...
 

Enumerations

enum  ftm_output_compare_mode_t { FTM_DISABLE_OUTPUT = 0x00U, FTM_TOGGLE_ON_MATCH = 0x01U, FTM_CLEAR_ON_MATCH = 0x02U, FTM_SET_ON_MATCH = 0x03U }
 FlexTimer Mode configuration for output compare mode. More...
 
enum  ftm_output_compare_update_t { FTM_RELATIVE_VALUE = 0x00U, FTM_ABSOLUTE_VALUE = 0x01U }
 FlexTimer input capture type of the next output compare value. More...
 

Functions

status_t FTM_DRV_InitOutputCompare (uint32_t instance, const ftm_output_cmp_param_t *param)
 Configures the FTM to generate timed pulses(Output compare mode). More...
 
status_t FTM_DRV_DeinitOutputCompare (uint32_t instance, const ftm_output_cmp_param_t *param)
 Disables compare match output control and clears FTM timer configuration. More...
 
status_t FTM_DRV_UpdateOutputCompareChannel (uint32_t instance, uint8_t channel, uint16_t nextComparematchValue, ftm_output_compare_update_t update, bool softwareTrigger)
 Sets the next compare match value based on the current counter value. More...
 

Enumeration Type Documentation

FlexTimer Mode configuration for output compare mode.

Implements : ftm_output_compare_mode_t_Class

Enumerator
FTM_DISABLE_OUTPUT 

No action on output pin

FTM_TOGGLE_ON_MATCH 

Toggle on match

FTM_CLEAR_ON_MATCH 

Clear on match

FTM_SET_ON_MATCH 

Set on match

Definition at line 44 of file ftm_oc_driver.h.

FlexTimer input capture type of the next output compare value.

Implements : ftm_output_compare_update_t_Class

Enumerator
FTM_RELATIVE_VALUE 

Next compared value is relative to current value

FTM_ABSOLUTE_VALUE 

Next compared value is absolute

Definition at line 57 of file ftm_oc_driver.h.

Function Documentation

status_t FTM_DRV_DeinitOutputCompare ( uint32_t  instance,
const ftm_output_cmp_param_t param 
)

Disables compare match output control and clears FTM timer configuration.

Parameters
[in]instanceThe FTM peripheral instance number.
[in]paramConfiguration of the output compare channel
Returns
success
  • STATUS_SUCCESS : Completed successfully.
  • STATUS_ERROR : Error occurred.

Definition at line 108 of file ftm_oc_driver.c.

status_t FTM_DRV_InitOutputCompare ( uint32_t  instance,
const ftm_output_cmp_param_t param 
)

Configures the FTM to generate timed pulses(Output compare mode).

When the FTM counter matches the value of CnV, the channel output is changed based on what is specified in the compareMode argument. The signal period can be modified using param->MaxCountValue. After this function max counter value and CnV are equal. FTM_DRV_SetNextComparematchValue function can be used to change CnV value.

Parameters
[in]instanceThe FTM peripheral instance number.
[in]paramconfiguration of the output compare channels
Returns
success
  • STATUS_SUCCESS : Completed successfully.
  • STATUS_ERROR : Error occurred.

Definition at line 45 of file ftm_oc_driver.c.

status_t FTM_DRV_UpdateOutputCompareChannel ( uint32_t  instance,
uint8_t  channel,
uint16_t  nextComparematchValue,
ftm_output_compare_update_t  update,
bool  softwareTrigger 
)

Sets the next compare match value based on the current counter value.

Parameters
[in]instanceThe FTM peripheral instance number.
[in]channelConfiguration of the output compare channel
[in]nextComparematchValueTimer value in ticks until the next compare match event should appear
[in]update
  • FTM_RELATIVE_VALUE : nextComparemantchValue will be added to current counter value
  • FTM_ABSOLUTE_VALUE : nextComparemantchValue will be written in counter register as it is
[in]softwareTriggerThis parameter will be true if software trigger sync is enabled and the user want to generate a software trigger (the value from buffer will be moved to register immediate or at next loading point depending on the sync configuration). Otherwise this parameter must be false and the next compared value will be stored in buffer until a trigger signal will be received.
Returns
success
  • STATUS_SUCCESS : Completed successfully.
  • STATUS_ERROR : Error occurred.

Definition at line 150 of file ftm_oc_driver.c.