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"
46 #define I2CDELAY(x) delay(x<<4)
49 //2xx only, need 1xx compat code
50 #if (board == tilaunchpad)
56 #define CLRSDA P3OUT&=~SDA
57 #define SETSDA P3OUT|=SDA
58 #define CLRSCL P3OUT&=~SCL
59 #define SETSCL P3OUT|=SCL
61 #define READSDA (P3IN&SDA?1:0)
62 #define SDAINPUT P3DIR&=~SDA
63 #define SDAOUTPUT P3DIR|=SDA
64 #define SCLINPUT P3DIR&=~SCL
65 #define SCLOUTPUT P3DIR|=SCL
66 #define SETBOTH P3OUT|=(SDA|SCL)
73 #define CLRSDA P5OUT&=~SDA
74 #define SETSDA P5OUT|=SDA
75 #define CLRSCL P5OUT&=~SCL
76 #define SETSCL P5OUT|=SCL
78 #define READSDA (P5IN&SDA?1:0)
79 #define SETBOTH P5OUT|=(SDA|SCL)
82 #define I2C_DATA_HI() SETSDA
83 #define I2C_DATA_LO() CLRSDA
85 #define I2C_CLOCK_HI() SETSCL
86 #define I2C_CLOCK_LO() CLRSCL
88 //#warning "Using internal resistors. Won't work on 161x devices."
90 //! Inits bitbanging port, must be called before using the functions below
95 //Direction, not value, is used to set the value.
98 #if (platform == tilaunchpad)
113 #if (platform == tilaunchpad)
121 //! Write an I2C bit.
122 void I2C_WriteBit( unsigned char c )
142 unsigned char I2C_ReadBit()
150 unsigned char c = READSDA;
160 //! Send a START Condition
163 // set both to high at the same time
174 //! Send a STOP Condition
184 //! write a byte to the I2C slave device
185 unsigned char I2C_Write( unsigned char c )
189 I2C_WriteBit( c & 0x80 );
193 return I2C_ReadBit();
197 //! read a byte from the I2C slave device
198 unsigned char I2C_Read( unsigned char ack )
200 unsigned char res = 0;
205 res |= I2C_ReadBit();
219 //! Handles an i2c command.
220 void i2c_handle_fn( uint8_t const app,
236 if(l > 0) //optional parameter of length
238 if(!l) //default value of 1
240 for(i = 0; i < l; i++)
241 cmddata[i]=I2C_Read(1); //Always acknowledge
247 cmddata[0]+=I2C_Write(cmddata[i]);