int I2C::read(int ack) { if (ack) { return i2c_byte_read(&_i2c, 0); } else { return i2c_byte_read(&_i2c, 1); } }
int I2C::read(int ack) { lock(); int ret; if (ack) { ret = i2c_byte_read(&_i2c, 0); } else { ret = i2c_byte_read(&_i2c, 1); } unlock(); return ret; }
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); int timeout; int count; int value; if (length == 0) return 0; i2c_start(obj); // Wait until SB flag is set timeout = FLAG_TIMEOUT; while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB) == RESET) { timeout--; if (timeout == 0) { return 0; } } i2c->DR = __HAL_I2C_7BIT_ADD_READ(address); // Wait address is acknowledged timeout = FLAG_TIMEOUT; while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == RESET) { timeout--; if (timeout == 0) { return 0; } } __HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle); // Read all bytes except last one for (count = 0; count < (length - 1); count++) { value = i2c_byte_read(obj, 0); data[count] = (char)value; } // If not repeated start, send stop. // Warning: must be done BEFORE the data is read. if (stop) { i2c_stop(obj); } // Read the last byte value = i2c_byte_read(obj, 1); data[count] = (char)value; return length; }
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); int timeout; int count; int value; if (length == 0) return 0; /* // Wait until the bus is not busy anymore timeout = LONG_TIMEOUT; while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) { timeout--; if (timeout == 0) { return 0; } } */ i2c_start(obj); // Send slave address for read I2C_Send7bitAddress(i2c, address, I2C_Direction_Receiver); // Wait address is acknowledged timeout = FLAG_TIMEOUT; while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) == ERROR) { timeout--; if (timeout == 0) { return 0; } } // Read all bytes except last one for (count = 0; count < (length - 1); count++) { value = i2c_byte_read(obj, 0); data[count] = (char)value; } // If not repeated start, send stop. // Warning: must be done BEFORE the data is read. if (stop) { i2c_stop(obj); } // Read the last byte value = i2c_byte_read(obj, 1); data[count] = (char)value; return length; }
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); int count; int timeout; int value; // Configure slave address, nbytes, reload, end mode and start or stop generation I2C_TransferHandling(i2c, address, length, I2C_SoftEnd_Mode, I2C_Generate_Start_Read); // Read all bytes for (count = 0; count < length; count++) { value = i2c_byte_read(obj, 0); data[count] = (char)value; } timeout = FLAG_TIMEOUT; while (!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) { timeout--; if (timeout == 0) return -1; } if (stop) i2c_stop(obj); return length; }
int i2c_slave_read(i2c_t *obj, char *data, int length) { char size = 0; while (size < length) data[size++] = (char)i2c_byte_read(obj, 0); return size; }
/****************************************************************************** ** Function : get_eeprom_size ** Description : Detects dynamically the size of the EEPROM using a ** simple technique of using the 16-bit EEPROMWrite ** and the 8-bitEEPROMWrite routines. ** Inputs : I2C Address ** Output : EEPROM TYPE ******************************************************************************/ int get_eeprom_size( u_char devadd ) { u_char dat,val; u_char buffer[2]; int eeprom_size = -1; dat = 0xdd; buffer[0] = dat; DBG("Detecting EEPROM device ......\n" ) ; if( i2c_word_write( devadd, 0x1230, 1, buffer )){ if( i2c_byte_read( devadd, 0x12, 2, buffer )) { val = buffer[1]; if( val == dat) eeprom_size = 2; else eeprom_size = 64; } else DBG("i2c_byte_read failed. \n" ) ; } else DBG("\nUnable to detect EEPROM Device\n"); return( eeprom_size ); }
int i2c_slave_read(i2c_t *obj, char *data, int length) { int count = 0; // Read all bytes for (count = 0; count < length; count++) { data[count] = i2c_byte_read(obj, 0); } return count; }
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { I2cHandle = (I2C_TypeDef *)(obj->i2c); int count; int value; if(!obj->is_setAddress) { if( I2C_Start(I2cHandle, address, I2C_READ_SA7) == ERROR ) { return -1; } obj->is_setAddress = 1; obj->ADDRESS = address; } else { I2C_Restart_Structure(I2cHandle, address, I2C_READ_SA7); obj->ADDRESS = address; } // Read all bytes for (count = 0; count < (length-1); count++) { if( (value = i2c_byte_read(obj, 0)) == -1) return value; data[count] = (char)value; } if(stop){ if( (value = i2c_byte_read(obj, 1)) == -1) return value; data[count] = (char)value; } else{ if( (value = i2c_byte_read(obj, 0)) == -1) return value; data[count] = (char)value; } return count; }
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); int timeout; int count; int value; if (length == 0) return 0; /* update CR2 register */ i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP))) | (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_READ); // Read all bytes for (count = 0; count < length; count++) { value = i2c_byte_read(obj, 0); data[count] = (char)value; } // Wait transfer complete timeout = LONG_TIMEOUT; while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) { timeout--; if (timeout == 0) { return 0; } } __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC); // If not repeated start, send stop. if (stop) { i2c_stop(obj); /* Wait until STOPF flag is set */ timeout = FLAG_TIMEOUT; while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) { timeout--; if (timeout == 0) { return 0; } } /* Clear STOP Flag */ __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF); } return length; }
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); int count; int value; if (length == 0) return 0; // Configure slave address, nbytes, reload, end mode and start or stop generation I2C_TransferHandling(i2c, address, length, I2C_AutoEnd_Mode, I2C_Generate_Start_Read); // Read all bytes for (count = 0; count < length; count++) { value = i2c_byte_read(obj, 0); data[count] = (char)value; } return length; }
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { // Zero-length RX transfers are not supported. Such transfers cannot // be easily achieved with TWI peripheral (some dirty tricks would be // required for this), and they are actually useless (TX can be used // to check if the address is acknowledged by a slave). MBED_ASSERT(length > 0); twi_info_t *twi_info = TWI_INFO(obj); #if DEVICE_I2C_ASYNCH if (twi_info->active) { return I2C_ERROR_BUS_BUSY; } #endif twi_info->start_twi = false; NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)]; start_twi_read(twi, address); int result = length; while (length > 0) { int byte_read_result = i2c_byte_read(obj, (stop && length == 1)); if (byte_read_result < 0) { // When an error occurs, return the number of bytes that have been // received successfully. result -= length; // Force STOP condition. stop = 1; break; } *data++ = (uint8_t)byte_read_result; --length; } if (stop) { (void)i2c_stop(obj); } return result; }
/* Returns number of bytes read */ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { int retval; i2c_start(obj); retval = i2c_byte_write(obj, (address | 1)); if ((!retval) || (length == 0)) { //Write address with W flag (last bit 1) obj->i2c.i2c->CMD = I2C_CMD_STOP | I2C_CMD_ABORT; while(obj->i2c.i2c->STATE & I2C_STATE_BUSY); // Wait until the bus is done return (retval == 0 ? I2C_ERROR_NO_SLAVE : 0); //NACK or error when writing adress. Return 0 as 0 bytes were read } int i = 0; while (i < length) { uint8_t last = (i == length - 1); data[i++] = i2c_byte_read(obj, last); } if (stop) { i2c_stop(obj); } return length; }
int I2CSlave::read(void) { return i2c_byte_read(&_i2c, 0); }