Do core interrupt masking in a less general fashion and save some instructions to...
[kugel-rb.git] / firmware / target / coldfire / iriver / h100 / adc-h100.c
blob7ea7618b09a850ad889594b6743170edb1de9a31
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "config.h"
20 #include "cpu.h"
21 #include "system.h"
22 #include "kernel.h"
23 #include "thread.h"
24 #include "adc.h"
27 #define CS_LO and_l(~0x80, &GPIO_OUT)
28 #define CS_HI or_l(0x80, &GPIO_OUT)
29 #define CLK_LO and_l(~0x00400000, &GPIO_OUT)
30 #define CLK_HI or_l(0x00400000, &GPIO_OUT)
31 #define DO (GPIO_READ & 0x80000000)
32 #define DI_LO and_l(~0x00200000, &GPIO_OUT)
33 #define DI_HI or_l(0x00200000, &GPIO_OUT)
35 /* delay loop */
36 #define DELAY \
37 ({ \
38 int _x_; \
39 asm volatile ( \
40 "move.l #11, %[_x_] \r\n" \
41 "1: \r\n" \
42 "subq.l #1, %[_x_] \r\n" \
43 "bhi.b 1b \r\n" \
44 : [_x_]"=&d"(_x_) \
45 ); \
48 unsigned short adc_scan(int channel)
50 int level = disable_irq_save();
51 unsigned char data = 0;
52 int i;
54 CS_LO;
56 DI_HI; /* Start bit */
57 DELAY;
58 CLK_HI;
59 DELAY;
60 CLK_LO;
62 DI_HI; /* Single channel */
63 DELAY;
64 CLK_HI;
65 DELAY;
66 CLK_LO;
68 if(channel & 1) /* LSB of channel number */
69 DI_HI;
70 else
71 DI_LO;
72 DELAY;
73 CLK_HI;
74 DELAY;
75 CLK_LO;
77 if(channel & 2) /* MSB of channel number */
78 DI_HI;
79 else
80 DI_LO;
81 DELAY;
82 CLK_HI;
83 DELAY;
84 CLK_LO;
86 DELAY;
88 for(i = 0;i < 8;i++) /* 8 bits of data */
90 CLK_HI;
91 DELAY;
92 CLK_LO;
93 DELAY;
94 data <<= 1;
95 data |= DO?1:0;
98 CS_HI;
100 restore_irq(level);
101 return data;
104 void adc_init(void)
106 or_l(0x80600080, &GPIO_FUNCTION); /* GPIO7: CS
107 GPIO21: Data In (to the ADC)
108 GPIO22: CLK
109 GPIO31: Data Out (from the ADC) */
110 or_l(0x00600080, &GPIO_ENABLE);
111 or_l(0x80, &GPIO_OUT); /* CS high */
112 and_l(~0x00400000, &GPIO_OUT); /* CLK low */