MCUXpresso SDK API Reference Manual  Rev. 0
NXP Semiconductors
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
CTIMER: Standard counter/timers

Files

file  fsl_ctimer.h
 

Data Structures

struct  ctimer_match_config_t
 Match configuration. More...
 
struct  ctimer_config_t
 Timer configuration structure. More...
 

Enumerations

enum  ctimer_capture_channel_t {
  kCTIMER_Capture_0 = 0U,
  kCTIMER_Capture_1,
  kCTIMER_Capture_2
}
 List of Timer capture channels. More...
 
enum  ctimer_capture_edge_t {
  kCTIMER_Capture_RiseEdge = 1U,
  kCTIMER_Capture_FallEdge = 2U,
  kCTIMER_Capture_BothEdge = 3U
}
 List of capture edge options. More...
 
enum  ctimer_match_t {
  kCTIMER_Match_0 = 0U,
  kCTIMER_Match_1,
  kCTIMER_Match_2,
  kCTIMER_Match_3
}
 List of Timer match registers. More...
 
enum  ctimer_match_output_control_t {
  kCTIMER_Output_NoAction = 0U,
  kCTIMER_Output_Clear,
  kCTIMER_Output_Set,
  kCTIMER_Output_Toggle
}
 List of output control options. More...
 
enum  ctimer_timer_mode_t
 List of Timer modes.
 
enum  ctimer_interrupt_enable_t {
  kCTIMER_Match0InterruptEnable = CTIMER_MCR_MR0I_MASK,
  kCTIMER_Match1InterruptEnable = CTIMER_MCR_MR1I_MASK,
  kCTIMER_Match2InterruptEnable = CTIMER_MCR_MR2I_MASK,
  kCTIMER_Match3InterruptEnable = CTIMER_MCR_MR3I_MASK,
  kCTIMER_Capture0InterruptEnable = CTIMER_CCR_CAP0I_MASK,
  kCTIMER_Capture1InterruptEnable = CTIMER_CCR_CAP1I_MASK,
  kCTIMER_Capture2InterruptEnable = CTIMER_CCR_CAP2I_MASK
}
 List of Timer interrupts. More...
 
enum  ctimer_status_flags_t {
  kCTIMER_Match0Flag = CTIMER_IR_MR0INT_MASK,
  kCTIMER_Match1Flag = CTIMER_IR_MR1INT_MASK,
  kCTIMER_Match2Flag = CTIMER_IR_MR2INT_MASK,
  kCTIMER_Match3Flag = CTIMER_IR_MR3INT_MASK,
  kCTIMER_Capture0Flag = CTIMER_IR_CR0INT_MASK,
  kCTIMER_Capture1Flag = CTIMER_IR_CR1INT_MASK,
  kCTIMER_Capture2Flag = CTIMER_IR_CR2INT_MASK
}
 List of Timer flags. More...
 
enum  ctimer_callback_type_t {
  kCTIMER_SingleCallback,
  kCTIMER_MultipleCallback
}
 Callback type when registering for a callback. More...
 

Functions

void CTIMER_SetupMatch (CTIMER_Type *base, ctimer_match_t matchChannel, const ctimer_match_config_t *config)
 Setup the match register. More...
 
void CTIMER_SetupCapture (CTIMER_Type *base, ctimer_capture_channel_t capture, ctimer_capture_edge_t edge, bool enableInt)
 Setup the capture. More...
 
static uint32_t CTIMER_GetTimerCountValue (CTIMER_Type *base)
 Get the timer count value from TC register. More...
 
void CTIMER_RegisterCallBack (CTIMER_Type *base, ctimer_callback_t *cb_func, ctimer_callback_type_t cb_type)
 Register callback. More...
 
static void CTIMER_Reset (CTIMER_Type *base)
 Reset the counter. More...
 

Driver version

#define FSL_CTIMER_DRIVER_VERSION   (MAKE_VERSION(2, 0, 3))
 Version 2.0.3.
 

Initialization and deinitialization

void CTIMER_Init (CTIMER_Type *base, const ctimer_config_t *config)
 Ungates the clock and configures the peripheral for basic operation. More...
 
void CTIMER_Deinit (CTIMER_Type *base)
 Gates the timer clock. More...
 
void CTIMER_GetDefaultConfig (ctimer_config_t *config)
 Fills in the timers configuration structure with the default settings. More...
 

PWM setup operations

