sys/timers.h
#include <sys/timers.h>
This header includes defines for the CE’s hardware timers. 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
Defines
-
timer_Enable(n, rate, int, dir)
Enables timer
n
with the specified settings.The CE has 3 different timers.
Warning
Timer 2 is needed by library functions like clock() and sleep(). Timer 3 is needed by USB.
- Parameters
n – [in] Timer to enable (range 1 - 3 inclusive).
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 (range 1 - 3 inclusive).
-
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 (range 1 - 3 inclusive).
-
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 (range 1 - 3 inclusive).
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 (range 1 - 3 inclusive).
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 (range 1 - 3 inclusive).
-
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 (range 1 - 3 inclusive).
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 (range 1 - 3 inclusive).
m – [in] Match compartor index (range 1 - 2 inclusive, 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 (range 1 - 3 inclusive).
m – [in] Match compartor index (range 1 - 2 inclusive 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 (range 1 - 3 inclusive).
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 (range 1 - 3 inclusive).
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.See also
sleep
See also
usleep
- Parameters
msec – [in] number of milliseconds
-
void msleep(uint16_t msec)
Suspends execution of the calling thread for (at least)
msec
milliseconds.See also
sleep
See also
usleep
- 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.See also
delay
See also
usleep
Note
Currently, signals do not exist, so this will never be interrupted.
- Parameters
seconds – [in] number of seconds
- 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
See also
delay
See also
usleep
- 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.
See also
delay
See also
sleep
See also
useconds_t
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.