summaryrefslogtreecommitdiff
path: root/firmware/target/coldfire/mpio/adc-mpio.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/coldfire/mpio/adc-mpio.c')
-rw-r--r--firmware/target/coldfire/mpio/adc-mpio.c97
1 files changed, 97 insertions, 0 deletions
diff --git a/firmware/target/coldfire/mpio/adc-mpio.c b/firmware/target/coldfire/mpio/adc-mpio.c
new file mode 100644
index 0000000000..92b9479279
--- /dev/null
+++ b/firmware/target/coldfire/mpio/adc-mpio.c
@@ -0,0 +1,97 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
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
32 * 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
36 */
37
38void ADC(void) __attribute__ ((interrupt_handler,section(".icode")));
39void ADC(void)
40{
41 static unsigned char channel IBSS_ATTR;
42 /* read current value */
43 adc_data[(channel&0x03)] = ADVALUE;
44
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
54 and_l(~(0x03<<24),&ADCONFIG);
55 or_l( (((channel&0x03) << 8 )|(1<<7))<<16, &ADCONFIG);
56
57 if ( (channel & 0x03) == 0 )
58 /* disable ADC interrupt */
59 and_l((~(1<<6))<<16,&ADCONFIG);
60}
61
62unsigned short adc_scan(int channel)
63{
64 /* maybe we can drop &0x03 part */
65 return adc_data[(channel&0x03)];
66}
67
68void adc_tick(void)
69{
70 /* enable ADC interrupt */
71 or_l( ((1<<6))<<16, &ADCONFIG);
72}
73
74void 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
82 * ENABLE INTERRUPT = 0
83 * ADOUT_DRIVE = 00
84 * ADCLK_SEL = 011 (busclk/8)
85 */
86
87 ADCONFIG = (1<<10)|(1<<8)|(1<<7)|0x03;
88
89 /* ADC interrupt level 4.0 */
90 or_l((4<<28), &INTPRI8);
91
92 /* create tick task which enables ADC interrupt */
93 tick_add_task(adc_tick);
94
95 /* let the interrupt handler fill readout array */
96 sleep(2);
97}