Skip to content

Commit

Permalink
Fix NuMaker I2C timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
cyliangtw committed Sep 29, 2020
1 parent f38aa59 commit 1553c7a
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 14 deletions.
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 @@ -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 @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
}
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 @@ -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 @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
}
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

0 comments on commit 1553c7a

Please sign in to comment.