summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/nds/libnds/include/nds/timers.h
blob: 12f94b582a401cbfc767f1022aa3a1ca3c797da6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*---------------------------------------------------------------------------------
	Copyright (C) 2005
		Michael Noland (joat)
		Jason Rogers (dovoto)
		Dave Murphy (WinterMute)

	This software is provided 'as-is', without any express or implied
	warranty.  In no event will the authors be held liable for any
	damages arising from the use of this software.

	Permission is granted to anyone to use this software for any
	purpose, including commercial applications, and to alter it and
	redistribute it freely, subject to the following restrictions:

	1.	The origin of this software must not be misrepresented; you
		must not claim that you wrote the original software. If you use
		this software in a product, an acknowledgment in the product
		documentation would be appreciated but is not required.
	2.	Altered source versions must be plainly marked as such, and
		must not be misrepresented as being the original software.
	3.	This notice may not be removed or altered from any source
		distribution.

---------------------------------------------------------------------------------*/


/*! \file timers.h
    \brief Contains defines and macros for ARM7 and ARM9 timer operation.

	The timers are fed with a 33.4 MHz source on the ARM9.  The ARM7
	timing hasn't been tested yet, but is likely to be the same.
*/

//---------------------------------------------------------------------------------
#ifndef NDS_TIMERS_INCLUDE
#define NDS_TIMERS_INCLUDE
//---------------------------------------------------------------------------------


