From 2a3830fb576d0b574b06ce1080330d0b64db6b2d Mon Sep 17 00:00:00 2001 From: Tim Crawford Date: Mon, 2 Aug 2021 11:56:47 -0600 Subject: [PATCH] i2c: Use u16 for data length The length will never be negative. Signed-off-by: Tim Crawford --- src/arch/avr/i2c.c | 8 ++++---- src/common/i2c.c | 8 ++++---- src/common/include/common/i2c.h | 12 ++++++------ src/ec/ite/i2c.c | 8 ++++---- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/arch/avr/i2c.c b/src/arch/avr/i2c.c index d22752e..1cf2e7e 100644 --- a/src/arch/avr/i2c.c +++ b/src/arch/avr/i2c.c @@ -46,8 +46,8 @@ void i2c_stop(struct I2C * i2c) { TWCR = BIT(TWINT) | BIT(TWEN) | BIT(TWSTO); } -int16_t i2c_write(struct I2C * i2c, uint8_t * data, int16_t length) { - int16_t i; +int16_t i2c_write(struct I2C * i2c, uint8_t * data, uint16_t length) { + uint16_t i; for (i = 0; i < length; i++) { // load data into data register TWDR = data[i]; @@ -65,8 +65,8 @@ int16_t i2c_write(struct I2C * i2c, uint8_t * data, int16_t length) { return i; } -int16_t i2c_read(struct I2C * i2c, uint8_t * data, int16_t length) { - int16_t i; +int16_t i2c_read(struct I2C * i2c, uint8_t * data, uint16_t length) { + uint16_t i; for (i = 0; i < length; i++) { if ((i + 1) < length) { // start TWI module and acknowledge data after reception diff --git a/src/common/i2c.c b/src/common/i2c.c index e35faa8..d6b8492 100644 --- a/src/common/i2c.c +++ b/src/common/i2c.c @@ -2,7 +2,7 @@ #include -int16_t i2c_recv(struct I2C * i2c, uint8_t addr, uint8_t* data, int16_t length) __reentrant { +int16_t i2c_recv(struct I2C * i2c, uint8_t addr, uint8_t* data, uint16_t length) __reentrant { int16_t res = 0; res = i2c_start(i2c, addr, true); @@ -16,7 +16,7 @@ int16_t i2c_recv(struct I2C * i2c, uint8_t addr, uint8_t* data, int16_t length) return res; } -int16_t i2c_send(struct I2C * i2c, uint8_t addr, uint8_t* data, int16_t length) __reentrant { +int16_t i2c_send(struct I2C * i2c, uint8_t addr, uint8_t* data, uint16_t length) __reentrant { int16_t res = 0; res = i2c_start(i2c, addr, false); @@ -30,7 +30,7 @@ int16_t i2c_send(struct I2C * i2c, uint8_t addr, uint8_t* data, int16_t length) return res; } -int16_t i2c_get(struct I2C * i2c, uint8_t addr, uint8_t reg, uint8_t* data, int16_t length) __reentrant { +int16_t i2c_get(struct I2C * i2c, uint8_t addr, uint8_t reg, uint8_t* data, uint16_t length) __reentrant { int16_t res = 0; res = i2c_start(i2c, addr, false); @@ -42,7 +42,7 @@ int16_t i2c_get(struct I2C * i2c, uint8_t addr, uint8_t reg, uint8_t* data, int1 return i2c_recv(i2c, addr, data, length); } -int16_t i2c_set(struct I2C * i2c, uint8_t addr, uint8_t reg, uint8_t* data, int16_t length) __reentrant { +int16_t i2c_set(struct I2C * i2c, uint8_t addr, uint8_t reg, uint8_t* data, uint16_t length) __reentrant { int16_t res = 0; res = i2c_start(i2c, addr, false); diff --git a/src/common/include/common/i2c.h b/src/common/include/common/i2c.h index 370630d..63f488c 100644 --- a/src/common/include/common/i2c.h +++ b/src/common/include/common/i2c.h @@ -24,22 +24,22 @@ void i2c_stop(struct I2C * i2c) __reentrant; // Send a byte on i2c bus // Must be defined by arch, board, or ec -int16_t i2c_write(struct I2C * i2c, uint8_t * data, int16_t length) __reentrant; +int16_t i2c_write(struct I2C * i2c, uint8_t * data, uint16_t length) __reentrant; // Read bytes from bus // Must be defined by arch, board, or ec -int16_t i2c_read(struct I2C * i2c, uint8_t * data, int16_t length) __reentrant; +int16_t i2c_read(struct I2C * i2c, uint8_t * data, uint16_t length) __reentrant; // Read multiple bytes from address in one transaction -int16_t i2c_recv(struct I2C * i2c, uint8_t addr, uint8_t* data, int16_t length) __reentrant; +int16_t i2c_recv(struct I2C * i2c, uint8_t addr, uint8_t* data, uint16_t length) __reentrant; // Write multiple bytes to address in one transaction -int16_t i2c_send(struct I2C * i2c, uint8_t addr, uint8_t* data, int16_t length) __reentrant; +int16_t i2c_send(struct I2C * i2c, uint8_t addr, uint8_t* data, uint16_t length) __reentrant; // Read multiple bytes from a register in one transaction -int16_t i2c_get(struct I2C * i2c, uint8_t addr, uint8_t reg, uint8_t* data, int16_t length) __reentrant; +int16_t i2c_get(struct I2C * i2c, uint8_t addr, uint8_t reg, uint8_t* data, uint16_t length) __reentrant; // Write multiple bytes to a register in one transaction -int16_t i2c_set(struct I2C * i2c, uint8_t addr, uint8_t reg, uint8_t* data, int16_t length) __reentrant; +int16_t i2c_set(struct I2C * i2c, uint8_t addr, uint8_t reg, uint8_t* data, uint16_t length) __reentrant; #endif // _COMMON_I2C_H diff --git a/src/ec/ite/i2c.c b/src/ec/ite/i2c.c index 51c7caa..a75bd05 100644 --- a/src/ec/ite/i2c.c +++ b/src/ec/ite/i2c.c @@ -94,8 +94,8 @@ void i2c_stop(struct I2C * i2c) { i2c_reset(i2c, false); } -static int16_t i2c_transaction(struct I2C * i2c, uint8_t * data, int16_t length, bool read) { - int16_t i; +static int16_t i2c_transaction(struct I2C * i2c, uint8_t * data, uint16_t length, bool read) { + uint16_t i; for (i = 0; i < length; i++) { if (read) { // If last byte @@ -153,10 +153,10 @@ static int16_t i2c_transaction(struct I2C * i2c, uint8_t * data, int16_t length, return i; } -int16_t i2c_read(struct I2C * i2c, uint8_t * data, int16_t length) __reentrant { +int16_t i2c_read(struct I2C * i2c, uint8_t * data, uint16_t length) __reentrant { return i2c_transaction(i2c, data, length, true); } -int16_t i2c_write(struct I2C * i2c, uint8_t * data, int16_t length) __reentrant { +int16_t i2c_write(struct I2C * i2c, uint8_t * data, uint16_t length) __reentrant { return i2c_transaction(i2c, data, length, false); }