![]() |
Synergy Software Package User's Manual
|
The AGT HAL module implements a high-level API for timing applications and uses the AGT peripheral on a Synergy MCU. A user-defined callback can be created to respond to a timer event.
The following hardware features are, or are not, supported by the SSP for the AGT.
Legend:
| Symbol | Meaning |
|---|---|
| ✓ | Available (Tested) |
| ⌧ | Not Available (Not tested/not functional or both) |
| N/A | Not supported by MCU |
| MCU Group | Timer Mode | Pulse Output Mode | Event Counter Mode | Pulse width measurement mode |
|---|---|---|---|---|
| S124 | ✓ | ✓ | ⌧ | ⌧ |
| S128 | ✓ | ✓ | ⌧ | ⌧ |
| S1JA | ✓ | ✓ | ⌧ | ⌧ |
| S3A1 | ✓ | ✓ | ⌧ | ⌧ |
| S3A3 | ✓ | ✓ | ⌧ | ⌧ |
| S3A6 | ✓ | ✓ | ⌧ | ⌧ |
| S3A7 | ✓ | ✓ | ⌧ | ⌧ |
| S5D3 | ✓ | ✓ | ⌧ | ⌧ |
| S5D5 | ✓ | ✓ | ⌧ | ⌧ |
| S5D9 | ✓ | ✓ | ⌧ | ⌧ |
| S7G2 | ✓ | ✓ | ⌧ | ⌧ |
| MCU Group | Pulse period measurement mode | Event link function through ELC HAL driver | Compare/Match Function |
|---|---|---|---|
| S124 | ⌧ | ⌧ | ✓ |
| S128 | ⌧ | ⌧ | ✓ |
| S1JA | ⌧ | ⌧ | ✓ |
| S3A1 | ⌧ | ⌧ | ✓ |
| S3A3 | ⌧ | ⌧ | ✓ |
| S3A6 | ⌧ | ⌧ | ✓ |
| S3A7 | ⌧ | ⌧ | ✓ |
| S5D3 | ⌧ | ⌧ | ✓ |
| S5D5 | ⌧ | ⌧ | ✓ |
| S5D9 | ⌧ | ⌧ | ✓ |
| S7G2 | ⌧ | ⌧ | ✓ |
The AGT HAL module defines APIs for opening, closing, starting and stopping timers. A complete list of the available APIs, an example API call and a short description of each can be found in the following table. A table of status return values follows the API summary table.
AGT HAL Module API Summary
| Function Name | Example API Call and Description |
|---|---|
| open | g_timer0.p_api->open(g_timer0.p_ctrl, g_timer0.p_cfg)Initial configuration. |
| stop | g_timer0.p_api->stop(g_timer0.p_ctrl)Stop the counter. |
| start | g_timer0.p_api->start(g_timer0.p_ctrl)Start the counter. |
| reset | g_timer0.p_api->reset(g_timer0.p_ctrl)Reset the counter initial value. |
| counterGet | g_timer0.p_api->counterGet(&value)Get current counter value and store it in the provided pointer, value. |
| periodSet | g_timer0.p_api->periodSet(g_timer0.p_ctrl, period, unit)Set the time until the timer expires. |
| dutyCycleSet | g_timer0.p_api->dutyCycleSet(g_timer0.p_ctrl, period, unit, pin)Sets the time until the duty cycle expires. |
| infoGet | g_timer0.p_api->infoGet(&info)Get the time until the timer expires in clock counts and store it in provided pointer, info. |
| close | g_timer0.p_api->close(g_timer0.p_ctrl)Allows driver to be reconfigured and may reduce power consumption. |
| versionGet | g_timer0.p_api->versionGet(g_timer0.p_ctrl, &version)Retrieve the API version with the version pointer. |
Status Return Values
| Name | Description |
|---|---|
| SSP_SUCCESS | Operation is successful. |
| SSP_ERR_ASSERTION | Parameter is NULL or configuration setting is not allowed. |
| SSP_ERR_IN_USE | The channel specified is already open. |
| SSP_ERR_IRQ_BSP_DISABLED | A required interrupt is not enabled in the BSP. |
| SSP_ERROR_NOT_OPEN | The channel is not open. |
| SSP_ERR_INVALID_ARG | Invalid argument provided. |
| SSP_ERR_INVALID_HW_CONDITION | Invalid hardware setting detected. |
| SSP_ERR_INVALID_PTR | A pointer parameter was NULL, but needed a non-NULL value. |
The AGT HAL module configures a timer to a user-specified period. When the period elapses, the CPU can be interrupted, a port pin can be toggled, a transfer of data using the DMAC or DTC can be initiated, or another peripheral can be triggered to begin operation.
The following figure shows a flowchart for toggling a port pin or generating a CPU interrupt after a specified period. This flowchart is appropriate for both AGT and GPT counters. (Replace the GPT references with AGT references for AGT operation. AGT is a down counter so change overflow to underflow.)
Two different timer modules, the GPT and the AGT, are supported in the SSP. The following sections provide information on both modules so that the developer can compare and contrast the capabilities of each module for a particular application. For additional information on the GPT, refer to the GPT User's Guide.
The GPT module is recommended for most generic timer applications, but either module can be used for a basic timer functionality. The use cases in which one timer module would be preferred over the other are described as follows:
Selecting the GPT Timer Module
The GPT module uses a high-resolution 32-bit counter that can only be clocked by PCLKA. There are more GPT channels than AGT channels on Synergy devices, so using the GPT is less likely to cause a resource conflict.
Selecting the AGT Timer Module
The AGT module uses a 16-bit counter that can be clocked by PCLKB, LOCO or Fsub. If clocked by LOCO or Fsub, the AGT interrupt can be used to wake the MCU from sleep modes. There are two channels, and channel 1 can be clocked by channel 0 underflow, effectively creating a 32-bit cascaded timer.
The maximum time period depends on the timer type and the input clock frequency.
The AGT counter underflow interrupt for the selected channel used must be enabled in the BSP in the following situations:
When the count source selected as AGTO Fsub or AGTO LOCO, turn on the count source using call cgc_api_t::clockStart API before calling timer_api_t::open.
When the AGTn AGTI interrupt is enabled in the BSP, the corresponding ISR is defined in the timer driver. The ISR calls a user-callback function if one was registered in open.
AGT Output Timer Signal
If the timer output is configured, (AGTO Output Enabled set to true) the output pin starts at a high level if the output inverted is configured to True and a low level if it is configured to False. The output pin toggles every time the period elapses, beginning with the first time the period elapses after the timer is started.
In one-shot mode, the output is also configured to toggle when the timer starts counting. This generates a pulse - the timer toggles from the stop level when counting begins and toggles back to the stop level when counting ends.
Timer Period Calculation
The timer period is defined as the time until the timer expires. When output compare is used, the output pin toggles once per period, so the traditional period (from rising edge to rising edge) is twice the period specified in the software.
Runtime period calculation based on the current clock settings is available from timer_api_t::open and timer_api_t::periodSet.
If the specified timer period is different than the raw counts, the period is calculated using the current timer clock frequency (see Configuring the GPT Clocks or Configuring the AGT Clocks). The current timer clock frequency is determined using systemClockFreqGet. This frequency is used in the appropriate formula from the following table as clk_freq_hz.
Timer Period Calculation
| Timer Units | Description |
|---|---|
| TIMER_UNIT_PERIOD_NSEC | Counts = (period * clk_freq_hz)/ (1000000000 * channel_0_period) |
| TIMER_UNIT_PERIOD_USEC | Counts = (period * clk_freq_hz)/ (1000000 * channel_0_period) |
| TIMER_UNIT_PERIOD_MSEC | Counts = (period * clk_freq_hz)/ (1000 * channel_0_period) |
| TIMER_UNIT_PERIOD_SEC | Counts = (period * clk_freq_hz) / (channel_0_period) |
| TIMER_UNIT_FREQUENCY_HZ | Counts = (clk_freq_hz)/ (period * channel_0_period) |
| TIMER_UNIT_FREQUENCY_KHZ | Counts = (clk_freq_hz)/ (1000 * period * channel_0_period) |
Timer Period Calculation
If the requested period is larger than the counter size (32-bit or 16-bit), the driver selects the smallest divisor that allows the result to fit in the counter size. If the counter value is larger than the counter size with the largest divisor (1024), an error code (SSP_ERR_INVALID_ARGUMENT) is returned.
Triggering DMAC/DTC with GPT
To trigger a transfer of data using the DMAC or DTC peripheral when the timer period elapses, configure the DMAC/DTC transfer with activation_source set to ELC_EVENT_GPTn_COUNTER_OVERFLOW (where n is the GPT channel number). See the DMAC or DTC guides for further information.
Triggering ELC Events with GPT
The GPT timer can trigger the start of other peripherals. The ELC guide provides a list of all available peripherals.
Triggering DMAC/DTC with AGT
To trigger a transfer of data using the DMAC or DTC peripheral when the timer period elapses, configure the DMAC/DTC transfer with activation_source set to ELC_EVENT_AGTn_AGTI (where n is the AGT channel number). See the Transfer Interface for further information.
Triggering ELC Events with AGT
The AGT timer can trigger the start of other peripherals. The ELC guide provides a list of all available peripherals listed in elc_peripheral_t. (See events and peripheral definitions for further information.)
Cascading AGT Timers to Create a 32-bit timer
AGT Channel 1 can be clocked by the AGT Channel 0 underflow, creating a cascaded 32-bit timer. In this mode, the AGT Channel 0 output frequency will be the input frequency of AGT Channel 1. With cascaded operation, longer time periods are achievable:
Cascaded Timer Period Calculation
AGT Channel 0 period calculation will be the same as normal timer period calculation as shown in the preceding table. If the requested period is larger than the counter size (32-bit or 16-bit), the driver selects the smallest divisor that allows the result to fit in the counter size.
AGT Channel 1 period calculation will be same as shown in the preceding table, except the "channel_0_period" value will be equal to AGT Channel 0's output period value (that is, after divisor selection).
Refer to the most recent SSP Release Notes for any additional operational limitations for this module.
This section describes how to include the AGT HAL Module in an application using the SSP configurator.
To add the Timer Driver to an application, simply add it to a thread using the stacks selection sequence given in the following table. (The default name for the Timer Driver is g_agt0. This name can be changed in the associated Properties window.)
AGT HAL Module Selection Sequence
| Resource | ISDE Tab | Stacks Selection Sequence |
|---|---|---|
| r_agt0 Timer Driver on r_agt | Threads | New Stack> Driver> Timers> Timer Driver on r_agt |
When the Timer Driver on r_agt is added to the thread stack as shown in the following figure, the configurator automatically adds any needed lower‑level modules. Any modules needing additional configuration information have the box text highlighted in Red. Modules with a Gray band are individual modules that stand alone. Modules with a Blue band are shared or common; they need only be added once and can be used by multiple stacks. Modules with a Pink band can require the selection of lower-level modules; these are either optional or recommended. (This is indicated in the block with the inclusion of this text.) If the addition of lower-level modules is required, the module description include Add in the text. Clicking on any Pink banded modules brings up the New icon and displays possible choices.
The AGT HAL Module must be configured by the user for the desired operation. The available configuration settings and defaults for all the user-accessible properties are given in the properties tab within the SSP configurator and are shown in the following tables for easy reference. Only properties that can be changed without causing conflicts are available for modification. Other properties are locked and not available for changes and are identified with a lock icon for the locked property in the Properties window in the ISDE. This approach simplifies the configuration process and makes it much less error-prone than previous manual approaches to configuration. The available configuration settings and defaults for all the user-accessible properties are given in the Properties tab within the SSP Configurator and are shown in the following tables for easy reference.
Configuration Settings for the AGT HAL Module on r_agt
| ISDE Property | Value | Description |
|---|---|---|
| Parameter Checking | BSP, Enabled, Disabled Default: BSP | Enables or disables parameter checking. |
| Name | g_timer0 | Module name. |
| Channel | 0 | Physical hardware channel. |
| Mode | Periodic, One Shot Default: Periodic | Warning: One Shot functionality is not available in the GPT hardware, so it is implemented in software by stopping the timer in the ISR called when the period expires. For this reason, ISR's must be enabled for one-shot mode even if the callback is unused. |
| Period Value | 10 | See Timer Period Calculation. |
| Period Unit | Raw Counts, Nanoseconds, Microseconds, Milliseconds, Seconds, Hertz, Kilohertz Default: Microseconds | See Timer Period Calculation. |
| Auto Start | True, False Default: True | Set to true to start the timer after configuring or false to leave the timer stopped until timer_api_t::start is called. |
| Count Source | PCLKB, PCLKB/8, PCLKB/2, LOCO, AGT0 Underflow, AGT0 fSub Default: PCLKB | The clock source for the AGT counter. |
| AGTO Output Enabled | True, False Default: False | Set to true to output the timer signal on a port pin configured for AGT (AGTO pin). Set to false for no output of the timer signal. |
| AGTIO Output Enabled | True, False Default: False | Set to true to output the timer signal on a port pin configured for AGT (AGTIO pin). Set to false for no output of the timer signal. |
| Output Inverted | True, False Default: False | Set to false to start the output signal low. Set to true to start the output signal high. |
| Enable comparator A output pin | True, False Default: False | Enable comparator A output pin selection. |
| Enable comparator B output pin | True, False Default: False | Enable comparator B output pin selection. |
| Callback | NULL | A user callback function can be registered in timer_api_t::open. If this callback function is provided, it will be called from the interrupt service routine (ISR) each time the timer period elapses. Warning: Since the callback is called from an ISR, care should be taken not to use blocking calls or lengthy processing. Spending excessive time in an ISR can affect the responsiveness of the system. |
| Interrupt Priority | Priority 0 (highest), Priority 1:14, Priority 15 (lowest - not valid if using ThreadX) Default: Disabled | Timer interrupt priority. 0 is the highest priority. |
The AGT timer is clocked based on the PCLKB, LOCO, Fsub or AGT Underflow frequency. The AGT clock is selectable in the Properties window in e2 studio. You can set the clock frequencies using the clock configurator in e2 studio or the CGC Interface at run-time.
The AGT peripheral module uses pins on the MCU to communicate to external devices. I/O pins must be selected and configured as required by the external device. The following table illustrates the method for selecting the pins within the SSP configuration window and the subsequent table illustrates an example selection for the associated pins.
Pin Selection for the AGT HAL Module on r_agt
| Resource | ISDE Tab | Pin selection Sequence |
|---|---|---|
| AGT | Pins | Select Peripherals> Timer: AGT> AGT0 |
Pin Configuration Settings for the AGT HAL Module on r_agt
| Property | Value | Description |
|---|---|---|
| Operation Mode | Disabled, Custom, Timer Output, Compare Match, Count Measurement, Gated Count Default: Disabled | Select timer operation mode. |
| AGTIO | None Default: None | AGTIO Pin. |
| AGTO | None, P102 Default: P102 | AGTO Pin. |
| AGTOA | None Default: None | AGTOA Pin. |
| AGTOB | None Default: None | AGTOB Pin. |
| AGTEE | None, P101 Default: P101 | AGTEE Pin. |
The typical steps in using the AGT HAL Module in an application are:
These common steps are illustrated in a typical operational flow diagram in the following figure: