From 3131bd63f179fe1c4d4cda8bd16d145fe257ac1c Mon Sep 17 00:00:00 2001 From: dongie Date: Mon, 19 May 2014 13:23:02 +0900 Subject: [PATCH] added fail check in hardware i2c driver. this doesn't solve the fact that it sucks. needs to be rewritten w/o IRQ --- src/drv_i2c.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/drv_i2c.c b/src/drv_i2c.c index fac54dfa6c..3a124261a2 100755 --- a/src/drv_i2c.c +++ b/src/drv_i2c.c @@ -129,12 +129,19 @@ bool i2cRead(uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t* buf) if (!(I2Cx->CR2 & I2C_IT_EVT)) { //if we are restarting the driver if (!(I2Cx->CR1 & 0x0100)) { // ensure sending a start - while (I2Cx->CR1 & 0x0200) { ; } //wait for any stop to finish sending + while (I2Cx->CR1 & 0x0200 && --timeout > 0) { ; } //wait for any stop to finish sending + if (timeout == 0) { + i2cErrorCount++; + // reinit peripheral + clock out garbage + i2cInit(I2Cx); + return false; + } I2C_GenerateSTART(I2Cx, ENABLE); //send the start for the new job } I2C_ITConfig(I2Cx, I2C_IT_EVT | I2C_IT_ERR, ENABLE); //allow the interrupts to fire off again } + timeout = I2C_DEFAULT_TIMEOUT; while (busy && --timeout > 0); if (timeout == 0) { i2cErrorCount++;