From a4a3cd9d52b1969bf44315bdf4a229636ceb3f24 Mon Sep 17 00:00:00 2001 From: Jens Arnold Date: Mon, 30 Jul 2007 20:12:03 +0000 Subject: First version of a 1st/2nd gen ADC driver. 1st gen is untested yet. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@14082 a1c6a512-1295-4272-9138-f99709370657 --- firmware/SOURCES | 16 ++--- firmware/export/config-ipod1g2g.h | 8 +-- firmware/target/arm/ipod/1g2g/adc-ipod-1g2g.c | 100 ++++++++++++++++++++++++++ firmware/target/arm/ipod/1g2g/adc-target.h | 34 +++++++++ firmware/target/arm/ipod/adc-ipod-pcf.c | 81 +++++++++++++++++++++ firmware/target/arm/ipod/adc-ipod.c | 91 ----------------------- 6 files changed, 227 insertions(+), 103 deletions(-) create mode 100644 firmware/target/arm/ipod/1g2g/adc-ipod-1g2g.c create mode 100644 firmware/target/arm/ipod/1g2g/adc-target.h create mode 100644 firmware/target/arm/ipod/adc-ipod-pcf.c delete mode 100644 firmware/target/arm/ipod/adc-ipod.c diff --git a/firmware/SOURCES b/firmware/SOURCES index b73a82a15c..91cead088d 100644 --- a/firmware/SOURCES +++ b/firmware/SOURCES @@ -562,7 +562,7 @@ target/arm/wmcodec-pp.c target/arm/i2s-pp.c target/arm/ata-as-arm.S target/arm/ata-pp5020.c -target/arm/ipod/adc-ipod.c +target/arm/ipod/adc-ipod-pcf.c target/arm/ipod/backlight-4g_color.c target/arm/ipod/button-clickwheel.c target/arm/ipod/lcd-gray.c @@ -578,7 +578,7 @@ target/arm/ata-as-arm.S target/arm/ata-pp5020.c target/arm/wmcodec-pp.c target/arm/i2s-pp.c -target/arm/ipod/adc-ipod.c +target/arm/ipod/adc-ipod-pcf.c target/arm/ipod/backlight-4g_color.c target/arm/ipod/button-clickwheel.c target/arm/ipod/lcd-color_nano.c @@ -594,7 +594,7 @@ target/arm/ata-as-arm.S target/arm/ata-pp5020.c target/arm/wmcodec-pp.c target/arm/i2s-pp.c -target/arm/ipod/adc-ipod.c +target/arm/ipod/adc-ipod-pcf.c target/arm/ipod/backlight-nano_video.c target/arm/ipod/button-clickwheel.c target/arm/ipod/lcd-color_nano.c @@ -610,7 +610,7 @@ target/arm/ata-as-arm.S target/arm/ata-pp5020.c target/arm/wmcodec-pp.c target/arm/i2s-pp.c -target/arm/ipod/adc-ipod.c +target/arm/ipod/adc-ipod-pcf.c target/arm/ipod/backlight-nano_video.c target/arm/ipod/button-clickwheel.c target/arm/ipod/power-ipod.c @@ -625,7 +625,7 @@ drivers/pcf50605.c target/arm/ata-pp5002.c target/arm/wmcodec-pp.c target/arm/i2s-pp.c -target/arm/ipod/adc-ipod.c +target/arm/ipod/adc-ipod-pcf.c target/arm/ipod/3g/backlight-3g.c target/arm/ipod/button-1g-3g.c target/arm/ipod/lcd-gray.c @@ -639,7 +639,7 @@ target/arm/usb-pp.c target/arm/ata-pp5002.c target/arm/wmcodec-pp.c target/arm/i2s-pp.c -target/arm/ipod/adc-ipod.c +target/arm/ipod/1g2g/adc-ipod-1g2g.c target/arm/ipod/1g2g/backlight-1g2g.c target/arm/ipod/button-1g-3g.c target/arm/ipod/lcd-gray.c @@ -654,7 +654,7 @@ target/arm/ata-as-arm.S target/arm/ata-pp5020.c target/arm/wmcodec-pp.c target/arm/i2s-pp.c -target/arm/ipod/adc-ipod.c +target/arm/ipod/adc-ipod-pcf.c target/arm/ipod/backlight-mini1g_mini2g.c target/arm/ipod/button-mini1g.c target/arm/ipod/lcd-gray.c @@ -670,7 +670,7 @@ target/arm/ata-as-arm.S target/arm/ata-pp5020.c target/arm/wmcodec-pp.c target/arm/i2s-pp.c -target/arm/ipod/adc-ipod.c +target/arm/ipod/adc-ipod-pcf.c target/arm/ipod/backlight-mini1g_mini2g.c target/arm/ipod/button-clickwheel.c target/arm/ipod/lcd-gray.c diff --git a/firmware/export/config-ipod1g2g.h b/firmware/export/config-ipod1g2g.h index b69d401f36..188c3930f6 100644 --- a/firmware/export/config-ipod1g2g.h +++ b/firmware/export/config-ipod1g2g.h @@ -81,11 +81,11 @@ /* Type of mobile power */ #define CONFIG_BATTERY BATT_LIPOL1300 -#define BATTERY_CAPACITY_MIN 630 /* min. capacity selectable */ -#define BATTERY_CAPACITY_MAX 1000 /* max. capacity selectable */ -#define BATTERY_CAPACITY_INC 10 /* capacity increment */ +#define BATTERY_CAPACITY_MIN 1200 /* min. capacity selectable */ +#define BATTERY_CAPACITY_MAX 1900 /* max. capacity selectable */ +#define BATTERY_CAPACITY_INC 50 /* capacity increment */ #define BATTERY_TYPES_COUNT 1 /* only one type */ -#define BATTERY_SCALE_FACTOR 5865 +#define BATTERY_SCALE_FACTOR 25882 /* Hardware controlled charging? FIXME */ //#define CONFIG_CHARGING CHARGING_SIMPLE diff --git a/firmware/target/arm/ipod/1g2g/adc-ipod-1g2g.c b/firmware/target/arm/ipod/1g2g/adc-ipod-1g2g.c new file mode 100644 index 0000000000..759b6fb592 --- /dev/null +++ b/firmware/target/arm/ipod/1g2g/adc-ipod-1g2g.c @@ -0,0 +1,100 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2007 by Jens Arnold + * + * All files in this archive are subject to the GNU General Public License. + * See the file COPYING in the source tree root for full license agreement. + * + * 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 "hwcompat.h" +#include "kernel.h" + +static struct mutex adc_mutex NOCACHEBSS_ATTR; + +unsigned short adc_scan(int channel) +{ + int i, j; + unsigned short data = 0; + unsigned pval; + + (void)channel; /* there is only one */ + spinlock_lock(&adc_mutex); + + if ((IPOD_HW_REVISION >> 16) == 1) + { + pval = GPIOB_OUTPUT_VAL; + GPIOB_OUTPUT_VAL = pval | 0x04; /* B2 -> high */ + for (i = 32; i > 0; --i); + + GPIOB_OUTPUT_VAL = pval; /* B2 -> low */ + for (i = 200; i > 0; --i); + + for (j = 0; j < 8; j++) + { + GPIOB_OUTPUT_VAL = pval | 0x02; /* B1 -> high */ + for (i = 8; i > 0; --i); + + data = (data << 1) | ((GPIOB_INPUT_VAL & 0x08) >> 3); + + GPIOB_OUTPUT_VAL = pval; /* B1 -> low */ + for (i = 320; i > 0; --i); + } + } + else if ((IPOD_HW_REVISION >> 16) == 2) + { + pval = GPIOB_OUTPUT_VAL; + GPIOB_OUTPUT_VAL = pval | 0x0a; /* B1, B3 -> high */ + while (!(GPIOB_INPUT_VAL & 0x04)); /* wait for B2 == 1 */ + + GPIOB_OUTPUT_VAL = pval; /* B1, B3 -> low */ + while (GPIOB_INPUT_VAL & 0x04); /* wait for B2 == 0 */ + + for (j = 0; j < 8; j++) + { + GPIOB_OUTPUT_VAL = pval | 0x02; /* B1 -> high */ + while (!(GPIOB_INPUT_VAL & 0x04)); /* wait for B2 == 1 */ + + data = (data << 1) | ((GPIOB_INPUT_VAL & 0x10) >> 4); + + GPIOB_OUTPUT_VAL = pval; /* B1 -> low */ + while (GPIOB_INPUT_VAL & 0x04); /* wait for B2 == 0 */ + } + } + spinlock_unlock(&adc_mutex); + return data; +} + +void adc_init(void) +{ + spinlock_init(&adc_mutex); + + GPIOB_ENABLE |= 0x1e; /* enable B1..B4 */ + + if ((IPOD_HW_REVISION >> 16) == 1) + { + GPIOB_OUTPUT_EN = (GPIOB_OUTPUT_EN & ~0x08) | 0x16; + /* B1, B2, B4 -> output, B3 -> input */ + GPIOB_OUTPUT_VAL = (GPIOB_OUTPUT_VAL & ~0x06) | 0x10; + /* B1, B2 -> low, B4 -> high */ + } + else if ((IPOD_HW_REVISION >> 16) == 2) + { + GPIOB_OUTPUT_EN = (GPIOB_OUTPUT_EN & ~0x14) | 0x0a; + /* B1, B3 -> output, B2, B4 -> input */ + GPIOB_OUTPUT_VAL &= ~0x0a; /* B1, B3 -> low */ + while (GPIOB_INPUT_VAL & 0x04); /* wait for B2 == 0 */ + } +} diff --git a/firmware/target/arm/ipod/1g2g/adc-target.h b/firmware/target/arm/ipod/1g2g/adc-target.h new file mode 100644 index 0000000000..b3f6cd6303 --- /dev/null +++ b/firmware/target/arm/ipod/1g2g/adc-target.h @@ -0,0 +1,34 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2007 by Jens Arnold + * + * All files in this archive are subject to the GNU General Public License. + * See the file COPYING in the source tree root for full license agreement. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +#ifndef _ADC_TARGET_H_ +#define _ADC_TARGET_H_ + +#define NUM_ADC_CHANNELS 1 + +#define ADC_BATTERY 0 +#define ADC_UNREG_POWER ADC_BATTERY + +/* Force a scan now */ +unsigned short adc_scan(int channel); +static inline unsigned short adc_read(int channel) +{ + return adc_scan(channel); +} +#endif 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 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2002 by Linus Nielsen Feltzing + * + * All files in this archive are subject to the GNU General Public License. + * See the file COPYING in the source tree root for full license agreement. + * + * 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" + +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) +{ + if (adc->timeout < current_tick) { + unsigned char data[2]; + unsigned short value; + /* 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; + return value; + } else + { + 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; + _adc_read(adc_battery); +} diff --git a/firmware/target/arm/ipod/adc-ipod.c b/firmware/target/arm/ipod/adc-ipod.c deleted file mode 100644 index 39463af30b..0000000000 --- a/firmware/target/arm/ipod/adc-ipod.c +++ /dev/null @@ -1,91 +0,0 @@ -/*************************************************************************** - * __________ __ ___. - * Open \______ \ ____ ____ | | _\_ |__ _______ ___ - * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / - * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < - * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ - * \/ \/ \/ \/ \/ - * $Id$ - * - * Copyright (C) 2002 by Linus Nielsen Feltzing - * - * All files in this archive are subject to the GNU General Public License. - * See the file COPYING in the source tree root for full license agreement. - * - * 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" - -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) -{ -#ifndef IPOD_1G2G - if (adc->timeout < current_tick) { - unsigned char data[2]; - unsigned short value; - /* 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; - return value; - } else -#endif - { - return adc->data; - } -} - -/* Force an ADC scan _now_ */ -unsigned short adc_scan(int channel) { - struct adc_struct *adc = &adcdata[channel]; - adc->timeout = 0; -#ifdef IPOD_1G2G - if (channel == ADC_UNREG_POWER) - return 681; /* FIXME fake 4.00V */ -#endif - return _adc_read(adc); -} - -/* Retrieve the ADC value, only does a scan periodically */ -unsigned short adc_read(int channel) { -#ifdef IPOD_1G2G - if (channel == ADC_UNREG_POWER) - return 681; /* FIXME fake 4.00V */ -#endif - 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; - _adc_read(adc_battery); -} -- cgit v1.2.3