2 /*-----------------------------------------------------------------------------
\r
3 * I2C read/write functions for FX2
\r
4 *-----------------------------------------------------------------------------
\r
5 * Code taken from USRP2 firmware (GNU Radio Project), version 3.0.2,
\r
6 * Copyright 2003 Free Software Foundation, Inc.
\r
7 *-----------------------------------------------------------------------------
\r
8 * This code is part of usbjtag. usbjtag is free software; you can redistribute
\r
9 * it and/or modify it under the terms of the GNU General Public License as
\r
10 * published by the Free Software Foundation; either version 2 of the License,
\r
11 * or (at your option) any later version. usbjtag is distributed in the hope
\r
12 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
\r
13 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details. You should have received a
\r
15 * copy of the GNU General Public License along with this program in the file
\r
16 * COPYING; if not, write to the Free Software Foundation, Inc., 51 Franklin
\r
17 * St, Fifth Floor, Boston, MA 02110-1301 USA
\r
18 *-----------------------------------------------------------------------------
\r
22 #include "fx2regs.h"
\r
26 // issue a stop bus cycle and wait for completion
\r
29 // returns non-zero if successful, else 0
\r
31 i2c_read (unsigned char addr, xdata unsigned char *buf, unsigned char len)
\r
33 volatile unsigned char junk;
\r
35 if (len == 0) // reading zero bytes always works
\r
38 while (I2CS & bmSTOP) // wait for stop to clear
\r
42 I2DAT = (addr << 1) | 1; // write address and direction (1's the read bit)
\r
44 while ((I2CS & bmDONE) == 0)
\r
47 if ((I2CS & bmBERR) || (I2CS & bmACK) == 0) // no device answered...
\r
53 junk = I2DAT; // trigger the first read cycle
\r
56 while ((I2CS & bmDONE) == 0)
\r
65 *buf++ = I2DAT; // get data, trigger another read
\r
68 // wait for final byte
\r
70 while ((I2CS & bmDONE) == 0)
\r
88 // returns non-zero if successful, else 0
\r
90 i2c_write (unsigned char addr, xdata const unsigned char *buf, unsigned char len)
\r
92 while (I2CS & bmSTOP) // wait for stop to clear
\r
96 I2DAT = (addr << 1) | 0; // write address and direction (0's the write bit)
\r
98 while ((I2CS & bmDONE) == 0)
\r
101 if ((I2CS & bmBERR) || (I2CS & bmACK) == 0) // no device answered...
\r
108 while ((I2CS & bmDONE) == 0)
\r
111 if ((I2CS & bmBERR) || (I2CS & bmACK) == 0) // no device answered...
\r