Skip to content

Commit 7ab463e

Browse files
readd timer functions in case people want to use them
1 parent 4adee9f commit 7ab463e

File tree

3 files changed

+120
-32
lines changed

3 files changed

+120
-32
lines changed

docs/headers/sys/timers.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ sys/timers.h
99
1010
This header includes defines for the CE's 3 hardware timers.
1111
It is discouraged from directly modifying the timers themseleves, and instead use the standard C `clock()` fuction.
12-
These functions provide direct maniplution of the hardware timers.
13-
Keep in mind that using them may cause the `clock()` and related functions to not work correctly.
14-
This header also includes functions such as `sleep()` and `delay()` which are based around `clock()`'s functionality and are always safe to use.
12+
This is because the toolchain uses the timers in the following way:
13+
14+
* Timer 1 is used for `clock()` and related functions like `sleep()`.
15+
* Timer 2 is used by the :ref:`usbdrvce library <usbdrvce_h>`.
16+
* Timer 3 is used by the TI-OS USB stack and shouldn't be touched in most every case.
17+
18+
Directly manipulating the hardware timers may cause the above functions and/or libraries to not work correctly.
19+
However, for example if you aren't using usbdrvce it is possible to use Timer 2 in your application.
1520

1621
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.
1722

src/ce/include/sys/timers.h

Lines changed: 106 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* @file
33
* @authors
44
* Matt "MateoConLechuga" Waltz\n
5-
* Jacob "jacobly" Young
5+
* Jacob "jacobly" Young\n
6+
* Zachary "Runer112" Wassall
67
* @brief CE hardware timers define file
78
*/
89

