Skip to content
Snippets Groups Projects
Commit 1540ec05 authored by Sebastian Meiling's avatar Sebastian Meiling
Browse files

nrf51: fix returns and error codes in periph/i2c

parent ba6f2a4c
No related branches found
No related tags found
No related merge requests found
...@@ -67,9 +67,13 @@ static int error(i2c_t bus) ...@@ -67,9 +67,13 @@ static int error(i2c_t bus)
static int write(i2c_t bus, uint8_t addr, const void *data, int len, int stop) static int write(i2c_t bus, uint8_t addr, const void *data, int len, int stop)
{ {
assert(len > 0);
if (!(bus < I2C_NUMOF)) {
return -1;
}
uint8_t *buf = (uint8_t *)data; uint8_t *buf = (uint8_t *)data;
assert((bus <= I2C_NUMOF) && (len > 0));
DEBUG("i2c: writing %i byte to the bus\n", len); DEBUG("i2c: writing %i byte to the bus\n", len);
dev(bus)->ADDRESS = (addr & 0x7f); dev(bus)->ADDRESS = (addr & 0x7f);
...@@ -98,7 +102,7 @@ static int write(i2c_t bus, uint8_t addr, const void *data, int len, int stop) ...@@ -98,7 +102,7 @@ static int write(i2c_t bus, uint8_t addr, const void *data, int len, int stop)
int i2c_init_master(i2c_t bus, i2c_speed_t speed) int i2c_init_master(i2c_t bus, i2c_speed_t speed)
{ {
if (bus >= I2C_NUMOF) { if (!(bus < I2C_NUMOF)) {
return -1; return -1;
} }
if (speed & INVALID_SPEED_MASK) { if (speed & INVALID_SPEED_MASK) {
...@@ -128,14 +132,18 @@ int i2c_init_master(i2c_t bus, i2c_speed_t speed) ...@@ -128,14 +132,18 @@ int i2c_init_master(i2c_t bus, i2c_speed_t speed)
int i2c_acquire(i2c_t bus) int i2c_acquire(i2c_t bus)
{ {
assert(bus <= I2C_NUMOF); if (!(bus < I2C_NUMOF)) {
return -1;
}
mutex_lock(&locks[bus]); mutex_lock(&locks[bus]);
return 0; return 0;
} }
int i2c_release(i2c_t bus) int i2c_release(i2c_t bus)
{ {
assert(bus <= I2C_NUMOF); if (!(bus < I2C_NUMOF)) {
return -1;
}
mutex_unlock(&locks[bus]); mutex_unlock(&locks[bus]);
return 0; return 0;
} }
...@@ -147,9 +155,12 @@ int i2c_read_byte(i2c_t bus, uint8_t address, void *data) ...@@ -147,9 +155,12 @@ int i2c_read_byte(i2c_t bus, uint8_t address, void *data)
int i2c_read_bytes(i2c_t bus, uint8_t address, void *data, int length) int i2c_read_bytes(i2c_t bus, uint8_t address, void *data, int length)
{ {
assert(length > 0);
if (!(bus < I2C_NUMOF)) {
return -1;
}
uint8_t *in_buf = (uint8_t *)data; uint8_t *in_buf = (uint8_t *)data;
assert((bus <= I2C_NUMOF) && (length > 0));
DEBUG("[i2c] reading %i byte from the bus\n", length); DEBUG("[i2c] reading %i byte from the bus\n", length);
/* set the client address */ /* set the client address */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment