X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=firmware%2Fapps%2Fi2c%2Fi2c.c;h=6e1dfdea058437b79d83b98749146d0f2bde1a17;hb=96ebf28fac2e03cfee954db85282eac7b8ec8015;hp=1018c5fba8d7e28723c3105425b95bcdd5d24a73;hpb=f78ae2192de82239171b62eab19fa69c571ebd92;p=goodfet diff --git a/firmware/apps/i2c/i2c.c b/firmware/apps/i2c/i2c.c index 1018c5f..6e1dfde 100644 --- a/firmware/apps/i2c/i2c.c +++ b/firmware/apps/i2c/i2c.c @@ -73,28 +73,33 @@ app_t const i2c_app = { //#warning "Using internal resistors. Won't work on 161x devices." -//! Inits bitbanging port, must be called before using the functions below -void I2C_Init() +// Take control of the bus +void I2C_Take() { - - //Clear SDA and SCL. - //Direction, not value, is used to set the value. - - SCLOUTPUT; - SDAOUTPUT; - I2C_CLOCK_HI(); I2C_DATA_HI(); - //PULLON; + SCLOUTPUT; + SDAOUTPUT; +} +void I2C_Release() +{ + SDAINPUT; + SCLINPUT; +} + +//! Inits bitbanging port, must be called before using the functions below +void I2C_Init() +{ + I2C_Take(); + //PULLON; I2CDELAY(1); } -// This is never called... +// XXX This is never called... void I2C_Exit() { - SDAINPUT; - SCLINPUT; + I2C_Release(); PULLOFF; } @@ -111,12 +116,6 @@ void I2C_WriteBit( unsigned char c ) I2C_CLOCK_LO(); I2CDELAY(1); - - /*if(c>0) - *I2C_DATA_LO(); - * - *I2CDELAY(1); - */ } //! Read an I2C bit. @@ -134,6 +133,7 @@ unsigned char I2C_ReadBit() SDAOUTPUT; I2CDELAY(1); I2C_CLOCK_LO(); + I2CDELAY(1); return c; } @@ -161,15 +161,15 @@ unsigned char I2C_ReadBit_Wait() SDAOUTPUT; I2CDELAY(1); I2C_CLOCK_LO(); + I2CDELAY(1); - return c; + return c?0:1; // return true on ACK (0) } //! Send a START Condition void I2C_Start() { - // set both to high at the same time - SETBOTH; + I2C_Take(); // XXX Should probably check to see if someone else is using the bus before we do this I2CDELAY(3); I2C_DATA_LO(); @@ -190,6 +190,7 @@ void I2C_Stop() I2C_DATA_HI(); I2CDELAY(1); + I2C_Release(); } //! write a byte to the I2C slave device @@ -249,15 +250,19 @@ void i2c_handle_fn( uint8_t const app, break; case WRITE: I2C_Start(); - for(i=0; i