int i2c_stop() { READSDA(); READSCL(); sda=-1; scl=-1; I2CSPEED=-1; return 0; }
static void stop_cond(void) { /* Pull SDA down */ CLRSDA(); I2CDELAY(I2CSPEED/2); /* Clock stretching - wait for slave to raise it*/ while (READSCL() == 0); /* SCL is high, set SDA from 0 to 1 */ if (READSDA() == 0) { ARBITRATION_LOST(); } I2CDELAY(I2CSPEED/2); start = 0; }
static unsigned char read_bit(void) { unsigned char bit; // Let the slave drive data READSDA(); I2CDELAY(I2CSPEED/2); /* Clock stretching */ while (READSCL() == 0); /* SCL is high, now data is valid */ bit = READSDA(); I2CDELAY(I2CSPEED/2); CLRSCL(); return bit; }
bool stop_cond(void) { /* set SDA to 0 */ CLRSDA(); I2CDELAY(I2CSPEED/2); /* Clock stretching */ while (READSCL() == 0); /* SCL is high, set SDA from 0 to 1 */ if (READSDA() == 0) return false; //arbitration lost I2CDELAY(I2CSPEED/2); start = 0; return true; }
bool read_bit(void) { bool bit; //Let slave drive data signal. READSDA(); I2CDELAY(I2CSPEED/2); /* Clock stretching */ while (READSCL() == 0); /* SCL is high, now data is valid */ bit = READSDA(); I2CDELAY(I2CSPEED/2); CLRSCL(); return bit; }
/* Read a bit from I2C bus */ static int i2c_read_bit(void) { int bit; /* Let the slave drive data */ READSDA(); I2CDELAY(); /* Clock stretching */ while (READSCL() == 0) ; /* You should add timeout to this loop */ /* SCL is high, now data is valid */ bit = READSDA(); I2CDELAY(); CLRSCL(); return bit; }
void i2c_stop_cond(void) { /* set SDA to 0 */ CLRSDA(); I2CDELAY(); /* Clock stretching */ while (READSCL() == 0) ; /* You should add timeout to this loop */ /* SCL is high. Respect I2C spec's minimum stop setup time of 4ms. */ I2CDELAY(); /* set SDA from 0 to 1 */ if (READSDA() == 0) ARBITRATION_LOST(); I2CDELAY(); started = false; }
bool write_bit(bool bit) { if(bit) READSDA(); else CLRSDA(); I2CDELAY(I2CSPEED/2); /* Clock stretching */ while (READSCL() == 0); /* SCL is high, now data is valid */ /* check that nobody is driving SDA */ if (bit && READSDA() == 0) return false; //arbitration lost I2CDELAY(I2CSPEED/2); CLRSCL(); return true; }
/* Write a bit to I2C bus */ static void i2c_write_bit(int bit) { if (bit) READSDA(); else CLRSDA(); I2CDELAY(); /* Clock stretching */ while (READSCL() == 0) ; /* You should add timeout to this loop */ /* SCL is high, now data is valid */ /* If SDA is high, check that nobody else is driving SDA */ if (bit && READSDA() == 0) ARBITRATION_LOST(); I2CDELAY(); CLRSCL(); }
static void start_cond(void) { if (start) { /* Let SDA rise */ READSDA(); I2CDELAY(I2CSPEED/2); /* Clock stretching */ while (READSCL() == 0); } if (READSDA() == 0) { ARBITRATION_LOST(); } /* SCL is high, we waited for slave to raise it, so pull SDA down */ CLRSDA(); I2CDELAY(I2CSPEED/2); // Now pull SCL down CLRSCL(); start = 1; }
static void write_bit(unsigned char bit) { //Put the bit on SDA by either letting it rise or pulling it down if (bit) { READSDA(); } else { CLRSDA(); } I2CDELAY(I2CSPEED/2); /* Clock stretching - Let SCL rise and wait for slave to let it rise */ while (READSCL() == 0); /* SCL is high, now data is being read */ /* If SDA is high, check that nobody else is driving SDA */ if (bit) { if (READSDA() == 0) { //Oops, someone else pulled SDA down ARBITRATION_LOST(); } } I2CDELAY(I2CSPEED/2); CLRSCL(); }
void i2c_start_cond(void) { if (started) { // if started, do a restart cond // set SDA to 1 READSDA(); I2CDELAY(); // Clock stretching while (READSCL() == 0) ; // You can add a timeout to this loop to // recover from SCL being stuck low. // Repeated start setup time, minimum 4.7us I2CDELAY(); } if (READSDA() == 0) ARBITRATION_LOST(); // SCL is high, set SDA from 1 to 0 CLRSDA(); I2CDELAY(); CLRSCL(); started = true; }