Move X5 ADC code to target tree. Fix power button detection on H10. New ADC
driver for H10. Thanks to Laurent Baum for all his help with this code.


git-svn-id: svn://svn.rockbox.org/rockbox/trunk@10701 a1c6a512-1295-4272-9138-f99709370657
diff --git a/firmware/drivers/adc.c b/firmware/drivers/adc.c
index 436c711..1ebac06 100644
--- a/firmware/drivers/adc.c
+++ b/firmware/drivers/adc.c
@@ -283,7 +283,7 @@
     sleep(2);    /* Ensure valid readings when adc_init returns */
 }
 
-#elif (CONFIG_CPU == PP5020) || (CONFIG_CPU == PP5002)
+#elif defined(IPOD_ARCH)
 
 struct adc_struct {
     long timeout;
diff --git a/firmware/export/adc.h b/firmware/export/adc.h
index 2f14e3c..3a43883 100644
--- a/firmware/export/adc.h
+++ b/firmware/export/adc.h
@@ -21,13 +21,8 @@
 
 #include "config.h"
 
-#ifdef IAUDIO_X5
-#define NUM_ADC_CHANNELS 3
-
-#define ADC_BUTTONS 0
-#define ADC_REMOTE  1
-#define ADC_BATTERY 2
-#define ADC_UNREG_POWER ADC_BATTERY /* For compatibility */
+#ifdef TARGET_TREE
+#include "adc-target.h"
 
 #elif defined(IPOD_ARCH)
 #define NUM_ADC_CHANNELS 1
@@ -112,7 +107,7 @@
 void adc_init(void);
 
 #if defined(IRIVER_H100_SERIES) || defined(IRIVER_H300_SERIES)\
- || defined(IAUDIO_X5) || defined(IPOD_ARCH)
+ || defined(IPOD_ARCH)
 /* Force a scan now */
 unsigned short adc_scan(int channel);
 #endif
diff --git a/firmware/target/arm/iriver/h10/adc-h10.c b/firmware/target/arm/iriver/h10/adc-h10.c
index 0e17ae4..b3a36e6 100755
--- a/firmware/target/arm/iriver/h10/adc-h10.c
+++ b/firmware/target/arm/iriver/h10/adc-h10.c
@@ -23,19 +23,82 @@
 #include "thread.h"
 #include "adc.h"
 
-/* TODO: implement adc functionality */
+static unsigned short adcdata[NUM_ADC_CHANNELS];
+
+/* Scan ADC so that adcdata[channel] gets updated */
 unsigned short adc_scan(int channel)
 {
-    (void)channel;
-    return 0;
+    unsigned int adc_data_1;
+    unsigned int adc_data_2;
+
+    /* Initialise */
+    ADC_ADDR=0x130;
+    ADC_STATUS=0;   /* 4 bytes, 1 per channel. Each byte is 0 if the channel is
+                       off, 0x40 if the channel is on */
+    
+    /* Enable Channel */
+    ADC_ADDR |= (0x1000000<<channel);
+    
+    /* Start? */
+    ADC_ADDR |= 0x20000000;
+    ADC_ADDR |= 0x80000000;
+    
+    /* Wait 50ms for things to settle */
+    sleep(HZ/20);
+    
+    /* ADC_DATA_1 and ADC_DATA_2 are both four bytes, one byte per channel.
+       For each channel, ADC_DATA_1 stores the 8-bit msb, ADC_DATA_2 stores the
+       2-bit lsb (in bits 0 and 1). Each channel is 10 bits total. */
+    adc_data_1 = ((ADC_DATA_1 >> (8*channel)) & 0xff);
+    adc_data_2 = ((ADC_DATA_2 >> (8*channel+6)) & 0x3);
+    
+    adcdata[channel] = (adc_data_1<<2 | adc_data_2);
+    
+    return adcdata[channel];
 }
 
+/* Read 10-bit channel data */
 unsigned short adc_read(int channel)
 {
-    (void)channel;
-    return 0;
+    return adcdata[channel];
+}
+
+static int adc_counter;
+
+static void adc_tick(void)
+{
+    if(++adc_counter == HZ)
+    {
+        adc_counter = 0;
+        adc_scan(ADC_BATTERY);
+        adc_scan(ADC_UNKNOWN_1);
+        adc_scan(ADC_UNKNOWN_2);
+        adc_scan(ADC_SCROLLPAD);
+    }
 }
 
 void adc_init(void)
 {
+    /* Enable ADC */
+    ADC_ENABLE_ADDR |= ADC_ENABLE;
+    
+    /* Initialise */
+    ADC_INIT=0;
+    ADC_ADDR=0x130;
+    ADC_STATUS=0;
+    
+    /* Enable Channels 1-4 */
+    ADC_ADDR |= 0x1000000;
+    ADC_ADDR |= 0x2000000;
+    ADC_ADDR |= 0x4000000;
+    ADC_ADDR |= 0x8000000;
+    
+    /* Start? */
+    ADC_ADDR |= 0x20000000;
+    ADC_ADDR |= 0x80000000;
+    
+    /* Wait 50ms for things to settle */
+    sleep(HZ/20);
+    
+    tick_add_task(adc_tick);
 }
diff --git a/firmware/target/arm/iriver/h10/adc-target.h b/firmware/target/arm/iriver/h10/adc-target.h
new file mode 100644
index 0000000..3aab373
--- /dev/null
+++ b/firmware/target/arm/iriver/h10/adc-target.h
@@ -0,0 +1,42 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id$
+ *
+ * Copyright (C) 2006 by Barry Wardell
+ *
+ * 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 ADC_ENABLE_ADDR     (*(volatile unsigned long*)(0x70000010))
+#define ADC_ENABLE          0x1100
+
+#define ADC_ADDR            (*(volatile unsigned long*)(0x7000ad00))
+#define ADC_STATUS          (*(volatile unsigned long*)(0x7000ad04))
+#define ADC_DATA_1          (*(volatile unsigned long*)(0x7000ad20))
+#define ADC_DATA_2          (*(volatile unsigned long*)(0x7000ad24))
+#define ADC_INIT            (*(volatile unsigned long*)(0x7000ad2c))
+
+#define NUM_ADC_CHANNELS 4
+
+#define ADC_BATTERY     0
+#define ADC_UNKNOWN_1   1
+#define ADC_UNKNOWN_2   2
+#define ADC_SCROLLPAD   3
+#define ADC_UNREG_POWER ADC_BATTERY /* For compatibility */
+
+/* Force a scan now */
+unsigned short adc_scan(int channel);
+
+#endif
diff --git a/firmware/target/arm/iriver/h10/button-h10.c b/firmware/target/arm/iriver/h10/button-h10.c
index 2a5983e..08fb808 100644
--- a/firmware/target/arm/iriver/h10/button-h10.c
+++ b/firmware/target/arm/iriver/h10/button-h10.c
@@ -70,7 +70,7 @@
         if ((state & 0x80) == 0) btn |= BUTTON_LEFT;
         
         /* Read power button */
-        if ((GPIOB_INPUT_VAL & 0x1) == 0) btn |= BUTTON_POWER;
+        if ((GPIOB_INPUT_VAL & 0x1) == 1) btn |= BUTTON_POWER;
         
         /* Read scroller */
         if ( ((GPIOC_INPUT_VAL & 0x4)==1) && ((GPIOD_INPUT_VAL & 0x10)==1) )
diff --git a/firmware/target/coldfire/iaudio/x5/adc-target.h b/firmware/target/coldfire/iaudio/x5/adc-target.h
new file mode 100644
index 0000000..5d15805
--- /dev/null
+++ b/firmware/target/coldfire/iaudio/x5/adc-target.h
@@ -0,0 +1,32 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   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.
+ *
+ ****************************************************************************/
+#ifndef _ADC_TARGET_H_
+#define _ADC_TARGET_H_
+
+#define NUM_ADC_CHANNELS 3
+
+#define ADC_BUTTONS 0
+#define ADC_REMOTE  1
+#define ADC_BATTERY 2
+#define ADC_UNREG_POWER ADC_BATTERY /* For compatibility */
+
+/* Force a scan now */
+unsigned short adc_scan(int channel);
+
+#endif