![]() |
RX Flexible Software Package Documentation
Release v1.0.0
|
|
Driver for the CMTW peripherals on RX MCUs. This module implements the Timer Interface.
The Compare Match Timer W (CMTW) module can be used to count events, measure external input signals, generate a periodic interrupt, and output a periodic to a TOC pin. This module supports two units (unit 0 and unit 1) with one channel of 32-bit.
The CMTW module has the following features:
RX MCUs have two timer peripherals: the General PWM Timer (GPT), the Compare Match Timer (CMT) and the Compare Match Timer W (CMTW). When selecting between them, consider these factors:
| GPT | CMT | CMTW | TMR | |
|---|---|---|---|---|
| Low Power Modes | The GPT can operate in sleep mode. | The CMT can operate in sleep mode. | The CMTW can operate in sleep mode. | The TMR can operate in sleep mode. |
| Available Channels | The number of GPT channels is device specific. All currently supported MCUs have at least 4 GPT channels. | All MCUs have 4 CMT channels. | All MCUs have 2 CMTW channels. | The number of TMR channels is device specific. All currently supported MCUs have at least 4 TMR channels. |
| Timer Resolution | All MCUs have at least one 32-bit GPT timer. | The CMT timers are 16-bit timers. | The CMTWs timers are 32-bit timers. | The TMR timers are 8-bit timers or 16-bit timers. |
| Clock Source | The GPT runs off PCLKD with a configurable divider up to 1024. It can also be configured to count ELC events or external pulses. | The CMT runs off PCLKB. | The CMTW runs off PCLKB. | The TMR runs off PCLKB. |
| Configuration | Options | Default | Description |
|---|---|---|---|
| Parameter Checking |
| Default (BSP) | If selected code for parameter checking is included in the build. |
| Configuration | Options | Default | Description |
|---|---|---|---|
| General > Name | Name must be a valid C symbol | g_timer0 | Module name. |
| General > Channel | Value must be a non-negative integer | 0 | Specify the hardware channel. |
| General > Mode |
| Periodic | Mode selection. Note: One-shot mode is implemented in software. ISR's 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 | Unit of the period specified above |
| General > Counter Size |
| 32-bit | Size of the timer counter |
| General > Counter Clear Source |
| Compare Match | Select the Counter Clear Source. |
| Output > TOC0 Output |
| Disabled | Configure TOC0 output. |
| Output > TOC1 Output |
| Disabled | Configure TOC1 output. |
| Output > TOC0 Output Enable |
| Output Disable | Configure TOC0 output enable. |
| Output > TOC1 Output Enable |
| Output Disable | Configure TOC1 output enable. |
| Input > TIC0 Capture Trigger |
| Rising Edge | Select the trigger edge. |
| Input > TIC1 Capture Trigger |
| Rising Edge | Select the trigger edge. |
| Input > TIC0 Input Enable |
| Input Disable | Configure TIC0 output enable. |
| Input > TIC1 Input Enable |
| Input Disable | Configure TIC1 output enable. |
| Interrupts > Callback | Name must be a valid C symbol | NULL | A user callback function. If this callback function is provided, it is 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. | |
| Interrupts > Input Capture 0 Interrupt Priority | MCU Specific Options | Select the input capture 0 interrupt priority. | |
| Interrupts > Input Capture 1 Interrupt Priority | MCU Specific Options | Select the input capture 1 interrupt priority. | |
| Interrupts > Output Compare 0 Interrupt Priority | MCU Specific Options | Select the output compare 0 interrupt priority. | |
| Interrupts > Output Compare 1 Interrupt Priority | MCU Specific Options | Select the output compare 1 interrupt priority. |
The CMTW clock is used to 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.
This module can use CMWCOR, CMWICR0, CMWICR1, CMWOCR0, and CMWOCR1 event as counter clear source.
This module can use TIC0, TIC1, TIC2, and TIC3 as input pins to measure input signals.
This module can use TOC0, TOC1, TOC2, and TOC3 as output pins for periodic signals.
The RX Configuration editor will automatically calculate the period count value and source clock divider based on the selected period time, units and clock speed.
When the selected period unit is "Raw counts", the maximum period setting is 0x2000000 for 16-bit or 0x20000000000 for 32-bit. This will configure the timer with the maximum period and a count clock divisor of 512.
The period is updated after the next counter overflow after calling R_CMTW_PeriodSet(). To force them to update before the next counter overflow, call R_CMTW_Reset() while the counter is running.
The CMTW 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. The callback is only called once in this case, but multiple events may be generated if the timer is linked to the Transfer (r_dtc).
Examples of one-shot signals that can be generated by this module are shown below:
Frequency dividing clock is selected from four frequency dividing clocks obtained by dividing the peripheral module clock (PCLKB), the CMWCNT counter starts counting up using the selected clock. When clearing of the counter is selected by counter clear register. The CMWCNT counter becomes 0000 0000h and continues counting. Otherwise, an overflow is generated when FFFF FFFFh changes to 0000 0000h during 32-bit count operation and 0000 FFFFh changes to 0000 0000h during 16-bit operation, and the CMWCNT counter continues counting.
The TOC pin toggles twice each time the timer expires in periodic mode. This is achieved by defining a PWM wave at a 50 percent duty cycle so that the period of the resulting square (from rising edge to rising edge) matches the period of the CMTW timer.
Examples of periodic signals that can be generated by this module are shown below:
Event counting can be done by selecting counting sources from CMWCR, ELC events.
The CMTW timer can be configured to start, count, restart when an ELC event occurs.
R_CMTW_Enable() must be called when external sources are used for the input capture.
This is a basic example of minimal use of the CMTW in an application.
This is an example of a timer callback.
To use the CMTW as a free running counter, select periodic mode and set the the Period to 0xFFFF for a 16-bit timer or 0xFFFFFFFF for a 32-bit timer.
This is an example of using the CMTW to capture pulse width or pulse period measurements.
This an example of updating the period.
Data Structures | |
| struct | cmtw_instance_ctrl_t |
| struct | cmtw_extended_cfg_t |
Enumerations | |
| enum | cmtw_source_edge_t |
| enum | cmtw_output_pin_t |
| enum | cmtw_clear_source_t |
| enum | cmtw_io_pin_t |
| enum | cmtw_output_control_t |
| enum | cmtw_input_control_t |
| struct cmtw_instance_ctrl_t |
Channel control block. DO NOT INITIALIZE. Initialization occurs when timer_api_t::open is called.
| struct cmtw_extended_cfg_t |
Optional CMTW extension data structure.
| Data Fields | ||
|---|---|---|
| cmtw_output_pin_t | toc0 | Configure TOC0 pin. |
| cmtw_output_pin_t | toc1 | Configure TOC1 pin. |
| cmtw_output_control_t | toc0_control | Output Compare 0 enable setting. |
| cmtw_output_control_t | toc1_control | Output Compare 1 enable setting. |
| cmtw_source_edge_t | capture_ic0_source | Event sources that trigger capture of TIC0. |
| cmtw_source_edge_t | capture_ic1_source | Event sources that trigger capture of TIC1. |
| cmtw_input_control_t | ic0_control | Input Capture 0 enable setting. |
| cmtw_input_control_t | ic1_control | Input Capture 1 enable setting. |
| cmtw_clear_source_t | clear_source | Counter clear source. |
| timer_variant_t | counter_size | Timer counter size. |
| uint8_t | capture_ic0_ipl | Input Capture 0 interrupt priority. |
| uint8_t | capture_ic1_ipl | Input Capture 1 interrupt priority. |
| IRQn_Type | capture_ic0_irq | Input Capture 0 interrupt. |
| IRQn_Type | capture_ic1_irq | Input Capture 1 interrupt. |
| uint8_t | compare_oc0_ipl | Output Compare 0 interrupt priority. |
| uint8_t | compare_oc1_ipl | Output Compare 1 interrupt priority. |
| IRQn_Type | compare_oc0_irq | Output Compare 0 interrupt. |
| IRQn_Type | compare_oc1_irq | Output Compare 1 interrupt. |
| enum cmtw_source_edge_t |
| enum cmtw_output_pin_t |
| enum cmtw_clear_source_t |
Clear source of CMWCNT
| enum cmtw_io_pin_t |
| enum cmtw_input_control_t |
| fsp_err_t R_CMTW_Open | ( | timer_ctrl_t *const | p_ctrl, |
| timer_cfg_t const *const | p_cfg | ||
| ) |
Initializes the CMTW module instance. Implements timer_api_t::open.
The CMTW hardware does not support one-shot functionality natively. The one-shot feature is therefore implemented in the CMTW HAL layer. For a timer configured as a one-shot timer, the timer is stopped upon the first timer expiration.
The CMTW implementation of the general timer can accept an optional cmtw_extended_cfg_t extension parameter.
| FSP_SUCCESS | Initialization was successful. |
| FSP_ERR_ASSERTION | A required input pointer is NULL or the period is not in the valid range of 1 to 0xFFFF in 16bit counter or 1 to 0xFFFFFFFF in 32bit counter. |
| FSP_ERR_ALREADY_OPEN | R_CMTW_Open has already been called for this p_ctrl. |
| FSP_ERR_IRQ_BSP_DISABLED | A required interrupt has not been enabled in the vector table. |
| FSP_ERR_IP_CHANNEL_NOT_PRESENT | Requested channel number is not available on CMTW. |
| fsp_err_t R_CMTW_Stop | ( | timer_ctrl_t *const | p_ctrl | ) |
Stops the timer. Implements timer_api_t::stop.
| FSP_SUCCESS | Timer stopped. |
| FSP_ERR_ASSERTION | p_ctrl was NULL. |
| FSP_ERR_NOT_OPEN | The instance control structure is not opened. |
| fsp_err_t R_CMTW_Start | ( | timer_ctrl_t *const | p_ctrl | ) |
Starts timer. Implements timer_api_t::start.
| FSP_SUCCESS | Timer started. |
| FSP_ERR_ASSERTION | p_ctrl is null. |
| FSP_ERR_NOT_OPEN | The instance control structure is not opened. |
| fsp_err_t R_CMTW_Reset | ( | timer_ctrl_t *const | p_ctrl | ) |
Resets the counter value to 0. Implements timer_api_t::reset.
| FSP_SUCCESS | Counter reset. |
| FSP_ERR_ASSERTION | p_ctrl is NULL |
| FSP_ERR_NOT_OPEN | The instance control structure is not opened. |
| fsp_err_t R_CMTW_Enable | ( | timer_ctrl_t *const | p_ctrl | ) |
Enables external event triggers that start, stop, clear, or capture the counter. Implements timer_api_t::enable.
| FSP_SUCCESS | External events successfully enabled. |
| FSP_ERR_ASSERTION | p_ctrl was NULL. |
| FSP_ERR_NOT_OPEN | The instance is not opened. |
| fsp_err_t R_CMTW_Disable | ( | timer_ctrl_t *const | p_ctrl | ) |
Disables external event triggers that start, stop, clear, or capture the counter. Implements timer_api_t::disable.
| FSP_SUCCESS | External events successfully disabled. |
| FSP_ERR_ASSERTION | p_ctrl was NULL. |
| FSP_ERR_NOT_OPEN | The instance is not opened. |
| fsp_err_t R_CMTW_PeriodSet | ( | timer_ctrl_t *const | p_ctrl, |
| uint32_t const | period_counts | ||
| ) |
Updates period. The new period is updated immediately. Implements timer_api_t::periodSet.
| FSP_SUCCESS | Period value updated. |
| FSP_ERR_ASSERTION | A required pointer was NULL, or the period was not in the valid range of 1 to 0xFFFF in 16bit counter or 1 to 0xFFFFFFFF in 32bit counter. |
| FSP_ERR_NOT_OPEN | The instance control structure is not opened. |
| fsp_err_t R_CMTW_DutyCycleSet | ( | timer_ctrl_t *const | p_ctrl, |
| uint32_t const | duty_cycle_counts, | ||
| uint32_t const | pin | ||
| ) |
Updates duty cycle. If the timer is counting, the new duty cycle is reflected after the next counter underflow. Implements timer_api_t::dutyCycleSet.
| FSP_ERR_UNSUPPORTED | CMTW duty cycle set is not supported. |
| fsp_err_t R_CMTW_InfoGet | ( | timer_ctrl_t *const | p_ctrl, |
| timer_info_t *const | p_info | ||
| ) |
Gets timer information and store it in provided pointer p_info. Implements timer_api_t::infoGet.
| FSP_SUCCESS | Period, count direction, and frequency stored in p_info. |
| FSP_ERR_ASSERTION | A required pointer is NULL. |
| FSP_ERR_NOT_OPEN | The instance control structure is not opened. |
| fsp_err_t R_CMTW_StatusGet | ( | timer_ctrl_t *const | p_ctrl, |
| timer_status_t *const | p_status | ||
| ) |
Retrieves the current state and counter value stores them in p_status. Implements timer_api_t::statusGet.
| FSP_SUCCESS | Current status and counter value provided in p_status. |
| FSP_ERR_ASSERTION | A required pointer is NULL. |
| FSP_ERR_NOT_OPEN | The instance control structure is not opened. |
| fsp_err_t R_CMTW_OutputEnable | ( | timer_ctrl_t *const | p_ctrl, |
| cmtw_io_pin_t | pin | ||
| ) |
Enable output for TOC0 and/or TOC1.
| FSP_SUCCESS | Output is enabled. |
| FSP_ERR_ASSERTION | p_ctrl or p_status was NULL. |
| FSP_ERR_NOT_OPEN | The instance is not opened. |
| fsp_err_t R_CMTW_OutputDisable | ( | timer_ctrl_t *const | p_ctrl, |
| cmtw_io_pin_t | pin | ||
| ) |
Disable output for TOC0 and/or TOC1.
| FSP_SUCCESS | Output is disabled. |
| FSP_ERR_ASSERTION | p_ctrl or p_status was NULL. |
| FSP_ERR_NOT_OPEN | The instance is not opened. |
| fsp_err_t R_CMTW_CallbackSet | ( | timer_ctrl_t *const | p_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.
| FSP_SUCCESS | Callback updated successfully. |
| FSP_ERR_ASSERTION | A required pointer is NULL. |
| FSP_ERR_NOT_OPEN | The control block has not been opened. |
| fsp_err_t R_CMTW_Close | ( | timer_ctrl_t *const | p_ctrl | ) |
Stops counter, disables interrupts, disables output pins, and clears internal driver data. Implements timer_api_t::close.
| FSP_SUCCESS | Timer closed. |
| FSP_ERR_ASSERTION | p_ctrl is NULL. |
| FSP_ERR_NOT_OPEN | The instance control structure is not opened. |
| fsp_err_t R_CMTW_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.
| FSP_ERR_UNSUPPORTED | CMTW compare match is not supported. |