static unsigned char twi_read_byte(bool nack) { unsigned char byte = 0; unsigned char bit; for (bit = 0; bit < 8; bit++) byte = (byte << 1) | twi_read_bit(); twi_write_bit(nack); return byte; }
static bool twi_write_byte(unsigned char byte) { unsigned char bit; for (bit = 0; bit < 8; bit++) { twi_write_bit(byte & 0x80); byte <<= 1; } return !twi_read_bit();//NACK/ACK }
static uint8_t ICACHE_FLASH_ATTR twi_read_byte(bool nack) { uint8_t byte = 0; uint8_t bit; for (bit = 0; bit < 8; bit++) byte = (byte << 1) | twi_read_bit(); twi_write_bit(nack); return byte; }
static bool ICACHE_FLASH_ATTR twi_write_byte(uint8_t byte) { uint8_t bit; for (bit = 0; bit < 8; bit++) { twi_write_bit(byte & 0x80); byte <<= 1; } return !twi_read_bit(); //NACK, ACK }
uint8_t twi_status() { if (SCL_READ() == 0) return I2C_SCL_HELD_LOW; // SCL held low by another device, no procedure available to recover int clockCount = 20; while (SDA_READ() == 0 && clockCount-- > 0) { // if SDA low, read the bits slaves have to sent to a max twi_read_bit(); if (SCL_READ() == 0) { return I2C_SCL_HELD_LOW_AFTER_READ; // I2C bus error. SCL held low beyond slave clock stretch time } } if (SDA_READ() == 0) return I2C_SDA_HELD_LOW; // I2C bus error. SDA line held low by slave/another_master after n bits. if (!twi_write_start()) return I2C_SDA_HELD_LOW_AFTER_INIT; // line busy. SDA again held low by another device. 2nd master? return I2C_OK; }