summaryrefslogtreecommitdiff
path: root/firmware/target/arm/ipod/adc-ipod-pcf.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/arm/ipod/adc-ipod-pcf.c')
-rw-r--r--firmware/target/arm/ipod/adc-ipod-pcf.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/firmware/target/arm/ipod/adc-ipod-pcf.c b/firmware/target/arm/ipod/adc-ipod-pcf.c
new file mode 100644
index 0000000000..bc2524de5d
--- /dev/null
+++ b/firmware/target/arm/ipod/adc-ipod-pcf.c
@@ -0,0 +1,81 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
11 *
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.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19#include "config.h"
20#include "cpu.h"
21#include "system.h"
22#include "kernel.h"
23#include "thread.h"
24#include "string.h"
25#include "adc.h"
26#include "pcf50605.h"
27
28struct adc_struct {
29 long timeout;
30 void (*conversion)(unsigned short *data);
31 short channelnum;
32 unsigned short data;
33};
34
35static struct adc_struct adcdata[NUM_ADC_CHANNELS] IDATA_ATTR;
36
37static unsigned short _adc_read(struct adc_struct *adc)
38{
39 if (adc->timeout < current_tick) {
40 unsigned char data[2];
41 unsigned short value;
42 /* 5x per 2 seconds */
43 adc->timeout = current_tick + (HZ * 2 / 5);
44
45 /* ADCC1, 10 bit, start */
46 pcf50605_write(0x2f, (adc->channelnum << 1) | 0x1);
47 pcf50605_read_multiple(0x30, data, 2); /* ADCS1, ADCS2 */
48 value = data[0];
49 value <<= 2;
50 value |= data[1] & 0x3;
51
52 if (adc->conversion) {
53 adc->conversion(&value);
54 }
55 adc->data = value;
56 return value;
57 } else
58 {
59 return adc->data;
60 }
61}
62
63/* Force an ADC scan _now_ */
64unsigned short adc_scan(int channel) {
65 struct adc_struct *adc = &adcdata[channel];
66 adc->timeout = 0;
67 return _adc_read(adc);
68}
69
70/* Retrieve the ADC value, only does a scan periodically */
71unsigned short adc_read(int channel) {
72 return _adc_read(&adcdata[channel]);
73}
74
75void adc_init(void)
76{
77 struct adc_struct *adc_battery = &adcdata[ADC_BATTERY];
78 adc_battery->channelnum = 0x2; /* ADCVIN1, resistive divider */
79 adc_battery->timeout = 0;
80 _adc_read(adc_battery);
81}