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