summaryrefslogtreecommitdiff
path: root/peripherals/tsadcc/tsadcc.c
blob: 430ef8a09449c47d769c0a7ca859c6ff04fbdbb2 (plain)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* ----------------------------------------------------------------------------
 *         ATMEL Microcontroller Software Support 
 * ----------------------------------------------------------------------------
 * Copyright (c) 2008, Atmel Corporation
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Atmel's name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ----------------------------------------------------------------------------
 */

//------------------------------------------------------------------------------
//         Headers
//------------------------------------------------------------------------------

#include <board.h>

#ifdef AT91C_BASE_TSADC

#include <utility/trace.h>
#include <utility/assert.h>

//------------------------------------------------------------------------------
//         Local variables
//------------------------------------------------------------------------------

/// TSADC clock frequency in Hz.
static unsigned int lAdcclk = 0;

//------------------------------------------------------------------------------
//         Global functions
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
/// Sets the operating mode of the TSADCC peripheral. The mode value can be
/// one of the following:
/// - AT91C_TSADC_TSAMOD_ADC_ONLY_MODE
/// - AT91C_TSADC_TSAMOD_TS_ONLY_MODE
/// \param mode  Desired mode for the TSADCC.
//------------------------------------------------------------------------------
void TSADCC_SetOperatingMode(unsigned int mode)
{
    SANITY_CHECK(  (mode == AT91C_TSADC_TSAMOD_ADC_ONLY_MODE)
                 | (mode == AT91C_TSADC_TSAMOD_TS_ONLY_MODE));
                 
    AT91C_BASE_TSADC->TSADC_MR = (AT91C_BASE_TSADC->TSADC_MR
                                  & ~AT91C_TSADC_TSAMOD)
                                 | mode;
}

//------------------------------------------------------------------------------
/// Enables or disables the low resolution precision on the TSADC.
/// \param enable  If true, low resolution (8 bit) is used; otherwise the TSADC
///                will use a 10-bit resolution.
//------------------------------------------------------------------------------
void TSADCC_SetLowResolution(unsigned char enable)
{
    if (enable) {
    
        AT91C_BASE_TSADC->TSADC_MR |= AT91C_TSADC_LOWRES;
    }
    else {
    
        AT91C_BASE_TSADC->TSADC_MR &= ~AT91C_TSADC_LOWRES;
    }
}

//------------------------------------------------------------------------------
/// Enables or disable SLEEP mode on the TSADC.
/// \param enable  If true, the TSADC is put into sleep mode; in normal mode
///                otherwise.
//------------------------------------------------------------------------------
void TSADCC_SetSleepMode(unsigned char enable)
{
    if (enable) {
    
        AT91C_BASE_TSADC->TSADC_MR |= AT91C_TSADC_SLEEP;
    }
    else {
    
        AT91C_BASE_TSADC->TSADC_MR &= ~AT91C_TSADC_SLEEP;
    }
}

//------------------------------------------------------------------------------
/// Enables or disables pen detection on the TSADC.
/// \param enable  If true, pen detection is enabled; otherwise it is disabled.
//------------------------------------------------------------------------------
void TSADCC_SetPenDetect(unsigned char enable)
{
    if (enable) {
    
        AT91C_BASE_TSADC->TSADC_MR |= AT91C_TSADC_PENDET;
    }
    else {
        
        AT91C_BASE_TSADC->TSADC_MR &= ~AT91C_TSADC_PENDET;
    }
}

//------------------------------------------------------------------------------
/// Sets the TSADC clock to the desired frequency. The prescaler is calculated
/// by this function so the resulting frequency is equal or inferior to the
/// desired one.
/// \param adcclk  Desired ADC clock frequency in Hz.
/// \param mck  Master clock frequency in Hz.
//------------------------------------------------------------------------------
void TSADCC_SetAdcFrequency(unsigned int adcclk, unsigned int mck)
{
    unsigned int prescal;
    
    // Formula for PRESCAL is:
    // PRESCAL = (MCK / (2 * ADCCLK)) + 1
    // First, we do the division, multiplied by 10 to get higher precision
    // If the last digit is not zero, we round up to avoid generating a higher
    // than required frequency.
    prescal = (mck * 5) / adcclk;
    if ((prescal % 10) > 0) {
    
        prescal = (prescal / 10);
    }
    else {
    
        SANITY_CHECK((prescal / 10) != 0);
        prescal = (prescal / 10) - 1;
    }
    SANITY_CHECK(((prescal << 8) & ~AT91C_TSADC_PRESCAL) == 0);
    
    AT91C_BASE_TSADC->TSADC_MR = (  AT91C_BASE_TSADC->TSADC_MR
                                  & ~AT91C_TSADC_PRESCAL)
                                 | (prescal << 8);
                                 
    // Save clock frequency for further timing calculations
    lAdcclk = adcclk;
}

//------------------------------------------------------------------------------
/// Sets the TSADC startup time. This function relies on the ADCCLK frequency
/// that has been set using TSADCC_SetAdcFrequency(), so it must have been
/// called first.
/// \param time  Startup time in µseconds.
//------------------------------------------------------------------------------
void TSADCC_SetStartupTime(unsigned int time)
{
    unsigned int startup;
    
    SANITY_CHECK(lAdcclk != 0);
        
    // Formula for STARTUP is:
    // STARTUP = (time x ADCCLK) / (1000000 x 8) - 1
    // Division multiplied by 10 for higher precision
    startup = (time * lAdcclk) / (800000);
    if ((startup % 10) > 0) {
    
        startup /= 10;
    }
    else {
    
        startup /= 10;
        if (startup > 0) {
        
            startup--;
        }
    }
    
    SANITY_CHECK((startup & ~0x7F) == 0);
    AT91C_BASE_TSADC->TSADC_MR = (  AT91C_BASE_TSADC->TSADC_MR
                                  & ~AT91C_TSADC_STARTUP)
                                 | (startup << 16);
}

