Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / io / i2c_bitbang.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2001,2004 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_bitbang.h"
24
25 i2c_bitbang::i2c_bitbang (i2c_bbio_sptr io)
26 {
27   d_io = io;
28   d_io->lock ();
29
30   stop ();      // get bus in known state
31
32   d_io->unlock ();
33 }
34
35 i2c_sptr 
36 make_i2c_bitbang (i2c_bbio_sptr io)
37 {
38   return i2c_sptr (new i2c_bitbang (io));
39 }
40
41
42 // start:
43 //      entry: SCL = 1, SDA = 1
44 //      exit:  SCL = 0, SDA = 0
45
46 void 
47 i2c_bitbang::start ()
48 {
49   set_sda (1);
50   set_scl (1);
51   set_sda (0);          // SDA high -> low while SCL high
52   set_scl (0);
53 }
54
55
56 // stop: 
57 //      entry: SCL = X, SDA = X
58 //      exit:  SCL = 1, SDA = 1
59
60 void 
61 i2c_bitbang::stop ()
62 {
63   set_scl (0);
64   set_sda (0);
65   set_scl (1);
66   set_sda (1);          // SDA low -> high while SCL high
67 }
68
69
70 // write_bit:
71 //      entry: SCL = 0, SDA = X
72 //      exit:  SCL = 0, SDA = X
73
74 void 
75 i2c_bitbang::write_bit (bool bit)
76 {
77   set_sda (bit);
78   set_scl (1);
79   set_scl (0);
80 }
81
82
83 // write_byte:
84 //      entry: SCL = 0, SDA = X
85 //      exit:  SCL = 0, SDA = 1
86
87 bool
88 i2c_bitbang::write_byte (char t)
89 {
90   int   i;
91   bool  ack_bit;
92
93   for (i = 0; i < 8; i++){
94     write_bit (t & 0x80);
95     t <<= 1;
96   }
97
98   // clock #9. This is the ACK bit.
99
100   set_sda (1);          // tristate SDA
101   set_scl (1);
102   ack_bit = get_sda (); // slave should pull SDA line low
103   set_scl (0);
104
105   return ack_bit == 0;
106 }
107
108
109 // write: the high level entry point...
110 //      entry: SCL = 1, SDA = 1
111 //      exit:  SCL = 1, SDA = 1
112
113 bool
114 i2c_bitbang::write (int addr, const unsigned char *buf, int nbytes)
115 {
116   bool  ok = true;
117   
118   d_io->lock ();
119   start ();
120   ok = write_byte ((addr << 1) | 0);    // addr plus "read opcode"
121
122   for (int i = 0; i < nbytes; i++)
123     ok &= write_byte (buf[i]);
124
125   stop ();
126   d_io->unlock ();
127   return ok;
128 }
129
130
131 // read: the high level entry point...
132 //      entry: SCL = 1, SDA = 1
133 //      exit:  SCL = 1, SDA = 1
134
135 int
136 i2c_bitbang::read (int addr, unsigned char *buf, int max_bytes)
137 {
138   d_io->lock ();
139
140   // FIXME
141
142   d_io->unlock ();
143   return -1;
144 }