2006-10-05 10:58:51 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 by Linus Nielsen Feltzing
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2006-10-05 10:58:51 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "config.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "string.h"
|
|
|
|
#include "adc.h"
|
|
|
|
#include "pcf50605.h"
|
2007-11-27 00:35:41 +00:00
|
|
|
#include "i2c-pp.h"
|
2006-10-05 10:58:51 +00:00
|
|
|
|
|
|
|
struct adc_struct {
|
|
|
|
long timeout;
|
|
|
|
void (*conversion)(unsigned short *data);
|
|
|
|
short channelnum;
|
|
|
|
unsigned short data;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct adc_struct adcdata[NUM_ADC_CHANNELS] IDATA_ATTR;
|
|
|
|
|
|
|
|
static unsigned short _adc_read(struct adc_struct *adc)
|
|
|
|
{
|
2009-10-18 15:50:30 +00:00
|
|
|
if (TIME_AFTER(current_tick, adc->timeout)) {
|
2006-10-05 10:58:51 +00:00
|
|
|
unsigned char data[2];
|
|
|
|
unsigned short value;
|
2007-11-27 00:35:41 +00:00
|
|
|
|
|
|
|
i2c_lock();
|
|
|
|
|
2006-10-05 10:58:51 +00:00
|
|
|
/* 5x per 2 seconds */
|
|
|
|
adc->timeout = current_tick + (HZ * 2 / 5);
|
|
|
|
|
|
|
|
/* ADCC1, 10 bit, start */
|
|
|
|
pcf50605_write(0x2f, (adc->channelnum << 1) | 0x1);
|
|
|
|
pcf50605_read_multiple(0x30, data, 2); /* ADCS1, ADCS2 */
|
|
|
|
value = data[0];
|
|
|
|
value <<= 2;
|
|
|
|
value |= data[1] & 0x3;
|
|
|
|
|
|
|
|
if (adc->conversion) {
|
|
|
|
adc->conversion(&value);
|
|
|
|
}
|
|
|
|
adc->data = value;
|
2007-11-27 00:35:41 +00:00
|
|
|
|
|
|
|
i2c_unlock();
|
2006-10-05 10:58:51 +00:00
|
|
|
return value;
|
2007-07-27 12:05:54 +00:00
|
|
|
} else
|
2007-07-27 09:57:27 +00:00
|
|
|
{
|
2006-10-05 10:58:51 +00:00
|
|
|
return adc->data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Force an ADC scan _now_ */
|
|
|
|
unsigned short adc_scan(int channel) {
|
|
|
|
struct adc_struct *adc = &adcdata[channel];
|
|
|
|
adc->timeout = 0;
|
|
|
|
return _adc_read(adc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Retrieve the ADC value, only does a scan periodically */
|
|
|
|
unsigned short adc_read(int channel) {
|
|
|
|
return _adc_read(&adcdata[channel]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void adc_init(void)
|
|
|
|
{
|
|
|
|
struct adc_struct *adc_battery = &adcdata[ADC_BATTERY];
|
|
|
|
adc_battery->channelnum = 0x2; /* ADCVIN1, resistive divider */
|
|
|
|
adc_battery->timeout = 0;
|
2008-05-29 06:12:08 +00:00
|
|
|
adcdata[ADC_ACCESSORY].channelnum = 4;
|
|
|
|
adcdata[ADC_ACCESSORY].timeout = 0;
|
2006-10-05 10:58:51 +00:00
|
|
|
_adc_read(adc_battery);
|
|
|
|
}
|