Skip to content

Commit 0f7ece0

Browse files
committed
Fixed I2C API
1 parent df5c36a commit 0f7ece0

File tree

1 file changed

+23
-19
lines changed
  • libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX

1 file changed

+23
-19
lines changed

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX/i2c_api.c

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ static const PinMap PinMap_I2C_SCL[] = {
3535
#define I2C_SCLL(x, val) (x->i2c->SCLL = val)
3636
#define I2C_SCLH(x, val) (x->i2c->SCLH = val)
3737

38-
static const uint32_t I2C_addr_offset[4] = {
39-
0x0C, 0x20, 0x24, 0x28
38+
static const uint32_t I2C_addr_offset[2][4] = {
39+
{0x0C, 0x20, 0x24, 0x28},
40+
{0x30, 0x34, 0x38, 0x3C}
4041
};
4142

4243
static inline void i2c_conclr(i2c_t *obj, int start, int stop, int interrupt, int acknowledge) {
@@ -124,17 +125,18 @@ inline int i2c_start(i2c_t *obj) {
124125
}
125126

126127
inline int i2c_stop(i2c_t *obj) {
128+
int timeout = 0;
129+
127130
// write the stop bit
128-
int timeout = 0;
129131
i2c_conset(obj, 0, 1, 0, 0);
130132
i2c_clear_SI(obj);
131133

132134
// wait for STO bit to reset
133135
while(I2C_CONSET(obj) & (1 << 4)) {
134-
timeout++;
135-
if (timeout>100000) return 1;
136-
}
137-
136+
timeout ++;
137+
if (timeout > 100000) return 1;
138+
}
139+
138140
return 0;
139141
}
140142

@@ -201,13 +203,13 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
201203

202204
if ((status != 0x10) && (status != 0x08)) {
203205
i2c_stop(obj);
204-
return status;
206+
return I2C_ERROR_BUS_BUSY;
205207
}
206208

207209
status = i2c_do_write(obj, (address | 0x01), 1);
208210
if (status != 0x40) {
209211
i2c_stop(obj);
210-
return status;
212+
return I2C_ERROR_NO_SLAVE;
211213
}
212214

213215
// Read in all except last byte
@@ -216,7 +218,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
216218
status = i2c_status(obj);
217219
if (status != 0x50) {
218220
i2c_stop(obj);
219-
return status;
221+
return count;
220222
}
221223
data[count] = (char) value;
222224
}
@@ -226,7 +228,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
226228
status = i2c_status(obj);
227229
if (status != 0x58) {
228230
i2c_stop(obj);
229-
return status;
231+
return length - 1;
230232
}
231233

232234
data[count] = (char) value;
@@ -236,7 +238,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
236238
i2c_stop(obj);
237239
}
238240

239-
return 0;
241+
return length;
240242
}
241243

242244
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
@@ -246,31 +248,33 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
246248

247249
if ((status != 0x10) && (status != 0x08)) {
248250
i2c_stop(obj);
249-
return status;
251+
return I2C_ERROR_BUS_BUSY;
250252
}
251253

252254
status = i2c_do_write(obj, (address & 0xFE), 1);
253255
if (status != 0x18) {
254256
i2c_stop(obj);
255-
return status;
257+
return I2C_ERROR_NO_SLAVE;
256258
}
257259

258260
for (i=0; i<length; i++) {
259261
status = i2c_do_write(obj, data[i], 0);
260262
if(status != 0x28) {
261263
i2c_stop(obj);
262-
return status;
264+
return i;
263265
}
264266
}
265267

266-
i2c_clear_SI(obj);
268+
// clearing the serial interrupt here might cause an unintended rewrite of the last byte
269+
// see also issue report https://mbed.org/users/mbed_official/code/mbed/issues/1
270+
// i2c_clear_SI(obj);
267271

268272
// If not repeated start, send stop.
269273
if (stop) {
270274
i2c_stop(obj);
271275
}
272276

273-
return 0;
277+
return length;
274278
}
275279

276280
void i2c_reset(i2c_t *obj) {
@@ -348,7 +352,7 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
348352

349353
i2c_clear_SI(obj);
350354

351-
return (count - 1);
355+
return count;
352356
}
353357

354358
int i2c_slave_write(i2c_t *obj, const char *data, int length) {
@@ -377,7 +381,7 @@ void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
377381
uint32_t addr;
378382

379383
if ((idx >= 0) && (idx <= 3)) {
380-
addr = ((uint32_t)obj->i2c) + I2C_addr_offset[idx];
384+
addr = ((uint32_t)obj->i2c) + I2C_addr_offset[0][idx];
381385
*((uint32_t *) addr) = address & 0xFF;
382386
}
383387
}

0 commit comments

Comments
 (0)