3 /*-----------------------------------------------------------------------------
\r
5 * I2C read/write functions for FX2
\r
7 *-----------------------------------------------------------------------------
\r
9 * Code taken from USRP2 firmware (GNU Radio Project), version 3.0.2,
\r
11 * Copyright 2003 Free Software Foundation, Inc.
\r
13 *-----------------------------------------------------------------------------
\r
15 * This code is part of usbjtag. usbjtag is free software; you can redistribute
\r
17 * it and/or modify it under the terms of the GNU General Public License as
\r
19 * published by the Free Software Foundation; either version 2 of the License,
\r
21 * or (at your option) any later version. usbjtag is distributed in the hope
\r
23 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
\r
25 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
27 * GNU General Public License for more details. You should have received a
\r
29 * copy of the GNU General Public License along with this program in the file
\r
31 * COPYING; if not, write to the Free Software Foundation, Inc., 51 Franklin
\r
33 * St, Fifth Floor, Boston, MA 02110-1301 USA
\r
35 *-----------------------------------------------------------------------------
\r
43 #include "fx2regs.h"
\r
51 // issue a stop bus cycle and wait for completion
\r
57 // returns non-zero if successful, else 0
\r
61 i2c_read (unsigned char addr, xdata unsigned char *buf, unsigned char len)
\r
65 volatile unsigned char junk;
\r
69 if (len == 0) // reading zero bytes always works
\r
75 while (I2CS & bmSTOP) // wait for stop to clear
\r
83 I2DAT = (addr << 1) | 1; // write address and direction (1's the read bit)
\r
87 while ((I2CS & bmDONE) == 0)
\r
93 if ((I2CS & bmBERR) || (I2CS & bmACK) == 0) // no device answered...
\r
105 junk = I2DAT; // trigger the first read cycle
\r
109 while (--len != 0){
\r
111 while ((I2CS & bmDONE) == 0)
\r
129 *buf++ = I2DAT; // get data, trigger another read
\r
135 // wait for final byte
\r
139 while ((I2CS & bmDONE) == 0)
\r
175 // returns non-zero if successful, else 0
\r
179 i2c_write (unsigned char addr, xdata const unsigned char *buf, unsigned char len)
\r
183 while (I2CS & bmSTOP) // wait for stop to clear
\r
191 I2DAT = (addr << 1) | 0; // write address and direction (0's the write bit)
\r
195 while ((I2CS & bmDONE) == 0)
\r
201 if ((I2CS & bmBERR) || (I2CS & bmACK) == 0) // no device answered...
\r
215 while ((I2CS & bmDONE) == 0)
\r
221 if ((I2CS & bmBERR) || (I2CS & bmACK) == 0) // no device answered...
\r