uint8_t ICACHE_FLASH_ATTR twi_readFrom(uint8_t address, uint8_t *buf, unsigned int len, uint8_t sendStop) { unsigned int i; if (!twi_write_start()) { serial_print("I2C: bus busy\r\n"); return 4; } if (!twi_write_byte(((address << 1) | 1) & 0xFF)) { if (sendStop) twi_write_stop(); serial_print("I2C: received NACK on transmit of address\r\n"); return 2; } for (i = 0; i < (len - 1); i++) buf[i] = twi_read_byte(false); buf[len - 1] = twi_read_byte(true); if (sendStop) twi_write_stop(); i = 0; while (SDA_READ() == 0 && (i++) < 10) { SCL_LOW(); twi_delay(twi_dcount); SCL_HIGH(); twi_delay(twi_dcount); } return 0; }
uint8_t ICACHE_FLASH_ATTR twi_writeTo(uint8_t address, uint8_t *buf, unsigned int len, uint8_t sendStop) { unsigned int i; if (!twi_write_start()) { serial_print("I2C: bus busy\r\n"); return 4; } if (!twi_write_byte(((address << 1) | 0) & 0xFF)) { if (sendStop) twi_write_stop(); serial_print("I2C: received NACK on transmit of address\r\n"); return 2; } for (i = 0; i < len; i++) { if (!twi_write_byte(buf[i])) { if (sendStop) twi_write_stop(); serial_print("I2C: received NACK on transmit of data\r\n"); return 3; } } if (sendStop) twi_write_stop(); i = 0; while (SDA_READ() == 0 && (i++) < 10) { SCL_LOW(); twi_delay(twi_dcount); SCL_HIGH(); twi_delay(twi_dcount); } return 0; }
static bool twi_read_bit(void) { uint32_t i = 0; SCL_LOW(); SDA_HIGH(); twi_delay(twi_dcount+2); SCL_HIGH(); while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit);// Clock stretching bool bit = SDA_READ(); twi_delay(twi_dcount); return bit; }
static bool twi_write_bit(bool bit) { uint32_t i = 0; SCL_LOW(); if (bit) SDA_HIGH(); else SDA_LOW(); twi_delay(twi_dcount+1); SCL_HIGH(); while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit);// Clock stretching twi_delay(twi_dcount); return true; }
static bool twi_write_start(void) { SCL_HIGH(); SDA_HIGH(); if (SDA_READ() == 0) { return false; } twi_delay(twi_dcount); SDA_LOW(); twi_delay(twi_dcount); return true; }
static bool ICACHE_FLASH_ATTR twi_read_bit() { unsigned int i = 0; SCL_LOW(); SDA_HIGH(); twi_delay(twi_dcount + 2); SCL_HIGH(); while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit); // Clock stretching bool bit = SDA_READ(); twi_delay(twi_dcount); return bit; }
static bool twi_write_stop(void){ uint32_t i = 0; SCL_LOW(); SDA_LOW(); twi_delay(twi_dcount); SCL_HIGH(); while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit); // Clock stretching twi_delay(twi_dcount); SDA_HIGH(); twi_delay(twi_dcount); return true; }
static bool ICACHE_FLASH_ATTR twi_write_start(void) { SCL_HIGH(); SDA_HIGH(); if (SDA_READ() == 0) { serial_printf("twi write start sda read false\r\n"); return false; } twi_delay(twi_dcount); SDA_LOW(); twi_delay(twi_dcount); return true; }
static bool ICACHE_FLASH_ATTR twi_write_stop(void) { unsigned int i = 0; SCL_LOW(); SDA_LOW(); twi_delay(twi_dcount); SCL_HIGH(); while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit); // Clock stretching twi_delay(twi_dcount); SDA_HIGH(); twi_delay(twi_dcount); return true; }
void ICACHE_RAM_ATTR twi_stop(void) { // send stop condition //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO); SCL_HIGH(); // _BV(TWINT) twi_ack = 1; // _BV(TWEA) twi_delay(5); // Maybe this should be here SDA_HIGH(); // _BV(TWSTO) // update twi state twi_state = TWI_READY; }
unsigned char twi_readFrom(unsigned char address, unsigned char* buf, unsigned int len, unsigned char sendStop){ unsigned int i; if(!twi_write_start()) return 4;//line busy if(!twi_write_byte(((address << 1) | 1) & 0xFF)) { if (sendStop) twi_write_stop(); return 2;//received NACK on transmit of address } for(i=0; i<(len-1); i++) buf[i] = twi_read_byte(false); buf[len-1] = twi_read_byte(true); if(sendStop) twi_write_stop(); i = 0; while(SDA_READ() == 0 && (i++) < 10){ SCL_LOW(); twi_delay(twi_dcount); SCL_HIGH(); unsigned int t=0; while(SCL_READ()==0 && (t++)<twi_clockStretchLimit); // twi_clockStretchLimit twi_delay(twi_dcount); } return 0; }
unsigned char twi_writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop){ unsigned int i; if(!twi_write_start()) return 4;//line busy if(!twi_write_byte(((address << 1) | 0) & 0xFF)) { if (sendStop) twi_write_stop(); return 2; //received NACK on transmit of address } for(i=0; i<len; i++) { if(!twi_write_byte(buf[i])) { if (sendStop) twi_write_stop(); return 3;//received NACK on transmit of data } } if(sendStop) twi_write_stop(); i = 0; while(SDA_READ() == 0 && (i++) < 10){ SCL_LOW(); twi_delay(twi_dcount); SCL_HIGH(); unsigned int t=0; while(SCL_READ()==0 && (t++)<twi_clockStretchLimit); // twi_clockStretchLimit twi_delay(twi_dcount); } return 0; }