2 \author Travis Goodspeed
7 //Higher level left to client application.
9 //Based upon a neighborly example at
10 //http://codinglab.blogspot.com/2008/10/i2c-on-avr-using-bit-banging.html
24 //! Handles an i2c command.
25 void i2c_handle_fn( uint8_t const app,
29 // define the i2c app's app_t
30 app_t const i2c_app = {
42 "\tThe I2C app implements the i2c bus protocol thus\n"
43 "\tturning your GoodFET into a USB-to-i2c adapter.\n"
49 #define I2CDELAY(x) delay(x<<4)
52 //2xx only, need 1xx compat code
53 #define CLRSDA P5OUT&=~SDA
54 #define SETSDA P5OUT|=SDA
55 #define CLRSCL P5OUT&=~SCL
56 #define SETSCL P5OUT|=SCL
58 #define READSDA (P5IN&SDA?1:0)
59 #define SETBOTH P5OUT|=(SDA|SCL)
61 #define I2C_DATA_HI() SETSDA
62 #define I2C_DATA_LO() CLRSDA
64 #define I2C_CLOCK_HI() SETSCL
65 #define I2C_CLOCK_LO() CLRSCL
67 //#warning "Using internal resistors. Won't work on 161x devices."
69 //! Inits bitbanging port, must be called before using the functions below
74 //Direction, not value, is used to set the value.
88 void I2C_WriteBit( unsigned char c )
108 unsigned char I2C_ReadBit()
115 unsigned char c = READSDA;
124 //! Send a START Condition
127 // set both to high at the same time
138 //! Send a STOP Condition
148 //! write a byte to the I2C slave device
149 unsigned char I2C_Write( unsigned char c )
153 I2C_WriteBit( c & 0x80 );
157 return I2C_ReadBit();
161 //! read a byte from the I2C slave device
162 unsigned char I2C_Read( unsigned char ack )
164 unsigned char res = 0;
169 res |= I2C_ReadBit();
183 //! Handles an i2c command.
184 void i2c_handle_fn( uint8_t const app,
200 if(l > 0) //optional parameter of length
202 if(!l) //default value of 1
204 for(i = 0; i < l; i++)
205 cmddata[i]=I2C_Read(1); //Always acknowledge
211 cmddata[0]+=I2C_Write(cmddata[i]);