1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
/***************************************************************************
* __________ __ ___.
* 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"
#include "pcf50606.h"
#if CONFIG_CPU == SH7034
/**************************************************************************
** The A/D conversion is done every tick, in three steps:
**
** 1) On the tick interrupt, the conversion of channels 0-3 is started, and
** the A/D interrupt is enabled.
**
** 2) After the conversion is done (approx. 256*4 cycles later), an interrupt
** is generated at level 1, which is the same level as the tick interrupt
** itself. This interrupt will be pending until the tick interrupt is
** finished.
** When the A/D interrupt is finally served, it will read the results
** from the first conversion and start the conversion of channels 4-7.
**
** 3) When the conversion of channels 4-7 is finished, the interrupt is
** triggered again, and the results are read. This time, no new
** conversion is started, it will be done in the next tick interrupt.
**
** Thus, each channel will be updated HZ times per second.
**
*************************************************************************/
static int current_channel;
static unsigned short adcdata[NUM_ADC_CHANNELS];
static void adc_tick(void)
{
/* Start a conversion of channel group 0. This will trigger an interrupt,
and the interrupt handler will take care of group 1. */
current_channel = 0;
ADCSR = ADCSR_ADST | ADCSR_ADIE | ADCSR_SCAN | 3;
}
void ADITI(void) __attribute__((interrupt_handler));
void ADITI(void)
{
if(ADCSR & ADCSR_ADF)
{
ADCSR = 0;
if(current_channel == 0)
{
adcdata[0] = ADDRA >> 6;
adcdata[1] = ADDRB >> 6;
adcdata[2] = ADDRC >> 6;
adcdata[3] = ADDRD >> 6;
current_channel = 4;
/* Convert the next group */
ADCSR = ADCSR_ADST | ADCSR_ADIE | ADCSR_SCAN | 7;
}
else
{
adcdata[4] = ADDRA >> 6;
adcdata[5] = ADDRB >> 6;
adcdata[6] = ADDRC >> 6;
adcdata[7] = ADDRD >> 6;
}
}
}
unsigned short adc_read(int channel)
{
return adcdata[channel];
}
void adc_init(void)
{
ADCR = 0x7f; /* No external trigger; other bits should be 1 according
to the manual... */
ADCSR = 0;
current_channel = 0;
/* Enable the A/D IRQ on level 1 */
IPRE = (IPRE & 0xf0ff) | 0x0100;
tick_add_task(adc_tick);
sleep(2); /* Ensure valid readings when adc_init returns */
}
#elif CONFIG_CPU == MCF5249
static unsigned char adcdata[NUM_ADC_CHANNELS];
#ifdef IRIVER_H300_SERIES
static int channelnum[] =
{
5, /* ADC_BUTTONS (ADCIN2) */
6, /* ADC_REMOTE (ADCIN3) */
0, /* ADC_BATTERY (BATVOLT, resistive divider) */
2, /* ADC_REMOTEDETECT (ADCIN1, resistive divider) */
};
unsigned short adc_scan(int channel)
{
unsigned char data;
pcf50606_write(0x2f, 0x80 | (channelnum[channel] << 1) | 1);
data = pcf50606_read(0x30);
adcdata[channel] = data;
return data;
}
#else
#define CS_LO and_l(~0x80, &GPIO_OUT)
#define CS_HI or_l(0x80, &GPIO_OUT)
#define CLK_LO and_l(~0x00400000, &GPIO_OUT)
#define CLK_HI or_l(0x00400000, &GPIO_OUT)
#define DO (GPIO_READ & 0x80000000)
#define DI_LO and_l(~0x00200000, &GPIO_OUT)
#define DI_HI or_l(0x00200000, &GPIO_OUT)
/* delay loop */
#define DELAY do { int _x; for(_x=0;_x<10;_x++);} while (0)
unsigned short adc_scan(int channel)
{
unsigned char data = 0;
int i;
CS_LO;
DI_HI; /* Start bit */
DELAY;
CLK_HI;
DELAY;
CLK_LO;
DI_HI; /* Single channel */
DELAY;
CLK_HI;
DELAY;
CLK_LO;
if(channel & 1) /* LSB of channel number */
DI_HI;
else
DI_LO;
DELAY;
CLK_HI;
DELAY;
CLK_LO;
if(channel & 2) /* MSB of channel number */
DI_HI;
else
DI_LO;
DELAY;
CLK_HI;
DELAY;
CLK_LO;
DELAY;
for(i = 0;i < 8;i++) /* 8 bits of data */
{
CLK_HI;
DELAY;
CLK_LO;
DELAY;
data <<= 1;
data |= DO?1:0;
}
CS_HI;
adcdata[channel] = data;
return data;
}
#endif
unsigned short adc_read(int channel)
{
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_REMOTEDETECT); /* Temporary. Remove when the remote
detection feels stable. */
}
}
void adc_init(void)
{
#ifndef IRIVER_H300_SERIES
or_l(0x80600080, &GPIO_FUNCTION); /* GPIO7: CS
GPIO21: Data In (to the ADC)
GPIO22: CLK
GPIO31: Data Out (from the ADC) */
or_l(0x00600080, &GPIO_ENABLE);
or_l(0x80, &GPIO_OUT); /* CS high */
and_l(~0x00400000, &GPIO_OUT); /* CLK low */
#endif
adc_scan(ADC_BATTERY);
tick_add_task(adc_tick);
}
#elif CONFIG_CPU == TCC730
/**************************************************************************
**
** Each channel will be updated HZ/CHANNEL_ORDER_SIZE times per second.
**
*************************************************************************/
static int current_channel;
static int current_channel_idx;
static unsigned short adcdata[NUM_ADC_CHANNELS];
#define CHANNEL_ORDER_SIZE 2
static int channel_order[CHANNEL_ORDER_SIZE] = {6,7};
static void adc_tick(void)
{
if (ADCON & (1 << 3)) {
/* previous conversion finished? */
adcdata[current_channel] = ADDATA >> 6;
if (++current_channel_idx >= CHANNEL_ORDER_SIZE)
current_channel_idx = 0;
current_channel = channel_order[current_channel_idx];
int adcon = (current_channel << 4) | 1;
ADCON = adcon;
}
}
unsigned short adc_read(int channel)
{
return adcdata[channel];
}
void adc_init(void)
{
current_channel_idx = 0;
current_channel = channel_order[current_channel_idx];
ADCON = (current_channel << 4) | 1;
tick_add_task(adc_tick);
sleep(2); /* Ensure valid readings when adc_init returns */
}
#elif defined(IPOD_ARCH)
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);
}
#elif CONFIG_CPU == PNX0101
static unsigned short adcdata[NUM_ADC_CHANNELS];
unsigned short adc_read(int channel)
{
return adcdata[channel];
}
static void adc_tick(void)
{
if (ADCST & 0x10) {
adcdata[0] = ADCCH0 & 0x3ff;
adcdata[1] = ADCCH1 & 0x3ff;
adcdata[2] = ADCCH2 & 0x3ff;
adcdata[3] = ADCCH3 & 0x3ff;
adcdata[4] = ADCCH4 & 0x3ff;
ADCST = 0xa;
}
}
void adc_init(void)
{
ADCR24 = 0xaaaaa;
ADCR28 = 0;
ADCST = 2;
ADCST = 0xa;
while (!(ADCST & 0x10));
adc_tick();
tick_add_task(adc_tick);
}
#endif
|