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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2006 by Linus Nielsen Feltzing
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "config.h"
#include "cpu.h"
#include <stdbool.h>
#include <stdlib.h>
#include "debug.h"
#include "logf.h"
#include "generic_i2c.h"
int i2c_num_ifs = 0;
struct i2c_interface *i2c_if[5];
static void i2c_start(struct i2c_interface *iface)
{
iface->sda_hi();
iface->scl_hi();
iface->sda_lo();
iface->delay_hd_sta();
iface->scl_lo();
}
static void i2c_stop(struct i2c_interface *iface)
{
iface->sda_lo();
iface->scl_hi();
iface->delay_su_sto();
iface->sda_hi();
}
static void i2c_ack(struct i2c_interface *iface, bool ack)
{
iface->scl_lo();
if ( ack )
iface->sda_lo();
else
iface->sda_hi();
iface->delay_su_dat();
iface->scl_hi();
iface->delay_thigh();
iface->scl_lo();
}
static int i2c_getack(struct i2c_interface *iface)
{
int ret = 1;
iface->sda_input();
iface->delay_su_dat();
iface->scl_hi();
if (iface->sda())
ret = 0; /* ack failed */
iface->delay_thigh();
iface->scl_lo();
iface->sda_hi();
iface->sda_output();
iface->delay_hd_dat();
return ret;
}
static unsigned char i2c_inb(struct i2c_interface *iface, bool ack)
{
int i;
unsigned char byte = 0;
/* clock in each bit, MSB first */
for ( i=0x80; i; i>>=1 ) {
iface->sda_input();
iface->scl_hi();
iface->delay_thigh();
if (iface->sda())
byte |= i;
iface->scl_lo();
iface->delay_hd_dat();
iface->sda_output();
}
i2c_ack(iface, ack);
return byte;
}
static void i2c_outb(struct i2c_interface *iface, unsigned char byte)
{
int i;
/* clock out each bit, MSB first */
for (i=0x80; i; i>>=1) {
if (i & byte)
iface->sda_hi();
else
iface->sda_lo();
iface->delay_su_dat();
iface->scl_hi();
iface->delay_thigh();
iface->scl_lo();
iface->delay_hd_dat();
}
iface->sda_hi();
}
static struct i2c_interface *find_if(int address)
{
int i;
for(i = 0;i < i2c_num_ifs;i++) {
if(i2c_if[i]->address == address)
return i2c_if[i];
}
return NULL;
}
int i2c_write_data(int bus_address, int address,
const unsigned char* buf, int count)
{
int i;
int ret = 0;
struct i2c_interface *iface = find_if(bus_address);
if(!iface)
return -1;
i2c_start(iface);
i2c_outb(iface, iface->address & 0xfe);
if (i2c_getack(iface)) {
i2c_outb(iface, address);
if (i2c_getack(iface)) {
for(i = 0;i < count;i++) {
i2c_outb(iface, buf[i]);
if (!i2c_getack(iface)) {
ret = -3;
break;
}
}
} else {
ret = -2;
}
} else {
logf("i2c_write_data() - no ack\n");
ret = -1;
}
i2c_stop(iface);
return ret;
}
int i2c_read_data(int bus_address, int address,
unsigned char* buf, int count)
{
int i;
int ret = 0;
struct i2c_interface *iface = find_if(bus_address);
if(!iface)
return -1;
i2c_start(iface);
i2c_outb(iface, iface->address & 0xfe);
if (i2c_getack(iface)) {
i2c_outb(iface, address);
if (i2c_getack(iface)) {
i2c_start(iface);
i2c_outb(iface, iface->address | 1);
if (i2c_getack(iface)) {
for(i = 0;i < count-1;i++)
buf[i] = i2c_inb(iface, true);
buf[i] = i2c_inb(iface, false);
} else {
ret = -3;
}
} else {
ret = -2;
}
} else {
ret = -1;
}
i2c_stop(iface);
return ret;
}
void i2c_add_node(struct i2c_interface *iface)
{
i2c_if[i2c_num_ifs++] = iface;
}
|