RA Flexible Software Package Documentation
Release v5.7.0
|
|
Driver for the AGT and AGTW peripheral on RA MCUs. This module implements the Timer Interface.
The AGT module has the following features:
RA MCUs have two timer peripherals: the General PWM Timer (GPT) and the Asynchronous General Purpose Timer (AGT). When selecting between them, consider these factors:
GPT | AGT | |
---|---|---|
Low Power Modes | The GPT can operate in sleep mode. | The AGT can operate in all low power modes (when count source is LOCO or subclock). |
Available Channels | The number of GPT channels is device specific. All currently supported MCUs have at least 7 GPT channels. | All MCUs have at least 2 AGT channels. |
Timer Resolution | All MCUs have at least one 32-bit GPT timer. | The AGT timers can be 16-bit or 32-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 AGT runs off PCLKB, LOCO, or subclock with a configurable divider up to 8 for PCLKB or up to 128 for LOCO or subclock. |
Configuration | Options | Default | Description |
---|---|---|---|
Parameter Checking |
| Default (BSP) | If selected code for parameter checking is included in the build. |
Pin Output Support |
| Disabled | If selected code for outputting a waveform to a pin is included in the build. |
Pin Input Support |
| Disabled | Enable input support to use pulse width measurement mode, pulse period measurement mode, or input from P402, P403, P404, or AGTIO. |
Configuration | Options | Default | Description |
---|---|---|---|
General | |||
Name | Name must be a valid C symbol | g_timer0 | Module name. |
Counter Bit Width | MCU Specific Options | Counter register bit width (16-bit or 32-bit)) | |
Channel | Channel number must be a non-negative integer | 0 | Physical hardware channel. |
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. |
Period | Value must be non-negative | 0x10000 | Specify the timer period based on the selected unit. When the unit is set to 'Raw Counts', setting the period to 0x100000000 (32-bit) or 0x10000 (16-bit) results in the maximum period at the lowest divisor (fastest timer tick). Set the period to 0x100000000 (32-bit) or 0x10000 (16-bit) for a free running timer, pulse width measurement or pulse period measurement. Setting the period higher will automatically select a higher divider; the period can be set up to 0x800000000 (32-bit) or 0x80000 (16-bit) when counting from PCLKB or 0x8000000000 (32-bit) or 0x800000 (16-bit) when counting from LOCO/subclock, which will use a divider of 8 or 128 respectively with the maximum period. If the requested period cannot be achieved, the settings with the largest possible period that is less than or equal to the requested period are used. The theoretical calculated period is printed in a comment in the timer_cfg_t structure. |
Period Unit |
| Raw Counts | Unit of the period specified above |
Count Source | MCU Specific Options | AGT counter clock source. NOTE: The divisor is calculated automatically based on the selected period. See agt_clock_t documentation for details. | |
Output | |||
Duty Cycle Percent (only applicable in PWM mode) | Value must be between 0 and 100 | 50 | Specify the timer duty cycle percent. Only used in PWM mode. |
AGTOA Output |
| Disabled | Configure AGTOA output. |
AGTOB Output |
| Disabled | Configure AGTOB output. |
AGTO Output |
| Disabled | Configure AGTO output. |
Input | |||
Measurement Mode |
| Measure Disabled | Select if the AGT should be used to measure pulse width or pulse period. In high level pulse width measurement mode, the AGT counts when AGTIO is high and starts counting immediately in the middle of a pulse if AGTIO is high when R_AGT_Start() is called. In low level pulse width measurement mode, the AGT counts when AGTIO is low and could start counting in the middle of a pulse if AGTIO is low when R_AGT_Start() is called. |
Input Filter |
| No Filter | Input filter, applies AGTIO in pulse period measurement, pulse width measurement, or event counter mode. The filter requires the signal to be at the same level for 3 successive reads at the specified filter frequency. |
Enable Pin |
| Enable Pin Not Used | Select active edge for the AGTEE pin if used. Only applies if the count source is P402, P403 or AGTIO. |
Trigger Edge |
| Trigger Edge Rising | Select the trigger edge. Applies if measurement mode is pulse period, or if the count source is P402, P403, or AGTIO. Do not select Trigger Edge Both with pulse period measurement. |
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. |
Underflow Interrupt Priority | MCU Specific Options | Timer interrupt priority. |
When using the AGT module on devices with both AGT and AGTW peripherals, the channel numbers used with the AGT driver are combined between both peripherals; AGTW channels are listed first followed by AGT. For example, on the RA2A2 there are 2 AGTW channels and 8 AGT channels. These correspond to channels 0-1 for AGTW0-AGTW1 and 2-9 for AGT0-AGT7.
The AGT clock is based on the PCLKB, LOCO, or Subclock frequency. You can set the clock frequency using the Clocks tab of the RA Configuration editor or by using the CGC Interface at run-time.
This module can use the AGTOA and AGTOB pins as output pins for periodic, one-shot, or PWM signals.
For input capture, the input signal must be applied to the AGTIOn pin.
For event counting, the AGTEEn enable pin is optional.
The RA 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 unit is "Raw counts", the maximum allowed period setting varies depending on the selected clock source:
Clock source | 16-bit Timer Maximum period (counts) | 32-bit Timer Maximum period (counts) |
---|---|---|
LOCO/Subclock | 0x800000 | 0x8000000000 |
PCLKB | 0x80000 | 0x800000000 |
All other sources | 0x10000 | 0x100000000 |
After starting or stopping the timer, AGT registers cannot be accessed until the AGT state is updated after 3 AGTCLK cycles. If another AGT function is called before the 3 AGTCLK period elapses, the function spins waiting for the AGT state to update. The required wait time after starting or stopping the timer can be determined using the frequency of AGTCLK, which is derived from timer_cfg_t::source_div and agt_extended_cfg_t::count_source.
The application is responsible for ensuring required clocks are started and stable before accessing MCU peripheral registers.
The AGT1 (channel 1 only) can be used to enter snooze mode or to wake the MCU from snooze, software standby, or deep software standby modes when a counter underflow occurs. The compare match A and B events can also be used to wake from software standby or snooze modes.
The AGT 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).
The output waveform in one-shot mode is one AGT clock cycle less than the configured period. The configured period must be at least 2 counts to generate an output pulse.
Examples of one-shot signals that can be generated by this module are shown below:
The AGTOA or AGTOB 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 AGT timer. Since the periodic output is actually a PWM output, the time at the stop level is one cycle shorter than the time opposite the stop level for odd period values.
Examples of periodic signals that can be generated by this module are shown below:
This module does not support in phase PWM output. The PWM output signal is low at the beginning of the cycle and high at the end of the cycle.
Examples of PWM signals that can be generated by this module are shown below:
The AGT timer can trigger the start of other peripherals. The Event Link Controller (r_elc) guide provides a list of all available peripherals.
This is a basic example of minimal use of the AGT in an application.
This is an example of a timer callback.
To use the AGT as a free running counter, select periodic mode and set the the Period to 0xFFFFFFFF for a 32-bit timer or 0xFFFF for a 16-bit timer.
This is an example of using the AGT to capture pulse width or pulse period measurements.
This an example of updating the period.
This an example of updating the duty cycle.
This an example of using underflow from an even AGT channel as the count source for the next channel (in this case, AGT0 and AGT1).
Data Structures | |
struct | agt_instance_ctrl_t |
struct | agt_extended_cfg_t |
Enumerations | |
enum | agt_clock_t |
enum | agt_measure_t |
enum | agt_agtio_filter_t |
enum | agt_enable_pin_t |
enum | agt_trigger_edge_t |
enum | agt_output_pin_t |
enum | agt_pin_cfg_t |
enum | agt_counter_bit_width_t |
struct agt_instance_ctrl_t |
Channel control block. DO NOT INITIALIZE. Initialization occurs when timer_api_t::open is called.
struct agt_extended_cfg_t |
Optional AGT extension data structure.
Data Fields | ||
---|---|---|
agt_clock_t | count_source | AGT channel clock source. Valid values are: AGT_CLOCK_PCLKB, AGT_CLOCK_LOCO, AGT_CLOCK_FSUB. |
union agt_extended_cfg_t | __unnamed__ | |
agt_pin_cfg_t | agto: 3 |
Configure AGTO pin.
|
agt_measure_t | measurement_mode | Measurement mode. |
agt_agtio_filter_t | agtio_filter | Input filter for AGTIO. |
agt_enable_pin_t | enable_pin | Enable pin (event counting only) |
agt_trigger_edge_t | trigger_edge | Trigger edge to start pulse period measurement or count external event. |
agt_counter_bit_width_t | counter_bit_width | Counter bit width. |
enum agt_clock_t |
Count source
enum agt_measure_t |
Enable pin for event counting mode.
enum agt_agtio_filter_t |
Input filter, applies AGTIO in pulse period measurement, pulse width measurement, or event counter mode. The filter requires the signal to be at the same level for 3 successive reads at the specified filter frequency.
Enumerator | |
---|---|
AGT_AGTIO_FILTER_NONE | No filter. |
AGT_AGTIO_FILTER_PCLKB | Filter at PCLKB. |
AGT_AGTIO_FILTER_PCLKB_DIV_8 | Filter at PCLKB / 8. |
AGT_AGTIO_FILTER_PCLKB_DIV_32 | Filter at PCLKB / 32. |
enum agt_enable_pin_t |
enum agt_trigger_edge_t |
Trigger edge for pulse period measurement mode and event counting mode.
enum agt_output_pin_t |
Output pins, used to select which duty cycle to update in R_AGT_DutyCycleSet().
Enumerator | |
---|---|
AGT_OUTPUT_PIN_AGTOA | AGTOA. |
AGT_OUTPUT_PIN_AGTOB | AGTOB. |
enum agt_pin_cfg_t |
fsp_err_t R_AGT_Open | ( | timer_ctrl_t *const | p_ctrl, |
timer_cfg_t const *const | p_cfg | ||
) |
Initializes the AGT module instance. Implements timer_api_t::open.
The AGT hardware does not support one-shot functionality natively. The one-shot feature is therefore implemented in the AGT HAL layer. For a timer configured as a one-shot timer, the timer is stopped upon the first timer expiration.
The AGT implementation of the general timer can accept an optional agt_extended_cfg_t extension parameter. For AGT, the extension specifies the clock to be used as timer source and the output pin configurations. If the extension parameter is not specified (NULL), the default clock PCLKB is used and the output pins are disabled.
Example:
FSP_SUCCESS | Initialization was successful and timer has started. |
FSP_ERR_ASSERTION | A required input pointer is NULL or the period is not in the valid range of 1 to 0xFFFF. |
FSP_ERR_ALREADY_OPEN | R_AGT_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 AGT. |
fsp_err_t R_AGT_Start | ( | timer_ctrl_t *const | p_ctrl | ) |
Starts timer. Implements timer_api_t::start.
Example:
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_AGT_Stop | ( | timer_ctrl_t *const | p_ctrl | ) |
Stops the timer. Implements timer_api_t::stop.
Example:
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_AGT_Reset | ( | timer_ctrl_t *const | p_ctrl | ) |
Resets the counter value to the period minus one. 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_AGT_Enable | ( | timer_ctrl_t *const | p_ctrl | ) |
Enables external event triggers that start, stop, clear, or capture the counter. Implements timer_api_t::enable.
Example:
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_AGT_Disable | ( | timer_ctrl_t *const | p_ctrl | ) |
Disables external event triggers that start, stop, clear, or capture the counter. Implements timer_api_t::disable.
Example:
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_AGT_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 the maximum value. Implements timer_api_t::periodSet.
Example:
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. |
FSP_ERR_NOT_OPEN | The instance control structure is not opened. |
fsp_err_t R_AGT_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.
Example:
FSP_SUCCESS | Duty cycle updated. |
FSP_ERR_ASSERTION | A required pointer was NULL, or the pin was not AGT_AGTO_AGTOA or AGT_AGTO_AGTOB. |
FSP_ERR_INVALID_ARGUMENT | Duty cycle was not in the valid range of 0 to period (counts) - 1 |
FSP_ERR_NOT_OPEN | The instance control structure is not opened. |
FSP_ERR_UNSUPPORTED | AGT_CFG_OUTPUT_SUPPORT_ENABLE is 0. |
fsp_err_t R_AGT_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 | AGT compare match is not supported. |
fsp_err_t R_AGT_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.
Example:
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_AGT_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.
Example:
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_AGT_CallbackSet | ( | timer_ctrl_t *const | p_api_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.
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_NO_CALLBACK_MEMORY | p_callback is non-secure and p_callback_memory is either secure or NULL. |
fsp_err_t R_AGT_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. |