//#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;
}
I2C_CLOCK_LO();
I2CDELAY(1);
-
- /*if(c>0)
- *I2C_DATA_LO();
- *
- *I2CDELAY(1);
- */
}
//! Read an I2C bit.
SDAOUTPUT;
I2CDELAY(1);
I2C_CLOCK_LO();
+ I2CDELAY(1);
return c;
}
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();
I2C_DATA_HI();
I2CDELAY(1);
+ I2C_Release();
}
//! write a byte to the I2C slave device
break;
case WRITE:
I2C_Start();
- for(i=0; i<len; i++)
- cmddata[0]+=I2C_Write(cmddata[i]);
+ cmddata[0] = cmddata[0] << 1;
+ for(i=0; i<len; i++) {
+ if (!I2C_Write(cmddata[i])) //if NACK
+ break;
+ }
I2C_Stop();
+ cmddata[0] = i;
txdata(app,verb,1);
break;
case PEEK:
l = cmddata[0];
I2C_Start();
- unsigned char address = cmddata[1]<<1;
+ unsigned char address = cmddata[1] << 1;
I2C_Write(address);
for(i=2; i < len; i++){
I2C_Write(cmddata[i]);