blob: 92b947927917293710f1c73323f97d2f008cc4eb [file] [log] [blame]
Marcin Bukat28d54c62010-04-26 21:40:16 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
Marcin Bukat082c7d32010-10-22 12:28:43 +00008 * $Id$
Marcin Bukat28d54c62010-04-26 21:40:16 +00009 *
10 * Copyright (C) 2010 Marcin Bukat
11 *
12 * 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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "config.h"
23#include "cpu.h"
24#include "system.h"
25#include "kernel.h"
26#include "thread.h"
27#include "adc.h"
28
29volatile unsigned short adc_data[NUM_ADC_CHANNELS] IBSS_ATTR;
30
31/* Reading takes 4096 adclk ticks
Marcin Bukatb6271062010-05-18 12:29:21 +000032 * 1) tick task is created that enables ADC interrupt
33 * 2) On interrupt single channel is readed and
34 * ADC is prepared for next channel
35 * 3) When all 4 channels are scanned ADC interrupt is disabled
Marcin Bukat28d54c62010-04-26 21:40:16 +000036 */
37
38void ADC(void) __attribute__ ((interrupt_handler,section(".icode")));
39void ADC(void)
40{
Marcin Bukatb6271062010-05-18 12:29:21 +000041 static unsigned char channel IBSS_ATTR;
Marcin Bukat28d54c62010-04-26 21:40:16 +000042 /* read current value */
Marcin Bukat6e409882010-07-03 21:44:49 +000043 adc_data[(channel&0x03)] = ADVALUE;
Marcin Bukat28d54c62010-04-26 21:40:16 +000044
45 /* switch channel
46 *
47 * set source remark
48 * ADCONFIG is 16bit wide so we have to shift data by 16bits left
49 * thats why we shift <<24 instead of <<8
50 */
51
52 channel++;
53
Marcin Bukat6e409882010-07-03 21:44:49 +000054 and_l(~(0x03<<24),&ADCONFIG);
55 or_l( (((channel&0x03) << 8 )|(1<<7))<<16, &ADCONFIG);
Marcin Bukatb6271062010-05-18 12:29:21 +000056
Marcin Bukat6e409882010-07-03 21:44:49 +000057 if ( (channel & 0x03) == 0 )
Marcin Bukatb6271062010-05-18 12:29:21 +000058 /* disable ADC interrupt */
59 and_l((~(1<<6))<<16,&ADCONFIG);
Marcin Bukat28d54c62010-04-26 21:40:16 +000060}
61
62unsigned short adc_scan(int channel)
63{
64 /* maybe we can drop &0x03 part */
65 return adc_data[(channel&0x03)];
66}
67
Marcin Bukatb6271062010-05-18 12:29:21 +000068void adc_tick(void)
69{
70 /* enable ADC interrupt */
71 or_l( ((1<<6))<<16, &ADCONFIG);
72}
73
Marcin Bukat28d54c62010-04-26 21:40:16 +000074void adc_init(void)
75{
76 /* GPIO38 GPIO39 */
77 and_l(~((1<<6)|(1<<7)), &GPIO1_FUNCTION);
78
79 /* ADOUT_SEL = 01
80 * SOURCE SELECT = 000
81 * CLEAR INTERRUPT FLAG
Marcin Bukatb6271062010-05-18 12:29:21 +000082 * ENABLE INTERRUPT = 0
Marcin Bukat28d54c62010-04-26 21:40:16 +000083 * ADOUT_DRIVE = 00
84 * ADCLK_SEL = 011 (busclk/8)
85 */
86
Marcin Bukat2d0d0822010-05-20 10:13:14 +000087 ADCONFIG = (1<<10)|(1<<8)|(1<<7)|0x03;
Marcin Bukat28d54c62010-04-26 21:40:16 +000088
89 /* ADC interrupt level 4.0 */
90 or_l((4<<28), &INTPRI8);
Marcin Bukatb6271062010-05-18 12:29:21 +000091
92 /* create tick task which enables ADC interrupt */
93 tick_add_task(adc_tick);
Marcin Bukat38edf672010-10-22 12:11:58 +000094
95 /* let the interrupt handler fill readout array */
96 sleep(2);
Marcin Bukat28d54c62010-04-26 21:40:16 +000097}