Display duration for audio files, by special demand and because it was easy to add.
[kugel-rb.git] / firmware / drivers / i2c.c
blobf919257c49daca8e116dc0d5c7843ea28e802976
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
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 ****************************************************************************/
21 #include "lcd.h"
22 #include "cpu.h"
23 #include "kernel.h"
24 #include "thread.h"
25 #include "debug.h"
26 #include "system.h"
27 #include "i2c.h"
29 /* cute little functions, atomic read-modify-write */
31 /* SDA is PB7 */
32 #define SDA_LO and_b(~0x80, &PBDRL)
33 #define SDA_HI or_b(0x80, &PBDRL)
34 #define SDA_INPUT and_b(~0x80, &PBIORL)
35 #define SDA_OUTPUT or_b(0x80, &PBIORL)
36 #define SDA (PBDRL & 0x80)
38 #if CONFIG_I2C == I2C_ONDIO
39 /* Ondio pinout, SCL moved to PB6 */
40 #define SCL_INPUT and_b(~0x40, &PBIORL)
41 #define SCL_OUTPUT or_b(0x40, &PBIORL)
42 #define SCL_LO and_b(~0x40, &PBDRL)
43 #define SCL_HI or_b(0x40, &PBDRL)
44 #define SCL (PBDRL & 0x40)
45 #else
46 /* "classic" pinout, SCL is PB13 */
47 #define SCL_INPUT and_b(~0x20, &PBIORH)
48 #define SCL_OUTPUT or_b(0x20, &PBIORH)
49 #define SCL_LO and_b(~0x20, &PBDRH)
50 #define SCL_HI or_b(0x20, &PBDRH)
51 #define SCL (PBDRH & 0x20)
52 #endif
54 /* arbitrary delay loop */
55 #define DELAY do { int _x; for(_x=0;_x<20;_x++);} while (0)
57 static struct mutex i2c_mtx;
59 void i2c_begin(void)
61 mutex_lock(&i2c_mtx);
64 void i2c_end(void)
66 mutex_unlock(&i2c_mtx);
69 void i2c_start(void)
71 SDA_OUTPUT;
72 SDA_HI;
73 SCL_HI;
74 SDA_LO;
75 DELAY;
76 SCL_LO;
79 void i2c_stop(void)
81 SDA_LO;
82 SCL_HI;
83 DELAY;
84 SDA_HI;
87 void i2c_init(void)
89 int i;
91 #if CONFIG_I2C == I2C_ONDIO
92 /* make PB6 & PB7 general I/O */
93 PBCR2 &= ~0xf000;
94 #else /* not Ondio */
95 /* make PB7 & PB13 general I/O */
96 PBCR1 &= ~0x0c00; /* PB13 */
97 PBCR2 &= ~0xc000; /* PB7 */
98 #endif
100 SCL_OUTPUT;
101 SDA_OUTPUT;
102 SDA_HI;
103 SCL_LO;
104 for (i=0;i<3;i++)
105 i2c_stop();
108 void i2c_ack(int bit)
110 /* Here's the deal. The MAS is slow, and sometimes needs to wait
111 before it can receive the acknowledge. Therefore it forces the clock
112 low until it is ready. We need to poll the clock line until it goes
113 high before we release the ack. */
115 SCL_LO; /* Set the clock low */
116 if ( bit )
118 SDA_HI;
120 else
122 SDA_LO;
125 SCL_INPUT; /* Set the clock to input */
126 while(!SCL) /* and wait for the MAS to release it */
127 sleep(0);
129 DELAY;
130 SCL_OUTPUT;
131 SCL_LO;
134 int i2c_getack(void)
136 int ret = 1;
138 /* Here's the deal. The MAS is slow, and sometimes needs to wait
139 before it can send the acknowledge. Therefore it forces the clock
140 low until it is ready. We need to poll the clock line until it goes
141 high before we read the ack. */
143 #ifdef HAVE_I2C_LOW_FIRST
144 SDA_LO; /* First, discharge the data line */
145 #endif
146 SDA_INPUT; /* And set to input */
147 SCL_INPUT; /* Set the clock to input */
148 while(!SCL) /* and wait for the MAS to release it */
149 sleep(0);
151 if (SDA)
152 /* ack failed */
153 ret = 0;
155 SCL_OUTPUT;
156 SCL_LO;
157 SDA_HI;
158 SDA_OUTPUT;
159 return ret;
162 void i2c_outb(unsigned char byte)
164 int i;
166 /* clock out each bit, MSB first */
167 for ( i=0x80; i; i>>=1 ) {
168 if ( i & byte )
170 SDA_HI;
172 else
174 SDA_LO;
176 SCL_HI;
177 SCL_LO;
180 SDA_HI;
183 unsigned char i2c_inb(int ack)
185 int i;
186 unsigned char byte = 0;
188 /* clock in each bit, MSB first */
189 for ( i=0x80; i; i>>=1 ) {
190 #ifdef HAVE_I2C_LOW_FIRST
191 /* Tricky business. Here we discharge the data line by driving it low
192 and then set it to input to see if it stays low or goes high */
193 SDA_LO; /* First, discharge the data line */
194 #endif
195 SDA_INPUT; /* And set to input */
196 SCL_HI;
197 if ( SDA )
198 byte |= i;
199 SCL_LO;
200 SDA_OUTPUT;
203 i2c_ack(ack);
205 return byte;
208 int i2c_write(int address, const unsigned char* buf, int count )
210 int i,x=0;
212 i2c_start();
213 i2c_outb(address & 0xfe);
214 if (i2c_getack())
216 for (i=0; i<count; i++)
218 i2c_outb(buf[i]);
219 if (!i2c_getack())
221 x=-2;
222 break;
226 else
228 debugf("i2c_write() - no ack\n");
229 x=-1;
231 i2c_stop();
232 return x;
235 int i2c_read(int address, unsigned char* buf, int count )
237 int i,x=0;
239 i2c_start();
240 i2c_outb(address | 1);
241 if (i2c_getack()) {
242 for (i=0; i<count; i++) {
243 buf[i] = i2c_inb(0);
246 else
247 x=-1;
248 i2c_stop();
249 return x;