RX Flexible Software Package Documentation  Release v1.0.0

 
Compare Match Timer (r_cmt)

Functions

fsp_err_t R_CMT_Open (timer_ctrl_t *const p_ctrl, timer_cfg_t const *const p_cfg)
 
fsp_err_t R_CMT_Stop (timer_ctrl_t *const p_ctrl)
 
fsp_err_t R_CMT_Start (timer_ctrl_t *const p_ctrl)
 
fsp_err_t R_CMT_Reset (timer_ctrl_t *const p_ctrl)
 
fsp_err_t R_CMT_Enable (timer_ctrl_t *const p_ctrl)
 
fsp_err_t R_CMT_Disable (timer_ctrl_t *const p_ctrl)
 
fsp_err_t R_CMT_PeriodSet (timer_ctrl_t *const p_ctrl, uint32_t const period_counts)
 
fsp_err_t R_CMT_DutyCycleSet (timer_ctrl_t *const p_ctrl, uint32_t const duty_cycle_counts, uint32_t const pin)
 
fsp_err_t R_CMT_CompareMatchSet (timer_ctrl_t *const p_ctrl, uint32_t const compare_match_value, timer_compare_match_t const match_channel)
 
fsp_err_t R_CMT_InfoGet (timer_ctrl_t *const p_ctrl, timer_info_t *const p_info)
 
fsp_err_t R_CMT_StatusGet (timer_ctrl_t *const p_ctrl, timer_status_t *const p_status)
 
fsp_err_t R_CMT_CounterSet (timer_ctrl_t *const p_ctrl, uint32_t counter)
 
fsp_err_t R_CMT_CallbackSet (timer_ctrl_t *const p_api_ctrl, void(*p_callback)(timer_callback_args_t *), void *const p_context, timer_callback_args_t *const p_callback_memory)
 
fsp_err_t R_CMT_Close (timer_ctrl_t *const p_ctrl)
 

Detailed Description

Driver for the CMT peripherals on RX MCU. This module implements the Timer Interface.

Overview

RX has two on-chip CMT units (unit 0 and unit 1), each consisting of a two-channel 16-bit timer.

The CMT has a 16-bit counter, and can generate interrupts at set intervals.

Features

The CMT module has the following features:

Selecting a Timer

When selecting CMT, consider these factors:

GPT CMTW CMT TMR
Timer Resolution All MCUs have at least one 32-bit GPT timer. The CMTW timer have at least one 32-bit CMTW timer. The CMT timers are 16-bit timers. The TMR timers are 8-bit timer or 16-bit timer.
Clock Source The GPT runs off PCLKA with a configurable divider up to 1024. It can also be configured to count ELC events or external pulses. The CMTW runs off PCLKB. The CMT runs off PCLKB with a configurable divider from 8 to 512. It can also be configured to count ELC events. The TMR runs off PCLKB.
Low Power Modes The GPT can operate in sleep mode. The CMTW can operate in sleep mode. The CMT can operate in in sleep mode. The TMR can operate in sleep mode.

Configuration

Build Time Configurations for r_cmt

The following build time configurations are defined in fsp_cfg/r_cmt_cfg.h:

ConfigurationOptionsDefaultDescription
Parameter Checking
  • Default (BSP)
  • Enabled
  • Disabled
Default (BSP) If selected code for parameter checking is included in the build.

Configurations for Timers > Timer, Compare Match Timer (r_cmt)

This module can be added to the Stacks tab via New Stack > Timers > Timer, Compare Match Timer (r_cmt).

ConfigurationOptionsDefaultDescription
General > NameName must be a valid C symbolg_timer0 Module name.
General > ChannelValue must be a non-negative integer0 Specify the hardware channel.
General > Mode
  • Periodic
  • One-Shot
Periodic Mode selection.
Periodic: Generates periodic interrupts or square waves.
One-shot: Generate a single interrupt or a pulse wave. Note: One-shot mode is implemented in software. ISRs must be enabled for one-shot even if callback is unused.
General > PeriodValue must be non-negative0x10000 Specify the timer period based on the selected unit.
General > Period Unit
  • Raw Counts
  • Nanoseconds
  • Microseconds
  • Milliseconds
  • Seconds
  • Hertz
  • Kilohertz
