projects
/
goodfet
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
new maxusb library
[goodfet]
/
firmware
/
apps
/
i2c
/
i2c.c
diff --git
a/firmware/apps/i2c/i2c.c
b/firmware/apps/i2c/i2c.c
index
1018c5f
..
6e1dfde
100644
(file)
--- 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."
//#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();
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);
}
I2CDELAY(1);
}
-// This is never called...
+//
XXX
This is never called...
void I2C_Exit()
{
void I2C_Exit()
{
- SDAINPUT;
- SCLINPUT;
+ I2C_Release();
PULLOFF;
}
PULLOFF;
}
@@
-111,12
+116,6
@@
void I2C_WriteBit( unsigned char c )
I2C_CLOCK_LO();
I2CDELAY(1);
I2C_CLOCK_LO();
I2CDELAY(1);
-
- /*if(c>0)
- *I2C_DATA_LO();
- *
- *I2CDELAY(1);
- */
}
//! Read an I2C bit.
}
//! Read an I2C bit.
@@
-134,6
+133,7
@@
unsigned char I2C_ReadBit()
SDAOUTPUT;
I2CDELAY(1);
I2C_CLOCK_LO();
SDAOUTPUT;
I2CDELAY(1);
I2C_CLOCK_LO();
+ I2CDELAY(1);
return c;
}
return c;
}
@@
-161,15
+161,15
@@
unsigned char I2C_ReadBit_Wait()
SDAOUTPUT;
I2CDELAY(1);
I2C_CLOCK_LO();
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()
{
}
//! 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();
I2CDELAY(3);
I2C_DATA_LO();
@@
-190,6
+190,7
@@
void I2C_Stop()
I2C_DATA_HI();
I2CDELAY(1);
I2C_DATA_HI();
I2CDELAY(1);
+ I2C_Release();
}
//! write a byte to the I2C slave device
}
//! 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();
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();
I2C_Stop();
+ cmddata[0] = i;
txdata(app,verb,1);
break;
case PEEK:
l = cmddata[0];
I2C_Start();
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]);
I2C_Write(address);
for(i=2; i < len; i++){
I2C_Write(cmddata[i]);