3 * Copyright 2001,2004 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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 3, or (at your option)
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.
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.
23 #include "i2c_bitbang.h"
25 i2c_bitbang::i2c_bitbang (i2c_bbio_sptr io)
30 stop (); // get bus in known state
36 make_i2c_bitbang (i2c_bbio_sptr io)
38 return i2c_sptr (new i2c_bitbang (io));
43 // entry: SCL = 1, SDA = 1
44 // exit: SCL = 0, SDA = 0
51 set_sda (0); // SDA high -> low while SCL high
57 // entry: SCL = X, SDA = X
58 // exit: SCL = 1, SDA = 1
66 set_sda (1); // SDA low -> high while SCL high
71 // entry: SCL = 0, SDA = X
72 // exit: SCL = 0, SDA = X
75 i2c_bitbang::write_bit (bool bit)
84 // entry: SCL = 0, SDA = X
85 // exit: SCL = 0, SDA = 1
88 i2c_bitbang::write_byte (char t)
93 for (i = 0; i < 8; i++){
98 // clock #9. This is the ACK bit.
100 set_sda (1); // tristate SDA
102 ack_bit = get_sda (); // slave should pull SDA line low
109 // write: the high level entry point...
110 // entry: SCL = 1, SDA = 1
111 // exit: SCL = 1, SDA = 1
114 i2c_bitbang::write (int addr, const unsigned char *buf, int nbytes)
120 ok = write_byte ((addr << 1) | 0); // addr plus "read opcode"
122 for (int i = 0; i < nbytes; i++)
123 ok &= write_byte (buf[i]);
131 // read: the high level entry point...
132 // entry: SCL = 1, SDA = 1
133 // exit: SCL = 1, SDA = 1
136 i2c_bitbang::read (int addr, unsigned char *buf, int max_bytes)