1 |
580 |
jeremybenn |
/* ----------------------------------------------------------------------------
|
2 |
|
|
* ATMEL Microcontroller Software Support
|
3 |
|
|
* ----------------------------------------------------------------------------
|
4 |
|
|
* Copyright (c) 2008, Atmel Corporation
|
5 |
|
|
*
|
6 |
|
|
* All rights reserved.
|
7 |
|
|
*
|
8 |
|
|
* Redistribution and use in source and binary forms, with or without
|
9 |
|
|
* modification, are permitted provided that the following conditions are met:
|
10 |
|
|
*
|
11 |
|
|
* - Redistributions of source code must retain the above copyright notice,
|
12 |
|
|
* this list of conditions and the disclaimer below.
|
13 |
|
|
*
|
14 |
|
|
* Atmel's name may not be used to endorse or promote products derived from
|
15 |
|
|
* this software without specific prior written permission.
|
16 |
|
|
*
|
17 |
|
|
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
|
18 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
19 |
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
|
20 |
|
|
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
|
21 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
23 |
|
|
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
24 |
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
25 |
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
26 |
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27 |
|
|
* ----------------------------------------------------------------------------
|
28 |
|
|
*/
|
29 |
|
|
|
30 |
|
|
//------------------------------------------------------------------------------
|
31 |
|
|
// Headers
|
32 |
|
|
//------------------------------------------------------------------------------
|
33 |
|
|
|
34 |
|
|
#include <board.h>
|
35 |
|
|
|
36 |
|
|
#ifdef AT91C_BASE_TSADC
|
37 |
|
|
|
38 |
|
|
#include <utility/trace.h>
|
39 |
|
|
#include <utility/assert.h>
|
40 |
|
|
|
41 |
|
|
//------------------------------------------------------------------------------
|
42 |
|
|
// Local variables
|
43 |
|
|
//------------------------------------------------------------------------------
|
44 |
|
|
|
45 |
|
|
/// TSADC clock frequency in Hz.
|
46 |
|
|
static unsigned int lAdcclk = 0;
|
47 |
|
|
|
48 |
|
|
//------------------------------------------------------------------------------
|
49 |
|
|
// Global functions
|
50 |
|
|
//------------------------------------------------------------------------------
|
51 |
|
|
|
52 |
|
|
//------------------------------------------------------------------------------
|
53 |
|
|
/// Sets the operating mode of the TSADCC peripheral. The mode value can be
|
54 |
|
|
/// one of the following:
|
55 |
|
|
/// - AT91C_TSADC_TSAMOD_ADC_ONLY_MODE
|
56 |
|
|
/// - AT91C_TSADC_TSAMOD_TS_ONLY_MODE
|
57 |
|
|
/// \param mode Desired mode for the TSADCC.
|
58 |
|
|
//------------------------------------------------------------------------------
|
59 |
|
|
void TSADCC_SetOperatingMode(unsigned int mode)
|
60 |
|
|
{
|
61 |
|
|
SANITY_CHECK( (mode == AT91C_TSADC_TSAMOD_ADC_ONLY_MODE)
|
62 |
|
|
| (mode == AT91C_TSADC_TSAMOD_TS_ONLY_MODE));
|
63 |
|
|
|
64 |
|
|
AT91C_BASE_TSADC->TSADC_MR = (AT91C_BASE_TSADC->TSADC_MR
|
65 |
|
|
& ~AT91C_TSADC_TSAMOD)
|
66 |
|
|
| mode;
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
//------------------------------------------------------------------------------
|
70 |
|
|
/// Enables or disables the low resolution precision on the TSADC.
|
71 |
|
|
/// \param enable If true, low resolution (8 bit) is used; otherwise the TSADC
|
72 |
|
|
/// will use a 10-bit resolution.
|
73 |
|
|
//------------------------------------------------------------------------------
|
74 |
|
|
void TSADCC_SetLowResolution(unsigned char enable)
|
75 |
|
|
{
|
76 |
|
|
if (enable) {
|
77 |
|
|
|
78 |
|
|
AT91C_BASE_TSADC->TSADC_MR |= AT91C_TSADC_LOWRES;
|
79 |
|
|
}
|
80 |
|
|
else {
|
81 |
|
|
|
82 |
|
|
AT91C_BASE_TSADC->TSADC_MR &= ~AT91C_TSADC_LOWRES;
|
83 |
|
|
}
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
//------------------------------------------------------------------------------
|
87 |
|
|
/// Enables or disable SLEEP mode on the TSADC.
|
88 |
|
|
/// \param enable If true, the TSADC is put into sleep mode; in normal mode
|
89 |
|
|
/// otherwise.
|
90 |
|
|
//------------------------------------------------------------------------------
|
91 |
|
|
void TSADCC_SetSleepMode(unsigned char enable)
|
92 |
|
|
{
|
93 |
|
|
if (enable) {
|
94 |
|
|
|
95 |
|
|
AT91C_BASE_TSADC->TSADC_MR |= AT91C_TSADC_SLEEP;
|
96 |
|
|
}
|
97 |
|
|
else {
|
98 |
|
|
|
99 |
|
|
AT91C_BASE_TSADC->TSADC_MR &= ~AT91C_TSADC_SLEEP;
|
100 |
|
|
}
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
//------------------------------------------------------------------------------
|
104 |
|
|
/// Enables or disables pen detection on the TSADC.
|
105 |
|
|
/// \param enable If true, pen detection is enabled; otherwise it is disabled.
|
106 |
|
|
//------------------------------------------------------------------------------
|
107 |
|
|
void TSADCC_SetPenDetect(unsigned char enable)
|
108 |
|
|
{
|
109 |
|
|
if (enable) {
|
110 |
|
|
|
111 |
|
|
AT91C_BASE_TSADC->TSADC_MR |= AT91C_TSADC_PENDET;
|
112 |
|
|
}
|
113 |
|
|
else {
|
114 |
|
|
|
115 |
|
|
AT91C_BASE_TSADC->TSADC_MR &= ~AT91C_TSADC_PENDET;
|
116 |
|
|
}
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
//------------------------------------------------------------------------------
|
120 |
|
|
/// Sets the TSADC clock to the desired frequency. The prescaler is calculated
|
121 |
|
|
/// by this function so the resulting frequency is equal or inferior to the
|
122 |
|
|
/// desired one.
|
123 |
|
|
/// \param adcclk Desired ADC clock frequency in Hz.
|
124 |
|
|
/// \param mck Master clock frequency in Hz.
|
125 |
|
|
//------------------------------------------------------------------------------
|
126 |
|
|
void TSADCC_SetAdcFrequency(unsigned int adcclk, unsigned int mck)
|
127 |
|
|
{
|
128 |
|
|
unsigned int prescal;
|
129 |
|
|
|
130 |
|
|
// Formula for PRESCAL is:
|
131 |
|
|
// PRESCAL = (MCK / (2 * ADCCLK)) + 1
|
132 |
|
|
// First, we do the division, multiplied by 10 to get higher precision
|
133 |
|
|
// If the last digit is not zero, we round up to avoid generating a higher
|
134 |
|
|
// than required frequency.
|
135 |
|
|
prescal = (mck * 5) / adcclk;
|
136 |
|
|
if ((prescal % 10) > 0) {
|
137 |
|
|
|
138 |
|
|
prescal = (prescal / 10);
|
139 |
|
|
}
|
140 |
|
|
else {
|
141 |
|
|
|
142 |
|
|
SANITY_CHECK((prescal / 10) != 0);
|
143 |
|
|
prescal = (prescal / 10) - 1;
|
144 |
|
|
}
|
145 |
|
|
SANITY_CHECK((prescal & ~0x3F) == 0);
|
146 |
|
|
|
147 |
|
|
AT91C_BASE_TSADC->TSADC_MR = ( AT91C_BASE_TSADC->TSADC_MR
|
148 |
|
|
& ~AT91C_TSADC_PRESCAL)
|
149 |
|
|
| (prescal << 8);
|
150 |
|
|
|
151 |
|
|
// Save clock frequency for further timing calculations
|
152 |
|
|
lAdcclk = adcclk;
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
//------------------------------------------------------------------------------
|
156 |
|
|
/// Sets the TSADC startup time. This function relies on the ADCCLK frequency
|
157 |
|
|
/// that has been set using TSADCC_SetAdcFrequency(), so it must have been
|
158 |
|
|
/// called first.
|
159 |
|
|
/// \param time Startup time in µseconds.
|
160 |
|
|
//------------------------------------------------------------------------------
|
161 |
|
|
void TSADCC_SetStartupTime(unsigned int time)
|
162 |
|
|
{
|
163 |
|
|
unsigned int startup;
|
164 |
|
|
|
165 |
|
|
SANITY_CHECK(lAdcclk != 0);
|
166 |
|
|
|
167 |
|
|
// Formula for STARTUP is:
|
168 |
|
|
// STARTUP = (time x ADCCLK) / (1000000 x 8) - 1
|
169 |
|
|
// Division multiplied by 10 for higher precision
|
170 |
|
|
startup = (time * lAdcclk) / (800000);
|
171 |
|
|
if ((startup % 10) > 0) {
|
172 |
|
|
|
173 |
|
|
startup /= 10;
|
174 |
|
|
}
|
175 |
|
|
else {
|
176 |
|
|
|
177 |
|
|
startup /= 10;
|
178 |
|
|
if (startup > 0) {
|
179 |
|
|
|
180 |
|
|
startup--;
|
181 |
|
|
}
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
SANITY_CHECK((startup & ~0x7F) == 0);
|
185 |
|
|
AT91C_BASE_TSADC->TSADC_MR = ( AT91C_BASE_TSADC->TSADC_MR
|
186 |
|
|
& ~AT91C_TSADC_STARTUP)
|
187 |
|
|
| (startup << 16);
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
//------------------------------------------------------------------------------
|
191 |
|
|
/// Sets the TSADC track and hold time. This function relies on the ADCCLK
|
192 |
|
|
/// frequency that has been set with TSADCC_SetAdcFrequency(), to it must be
|
193 |
|
|
/// called first.
|
194 |
|
|
/// This function also sets the track and hold time in the TSADC_TSR register.
|
195 |
|
|
/// \param time Track and hold time in nanoseconds.
|
196 |
|
|
//------------------------------------------------------------------------------
|
197 |
|
|
void TSADCC_SetTrackAndHoldTime(unsigned int time)
|
198 |
|
|
{
|
199 |
|
|
unsigned int shtim;
|
200 |
|
|
|
201 |
|
|
SANITY_CHECK(lAdcclk != 0);
|
202 |
|
|
|
203 |
|
|
// Formula for SHTIM:
|
204 |
|
|
// SHTIM = (time x ADCCLK) / 1000000000 - 1
|
205 |
|
|
// Since 1 billion is close to the maximum value for an integer, we first
|
206 |
|
|
// divide ADCCLK by 1000 to avoid an overflow
|
207 |
|
|
shtim = (time * (lAdcclk / 1000)) / 100000;
|
208 |
|
|
if ((shtim % 10) > 0) {
|
209 |
|
|
|
210 |
|
|
shtim /= 10;
|
211 |
|
|
}
|
212 |
|
|
else {
|
213 |
|
|
|
214 |
|
|
shtim /= 10;
|
215 |
|
|
if (shtim > 0) shtim--;
|
216 |
|
|
}
|
217 |
|
|
|
218 |
|
|
SANITY_CHECK((shtim & ~0xF) == 0);
|
219 |
|
|
AT91C_BASE_TSADC->TSADC_MR = ( AT91C_BASE_TSADC->TSADC_MR
|
220 |
|
|
& ~AT91C_TSADC_SHTIM)
|
221 |
|
|
| (shtim << 24);
|
222 |
|
|
AT91C_BASE_TSADC->TSADC_TSR = shtim << 24;
|
223 |
|
|
}
|
224 |
|
|
|
225 |
|
|
//------------------------------------------------------------------------------
|
226 |
|
|
/// Sets the TSADC debounce time. This function relies on the ADCCLK
|
227 |
|
|
/// frequency that has been set with TSADCC_SetAdcFrequency(), to it must be
|
228 |
|
|
/// called first.
|
229 |
|
|
/// \param time Debounce time in nanoseconds (cannot be 0).
|
230 |
|
|
//------------------------------------------------------------------------------
|
231 |
|
|
void TSADCC_SetDebounceTime(unsigned int time)
|
232 |
|
|
{
|
233 |
|
|
unsigned int divisor = 1000000000;
|
234 |
|
|
unsigned int clock = lAdcclk;
|
235 |
|
|
unsigned int pendbc = 0;
|
236 |
|
|
unsigned int targetValue;
|
237 |
|
|
unsigned int currentValue;
|
238 |
|
|
|
239 |
|
|
SANITY_CHECK(lAdcclk != 0);
|
240 |
|
|
SANITY_CHECK(time != 0);
|
241 |
|
|
|
242 |
|
|
// Divide time & ADCCLK first to avoid overflows
|
243 |
|
|
while ((divisor > 1) && ((time % 10) == 0)) {
|
244 |
|
|
|
245 |
|
|
time /= 10;
|
246 |
|
|
divisor /= 10;
|
247 |
|
|
}
|
248 |
|
|
while ((divisor > 1) && ((clock % 10) == 0)) {
|
249 |
|
|
|
250 |
|
|
clock /= 10;
|
251 |
|
|
divisor /= 10;
|
252 |
|
|
}
|
253 |
|
|
|
254 |
|
|
// Compute PENDBC value
|
255 |
|
|
targetValue = time * clock / divisor;
|
256 |
|
|
currentValue = 1;
|
257 |
|
|
while (currentValue < targetValue) {
|
258 |
|
|
|
259 |
|
|
pendbc++;
|
260 |
|
|
currentValue *= 2;
|
261 |
|
|
}
|
262 |
|
|
|
263 |
|
|
SANITY_CHECK((pendbc & ~0xF) == 0);
|
264 |
|
|
AT91C_BASE_TSADC->TSADC_MR = ( AT91C_BASE_TSADC->TSADC_MR
|
265 |
|
|
& ~AT91C_TSADC_PENDBC)
|
266 |
|
|
| (pendbc << 28);
|
267 |
|
|
}
|
268 |
|
|
|
269 |
|
|
//------------------------------------------------------------------------------
|
270 |
|
|
/// Sets the trigger mode of the TSADCC to one of the following values:
|
271 |
|
|
/// - AT91C_TSADC_TRGMOD_NO_TRIGGER
|
272 |
|
|
/// - AT91C_TSADC_TRGMOD_EXTERNAL_TRIGGER_RE
|
273 |
|
|
/// - AT91C_TSADC_TRGMOD_EXTERNAL_TRIGGER_FE
|
274 |
|
|
/// - AT91C_TSADC_TRGMOD_EXTERNAL_TRIGGER_AE
|
275 |
|
|
/// - AT91C_TSADC_TRGMOD_PENDET_TRIGGER
|
276 |
|
|
/// - AT91C_TSADC_TRGMOD_PERIODIC_TRIGGER
|
277 |
|
|
/// - AT91C_TSADC_TRGMOD_CONT_TRIGGER
|
278 |
|
|
/// \param mode Trigger mode.
|
279 |
|
|
//------------------------------------------------------------------------------
|
280 |
|
|
void TSADCC_SetTriggerMode(unsigned int mode)
|
281 |
|
|
{
|
282 |
|
|
SANITY_CHECK(((mode & ~AT91C_TSADC_TRGMOD) == 0)
|
283 |
|
|
| ((mode & AT91C_TSADC_TRGMOD) != 0x7));
|
284 |
|
|
|
285 |
|
|
AT91C_BASE_TSADC->TSADC_TRGR = (AT91C_BASE_TSADC->TSADC_TRGR
|
286 |
|
|
& ~AT91C_TSADC_TRGMOD)
|
287 |
|
|
| mode;
|
288 |
|
|
}
|
289 |
|
|
|
290 |
|
|
//------------------------------------------------------------------------------
|
291 |
|
|
/// Sets the trigger period when using the TSADCC in periodic trigger mode.
|
292 |
|
|
/// As usual, this function requires TSADCC_SetAdcFrequency() to be called
|
293 |
|
|
/// before it.
|
294 |
|
|
/// \param period Trigger period in nanoseconds.
|
295 |
|
|
//------------------------------------------------------------------------------
|
296 |
|
|
void TSADCC_SetTriggerPeriod(unsigned int period)
|
297 |
|
|
{
|
298 |
|
|
unsigned int trgper;
|
299 |
|
|
unsigned int divisor = 100000000;
|
300 |
|
|
|
301 |
|
|
while ((period >= 10) && (divisor >= 10)) {
|
302 |
|
|
|
303 |
|
|
period /= 10;
|
304 |
|
|
divisor /= 10;
|
305 |
|
|
}
|
306 |
|
|
|
307 |
|
|
trgper = (period * lAdcclk) / divisor;
|
308 |
|
|
if ((trgper % 10) > 0) {
|
309 |
|
|
|
310 |
|
|
trgper /= 10;
|
311 |
|
|
}
|
312 |
|
|
else {
|
313 |
|
|
|
314 |
|
|
trgper /= 10;
|
315 |
|
|
if (trgper > 0) trgper--;
|
316 |
|
|
}
|
317 |
|
|
|
318 |
|
|
SANITY_CHECK((trgper & ~0xFFFF) == 0);
|
319 |
|
|
AT91C_BASE_TSADC->TSADC_TRGR = (AT91C_BASE_TSADC->TSADC_TRGR
|
320 |
|
|
& ~AT91C_TSADC_TRGPER)
|
321 |
|
|
| (trgper << 16);
|
322 |
|
|
}
|
323 |
|
|
|
324 |
|
|
#endif //#ifdef AT91C_BASE_TSADC
|