|
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) |
|
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:
- Supports periodic mode and one-shot mode.
- Supports count source of PCLKL.
- Configurable period (counts per timer cycle).
- Supports runtime reconfiguration of period.
- APIs are provided to start, stop, and reset the counter.
- APIs are provided to get the current period, source clock frequency, and count direction.
- APIs are provided to get the current timer status and counter value.
- Supports start, stop, clear, count up.
Configuration
Build Time Configurations for r_cmt
The following build time configurations are defined in fsp_cfg/r_cmt_cfg.h:
Configuration | Options | Default | Description |
Parameter Checking |
-
Default (BSP)
-
Enabled
-
Disabled
| Default (BSP) | If selected code for parameter checking is included in the build. |
Multiplex Interrupt |
| 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).
Configuration | Options | Default | Description |
General > Name | Name must be a valid C symbol | g_timer0 | Module name. |
General > Channel | Channel number must exist on this MCU | 0 | Specify the hardware channel. |
General > Mode |
| 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 > Period | Value must be non-negative | 0x10000 | 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 > Callback | Name must be a valid C symbol | NULL | 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 Priority | MCU Specific Options | | Select 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)
{
handle_error(err);
}
CMT Callback Example
This is an example of a timer callback.
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)
{
handle_error(err);
}
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)
void cmt_period_calculation_example (void)
{
handle_error(err);
uint32_t period_counts =
(uint32_t) (((uint64_t) timer_freq_hz * CMT_EXAMPLE_DESIRED_PERIOD_MSEC) / CMT_EXAMPLE_MSEC_PER_SEC);
handle_error(err);
}
◆ cmt_instance_ctrl_t
struct cmt_instance_ctrl_t |
Channel control block. DO NOT INITIALIZE. Initialization occurs when timer_api_t::open is called.
◆ R_CMT_Open()
Initializes the timer module and applies configurations. Implements timer_api_t::open.
- Return values
-
FSP_SUCCESS | Initialization was successful and timer has started. |
FSP_ERR_ASSERTION | A required input pointer is NULL or the source divider is invalid. |
FSP_ERR_ALREADY_OPEN | Module is already open. |
FSP_ERR_IRQ_BSP_DISABLED | timer_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_PRESENT | The channel requested in the p_cfg parameter is not available on this device. |
◆ R_CMT_Stop()
Stops timer. Implements timer_api_t::stop.
- Return values
-
FSP_SUCCESS | Timer successfully stopped. |
FSP_ERR_ASSERTION | p_ctrl was NULL. |
FSP_ERR_NOT_OPEN | The instance is not opened. |
◆ R_CMT_Start()
Starts timer. Implements timer_api_t::start.
- Return values
-
FSP_SUCCESS | Timer successfully started. |
FSP_ERR_ASSERTION | p_ctrl was NULL. |
FSP_ERR_NOT_OPEN | The instance is not opened. |
◆ R_CMT_Reset()
Resets the counter value to 0. Implements timer_api_t::reset.
- Return values
-
FSP_SUCCESS | Counter value written successfully. |
FSP_ERR_ASSERTION | p_ctrl was NULL. |
FSP_ERR_NOT_OPEN | The instance is not opened. |
◆ R_CMT_Enable()
Enables external event triggers that start, stop, clear, or capture the counter. Implements timer_api_t::enable.
- Return values
-
FSP_ERR_UNSUPPORTED | API not supported by CMT. |
◆ R_CMT_Disable()
Disables external event triggers that start, stop, clear, or capture the counter. Implements timer_api_t::disable.
- Return values
-
FSP_ERR_UNSUPPORTED | API not supported by CMT. |
◆ R_CMT_PeriodSet()
Updates period. The new period is updated immediately and the counter is reset to 0.
Implements timer_api_t::periodSet.
- Return values
-
FSP_SUCCESS | Period value written successfully. |
FSP_ERR_ASSERTION | p_ctrl was NULL. |
FSP_ERR_NOT_OPEN | The 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_UNSUPPORTED | API not supported by CMT. |
◆ R_CMT_InfoGet()
Get timer information and store it in provided pointer p_info. Implements timer_api_t::infoGet.
- Return values
-
FSP_SUCCESS | Period, count direction, frequency, and ELC event written to caller's structure successfully. |
FSP_ERR_ASSERTION | p_ctrl or p_info was NULL. |
FSP_ERR_NOT_OPEN | The instance is not opened. |
◆ R_CMT_StatusGet()
Get current timer status and store it in provided pointer p_status. Implements timer_api_t::statusGet.
- Return values
-
FSP_SUCCESS | Current timer state and counter value set successfully. |
FSP_ERR_ASSERTION | p_ctrl or p_status was NULL. |
FSP_ERR_NOT_OPEN | The instance is not opened. |
◆ R_CMT_CounterSet()
Set counter value.
- Note
- Do not call this API while the counter is counting.
- Return values
-
FSP_SUCCESS | Counter value updated. |
FSP_ERR_ASSERTION | p_ctrl or p_status was NULL. |
FSP_ERR_NOT_OPEN | The instance is not opened. |
◆ R_CMT_CallbackSet()
Updates the user callback with the option to provide memory for the callback argument structure. Implements timer_api_t::callbackSet.
- Return values
-
FSP_SUCCESS | Callback updated successfully. |
FSP_ERR_ASSERTION | A required pointer is NULL. |
FSP_ERR_NOT_OPEN | The control block has not been opened. |
◆ R_CMT_Close()
Stops counter, clears internal driver data. Implements timer_api_t::close.
- Return values
-
FSP_SUCCESS | Successful close. |
FSP_ERR_ASSERTION | p_ctrl was NULL. |
FSP_ERR_NOT_OPEN | The instance is not opened. |