i2c: Use u16 for data length

The length will never be negative.

Signed-off-by: Tim Crawford <tcrawford@system76.com>
This commit is contained in:
Tim Crawford
2021-08-02 11:56:47 -06:00
committed by Jeremy Soller
parent 784202383e
commit 2a3830fb57
4 changed files with 18 additions and 18 deletions

View File

@ -46,8 +46,8 @@ void i2c_stop(struct I2C * i2c) {
TWCR = BIT(TWINT) | BIT(TWEN) | BIT(TWSTO); TWCR = BIT(TWINT) | BIT(TWEN) | BIT(TWSTO);
} }
int16_t i2c_write(struct I2C * i2c, uint8_t * data, int16_t length) { int16_t i2c_write(struct I2C * i2c, uint8_t * data, uint16_t length) {
int16_t i; uint16_t i;
for (i = 0; i < length; i++) { for (i = 0; i < length; i++) {
// load data into data register // load data into data register
TWDR = data[i]; TWDR = data[i];
@ -65,8 +65,8 @@ int16_t i2c_write(struct I2C * i2c, uint8_t * data, int16_t length) {
return i; return i;
} }
int16_t i2c_read(struct I2C * i2c, uint8_t * data, int16_t length) { int16_t i2c_read(struct I2C * i2c, uint8_t * data, uint16_t length) {
int16_t i; uint16_t i;
for (i = 0; i < length; i++) { for (i = 0; i < length; i++) {
if ((i + 1) < length) { if ((i + 1) < length) {
// start TWI module and acknowledge data after reception // start TWI module and acknowledge data after reception

View File

@ -2,7 +2,7 @@
#include <common/i2c.h> #include <common/i2c.h>
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; int16_t res = 0;
res = i2c_start(i2c, addr, true); 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; 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; int16_t res = 0;
res = i2c_start(i2c, addr, false); 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; 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; int16_t res = 0;
res = i2c_start(i2c, addr, false); 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); 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; int16_t res = 0;
res = i2c_start(i2c, addr, false); res = i2c_start(i2c, addr, false);

View File

@ -24,22 +24,22 @@ void i2c_stop(struct I2C * i2c) __reentrant;
// Send a byte on i2c bus // Send a byte on i2c bus
// Must be defined by arch, board, or ec // 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 // Read bytes from bus
// Must be defined by arch, board, or ec // 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 // 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 // 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 // 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 // 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 #endif // _COMMON_I2C_H

View File

@ -94,8 +94,8 @@ void i2c_stop(struct I2C * i2c) {
i2c_reset(i2c, false); i2c_reset(i2c, false);
} }
static int16_t i2c_transaction(struct I2C * i2c, uint8_t * data, int16_t length, bool read) { static int16_t i2c_transaction(struct I2C * i2c, uint8_t * data, uint16_t length, bool read) {
int16_t i; uint16_t i;
for (i = 0; i < length; i++) { for (i = 0; i < length; i++) {
if (read) { if (read) {
// If last byte // If last byte
@ -153,10 +153,10 @@ static int16_t i2c_transaction(struct I2C * i2c, uint8_t * data, int16_t length,
return i; 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); 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); return i2c_transaction(i2c, data, length, false);
} }