From 4b7432ffd1cc3e21ac73bfc229f850c53ffdb2d4 Mon Sep 17 00:00:00 2001 From: ccli8 Date: Thu, 10 Nov 2016 16:19:47 +0800 Subject: [PATCH] Fix I2C issues 1. Fix error on return of i2c_byte_write(). 2. Fix error in zero-length transfer corner case. --- targets/TARGET_NUVOTON/TARGET_M451/i2c_api.c | 6 +++++- targets/TARGET_NUVOTON/TARGET_NUC472/i2c_api.c | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/targets/TARGET_NUVOTON/TARGET_M451/i2c_api.c b/targets/TARGET_NUVOTON/TARGET_M451/i2c_api.c index 02808b4933..bf1ee28ef6 100644 --- a/targets/TARGET_NUVOTON/TARGET_M451/i2c_api.c +++ b/targets/TARGET_NUVOTON/TARGET_M451/i2c_api.c @@ -221,7 +221,7 @@ int i2c_byte_read(i2c_t *obj, int last) int i2c_byte_write(i2c_t *obj, int data) { - return i2c_do_write(obj, (data & 0xFF), 0); + return i2c_do_write(obj, (data & 0xFF), 0) == 0 ? 1 : 0; } #if DEVICE_I2CSLAVE @@ -352,6 +352,10 @@ int i2c_allow_powerdown(void) static int i2c_do_tran(i2c_t *obj, char *buf, int length, int read, int naklastdata) { + if (! buf || ! length) { + return 0; + } + int tran_len = 0; i2c_disable_int(obj); diff --git a/targets/TARGET_NUVOTON/TARGET_NUC472/i2c_api.c b/targets/TARGET_NUVOTON/TARGET_NUC472/i2c_api.c index 2bc453cc60..73fd4f646e 100644 --- a/targets/TARGET_NUVOTON/TARGET_NUC472/i2c_api.c +++ b/targets/TARGET_NUVOTON/TARGET_NUC472/i2c_api.c @@ -238,7 +238,7 @@ int i2c_byte_read(i2c_t *obj, int last) int i2c_byte_write(i2c_t *obj, int data) { - return i2c_do_write(obj, (data & 0xFF), 0); + return i2c_do_write(obj, (data & 0xFF), 0) == 0 ? 1 : 0; } #if DEVICE_I2CSLAVE @@ -369,6 +369,10 @@ int i2c_allow_powerdown(void) static int i2c_do_tran(i2c_t *obj, char *buf, int length, int read, int naklastdata) { + if (! buf || ! length) { + return 0; + } + int tran_len = 0; i2c_disable_int(obj);