OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [CORTEX_AT91SAM3U256_IAR/] [AT91Lib/] [peripherals/] [adc/] [adc.c] - Blame information for rev 595

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
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
#include <adc/adc.h>
36
#include <utility/trace.h>
37
#include <utility/assert.h>
38
 
39
//-----------------------------------------------------------------------------
40
/// Configure the Mode Register of the ADC controller
41
/// \param pAdc Pointer to an AT91S_ADC instance.
42
/// \param mode value to write in mode register
43
//-----------------------------------------------------------------------------
44
 
45
//replaced with macro definition in adc.h
46
 
47
//void ADC_CfgModeReg(AT91S_ADC *pAdc, unsigned int mode, unsigned int extmode)
48
//{
49
//    ASSERT((mode&0xF00000C0)== 0, "ADC Bad configuration ADC MR");
50
//
51
//    // Write to the MR register
52
//    pAdc->ADC_MR = mode;
53
//}
54
 
55
//------------------------------------------------------------------------------
56
//         Global Functions
57
//------------------------------------------------------------------------------
58
 
59
//-----------------------------------------------------------------------------
60
/// Initialize the ADC controller
61
/// \param pAdc Pointer to an AT91S_ADC instance.
62
/// \param trgEn trigger mode, software or Hardware
63
/// \param trgSel hardware trigger selection
64
/// \param sleepMode sleep mode selection
65
/// \param resolution resolution selection 8 bits or 10 bits
66
/// \param mckClock value of MCK in Hz
67
/// \param adcClock value of the ADC clock in Hz
68
/// \param startupTime value of the start up time (in µs) (see datasheet)
69
/// \param sampleAndHoldTime (in ns)
70
//-----------------------------------------------------------------------------
71
void ADC_Initialize (AT91S_ADC *pAdc,
72
                     unsigned char idAdc,
73
                     unsigned char trgEn,
74
                     unsigned char trgSel,
75
                     unsigned char sleepMode,
76
                     unsigned char resolution,
77
                     unsigned int mckClock,
78
                     unsigned int adcClock,
79
                     unsigned int startupTime,
80
                     unsigned int sampleAndHoldTime)
81
{
82
    unsigned int prescal;
83
    unsigned int startup;
84
    unsigned int shtim;
85
 
86
    ASSERT(startupTime<=ADC_STARTUP_TIME_MAX, "ADC Bad startupTime\n\r");
87
    ASSERT(sampleAndHoldTime>=ADC_TRACK_HOLD_TIME_MIN, "ADC Bad sampleAndHoldTime\n\r");
88
 
89
    // Example:
90
    // 5 MHz operation, 20µs startup time, 600ns track and hold time
91
    // PRESCAL: Prescaler Rate Selection ADCClock = MCK / ( (PRESCAL+1) * 2 )
92
    // PRESCAL = [MCK / (ADCClock * 2)] -1 = [48/(5*2)]-1 = 3,8
93
    // PRESCAL =  4 -> 48/((4+1)*2) = 48/10 = 4.8MHz
94
    // 48/((3+1)*2) = 48/8 = 6MHz
95
    // Startup Time = (STARTUP+1) * 8 / ADCClock
96
    // STARTUP = [(Startup Time * ADCClock)/8]-1 = [(20 10e-6 * 5000000)/8]-1 = 11,5
97
    // STARTUP = 11 -> (11+1)*8/48000000 = 96/4800000 = 20µs
98
    //
99
    // Sample & Hold Time = (SHTIM+1) / ADCClock
100
    // SHTIM = (HoldTime * ADCClock)-1 = (600 10e-9 * 5000000)-1 = 2
101
    // SHTIM   =  2 -> (2+1)/4800000 = 1/1200000 = 833ns
102
    prescal = (mckClock / (2*adcClock)) - 1;
103
    startup = ((adcClock/1000000) * startupTime / 8) - 1;
104
    shtim = (((adcClock/1000000) * sampleAndHoldTime)/1000) - 1;
105
 
106
    ASSERT( (prescal<0x3F), "ADC Bad PRESCAL\n\r");
107
    ASSERT(startup<0x7F, "ADC Bad STARTUP\n\r");
108
    ASSERT(shtim<0xF, "ADC Bad SampleAndHoldTime\n\r");
109
 
110
    TRACE_DEBUG("adcClock:%d MasterClock:%d\n\r", (mckClock/((prescal+1)*2)), mckClock);
111
    TRACE_DEBUG("prescal:0x%X startup:0x%X shtim:0x%X\n\r", prescal, startup, shtim);
112
 
113
    if( adcClock != (mckClock/((prescal+1)*2)) ) {
114
        TRACE_WARNING("User and calculated adcClocks are different : user=%d calc=%d\n\r",
115
            adcClock, (mckClock/((prescal+1)*2)));
116
    }
117
 
118
    // Enable peripheral clock    
119
    AT91C_BASE_PMC->PMC_PCER = 1 << idAdc;
120
 
121
    // Reset the controller
122
    ADC_SoftReset(pAdc);
123
 
124
    // Write to the MR register
125
    ADC_CfgModeReg( pAdc,
126
                    ( trgEn & AT91C_ADC_TRGEN)
127
                  | ( trgSel & AT91C_ADC_TRGSEL)
128
                  | ( resolution & AT91C_ADC_LOWRES)
129
                  | ( sleepMode & AT91C_ADC_SLEEP)
130
                  | ( (prescal<<8) & AT91C_ADC_PRESCAL)
131
                  | ( (startup<<16) & AT91C_ADC_STARTUP)
132
                  | ( (shtim<<24) & AT91C_ADC_SHTIM) );
133
}
134
 
135
//-----------------------------------------------------------------------------
136
/// Return the Mode Register of the ADC controller value
137
/// \param pAdc Pointer to an AT91S_ADC instance.
138
/// \return ADC Mode register
139
//-----------------------------------------------------------------------------
140
 
141
//replaced with macro definition in adc.h
142
 
143
//unsigned int ADC_GetModeReg(AT91S_ADC *pAdc)
144
//{
145
//    return pAdc->ADC_MR;    
146
//}
147
 
148
//-----------------------------------------------------------------------------
149
/// Enable Channel
150
/// \param pAdc Pointer to an AT91S_ADC instance.
151
/// \param channel channel to enable
152
//-----------------------------------------------------------------------------
153
//void ADC_EnableChannel(AT91S_ADC *pAdc, unsigned int channel)
154
//{
155
//    ASSERT(channel < 8, "ADC Channel not exist");
156
//
157
//    // Write to the CHER register
158
//    pAdc->ADC_CHER = (1 << channel);
159
//}
160
 
161
//-----------------------------------------------------------------------------
162
/// Disable Channel
163
/// \param pAdc Pointer to an AT91S_ADC instance.
164
/// \param channel channel to disable
165
//-----------------------------------------------------------------------------
166
//void ADC_DisableChannel (AT91S_ADC *pAdc, unsigned int channel)
167
//{
168
//    ASSERT(channel < 8, "ADC Channel not exist");
169
//
170
//    // Write to the CHDR register
171
//    pAdc->ADC_CHDR = (1 << channel);
172
//}
173
 
174
//-----------------------------------------------------------------------------
175
/// Return chanel status
176
/// \param pAdc Pointer to an AT91S_ADC instance.
177
/// \return ADC Channel Status Register
178
//-----------------------------------------------------------------------------
179
//unsigned int ADC_GetChannelStatus(AT91S_ADC *pAdc)
180
//{
181
//    return pAdc->ADC_CHSR;    
182
//}
183
 
184
//-----------------------------------------------------------------------------
185
/// Software request for a analog to digital conversion 
186
/// \param pAdc Pointer to an AT91S_ADC instance.
187
//-----------------------------------------------------------------------------
188
//void ADC_StartConversion(AT91S_ADC *pAdc)
189
//{
190
//    pAdc->ADC_CR = AT91C_ADC_START;    
191
//}
192
 
193
//-----------------------------------------------------------------------------
194
/// Software reset
195
/// \param pAdc Pointer to an AT91S_ADC instance.
196
//-----------------------------------------------------------------------------
197
//void ADC_SoftReset(AT91S_ADC *pAdc)
198
//{
199
//    pAdc->ADC_CR = AT91C_ADC_SWRST;    
200
//}
201
 
202
//-----------------------------------------------------------------------------
203
/// Return the Last Converted Data
204
/// \param pAdc Pointer to an AT91S_ADC instance.
205
/// \return Last Converted Data
206
//-----------------------------------------------------------------------------
207
//unsigned int ADC_GetLastConvertedData(AT91S_ADC *pAdc)
208
//{
209
//    return pAdc->ADC_LCDR;    
210
//}
211
 
212
//-----------------------------------------------------------------------------
213
/// Return the Channel Converted Data
214
/// \param pAdc Pointer to an AT91S_ADC instance.
215
/// \param channel channel to get converted value
216
/// \return Channel converted data of the specified channel
217
//-----------------------------------------------------------------------------
218
unsigned int ADC_GetConvertedData(AT91S_ADC *pAdc, unsigned int channel)
219
{
220
    unsigned int data=0;
221
 
222
    ASSERT(channel < 8, "ADC channel not exist");
223
 
224
    switch(channel) {
225
        case 0: data = pAdc->ADC_CDR0; break;
226
        case 1: data = pAdc->ADC_CDR1; break;
227
        case 2: data = pAdc->ADC_CDR2; break;
228
        case 3: data = pAdc->ADC_CDR3; break;
229
        #ifdef AT91C_ADC_CDR4
230
        case 4: data = pAdc->ADC_CDR4; break;
231
        #endif
232
        #ifdef AT91C_ADC_CDR5
233
        case 5: data = pAdc->ADC_CDR5; break;
234
        #endif
235
        #ifdef AT91C_ADC_CDR6
236
        case 6: data = pAdc->ADC_CDR6; break;
237
        #endif
238
        #ifdef AT91C_ADC_CDR7
239
        case 7: data = pAdc->ADC_CDR7; break;
240
        #endif
241
    }
242
    return data;
243
}
244
 
245
//-----------------------------------------------------------------------------
246
/// Enable ADC interrupt
247
/// \param pAdc Pointer to an AT91S_ADC instance.
248
/// \param flag IT to be enabled
249
//-----------------------------------------------------------------------------
250
//void ADC_EnableIt(AT91S_ADC *pAdc, unsigned int flag)
251
//{
252
//    ASSERT((flag&0xFFF00000)== 0, "ADC bad interrupt IER");
253
//
254
//    // Write to the IER register
255
//    pAdc->ADC_IER = flag;
256
//}
257
 
258
//-----------------------------------------------------------------------------
259
/// Enable ADC Data Ready interrupt
260
/// \param pAdc Pointer to an AT91S_ADC instance.
261
//-----------------------------------------------------------------------------
262
 
263
//void ADC_EnableDataReadyIt(AT91S_ADC *pAdc)
264
//{
265
//    pAdc->ADC_IER = AT91C_ADC_DRDY;
266
//}
267
 
268
//-----------------------------------------------------------------------------
269
/// Disable ADC interrupt
270
/// \param pAdc Pointer to an AT91S_ADC instance.
271
/// \param flag IT to be disabled
272
//-----------------------------------------------------------------------------
273
//void ADC_DisableIt(AT91S_ADC *pAdc, unsigned int flag)
274
//{
275
//    ASSERT((flag&0xFFF00000)== 0, "ADC bad interrupt IDR");
276
//
277
//    // Write to the IDR register
278
//    pAdc->ADC_IDR = flag;
279
//}
280
 
281
//-----------------------------------------------------------------------------
282
/// Return ADC Interrupt Status
283
/// \param pAdc Pointer to an AT91S_ADC instance.
284
/// \return ADC Stats register
285
//-----------------------------------------------------------------------------
286
//unsigned int ADC_GetStatus(AT91S_ADC *pAdc)
287
//{
288
//    return pAdc->ADC_SR;
289
//}
290
 
291
//-----------------------------------------------------------------------------
292
/// Return ADC Interrupt Mask Status
293
/// \param pAdc Pointer to an AT91S_ADC instance.
294
/// \return ADC Interrupt Mask Register
295
//-----------------------------------------------------------------------------
296
//unsigned int ADC_GetInterruptMaskStatus(AT91S_ADC *pAdc)
297
//{
298
//    return pAdc->ADC_IMR;
299
//}
300
 
301
//-----------------------------------------------------------------------------
302
/// Test if ADC Interrupt is Masked 
303
/// \param pAdc Pointer to an AT91S_ADC instance.
304
/// \param flag flag to be tested
305
/// \return 1 if interrupt is masked, otherwise 0
306
//-----------------------------------------------------------------------------
307
unsigned int ADC_IsInterruptMasked(AT91S_ADC *pAdc, unsigned int flag)
308
{
309
    return (ADC_GetInterruptMaskStatus(pAdc) & flag);
310
}
311
 
312
//-----------------------------------------------------------------------------
313
/// Test if ADC Status is Set
314
/// \param pAdc Pointer to an AT91S_ADC instance.
315
/// \param flag flag to be tested
316
/// \return 1 if the staus is set; 0 otherwise
317
//-----------------------------------------------------------------------------
318
unsigned int ADC_IsStatusSet(AT91S_ADC *pAdc, unsigned int flag)
319
{
320
    return (ADC_GetStatus(pAdc) & flag);
321
}
322
 
323
 
324
//-----------------------------------------------------------------------------
325
/// Test if ADC channel interrupt Status is Set
326
/// \param adc_sr Value of SR register
327
/// \param channel Channel to be tested
328
/// \return 1 if interrupt status is set, otherwise 0
329
//-----------------------------------------------------------------------------
330
unsigned char ADC_IsChannelInterruptStatusSet(unsigned int adc_sr,
331
                                              unsigned int channel)
332
{
333
    unsigned char status;
334
 
335
    if((adc_sr & (1<<channel)) == (1<<channel)) {
336
        status = 1;
337
    }
338
    else {
339
        status = 0;
340
    }
341
    return status;
342
}
343
 
344
 
345
 
346
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.