//---------------------------------------------------------------------------------
#ifdef __cplusplus
extern "C" {
#endif
//---------------------------------------------------------------------------------


#include <nds/jtypes.h>

// Timers

//!	A macro that calculates %TIMER_DATA(n) settings for a given frequency of n.
/*!	Use the appropriate macro depending on clock divider:  \n
	<ul>
		<li>	%TIMER_FREQ(f) </li>
		<li>	%TIMER_FREQ_64(f) </li>
		<li>	%TIMER_FREQ_256(f) </li>
		<li>	%TIMER_FREQ_1024(f) </li>
	</ul>
	Clock divider is controlled by %TIMER_CR(n) \n
	\n
	<b>Example Usage:</b>

		%TIMER_DATA(0) = %TIMER_FREQ(freq_in_hz); \n
		%TIMER_CR(0) = %TIMER_DIV1 | %TIMER_ENABLE; \n
	\n
	Max frequency is: 33554432Hz\n
	Min frequency is: 512Hz\n
*/
#define TIMER_FREQ(n)    (-0x2000000/(n))

//!	A macro that calculates %TIMER_DATA(n) settings for a given frequency of n.
/*!	Use the appropriate macro depending on clock divider:  \n
	<ul>
		<li>	%TIMER_FREQ(f) </li>
		<li>	%TIMER_FREQ_64(f) </li>
		<li>	%TIMER_FREQ_256(f) </li>
		<li>	%TIMER_FREQ_1024(f) </li>
	</ul>


	Clock divider is controlled by %TIMER_CR(n) \n
	\n
	<b>Example Usage:</b>
		%TIMER_DATA(x) = %TIMER_FREQ_64(freq_in_hz)\n
	\n
	Max frequency is: 524288Hz\n
	Min frequency is: 8Hz\n
*/
#define TIMER_FREQ_64(n)  (-(0x2000000>>6)/(n))

//!	A macro that calculates %TIMER_DATA(n) settings for a given frequency of n.
/*!	Use the appropriate macro depending on clock divider:  \n
	<ul>
		<li>	%TIMER_FREQ(f) </li>
		<li>	%TIMER_FREQ_64(f) </li>
		<li>	%TIMER_FREQ_256(f) </li>
		<li>	%TIMER_FREQ_1024(f) </li>
	</ul> <p>

	Clock divider is controlled by %TIMER_CR(n) \n
	\n
	<b>Example Usage:</b>
		%TIMER_DATA(x) = %TIMER_FREQ_256(freq_in_hz)\n
	\n
	Max frequency is: 131072Hz\n
	Min frequency is: 2Hz\n
*/
#define TIMER_FREQ_256(n) (-(0x2000000>>8)/(n))

//!	A macro that calculates %TIMER_DATA(n) settings for a given frequency of n.
/*!	Use the appropriate macro depending on clock divider:  \n
	<ul>
		<li>	%TIMER_FREQ(f) </li>
		<li>	%TIMER_FREQ_64(f) </li>
		<li>	%TIMER_FREQ_256(f) </li>
		<li>	TIMER_FREQ_1024(f) </li>
	</ul>

	Clock divider is controlled by %TIMER_CR(n) \n
	\n
	<b>Example Usage:</b>
		%TIMER_DATA(x) = %TIMER_FREQ_1024(freq_in_hz)\n
	\n
	Max frequency is: 32768Hz\n
	Min frequency is: 0.5Hz\n
*/
#define TIMER_FREQ_1024(n) (-(0x2000000>>10)/(n))


//!	Same as %TIMER_DATA(0).
#define TIMER0_DATA    (*(vuint16*)0x04000100)
//!	Same as %TIMER_DATA(1).
#define TIMER1_DATA    (*(vuint16*)0x04000104)
//!	Same as %TIMER_DATA(2).
#define TIMER2_DATA    (*(vuint16*)0x04000108)
//!	Same as %TIMER_DATA(3).
#define TIMER3_DATA    (*(vuint16*)0x0400010C)

//!	Returns a dereferenced pointer to the data register for timer number "n".
/*!	\see TIMER_CR(n)
	\see TIMER_FREQ(n)

	%TIMER_DATA(n) when set will latch that value into the counter.  Everytime the
	counter rolls over %TIMER_DATA(0) will return to the latched value.  This allows
	you to control the frequency of the timer using the following formula:\n
		%TIMER_DATA(x) = -(0x2000000/(freq * divider)); \n
	\n
	<b>Example Usage:</b>
	%TIMER_DATA(0) = value;  were 0 can be 0 through 3 and value is 16 bits.
*/
#define TIMER_DATA(n)  (*(vuint16*)(0x04000100+((n)<<2)))

// Timer control registers
//!	Same as %TIMER_CR(0).
#define TIMER0_CR   (*(vuint16*)0x04000102)
//!	Same as %TIMER_CR(1).
#define TIMER1_CR   (*(vuint16*)0x04000106)
//!	Same as %TIMER_CR(2).
#define TIMER2_CR   (*(vuint16*)0x0400010A)
//!	Same as %TIMER_CR(3).
#define TIMER3_CR   (*(vuint16*)0x0400010E)

//!	Returns a dereferenced pointer to the data register for timer control Register.
/*!	<b>Example Usage:</b> %TIMER_CR(x) = %TIMER_ENABLE | %TIMER_DIV_64; \n
	\n
	Possible bit defines: \n

	\see TIMER_ENABLE
	\see TIMER_IRQ_REQ
	\see TIMER_DIV_1
	\see TIMER_DIV_64
	\see TIMER_DIV_256
	\see TIMER_DIV_1024
*/
#define TIMER_CR(n) (*(vuint16*)(0x04000102+((n)<<2)))

//!	Enables the timer.
#define TIMER_ENABLE    (1<<7)

//!	Causes the timer to request an Interupt on overflow.
#define TIMER_IRQ_REQ   (1<<6)

//!	When set will cause the timer to count when the timer below overflows (unavailable for timer 0).
#define TIMER_CASCADE   (1<<2)

//!	Causes the timer to count at 33.514Mhz.
#define TIMER_DIV_1     (0)
//!	Causes the timer to count at (33.514 / 64) Mhz.
#define TIMER_DIV_64    (1)
//!	Causes the timer to count at (33.514 / 256) Mhz.
#define TIMER_DIV_256   (2)
//!	Causes the timer to count at (33.514 / 1024)Mhz.
#define TIMER_DIV_1024  (3)

//---------------------------------------------------------------------------------
#ifdef __cplusplus
}
#endif
//---------------------------------------------------------------------------------

//---------------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------------