1 | /* -*- c++ -*- */
|
---|
2 | /*-----------------------------------------------------------------------------
|
---|
3 | * I2C read/write functions for FX2
|
---|
4 | *-----------------------------------------------------------------------------
|
---|
5 | * Code taken from USRP2 firmware (GNU Radio Project), version 3.0.2,
|
---|
6 | * Copyright 2003 Free Software Foundation, Inc.
|
---|
7 | *-----------------------------------------------------------------------------
|
---|
8 | * This code is part of usbjtag. usbjtag is free software; you can redistribute
|
---|
9 | * it and/or modify it under the terms of the GNU General Public License as
|
---|
10 | * published by the Free Software Foundation; either version 2 of the License,
|
---|
11 | * or (at your option) any later version. usbjtag is distributed in the hope
|
---|
12 | * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
|
---|
13 | * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details. You should have received a
|
---|
15 | * copy of the GNU General Public License along with this program in the file
|
---|
16 | * COPYING; if not, write to the Free Software Foundation, Inc., 51 Franklin
|
---|
17 | * St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
18 | *-----------------------------------------------------------------------------
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "i2c.h"
|
---|
22 | #include "fx2regs.h"
|
---|
23 | #include <string.h>
|
---|
24 |
|
---|
25 |
|
---|
26 | // issue a stop bus cycle and wait for completion
|
---|
27 |
|
---|
28 |
|
---|
29 | // returns non-zero if successful, else 0
|
---|
30 | unsigned char
|
---|
31 | i2c_read (unsigned char addr, xdata unsigned char *buf, unsigned char len)
|
---|
32 | {
|
---|
33 | volatile unsigned char junk;
|
---|
34 |
|
---|
35 | if (len == 0) // reading zero bytes always works
|
---|
36 | return 1;
|
---|
37 |
|
---|
38 | while (I2CS & bmSTOP) // wait for stop to clear
|
---|
39 | ;
|
---|
40 |
|
---|
41 | I2CS = bmSTART;
|
---|
42 | I2DAT = (addr << 1) | 1; // write address and direction (1's the read bit)
|
---|
43 |
|
---|
44 | while ((I2CS & bmDONE) == 0)
|
---|
45 | ;
|
---|
46 |
|
---|
47 | if ((I2CS & bmBERR) || (I2CS & bmACK) == 0) // no device answered...
|
---|
48 | goto fail;
|
---|
49 |
|
---|
50 | if (len == 1)
|
---|
51 | I2CS |= bmLASTRD;
|
---|
52 |
|
---|
53 | junk = I2DAT; // trigger the first read cycle
|
---|
54 |
|
---|
55 | while (--len != 0){
|
---|
56 | while ((I2CS & bmDONE) == 0)
|
---|
57 | ;
|
---|
58 |
|
---|
59 | if (I2CS & bmBERR)
|
---|
60 | goto fail;
|
---|
61 |
|
---|
62 | if (len == 1)
|
---|
63 | I2CS |= bmLASTRD;
|
---|
64 |
|
---|
65 | *buf++ = I2DAT; // get data, trigger another read
|
---|
66 | }
|
---|
67 |
|
---|
68 | // wait for final byte
|
---|
69 |
|
---|
70 | while ((I2CS & bmDONE) == 0)
|
---|
71 | ;
|
---|
72 |
|
---|
73 | if (I2CS & bmBERR)
|
---|
74 | goto fail;
|
---|
75 |
|
---|
76 | I2CS |= bmSTOP;
|
---|
77 | *buf = I2DAT;
|
---|
78 |
|
---|
79 | return 1;
|
---|
80 |
|
---|
81 | fail:
|
---|
82 | I2CS |= bmSTOP;
|
---|
83 | return 0;
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 |
|
---|
88 | // returns non-zero if successful, else 0
|
---|
89 | unsigned char
|
---|
90 | i2c_write (unsigned char addr, xdata const unsigned char *buf, unsigned char len)
|
---|
91 | {
|
---|
92 | while (I2CS & bmSTOP) // wait for stop to clear
|
---|
93 | ;
|
---|
94 |
|
---|
95 | I2CS = bmSTART;
|
---|
96 | I2DAT = (addr << 1) | 0; // write address and direction (0's the write bit)
|
---|
97 |
|
---|
98 | while ((I2CS & bmDONE) == 0)
|
---|
99 | ;
|
---|
100 |
|
---|
101 | if ((I2CS & bmBERR) || (I2CS & bmACK) == 0) // no device answered...
|
---|
102 | goto fail;
|
---|
103 |
|
---|
104 | while (len > 0){
|
---|
105 | I2DAT = *buf++;
|
---|
106 | len--;
|
---|
107 |
|
---|
108 | while ((I2CS & bmDONE) == 0)
|
---|
109 | ;
|
---|
110 |
|
---|
111 | if ((I2CS & bmBERR) || (I2CS & bmACK) == 0) // no device answered...
|
---|
112 | goto fail;
|
---|
113 | }
|
---|
114 |
|
---|
115 | I2CS |= bmSTOP;
|
---|
116 | return 1;
|
---|
117 |
|
---|
118 | fail:
|
---|
119 | I2CS |= bmSTOP;
|
---|
120 | return 0;
|
---|
121 | }
|
---|