diff options
-rw-r--r-- | firmware/drivers/adc.c | 89 |
1 files changed, 88 insertions, 1 deletions
diff --git a/firmware/drivers/adc.c b/firmware/drivers/adc.c index 1865e8c582..4b11c7dbab 100644 --- a/firmware/drivers/adc.c +++ b/firmware/drivers/adc.c | |||
@@ -17,11 +17,12 @@ | |||
17 | * | 17 | * |
18 | ****************************************************************************/ | 18 | ****************************************************************************/ |
19 | #include "config.h" | 19 | #include "config.h" |
20 | #include "sh7034.h" | 20 | #include "cpu.h" |
21 | #include "kernel.h" | 21 | #include "kernel.h" |
22 | #include "thread.h" | 22 | #include "thread.h" |
23 | #include "adc.h" | 23 | #include "adc.h" |
24 | 24 | ||
25 | #if CONFIG_CPU == SH7034 | ||
25 | /************************************************************************** | 26 | /************************************************************************** |
26 | ** The A/D conversion is done every tick, in three steps: | 27 | ** The A/D conversion is done every tick, in three steps: |
27 | ** | 28 | ** |
@@ -104,3 +105,89 @@ void adc_init(void) | |||
104 | 105 | ||
105 | sleep(2); /* Ensure valid readings when adc_init returns */ | 106 | sleep(2); /* Ensure valid readings when adc_init returns */ |
106 | } | 107 | } |
108 | #elif CONFIG_CPU == MCF5249 | ||
109 | |||
110 | static unsigned char adcdata[NUM_ADC_CHANNELS]; | ||
111 | |||
112 | #define CS_LO GPIO_OUT &= ~0x80 | ||
113 | #define CS_HI GPIO_OUT |= 0x80 | ||
114 | #define CLK_LO GPIO_OUT &= ~0x00400000 | ||
115 | #define CLK_HI GPIO_OUT |= 0x00400000 | ||
116 | #define DO (GPIO_READ & 0x80000000) | ||
117 | #define DI_LO GPIO_OUT &= ~0x00200000 | ||
118 | #define DI_HI GPIO_OUT |= 0x00200000 | ||
119 | |||
120 | /* delay loop */ | ||
121 | #define DELAY do { int _x; for(_x=0;_x<10;_x++);} while (0) | ||
122 | |||
123 | unsigned char adc_scan(int channel) | ||
124 | { | ||
125 | unsigned char data = 0; | ||
126 | int i; | ||
127 | |||
128 | CS_LO; | ||
129 | |||
130 | DI_HI; /* Start bit */ | ||
131 | DELAY; | ||
132 | CLK_HI; | ||
133 | DELAY; | ||
134 | CLK_LO; | ||
135 | |||
136 | DI_HI; /* Single channel */ | ||
137 | DELAY; | ||
138 | CLK_HI; | ||
139 | DELAY; | ||
140 | CLK_LO; | ||
141 | |||
142 | if(channel & 1) /* LSB of channel number */ | ||
143 | DI_HI; | ||
144 | else | ||
145 | DI_LO; | ||
146 | DELAY; | ||
147 | CLK_HI; | ||
148 | DELAY; | ||
149 | CLK_LO; | ||
150 | |||
151 | if(channel & 2) /* MSB of channel number */ | ||
152 | DI_HI; | ||
153 | else | ||
154 | DI_LO; | ||
155 | DELAY; | ||
156 | CLK_HI; | ||
157 | DELAY; | ||
158 | CLK_LO; | ||
159 | |||
160 | DELAY; | ||
161 | |||
162 | for(i = 0;i < 8;i++) /* 8 bits of data */ | ||
163 | { | ||
164 | CLK_HI; | ||
165 | DELAY; | ||
166 | CLK_LO; | ||
167 | DELAY; | ||
168 | data <<= 1; | ||
169 | data |= DO?1:0; | ||
170 | } | ||
171 | |||
172 | CS_HI; | ||
173 | |||
174 | return data; | ||
175 | } | ||
176 | |||
177 | unsigned short adc_read(int channel) | ||
178 | { | ||
179 | return adcdata[channel]; | ||
180 | } | ||
181 | |||
182 | void adc_init(void) | ||
183 | { | ||
184 | GPIO_FUNCTION |= 0x80600080; /* GPIO7: CS | ||
185 | GPIO21: Data In (to the ADC) | ||
186 | GPIO22: CLK | ||
187 | GPIO31: Data Out (from the ADC) */ | ||
188 | GPIO_ENABLE |= 0x00600080; | ||
189 | GPIO_OUT |= 0x80; /* CS high */ | ||
190 | GPIO_OUT &= ~0x00400000; /* CLK low */ | ||
191 | } | ||
192 | |||
193 | #endif | ||