diff --git a/server/src/Peripherals/DebuggerClient.cs b/server/src/Peripherals/DebuggerClient.cs index c85da01..609b150 100644 --- a/server/src/Peripherals/DebuggerClient.cs +++ b/server/src/Peripherals/DebuggerClient.cs @@ -55,28 +55,28 @@ class DebuggerCmd public const UInt32 ClearSignal = 0xFFFF_FFFF; } -/// +/// /// 信号捕获模式枚举 /// public enum CaptureMode : byte { - /// + /// /// 无捕获模式 /// None = 0, - /// + /// /// 低电平触发模式 /// Logic0 = 1, - /// + /// /// 高电平触发模式 /// Logic1 = 2, - /// + /// /// 上升沿触发模式 /// Rise = 3, - /// + /// /// 下降沿触发模式 /// Fall = 4, @@ -170,7 +170,7 @@ public class DebuggerClient /// 操作结果,成功返回状态标志字节,失败返回错误信息 public async ValueTask> ReadFlag() { - var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, DebuggerAddr.Signal, this.timeout); + var ret = await UDPClientPool.ReadAddrByte(this.ep, this.taskID, DebuggerAddr.Signal, this.timeout); if (!ret.IsSuccessful) { logger.Error($"Failed to read flag: {ret.Error}"); diff --git a/server/src/Peripherals/I2cClient.cs b/server/src/Peripherals/I2cClient.cs index 878e891..7ea28f4 100644 --- a/server/src/Peripherals/I2cClient.cs +++ b/server/src/Peripherals/I2cClient.cs @@ -8,8 +8,8 @@ static class I2cAddr const UInt32 Base = 0x6000_0000; - /// - /// 0x0000_0000: + /// + /// 0x0000_0000: /// [7:0] 本次传输的i2c地址(最高位总为0); /// [8] 1为读,0为写; /// [16] 1为SCCB协议,0为I2C协议; @@ -17,45 +17,45 @@ static class I2cAddr /// public const UInt32 BaseConfig = Base + 0x0000_0000; - /// + /// /// 0x0000_0001: /// [15:0] 本次传输的数据量(以字节为单位,0为传1个字节); /// [31:16] 若本次传输为读的DUMMY数据量(字节为单位,0为传1个字节) /// public const UInt32 TranConfig = Base + 0x0000_0001; - /// + /// /// 0x0000_0002: [0] cmd_done; [8] cmd_error; /// public const UInt32 Flag = Base + 0x0000_0002; - /// + /// /// 0x0000_0003: FIFO写入口,仅低8位有效,只写 /// public const UInt32 Write = Base + 0x0000_0003; - /// + /// /// 0x0000_0004: FIFO读出口,仅低8位有效,只读 /// public const UInt32 Read = Base + 0x0000_0004; - /// + /// /// 0x0000_0005: [0] FIFO写入口清空;[8] FIFO读出口清空; /// public const UInt32 Clear = Base + 0x0000_0005; } -/// +/// /// [TODO:Enum] /// public enum I2cProtocol { - /// + /// /// [TODO:Enum] /// I2c = 0, - /// + /// /// [TODO:Enum] /// SCCB = 1 @@ -296,7 +296,7 @@ public class I2c // 读取数据 { - var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, I2cAddr.Read); + var ret = await UDPClientPool.ReadAddrByte(this.ep, this.taskID, I2cAddr.Read); if (!ret.IsSuccessful) { logger.Error($"Failed to read data from I2C FIFO: {ret.Error}"); diff --git a/server/src/Peripherals/JpegClient.cs b/server/src/Peripherals/JpegClient.cs new file mode 100644 index 0000000..870c441 --- /dev/null +++ b/server/src/Peripherals/JpegClient.cs @@ -0,0 +1,281 @@ +using System.Net; +using DotNext; +using Common; + +namespace Peripherals.JpegClient; + +static class JpegAddr +{ + const UInt32 BASE = 0x0000_0000; + public const UInt32 ENABLE = BASE + 0x0; + public const UInt32 FRAME_NUM = BASE + 0x1; + public const UInt32 FRAME_INFO = BASE + 0x2; + public const UInt32 FRAME_SAMPLE_RATE = BASE + 0x3; + public const UInt32 FRAME_DATA_MAX_POINTER = BASE + 0x4; + + public const UInt32 DDR_FRAME_DATA_ADDR = 0x0000_0000; + public const UInt32 DDR_FRAME_DATA_MAX_ADDR = 0x8000_0000; +} + +public class JpegInfo +{ + public UInt32 Width { get; set; } + public UInt32 Height { get; set; } + public UInt32 Size { get; set; } + + public JpegInfo(UInt32 width, UInt32 height, UInt32 size) + { + Width = width; + Height = height; + Size = size; + } + + public JpegInfo(byte[] data) + { + if (data.Length < 8) + throw new ArgumentException("Invalid data length", nameof(data)); + + Width = ((UInt32)(data[5] << 8 + data[6] & 0xF0)); + Height = ((UInt32)((data[6] & 0x0F) << 4 + data[7])); + Size = Number.BytesToUInt32(data, 0, 4).Value; + } +} + +public enum JpegSampleRate : UInt32 +{ + RATE_1_1 = 0b1111_1111_1111_1111_1111_1111_1111_1111, + RATE_1_2 = 0b1010_1010_1010_1010_1010_1010_1010_1010, + RATE_1_4 = 0b1000_1000_1000_1000_1000_1000_1000_1000, + RATE_3_4 = 0b1110_1110_1110_1110_1110_1110_1110_1110, + RATE_1_8 = 0b1000_0000_1000_0000_1000_0000_1000_0000, + RATE_3_8 = 0b1001_0010_0100_1001_1001_0010_0100_1001, + RATE_7_8 = 0b1111_1110_1111_1110_1111_1110_1111_1110, + RATE_1_16 = 0b1000_0000_0000_0000_1000_0000_0000_0000, + RATE_3_16 = 0b1000_0100_0010_0000_1000_0100_0010_0000, + RATE_5_16 = 0b1001_0001_0010_0010_0100_0100_1000_1001, + RATE_15_16 = 0b1111_1111_1111_1110_1111_1111_1111_1110, + RATE_1_32 = 0b1000_0000_0000_0000_0000_0000_0000_0000, + RATE_31_32 = 0b1111_1111_1111_1111_1111_1111_1111_1110, +} + +public class Jpeg +{ + private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); + + readonly int timeout = 2000; + readonly int taskID; + readonly int port; + readonly string address; + private IPEndPoint ep; + + public Jpeg(string address, int port, int taskID, int timeout = 2000) + { + if (timeout < 0) + throw new ArgumentException("Timeout couldn't be negative", nameof(timeout)); + this.address = address; + this.taskID = taskID; + this.port = port; + this.ep = new IPEndPoint(IPAddress.Parse(address), port); + this.timeout = timeout; + } + + public async ValueTask SetEnable(bool enable) + { + var ret = await UDPClientPool.WriteAddr( + this.ep, this.taskID, JpegAddr.ENABLE, Convert.ToUInt32(enable), this.timeout); + if (!ret.IsSuccessful) + { + logger.Error($"Failed to set JPEG enable: {ret.Error}"); + return false; + } + return ret.Value; + } + + public async ValueTask SetSampleRate(uint rate) + { + var ret = await UDPClientPool.WriteAddr( + this.ep, this.taskID, JpegAddr.FRAME_SAMPLE_RATE, rate, this.timeout); + if (!ret.IsSuccessful) + { + logger.Error($"Failed to set JPEG sample rate: {ret.Error}"); + return false; + } + return ret.Value; + } + + public async ValueTask SetSampleRate(JpegSampleRate rate) + { + return await SetSampleRate((uint)rate); + } + + public async ValueTask GetFrameNumber() + { + var ret = await UDPClientPool.ReadAddrByte( + this.ep, this.taskID, JpegAddr.FRAME_NUM, this.timeout); + if (!ret.IsSuccessful) + { + logger.Error($"Failed to get JPEG frame number: {ret.Error}"); + return 0; + } + return Number.BytesToUInt32(ret.Value.Options.Data ?? Array.Empty()).Value; + } + + public async ValueTask>> GetFrameInfo(int num) + { + var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, JpegAddr.FRAME_INFO, num, this.timeout); + if (!ret.IsSuccessful) + { + logger.Error($"Failed to get JPEG frame info: {ret.Error}"); + return new(null); + } + + var data = ret.Value.Options.Data; + if (data == null || data.Length == 0) + { + logger.Error($"Data is null or empty"); + return new(null); + } + if (data.Length != num * 2) + { + logger.Error( + $"Data length should be {num * 2} bytes, instead of {data.Length} bytes"); + return new(null); + } + + var infos = new List(); + for (int i = 0; i < num; i++) + { + infos.Add(new JpegInfo(data[i..(i + 1)])); + } + return new(infos); + } + + public async ValueTask UpdatePointer(uint cnt) + { + var ret = await UDPClientPool.WriteAddr( + this.ep, this.taskID, JpegAddr.FRAME_DATA_MAX_POINTER, cnt, this.timeout); + if (!ret.IsSuccessful) + { + logger.Error($"Failed to update pointer: {ret.Error}"); + return false; + } + return ret.Value; + } + + public async ValueTask> GetFrame(uint offset, uint length) + { + if (!MsgBus.IsRunning) + { + logger.Error("Message bus is not running"); + return new(new Exception("Message bus is not running")); + } + MsgBus.UDPServer.ClearUDPData(this.ep.Address.ToString(), this.ep.Port); + + var firstReadLength = (int)(Math.Min(length, JpegAddr.DDR_FRAME_DATA_MAX_ADDR - offset)); + var secondReadLength = (int)(length - firstReadLength); + var dataBytes = new byte[length]; + + { + var ret = await UDPClientPool.ReadAddr4Bytes( + this.ep, this.taskID, JpegAddr.DDR_FRAME_DATA_ADDR + offset, firstReadLength, this.timeout); + if (!ret.IsSuccessful) + { + logger.Error($"Failed to get JPEG frame data: {ret.Error}"); + return null; + } + if (ret.Value.Length != firstReadLength) + { + logger.Error($"Data length should be {firstReadLength} bytes, instead of {ret.Value.Length} bytes"); + return null; + } + Buffer.BlockCopy(ret.Value, 0, dataBytes, 0, firstReadLength); + } + + if (secondReadLength > 0) + { + var ret = await UDPClientPool.ReadAddr4Bytes( + this.ep, this.taskID, JpegAddr.DDR_FRAME_DATA_ADDR, secondReadLength, this.timeout); + if (!ret.IsSuccessful) + { + logger.Error($"Failed to get JPEG frame data: {ret.Error}"); + return null; + } + if (ret.Value.Length != secondReadLength) + { + logger.Error($"Data length should be {secondReadLength} bytes, instead of {ret.Value.Length} bytes"); + return null; + } + Buffer.BlockCopy(ret.Value, 0, dataBytes, firstReadLength, secondReadLength); + } + + return dataBytes; + } + + public async ValueTask> GetMultiFrames(uint offset, uint[] sizes) + { + var frames = new List(); + for (int i = 0; i < sizes.Length; i++) + { + var ret = await GetFrame(offset, sizes[i]); + if (!ret.IsSuccessful) + { + logger.Error($"Failed to get JPEG frame {i} data: {ret.Error}"); + continue; + } + if (ret.Value == null) + { + logger.Error($"Frame {i} data is null"); + continue; + } + if (ret.Value.Length != sizes[i]) + { + logger.Error( + $"Frame {i} data length should be {sizes[i]} bytes, instead of {ret.Value.Length} bytes"); + continue; + } + + frames.Add(ret.Value); + offset += sizes[i]; + } + + { + var ret = await UpdatePointer((uint)sizes.Length); + if (!ret) logger.Error($"Failed to update pointer"); + } + + return frames; + } + + public async ValueTask?>> GetMultiFrames(uint offset) + { + if (!MsgBus.IsRunning) + { + logger.Error("Message bus is not running"); + return new(new Exception("Message bus is not running")); + } + MsgBus.UDPServer.ClearUDPData(this.ep.Address.ToString(), this.ep.Port); + + var frameNum = await GetFrameNumber(); + if (frameNum == 0) return null; + + List? frameSizes = null; + { + var ret = await GetFrameInfo((int)frameNum); + if (!ret.HasValue || ret.Value.Count == 0) + { + logger.Error($"Failed to get frame info"); + return null; + } + frameSizes = ret.Value.Select(x => x.Size).ToList(); + } + + var frames = await GetMultiFrames(offset, frameSizes.ToArray()); + if (frames.Count == 0) + { + logger.Error($"Failed to get frames"); + return null; + } + + return frames; + } +} diff --git a/server/src/Peripherals/LogicAnalyzerClient.cs b/server/src/Peripherals/LogicAnalyzerClient.cs index 342ff5d..c6764d9 100644 --- a/server/src/Peripherals/LogicAnalyzerClient.cs +++ b/server/src/Peripherals/LogicAnalyzerClient.cs @@ -12,7 +12,7 @@ static class AnalyzerAddr const UInt32 DMA1_BASE = 0x7000_0000; const UInt32 DDR_BASE = 0x0000_0000; - /// + /// /// 0x0000_0000 R/W [ 0] capture on: 置1开始等待捕获,0停止捕获。捕获到信号后该位自动清零。
/// [ 8] capture force: 置1则强制捕获信号,自动置0。
/// [16] capture busy: 1为逻辑分析仪正在捕获信号。
@@ -21,7 +21,7 @@ static class AnalyzerAddr ///
public const UInt32 CAPTURE_MODE = BASE + 0x0000_0000; - /// + /// /// 0x0000_0001 R/W [1:0] global trig mode: 00: 全局与 (&)
/// 01: 全局或 (|)
/// 10: 全局非与(~&)
@@ -29,7 +29,7 @@ static class AnalyzerAddr ///
public const UInt32 GLOBAL_TRIG_MODE = BASE + 0x0000_0001; - /// + /// /// 0x0000_0010 - 0x0000_0017 R/W [5:0] 信号M的触发操作符,共8路
/// [5:3] M's Operator: 000 ==
/// 001 !=
@@ -73,7 +73,7 @@ static class AnalyzerAddr public const UInt32 DMA1_CAPTURE_CTRL_ADDR = DMA1_BASE + 0x0000_0014; public const UInt32 STORE_OFFSET_ADDR = DDR_BASE + 0x0100_0000; - /// + /// /// 0x0100_0000 - 0x0100_03FF 只读 32位波形存储,得到的32位数据中低八位最先捕获,高八位最后捕获。
/// 共1024个地址,每个地址存储4组,深度为4096。
///
@@ -87,53 +87,53 @@ static class AnalyzerAddr [Flags] public enum CaptureStatus { - /// + /// /// 无状态标志 /// None = 0, - /// + /// /// 捕获使能位,置1开始等待捕获,0停止捕获。捕获到信号后该位自动清零 /// CaptureOn = 1 << 0, // [0] 捕获使能 - /// + /// /// 强制捕获位,置1则强制捕获信号,自动置0 /// CaptureForce = 1 << 8, // [8] 强制捕获 - /// + /// /// 捕获忙碌位,1为逻辑分析仪正在捕获信号 /// CaptureBusy = 1 << 16, // [16] 捕获进行中 - /// + /// /// 捕获完成位,1为逻辑分析仪内存完整存储了此次捕获的信号 /// CaptureDone = 1 << 24 // [24] 捕获完成 } -/// +/// /// 全局触发模式枚举,定义多路信号触发条件的逻辑组合方式 /// public enum GlobalCaptureMode { - /// + /// /// 全局与模式,所有触发条件都必须满足 /// AND = 0b00, - /// + /// /// 全局或模式,任一触发条件满足即可 /// OR = 0b01, - /// + /// /// 全局非与模式,不是所有触发条件都满足 /// NAND = 0b10, - /// + /// /// 全局非或模式,所有触发条件都不满足 /// NOR = 0b11 @@ -144,32 +144,32 @@ public enum GlobalCaptureMode /// public enum AnalyzerClockDiv { - /// + /// /// 1分频 /// DIV1 = 0x0000_0000, - /// + /// /// 2分频 /// DIV2 = 0x0000_0001, - /// + /// /// 4分频 /// DIV4 = 0x0000_0002, - /// + /// /// 8分频 /// DIV8 = 0x0000_0003, - /// + /// /// 16分频 /// DIV16 = 0x0000_0004, - /// + /// /// 32分频 /// DIV32 = 0x0000_0005, @@ -190,27 +190,27 @@ public enum AnalyzerClockDiv /// public enum SignalOperator : byte { - /// + /// /// 等于操作符 /// Equal = 0b000, // == - /// + /// /// 不等于操作符 /// NotEqual = 0b001, // != - /// + /// /// 小于操作符 /// LessThan = 0b010, // < - /// + /// /// 小于等于操作符 /// LessThanOrEqual = 0b011, // <= - /// + /// /// 大于操作符 /// GreaterThan = 0b100, // > - /// + /// /// 大于等于操作符 /// GreaterThanOrEqual = 0b101 // >= @@ -221,35 +221,35 @@ public enum SignalOperator : byte /// public enum SignalValue : byte { - /// + /// /// 逻辑0电平 /// Logic0 = 0b000, // LOGIC 0 - /// + /// /// 逻辑1电平 /// Logic1 = 0b001, // LOGIC 1 - /// + /// /// 不关心该信号状态 /// NotCare = 0b010, // X(not care) - /// + /// /// 上升沿触发 /// Rise = 0b011, // RISE - /// + /// /// 下降沿触发 /// Fall = 0b100, // FALL - /// + /// /// 上升沿或下降沿触发 /// RiseOrFall = 0b101, // RISE OR FALL - /// + /// /// 信号无变化 /// NoChange = 0b110, // NOCHANGE - /// + /// /// 特定数值 /// SomeNumber = 0b111 // SOME NUMBER @@ -260,11 +260,11 @@ public enum SignalValue : byte /// public enum AnalyzerChannelDiv { - /// + /// /// 1路 /// ONE = 0x0000_0000, - /// + /// /// 2路 /// TWO = 0x0000_0001, @@ -366,7 +366,7 @@ public class Analyzer /// 操作结果,成功返回寄存器值,否则返回异常信息 public async ValueTask> ReadCaptureStatus() { - var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, AnalyzerAddr.CAPTURE_MODE, this.timeout); + var ret = await UDPClientPool.ReadAddrByte(this.ep, this.taskID, AnalyzerAddr.CAPTURE_MODE, this.timeout); if (!ret.IsSuccessful) { logger.Error($"Failed to read capture status: {ret.Error}"); diff --git a/server/src/Peripherals/OscilloscopeClient.cs b/server/src/Peripherals/OscilloscopeClient.cs index 173924e..d3055a1 100644 --- a/server/src/Peripherals/OscilloscopeClient.cs +++ b/server/src/Peripherals/OscilloscopeClient.cs @@ -9,57 +9,57 @@ static class OscilloscopeAddr { const UInt32 BASE = 0x8000_0000; - /// + /// /// 0x0000_0000:R/W[0] wave_run 启动捕获/关闭 /// public const UInt32 START_CAPTURE = BASE + 0x0000_0000; - /// + /// /// 0x0000_0001: R/W[7:0] trig_level 触发电平 /// public const UInt32 TRIG_LEVEL = BASE + 0x0000_0001; - /// + /// /// 0x0000_0002:R/W[0] trig_edge 触发边沿,0-下降沿,1-上升沿 /// public const UInt32 TRIG_EDGE = BASE + 0x0000_0002; - /// + /// /// 0x0000_0003: R/W[9:0] h shift 水平偏移量 /// public const UInt32 H_SHIFT = BASE + 0x0000_0003; - /// + /// /// 0x0000_0004: R/W[9:0] deci rate 抽样率,0—1023 /// public const UInt32 DECI_RATE = BASE + 0x0000_0004; - /// + /// /// 0x0000_0005:R/W[0] ram refresh RAM刷新 /// public const UInt32 RAM_FRESH = BASE + 0x0000_0005; - /// + /// /// 0x0000 0006:R[19: 0] ad_freq AD采样频率 /// public const UInt32 AD_FREQ = BASE + 0x0000_0006; - /// + /// /// Ox0000_0007: R[7:0] ad_vpp AD采样幅度 /// public const UInt32 AD_VPP = BASE + 0x0000_0007; - /// + /// /// 0x0000_0008: R[7:0] ad max AD采样最大值 /// public const UInt32 AD_MAX = BASE + 0x0000_0008; - /// + /// /// 0x0000_0009: R[7:0] ad_min AD采样最小值 /// public const UInt32 AD_MIN = BASE + 0x0000_0009; - /// + /// /// 0x0000_1000-0x0000_13FF:R[7:0] wave_rd_data 共1024个字节 /// public const UInt32 RD_DATA_ADDR = BASE + 0x0000_1000; @@ -232,7 +232,7 @@ class Oscilloscope /// 操作结果,成功返回采样频率值,否则返回异常信息 public async ValueTask> GetADFrequency() { - var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, OscilloscopeAddr.AD_FREQ, this.timeout); + var ret = await UDPClientPool.ReadAddrByte(this.ep, this.taskID, OscilloscopeAddr.AD_FREQ, this.timeout); if (!ret.IsSuccessful) { logger.Error($"Failed to read AD frequency: {ret.Error}"); @@ -255,7 +255,7 @@ class Oscilloscope /// 操作结果,成功返回采样幅度值,否则返回异常信息 public async ValueTask> GetADVpp() { - var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, OscilloscopeAddr.AD_VPP, this.timeout); + var ret = await UDPClientPool.ReadAddrByte(this.ep, this.taskID, OscilloscopeAddr.AD_VPP, this.timeout); if (!ret.IsSuccessful) { logger.Error($"Failed to read AD VPP: {ret.Error}"); @@ -275,7 +275,7 @@ class Oscilloscope /// 操作结果,成功返回采样最大值,否则返回异常信息 public async ValueTask> GetADMax() { - var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, OscilloscopeAddr.AD_MAX, this.timeout); + var ret = await UDPClientPool.ReadAddrByte(this.ep, this.taskID, OscilloscopeAddr.AD_MAX, this.timeout); if (!ret.IsSuccessful) { logger.Error($"Failed to read AD max: {ret.Error}"); @@ -295,7 +295,7 @@ class Oscilloscope /// 操作结果,成功返回采样最小值,否则返回异常信息 public async ValueTask> GetADMin() { - var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, OscilloscopeAddr.AD_MIN, this.timeout); + var ret = await UDPClientPool.ReadAddrByte(this.ep, this.taskID, OscilloscopeAddr.AD_MIN, this.timeout); if (!ret.IsSuccessful) { logger.Error($"Failed to read AD min: {ret.Error}"); diff --git a/server/src/Peripherals/RemoteUpdateClient.cs b/server/src/Peripherals/RemoteUpdateClient.cs index 296afb8..922f3e2 100644 --- a/server/src/Peripherals/RemoteUpdateClient.cs +++ b/server/src/Peripherals/RemoteUpdateClient.cs @@ -7,20 +7,20 @@ static class RemoteUpdaterAddr { public const UInt32 Base = 0x20_00_00_00; - /// + /// /// ADDR: 0X00: 写Flash-读写地址——控制位
/// [31:16]: wr_sector_num
/// [15: 0]: {flash_wr_en,-,-,-, start_wr_sector}
///
public const UInt32 WriteCtrl = Base + 0x00; - /// + /// /// ADDR: 0X01: 写Flash-只写地址——FIFO入口
/// [31:0]: 写比特流数据入口
///
public const UInt32 WriteFIFO = Base + 0x01; - /// + /// /// ADDR: 0X02: 写Flash-只读地址——标志位
/// [31:24]: {-, -, -, -, -, -, -, wr_fifo_full}
/// [23:16]: {-, -, -, -, -, -, -, wr_fifo_empty}
@@ -29,14 +29,14 @@ static class RemoteUpdaterAddr ///
public const UInt32 WriteSign = Base + 0x02; - /// + /// /// ADDR: 0X03: 读Flash-读写地址——控制位1
/// [31:16]: rd_sector_num
/// [15: 0]: {flash_rd_en,-,-,-, start_rd_sub_sector}
///
public const UInt32 ReadCtrl1 = Base + 0x03; - /// + /// /// ADDR: 0X04: 读Flash-读写地址——控制位2
/// [31:24]: { }
/// [23:16]: {-, -, -, -, -, -,{ bs_crc32_ok }}
@@ -45,19 +45,19 @@ static class RemoteUpdaterAddr ///
public const UInt32 ReadCtrl2 = Base + 0x04; - /// + /// /// ADDR: 0X05: 读Flash-只读地址——FIFO出口
/// [31:0]: 读比特流数据出口
///
public const UInt32 ReadFIFO = Base + 0x05; - /// + /// /// ADDR: 0X06: 读Flash-只读地址——CRC校验值
/// [31:0]: CRC校验值 bs_readback_crc
///
public const UInt32 ReadCRC = Base + 0x06; - /// + /// /// ADDR: 0X07: 读Flash-只读地址——标志位
/// [31:24]: {-, -, -, -, -, -, -, rd_fifo_afull}
/// [23:16]: {-, -, -, -, -, -, -, rd_fifo_empty}
@@ -66,14 +66,14 @@ static class RemoteUpdaterAddr ///
public const UInt32 ReadSign = Base + 0x07; - /// + /// /// ADDR: 0X08: 热启动开关-读写地址——控制位
/// [31: 8]: hotreset_addr
/// [ 7: 0]: {-, -, -, -, -, -, -, hotreset_en}
///
public const UInt32 HotResetCtrl = Base + 0x08; - /// + /// /// ADDR: 0X09: 只读地址 版本号
/// [31: 0]: FPGA_VERSION[31:0]
///
@@ -339,7 +339,7 @@ public class RemoteUpdater } { - var ret = await UDPClientPool.ReadAddr(this.ep, 0, RemoteUpdaterAddr.ReadCRC, this.timeout); + var ret = await UDPClientPool.ReadAddrByte(this.ep, 0, RemoteUpdaterAddr.ReadCRC, this.timeout); if (!ret.IsSuccessful) return new(ret.Error); var bytes = ret.Value.Options.Data; @@ -543,7 +543,7 @@ public class RemoteUpdater logger.Trace("Clear udp data finished"); { - var ret = await UDPClientPool.ReadAddr(this.ep, 0, RemoteUpdaterAddr.Version, this.timeout); + var ret = await UDPClientPool.ReadAddrByte(this.ep, 0, RemoteUpdaterAddr.Version, this.timeout); if (!ret.IsSuccessful) return new(ret.Error); var retData = ret.Value.Options.Data; diff --git a/server/src/UdpClientPool.cs b/server/src/UdpClientPool.cs index 3cc45b1..582d7d9 100644 --- a/server/src/UdpClientPool.cs +++ b/server/src/UdpClientPool.cs @@ -223,22 +223,28 @@ public class UDPClientPool /// IP端点(IP地址与端口) /// 任务ID /// 设备地址 + /// 数据长度(0~255) /// 超时时间(毫秒) /// 读取结果,包含接收到的数据包 public static async ValueTask> ReadAddr( - IPEndPoint endPoint, int taskID, uint devAddr, int timeout = 1000) + IPEndPoint endPoint, int taskID, uint devAddr, int dataLength, int timeout = 1000) { + if (dataLength <= 0) + return new(new ArgumentException("Data length must be greater than 0")); + + if (dataLength > 255) + return new(new ArgumentException("Data length must be less than or equal to 255")); + var ret = false; var opts = new SendAddrPackOptions() { BurstType = BurstType.FixedBurst, - BurstLength = 0, + BurstLength = ((byte)(dataLength - 1)), CommandID = Convert.ToByte(taskID), Address = devAddr, IsWrite = false, }; - // Read Register ret = await UDPClientPool.SendAddrPackAsync(endPoint, new SendAddrPackage(opts)); if (!ret) return new(new Exception("Send Address Package Failed!")); @@ -260,6 +266,20 @@ public class UDPClientPool return retPack; } + /// + /// 读取设备地址数据 + /// + /// IP端点(IP地址与端口) + /// 任务ID + /// 设备地址 + /// 超时时间(毫秒) + /// 读取结果,包含接收到的数据包 + public static async ValueTask> ReadAddrByte( + IPEndPoint endPoint, int taskID, uint devAddr, int timeout = 1000) + { + return await ReadAddr(endPoint, taskID, devAddr, 0, timeout); + } + /// /// 读取设备地址数据并校验结果 /// @@ -271,11 +291,11 @@ public class UDPClientPool /// 超时时间(毫秒) /// 校验结果,true表示数据匹配期望值 public static async ValueTask> ReadAddr( - IPEndPoint endPoint, int taskID, uint devAddr, UInt32 result, UInt32 resultMask, int timeout = 1000) + IPEndPoint endPoint, int taskID, uint devAddr, UInt32 result, UInt32 resultMask, int timeout = 1000) { var address = endPoint.Address.ToString(); - var ret = await ReadAddr(endPoint, taskID, devAddr, timeout); + var ret = await ReadAddrByte(endPoint, taskID, devAddr, timeout); if (!ret.IsSuccessful) return new(ret.Error); if (!ret.Value.IsSuccessful) return new(new Exception($"Read device {address} address {devAddr} failed")); @@ -324,7 +344,7 @@ public class UDPClientPool await Task.Delay(waittime); try { - var ret = await ReadAddr(endPoint, taskID, devAddr, Convert.ToInt32(timeleft.TotalMilliseconds)); + var ret = await ReadAddrByte(endPoint, taskID, devAddr, Convert.ToInt32(timeleft.TotalMilliseconds)); if (!ret.IsSuccessful) return new(ret.Error); if (!ret.Value.IsSuccessful) return new(new Exception($"Read device {address} address {devAddr} failed")); @@ -555,7 +575,7 @@ public class UDPClientPool var resultData = new List(); for (int i = 0; i < length; i++) { - var ret = await ReadAddr(endPoint, taskID, addr[i], timeout); + var ret = await ReadAddrByte(endPoint, taskID, addr[i], timeout); if (!ret.IsSuccessful) { logger.Error($"ReadAddrSeq failed at index {i}: {ret.Error}");