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
|
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_external_match_t {
kCTIMER_External_Match_0 = (1U << 0),
kCTIMER_External_Match_1 = (1U << 1),
kCTIMER_External_Match_2 = (1U << 2),
kCTIMER_External_Match_3 = (1U << 3)
} |
| List of external match. 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...
|
|
|
status_t | CTIMER_SetupPwmPeriod (CTIMER_Type *base, const ctimer_match_t pwmPeriodChannel, 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, const ctimer_match_t pwmPeriodChannel, 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, const ctimer_match_t pwmPeriodChannel, ctimer_match_t matchChannel, uint8_t dutyCyclePercent) |
| Updates the duty cycle of an active PWM signal. More...
|
|
struct ctimer_match_config_t |
This structure holds the configuration settings for each match register.
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.
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_External_Match_0 |
External match 0.
|
kCTIMER_External_Match_1 |
External match 1.
|
kCTIMER_External_Match_2 |
External match 2.
|
kCTIMER_External_Match_3 |
External match 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
|
- Note
- This API should be called at the beginning of the application before using the driver.
- Parameters
-
base | Ctimer peripheral base address |
config | Pointer to the user configuration structure. |
void CTIMER_Deinit |
( |
CTIMER_Type * |
base | ) |
|
- Parameters
-
base | Ctimer peripheral base address |
The default values are:
* config->mode = kCTIMER_TimerMode;
* config->prescale = 0;
*
- Parameters
-
config | Pointer to the user configuration structure. |
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 can manually assign the specified channel to set the PWM cycle.
- Note
- When setting PWM output from multiple output pins, all should use the same PWM period
- Parameters
-
base | Ctimer peripheral base address |
pwmPeriodChannel | Specify the channel to control the PWM period |
matchChannel | Match pin to be used to output the PWM signal |
pwmPeriod | PWM period match value |
pulsePeriod | Pulse width match value |
enableInt | Enable interrupt when the timer value reaches the match value of the PWM pulse, if it is 0 then no interrupt will be generated. |
status_t CTIMER_SetupPwm |
( |
CTIMER_Type * |
base, |
|
|
const ctimer_match_t |
pwmPeriodChannel, |
|
|
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 can manually assign the specified channel 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
-
base | Ctimer peripheral base address |
pwmPeriodChannel | Specify the channel to control the PWM period |
matchChannel | Match pin to be used to output the PWM signal |
dutyCyclePercent | PWM pulse width; the value should be between 0 to 100 |
pwmFreq_Hz | PWM signal frequency in Hz |
srcClock_Hz | Timer counter clock in Hz |
enableInt | Enable interrupt when the timer value reaches the match value of the PWM pulse, if it is 0 then no interrupt will be generated. |
static void CTIMER_UpdatePwmPulsePeriod |
( |
CTIMER_Type * |
base, |
|
|
ctimer_match_t |
matchChannel, |
|
|
uint32_t |
pulsePeriod |
|
) |
| |
|
inlinestatic |
- Parameters
-
base | Ctimer peripheral base address |
matchChannel | Match pin to be used to output the PWM signal |
pulsePeriod | New PWM pulse width match value |
void CTIMER_UpdatePwmDutycycle |
( |
CTIMER_Type * |
base, |
|
|
const ctimer_match_t |
pwmPeriodChannel, |
|
|
ctimer_match_t |
matchChannel, |
|
|
uint8_t |
dutyCyclePercent |
|
) |
| |
- Note
- Please use CTIMER_SetupPwmPeriod to update the PWM with high resolution. This function can manually assign the specified channel to set the PWM cycle.
- Parameters
-
base | Ctimer peripheral base address |
pwmPeriodChannel | Specify the channel to control the PWM period |
matchChannel | Match pin to be used to output the PWM signal |
dutyCyclePercent | New PWM pulse width; the value should be between 0 to 100 |
User configuration is used to setup the match value and action to be taken when a match occurs.
- Parameters
-
base | Ctimer peripheral base address |
matchChannel | Match register to configure |
config | Pointer to the match configuration structure |
uint32_t CTIMER_GetOutputMatchStatus |
( |
CTIMER_Type * |
base, |
|
|
uint32_t |
matchChannel |
|
) |
| |
This function gets the status of output MAT, whether or not this output is connected to a pin. This status is driven to the MAT pins if the match function is selected via IOCON. 0 = LOW. 1 = HIGH.
- Parameters
-
base | Ctimer peripheral base address |
matchChannel | External match channel, user can obtain the status of multiple match channels at the same time by using the logic of "|" enumeration ctimer_external_match_t |
- Returns
- The mask of external match channel status flags. Users need to use the _ctimer_external_match type to decode the return variables.
- Parameters
-
base | Ctimer peripheral base address |
capture | Capture channel to configure |
edge | Edge on the channel that will trigger a capture |
enableInt | Flag 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
-
base | Ctimer 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
-
base | Ctimer peripheral base address |
cb_func | callback function |
cb_type | callback function type, singular or multiple |
static void CTIMER_EnableInterrupts |
( |
CTIMER_Type * |
base, |
|
|
uint32_t |
mask |
|
) |
| |
|
inlinestatic |
- Parameters
-
base | Ctimer peripheral base address |
mask | The 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
-
base | Ctimer peripheral base address |
mask | The 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
-
base | Ctimer 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
-
base | Ctimer 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
-
base | Ctimer peripheral base address |
mask | The 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
-
base | Ctimer peripheral base address |
static void CTIMER_StopTimer |
( |
CTIMER_Type * |
base | ) |
|
|
inlinestatic |
- Parameters
-
base | Ctimer 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
-
base | Ctimer peripheral base address |