status_t CTIMER_SetupPwmPeriod (CTIMER_Type *base, ctimer_match_t matchChannel, uint32_t pwmPeriod, uint32_t pulsePeriod, bool enableInt)
 Configures the PWM signal parameters. More...
 
status_t CTIMER_SetupPwm (CTIMER_Type *base, ctimer_match_t matchChannel, uint8_t dutyCyclePercent, uint32_t pwmFreq_Hz, uint32_t srcClock_Hz, bool enableInt)
 Configures the PWM signal parameters. More...
 
static void CTIMER_UpdatePwmPulsePeriod (CTIMER_Type *base, ctimer_match_t matchChannel, uint32_t pulsePeriod)
 Updates the pulse period of an active PWM signal. More...
 
void CTIMER_UpdatePwmDutycycle (CTIMER_Type *base, ctimer_match_t matchChannel, uint8_t dutyCyclePercent)
 Updates the duty cycle of an active PWM signal. More...
 

Interrupt Interface

static void CTIMER_EnableInterrupts (CTIMER_Type *base, uint32_t mask)
 Enables the selected Timer interrupts. More...
 
static void CTIMER_DisableInterrupts (CTIMER_Type *base, uint32_t mask)
 Disables the selected Timer interrupts. More...
 
static uint32_t CTIMER_GetEnabledInterrupts (CTIMER_Type *base)
 Gets the enabled Timer interrupts. More...
 

Status Interface

static uint32_t CTIMER_GetStatusFlags (CTIMER_Type *base)
 Gets the Timer status flags. More...
 
static void CTIMER_ClearStatusFlags (CTIMER_Type *base, uint32_t mask)
 Clears the Timer status flags. More...
 

Counter Start and Stop

static void CTIMER_StartTimer (CTIMER_Type *base)
 Starts the Timer counter. More...
 
static void CTIMER_StopTimer (CTIMER_Type *base)
 Stops the Timer counter. More...
 

Detailed Description

The MCUXpresso SDK provides a driver for the cTimer module of MCUXpresso SDK devices.

Function groups

The cTimer driver supports the generation of PWM signals, input capture, and setting up the timer match conditions.

Initialization and deinitialization

The function CTIMER_Init() initializes the cTimer with specified configurations. The function CTIMER_GetDefaultConfig() gets the default configurations. The initialization function configures the counter/timer mode and input selection when running in counter mode.

The function CTIMER_Deinit() stops the timer and turns off the module clock.

PWM Operations

The function CTIMER_SetupPwm() sets up channels for PWM output. Each channel has its own duty cycle, however the same PWM period is applied to all channels requesting the PWM output. The signal duty cycle is provided as a percentage of the PWM period. Its value should be between 0 and 100 0=inactive signal(0% duty cycle) and 100=always active signal (100% duty cycle).

The function CTIMER_UpdatePwmDutycycle() updates the PWM signal duty cycle of a particular channel.

Match Operation

The function CTIMER_SetupMatch() sets up channels for match operation. Each channel is configured with a match value: if the counter should stop on match, if counter should reset on match, and output pin action. The output signal can be cleared, set, or toggled on match.

Input capture operations

The function CTIMER_SetupCapture() sets up an channel for input capture. The user can specify the capture edge and if a interrupt should be generated when processing the input signal.

Typical use case

Match example

Set up a match channel to toggle output when a match occurs. Refer to the driver examples codes located at <SDK_ROOT>/boards/<BOARD>/driver_examples/ctimer

PWM output example

Set up a channel for PWM output. Refer to the driver examples codes located at <SDK_ROOT>/boards/<BOARD>/driver_examples/ctimer


Data Structure Documentation

struct ctimer_match_config_t

This structure holds the configuration settings for each match register.

Data Fields

uint32_t matchValue
 This is stored in the match register.
 
bool enableCounterReset
 true: Match will reset the counter false: Match will not reser the counter
 
bool enableCounterStop
 true: Match will stop the counter false: Match will not stop the counter
 
ctimer_match_output_control_t outControl
 Action to be taken on a match on the EM bit/output.
 
bool outPinInitState
 Initial value of the EM bit/output.
 
bool enableInterrupt
 true: Generate interrupt upon match false: Do not generate interrupt on match
 
struct ctimer_config_t

This structure holds the configuration settings for the Timer peripheral. To initialize this structure to reasonable defaults, call the CTIMER_GetDefaultConfig() function and pass a pointer to the configuration structure instance.

The configuration structure can be made constant so as to reside in flash.

Data Fields

ctimer_timer_mode_t mode
 Timer mode.
 
ctimer_capture_channel_t input
 Input channel to increment the timer, used only in timer modes that rely on this input signal to increment TC.
 
uint32_t prescale
 Prescale value.
 

Enumeration Type Documentation

Enumerator
kCTIMER_Capture_0 

Timer capture channel 0.

kCTIMER_Capture_1 

Timer capture channel 1.

kCTIMER_Capture_2 

Timer capture channel 2.

Enumerator
kCTIMER_Capture_RiseEdge 

Capture on rising edge.

kCTIMER_Capture_FallEdge 

Capture on falling edge.

kCTIMER_Capture_BothEdge 

Capture on rising and falling edge.

Enumerator
kCTIMER_Match_0 

Timer match register 0.

kCTIMER_Match_1 

Timer match register 1.

kCTIMER_Match_2 

Timer match register 2.

kCTIMER_Match_3 

Timer match register 3.

Enumerator
kCTIMER_Output_NoAction 

No action is taken.

kCTIMER_Output_Clear 

Clear the EM bit/output to 0.

kCTIMER_Output_Set 

Set the EM bit/output to 1.

kCTIMER_Output_Toggle 

Toggle the EM bit/output.

Enumerator
kCTIMER_Match0InterruptEnable 

Match 0 interrupt.

kCTIMER_Match1InterruptEnable 

Match 1 interrupt.

kCTIMER_Match2InterruptEnable 

Match 2 interrupt.

kCTIMER_Match3InterruptEnable 

Match 3 interrupt.

kCTIMER_Capture0InterruptEnable 

Capture 0 interrupt.

kCTIMER_Capture1InterruptEnable 

Capture 1 interrupt.

kCTIMER_Capture2InterruptEnable 

Capture 2 interrupt.

Enumerator
kCTIMER_Match0Flag 

Match 0 interrupt flag.

kCTIMER_Match1Flag 

Match 1 interrupt flag.

kCTIMER_Match2Flag 

Match 2 interrupt flag.

kCTIMER_Match3Flag 

Match 3 interrupt flag.

kCTIMER_Capture0Flag 

Capture 0 interrupt flag.

kCTIMER_Capture1Flag 

Capture 1 interrupt flag.

kCTIMER_Capture2Flag 

Capture 2 interrupt flag.

When registering a callback an array of function pointers is passed the size could be 1 or 8, the callback type will tell that.

Enumerator
kCTIMER_SingleCallback 

Single Callback type where there is only one callback for the timer.

based on the status flags different channels needs to be handled differently

kCTIMER_MultipleCallback 

Multiple Callback type where there can be 8 valid callbacks, one per channel.

for both match/capture

Function Documentation

void CTIMER_Init ( CTIMER_Type *  base,
const ctimer_config_t config 
)
Note
This API should be called at the beginning of the application before using the driver.
Parameters
baseCtimer peripheral base address
configPointer to the user configuration structure.
void CTIMER_Deinit ( CTIMER_Type *  base)
Parameters
baseCtimer peripheral base address
void CTIMER_GetDefaultConfig ( ctimer_config_t config)

The default values are:

* config->mode = kCTIMER_TimerMode;
* config->input = kCTIMER_Capture_0;
* config->prescale = 0;
*
Parameters
configPointer to the user configuration structure.
status_t CTIMER_SetupPwmPeriod ( CTIMER_Type *  base,
ctimer_match_t  matchChannel,
uint32_t  pwmPeriod,
uint32_t  pulsePeriod,
bool  enableInt 
)

Enables PWM mode on the match channel passed in and will then setup the match value and other match parameters to generate a PWM signal. This function will assign match channel 3 to set the PWM cycle.

Note
When setting PWM output from multiple output pins, all should use the same PWM period
Parameters
baseCtimer peripheral base address
matchChannelMatch pin to be used to output the PWM signal
pwmPeriodPWM period match value
pulsePeriodPulse width match value
enableIntEnable interrupt when the timer value reaches the match value of the PWM pulse, if it is 0 then no interrupt is generated
Returns
kStatus_Success on success kStatus_Fail If matchChannel passed in is 3; this channel is reserved to set the PWM period
status_t CTIMER_SetupPwm ( CTIMER_Type *  base,
ctimer_match_t  matchChannel,
uint8_t  dutyCyclePercent,
uint32_t  pwmFreq_Hz,
uint32_t  srcClock_Hz,
bool  enableInt 
)

Enables PWM mode on the match channel passed in and will then setup the match value and other match parameters to generate a PWM signal. This function will assign match channel 3 to set the PWM cycle.

