There are basically two approaches. You can either study
- the microcontroller manual (TM4C1294NCPDT) and implement your application by directly manipulating the registers and special function registers (SFR), or study
- the peripheral driver manual (TivaWare) and implement your application using the functions provided by the API.
When working at "register" level, there are several files defining useful macros, for working with both timers.
hw_nvic.h
: defines macros for the SysTick timer, which is a part of the NVIC controller
hw_timer.h
: defines macros for the GPTM timer
Using the TivaWare "driver" API is outlined below. For further details on both approaches, please refer to the mentioned manuals.
TivaWare provides several function for working with timers (SysTick, GPTM) on the TM4C1294NCPDT.
First section shows functions most important functions of the SysTick timer.
The second section show functions related to GPTMs and provides some guidance on LOAD
and MATCH
calculation for different modes (16 bit up/down, 32 bit).
SysTick is a simple 24 bit timer that is part of the NVIC controller in the Cortex-M microprocessor.
Its intended purpose is to provide a periodic interrupt for an RTOS, but it can be used for other simple timing purposes.
#include <driverlib/systick.h>
Loading start values (LOAD
) is achieved with the following function.
ISRs can be registered with a register function.
The SysTick ISR does not need to clear the SysTick interrupt source as it is cleared automatically by the NVIC when the SysTick ISR is called.
Following two functions are used to enable or disable the IRQ, respectively.
SysTickIntEnable()
SysTickIntDisable()
Finally, the following functions are provided allowing to enable or disable the SysTick timer, respectively.
SysTickEnable()
SysTickDisable()
The GPT Module is probably one of the most complex modules of the TM4C1294NCPDT microcontroller.
#include <driverlib/timer.h>
Usually, you'll want to operate a GPT module as a 32 bit (count-down) timer.
However, each timer module consists of two independent 16 bit timer (Timer A, Timer B) which can be concatenated to a single 32 bit timer.
Each timer unit can be operated in different modes (one-shot, periodic, input edge count/time, PWM).
Configuration
The TivaWare provides a function to configure the timer modules (Timer A, Timer B).
Several macros are provided and can be used in combination to enable the different modes.
Make sure that the timer is disabled before providing a new configuration.
When using a GPTM in split-pair mode, that is, one of the 16 bit timers, you have to provide a configuration for both.
// Periodic 32 bit timer (down).
TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
// Periodic 16 bit timer (down) and one-shot 16 bit timer (up).
TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PERIODIC | TIMER_CFG_B_ONE_SHOT_UP);
Note, however, that the SysTick timer does not provide such a function, as no other mode, except for count down, is provided.
Interval Load & Match
Loading start (or overflow) values (LOAD
) is achieved with the following function.
When in count-down mode, the timer will load this value and start couting down to 0 (timeout).
In count-up mode, the timer will start from 0 and count-up until reaching the loaded value.
Note, that when using the GPT module as 32 bit timer, you have to use TIMER_A
for the configuration and loading values (LOAD
, MATCH
).
// Set 32 bit LOAD value
TimerLoadSet(TIMER0_BASE, TIMER_A, 0xffffffff);
// Set 16-bit LOAD value for both timer
TimerLoadSet(TIMER0_BASE, TIMER_BOTH, 0xffff);
In periodic mode, the timer will reload the initial value, depending on the current count direction, and repeat the complete operation.
In one-shot mode, the timer will stop after the first iteration.
To provide the timer a value to compare against the counter (MATCH
), use the following function.
Update
The default behavior of the timer is to update the counter or match values (LOAD
, MATCH
), immediately, on the next clock cycle, when TimerLoadSet()
is called.
If desired, the counter and match values can be updated after a timeout, independently.
Prescaler
To set the prescaler value of a timer, use the following function.
NOTE: Values from 0-255, will result in prescale values from 1-256.
IMPORTANT: The prescaler of GPT modules of the TM4C1294NCPDT behaves differently, depending on the count direction (up/down).
When counting down, the prescaler behaves as a true prescaler, effectively, dividing the clock frequency of the timer.
However, when counting up, the prescaler behaves as a linear extension to the counter, effectively creating a 24 bit timer.
When counting down, you have to choose a prescaler and load the 16 bit start value (LOAD
).
Note, that the size of the prescaler has changes the resolution of the timer.
When counting up, you have to calculate a 24 bit start value (without frequency division).
The upper 8 bits are loaded into the prescaler, the lower 16 bits are the required timeout value (LOAD
).
Interrupt
ISRs for timer interrupts can be registered with a register function.
Individual interrupt sources (timeout, match) within the a timer module are managed with following two functions.
TimerIntEnable()
TimerIntDisable()
Inside the ISR, you can use following function to retrieve and clear the current interrupt status, respectively.
TimerIntStatus()
TimerIntClear()
Debugging
When stoping the microcontroller during debugging, the timer continues counting, on default.
Following function can be used to configure the behavior during debugging.
Pin Control
Some GPIO are connected to the GPT Modules (Alternate Pin Function
, cf. GPIO Pins and Alternate Functions, Table 10-2).
When enabled, the state of those pins can be control by timers.
Hence, ISRs to toggle the pin state are not required.
GPIOPinConfigure()
: allow the timer to drive the GPIO pin state.
TimerConfigure()
: enable and configure timer functionality.
This could be used to toggle the board LED without an intervention by the Cortex-M microprocessor.
Additionally, with a changing match value, different Duty Cycles can be achieved, without manually controlling the pin state (cf. 16-Bit PWM Mode Example, Figure 13-4).