Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mbed-os-5.15: Nuvoton: Fix NuMaker I2C timeout #13709

Merged
merged 3 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions targets/TARGET_NUVOTON/TARGET_M2351/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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;
Expand Down Expand Up @@ -463,6 +464,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) {
Expand All @@ -472,7 +475,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;
}
Expand All @@ -487,6 +490,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;
Expand All @@ -510,7 +515,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;
}
Expand Down
9 changes: 7 additions & 2 deletions targets/TARGET_NUVOTON/TARGET_M2354/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -466,6 +467,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) {
Expand All @@ -475,7 +478,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;
}
Expand All @@ -490,6 +493,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;
Expand All @@ -513,7 +518,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;
}
Expand Down
9 changes: 7 additions & 2 deletions targets/TARGET_NUVOTON/TARGET_M251/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down
9 changes: 7 additions & 2 deletions targets/TARGET_NUVOTON/TARGET_M261/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down
9 changes: 7 additions & 2 deletions targets/TARGET_NUVOTON/TARGET_M451/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions targets/TARGET_NUVOTON/TARGET_M480/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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;
Expand Down Expand Up @@ -454,6 +455,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) {
Expand All @@ -463,7 +466,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;
}
Expand All @@ -478,6 +481,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;
Expand All @@ -501,7 +506,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;
}
Expand Down
9 changes: 7 additions & 2 deletions targets/TARGET_NUVOTON/TARGET_NANO100/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions targets/TARGET_NUVOTON/TARGET_NUC472/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down