void __attribute__ ((noinline)) i2c_start(void) { if ( i2c_started != 0 ) { /* if already started: do restart */ i2c_read_sda(); /* SDA = 1 */ i2c_delay(); i2c_read_scl(); /* clock stretching is not done */ /* while (i2c_read_scl() == 0) ; */ i2c_delay(); /* another delay, just to be sure... */ } i2c_read_sda(); /* if (i2c_read_sda() == 0) { // do something because arbitration is lost } */ /* send the start condition, both lines go from 1 to 0 */ i2c_clear_sda(); i2c_delay(); i2c_clear_scl(); i2c_started = 1; }
/* i2c_read_byte Read a byte from I2C bus => ack = acknowlege bit to send after read <= returns data read from the bus */ unsigned char i2c_read_byte(unsigned char ack) { unsigned char loop, byte; i2c_set_sda(); /* read data msb first */ for(loop = 0; loop < 8; loop++) { byte = byte << 1; i2c_set_scl(); i2c_delay(); if(i2c_read_sda()) byte = byte | 1; i2c_clear_scl(); } /* send ack bit */ if(ack) i2c_set_sda(); else i2c_clear_sda(); i2c_set_scl(); i2c_delay(); i2c_clear_scl(); i2c_set_sda(); return byte; }
/* i2c_write_byte Write a byte to I2C bus => byte = data to write out <= return 0 if acknowleged by the slave */ unsigned char i2c_write_byte(unsigned char byte) { unsigned char loop, ack, original_byte = byte; /* send data, msb first */ for(loop = 0; loop < 8; loop++) { if(byte & 0x80) i2c_set_sda(); else i2c_clear_sda(); i2c_set_scl(); i2c_delay(); i2c_clear_scl(); byte = byte << 1; } /* read ack bit from slave */ i2c_set_sda(); i2c_set_scl(); i2c_delay(); ack = i2c_read_sda(); i2c_clear_scl(); return ack; }
static void i2c_write_bit(uint8_t val) { if (val) i2c_read_sda(); else i2c_clear_sda(); i2c_delay(); i2c_read_scl_and_delay(); i2c_clear_scl(); }
static void i2c_stop(void) { /* set SDA to 0 */ i2c_clear_sda(); i2c_delay(); /* now release all lines */ i2c_read_scl_and_delay(); /* set SDA to 1 */ i2c_read_sda(); i2c_delay(); i2c_started = 0; }
void i2c_send_stop(void) { /* set SDA to 0 */ i2c_clear_sda(); i2c_delay(); /* bring the clock line high */ i2c_set_scl(); i2c_delay(); /* and set SDA back to 1 */ i2c_set_sda(); i2c_delay(); }
void i2c_send_start(void) { /* take data and clock high, then toggle */ i2c_set_sda(); i2c_delay(); i2c_set_scl(); i2c_delay(); i2c_clear_sda(); i2c_delay(); i2c_clear_scl(); i2c_delay(); }
static void i2c_start(void) { if ( i2c_started != 0 ) { /* if already started: do restart */ i2c_read_sda(); /* SDA = 1 */ i2c_delay(); i2c_read_scl_and_delay(); } i2c_read_sda(); /* if (i2c_read_sda() == 0) { // do something because arbitration is lost } */ /* send the start condition, both lines go from 1 to 0 */ i2c_clear_sda(); i2c_delay(); i2c_clear_scl(); i2c_started = 1; }
void i2c_write_bit(uint8_t val) { if (val) i2c_read_sda(); else i2c_clear_sda(); i2c_delay(); i2c_read_scl(); /* clock stretching is not done */ /* while (i2c_read_scl() == 0) ; */ /* validation is skipped, because this will be the only master */ /* If SDA is high, check that nobody else is driving SDA */ /* if (val && i2c_read_sda() == 0) { arbitration_lost(); } */ i2c_delay(); i2c_clear_scl(); }
void __attribute__ ((noinline)) i2c_stop(void) { /* set SDA to 0 */ i2c_clear_sda(); i2c_delay(); /* now release all lines */ i2c_read_scl(); /* clock stretching is not done */ /* while (i2c_read_scl() == 0) ; */ i2c_delay(); /* another delay, just to be sure... */ /* set SDA to 1 */ i2c_read_sda(); /* if (i2c_read_sda() == 0) { // do something because arbitration is lost } */ i2c_delay(); i2c_started = 0; }