diff --git a/targets/TARGET_NUVOTON/TARGET_M2351/i2c_api.c b/targets/TARGET_NUVOTON/TARGET_M2351/i2c_api.c index 73878f9bb52e..e568fbe9bedf 100644 --- a/targets/TARGET_NUVOTON/TARGET_M2351/i2c_api.c +++ b/targets/TARGET_NUVOTON/TARGET_M2351/i2c_api.c @@ -27,6 +27,7 @@ #include "nu_miscutil.h" #include "nu_bitutil.h" #include "mbed_critical.h" +#include "us_ticker_api.h" struct nu_i2c_var { i2c_t * obj; @@ -465,6 +466,8 @@ static int i2c_poll_status_timeout(i2c_t *obj, int (*is_status)(i2c_t *obj), uin { uint32_t t1, t2, elapsed = 0; int status_assert = 0; + const uint32_t bits = us_ticker_get_info()->bits; + const uint32_t mask = (1 << bits) - 1; t1 = us_ticker_read(); while (1) { @@ -474,7 +477,7 @@ static int i2c_poll_status_timeout(i2c_t *obj, int (*is_status)(i2c_t *obj), uin } t2 = us_ticker_read(); - elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + 0xFFFFFFFF - t1 + 1); + elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + mask - t1 + 1); if (elapsed >= timeout) { break; } @@ -489,6 +492,8 @@ static int i2c_poll_tran_heatbeat_timeout(i2c_t *obj, uint32_t timeout) int tran_started; char *tran_pos = NULL; char *tran_pos2 = NULL; + const uint32_t bits = us_ticker_get_info()->bits; + const uint32_t mask = (1 << bits) - 1; i2c_disable_int(obj); tran_pos = obj->i2c.tran_pos; @@ -512,7 +517,7 @@ static int i2c_poll_tran_heatbeat_timeout(i2c_t *obj, uint32_t timeout) continue; } - elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + 0xFFFFFFFF - t1 + 1); + elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + mask - t1 + 1); if (elapsed >= timeout) { // Transfer idle break; } diff --git a/targets/TARGET_NUVOTON/TARGET_M251/i2c_api.c b/targets/TARGET_NUVOTON/TARGET_M251/i2c_api.c index 3652ca3dde84..b07e088742af 100644 --- a/targets/TARGET_NUVOTON/TARGET_M251/i2c_api.c +++ b/targets/TARGET_NUVOTON/TARGET_M251/i2c_api.c @@ -27,6 +27,7 @@ #include "nu_miscutil.h" #include "nu_bitutil.h" #include "mbed_critical.h" +#include "us_ticker_api.h" struct nu_i2c_var { i2c_t * obj; @@ -450,6 +451,8 @@ static int i2c_poll_status_timeout(i2c_t *obj, int (*is_status)(i2c_t *obj), uin { uint32_t t1, t2, elapsed = 0; int status_assert = 0; + const uint32_t bits = us_ticker_get_info()->bits; + const uint32_t mask = (1 << bits) - 1; t1 = us_ticker_read(); while (1) { @@ -459,7 +462,7 @@ static int i2c_poll_status_timeout(i2c_t *obj, int (*is_status)(i2c_t *obj), uin } t2 = us_ticker_read(); - elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + 0xFFFFFFFF - t1 + 1); + elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + mask - t1 + 1); if (elapsed >= timeout) { break; } @@ -474,6 +477,8 @@ static int i2c_poll_tran_heatbeat_timeout(i2c_t *obj, uint32_t timeout) int tran_started; char *tran_pos = NULL; char *tran_pos2 = NULL; + const uint32_t bits = us_ticker_get_info()->bits; + const uint32_t mask = (1 << bits) - 1; i2c_disable_int(obj); tran_pos = obj->i2c.tran_pos; @@ -497,7 +502,7 @@ static int i2c_poll_tran_heatbeat_timeout(i2c_t *obj, uint32_t timeout) continue; } - elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + 0xFFFFFFFF - t1 + 1); + elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + mask - t1 + 1); if (elapsed >= timeout) { // Transfer idle break; } diff --git a/targets/TARGET_NUVOTON/TARGET_M261/i2c_api.c b/targets/TARGET_NUVOTON/TARGET_M261/i2c_api.c index 4075f57d6d14..16b5a445ecfa 100644 --- a/targets/TARGET_NUVOTON/TARGET_M261/i2c_api.c +++ b/targets/TARGET_NUVOTON/TARGET_M261/i2c_api.c @@ -26,6 +26,7 @@ #include "nu_miscutil.h" #include "nu_bitutil.h" #include "mbed_critical.h" +#include "us_ticker_api.h" struct nu_i2c_var { i2c_t * obj; @@ -455,6 +456,8 @@ static int i2c_poll_status_timeout(i2c_t *obj, int (*is_status)(i2c_t *obj), uin { uint32_t t1, t2, elapsed = 0; int status_assert = 0; + const uint32_t bits = us_ticker_get_info()->bits; + const uint32_t mask = (1 << bits) - 1; t1 = us_ticker_read(); while (1) { @@ -464,7 +467,7 @@ static int i2c_poll_status_timeout(i2c_t *obj, int (*is_status)(i2c_t *obj), uin } t2 = us_ticker_read(); - elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + 0xFFFFFFFF - t1 + 1); + elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + mask - t1 + 1); if (elapsed >= timeout) { break; } @@ -479,6 +482,8 @@ static int i2c_poll_tran_heatbeat_timeout(i2c_t *obj, uint32_t timeout) int tran_started; char *tran_pos = NULL; char *tran_pos2 = NULL; + const uint32_t bits = us_ticker_get_info()->bits; + const uint32_t mask = (1 << bits) - 1; i2c_disable_int(obj); tran_pos = obj->i2c.tran_pos; @@ -502,7 +507,7 @@ static int i2c_poll_tran_heatbeat_timeout(i2c_t *obj, uint32_t timeout) continue; } - elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + 0xFFFFFFFF - t1 + 1); + elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + mask - t1 + 1); if (elapsed >= timeout) { // Transfer idle break; } diff --git a/targets/TARGET_NUVOTON/TARGET_M451/i2c_api.c b/targets/TARGET_NUVOTON/TARGET_M451/i2c_api.c index 23ee8a7fd705..b2e5d4395338 100644 --- a/targets/TARGET_NUVOTON/TARGET_M451/i2c_api.c +++ b/targets/TARGET_NUVOTON/TARGET_M451/i2c_api.c @@ -25,6 +25,7 @@ #include "nu_miscutil.h" #include "nu_bitutil.h" #include "mbed_critical.h" +#include "us_ticker_api.h" #define NU_I2C_DEBUG 0 @@ -480,6 +481,8 @@ static int i2c_poll_status_timeout(i2c_t *obj, int (*is_status)(i2c_t *obj), uin { uint32_t t1, t2, elapsed = 0; int status_assert = 0; + const uint32_t bits = us_ticker_get_info()->bits; + const uint32_t mask = (1 << bits) - 1; t1 = us_ticker_read(); while (1) { @@ -489,7 +492,7 @@ static int i2c_poll_status_timeout(i2c_t *obj, int (*is_status)(i2c_t *obj), uin } t2 = us_ticker_read(); - elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + 0xFFFFFFFF - t1 + 1); + elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + mask - t1 + 1); if (elapsed >= timeout) { #if NU_I2C_DEBUG MY_I2C_T1 = t1; @@ -512,6 +515,8 @@ static int i2c_poll_tran_heatbeat_timeout(i2c_t *obj, uint32_t timeout) int tran_started; char *tran_pos = NULL; char *tran_pos2 = NULL; + const uint32_t bits = us_ticker_get_info()->bits; + const uint32_t mask = (1 << bits) - 1; i2c_disable_int(obj); tran_pos = obj->i2c.tran_pos; @@ -535,7 +540,7 @@ static int i2c_poll_tran_heatbeat_timeout(i2c_t *obj, uint32_t timeout) continue; } - elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + 0xFFFFFFFF - t1 + 1); + elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + mask - t1 + 1); if (elapsed >= timeout) { // Transfer idle #if NU_I2C_DEBUG MY_I2C = obj->i2c; diff --git a/targets/TARGET_NUVOTON/TARGET_M480/i2c_api.c b/targets/TARGET_NUVOTON/TARGET_M480/i2c_api.c index dbad846269b6..66dae37473fc 100644 --- a/targets/TARGET_NUVOTON/TARGET_M480/i2c_api.c +++ b/targets/TARGET_NUVOTON/TARGET_M480/i2c_api.c @@ -28,6 +28,7 @@ #include "nu_miscutil.h" #include "nu_bitutil.h" #include "mbed_critical.h" +#include "us_ticker_api.h" struct nu_i2c_var { i2c_t * obj; @@ -457,6 +458,8 @@ static int i2c_poll_status_timeout(i2c_t *obj, int (*is_status)(i2c_t *obj), uin { uint32_t t1, t2, elapsed = 0; int status_assert = 0; + const uint32_t bits = us_ticker_get_info()->bits; + const uint32_t mask = (1 << bits) - 1; t1 = us_ticker_read(); while (1) { @@ -466,7 +469,7 @@ static int i2c_poll_status_timeout(i2c_t *obj, int (*is_status)(i2c_t *obj), uin } t2 = us_ticker_read(); - elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + 0xFFFFFFFF - t1 + 1); + elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + mask - t1 + 1); if (elapsed >= timeout) { break; } @@ -481,6 +484,8 @@ static int i2c_poll_tran_heatbeat_timeout(i2c_t *obj, uint32_t timeout) int tran_started; char *tran_pos = NULL; char *tran_pos2 = NULL; + const uint32_t bits = us_ticker_get_info()->bits; + const uint32_t mask = (1 << bits) - 1; i2c_disable_int(obj); tran_pos = obj->i2c.tran_pos; @@ -504,7 +509,7 @@ static int i2c_poll_tran_heatbeat_timeout(i2c_t *obj, uint32_t timeout) continue; } - elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + 0xFFFFFFFF - t1 + 1); + elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + mask - t1 + 1); if (elapsed >= timeout) { // Transfer idle break; } diff --git a/targets/TARGET_NUVOTON/TARGET_NANO100/i2c_api.c b/targets/TARGET_NUVOTON/TARGET_NANO100/i2c_api.c index 2d30a1fe3d0b..1d9022af0140 100644 --- a/targets/TARGET_NUVOTON/TARGET_NANO100/i2c_api.c +++ b/targets/TARGET_NUVOTON/TARGET_NANO100/i2c_api.c @@ -25,6 +25,7 @@ #include "nu_miscutil.h" #include "nu_bitutil.h" #include "mbed_critical.h" +#include "us_ticker_api.h" #define NU_I2C_DEBUG 0 @@ -500,6 +501,8 @@ static int i2c_poll_status_timeout(i2c_t *obj, int (*is_status)(i2c_t *obj), uin { uint32_t t1, t2, elapsed = 0; int status_assert = 0; + const uint32_t bits = us_ticker_get_info()->bits; + const uint32_t mask = (1 << bits) - 1; t1 = us_ticker_read(); while (1) { @@ -509,7 +512,7 @@ static int i2c_poll_status_timeout(i2c_t *obj, int (*is_status)(i2c_t *obj), uin } t2 = us_ticker_read(); - elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + 0xFFFFFFFF - t1 + 1); + elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + mask - t1 + 1); if (elapsed >= timeout) { #if NU_I2C_DEBUG MY_I2C_T1 = t1; @@ -532,6 +535,8 @@ static int i2c_poll_tran_heatbeat_timeout(i2c_t *obj, uint32_t timeout) int tran_started; char *tran_pos = NULL; char *tran_pos2 = NULL; + const uint32_t bits = us_ticker_get_info()->bits; + const uint32_t mask = (1 << bits) - 1; i2c_disable_int(obj); tran_pos = obj->i2c.tran_pos; @@ -555,7 +560,7 @@ static int i2c_poll_tran_heatbeat_timeout(i2c_t *obj, uint32_t timeout) continue; } - elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + 0xFFFFFFFF - t1 + 1); + elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + mask - t1 + 1); if (elapsed >= timeout) { // Transfer idle #if NU_I2C_DEBUG MY_I2C = obj->i2c; diff --git a/targets/TARGET_NUVOTON/TARGET_NUC472/i2c_api.c b/targets/TARGET_NUVOTON/TARGET_NUC472/i2c_api.c index 0c514d26deac..f9e2b96c5676 100644 --- a/targets/TARGET_NUVOTON/TARGET_NUC472/i2c_api.c +++ b/targets/TARGET_NUVOTON/TARGET_NUC472/i2c_api.c @@ -25,6 +25,7 @@ #include "nu_miscutil.h" #include "nu_bitutil.h" #include "mbed_critical.h" +#include "us_ticker_api.h" #define NU_I2C_DEBUG 0 @@ -497,6 +498,8 @@ static int i2c_poll_status_timeout(i2c_t *obj, int (*is_status)(i2c_t *obj), uin { uint32_t t1, t2, elapsed = 0; int status_assert = 0; + const uint32_t bits = us_ticker_get_info()->bits; + const uint32_t mask = (1 << bits) - 1; t1 = us_ticker_read(); while (1) { @@ -506,7 +509,7 @@ static int i2c_poll_status_timeout(i2c_t *obj, int (*is_status)(i2c_t *obj), uin } t2 = us_ticker_read(); - elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + 0xFFFFFFFF - t1 + 1); + elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + mask - t1 + 1); if (elapsed >= timeout) { #if NU_I2C_DEBUG MY_I2C_T1 = t1; @@ -529,6 +532,8 @@ static int i2c_poll_tran_heatbeat_timeout(i2c_t *obj, uint32_t timeout) int tran_started; char *tran_pos = NULL; char *tran_pos2 = NULL; + const uint32_t bits = us_ticker_get_info()->bits; + const uint32_t mask = (1 << bits) - 1; i2c_disable_int(obj); tran_pos = obj->i2c.tran_pos; @@ -552,7 +557,7 @@ static int i2c_poll_tran_heatbeat_timeout(i2c_t *obj, uint32_t timeout) continue; } - elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + 0xFFFFFFFF - t1 + 1); + elapsed = (t2 > t1) ? (t2 - t1) : ((uint64_t) t2 + mask - t1 + 1); if (elapsed >= timeout) { // Transfer idle #if NU_I2C_DEBUG MY_I2C = obj->i2c;