From 2c40f4a0e5c9e9bda8d3b59553abf01abe97c5cd Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Tue, 20 Jan 2015 15:51:50 +0100 Subject: i2c: twRequests returns false if bus is busy --- gyro.c | 38 +++++++++++++++++++------------------- i2c.c | 8 ++++++-- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/gyro.c b/gyro.c index a533e3f..14e25ed 100644 --- a/gyro.c +++ b/gyro.c @@ -82,21 +82,21 @@ static void gyroProcessTicks () { */ bool gyroProcess () { switch (state) { - case START_REQUEST: - if (twr.status == TWST_OK) { - /* configuration: - * disable power-down-mode, enable z - * defaults - * high-active, push-pull, drdy on int2 - * select 2000dps - */ - static uint8_t data[] = {0b00001100, 0b0, 0b00001000, 0b00110000}; - const bool ret = twRequest (TWM_WRITE, L3GD20, L3GD20_CTRLREG1, data, - sizeof (data)/sizeof (*data)); - assert (ret); + case START_REQUEST: { + /* configuration: + * disable power-down-mode, enable z + * defaults + * high-active, push-pull, drdy on int2 + * select 2000dps + */ + static uint8_t data[] = {0b00001100, 0b0, 0b00001000, 0b00110000}; + const bool ret = twRequest (TWM_WRITE, L3GD20, L3GD20_CTRLREG1, data, + sizeof (data)/sizeof (*data)); + if (ret) { state = STARTING; } break; + } case STARTING: if (shouldWakeup (WAKE_I2C)) { @@ -105,17 +105,17 @@ bool gyroProcess () { } break; - case STOP_REQUEST: - if (twr.status == TWST_OK) { - /* enable power-down mode */ - static uint8_t data[] = {0b00000000}; + case STOP_REQUEST: { + /* enable power-down mode */ + static uint8_t data[] = {0b00000000}; - const bool ret = twRequest (TWM_WRITE, L3GD20, L3GD20_CTRLREG1, data, - sizeof (data)/sizeof (*data)); - assert (ret); + const bool ret = twRequest (TWM_WRITE, L3GD20, L3GD20_CTRLREG1, data, + sizeof (data)/sizeof (*data)); + if (ret) { state = STOPPING; } break; + } case STOPPING: if (shouldWakeup (WAKE_I2C)) { diff --git a/i2c.c b/i2c.c index bbee35b..54e1f29 100644 --- a/i2c.c +++ b/i2c.c @@ -66,11 +66,15 @@ void twInit () { twr.status = TWST_OK; } -/* high-level write +/* high-level write, returns false if bus is busy */ bool twRequest (const twMode mode, const uint8_t address, const uint8_t subaddress, uint8_t * const data, const uint8_t count) { - assert (twr.status == TWST_OK); + /* busy */ + if (twr.status != TWST_OK) { + return false; + } + assert (count > 0); assert (data != NULL); -- cgit v1.2.3