-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathi2cBus.c
271 lines (239 loc) · 6.71 KB
/
i2cBus.c
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* $Id: i2cBus.c,v 1.3 2010/10/01 19:55:03 clivewebster Exp $
* Revision History
* ================
* $Log: i2cBus.c,v $
* Revision 1.3 2010/10/01 19:55:03 clivewebster
* Setting the bit rate only applies to a hardware master
*
* Revision 1.2 2010/10/01 15:28:11 clivewebster
* Refactored - merge master and slave into i2cBus.h
*
* Revision 1.1 2010/09/30 16:44:49 clivewebster
* Added
*
* ================
*
* Copyright (C) 2010 Clive Webster ([email protected])
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* File: i2cBus.c
* Created on: 24 Sep 2010
* Author: Clive Webster
*
* Implement the common functions for an abstract i2c bus
*
*/
#include "i2cBus.h"
#include "errors.h"
//--- Helper methods to redirect the call to the correct bus -----
void i2cAbstractBusInit(I2C_ABSTRACT_BUS* i2c){
if(i2c && !i2c->initialised){
const I2C_CLASS* class = i2c->class;
// Put each device on this bus
for(uint8_t i=0; i<i2c->numDevices;i++){
I2C_DEVICE* device = (I2C_DEVICE*)pgm_read_word(&i2c->devices[i]);
if(device){
// Check for address conflicts
for(uint8_t j=i+1; j<i2c->numDevices;j++){
I2C_DEVICE* other = (I2C_DEVICE*)pgm_read_word(&i2c->devices[j]);
if(other){
if(device->addr == other->addr){
setError(I2C_DUP_ADDR);
return;
}
}
}
device->bus = i2c;
}
}
// Call the initialisation method for the actual class
void (*fn)(I2C_ABSTRACT_BUS*) = (void (*)(I2C_ABSTRACT_BUS*))pgm_read_word(&class->init);
if(fn){
fn(i2c); // initialise the bus
}
i2c->initialised = TRUE;
}
}
void i2cStop(const I2C_ABSTRACT_BUS* i2c){
if(i2c){
if(i2c->initialised){
const I2C_CLASS* class = i2c->class;
void (*fn)(const I2C_ABSTRACT_BUS*) =
(void (*)(const I2C_ABSTRACT_BUS*))pgm_read_word(&class->stop);
fn(i2c); // call the stop method
}else{
setError(I2C_BUS_NOT_INITIALIZED);
}
}
}
uint8_t i2cGet(const I2C_ABSTRACT_BUS* i2c, boolean isLastByte){
uint8_t rtn = 0;
if(i2c){
if(i2c->initialised){
const I2C_CLASS* class = i2c->class;
uint8_t (*fn)(const I2C_ABSTRACT_BUS*, boolean) =
(uint8_t (*)(const I2C_ABSTRACT_BUS*,boolean))pgm_read_word(&class->get);
rtn = fn(i2c, isLastByte); // call the get method
}else{
setError(I2C_BUS_NOT_INITIALIZED);
}
}
return rtn;
}
boolean i2cPut(const I2C_ABSTRACT_BUS* i2c, uint8_t b){
boolean rtn = FALSE;
if(i2c){
if(i2c->initialised){
const I2C_CLASS* class = i2c->class;
boolean (*fn)(const I2C_ABSTRACT_BUS*, uint8_t) =
(boolean (*)(const I2C_ABSTRACT_BUS*,uint8_t))pgm_read_word(&class->put);
rtn = fn(i2c, b); // call the put method
}else{
setError(I2C_BUS_NOT_INITIALIZED);
}
}
return rtn;
}
boolean i2cStart(const I2C_DEVICE* device, boolean writeMode){
boolean rtn = FALSE;
if(device){
const I2C_ABSTRACT_BUS* i2c = device->bus;
if(i2c){
if(i2c->initialised){
const I2C_CLASS* class = i2c->class;
boolean (*fn)(const I2C_DEVICE*,boolean) =
(boolean (*)(const I2C_DEVICE*,boolean))pgm_read_word(&class->start);
rtn = fn(device,writeMode); // start
}else{
setError(I2C_BUS_NOT_INITIALIZED);
}
}
}
return rtn;
}
void i2cAbstractSetBitrate(I2C_ABSTRACT_BUS* i2c,uint16_t bitrateKHz){
if(i2c){
const I2C_CLASS* class = i2c->class;
// Call the method for the actual class
void (*fn)(const I2C_ABSTRACT_BUS*,uint16_t) = (void (*)(const I2C_ABSTRACT_BUS*,uint16_t))pgm_read_word(&class->speed);
if(fn){
fn(i2c,bitrateKHz); // initialise the bus
}
}
}
// --- High level routines ----
boolean i2cMasterReceive(const I2C_DEVICE* device, size_t length, uint8_t *data){
boolean ack = FALSE;
if(device){
const I2C_ABSTRACT_BUS* i2c = device->bus;
ack = i2cStart(device,FALSE);
if(ack){
// receive data bytes
while(length--){
*data++ = i2cGet(i2c, (length) ? FALSE : TRUE);
}
}
i2cStop(i2c); // Send stop bit
}
return ack;
}
boolean i2cMasterSend(const I2C_DEVICE* device, size_t length, const uint8_t *data){
boolean ack = FALSE;
if(device){
const I2C_ABSTRACT_BUS* i2c = device->bus;
ack = i2cStart(device,TRUE);
if(ack){
// send the data
while(ack && length--){
ack &= i2cPut(i2c,*data++);
}
}
i2cStop(i2c);
}
return ack;
}
boolean i2cMasterTransfer(const I2C_DEVICE* device, size_t wlen, const uint8_t *wdata, size_t rlen, uint8_t * rdata){
boolean ack = false;
if(device){
const I2C_ABSTRACT_BUS* i2c = device->bus;
// Write the data
ack = i2cStart(device,TRUE);
if(ack){
// send the data
while(ack && wlen--){
ack &= i2cPut(i2c,*wdata++);
}
}
// Read the response
if(ack){
ack = i2cStart(device,FALSE); // repeated start
}
if(ack){
// Read the data
while(rlen--){
*rdata++ = i2cGet(i2c, (rlen) ? FALSE : TRUE);
}
}
// Stop
i2cStop(i2c);
}
return ack;
}
boolean i2cMasterSendWithPrefix(const I2C_DEVICE* device, size_t prefixLen, const uint8_t* prefix, size_t length, const uint8_t* data){
boolean ack = FALSE;
if(device){
const I2C_ABSTRACT_BUS* i2c = device->bus;
ack = i2cStart(device,TRUE);
if(ack){
// Put the first block of data
while(ack && prefixLen--){
ack &= i2cPut(i2c,*prefix++);
}
// send the second block of data
while(ack && length--){
ack &= i2cPut(i2c,*data++);
}
}
i2cStop(i2c);
}
return ack;
}
// Implement shorthand versions for any kind of i2c bus
boolean i2cMasterWriteRegisters(const I2C_DEVICE* device,uint8_t startReg, size_t numBytes, const uint8_t* data){
boolean ack = FALSE;
if(device){
const I2C_ABSTRACT_BUS* i2c = device->bus;
ack = i2cStart(device,TRUE);
if(ack){
// Put the first register number
ack &= i2cPut(i2c,startReg);
// send the data
while(ack && numBytes--){
ack &= i2cPut(i2c,*data++);
}
}
i2cStop(i2c);
}
return ack;
}
boolean i2cMasterReadRegisters(const I2C_DEVICE* device, uint8_t startReg, size_t numBytes, uint8_t* response){
return i2cMasterTransfer(device, 1, &startReg, numBytes, response);
}
boolean i2cMasterWriteRegister(const I2C_DEVICE* device, uint8_t reg, uint8_t value){
uint8_t data[2] = {reg,value};
return i2cMasterSend(device, sizeof(data), data);
}