//------------------------------------------------------------------------------
/// Sets the TSADC track and hold time. This function relies on the ADCCLK
/// frequency that has been set with TSADCC_SetAdcFrequency(), to it must be
/// called first.
/// This function also sets the track and hold time in the TSADC_TSR register.
/// \param time  Track and hold time in nanoseconds.
//------------------------------------------------------------------------------
void TSADCC_SetTrackAndHoldTime(unsigned int time)
{
    unsigned int shtim;
    
    SANITY_CHECK(lAdcclk != 0);
    
    // Formula for SHTIM:
    // SHTIM = (time x ADCCLK) / 1000000000 - 1
    // Since 1 billion is close to the maximum value for an integer, we first
    // divide ADCCLK by 1000 to avoid an overflow
    shtim = (time * (lAdcclk / 1000)) / 100000;
    if ((shtim % 10) > 0) {
    
        shtim /= 10;
    }
    else {
    
        shtim /= 10;
        if (shtim > 0) shtim--;
    }
    
    SANITY_CHECK((shtim & ~0xF) == 0);
    AT91C_BASE_TSADC->TSADC_MR = (  AT91C_BASE_TSADC->TSADC_MR
                                  & ~AT91C_TSADC_SHTIM)
                                 | (shtim << 24);
    AT91C_BASE_TSADC->TSADC_TSR = shtim << 24;
}

//------------------------------------------------------------------------------
/// Sets the TSADC debounce time. This function relies on the ADCCLK
/// frequency that has been set with TSADCC_SetAdcFrequency(), to it must be
/// called first.
/// \param time  Debounce time in nanoseconds (cannot be 0).
//------------------------------------------------------------------------------
void TSADCC_SetDebounceTime(unsigned int time)
{
    unsigned int divisor = 1000000000;
    unsigned int clock = lAdcclk;
    unsigned int pendbc = 0;
    unsigned int targetValue;
    unsigned int currentValue;
    
    SANITY_CHECK(lAdcclk != 0);
    SANITY_CHECK(time != 0);
    
    // Divide time & ADCCLK first to avoid overflows
    while ((divisor > 1) && ((time % 10) == 0)) {
    
        time /= 10;
        divisor /= 10;
    }
    while ((divisor > 1) && ((clock % 10) == 0)) {
    
        clock /= 10;
        divisor /= 10;
    }
    
    // Compute PENDBC value
    targetValue = time * clock / divisor;
    currentValue = 1;
    while (currentValue < targetValue) {
    
        pendbc++;
        currentValue *= 2;
    }
    
    SANITY_CHECK((pendbc & ~0xF) == 0);
    AT91C_BASE_TSADC->TSADC_MR = (  AT91C_BASE_TSADC->TSADC_MR
                                  & ~AT91C_TSADC_PENDBC)
                                 | (pendbc << 28);
}

//------------------------------------------------------------------------------
/// Sets the trigger mode of the TSADCC to one of the following values:
/// - AT91C_TSADC_TRGMOD_NO_TRIGGER
/// - AT91C_TSADC_TRGMOD_EXTERNAL_TRIGGER_RE
/// - AT91C_TSADC_TRGMOD_EXTERNAL_TRIGGER_FE
/// - AT91C_TSADC_TRGMOD_EXTERNAL_TRIGGER_AE
/// - AT91C_TSADC_TRGMOD_PENDET_TRIGGER
/// - AT91C_TSADC_TRGMOD_PERIODIC_TRIGGER
/// - AT91C_TSADC_TRGMOD_CONT_TRIGGER
/// \param mode  Trigger mode.
//------------------------------------------------------------------------------
void TSADCC_SetTriggerMode(unsigned int mode)
{
    SANITY_CHECK(((mode & ~AT91C_TSADC_TRGMOD) == 0)
                 | ((mode & AT91C_TSADC_TRGMOD) != 0x7));
    
    AT91C_BASE_TSADC->TSADC_TRGR = (AT91C_BASE_TSADC->TSADC_TRGR
                                    & ~AT91C_TSADC_TRGMOD)
                                   | mode;
}

//------------------------------------------------------------------------------
/// Sets the trigger period when using the TSADCC in periodic trigger mode.
/// As usual, this function requires TSADCC_SetAdcFrequency() to be called
/// before it.
/// \param period  Trigger period in nanoseconds.
//------------------------------------------------------------------------------
void TSADCC_SetTriggerPeriod(unsigned int period)
{
    unsigned int trgper;
    unsigned int divisor = 100000000;
    
    while ((period >= 10) && (divisor >= 10)) {
    
        period /= 10;
        divisor /= 10;
    }
    
    trgper = (period * lAdcclk) / divisor;
    if ((trgper % 10) > 0) {
    
        trgper /= 10;
    }
    else {
    
        trgper /= 10;
        if (trgper > 0) trgper--;
    }
    
    SANITY_CHECK((trgper & ~0xFFFF) == 0);
    AT91C_BASE_TSADC->TSADC_TRGR = (AT91C_BASE_TSADC->TSADC_TRGR
                                    & ~AT91C_TSADC_TRGPER)
                                   | (trgper << 16);
}

#endif //#ifdef AT91C_BASE_TSADC
personal git repositories of Harald Welte. Your mileage may vary