Note
When setting PWM output from multiple output pins, all should use the same PWM frequency. Please use CTIMER_SetupPwmPeriod to set up the PWM with high resolution.
Parameters
baseCtimer peripheral base address
matchChannelMatch pin to be used to output the PWM signal
dutyCyclePercentPWM pulse width; the value should be between 0 to 100
pwmFreq_HzPWM signal frequency in Hz
srcClock_HzTimer counter clock in Hz
enableIntEnable interrupt when the timer value reaches the match value of the PWM pulse, if it is 0 then no interrupt is generated
Returns
kStatus_Success on success kStatus_Fail If matchChannel passed in is 3; this channel is reserved to set the PWM cycle
static void CTIMER_UpdatePwmPulsePeriod ( CTIMER_Type *  base,
ctimer_match_t  matchChannel,
uint32_t  pulsePeriod 
)
inlinestatic
Parameters
baseCtimer peripheral base address
matchChannelMatch pin to be used to output the PWM signal
pulsePeriodNew PWM pulse width match value
void CTIMER_UpdatePwmDutycycle ( CTIMER_Type *  base,
ctimer_match_t  matchChannel,
uint8_t  dutyCyclePercent 
)
Note
Please use CTIMER_UpdatePwmPulsePeriod to update the PWM with high resolution.
Parameters
baseCtimer peripheral base address
matchChannelMatch pin to be used to output the PWM signal
dutyCyclePercentNew PWM pulse width; the value should be between 0 to 100
void CTIMER_SetupMatch ( CTIMER_Type *  base,
ctimer_match_t  matchChannel,
const ctimer_match_config_t config 
)

User configuration is used to setup the match value and action to be taken when a match occurs.

Parameters
baseCtimer peripheral base address
matchChannelMatch register to configure
configPointer to the match configuration structure
void CTIMER_SetupCapture ( CTIMER_Type *  base,
ctimer_capture_channel_t  capture,
ctimer_capture_edge_t  edge,
bool  enableInt 
)
Parameters
baseCtimer peripheral base address
captureCapture channel to configure
edgeEdge on the channel that will trigger a capture
enableIntFlag to enable channel interrupts, if enabled then the registered call back is called upon capture
static uint32_t CTIMER_GetTimerCountValue ( CTIMER_Type *  base)
inlinestatic
Parameters
baseCtimer peripheral base address.
Returns
return the timer count value.
void CTIMER_RegisterCallBack ( CTIMER_Type *  base,
ctimer_callback_t *  cb_func,
ctimer_callback_type_t  cb_type 
)
Parameters
baseCtimer peripheral base address
cb_funccallback function
cb_typecallback function type, singular or multiple
static void CTIMER_EnableInterrupts ( CTIMER_Type *  base,
uint32_t  mask 
)
inlinestatic
Parameters
baseCtimer peripheral base address
maskThe interrupts to enable. This is a logical OR of members of the enumeration ctimer_interrupt_enable_t
static void CTIMER_DisableInterrupts ( CTIMER_Type *  base,
uint32_t  mask 
)
inlinestatic
Parameters
baseCtimer peripheral base address
maskThe interrupts to enable. This is a logical OR of members of the enumeration ctimer_interrupt_enable_t
static uint32_t CTIMER_GetEnabledInterrupts ( CTIMER_Type *  base)
inlinestatic
Parameters
baseCtimer peripheral base address
Returns
The enabled interrupts. This is the logical OR of members of the enumeration ctimer_interrupt_enable_t
static uint32_t CTIMER_GetStatusFlags ( CTIMER_Type *  base)
inlinestatic
Parameters
baseCtimer peripheral base address
Returns
The status flags. This is the logical OR of members of the enumeration ctimer_status_flags_t
static void CTIMER_ClearStatusFlags ( CTIMER_Type *  base,
uint32_t  mask 
)
inlinestatic
Parameters
baseCtimer peripheral base address
maskThe status flags to clear. This is a logical OR of members of the enumeration ctimer_status_flags_t
static void CTIMER_StartTimer ( CTIMER_Type *  base)
inlinestatic
Parameters
baseCtimer peripheral base address
static void CTIMER_StopTimer ( CTIMER_Type *  base)
inlinestatic
Parameters
baseCtimer peripheral base address
static void CTIMER_Reset ( CTIMER_Type *  base)
inlinestatic

The timer counter and prescale counter are reset on the next positive edge of the APB clock.

Parameters
baseCtimer peripheral base address