Skip to content

Instantly share code, notes, and snippets.

@sabicalija
Last active October 30, 2019 15:41
Show Gist options
  • Save sabicalija/b1229bee89599dd1b15abfcbaa095655 to your computer and use it in GitHub Desktop.
Save sabicalija/b1229bee89599dd1b15abfcbaa095655 to your computer and use it in GitHub Desktop.
TM4C1294NCPDT Timer

Timer

The GPT Modules of the TM4C1294NCDPT can be driven by 4 different clock sources. Either by the

  • System Clock, or a global
  • Alternate Clock (ALTCLK)
    • Precision Internal Oscillator (PIOSC)
    • Hibernation Module Real-time clock output (RTCOSC)
    • Low-frequency internal oscillator (LFIOSC)

When operating the GPT Modules with a system clock of 5 MHz, the modules will receive the same amount of clock ticks.

timer1

For a better presentation, we may display the clock ticks in the following way.

timer2

During the time of 1 second, the clock receives 5.000.000 ticks.

Timeout

To generate a timeout with a 5 Hz frequency, for instance, we have to divide the clock ticks per second by the desired frequency, that is 5.000.000 by 5. We can now use a timer to count the clock ticks (= 1.000.000) to generate timeout events with the desired frequency.

timer3

Alternatively, if we want a timeout every 200 ms, we can multiply the clock ticks by the duration (in seconds), that is 5.000.000 by 0.2.

These timeout events could be used to register a ISR which toggles LEDs on the board.

timer4

NOTE: When counting up, the timer will start from 0 and count until reaching the timeout value (LOAD).
It won't (!) load the value (LOAD) as a initial value, counting until reaching an overflow.

Match

The GPT Modules of the TM4C1294NCPDT support the generation of IRQs when the clock counter matches a specific value. The counter of the timer is only reloaded after a timeout event. However, interrupts are generated on both events.

timer7

PWM with Match
The match and load values of the GPT Modules can be used, to generate PWM signals with duty cycles corresponding the ratio of the values of the match and load register. However, PWM signal generation (with varying duty cycle) can be achieved without a match interrupt event, simply by reloading the timeout value (LOAD) after each cycle.

For a duty cycle of 25%, with a PWM frequency of 5 Hz, we have to activate the LED for 25% of the PWM period. The PWM period is 200 ms (T). Therefore, we have to activate the LED for 50 ms (Ton) and deactivate for 150 ms (Toff).

timer5

If we update the match value of the timer during operation, we can generate PWM signals with different duty cycles.

timer6

Prescaler

The GPTM of the TM4C1294NCPDT provides two independent 16 bit timer per module (Timer A, Timer B), which can be concatenated to operate as a single 32 bit timer.

When working as a 32 bit timer, the prescaler settings have no effect. However, when operated as 16 bit timer, each timer has a 8 bit prescaler.

A prescaler basically behaves as a frequency divider, scaling the frequency of the clock provided to the timer. Following figure illustrates the frequency (and ticks) of the timer counter with different prescaler values.

timer8

Exercises

Practice makes perfect!

Exercise 01: Toggle

Implement a simple timer application which toggles the board LED with a frequency configurable with a macro.

#define FREQUENCY (8) // in Hz

The timer should trigger an IRQ, which is served by an ISR, toggling the LED.

Implement the application with different timer and timer configurations.

  • Systick
  • GPTM: 16 Bit (down, prescaler)
  • GPTM: 16 Bit (up, prescaler)
  • GPTM: 32 Bit (down)
  • GPTM: 32 Bit (up)

What maximal and minimal values are possible for the macro FREQUENCY, for the different setups?

Exercise 02: Chasing Light

Implement a simple chasing light application, which increments the position of the chasing light after every timeout

Use either of the two:

  • Systick
  • GPTM: 32 Bit (down)

Extension 1:
Provide a macro to define the frequency in a single place.

Extension 2:
Allow changing (increment/decrement) the direction of the chasing light by pressing the buttons.

Exercise 03: Dimmer

Implement a simple dimmer application, allowing to set the brightness (duty cycle) of the LEDs from 0-100%.

#define BRIGHTNESS (75) // in %

Use either of the two:

  • Systick
  • GPTM: 32 Bit (down)

Advanced
Implement the same functionality, but use the PWM mode and the CCP functionality of the GPTMs, connected GPIO pins, to drive the pin state.

Exercise 04: Chasing Light & Button

Implement a simple chasing light application, which increments the position of the chasing light after a timeout.
Allow the user to increment/decrement the speed, from 1 to 10 s, in 1 s steps.

Exercise 05: Dimmer & Button

Implement a simple dimmer application, allowing to set the brightness (duty cycle) of the LED.
Allow the user to increment/decrement the brightness from 0-100%, in 10% steps.

Extension 1:
After reaching 100%, the application should restart again from 0% on the next button press.

HowTo: Timers on TivaC

API

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

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

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.

  • SysTickPeriodSet()

ISRs can be registered with a register function.

  • SysTickIntRegister()

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()

GPTM

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).

  • TimerConfigure()

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.

  • TimerLoadSet()

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.

  • TimerMatchSet()

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.

  • TimerUpdateMode()

Prescaler
To set the prescaler value of a timer, use the following function.

  • TimerPrescalerSet()

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.

  • TimerIntRegister()

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.

  • TimerControlStall()

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).

Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment