Skip to content

Commit 0e330ef

Browse files
committed
[MAX326xx] Fixed I2C bug.
1 parent 8d36877 commit 0e330ef

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

hal/targets/hal/TARGET_Maxim/TARGET_MAX32600/i2c_api.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* ownership rights.
3131
*******************************************************************************
3232
*/
33-
33+
3434
#include "mbed_assert.h"
3535
#include "i2c_api.h"
3636
#include "cmsis.h"
@@ -230,14 +230,16 @@ int i2c_byte_write(i2c_t *obj, int data)
230230
// Wait for the FIFO to be empty
231231
while(!(obj->i2c->intfl & MXC_F_I2CM_INTFL_TX_FIFO_EMPTY)) {}
232232

233-
if(obj->i2c->intfl & MXC_F_I2CM_INTFL_TX_NACKED) {
233+
if (obj->i2c->intfl & MXC_F_I2CM_INTFL_TX_NACKED) {
234+
i2c_reset(obj);
234235
return 1;
235236
}
236237

237-
if(obj->i2c->intfl & (MXC_F_I2CM_INTFL_TX_TIMEOUT | MXC_F_I2CM_INTFL_TX_LOST_ARBITR)) {
238+
if (obj->i2c->intfl & (MXC_F_I2CM_INTFL_TX_TIMEOUT | MXC_F_I2CM_INTFL_TX_LOST_ARBITR)) {
239+
i2c_reset(obj);
238240
return 2;
239241
}
240-
242+
241243
return 0;
242244
}
243245

@@ -256,6 +258,7 @@ int i2c_byte_read(i2c_t *obj, int last)
256258
}
257259

258260
if ((err = write_tx_fifo(obj, fifo_value)) != 0) {
261+
i2c_reset(obj);
259262
return err;
260263
}
261264

@@ -264,7 +267,8 @@ int i2c_byte_read(i2c_t *obj, int last)
264267
int timeout = MXC_I2CM_RX_TIMEOUT;
265268
while (!(obj->i2c->intfl & MXC_F_I2CM_INTFL_RX_FIFO_NOT_EMPTY) &&
266269
(!(obj->i2c->bb & MXC_F_I2CM_BB_RX_FIFO_CNT))) {
267-
if ((--timeout < 0) || !(obj->i2c->trans & MXC_F_I2CM_TRANS_TX_IN_PROGRESS)) {
270+
if ((--timeout < 0) || (obj->i2c->trans & (MXC_F_I2CM_TRANS_TX_TIMEOUT |
271+
MXC_F_I2CM_TRANS_TX_LOST_ARBITR | MXC_F_I2CM_TRANS_TX_NACKED))) {
268272
break;
269273
}
270274
}
@@ -274,6 +278,8 @@ int i2c_byte_read(i2c_t *obj, int last)
274278
return *obj->rxfifo;
275279
}
276280

281+
i2c_reset(obj);
282+
277283
return -1;
278284
}
279285

@@ -291,6 +297,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
291297

292298
// write the address to the fifo
293299
if ((err = write_tx_fifo(obj, (MXC_S_I2CM_TRANS_TAG_START | address))) != 0) { // start + addr (write)
300+
i2c_reset(obj);
294301
return err;
295302
}
296303
obj->start_pending = 0;
@@ -319,13 +326,15 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
319326
obj->stop_pending = 1;
320327
int timeout = MXC_I2CM_TX_TIMEOUT;
321328
// Wait for TX fifo to be empty
322-
while(!(obj->i2c->intfl & MXC_F_I2CM_INTFL_TX_FIFO_EMPTY) && timeout--) {}
329+
while (!(obj->i2c->intfl & MXC_F_I2CM_INTFL_TX_FIFO_EMPTY) && timeout--);
323330
}
324331

325332
if (retval == 0) {
326333
return length;
327334
}
328335

336+
i2c_reset(obj);
337+
329338
return retval;
330339
}
331340

@@ -379,7 +388,8 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
379388
while (i < length) {
380389
while (!(obj->i2c->intfl & MXC_F_I2CM_INTFL_RX_FIFO_NOT_EMPTY) &&
381390
(!(obj->i2c->bb & MXC_F_I2CM_BB_RX_FIFO_CNT))) {
382-
if ((--timeout < 0) || !(obj->i2c->trans & MXC_F_I2CM_TRANS_TX_IN_PROGRESS)) {
391+
if ((--timeout < 0) || (obj->i2c->trans & (MXC_F_I2CM_TRANS_TX_TIMEOUT |
392+
MXC_F_I2CM_TRANS_TX_LOST_ARBITR | MXC_F_I2CM_TRANS_TX_NACKED))) {
383393
retval = -3;
384394
goto read_done;
385395
}
@@ -412,5 +422,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
412422
return length;
413423
}
414424

425+
i2c_reset(obj);
426+
415427
return retval;
416428
}

hal/targets/hal/TARGET_Maxim/TARGET_MAX32610/i2c_api.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,13 @@ int i2c_byte_write(i2c_t *obj, int data)
230230
// Wait for the FIFO to be empty
231231
while(!(obj->i2c->intfl & MXC_F_I2CM_INTFL_TX_FIFO_EMPTY)) {}
232232

233-
if(obj->i2c->intfl & MXC_F_I2CM_INTFL_TX_NACKED) {
233+
if (obj->i2c->intfl & MXC_F_I2CM_INTFL_TX_NACKED) {
234+
i2c_reset(obj);
234235
return 1;
235236
}
236237

237-
if(obj->i2c->intfl & (MXC_F_I2CM_INTFL_TX_TIMEOUT | MXC_F_I2CM_INTFL_TX_LOST_ARBITR)) {
238+
if (obj->i2c->intfl & (MXC_F_I2CM_INTFL_TX_TIMEOUT | MXC_F_I2CM_INTFL_TX_LOST_ARBITR)) {
239+
i2c_reset(obj);
238240
return 2;
239241
}
240242

@@ -256,6 +258,7 @@ int i2c_byte_read(i2c_t *obj, int last)
256258
}
257259

258260
if ((err = write_tx_fifo(obj, fifo_value)) != 0) {
261+
i2c_reset(obj);
259262
return err;
260263
}
261264

@@ -264,7 +267,8 @@ int i2c_byte_read(i2c_t *obj, int last)
264267
int timeout = MXC_I2CM_RX_TIMEOUT;
265268
while (!(obj->i2c->intfl & MXC_F_I2CM_INTFL_RX_FIFO_NOT_EMPTY) &&
266269
(!(obj->i2c->bb & MXC_F_I2CM_BB_RX_FIFO_CNT))) {
267-
if ((--timeout < 0) || !(obj->i2c->trans & MXC_F_I2CM_TRANS_TX_IN_PROGRESS)) {
270+
if ((--timeout < 0) || (obj->i2c->trans & (MXC_F_I2CM_TRANS_TX_TIMEOUT |
271+
MXC_F_I2CM_TRANS_TX_LOST_ARBITR | MXC_F_I2CM_TRANS_TX_NACKED))) {
268272
break;
269273
}
270274
}
@@ -274,6 +278,8 @@ int i2c_byte_read(i2c_t *obj, int last)
274278
return *obj->rxfifo;
275279
}
276280

281+
i2c_reset(obj);
282+
277283
return -1;
278284
}
279285

@@ -291,6 +297,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
291297

292298
// write the address to the fifo
293299
if ((err = write_tx_fifo(obj, (MXC_S_I2CM_TRANS_TAG_START | address))) != 0) { // start + addr (write)
300+
i2c_reset(obj);
294301
return err;
295302
}
296303
obj->start_pending = 0;
@@ -319,13 +326,15 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
319326
obj->stop_pending = 1;
320327
int timeout = MXC_I2CM_TX_TIMEOUT;
321328
// Wait for TX fifo to be empty
322-
while(!(obj->i2c->intfl & MXC_F_I2CM_INTFL_TX_FIFO_EMPTY) && timeout--) {}
329+
while (!(obj->i2c->intfl & MXC_F_I2CM_INTFL_TX_FIFO_EMPTY) && timeout--);
323330
}
324331

325332
if (retval == 0) {
326333
return length;
327334
}
328335

336+
i2c_reset(obj);
337+
329338
return retval;
330339
}
331340

@@ -379,7 +388,8 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
379388
while (i < length) {
380389
while (!(obj->i2c->intfl & MXC_F_I2CM_INTFL_RX_FIFO_NOT_EMPTY) &&
381390
(!(obj->i2c->bb & MXC_F_I2CM_BB_RX_FIFO_CNT))) {
382-
if ((--timeout < 0) || !(obj->i2c->trans & MXC_F_I2CM_TRANS_TX_IN_PROGRESS)) {
391+
if ((--timeout < 0) || (obj->i2c->trans & (MXC_F_I2CM_TRANS_TX_TIMEOUT |
392+
MXC_F_I2CM_TRANS_TX_LOST_ARBITR | MXC_F_I2CM_TRANS_TX_NACKED))) {
383393
retval = -3;
384394
goto read_done;
385395
}
@@ -412,5 +422,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
412422
return length;
413423
}
414424

425+
i2c_reset(obj);
426+
415427
return retval;
416428
}

0 commit comments

Comments
 (0)