FS#11968 by Peter Lecky - Slovak language update
[maemo-rb.git] / firmware / target / coldfire / mpio / adc-mpio.c
blob92b947927917293710f1c73323f97d2f008cc4eb
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Marcin Bukat
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "config.h"
23 #include "cpu.h"
24 #include "system.h"
25 #include "kernel.h"
26 #include "thread.h"
27 #include "adc.h"
29 volatile unsigned short adc_data[NUM_ADC_CHANNELS] IBSS_ATTR;
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
38 void ADC(void) __attribute__ ((interrupt_handler,section(".icode")));
39 void ADC(void)
41 static unsigned char channel IBSS_ATTR;
42 /* read current value */
43 adc_data[(channel&0x03)] = ADVALUE;
45 /* switch channel
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
52 channel++;
54 and_l(~(0x03<<24),&ADCONFIG);
55 or_l( (((channel&0x03) << 8 )|(1<<7))<<16, &ADCONFIG);
57 if ( (channel & 0x03) == 0 )
58 /* disable ADC interrupt */
59 and_l((~(1<<6))<<16,&ADCONFIG);
62 unsigned short adc_scan(int channel)
64 /* maybe we can drop &0x03 part */
65 return adc_data[(channel&0x03)];
68 void adc_tick(void)
70 /* enable ADC interrupt */
71 or_l( ((1<<6))<<16, &ADCONFIG);
74 void adc_init(void)
76 /* GPIO38 GPIO39 */
77 and_l(~((1<<6)|(1<<7)), &GPIO1_FUNCTION);
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)
87 ADCONFIG = (1<<10)|(1<<8)|(1<<7)|0x03;
89 /* ADC interrupt level 4.0 */
90 or_l((4<<28), &INTPRI8);
92 /* create tick task which enables ADC interrupt */
93 tick_add_task(adc_tick);
95 /* let the interrupt handler fill readout array */
96 sleep(2);