Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
Marcin Bukat | 082c7d3 | 2010-10-22 12:28:43 +0000 | [diff] [blame^] | 8 | * $Id$ |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 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 | |
| 29 | volatile unsigned short adc_data[NUM_ADC_CHANNELS] IBSS_ATTR; |
| 30 | |
| 31 | /* Reading takes 4096 adclk ticks |
Marcin Bukat | b627106 | 2010-05-18 12:29:21 +0000 | [diff] [blame] | 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 |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | void ADC(void) __attribute__ ((interrupt_handler,section(".icode"))); |
| 39 | void ADC(void) |
| 40 | { |
Marcin Bukat | b627106 | 2010-05-18 12:29:21 +0000 | [diff] [blame] | 41 | static unsigned char channel IBSS_ATTR; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 42 | /* read current value */ |
Marcin Bukat | 6e40988 | 2010-07-03 21:44:49 +0000 | [diff] [blame] | 43 | adc_data[(channel&0x03)] = ADVALUE; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 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 | |
Marcin Bukat | 6e40988 | 2010-07-03 21:44:49 +0000 | [diff] [blame] | 54 | and_l(~(0x03<<24),&ADCONFIG); |
| 55 | or_l( (((channel&0x03) << 8 )|(1<<7))<<16, &ADCONFIG); |
Marcin Bukat | b627106 | 2010-05-18 12:29:21 +0000 | [diff] [blame] | 56 | |
Marcin Bukat | 6e40988 | 2010-07-03 21:44:49 +0000 | [diff] [blame] | 57 | if ( (channel & 0x03) == 0 ) |
Marcin Bukat | b627106 | 2010-05-18 12:29:21 +0000 | [diff] [blame] | 58 | /* disable ADC interrupt */ |
| 59 | and_l((~(1<<6))<<16,&ADCONFIG); |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | unsigned short adc_scan(int channel) |
| 63 | { |
| 64 | /* maybe we can drop &0x03 part */ |
| 65 | return adc_data[(channel&0x03)]; |
| 66 | } |
| 67 | |
Marcin Bukat | b627106 | 2010-05-18 12:29:21 +0000 | [diff] [blame] | 68 | void adc_tick(void) |
| 69 | { |
| 70 | /* enable ADC interrupt */ |
| 71 | or_l( ((1<<6))<<16, &ADCONFIG); |
| 72 | } |
| 73 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 74 | void 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 Bukat | b627106 | 2010-05-18 12:29:21 +0000 | [diff] [blame] | 82 | * ENABLE INTERRUPT = 0 |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 83 | * ADOUT_DRIVE = 00 |
| 84 | * ADCLK_SEL = 011 (busclk/8) |
| 85 | */ |
| 86 | |
Marcin Bukat | 2d0d082 | 2010-05-20 10:13:14 +0000 | [diff] [blame] | 87 | ADCONFIG = (1<<10)|(1<<8)|(1<<7)|0x03; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 88 | |
| 89 | /* ADC interrupt level 4.0 */ |
| 90 | or_l((4<<28), &INTPRI8); |
Marcin Bukat | b627106 | 2010-05-18 12:29:21 +0000 | [diff] [blame] | 91 | |
| 92 | /* create tick task which enables ADC interrupt */ |
| 93 | tick_add_task(adc_tick); |
Marcin Bukat | 38edf67 | 2010-10-22 12:11:58 +0000 | [diff] [blame] | 94 | |
| 95 | /* let the interrupt handler fill readout array */ |
| 96 | sleep(2); |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 97 | } |