blob: 8c438e2689ce12986a53ccf457bd5c59e1933905 (
plain)
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
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2007 by Dave Chapman
*
* 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"
static unsigned short adcdata[8];
#ifndef BOOTLOADER
/* Tick task */
static void adc_tick(void)
{
int i;
PCLK_ADC |= PCK_EN; /* Enable ADC clock */
/* Start converting the first 4 channels */
for (i = 0; i < 4; i++)
ADCCON = i;
}
/* IRQ handler */
void ADC(void)
{
int num;
uint32_t adc_status;
do
{
adc_status = ADCSTATUS;
num = (adc_status>>24) & 7;
if (num) adcdata[(adc_status >> 16) & 0x7] = adc_status & 0x3ff;
} while (num);
PCLK_ADC &= ~PCK_EN; /* Disable ADC clock */
}
#endif /* BOOTLOADER */
unsigned short adc_read(int channel)
{
#ifdef BOOTLOADER
/* IRQs aren't enabled in the bootloader - just do the read directly */
int i;
uint32_t adc_status;
PCLK_ADC |= PCK_EN; /* Enable ADC clock */
/* Start converting the first 4 channels */
for (i = 0; i < 4; i++)
ADCCON = i;
/* Now read the values back */
for (i=0; i < 4; i++)
{
/* Wait for data to become stable */
while ((ADCDATA & 0x1) == 0);
adc_status = ADCSTATUS;
adcdata[(adc_status >> 16) & 0x7] = adc_status & 0x3ff;
}
PCLK_ADC &= ~PCK_EN; /* Disable ADC clock */
#endif
return adcdata[channel];
}
void adc_init(void)
{
/* consider configuring PCK_ADC source here */
ADCCON = (1<<4); /* Enter standby mode */
ADCCFG |= 0x00000003; /* Single-mode, auto power-down */
#ifndef BOOTLOADER
ADCCFG |= (1<<3); /* Request IRQ on ADC completion */
IEN |= ADC_IRQ_MASK; /* Unmask ADC IRQs */
tick_add_task(adc_tick);
sleep(2); /* Ensure valid readings when adc_init returns */
#endif
}
|