1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
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
;
62 void i2c_outb(unsigned char byte
)
68 for (bit
= 0; bit
< 8; bit
++)
70 if ((byte
<<bit
) & 0x80)
84 unsigned char i2c_inb(int ack
)
87 unsigned char byte
= 0;
91 /* clock in each bit, MSB first */
92 for ( i
=0x80; i
; i
>>=1 )
107 void i2c_ack(int bit
)
140 /* device = 8 bit slave address */
141 int i2c_write(int device
, const unsigned char* buf
, int count
)
144 mutex_lock(&i2c_mtx
);
147 i2c_outb(device
& 0xfe);
149 while (!i2c_getack() && i
< count
)
155 mutex_unlock(&i2c_mtx
);
160 /* device = 8 bit slave address */
161 int i2c_readmem(int device
, int address
, unsigned char* buf
, int count
)
164 mutex_lock(&i2c_mtx
);
167 i2c_outb(device
& 0xfe);
168 if (i2c_getack()) goto exit
;
171 if (i2c_getack()) goto exit
;
174 i2c_outb(device
| 1);
175 if (i2c_getack()) goto exit
;
179 buf
[i
] = i2c_inb(i
== (count
-1));
185 mutex_unlock(&i2c_mtx
);