Skip to content

Commit 39e2f31

Browse files
committed
Apply the same NuMaker I2C timeout measurement to M2354
1 parent 99e767b commit 39e2f31

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

targets/TARGET_NUVOTON/TARGET_M2354/i2c_api.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "nu_miscutil.h"
2929
#include "nu_bitutil.h"
3030
#include "mbed_critical.h"
31+
#include "us_ticker_api.h"
3132

3233
struct nu_i2c_var {
3334
i2c_t * obj;
@@ -466,6 +467,8 @@ static int i2c_poll_status_timeout(i2c_t *obj, int (*is_status)(i2c_t *obj), uin
466467
{
467468
uint32_t t1, t2, elapsed = 0;
468469
int status_assert = 0;
470+
const uint32_t bits = us_ticker_get_info()->bits;
471+
const uint32_t mask = (1 << bits) - 1;
469472

470473
t1 = us_ticker_read();
471474
while (1) {
@@ -475,7 +478,7 @@ static int i2c_poll_status_timeout(i2c_t *obj, int (*is_status)(i2c_t *obj), uin
475478
}
476479

477480
t2 = us_ticker_read();
478-
elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + 0xFFFFFFFF - t1 + 1);
481+
elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + mask - t1 + 1);
479482
if (elapsed >= timeout) {
480483
break;
481484
}
@@ -490,6 +493,8 @@ static int i2c_poll_tran_heatbeat_timeout(i2c_t *obj, uint32_t timeout)
490493
int tran_started;
491494
char *tran_pos = NULL;
492495
char *tran_pos2 = NULL;
496+
const uint32_t bits = us_ticker_get_info()->bits;
497+
const uint32_t mask = (1 << bits) - 1;
493498

494499
i2c_disable_int(obj);
495500
tran_pos = obj->i2c.tran_pos;
@@ -513,7 +518,7 @@ static int i2c_poll_tran_heatbeat_timeout(i2c_t *obj, uint32_t timeout)
513518
continue;
514519
}
515520

516-
elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + 0xFFFFFFFF - t1 + 1);
521+
elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + mask - t1 + 1);
517522
if (elapsed >= timeout) { // Transfer idle
518523
break;
519524
}

0 commit comments

Comments
 (0)