302 is also a valid redirect. Also keep the original query when redirecting.
[Rockbox.git] / firmware / drivers / i2c.c
blob83ac21fae87316c408325e8b7089d5caa9440600
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 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 "lcd.h"
20 #include "cpu.h"
21 #include "kernel.h"
22 #include "thread.h"
23 #include "debug.h"
24 #include "system.h"
26 /* cute little functions, atomic read-modify-write */
28 /* SDA is PB7 */
29 #define SDA_LO and_b(~0x80, &PBDRL)
30 #define SDA_HI or_b(0x80, &PBDRL)
31 #define SDA_INPUT and_b(~0x80, &PBIORL)
32 #define SDA_OUTPUT or_b(0x80, &PBIORL)
33 #define SDA (PBDRL & 0x80)
35 #if CONFIG_I2C == I2C_ONDIO
36 /* Ondio pinout, SCL moved to PB6 */
37 #define SCL_INPUT and_b(~0x40, &PBIORL)
38 #define SCL_OUTPUT or_b(0x40, &PBIORL)
39 #define SCL_LO and_b(~0x40, &PBDRL)
40 #define SCL_HI or_b(0x40, &PBDRL)
41 #define SCL (PBDRL & 0x40)
42 #else
43 /* "classic" pinout, SCL is PB13 */
44 #define SCL_INPUT and_b(~0x20, &PBIORH)
45 #define SCL_OUTPUT or_b(0x20, &PBIORH)
46 #define SCL_LO and_b(~0x20, &PBDRH)
47 #define SCL_HI or_b(0x20, &PBDRH)
48 #define SCL (PBDRH & 0x20)
49 #endif
51 /* arbitrary delay loop */
52 #define DELAY do { int _x; for(_x=0;_x<20;_x++);} while (0)
54 static struct mutex i2c_mtx;
56 void i2c_begin(void)
58 mutex_lock(&i2c_mtx);
61 void i2c_end(void)
63 mutex_unlock(&i2c_mtx);
66 void i2c_start(void)
68 SDA_OUTPUT;
69 SDA_HI;
70 SCL_HI;
71 SDA_LO;
72 DELAY;
73 SCL_LO;
76 void i2c_stop(void)
78 SDA_LO;
79 SCL_HI;
80 DELAY;
81 SDA_HI;
84 void i2c_init(void)
86 int i;
88 #if CONFIG_I2C == I2C_ONDIO
89 /* make PB6 & PB7 general I/O */
90 PBCR2 &= ~0xf000;
91 #else /* not Ondio */
92 /* make PB7 & PB13 general I/O */
93 PBCR1 &= ~0x0c00; /* PB13 */
94 PBCR2 &= ~0xc000; /* PB7 */
95 #endif
97 SCL_OUTPUT;
98 SDA_OUTPUT;
99 SDA_HI;
100 SCL_LO;
101 for (i=0;i<3;i++)
102 i2c_stop();
105 void i2c_ack(int bit)
107 /* Here's the deal. The MAS is slow, and sometimes needs to wait
108 before it can receive the acknowledge. Therefore it forces the clock
109 low until it is ready. We need to poll the clock line until it goes
110 high before we release the ack. */
112 SCL_LO; /* Set the clock low */
113 if ( bit )
115 SDA_HI;
117 else
119 SDA_LO;
122 SCL_INPUT; /* Set the clock to input */
123 while(!SCL) /* and wait for the MAS to release it */
124 sleep_thread(1);
126 DELAY;
127 SCL_OUTPUT;
128 SCL_LO;
131 int i2c_getack(void)
133 int ret = 1;
135 /* Here's the deal. The MAS is slow, and sometimes needs to wait
136 before it can send the acknowledge. Therefore it forces the clock
137 low until it is ready. We need to poll the clock line until it goes
138 high before we read the ack. */
140 #ifdef HAVE_I2C_LOW_FIRST
141 SDA_LO; /* First, discharge the data line */
142 #endif
143 SDA_INPUT; /* And set to input */
144 SCL_INPUT; /* Set the clock to input */
145 while(!SCL) /* and wait for the MAS to release it */
146 sleep_thread(1);
148 if (SDA)
149 /* ack failed */
150 ret = 0;
152 SCL_OUTPUT;
153 SCL_LO;
154 SDA_HI;
155 SDA_OUTPUT;
156 return ret;
159 void i2c_outb(unsigned char byte)
161 int i;
163 /* clock out each bit, MSB first */
164 for ( i=0x80; i; i>>=1 ) {
165 if ( i & byte )
167 SDA_HI;
169 else
171 SDA_LO;
173 SCL_HI;
174 SCL_LO;
177 SDA_HI;
180 unsigned char i2c_inb(int ack)
182 int i;
183 unsigned char byte = 0;
185 /* clock in each bit, MSB first */
186 for ( i=0x80; i; i>>=1 ) {
187 #ifdef HAVE_I2C_LOW_FIRST
188 /* Tricky business. Here we discharge the data line by driving it low
189 and then set it to input to see if it stays low or goes high */
190 SDA_LO; /* First, discharge the data line */
191 #endif
192 SDA_INPUT; /* And set to input */
193 SCL_HI;
194 if ( SDA )
195 byte |= i;
196 SCL_LO;
197 SDA_OUTPUT;
200 i2c_ack(ack);
202 return byte;
205 int i2c_write(int address, const unsigned char* buf, int count )
207 int i,x=0;
209 i2c_start();
210 i2c_outb(address & 0xfe);
211 if (i2c_getack())
213 for (i=0; i<count; i++)
215 i2c_outb(buf[i]);
216 if (!i2c_getack())
218 x=-2;
219 break;
223 else
225 debugf("i2c_write() - no ack\n");
226 x=-1;
228 i2c_stop();
229 return x;
232 int i2c_read(int address, unsigned char* buf, int count )
234 int i,x=0;
236 i2c_start();
237 i2c_outb(address | 1);
238 if (i2c_getack()) {
239 for (i=0; i<count; i++) {
240 buf[i] = i2c_inb(0);
243 else
244 x=-1;
245 i2c_stop();
246 return x;