Fixed prototype of i2c_write for plugins and for i2c-telechips (fix red/yellow).
[Rockbox.git] / firmware / target / arm / i2c-telechips.c
blobbf975f66f0196c0e254145b97300cbdda7bf870c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008 by Rob Purchase
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"
21 #include "system.h"
22 #include "i2c.h"
23 #include "i2c-target.h"
25 /* arbitrary delay loop */
26 #define DELAY do { int _x; for(_x=0;_x<40;_x++);} while (0)
28 static struct mutex i2c_mtx;
30 void i2c_init(void)
32 /* nothing to do */
35 void i2c_start(void)
37 SDA_OUTPUT;
39 SCL_HI;
40 SDA_HI;
41 DELAY;
43 SDA_LO;
44 DELAY;
45 SCL_LO;
46 DELAY;
49 void i2c_stop(void)
51 SDA_OUTPUT;
53 SDA_LO;
54 DELAY;
56 SCL_HI;
57 DELAY;
58 SDA_HI;
59 DELAY;
62 void i2c_outb(unsigned char byte)
64 int bit;
66 SDA_OUTPUT;
68 for (bit = 0; bit < 8; bit++)
70 if ((byte<<bit) & 0x80)
71 SDA_HI;
72 else
73 SDA_LO;
75 DELAY;
77 SCL_HI;
78 DELAY;
79 SCL_LO;
80 DELAY;
84 unsigned char i2c_inb(int ack)
86 int i;
87 unsigned char byte = 0;
89 SDA_INPUT;
91 /* clock in each bit, MSB first */
92 for ( i=0x80; i; i>>=1 )
94 SCL_HI;
95 DELAY;
97 if ( SDA ) byte |= i;
99 SCL_LO;
100 DELAY;
103 i2c_ack(ack);
104 return byte;
107 void i2c_ack(int bit)
109 SDA_OUTPUT;
111 if (bit)
112 SDA_HI;
113 else
114 SDA_LO;
116 SCL_HI;
117 DELAY;
118 SCL_LO;
119 DELAY;
122 int i2c_getack(void)
124 bool ack_bit;
126 SDA_INPUT;
128 SCL_HI;
129 DELAY;
131 ack_bit = SDA;
132 DELAY;
134 SCL_LO;
135 DELAY;
137 return ack_bit;
140 /* device = 8 bit slave address */
141 int i2c_write(int device, const unsigned char* buf, int count )
143 int i = 0;
144 mutex_lock(&i2c_mtx);
146 i2c_start();
147 i2c_outb(device & 0xfe);
149 while (!i2c_getack() && i < count)
151 i2c_outb(buf[i++]);
154 i2c_stop();
155 mutex_unlock(&i2c_mtx);
156 return 0;
160 /* device = 8 bit slave address */
161 int i2c_readmem(int device, int address, unsigned char* buf, int count )
163 int i = 0;
164 mutex_lock(&i2c_mtx);
166 i2c_start();
167 i2c_outb(device & 0xfe);
168 if (i2c_getack()) goto exit;
170 i2c_outb(address);
171 if (i2c_getack()) goto exit;
173 i2c_start();
174 i2c_outb(device | 1);
175 if (i2c_getack()) goto exit;
177 while (i < count)
179 buf[i] = i2c_inb(i == (count-1));
180 i++;
183 exit:
184 i2c_stop();
185 mutex_unlock(&i2c_mtx);
186 return 0;