X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=firmware%2Fapps%2Fi2c%2Fi2c.c;h=6ee1623b041826219441a9cb4e0709c92bb22062;hb=ae09939eb8c62c83f244527e7916cee5f9145e6c;hp=1f92b9e92b478d7f0b5456e1b8c5647677a3c72c;hpb=aa1ad64b8f1a87590c9a94d32a1561bca33e843c;p=goodfet diff --git a/firmware/apps/i2c/i2c.c b/firmware/apps/i2c/i2c.c index 1f92b9e..6ee1623 100644 --- a/firmware/apps/i2c/i2c.c +++ b/firmware/apps/i2c/i2c.c @@ -1,5 +1,8 @@ -//GoodFET I2C Master Application -//Handles basic I/O +/*! \file i2c.c + \author Travis Goodspeed + \brief I2C Master +*/ + //Higher level left to client application. @@ -15,21 +18,21 @@ //Pins and I/O -#define SS BIT3 -#define SDA BIT2 -#define SCL BIT3 +#include +#define SDA TDI +#define SCL TDO + +#define I2CDELAY(x) delay(x<<4) -#define I2CDELAY(x) delay(x) -//Bits are cleared by output of low. -//Bits are set but input and pull-up resistor. -#define SETSDA P5DIR&=~SDA -#define CLRSDA P5DIR|=SDA -#define SETSCL P5DIR&=~SCL -#define CLRSCL P5DIR|=SCL +//2xx only, need 1xx compat code +#define CLRSDA P5OUT&=~SDA +#define SETSDA P5OUT|=SDA +#define CLRSCL P5OUT&=~SCL +#define SETSCL P5OUT|=SCL #define READSDA (P5IN&SDA?1:0) -#define SETBOTH P5DIR&=~(SDA|SCL) +#define SETBOTH P5OUT|=(SDA|SCL) #define I2C_DATA_HI() SETSDA #define I2C_DATA_LO() CLRSDA @@ -37,6 +40,26 @@ #define I2C_CLOCK_HI() SETSCL #define I2C_CLOCK_LO() CLRSCL +//#warning "Using internal resistors. Won't work on 161x devices." + +//! Inits bitbanging port, must be called before using the functions below +void I2C_Init() +{ + + //Clear SDA and SCL. + //Direction, not value, is used to set the value. + //(Pull-up or 0.) + + P5DIR|=(SDA|SCL); + P5REN|=SDA|SCL; + + + I2C_CLOCK_HI(); + I2C_DATA_HI(); + + I2CDELAY(1); +} + //! Write an I2C bit. void I2C_WriteBit( unsigned char c ) { @@ -64,7 +87,7 @@ unsigned char I2C_ReadBit() I2C_CLOCK_HI(); I2CDELAY(1); - + unsigned char c = READSDA; I2C_CLOCK_LO(); @@ -73,19 +96,6 @@ unsigned char I2C_ReadBit() return c; } -//! Inits bitbanging port, must be called before using the functions below -void I2C_Init() -{ - //Clear SDA and SCL. - //Direction, not value, is used to set the value. - //(Pull-up or 0.) - P5OUT&=~(SDA|SCL); - - I2C_CLOCK_HI(); - I2C_DATA_HI(); - - I2CDELAY(1); -} //! Send a START Condition void I2C_Start() @@ -116,7 +126,7 @@ unsigned char I2C_Write( unsigned char c ) { char i; for (i=0;i<8;i++){ - I2C_WriteBit( c & 128 ); + I2C_WriteBit( c & 0x80 ); c<<=1; } @@ -161,22 +171,25 @@ void i2chandle(unsigned char app, case READ: if(len>0) //optional parameter of length len=cmddata[0]; - if(len==0) //default value of 1 + if(!len) //default value of 1 len=1; for(i=0;i