fix: 修改I2C读逻辑
This commit is contained in:
		@@ -37,12 +37,12 @@ static class I2cAddr
 | 
			
		||||
    /// <summary> 
 | 
			
		||||
    /// 0x0000_0004: FIFO读出口,仅低8位有效,只读
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public const UInt32 Read = Base + 0x0000_0003;
 | 
			
		||||
    public const UInt32 Read = Base + 0x0000_0004;
 | 
			
		||||
 | 
			
		||||
    /// <summary> 
 | 
			
		||||
    /// 0x0000_0005: [0] FIFO写入口清空;[8] FIFO读出口清空;
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public const UInt32 Clear = Base + 0x0000_0003;
 | 
			
		||||
    public const UInt32 Clear = Base + 0x0000_0005;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// <summary> 
 | 
			
		||||
@@ -195,10 +195,11 @@ public class I2c
 | 
			
		||||
    /// 从指定I2C设备读取数据
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    /// <param name="devAddr">I2C设备地址</param>
 | 
			
		||||
    /// <param name="data">要写入的数据(dummy数据)</param>
 | 
			
		||||
    /// <param name="length">要读取的数据长度</param>
 | 
			
		||||
    /// <param name="proto">I2C协议类型</param>
 | 
			
		||||
    /// <returns>操作结果,成功返回读取到的数据,否则返回异常信息</returns>
 | 
			
		||||
    public async ValueTask<Result<byte[]>> ReadData(UInt32 devAddr, int length, I2cProtocol proto)
 | 
			
		||||
    public async ValueTask<Result<byte[]>> ReadData(UInt32 devAddr, byte[] data, int length, I2cProtocol proto)
 | 
			
		||||
    {
 | 
			
		||||
        if (length <= 0 || length > 0x0000_FFFF)
 | 
			
		||||
        {
 | 
			
		||||
@@ -206,14 +207,47 @@ public class I2c
 | 
			
		||||
            return new(new ArgumentException($"Read length {length} is invalid or exceeds maximum allowed 0x0000_FFFF"));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (data.Length > 0x0000_FFFF)
 | 
			
		||||
        {
 | 
			
		||||
            logger.Error($"Data length {data.Length} exceeds maximum allowed 0x0000_FFFF");
 | 
			
		||||
            return new(new ArgumentException($"Data length {data.Length} exceeds maximum allowed 0x0000_FFFF"));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // 清除UDP服务器接收缓冲区
 | 
			
		||||
        await MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
 | 
			
		||||
 | 
			
		||||
        logger.Trace($"Clear up udp server {this.address} receive data");
 | 
			
		||||
 | 
			
		||||
        // 配置本次传输数据量
 | 
			
		||||
        // 配置写FIFO内容,内容为data[]
 | 
			
		||||
        {
 | 
			
		||||
            var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, I2cAddr.TranConfig, ((uint)(length - 1)));
 | 
			
		||||
            var i2cData = new byte[data.Length * 4];
 | 
			
		||||
            int i = 0;
 | 
			
		||||
            foreach (var item in data)
 | 
			
		||||
            {
 | 
			
		||||
                i2cData[i++] = 0x00;
 | 
			
		||||
                i2cData[i++] = 0x00;
 | 
			
		||||
                i2cData[i++] = 0x00;
 | 
			
		||||
                i2cData[i++] = item;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, I2cAddr.Write, i2cData);
 | 
			
		||||
            if (!ret.IsSuccessful)
 | 
			
		||||
            {
 | 
			
		||||
                logger.Error($"Failed to write data to I2C FIFO: {ret.Error}");
 | 
			
		||||
                return new(ret.Error);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (!ret.Value)
 | 
			
		||||
            {
 | 
			
		||||
                logger.Error("WriteAddr to I2C FIFO returned false");
 | 
			
		||||
                return new(new Exception("Failed to write data to I2C FIFO"));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // 配置本次传输数据量:[15:0]为dummy长度(data.Length-1),[31:16]为读长度(length-1)
 | 
			
		||||
        {
 | 
			
		||||
            uint tranConfig = ((uint)(data.Length - 1)) | (((uint)(length - 1)) << 16);
 | 
			
		||||
            var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, I2cAddr.TranConfig, tranConfig);
 | 
			
		||||
            if (!ret.IsSuccessful)
 | 
			
		||||
            {
 | 
			
		||||
                logger.Error($"Failed to configure transfer length: {ret.Error}");
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user