From 1540ec05095ddb1e201f4c009ee1b47abdf078c3 Mon Sep 17 00:00:00 2001 From: smlng Date: Mon, 13 Nov 2017 13:38:47 +0100 Subject: [PATCH] nrf51: fix returns and error codes in periph/i2c --- cpu/nrf51/periph/i2c.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/cpu/nrf51/periph/i2c.c b/cpu/nrf51/periph/i2c.c index a8c1223e1a..17c2d54e6b 100644 --- a/cpu/nrf51/periph/i2c.c +++ b/cpu/nrf51/periph/i2c.c @@ -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) { + assert(len > 0); + if (!(bus < I2C_NUMOF)) { + return -1; + } + uint8_t *buf = (uint8_t *)data; - assert((bus <= I2C_NUMOF) && (len > 0)); DEBUG("i2c: writing %i byte to the bus\n", len); 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) int i2c_init_master(i2c_t bus, i2c_speed_t speed) { - if (bus >= I2C_NUMOF) { + if (!(bus < I2C_NUMOF)) { return -1; } if (speed & INVALID_SPEED_MASK) { @@ -128,14 +132,18 @@ int i2c_init_master(i2c_t bus, i2c_speed_t speed) int i2c_acquire(i2c_t bus) { - assert(bus <= I2C_NUMOF); + if (!(bus < I2C_NUMOF)) { + return -1; + } mutex_lock(&locks[bus]); return 0; } int i2c_release(i2c_t bus) { - assert(bus <= I2C_NUMOF); + if (!(bus < I2C_NUMOF)) { + return -1; + } mutex_unlock(&locks[bus]); return 0; } @@ -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) { + assert(length > 0); + if (!(bus < I2C_NUMOF)) { + return -1; + } uint8_t *in_buf = (uint8_t *)data; - assert((bus <= I2C_NUMOF) && (length > 0)); DEBUG("[i2c] reading %i byte from the bus\n", length); /* set the client address */