summaryrefslogtreecommitdiff
path: root/firmware/target/coldfire/iriver/h100/adc-h100.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/coldfire/iriver/h100/adc-h100.c')
-rw-r--r--firmware/target/coldfire/iriver/h100/adc-h100.c117
1 files changed, 117 insertions, 0 deletions
diff --git a/firmware/target/coldfire/iriver/h100/adc-h100.c b/firmware/target/coldfire/iriver/h100/adc-h100.c
new file mode 100644
index 0000000000..b29a2247ad
--- /dev/null
+++ b/firmware/target/coldfire/iriver/h100/adc-h100.c
@@ -0,0 +1,117 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2006 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 "adc.h"
25static unsigned char adcdata[NUM_ADC_CHANNELS];
26
27
28#define CS_LO and_l(~0x80, &GPIO_OUT)
29#define CS_HI or_l(0x80, &GPIO_OUT)
30#define CLK_LO and_l(~0x00400000, &GPIO_OUT)
31#define CLK_HI or_l(0x00400000, &GPIO_OUT)
32#define DO (GPIO_READ & 0x80000000)
33#define DI_LO and_l(~0x00200000, &GPIO_OUT)
34#define DI_HI or_l(0x00200000, &GPIO_OUT)
35
36/* delay loop */
37#define DELAY do { int _x; for(_x=0;_x<10;_x++);} while (0)
38
39unsigned short adc_scan(int channel)
40{
41 unsigned char data = 0;
42 int i;
43
44 CS_LO;
45
46 DI_HI; /* Start bit */
47 DELAY;
48 CLK_HI;
49 DELAY;
50 CLK_LO;
51
52 DI_HI; /* Single channel */
53 DELAY;
54 CLK_HI;
55 DELAY;
56 CLK_LO;
57
58 if(channel & 1) /* LSB of channel number */
59 DI_HI;
60 else
61 DI_LO;
62 DELAY;
63 CLK_HI;
64 DELAY;
65 CLK_LO;
66
67 if(channel & 2) /* MSB of channel number */
68 DI_HI;
69 else
70 DI_LO;
71 DELAY;
72 CLK_HI;
73 DELAY;
74 CLK_LO;
75
76 DELAY;
77
78 for(i = 0;i < 8;i++) /* 8 bits of data */
79 {
80 CLK_HI;
81 DELAY;
82 CLK_LO;
83 DELAY;
84 data <<= 1;
85 data |= DO?1:0;
86 }
87
88 CS_HI;
89
90 adcdata[channel] = data;
91
92 return data;
93}
94unsigned short adc_read(int channel)
95{
96 return adcdata[channel];
97}
98
99static int adc_counter;
100
101static void adc_tick(void)
102{
103 if(++adc_counter == HZ)
104 {
105 adc_counter = 0;
106 adc_scan(ADC_BATTERY);
107 adc_scan(ADC_REMOTEDETECT); /* Temporary. Remove when the remote
108 detection feels stable. */
109 }
110}
111
112void adc_init(void)
113{
114 adc_scan(ADC_BATTERY);
115
116 tick_add_task(adc_tick);
117}