1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
/* -*- c++ -*- */
/*
* Copyright 2001,2004 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* GNU Radio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "i2c_bitbang.h"
i2c_bitbang::i2c_bitbang (i2c_bbio_sptr io)
{
d_io = io;
d_io->lock ();
stop (); // get bus in known state
d_io->unlock ();
}
i2c_sptr
make_i2c_bitbang (i2c_bbio_sptr io)
{
return i2c_sptr (new i2c_bitbang (io));
}
// start:
// entry: SCL = 1, SDA = 1
// exit: SCL = 0, SDA = 0
void
i2c_bitbang::start ()
{
set_sda (1);
set_scl (1);
set_sda (0); // SDA high -> low while SCL high
set_scl (0);
}
// stop:
// entry: SCL = X, SDA = X
// exit: SCL = 1, SDA = 1
void
i2c_bitbang::stop ()
{
set_scl (0);
set_sda (0);
set_scl (1);
set_sda (1); // SDA low -> high while SCL high
}
// write_bit:
// entry: SCL = 0, SDA = X
// exit: SCL = 0, SDA = X
void
i2c_bitbang::write_bit (bool bit)
{
set_sda (bit);
set_scl (1);
set_scl (0);
}
// write_byte:
// entry: SCL = 0, SDA = X
// exit: SCL = 0, SDA = 1
bool
i2c_bitbang::write_byte (char t)
{
int i;
bool ack_bit;
for (i = 0; i < 8; i++){
write_bit (t & 0x80);
t <<= 1;
}
// clock #9. This is the ACK bit.
set_sda (1); // tristate SDA
set_scl (1);
ack_bit = get_sda (); // slave should pull SDA line low
set_scl (0);
return ack_bit == 0;
}
// write: the high level entry point...
// entry: SCL = 1, SDA = 1
// exit: SCL = 1, SDA = 1
bool
i2c_bitbang::write (int addr, const unsigned char *buf, int nbytes)
{
bool ok = true;
d_io->lock ();
start ();
ok = write_byte ((addr << 1) | 0); // addr plus "read opcode"
for (int i = 0; i < nbytes; i++)
ok &= write_byte (buf[i]);
stop ();
d_io->unlock ();
return ok;
}
// read: the high level entry point...
// entry: SCL = 1, SDA = 1
// exit: SCL = 1, SDA = 1
int
i2c_bitbang::read (int addr, unsigned char *buf, int max_bytes)
{
d_io->lock ();
// FIXME
d_io->unlock ();
return -1;
}
|