2010-04-26 21:40:16 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
2010-10-22 12:28:43 +00:00
|
|
|
* $Id$
|
2010-04-26 21:40:16 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Marcin Bukat
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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 "adc.h"
|
|
|
|
|
|
|
|
volatile unsigned short adc_data[NUM_ADC_CHANNELS] IBSS_ATTR;
|
|
|
|
|
|
|
|
/* Reading takes 4096 adclk ticks
|
2010-05-18 12:29:21 +00:00
|
|
|
* 1) tick task is created that enables ADC interrupt
|
|
|
|
* 2) On interrupt single channel is readed and
|
|
|
|
* ADC is prepared for next channel
|
|
|
|
* 3) When all 4 channels are scanned ADC interrupt is disabled
|
2010-04-26 21:40:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
void ADC(void) __attribute__ ((interrupt_handler,section(".icode")));
|
|
|
|
void ADC(void)
|
|
|
|
{
|
2010-05-18 12:29:21 +00:00
|
|
|
static unsigned char channel IBSS_ATTR;
|
2010-04-26 21:40:16 +00:00
|
|
|
/* read current value */
|
2010-07-03 21:44:49 +00:00
|
|
|
adc_data[(channel&0x03)] = ADVALUE;
|
2010-04-26 21:40:16 +00:00
|
|
|
|
|
|
|
/* switch channel
|
|
|
|
*
|
|
|
|
* set source remark
|
|
|
|
* ADCONFIG is 16bit wide so we have to shift data by 16bits left
|
|
|
|
* thats why we shift <<24 instead of <<8
|
|
|
|
*/
|
|
|
|
|
|
|
|
channel++;
|
|
|
|
|
2010-07-03 21:44:49 +00:00
|
|
|
and_l(~(0x03<<24),&ADCONFIG);
|
|
|
|
or_l( (((channel&0x03) << 8 )|(1<<7))<<16, &ADCONFIG);
|
2010-05-18 12:29:21 +00:00
|
|
|
|
2010-07-03 21:44:49 +00:00
|
|
|
if ( (channel & 0x03) == 0 )
|
2010-05-18 12:29:21 +00:00
|
|
|
/* disable ADC interrupt */
|
|
|
|
and_l((~(1<<6))<<16,&ADCONFIG);
|
2010-04-26 21:40:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short adc_scan(int channel)
|
|
|
|
{
|
|
|
|
/* maybe we can drop &0x03 part */
|
|
|
|
return adc_data[(channel&0x03)];
|
|
|
|
}
|
|
|
|
|
2010-05-18 12:29:21 +00:00
|
|
|
void adc_tick(void)
|
|
|
|
{
|
|
|
|
/* enable ADC interrupt */
|
|
|
|
or_l( ((1<<6))<<16, &ADCONFIG);
|
|
|
|
}
|
|
|
|
|
2010-04-26 21:40:16 +00:00
|
|
|
void adc_init(void)
|
|
|
|
{
|
|
|
|
/* GPIO38 GPIO39 */
|
|
|
|
and_l(~((1<<6)|(1<<7)), &GPIO1_FUNCTION);
|
|
|
|
|
|
|
|
/* ADOUT_SEL = 01
|
|
|
|
* SOURCE SELECT = 000
|
|
|
|
* CLEAR INTERRUPT FLAG
|
2010-05-18 12:29:21 +00:00
|
|
|
* ENABLE INTERRUPT = 0
|
2010-04-26 21:40:16 +00:00
|
|
|
* ADOUT_DRIVE = 00
|
|
|
|
* ADCLK_SEL = 011 (busclk/8)
|
|
|
|
*/
|
|
|
|
|
2010-05-20 10:13:14 +00:00
|
|
|
ADCONFIG = (1<<10)|(1<<8)|(1<<7)|0x03;
|
2010-04-26 21:40:16 +00:00
|
|
|
|
|
|
|
/* ADC interrupt level 4.0 */
|
|
|
|
or_l((4<<28), &INTPRI8);
|
2010-05-18 12:29:21 +00:00
|
|
|
|
|
|
|
/* create tick task which enables ADC interrupt */
|
|
|
|
tick_add_task(adc_tick);
|
2010-10-22 12:11:58 +00:00
|
|
|
|
|
|
|
/* let the interrupt handler fill readout array */
|
|
|
|
sleep(2);
|
2010-04-26 21:40:16 +00:00
|
|
|
}
|