Skip to content

Commit 7c8c722

Browse files
committed
Pushed LPC1114 I2C updates to the LPC11C24
1 parent b470ea0 commit 7c8c722

File tree

1 file changed

+25
-17
lines changed
  • libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11CXX

1 file changed

+25
-17
lines changed

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

Lines changed: 25 additions & 17 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,14 +125,19 @@ 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
128131
i2c_conset(obj, 0, 1, 0, 0);
129132
i2c_clear_SI(obj);
130133

131134
// wait for STO bit to reset
132-
while(I2C_CONSET(obj) & (1 << 4));
133-
134-
return 0;
135+
while(I2C_CONSET(obj) & (1 << 4)) {
136+
timeout ++;
137+
if (timeout > 100000) return 1;
138+
}
139+
140+
return 0;
135141
}
136142

137143

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

198204
if ((status != 0x10) && (status != 0x08)) {
199205
i2c_stop(obj);
200-
return status;
206+
return I2C_ERROR_BUS_BUSY;
201207
}
202208

203209
status = i2c_do_write(obj, (address | 0x01), 1);
204210
if (status != 0x40) {
205211
i2c_stop(obj);
206-
return status;
212+
return I2C_ERROR_NO_SLAVE;
207213
}
208214

209215
// Read in all except last byte
@@ -212,7 +218,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
212218
status = i2c_status(obj);
213219
if (status != 0x50) {
214220
i2c_stop(obj);
215-
return status;
221+
return count;
216222
}
217223
data[count] = (char) value;
218224
}
@@ -222,7 +228,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
222228
status = i2c_status(obj);
223229
if (status != 0x58) {
224230
i2c_stop(obj);
225-
return status;
231+
return length - 1;
226232
}
227233

228234
data[count] = (char) value;
@@ -232,7 +238,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
232238
i2c_stop(obj);
233239
}
234240

235-
return 0;
241+
return length;
236242
}
237243

238244
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
@@ -242,31 +248,33 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
242248

243249
if ((status != 0x10) && (status != 0x08)) {
244250
i2c_stop(obj);
245-
return status;
251+
return I2C_ERROR_BUS_BUSY;
246252
}
247253

248254
status = i2c_do_write(obj, (address & 0xFE), 1);
249255
if (status != 0x18) {
250256
i2c_stop(obj);
251-
return status;
257+
return I2C_ERROR_NO_SLAVE;
252258
}
253259

254260
for (i=0; i<length; i++) {
255261
status = i2c_do_write(obj, data[i], 0);
256262
if(status != 0x28) {
257263
i2c_stop(obj);
258-
return status;
264+
return i;
259265
}
260266
}
261267

262-
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);
263271

264272
// If not repeated start, send stop.
265273
if (stop) {
266274
i2c_stop(obj);
267275
}
268276

269-
return 0;
277+
return length;
270278
}
271279

272280
void i2c_reset(i2c_t *obj) {
@@ -344,7 +352,7 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
344352

345353
i2c_clear_SI(obj);
346354

347-
return (count - 1);
355+
return count;
348356
}
349357

350358
int i2c_slave_write(i2c_t *obj, const char *data, int length) {
@@ -373,7 +381,7 @@ void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
373381
uint32_t addr;
374382

375383
if ((idx >= 0) && (idx <= 3)) {
376-
addr = ((uint32_t)obj->i2c) + I2C_addr_offset[idx];
384+
addr = ((uint32_t)obj->i2c) + I2C_addr_offset[0][idx];
377385
*((uint32_t *) addr) = address & 0xFF;
378386
}
379387
}

0 commit comments

Comments
 (0)