2006-09-29 10:52:34 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 by Wade Brown
|
|
|
|
*
|
|
|
|
* 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 "cpu.h"
|
|
|
|
#include "adc-target.h"
|
2006-12-29 02:49:12 +00:00
|
|
|
#include "kernel.h"
|
2006-09-29 10:52:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2006-12-29 02:49:12 +00:00
|
|
|
static unsigned short adc_readings[NUM_ADC_CHANNELS];
|
2006-09-29 10:52:34 +00:00
|
|
|
|
2006-12-29 02:49:12 +00:00
|
|
|
/* prototypes */
|
|
|
|
static unsigned short __adc_read(int channel);
|
|
|
|
static void adc_tick(void);
|
2006-09-29 10:52:34 +00:00
|
|
|
|
2006-12-29 02:49:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
void adc_init(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Turn on the ADC PCLK */
|
|
|
|
CLKCON |= (1<<15);
|
|
|
|
|
|
|
|
/* Set channel 0, normal mode, disable "start by read" */
|
|
|
|
ADCCON &= ~(0x3F);
|
|
|
|
|
|
|
|
/* No start delay. Use normal conversion mode. */
|
|
|
|
ADCDLY = 0x1;
|
|
|
|
|
|
|
|
/* Set and enable the prescaler */
|
|
|
|
ADCCON = (ADCCON & ~(0xff<<6)) | (0x19<<6);
|
|
|
|
ADCCON |= (1<<14);
|
|
|
|
|
|
|
|
/* prefill the adc channels */
|
|
|
|
for (i = 0; i < NUM_ADC_CHANNELS; i++)
|
|
|
|
{
|
|
|
|
adc_readings[i] = __adc_read(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start at zero so when the tick starts it is at zero */
|
|
|
|
adc_readings[0] = __adc_read(0);
|
|
|
|
|
|
|
|
/* attach the adc reading to the tick */
|
|
|
|
tick_add_task(adc_tick);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Called to get the recent ADC reading */
|
|
|
|
inline unsigned short adc_read(int channel)
|
|
|
|
{
|
|
|
|
return adc_readings[channel];
|
2006-09-29 10:52:34 +00:00
|
|
|
}
|
|
|
|
|
2006-12-29 02:49:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read the ADC by polling
|
|
|
|
* @param channel The ADC channel to read
|
|
|
|
* @return 10bit reading from ADC channel or ADC_READ_ERROR if timeout
|
|
|
|
*/
|
|
|
|
static unsigned short __adc_read(int channel)
|
|
|
|
{
|
2006-09-29 10:52:34 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Set the channel */
|
|
|
|
ADCCON = (ADCCON & ~(0x7<<3)) | (channel<<3);
|
|
|
|
|
|
|
|
/* Start the conversion process */
|
|
|
|
ADCCON |= 0x1;
|
|
|
|
|
|
|
|
/* Wait for a low Enable_start */
|
2006-12-29 02:49:12 +00:00
|
|
|
for (i = 20000;;) {
|
|
|
|
if(0 == (ADCCON & 0x1)) {
|
|
|
|
break;
|
2006-09-29 10:52:34 +00:00
|
|
|
}
|
|
|
|
else {
|
2006-12-29 02:49:12 +00:00
|
|
|
i--;
|
|
|
|
if (0 == i) {
|
|
|
|
/* Ran out of time */
|
|
|
|
return ADC_READ_ERROR;
|
|
|
|
}
|
2006-09-29 10:52:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for high End_of_Conversion */
|
2006-12-29 02:49:12 +00:00
|
|
|
for(i = 20000;;) {
|
|
|
|
if(ADCCON & (1<<15)) {
|
2006-09-29 10:52:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i--;
|
2006-12-29 02:49:12 +00:00
|
|
|
if(0 == i) {
|
|
|
|
/* Ran out of time */
|
|
|
|
return ADC_READ_ERROR;
|
|
|
|
}
|
2006-09-29 10:52:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-29 02:49:12 +00:00
|
|
|
return (ADCDAT0 & 0x3ff);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* add this to the tick so that the ADC converts are done in the background */
|
|
|
|
static void adc_tick(void)
|
|
|
|
{
|
|
|
|
static unsigned channel;
|
|
|
|
|
|
|
|
/* Check if the End Of Conversion is set */
|
|
|
|
if (ADCCON & (1<<15))
|
|
|
|
{
|
|
|
|
adc_readings[channel] = (ADCDAT0 & 0x3FF);
|
|
|
|
if (++channel >= NUM_ADC_CHANNELS)
|
|
|
|
{
|
|
|
|
channel = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setup the next conversion and start it*/
|
|
|
|
ADCCON = (ADCCON & ~(0x7<<3)) | (channel<<3) | 0x01;
|
|
|
|
}
|
2006-09-29 10:52:34 +00:00
|
|
|
}
|
2006-12-29 02:49:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|