1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
25 #if CONFIG_CPU == SH7034
26 /**************************************************************************
27 ** The A/D conversion is done every tick, in three steps:
29 ** 1) On the tick interrupt, the conversion of channels 0-3 is started, and
30 ** the A/D interrupt is enabled.
32 ** 2) After the conversion is done (approx. 256*4 cycles later), an interrupt
33 ** is generated at level 1, which is the same level as the tick interrupt
34 ** itself. This interrupt will be pending until the tick interrupt is
36 ** When the A/D interrupt is finally served, it will read the results
37 ** from the first conversion and start the conversion of channels 4-7.
39 ** 3) When the conversion of channels 4-7 is finished, the interrupt is
40 ** triggered again, and the results are read. This time, no new
41 ** conversion is started, it will be done in the next tick interrupt.
43 ** Thus, each channel will be updated HZ times per second.
45 *************************************************************************/
47 static int current_channel
;
48 static unsigned short adcdata
[NUM_ADC_CHANNELS
];
50 static void adc_tick(void)
52 /* Start a conversion of channel group 0. This will trigger an interrupt,
53 and the interrupt handler will take care of group 1. */
56 ADCSR
= ADCSR_ADST
| ADCSR_ADIE
| ADCSR_SCAN
| 3;
66 if(current_channel
== 0)
68 adcdata
[0] = ADDRA
>> 6;
69 adcdata
[1] = ADDRB
>> 6;
70 adcdata
[2] = ADDRC
>> 6;
71 adcdata
[3] = ADDRD
>> 6;
74 /* Convert the next group */
75 ADCSR
= ADCSR_ADST
| ADCSR_ADIE
| ADCSR_SCAN
| 7;
79 adcdata
[4] = ADDRA
>> 6;
80 adcdata
[5] = ADDRB
>> 6;
81 adcdata
[6] = ADDRC
>> 6;
82 adcdata
[7] = ADDRD
>> 6;
87 unsigned short adc_read(int channel
)
89 return adcdata
[channel
];
94 ADCR
= 0x7f; /* No external trigger; other bits should be 1 according
101 /* Enable the A/D IRQ on level 1 */
102 IPRE
= (IPRE
& 0xf0ff) | 0x0100;
104 tick_add_task(adc_tick
);
106 sleep(2); /* Ensure valid readings when adc_init returns */
108 #elif CONFIG_CPU == MCF5249
110 static unsigned char adcdata
[NUM_ADC_CHANNELS
];
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
121 #define DELAY do { int _x; for(_x=0;_x<10;_x++);} while (0)
123 unsigned char adc_scan(int channel
)
125 unsigned char data
= 0;
130 DI_HI
; /* Start bit */
136 DI_HI
; /* Single channel */
142 if(channel
& 1) /* LSB of channel number */
151 if(channel
& 2) /* MSB of channel number */
162 for(i
= 0;i
< 8;i
++) /* 8 bits of data */
177 unsigned short adc_read(int channel
)
179 return adcdata
[channel
];
182 static int adc_counter
;
184 static void adc_tick(void)
186 if(++adc_counter
== HZ
)
189 adcdata
[ADC_BATTERY
] = adc_scan(ADC_BATTERY
);
195 GPIO_FUNCTION
|= 0x80600080; /* GPIO7: CS
196 GPIO21: Data In (to the ADC)
198 GPIO31: Data Out (from the ADC) */
199 GPIO_ENABLE
|= 0x00600080;
200 GPIO_OUT
|= 0x80; /* CS high */
201 GPIO_OUT
&= ~0x00400000; /* CLK low */
203 tick_add_task(adc_tick
);
205 adcdata
[3] = adc_scan(3);
208 #elif CONFIG_CPU == TCC730
211 /**************************************************************************
213 ** Each channel will be updated HZ/CHANNEL_ORDER_SIZE times per second.
215 *************************************************************************/
217 static int current_channel
;
218 static int current_channel_idx
;
219 static unsigned short adcdata
[NUM_ADC_CHANNELS
];
221 #define CHANNEL_ORDER_SIZE 2
222 static int channel_order
[CHANNEL_ORDER_SIZE
] = {6,7};
224 static void adc_tick(void)
226 if (ADCON
& (1 << 3)) {
227 /* previous conversion finished? */
228 adcdata
[current_channel
] = ADDATA
>> 6;
229 if (++current_channel_idx
>= CHANNEL_ORDER_SIZE
)
230 current_channel_idx
= 0;
231 current_channel
= channel_order
[current_channel_idx
];
232 int adcon
= (current_channel
<< 4) | 1;
237 unsigned short adc_read(int channel
)
239 return adcdata
[channel
];
244 current_channel_idx
= 0;
245 current_channel
= channel_order
[current_channel_idx
];
247 ADCON
= (current_channel
<< 4) | 1;
249 tick_add_task(adc_tick
);
251 sleep(2); /* Ensure valid readings when adc_init returns */