/*!
* \brief Reads data from I2C slave
*
* This function reads the specified number of bytes from an addressed I2C slave
*
* \param[in] handle Handle of the channel
* \param[in] deviceAddress Address of the I2C slave
* \param[in] sizeToTransfer Number of bytes to be read
* \param[out] buffer Pointer to the buffer where data is to be read
* \param[out] sizeTransferred Pointer to variable containing the number of bytes read
* \param[in] options This parameter specifies data transfer options. Namely, if a start/stop bits
* are required, if the transfer should continue or stop if device nAcks, etc
* \return Returns status code of type FT_STATUS(see D2XX Programmer's Guide)
* \sa Definitions of macros I2C_TRANSFER_OPTIONS_START_BIT,
* I2C_TRANSFER_OPTIONS_STOP_BIT, I2C_TRANSFER_OPTIONS_BREAK_ON_NACK,
* I2C_TRANSFER_OPTIONS_FAST_TRANSFER_BYTES,
* I2C_TRANSFER_OPTIONS_FAST_TRANSFER_BITS &
* I2C_TRANSFER_OPTIONS_NO_ADDRESS
* \note
* \warning
*/
FTDI_API FT_STATUS I2C_DeviceRead(FT_HANDLE handle, uint32 deviceAddress,
uint32 sizeToTransfer, uint8 *buffer, uint32 *sizeTransferred, uint32 options)
{
FT_STATUS status=FT_OK;
bool ack=TRUE;
uint32 i;
FN_ENTER;
#ifdef ENABLE_PARAMETER_CHECKING
CHECK_NULL_RET(handle);
CHECK_NULL_RET(buffer);
CHECK_NULL_RET(sizeTransferred);
if(deviceAddress>127)
{
DBG(MSG_WARN,"deviceAddress(0x%x) is greater than 127\n", \
(unsigned)deviceAddress);
return FT_INVALID_PARAMETER;
}
#endif
LOCK_CHANNEL(handle);
if(options & I2C_TRANSFER_OPTIONS_FAST_TRANSFER)
{
status = I2C_FastRead (handle, deviceAddress, sizeToTransfer, buffer, NULL, sizeTransferred, options);
}
else
{
/* Write START bit */
if(options & I2C_TRANSFER_OPTIONS_START_BIT)
{
status = I2C_Start(handle);
CHECK_STATUS(status);
}
/* Write device address (with LSB=1 => READ) & Get ACK */
status = I2C_WriteDeviceAddress(handle,deviceAddress,TRUE,FALSE,&ack);
CHECK_STATUS(status);
if(!ack) /* check acknowledgement of device address write */
{
for(i=0; (i<sizeToTransfer) && (status == FT_OK); i++)
{
/* Read byte to buffer & give ACK (or nACK if it is last byte and
I2C_TRANSFER_OPTIONS_NACK_LAST_BYTE is set)*/
status = I2C_Read8bitsAndGiveAck(handle,&(buffer[i]), \
(i<(sizeToTransfer-1))?TRUE: \
((options & I2C_TRANSFER_OPTIONS_NACK_LAST_BYTE)?FALSE:TRUE));
printf("i = %d\n", i);
}
printf("i = %d\n", i);
*sizeTransferred = i;
if(*sizeTransferred != sizeToTransfer)
{
DBG(MSG_ERR," sizeToTransfer=%u sizeTransferred=%u\n",\
(unsigned)sizeToTransfer, (unsigned)*sizeTransferred);
status = FT_IO_ERROR;
}
else
{
/* Write STOP bit */
if(options & I2C_TRANSFER_OPTIONS_STOP_BIT)
{
status = I2C_Stop(handle);
CHECK_STATUS(status);
}
}
}
else
{
DBG(MSG_ERR,"I2C device with address 0x%x didn't ack when addressed\n",\
(unsigned)deviceAddress);
/*20111102 : FT_IO_ERROR was returned when a device doesn't respond to the
master when it is addressed, as well as when a data transfer fails. To distinguish
between these to errors, FT_DEVICE_NOT_FOUND is now returned after a device
doesn't respond when its addressed*/
/* old code: status = FT_IO_ERROR; */
status = FT_DEVICE_NOT_FOUND;
}
}
UNLOCK_CHANNEL(handle);
FN_EXIT;
return status;
}
源码/*! * \brief Reads data from I2C slave * * This function reads the specified number of bytes from an addressed I2C slave * * \param[in] handle Handle of the channel * \param[in] deviceAddress...