From 55be863b84485d8d8032d5c680ee53c9a338515f Mon Sep 17 00:00:00 2001 From: Markus Hitter Date: Tue, 5 Apr 2016 19:00:20 +0200 Subject: [PATCH] I2C: provide a busy-detection. Nowhere used so far, so no binary size change. --- i2c.c | 17 +++++++++++++++++ i2c.h | 1 + 2 files changed, 18 insertions(+) diff --git a/i2c.c b/i2c.c index 9caa371..01dc24a 100644 --- a/i2c.c +++ b/i2c.c @@ -141,6 +141,20 @@ void i2c_init(uint8_t address) { #endif } +/** + Report wether I2C is busy. + + \return Wether I2C is currently busy, which means that eventual new + transactions would have to wait. + + Idea is that non-crucial display writes check the bus before actually + writing, so they avoid long waits. If i2c_busy() returns zero, the bus + is free and writes won't cause a delay. +*/ +uint8_t i2c_busy(void) { + return (i2c_state & I2C_MODE_BUSY); +} + /** Send a byte to the I2C partner. @@ -164,6 +178,9 @@ void i2c_init(uint8_t address) { amounts don't get lost, but this function has to wait until sufficient previous data was sent. + To avoid unexpected delays, invoking code can check for bus availability + with i2c_busy(). + Note that calling code has to send bytes quickly enough to not drain the buffer. It looks like the I2C protocol doesn't, unlike e.g. SPI, allow to pause sending without dropping the transmission. Positive of this diff --git a/i2c.h b/i2c.h index d5d50a7..35f2827 100644 --- a/i2c.h +++ b/i2c.h @@ -39,6 +39,7 @@ void i2c_init(uint8_t address); +uint8_t i2c_busy(void); void i2c_write(uint8_t address, uint8_t data, uint8_t last_byte); #endif /* I2C */