X-Git-Url: http://git.rot13.org/?p=goodfet;a=blobdiff_plain;f=firmware%2Fapps%2Fi2c%2Fi2c.c;h=af2c7f16702af9b87b7001c7683de5c2871ec520;hp=462a2cfbb3b98d3a703ade376234f93d77c471b2;hb=69539bb167246135b1bde3c55dca7d19bc3c7aee;hpb=ce0bba3cfbf4844bdce1f6984af24d1f78c347f1 diff --git a/firmware/apps/i2c/i2c.c b/firmware/apps/i2c/i2c.c index 462a2cf..af2c7f1 100644 --- a/firmware/apps/i2c/i2c.c +++ b/firmware/apps/i2c/i2c.c @@ -1,9 +1,6 @@ /*! \file i2c.c \author Travis Goodspeed - - GoodFET I2C Master Application - Handles basic I/O - + \brief I2C Master */ @@ -14,6 +11,7 @@ #include "platform.h" #include "command.h" +#include "i2c.h" #include #include @@ -22,13 +20,56 @@ //Pins and I/O #include -#define SDA TDI -#define SCL TDO + +//! Handles an i2c command. +void i2c_handle_fn( uint8_t const app, + uint8_t const verb, + uint32_t const len); + +// define the i2c app's app_t +app_t const i2c_app = { + + /* app number */ + I2C_APP, + + /* handle fn */ + i2c_handle_fn, + + /* name */ + "I2C", + + /* desc */ + "\tThe I2C app implements the i2c bus protocol thus\n" + "\tturning your GoodFET into a USB-to-i2c adapter.\n" +}; #define I2CDELAY(x) delay(x<<4) //2xx only, need 1xx compat code +#if (platform == tilaunchpad) +// P3.1 SDA +// P3.3 SCL +#define SDA (1<<1) +#define SCL (1<<3) + +#define CLRSDA P3OUT&=~SDA +#define SETSDA P3OUT|=SDA +#define CLRSCL P3OUT&=~SCL +#define SETSCL P3OUT|=SCL + +#define READSDA (P3IN&SDA?1:0) +#define SDAINPUT P3DIR&=~SDA +#define SDAOUTPUT P3DIR|=SDA +#define SCLINPUT P3DIR&=~SCL +#define SCLOUTPUT P3DIR|=SCL +#define SETBOTH P3OUT|=(SDA|SCL) + +#else + +#define SDA TDI +#define SCL TDO + #define CLRSDA P5OUT&=~SDA #define SETSDA P5OUT|=SDA #define CLRSCL P5OUT&=~SCL @@ -36,6 +77,7 @@ #define READSDA (P5IN&SDA?1:0) #define SETBOTH P5OUT|=(SDA|SCL) +#endif #define I2C_DATA_HI() SETSDA #define I2C_DATA_LO() CLRSDA @@ -53,8 +95,13 @@ void I2C_Init() //Direction, not value, is used to set the value. //(Pull-up or 0.) +#if (platform == tilaunchpad) + SDAOUTPUT; + SCLOUTPUT; +#else P5DIR|=(SDA|SCL); - P5REN|=SDA|SCL; +#endif + //P5REN|=SDA|SCL; I2C_CLOCK_HI(); @@ -63,6 +110,14 @@ void I2C_Init() I2CDELAY(1); } +#if (platform == tilaunchpad) +void I2C_Exit() +{ + SDAINPUT; + SCLINPUT; +} +#endif + //! Write an I2C bit. void I2C_WriteBit( unsigned char c ) { @@ -89,12 +144,14 @@ unsigned char I2C_ReadBit() I2C_DATA_HI(); I2C_CLOCK_HI(); + SDAINPUT; I2CDELAY(1); unsigned char c = READSDA; I2C_CLOCK_LO(); I2CDELAY(1); + SDAOUTPUT; return c; } @@ -159,44 +216,48 @@ unsigned char I2C_Read( unsigned char ack ) } -//! Handles a monitor command. -void i2chandle(unsigned char app, - unsigned char verb, - unsigned char len){ - unsigned char i; - switch(verb){ - - case PEEK: - break; - case POKE: - break; - - case READ: - if(len>0) //optional parameter of length - len=cmddata[0]; - if(!len) //default value of 1 - len=1; - for(i=0;i 0) //optional parameter of length + l=cmddata[0]; + if(!l) //default value of 1 + l=1; + for(i = 0; i < l; i++) + cmddata[i]=I2C_Read(1); //Always acknowledge + txdata(app,verb,l); + break; + case WRITE: + cmddata[0]=0; + for(i=0;i