![]() |
Synergy Software Package User's Manual
|
The General PWM Timer (GPT) HAL module provides a high-level API for timer applications and uses the GPT peripheral on the Synergy MCU. A user-defined callback can be created to respond to a timer event.
The GPT HAL module configures a timer to a user-specified period. When the period elapses, any of the following events can occur:
General PWM Timer (GPT)
The following hardware features are, or are not, supported by SSP for GPT.
Legend:
| Symbol | Meaning |
|---|---|
| ✓ | Available (Tested) |
| ⌧ | Not Available (Not tested/not functional or both) |
| N/A | Not supported by MCU |
| MCU Group | Saw Waves | Triangle Waves | PWM waveform for controlling brushless DC motors | Compare match output for Low, High, and Toggle | Input capture function | Automatic addition of dead time |
|---|---|---|---|---|---|---|
| S124 | ✓ | ⌧ | ⌧ | ✓ | ⌧ | ⌧ |
| S128 | ✓ | ⌧ | ⌧ | ✓ | ⌧ | ⌧ |
| S1JA | ✓ | ⌧ | ⌧ | ✓ | ⌧ | ⌧ |
| S3A1 | ✓ | ⌧ | ⌧ | ✓ | ⌧ | ⌧ |
| S3A3 | ✓ | ⌧ | ⌧ | ✓ | ⌧ | ⌧ |
| S3A6 | ✓ | ⌧ | ⌧ | ✓ | ⌧ | ⌧ |
| S3A7 | ✓ | ⌧ | ⌧ | ✓ | ⌧ | ⌧ |
| S5D3 | ✓ | ⌧ | ⌧ | ✓ | ⌧ | ⌧ |
| S5D5 | ✓ | ⌧ | ⌧ | ✓ | ⌧ | ⌧ |
| S5D9 | ✓ | ⌧ | ⌧ | ✓ | ⌧ | ⌧ |
| S7G2 | ✓ | ⌧ | ⌧ | ✓ | ⌧ | ⌧ |
| MCU Group | PWM Mode | Phase Count Function | One-Shot Operation | Event link function through ELC HAL driver | Noise filtering function |
|---|---|---|---|---|---|
| S124 | ✓ | ⌧ | ✓ | ⌧ | ⌧ |
| S128 | ✓ | ⌧ | ✓ | ⌧ | ⌧ |
| S1JA | ✓ | ⌧ | ✓ | ⌧ | ⌧ |
| S3A1 | ✓ | ⌧ | ✓ | ⌧ | ⌧ |
| S3A3 | ✓ | ⌧ | ✓ | ⌧ | ⌧ |
| S3A6 | ✓ | ⌧ | ✓ | ⌧ | ⌧ |
| S3A7 | ✓ | ⌧ | ✓ | ⌧ | ⌧ |
| S5D3 | ✓ | ⌧ | ✓ | ⌧ | ⌧ |
| S5D5 | ✓ | ⌧ | ✓ | ⌧ | ⌧ |
| S5D9 | ✓ | ⌧ | ✓ | ⌧ | ⌧ |
| S7G2 | ✓ | ⌧ | ✓ | ⌧ | ⌧ |
The GPT HAL module defines APIs to open, start, stop, read status, trim and close the module. 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.
GPT 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 has already been opened. |
| SSP_ERROR_NOT_OPEN | The channel is not open. |
| SSP_ERR_INVALID_ARGUMENT | Invalid argument provided. |
The GPT 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:
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 AGT, refer to the AGT 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 following use cases describe why one timer module would be preferred over the other.
Selecting the GPT Timer Module
The GPT module uses a high-resolution 32-bit counter that can only be clocked by PCLKD. There are more GPT channels than AGT channels on Synergy devices, so using 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 GPT counter overflow interrupt must be enabled in the following situations:
When counter overflow interrupt is enabled, the corresponding ISR is linked in the vector table. The ISR calls a user callback function if one was registered in open.
Note: Interrupts may be skipped when used with the DTC peripheral with irq set to TRANSFER_IRQ_END.
GPT Output Timer Signal
If the timer output is configured (GTIOCA/B Output Enabled set to True), the output pin will start at the GTIOCA/B Stop Level and toggle 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 will toggle 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 open and periodSet.
Timer Period Calculation
| Timer Units | Formula |
|---|---|
| TIMER_UNIT_PERIOD_NSEC | Counts = (period * clk_freq_hz) / 1000000000 |
| TIMER_UNIT_PERIOD_USEC | Counts = (period * clk_freq_hz) / 1000000 |
| TIMER_UNIT_PERIOD_MSEC | Counts = (period * clk_freq_hz) / 1000 |
| TIMER_UNIT_PERIOD_SEC | Counts = (period * clk_freq_hz) |
| TIMER_UNIT_FREQUENCY_HZ | Counts = (clk_freq_hz) / period |
| TIMER_UNIT_FREQUENCY_KHZ | Counts = (clk_freq_hz) / 1000 * period |
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.
Free Running Counter Mode
To use the GPT as a free running counter, set the Period to 0xFFFFFFFF for a 32-bit timer or 0xFFFF for a 16-bit timer and the Period Unit to Raw Counts in the Properties window of the Synergy Configuration tool. Stop and start the timer using the timer_api_t::stop API and the timer_api_t::start API. Check the counter value using the timer_api_t::counterGet API. Reset the timer using the timer_api_t::reset API. If the counter overflows, handle the counter overflow in the callback.
GPT PWM Mode
To use the GPT in PWM mode, set the Period and Duty cycle and select the duty cycle range.
The driver provides two options to select the duty cycle range:
This section describes how to include the GPT 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_timer0. This name can be changed in the associated Properties window.)
GPT HAL Module Selection Sequence
| Resource | ISDE Tab | Stacks Selection Sequence |
|---|---|---|
| g_timer0 Timer Driver on r_gpt | Threads> HAL/Common | New Stack> Driver> Timers> Timer Driver on r_gpt |
When the Timer Driver on r_gpt 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 GPT 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 GPT HAL Module on r_gpt
| ISDE Property | Value | Description |
|---|---|---|
| Parameter Checking | BSP, Enabled, Disabled Default: BSP | Enables or disables the parameter checking. |
| Name | g_timer0 | Module name. |
| Channel | 0 | Channel selection. |
| Mode | Periodic, One Shot, PWM 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: Milliseconds | See Timer Period Calculation. |
| Duty Cycle Value | 50 | Duty cycle value selection. |
| Duty Cycle Unit | Unit Raw Counts, Unit Percent, Unit Percent x 1000 Default: Unit Raw Counts | Duty cycle unit selection. |
| 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. |
| GTIOCA Output Enabled | True, False Default: False | Set to true to output the timer signal on a port pin configured for GPT. Set to false for no output of the timer signal. |
| GTIOCA Stop Level | Pin Level Low, Pin Level High, Pin Level Retained Default: Pin Level Low | Controls output pin level when the timer is stopped. |
| GTIOCB Output Enabled | True, False Default: False | Set to true to output the timer signal on a port pin configured for GPT. Set to false for no output of the timer signal. |
| GTIOCB Stop Level | Pin Level Low, Pin Level High, Pin Level Retained Default: Pin Level Low | Controls output pin level when the timer is stopped. |
| 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. |
| Overflow Interrupt Priority | Priority 0 (highest), Priority 1:14, Priority 15 (lowest - not valid if using ThreadX) Default: Disabled | Overflow interrupt priority selection. |
The GPT timer is clocked based on the PCLKD frequency. You can set the PCLKD frequency using the clock configurator in the ISDE Configuring Clocks tab, or the CGC Interface at run-time.
The GPT 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 lists the method used to select pins within the SSP configuration window and the subsequent table lists an example selection for the associated pins:
Pin Selection for the GPT HAL Module on r_gpt
| Resource | ISDE Tab | Pin selection Sequence |
|---|---|---|
| GPT | Pins | Select Peripherals> Timer: GPT> GPT0 |
Pin Configuration Settings for GPT HAL Driver
| Property | Value | Description |
|---|---|---|
| Pin Group Selection | Mixed, _A Only, _B Only Default: Mixed | Select pin group mapping. |
| Operation Mode | Disabled, GTIOCA or GTIOCB, GTIOCA and GTIOCB Default: Disabled | Select timer operation mode. |
| GTIOCA: | None, P300, P512 Default: P512 | GTIOCA Pin. |
| GTIOCB: | None, P108, P511 Default: P511 | GTIOCB Pin. |
The typical steps in using the GPT HAL module in an application are:
In PWM mode, there will be one extra PCLK added by the hardware in ON time if Duty cycle range is selected to GPT_SHORTEST_LEVEL_OFF and in OFF time if the Duty cycle range is selected to GPT_SHORTEST_LEVEL_ON.
These common steps are illustrated in a typical operational flow diagram in the following figure: