diff options
Diffstat (limited to 'firmware/target/arm/s3c2440/adc-s3c2440.c')
-rw-r--r-- | firmware/target/arm/s3c2440/adc-s3c2440.c | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/firmware/target/arm/s3c2440/adc-s3c2440.c b/firmware/target/arm/s3c2440/adc-s3c2440.c new file mode 100644 index 0000000000..fd5151a3bf --- /dev/null +++ b/firmware/target/arm/s3c2440/adc-s3c2440.c | |||
@@ -0,0 +1,140 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2006 by Wade Brown | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | #include "cpu.h" | ||
22 | #include "system.h" | ||
23 | #include "adc.h" | ||
24 | #include "adc-target.h" | ||
25 | #include "kernel.h" | ||
26 | |||
27 | static unsigned short adc_readings[NUM_ADC_CHANNELS]; | ||
28 | |||
29 | /* prototypes */ | ||
30 | static unsigned short __adc_read(int channel); | ||
31 | static void adc_tick(void); | ||
32 | |||
33 | void adc_init(void) | ||
34 | { | ||
35 | int i; | ||
36 | |||
37 | /* Turn on the ADC PCLK */ | ||
38 | s3c_regset32(&CLKCON, 1<<15); | ||
39 | |||
40 | /* Set channel 0, normal mode, disable "start by read" */ | ||
41 | ADCCON &= ~(0x3F); | ||
42 | |||
43 | /* No start delay. Use normal conversion mode. */ | ||
44 | ADCDLY = 0x1; | ||
45 | |||
46 | /* Set and enable the prescaler */ | ||
47 | ADCCON = (ADCCON & ~(0xff<<6)) | (0x19<<6); | ||
48 | ADCCON |= (1<<14); | ||
49 | |||
50 | /* prefill the adc channels */ | ||
51 | for (i = 0; i < NUM_ADC_CHANNELS; i++) | ||
52 | { | ||
53 | adc_readings[i] = __adc_read(i); | ||
54 | } | ||
55 | |||
56 | /* start at zero so when the tick starts it is at zero */ | ||
57 | adc_readings[0] = __adc_read(0); | ||
58 | |||
59 | /* attach the adc reading to the tick */ | ||
60 | tick_add_task(adc_tick); | ||
61 | } | ||
62 | |||
63 | /* Called to get the recent ADC reading */ | ||
64 | inline unsigned short adc_read(int channel) | ||
65 | { | ||
66 | return adc_readings[channel]; | ||
67 | } | ||
68 | |||
69 | /** | ||
70 | * Read the ADC by polling | ||
71 | * @param channel The ADC channel to read | ||
72 | * @return 10bit reading from ADC channel or ADC_READ_ERROR if timeout | ||
73 | */ | ||
74 | static unsigned short __adc_read(int channel) | ||
75 | { | ||
76 | int i; | ||
77 | |||
78 | /* Set the channel */ | ||
79 | ADCCON = (ADCCON & ~(0x7<<3)) | (channel<<3); | ||
80 | |||
81 | /* Start the conversion process */ | ||
82 | ADCCON |= 0x1; | ||
83 | |||
84 | /* Wait for a low Enable_start */ | ||
85 | for (i = 20000;;) | ||
86 | { | ||
87 | if(0 == (ADCCON & 0x1)) | ||
88 | { | ||
89 | break; | ||
90 | } | ||
91 | else | ||
92 | { | ||
93 | i--; | ||
94 | if (0 == i) | ||
95 | { | ||
96 | /* Ran out of time */ | ||
97 | return ADC_READ_ERROR; | ||
98 | } | ||
99 | } | ||
100 | } | ||
101 | |||
102 | /* Wait for high End_of_Conversion */ | ||
103 | for(i = 20000;;) | ||
104 | { | ||
105 | if(ADCCON & (1<<15)) | ||
106 | { | ||
107 | break; | ||
108 | } | ||
109 | else | ||
110 | { | ||
111 | i--; | ||
112 | if(0 == i) | ||
113 | { | ||
114 | /* Ran out of time */ | ||
115 | return ADC_READ_ERROR; | ||
116 | } | ||
117 | } | ||
118 | } | ||
119 | return (ADCDAT0 & 0x3ff); | ||
120 | } | ||
121 | |||
122 | /* add this to the tick so that the ADC converts are done in the background */ | ||
123 | static void adc_tick(void) | ||
124 | { | ||
125 | static unsigned channel; | ||
126 | |||
127 | /* Check if the End Of Conversion is set */ | ||
128 | if (ADCCON & (1<<15)) | ||
129 | { | ||
130 | adc_readings[channel] = (ADCDAT0 & 0x3FF); | ||
131 | if (++channel >= NUM_ADC_CHANNELS) | ||
132 | { | ||
133 | channel = 0; | ||
134 | } | ||
135 | |||
136 | /* setup the next conversion and start it*/ | ||
137 | ADCCON = (ADCCON & ~(0x7<<3)) | (channel<<3) | 0x01; | ||
138 | } | ||
139 | } | ||
140 | |||