Raw Counts Unit of the period specified above
Interrupts > CallbackName must be a valid C symbolNULL A user callback function can be specified here. If this callback function is provided, it will be called from the interrupt service routine (ISR) each time the timer period elapses
Interrupts > Compare Match Interrupt PriorityMCU Specific OptionsSelect the compare match interrupt priority.

Clock Configuration

The CMT clock is based on the PCLKB frequency. You can set the PCLKB frequency using the Clocks tab of the RX Configuration editor or by using the CGC Interface at run-time.

Usage Notes

Period

When period unit is selected as "Raw Counts", the output waveform period is calculated based on default clock frequency PCLKB/8

Maximum Period

The RX Configuration editor will automatically calculate the period count value and source clock divider based on the selected period time and units.

When the selected period unit is "Raw counts", the maximum period setting is 0x10000 on a 16-bit timer. This will configure the timer with the maximum period and a count clock divisor of 512.

Updating Period

The period is updated after the next counter overflow after calling R_CMT_PeriodSet(). To force them to update before the next counter overflow, call R_CMT_Reset() while the counter is running.

Note
When manually changing the timer period counts the maximum value for a 16-bit CMT is 0x10000. This number overflows the 16-bit value for timer_cfg_t::period_counts. To configure the timer for the maximum period, set timer_cfg_t::period_counts to 0.

One-Shot Mode

After a compare match interrupt is generated, the counter then reset to 0000h and stop.

Periodic Mode:

Frequency dividing clock is selected from four frequency dividing clocks obtained by dividing the peripheral module clock (PCLKB), the CMCNT counter starts counting up using the selected clock. When the value in the counter and the value in the register match, a compare match interrupt (CMIn) is generated. The CMCNT counter then starts counting up again from 0000h.

Event Counting

Event counting can be done by selecting counting sources from CMCR, ELC events.

Note
In event counting mode, the application must call R_CMT_Start() to enable event counting. The counter will not change after calling R_CMT_Start() until an event occurs.

Controlling CMT with ELC Events

The cmt timer can be configured to start, count, restart when an ELC event occurs.

Note
Event signal input and output are available for CMT1 only (channel 2, channel 3).

Examples

CMT Basic Example

This is a basic example of minimal use of the cmt in an application.

void cmt_basic_example (void)
{
fsp_err_t err = FSP_SUCCESS;
/* Initializes the module. */
err = R_CMT_Open(&g_timer0_ctrl, &g_timer0_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
/* Start the timer. */
(void) R_CMT_Start(&g_timer0_ctrl);
}
void cmt_reset_example (void)
{
fsp_err_t err = FSP_SUCCESS;
/* Initializes the module. */
err = R_CMT_Open(&g_timer0_ctrl, &g_timer0_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
/* Start the timer. */
(void) R_CMT_Start(&g_timer0_ctrl);
/* Reset the counter. */
(void) R_CMT_Reset(&g_timer0_ctrl);
/* Read the current counter value. Counter value is in status.counter. */
(void) R_CMT_StatusGet(&g_timer0_ctrl, &status);
}

CMT Callback example

This is an example of a timer callback.

/* Example callback called when timer expires. */
void timer_callback (timer_callback_args_t * p_args)
{
if (TIMER_EVENT_CYCLE_END == p_args->event)
{
/* Add application code to be called periodically here. */
}
}

CMT Free Running Counter Example

To use the CMT as a free running counter, select periodic mode and set the the Period to 0xFFFF for a 16-bit timer.

void cmt_counter_example (void)
{
fsp_err_t err = FSP_SUCCESS;
/* Initializes the module. */
err = R_CMT_Open(&g_timer0_ctrl, &g_timer0_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
/* Start the timer. */
(void) R_CMT_Start(&g_timer0_ctrl);
/* (Optional) Stop the timer. */
(void) R_CMT_Stop(&g_timer0_ctrl);
/* Read the current counter value. Counter value is in status.counter. */
(void) R_CMT_StatusGet(&g_timer0_ctrl, &status);
}

CMT Period Update Example

This an example of updating the period.

#define CMT_EXAMPLE_MSEC_PER_SEC (1000)
#define CMT_EXAMPLE_DESIRED_PERIOD_MSEC (20)
/* This example shows how to calculate a new period value at runtime. */
void cmt_period_calculation_example (void)
{
fsp_err_t err = FSP_SUCCESS;
/* Initializes the module. */
err = R_CMT_Open(&g_timer0_ctrl, &g_timer0_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
/* Start the timer. */
(void) R_CMT_Start(&g_timer0_ctrl);
/* Get the source clock frequency (in Hz). There are 3 ways to do this in FSP:
* - If the PCLKB frequency has not changed since reset, the source clock frequency is
* BSP_STARTUP_PCLKB_HZ >> timer_cfg_t::source_div
* - Use the R_CMT_InfoGet function (it accounts for the divider).
* - Calculate the current PCLKB frequency using R_FSP_SystemClockHzGet(FSP_PRIV_CLOCK_PCLKB) and right shift
* by timer_cfg_t::source_div.
*
* This example uses the 3rd option (R_FSP_SystemClockHzGet).
*/
uint32_t pclk_freq_hz = R_FSP_SystemClockHzGet(FSP_PRIV_CLOCK_PCLKB) >> g_timer0_cfg.source_div;
/* Calculate the desired period based on the current clock. Note that this calculation could overflow if the
* desired period is larger than UINT32_MAX / pclk_freq_hz. A cast to uint64_t is used to prevent this. */
uint32_t period_counts =
(uint32_t) (((uint64_t) pclk_freq_hz * CMT_EXAMPLE_DESIRED_PERIOD_MSEC) / CMT_EXAMPLE_MSEC_PER_SEC);
/* Set the calculated period. */
err = R_CMT_PeriodSet(&g_timer0_ctrl, period_counts);
assert(FSP_SUCCESS == err);
}

Data Structures

struct  cmt_instance_ctrl_t
 

Data Structure Documentation

◆ cmt_instance_ctrl_t

struct cmt_instance_ctrl_t

Channel control block. DO NOT INITIALIZE. Initialization occurs when timer_api_t::open is called.

Function Documentation

◆ R_CMT_Open()

fsp_err_t R_CMT_Open ( timer_ctrl_t *const  p_ctrl,
timer_cfg_t const *const  p_cfg 
)

Initializes the timer module and applies configurations. Implements timer_api_t::open.

Return values
FSP_SUCCESSInitialization was successful.
FSP_ERR_ASSERTIONA required input pointer is NULL or the source divider is invalid.
FSP_ERR_ALREADY_OPENModule is already open.
FSP_ERR_IRQ_BSP_DISABLEDtimer_cfg_t::mode is TIMER_MODE_ONE_SHOT or timer_cfg_t::p_callback is not NULL, but ISR is not enabled. ISR must be enabled to use one-shot mode or callback.
FSP_ERR_IP_CHANNEL_NOT_PRESENTThe channel requested in the p_cfg parameter is not available on this device.

◆ R_CMT_Stop()

fsp_err_t R_CMT_Stop ( timer_ctrl_t *const  p_ctrl)

Stops timer. Implements timer_api_t::stop.

Return values
FSP_SUCCESSTimer successfully stopped.
FSP_ERR_ASSERTIONp_ctrl was NULL.
FSP_ERR_NOT_OPENThe instance is not opened.

◆ R_CMT_Start()

fsp_err_t R_CMT_Start ( timer_ctrl_t *const  p_ctrl)

Starts timer. Implements timer_api_t::start.

Return values
FSP_SUCCESSTimer successfully started.
FSP_ERR_ASSERTIONp_ctrl was NULL.
FSP_ERR_NOT_OPENThe instance is not opened.

◆ R_CMT_Reset()

fsp_err_t R_CMT_Reset ( timer_ctrl_t *const  p_ctrl)

Resets the counter value to 0. Implements timer_api_t::reset.

Return values
FSP_SUCCESSCounter value written successfully.
FSP_ERR_ASSERTIONp_ctrl was NULL.
FSP_ERR_NOT_OPENThe instance is not opened.

◆ R_CMT_Enable()

fsp_err_t R_CMT_Enable ( timer_ctrl_t *const  p_ctrl)

Enables external event triggers that start, event count, or restart the counter. Implements timer_api_t::enable.

Return values
FSP_ERR_UNSUPPORTEDAPI not supported by CMT.

◆ R_CMT_Disable()

fsp_err_t R_CMT_Disable ( timer_ctrl_t *const  p_ctrl)

Disables external event triggers that start, event count or restart the counter. Implements timer_api_t::disable.

Note
The timer could be running after R_CMT_Disable(). To ensure it is stopped, call R_CMT_Stop().
Return values
FSP_ERR_UNSUPPORTEDAPI not supported by CMT.

◆ R_CMT_PeriodSet()

fsp_err_t R_CMT_PeriodSet ( timer_ctrl_t *const  p_ctrl,
uint32_t const  period_counts 
)

Updates period. The new period is updated immediately and the counter is reset to 0. Period unit is raw. Other units will be converted to raw before setting.

Implements timer_api_t::periodSet.

Return values
FSP_SUCCESSPeriod value written successfully.
FSP_ERR_ASSERTIONp_ctrl was NULL.
FSP_ERR_NOT_OPENThe instance is not opened.

◆ R_CMT_DutyCycleSet()

fsp_err_t R_CMT_DutyCycleSet ( timer_ctrl_t *const  p_ctrl,
uint32_t const  duty_cycle_counts,
uint32_t const  pin 
)

Sets duty cycle on requested pin. Implements timer_api_t::dutyCycleSet.

Return values
FSP_ERR_UNSUPPORTEDAPI not supported by CMT.

◆ R_CMT_CompareMatchSet()

fsp_err_t R_CMT_CompareMatchSet ( timer_ctrl_t *const  p_ctrl,
uint32_t const  compare_match_value,
timer_compare_match_t const  match_channel 
)

Placeholder for unsupported compareMatch function. Implements timer_api_t::compareMatchSet.

Return values
FSP_ERR_UNSUPPORTEDCMT compare match is not supported.

◆ R_CMT_InfoGet()

fsp_err_t R_CMT_InfoGet ( timer_ctrl_t *const  p_ctrl,
timer_info_t *const  p_info 
)

Get timer information and store it in provided pointer p_info. Implements timer_api_t::infoGet.

Return values
FSP_SUCCESSPeriod, count direction, frequency, and ELC event written to caller's structure successfully.
FSP_ERR_ASSERTIONp_ctrl or p_info was NULL.
FSP_ERR_NOT_OPENThe instance is not opened.

◆ R_CMT_StatusGet()

fsp_err_t R_CMT_StatusGet ( timer_ctrl_t *const  p_ctrl,
timer_status_t *const  p_status 
)

Get current timer status and store it in provided pointer p_status. Implements timer_api_t::statusGet.

Return values
FSP_SUCCESSCurrent timer state and counter value set successfully.
FSP_ERR_ASSERTIONp_ctrl or p_status was NULL.
FSP_ERR_NOT_OPENThe instance is not opened.

◆ R_CMT_CounterSet()

fsp_err_t R_CMT_CounterSet ( timer_ctrl_t *const  p_ctrl,
uint32_t  counter 
)

Set counter value.

Note
Do not call this API while the counter is counting.
Return values
FSP_SUCCESSCounter value updated.
FSP_ERR_ASSERTIONp_ctrl or p_status was NULL.
FSP_ERR_NOT_OPENThe instance is not opened.

◆ R_CMT_CallbackSet()

fsp_err_t R_CMT_CallbackSet ( timer_ctrl_t *const  p_api_ctrl,
void(*)(timer_callback_args_t *)  p_callback,
void *const  p_context,
timer_callback_args_t *const  p_callback_memory 
)

Updates the user callback with the option to provide memory for the callback argument structure. Implements timer_api_t::callbackSet.

Return values
FSP_SUCCESSCallback updated successfully.
FSP_ERR_ASSERTIONA required pointer is NULL.
FSP_ERR_NOT_OPENThe control block has not been opened.

◆ R_CMT_Close()

fsp_err_t R_CMT_Close ( timer_ctrl_t *const  p_ctrl)

Stops counter, clears internal driver data. Implements timer_api_t::close.

Return values
FSP_SUCCESSSuccessful close.
FSP_ERR_ASSERTIONp_ctrl was NULL.
FSP_ERR_NOT_OPENThe instance is not opened.