blob: 843ab68fb6bfa60c469b8cf3d95790615f687998 [file] [log] [blame]
Dave Chapman657dcb52006-08-31 19:19:35 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2006 by Barry Wardell
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
Dave Chapman657dcb52006-08-31 19:19:35 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include "config.h"
22#include "cpu.h"
23#include "system.h"
24#include "kernel.h"
25#include "thread.h"
26#include "adc.h"
27
28static unsigned short adcdata[NUM_ADC_CHANNELS];
29
30/* Scan ADC so that adcdata[channel] gets updated */
31unsigned short adc_scan(int channel)
32{
33 unsigned int adc_data_1;
34 unsigned int adc_data_2;
35
36 /* Initialise */
37 ADC_ADDR=0x130;
38 ADC_STATUS=0; /* 4 bytes, 1 per channel. Each byte is 0 if the channel is
39 off, 0x40 if the channel is on */
40
41 /* Enable Channel */
42 ADC_ADDR |= (0x1000000<<channel);
43
44 /* Start? */
45 ADC_ADDR |= 0x20000000;
46 ADC_ADDR |= 0x80000000;
47
48 /* ADC_DATA_1 and ADC_DATA_2 are both four bytes, one byte per channel.
49 For each channel, ADC_DATA_1 stores the 8-bit msb, ADC_DATA_2 stores the
50 2-bit lsb (in bits 0 and 1). Each channel is 10 bits total. */
51 adc_data_1 = ((ADC_DATA_1 >> (8*channel)) & 0xff);
52 adc_data_2 = ((ADC_DATA_2 >> (8*channel+6)) & 0x3);
53
54 adcdata[channel] = (adc_data_1<<2 | adc_data_2);
55
56 return adcdata[channel];
57}
58
59/* Read 10-bit channel data */
60unsigned short adc_read(int channel)
61{
62 return adcdata[channel];
63}
64
65static int adc_counter;
66
67static void adc_tick(void)
68{
69 if(++adc_counter == HZ)
70 {
71 adc_counter = 0;
72 adc_scan(ADC_BATTERY);
73 adc_scan(ADC_UNKNOWN_1);
74 adc_scan(ADC_UNKNOWN_2);
75 adc_scan(ADC_SCROLLPAD);
76 }
77}
78
79void adc_init(void)
80{
81 /* Enable ADC */
82 ADC_ENABLE_ADDR |= ADC_ENABLE;
83
84 /* Initialise */
85 ADC_INIT=0;
86 ADC_ADDR=0x130;
87 ADC_STATUS=0;
88
89 /* Enable Channels 1-4 */
90 ADC_ADDR |= 0x1000000;
91 ADC_ADDR |= 0x2000000;
92 ADC_ADDR |= 0x4000000;
93 ADC_ADDR |= 0x8000000;
94
95 /* Start? */
96 ADC_ADDR |= 0x20000000;
97 ADC_ADDR |= 0x80000000;
98
99 /* Wait 50ms for things to settle */
100 sleep(HZ/20);
101
102 tick_add_task(adc_tick);
103}