@@ -16,24 +17,17 @@
1617
extern "C" {
1718
#endif
1819

19-
/* @cond */
20-
/* @endcond */
21-
2220
/**
2321
* Suspends execution of the calling thread for (at least) @p msec milliseconds.
2422
*
2523
* @param[in] msec number of milliseconds
26-
* @see sleep
27-
* @see usleep
2824
*/
2925
void delay(uint16_t msec);
3026

3127
/**
3228
* Suspends execution of the calling thread for (at least) @p msec milliseconds.
3329
*
3430
* @param[in] msec number of milliseconds
35-
* @see sleep
36-
* @see usleep
3731
*/
3832
void msleep(uint16_t msec);
3933

@@ -44,11 +38,9 @@ void msleep(uint16_t msec);
4438
* @note
4539
* Currently, signals do not exist, so this will never be interrupted.
4640
*
47-
* @param[in] seconds number of seconds
41+
* @param[in] seconds number of seconds (must be < 65536).
4842
* @return zero if the requested time has elapsed, or the number of seconds left
4943
* to sleep, if the call was interrupted by a signal handler
50-
* @see delay
51-
* @see usleep
5244
*/
5345
unsigned int sleep(unsigned int seconds);
5446

@@ -57,8 +49,6 @@ unsigned int sleep(unsigned int seconds);
5749
*
5850
* @param[in] ticks number of clock ticks
5951
* @see CLOCKS_PER_SEC
60-
* @see delay
61-
* @see usleep
6252
*/
6353
void ticksleep(unsigned long ticks);
6454

@@ -81,9 +71,6 @@ typedef unsigned int useconds_t;
8171
*
8272
* @param[in] usec number of microseconds
8373
* @return 0 on success, or -1 on error, with \c errno set to indicate the error
84-
* @see delay
85-
* @see sleep
86-
* @see useconds_t
8774
*/
8875
int usleep(useconds_t usec);
8976

@@ -92,29 +79,123 @@ int usleep(useconds_t usec);
9279
*/
9380
void boot_WaitShort(void);
9481

95-
/* @cond */
82+
/**
83+
* Enables timer \p n with the specified settings.
84+
*
85+
* @param[in] n Timer to enable (1,2,3).
86+
* @param[in] rate Rate in Hz the timer ticks at. Can be TIMER_32K or TIMER_CPU.
87+
* @param[in] int Throw an interrupt when the timer reaches 0. Can be TIMER_0INT or TIMER_NOINT.
88+
* @param[in] dir Direction in which to count. Can be TIMER_UP or TIMER_DOWN.
89+
*/
9690
#define timer_Enable(n, rate, int, dir) (timer_Control = timer_Control & ~(0x7 << 3 * ((n) - 1) | 1 << ((n) + 8)) | (1 | (rate) << 1 | (int) << 2) << 3 * ((n) - 1) | (dir) << ((n) + 8))
91+
92+
/**
93+
* Disables a timer.
94+
*
95+
* @param[in] n Timer to disable (1,2,3).
96+
*/
9797
#define timer_Disable(n) (timer_Control &= ~(1 << 3 * ((n) - 1)))
98+
99+
/**
100+
* Gets the current count value of a timer.
101+
*
102+
* @param[in] n Timer to get count value of (1,2,3).
103+
*
104+
* @warning
105+
* Do not use this function if the timer is configured with TIMER_CPU.
106+
* Use the timer_GetSafe() function instead.
107+
*/
98108
#define timer_Get(n) atomic_load_32(TIMER_COUNT_ADDR(n))
109+
110+
/**
111+
* Safely gets the current count value of a timer.
112+
* This should be used if the timer is ticking at >= 1MHz.
113+
*
114+
* @param[in] n Timer to get count value of (1,2,3).
115+
* @param[in] dir Direction the timer is counting.
116+
*/
99117
#define timer_GetSafe(n, dir) \
100118
((dir) == TIMER_UP ? \
101119
atomic_load_increasing_32(TIMER_COUNT_ADDR(n)) : \
102120
atomic_load_decreasing_32(TIMER_COUNT_ADDR(n)))
121+
122+
/**
123+
* Sets the count value of a timer.
124+
*
125+
* @param[in] n Timer to set count value of (1,2,3).
126+
* @param[in] value Value to set timer count to.
127+
*/
103128
#define timer_Set(n, value) *TIMER_COUNT_ADDR(n) = (uint32_t)(value)
129+
130+
/**
131+
* Gets the current reload value of a timer.
132+
* The reload value is loaded into the timer count when the timer reaches zero.
133+
*
134+
* @param[in] n Timer to get count reload value of (1,2,3).
135+
*/
104136
#define timer_GetReload(n) *TIMER_RELOAD_ADDR(n)
137+
138+
/**
139+
* Sets the reload value of a timer.
140+
* The reload value is loaded into the timer count when the timer reaches zero.
141+
*
142+
* @param[in] n Timer to set count reload value of (1,2,3).
143+
* @param[in] value Value to set timer reload count to.
144+
*/
105145
#define timer_SetReload(n, value) *TIMER_RELOAD_ADDR(n) = (uint32_t)(value)
146+
147+
/**
148+
* Gets the match \p m value of a timer.
149+
* There are two match value comparators per timer.
150+
*
151+
* @param[in] n Timer to get match comparator value of (1,2,3).
152+
* @param[in] m Match compartor index (1,2,3,
153+
* recommended to use TIMER_MATCH(1) or TIMER_MATCH(2)).
154+
*/
106155
#define timer_GetMatch(n, m) *TIMER_MATCH_ADDR(n, m)
156+
157+
/**
158+
* Sets the match \p m value of a timer.
159+
* There are two match value comparators per timer.
160+
*
161+
* @param[in] n Timer to set match comparator value of (1,2,3).
162+
* @param[in] m Match compartor index (1,2,3,
163+
* recommended to use TIMER_MATCH(1) or TIMER_MATCH(2)).
164+
* @param[in] value Value to set match compartor to.
165+
*/
107166
#define timer_SetMatch(n, m, value) *TIMER_MATCH_ADDR(n, m) = (uint32_t)(value)
167+
168+
/**
169+
* Acknowledges a timer interrupt.
170+
* This should be used to clear the condition that is causing the interrupt.
171+
*
172+
* @param[in] n Timer to acknowledge interrupt of (1,2,3).
173+
* @param[in] mask Interrupt mask, combination of TIMER_RELOADED, TIMER_MATCH(1),
174+
* or TIMER_MATCH(2).
175+
*/
108176
#define timer_AckInterrupt(n, mask) (timer_IntAcknowledge = (mask) << 3 * ((n) - 1))
177+
178+
/**
179+
* Checks if a timer interrupt condition has occurred.
180+
*
181+
* @param[in] n Timer to check interrupt for (1,2,3).
182+
* @param[in] mask Interrupt mask, combination of TIMER_RELOADED, TIMER_MATCH(1),
183+
* or TIMER_MATCH(2).
184+
*/
109185
#define timer_ChkInterrupt(n, mask) ((timer_IntStatus >> 3 * ((n) - 1)) & (mask))
110-
#define TIMER_32K 1
111-
#define TIMER_CPU 0
112-
#define TIMER_0INT 1
113-
#define TIMER_NOINT 0
114-
#define TIMER_UP 1
115-
#define TIMER_DOWN 0
116-
#define TIMER_MATCH(i) (1<<((i) - 1))
117-
#define TIMER_RELOADED (1<<2)
186+
187+
#define TIMER_32K 1 /**< Use the 32K clock for timer */
188+
#define TIMER_CPU 0 /**< Use the CPU clock rate for timer */
189+
#define TIMER_0INT 1 /**< Enable an interrupt when 0 is reached for the timer */
190+
#define TIMER_NOINT 0 /**< Disable interrupts for the timer */
191+
#define TIMER_UP 1 /**< Timer counts up */
192+
#define TIMER_DOWN 0 /**< Timer counts down */
193+
194+
#define TIMER_MATCH(i) (1<<((i) - 1)) /**< Timer hit the match value. There are 2 match values per timer */
195+
#define TIMER_RELOADED (1<<2) /**< Timer was reloaded (Needs TIMER_0INT enabled) */
196+
197+
/* @cond */
198+
/* Compatibility defines (do not use in new projects) */
118199
#define TIMER1_ENABLE (1<<0)
119200
#define TIMER1_DISABLE (0<<0)
120201
#define TIMER1_32K (1<<1)

src/ce/sleep.src

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ _sleep:
3232
ex (sp), hl ; hl = seconds
3333
push de
3434
; Convert from seconds to clock ticks.
35-
ld bc, 32768
36-
xor a, a
37-
ld e, a
38-
call __lmulu ; euhl = seconds * 32768
35+
ld b,15
36+
.loop:
37+
add hl,hl
38+
rla
39+
djnz .loop
40+
ld e,a ; euhl = seconds * 32768
3941
; = ticks
4042
; Join common code for the rest.
4143
jp ___sleep_common.1

0 commit comments

Comments
 (0)