1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
33 void (*conversion
)(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];
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 */
56 value
|= data
[1] & 0x3;
58 if (adc
->conversion
) {
59 adc
->conversion(&value
);
71 /* Force an ADC scan _now_ */
72 unsigned short adc_scan(int channel
) {
73 struct adc_struct
*adc
= &adcdata
[channel
];
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
]);
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;
91 adcdata
[ADC_4066_ISTAT
].channelnum
= 7;
92 adcdata
[ADC_4066_ISTAT
].timeout
= 0;
94 _adc_read(adc_battery
);