vuprintf does not belong in stdio.h, causes problems with other versions of stdio.h
[kugel-rb.git] / firmware / target / arm / ipod / adc-ipod-pcf.c
blobe60d8ebb8fa0d19ca8c8aef1340ac577e558b4a2
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
22 #include "cpu.h"
23 #include "system.h"
24 #include "kernel.h"
25 #include "thread.h"
26 #include "string.h"
27 #include "adc.h"
28 #include "pcf50605.h"
29 #include "i2c-pp.h"
31 struct adc_struct {
32 long timeout;
33 void (*conversion)(unsigned short *data);
34 short channelnum;
35 unsigned short data;
38 static struct adc_struct adcdata[NUM_ADC_CHANNELS] IDATA_ATTR;
40 static unsigned short _adc_read(struct adc_struct *adc)
42 if (TIME_AFTER(current_tick, adc->timeout)) {
43 unsigned char data[2];
44 unsigned short value;
46 i2c_lock();
48 /* 5x per 2 seconds */
49 adc->timeout = current_tick + (HZ * 2 / 5);
51 /* ADCC1, 10 bit, start */
52 pcf50605_write(0x2f, (adc->channelnum << 1) | 0x1);
53 pcf50605_read_multiple(0x30, data, 2); /* ADCS1, ADCS2 */
54 value = data[0];
55 value <<= 2;
56 value |= data[1] & 0x3;
58 if (adc->conversion) {
59 adc->conversion(&value);
61 adc->data = value;
63 i2c_unlock();
64 return value;
65 } else
67 return adc->data;
71 /* Force an ADC scan _now_ */
72 unsigned short adc_scan(int channel) {
73 struct adc_struct *adc = &adcdata[channel];
74 adc->timeout = 0;
75 return _adc_read(adc);
78 /* Retrieve the ADC value, only does a scan periodically */
79 unsigned short adc_read(int channel) {
80 return _adc_read(&adcdata[channel]);
83 void adc_init(void)
85 struct adc_struct *adc_battery = &adcdata[ADC_BATTERY];
86 adc_battery->channelnum = 0x2; /* ADCVIN1, resistive divider */
87 adc_battery->timeout = 0;
88 adcdata[ADC_ACCESSORY].channelnum = 4;
89 adcdata[ADC_ACCESSORY].timeout = 0;
90 #ifdef IPOD_VIDEO
91 adcdata[ADC_4066_ISTAT].channelnum = 7;
92 adcdata[ADC_4066_ISTAT].timeout = 0;
93 #endif
94 _adc_read(adc_battery);