LibMPSSE I2C学习笔记----I2C设备读函数源码学习

源码

/*!
 * \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;
}

LOCK_CHANNEL与UNLOCK_CHANNEL

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值