/****************************************************************************** * FunctionName : i2c_master_readByte * Description : read Byte from i2c bus * Parameters : NONE * Returns : uint8 - readed value *******************************************************************************/ uint8 i2c_master_readByte(void) { uint8 retVal = 0; uint8 k, i; i2c_master_setDC(1, m_nLastSCL); for (i = 0; i < 8; i++) { i2c_master_wait(I2C_SLEEP_TIME); i2c_master_setDC(1, 0); i2c_master_wait(I2C_SLEEP_TIME);// sda 1, scl 0 i2c_master_setDC(1, 1); while(i2c_master_getCL()==0) ; // clock stretch i2c_master_wait(I2C_SLEEP_TIME);// sda 1, scl 1 k = i2c_master_getDC(); i2c_master_wait(I2C_SLEEP_TIME); //if (i == 7) { // i2c_master_wait(5); //// //} k <<= (7 - i); retVal |= k; } i2c_master_setDC(m_nLastSDA, 0); i2c_master_wait(I2C_SLEEP_TIME);// sda 1, scl 0 return retVal; }
/****************************************************************************** * FunctionName : i2c_master_readByte * Description : read Byte from i2c bus * Parameters : NONE * Returns : uint8 - readed value *******************************************************************************/ uint8 ICACHE_FLASH_ATTR i2c_master_readByte(void) { uint8 retVal = 0; uint8 k, i; i2c_master_wait(5); i2c_master_setDC(m_nLastSDA, 0); i2c_master_wait(5); // sda 1, scl 0 for (i = 0; i < 8; i++) { i2c_master_wait(5); i2c_master_setDC(1, 0); i2c_master_wait(5); // sda 1, scl 0 i2c_master_setDC(1, 1); i2c_master_wait(5); // sda 1, scl 1 k = i2c_master_getDC(); i2c_master_wait(5); if (i == 7) { i2c_master_wait(3); //// } k <<= (7 - i); retVal |= k; } i2c_master_setDC(1, 0); i2c_master_wait(5); // sda 1, scl 0 return retVal; }
/****************************************************************************** * FunctionName : i2c_master_getAck * Description : confirm if peer send ack * Parameters : NONE * Returns : uint8 - ack value, 0 or 1 *******************************************************************************/ uint8 ICACHE_FLASH_ATTR i2c_master_getAck(void) { uint8 retVal; i2c_master_setDC(1, 0); i2c_master_wait(2); i2c_master_setDC(1, 1); i2c_master_wait(4); retVal = i2c_master_getDC(); i2c_master_setDC(1, 0); i2c_master_wait(2); return !retVal; // 0->true->ACK, 1->false->NACK }
/****************************************************************************** * FunctionName : i2c_master_getAck * Description : confirm if peer send ack * Parameters : NONE * Returns : uint8 - ack value, 0 or 1 *******************************************************************************/ uint8 ICACHE_FLASH_ATTR i2c_master_getAck(void) { uint8 retVal; i2c_master_setDC(m_nLastSDA, 0); i2c_master_setDC(1, 0); i2c_master_wait(I2C_DELAY); // extra delay i2c_master_setDC(1, 1); retVal = i2c_master_getDC(); i2c_master_setDC(1, 0); return retVal; }
/****************************************************************************** * FunctionName : i2c_master_getAck * Description : confirm if peer send ack * Parameters : NONE * Returns : uint8 - ack value, 0 or 1 *******************************************************************************/ uint8 i2c_master_getAck(void) { uint8 retVal; i2c_master_setDC(m_nLastSDA, 0); i2c_master_wait(I2C_SLEEP_TIME); i2c_master_setDC(1, 0); i2c_master_wait(I2C_SLEEP_TIME); i2c_master_setDC(1, 1); i2c_master_wait(I2C_SLEEP_TIME); retVal = i2c_master_getDC(); i2c_master_wait(I2C_SLEEP_TIME); i2c_master_setDC(1, 0); i2c_master_wait(I2C_SLEEP_TIME); return retVal; }
/****************************************************************************** * FunctionName : i2c_master_getAck * Description : confirm if peer send ack * Parameters : NONE * Returns : uint8 - ack value, 0 or 1 *******************************************************************************/ uint8 ICACHE_FLASH_ATTR i2c_master_getAck(void) { uint8 retVal; i2c_master_setDC(m_nLastSDA, 0); i2c_master_wait(5); i2c_master_setDC(1, 0); i2c_master_wait(5); i2c_master_setDC(1, 1); i2c_master_wait(5); retVal = i2c_master_getDC(); i2c_master_wait(5); i2c_master_setDC(1, 0); i2c_master_wait(5); return retVal; }
/****************************************************************************** * FunctionName : i2c_master_readByte * Description : read Byte from i2c bus * Parameters : NONE * Returns : uint8 - readed value *******************************************************************************/ uint8 ICACHE_FLASH_ATTR i2c_master_readByte(void) { uint8 retVal = 0; uint8 i; i2c_master_wait(2); i2c_master_setDC(m_nLastSDA, 0); i2c_master_wait(4); // sda 1, scl 0 for (i = 0; i < 8; i++) { i2c_master_setDC(1, 0); i2c_master_wait(4); // sda 1, scl 0 i2c_master_setDC(1, 1); i2c_master_wait(3); // sda 1, scl 1 retVal = (retVal<<1) | (i2c_master_getDC()&1); } i2c_master_setDC(1, 0); i2c_master_wait(2); // sda 1, scl 0 return retVal; }