sys/timers.h

#include <sys/timers.h>

This header includes defines for the CE’s 3 hardware timers. It is discouraged from directly modifying the timers themseleves, and instead use the standard C clock() fuction. This is because the toolchain uses the timers in the following way:

  • Timer 1 is used for clock() and related functions like sleep().

  • Timer 2 is used by the usbdrvce library.

  • Timer 3 is used by the TI-OS USB stack and shouldn’t be touched in most every case.

Directly manipulating the hardware timers may cause the above functions and/or libraries to not work correctly. However, for example if you aren’t using usbdrvce it is possible to use Timer 2 in your application.

There are two possible timing (“clock”) sources: a 32768 Hz crystal with similar accuracy to the clock found in any smartphone or wristwatch, and the CPU’s main 48 MHz clock with likely much inferior accuracy.

API Documentation

CE hardware timers define file.

Authors

Matt “MateoConLechuga” Waltz

Jacob “jacobly” Young

Zachary “Runer112” Wassall

Defines

timer_Enable(n, rate, int, dir)

Enables timer n with the specified settings.

Parameters
  • n[in] Timer to enable (1,2,3).

  • rate[in] Rate in Hz the timer ticks at. Can be TIMER_32K or TIMER_CPU.

  • int[in] Throw an interrupt when the timer reaches 0. Can be TIMER_0INT or TIMER_NOINT.

  • dir[in] Direction in which to count. Can be TIMER_UP or TIMER_DOWN.

timer_Disable(n)

Disables a timer.

Parameters
  • n[in] Timer to disable (1,2,3).

timer_Get(n)

Gets the current count value of a timer.

Warning

Do not use this function if the timer is configured with TIMER_CPU. Use the timer_GetSafe() function instead.

Parameters
  • n[in] Timer to get count value of (1,2,3).

timer_GetSafe(n, dir)

Safely gets the current count value of a timer.

This should be used if the timer is ticking at >= 1MHz.

Parameters
  • n[in] Timer to get count value of (1,2,3).

  • dir[in] Direction the timer is counting.

timer_Set(n, value)

Sets the count value of a timer.

Parameters
  • n[in] Timer to set count value of (1,2,3).

  • value[in] Value to set timer count to.

timer_GetReload(n)

Gets the current reload value of a timer.

The reload value is loaded into the timer count when the timer reaches zero.

Parameters
  • n[in] Timer to get count reload value of (1,2,3).

timer_SetReload(n, value)

Sets the reload value of a timer.

The reload value is loaded into the timer count when the timer reaches zero.

Parameters
  • n[in] Timer to set count reload value of (1,2,3).

  • value[in] Value to set timer reload count to.

timer_GetMatch(n, m)

Gets the match m value of a timer.

There are two match value comparators per timer.

Parameters
  • n[in] Timer to get match comparator value of (1,2,3).

  • m[in] Match compartor index (1,2,3, recommended to use TIMER_MATCH(1) or TIMER_MATCH(2)).

timer_SetMatch(n, m, value)

Sets the match m value of a timer.

There are two match value comparators per timer.

Parameters
  • n[in] Timer to set match comparator value of (1,2,3).

  • m[in] Match compartor index (1,2,3, recommended to use TIMER_MATCH(1) or TIMER_MATCH(2)).

  • value[in] Value to set match compartor to.

timer_AckInterrupt(n, mask)

Acknowledges a timer interrupt.

This should be used to clear the condition that is causing the interrupt.

Parameters
  • n[in] Timer to acknowledge interrupt of (1,2,3).

  • mask[in] Interrupt mask, combination of TIMER_RELOADED, TIMER_MATCH(1), or TIMER_MATCH(2).

timer_ChkInterrupt(n, mask)

Checks if a timer interrupt condition has occurred.

Parameters
  • n[in] Timer to check interrupt for (1,2,3).

  • mask[in] Interrupt mask, combination of TIMER_RELOADED, TIMER_MATCH(1), or TIMER_MATCH(2).

TIMER_32K

Use the 32K clock for timer.

TIMER_CPU

Use the CPU clock rate for timer.

TIMER_0INT

Enable an interrupt when 0 is reached for the timer.

TIMER_NOINT

Disable interrupts for the timer.

TIMER_UP

Timer counts up.

TIMER_DOWN

Timer counts down.

TIMER_MATCH(i)

Timer hit the match value.

There are 2 match values per timer

TIMER_RELOADED

Timer was reloaded (Needs TIMER_0INT enabled)

Typedefs

typedef unsigned int useconds_t

An unsigned integer type capable of holding integers in the range [0,1000000].

See also

usleep

Functions

void delay(uint16_t msec)

Suspends execution of the calling thread for (at least) msec milliseconds.

Parameters

msec[in] number of milliseconds

void msleep(uint16_t msec)

Suspends execution of the calling thread for (at least) msec milliseconds.

Parameters

msec[in] number of milliseconds

unsigned int sleep(unsigned int seconds)

Sleeps until the number of real-time seconds specified in seconds have elapsed or until a signal arrives which is not ignored.

Note

Currently, signals do not exist, so this will never be interrupted.

Parameters

seconds[in] number of seconds (must be < 65536).

Returns

zero if the requested time has elapsed, or the number of seconds left to sleep, if the call was interrupted by a signal handler

void ticksleep(unsigned long ticks)

Suspends execution of the calling thread for (at least) ticks clock ticks.

See also

CLOCKS_PER_SEC

Parameters

ticks[in] number of clock ticks

int usleep(useconds_t usec)

Suspends execution of the calling thread for (at least) usec microseconds.

The sleep may be lengthened slightly by any system activity or by the time spent processing the call or by the granularity of system timers.

Note

Currently, no errors are possible.

Parameters

usec[in] number of microseconds

Returns

0 on success, or -1 on error, with errno set to indicate the error

void boot_WaitShort(void)

Waits for ~10 ms.

In most cases it is better to use the delay() function.