diff options
author | Lars-Dominik Braun <lars@6xq.net> | 2015-01-20 15:51:50 +0100 |
---|---|---|
committer | Lars-Dominik Braun <lars@6xq.net> | 2015-01-20 15:51:50 +0100 |
commit | 2c40f4a0e5c9e9bda8d3b59553abf01abe97c5cd (patch) | |
tree | b384758140112e5ec55acd20141339b83d1f1244 | |
parent | 1f6686fe0f16a3620ee4f0ad012a2add5e03c584 (diff) | |
download | hourglass-2c40f4a0e5c9e9bda8d3b59553abf01abe97c5cd.tar.gz hourglass-2c40f4a0e5c9e9bda8d3b59553abf01abe97c5cd.tar.bz2 hourglass-2c40f4a0e5c9e9bda8d3b59553abf01abe97c5cd.zip |
i2c: twRequests returns false if bus is busy
-rw-r--r-- | gyro.c | 38 | ||||
-rw-r--r-- | i2c.c | 8 |
2 files changed, 25 insertions, 21 deletions
@@ -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)) { @@ -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); |