RZT Flexible Software Package Documentation  Release v2.2.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_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_ctrl, void(*p_callback)(timer_callback_args_t *), void const *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 RZ microprocessor. This module implements the Timer Interface.

Overview

The CMT module can be used to generate a periodic interrupt.

Features

The CMT module has the following features:

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.
Multiplex Interrupt
  • Enabled
  • Disabled
Disabled Enable multiplex interrupt for a single driver.

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 > ChannelChannel number must exist on this MCU0 Specify the hardware channel.
General > Mode
  • Periodic
  • One-Shot
Periodic Mode selection.
Periodic: Generates periodic interrupts.
One-shot: Generate a single interrupt. 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 PCLKL frequency. You can set the PCLKL frequency using the Clocks tab of the FSP Configuration editor or by using the CGC Interface at run-time.

Timer Period

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

The maximum period setting is 0x10000 on a 16-bit timer.

Note
Because the period interrupt occurs when the counter overflows, setting the period register to 0 results in an effective period of 1 count. For this reason all user-provided raw count values reflect the actual number of period counts (not the raw register values).

Channel Configuration

The CMT peripheral on RZ microprocessor consist of multiple channels and multiple units. The correspondence between the channel number configured in the FSP Configuration editor and the units and channels actually used by CMT peripheral is shown below.

Channel Number configured in the FSP Configuration editor CMT Unit CMT Channel
0 0 0
1 0 1
2 1 0
3 1 1
4 2 0
5 2 1

Usage Notes

One-Shot Mode

The CMT timer does not support one-shot mode natively. One-shot mode is achieved by stopping the timer in the interrupt service routine before the callback is called. If the interrupt is not serviced before the timer period expires again, the timer generates more than one event.

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. */
handle_error(err);
/* Start the timer. */
(void) R_CMT_Start(&g_timer0_ctrl);
}

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.

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. */
handle_error(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 (2)
/* 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. */
handle_error(err);
/* Start the timer. */
(void) R_CMT_Start(&g_timer0_ctrl);
/* Get the source clock frequency (in Hz). There are several ways to do this in FSP:
* - Use the R_CMT_InfoGet function (it accounts for the clock source and divider).
* - Calculate the current PCLKL frequency using R_FSP_SystemClockHzGet(FSP_PRIV_CLOCK_PCLKL) and right shift
* by timer_cfg_t::source_div.
*
* This example uses the last option (R_FSP_SystemClockHzGet).
*/
uint32_t timer_freq_hz = R_FSP_SystemClockHzGet(FSP_PRIV_CLOCK_PCLKL) >> 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 / pclkl_freq_hz. A cast to uint64_t is used to prevent this. */
uint32_t period_counts =
(uint32_t) (((uint64_t) timer_freq_hz * CMT_EXAMPLE_DESIRED_PERIOD_MSEC) / CMT_EXAMPLE_MSEC_PER_SEC);
/* Set the calculated period. This will return an error if parameter checking is enabled and the calculated
* period is larger than UINT16_MAX. */
err = R_CMT_PeriodSet(&g_timer0_ctrl, period_counts);
handle_error(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 and timer has started.
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, stop, clear, or capture 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, stop, clear, or capture the counter. Implements timer_api_t::disable.

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.

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_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_ctrl,
void(*)(timer_callback_args_t *)  p_callback,
void const *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.