feat: 完成基本的Jpeg控制
This commit is contained in:
parent
58378851bb
commit
771f5e8e9f
|
@ -55,28 +55,28 @@ class DebuggerCmd
|
|||
public const UInt32 ClearSignal = 0xFFFF_FFFF;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 信号捕获模式枚举
|
||||
/// </summary>
|
||||
public enum CaptureMode : byte
|
||||
{
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 无捕获模式
|
||||
/// </summary>
|
||||
None = 0,
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 低电平触发模式
|
||||
/// </summary>
|
||||
Logic0 = 1,
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 高电平触发模式
|
||||
/// </summary>
|
||||
Logic1 = 2,
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 上升沿触发模式
|
||||
/// </summary>
|
||||
Rise = 3,
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 下降沿触发模式
|
||||
/// </summary>
|
||||
Fall = 4,
|
||||
|
@ -170,7 +170,7 @@ public class DebuggerClient
|
|||
/// <returns>操作结果,成功返回状态标志字节,失败返回错误信息</returns>
|
||||
public async ValueTask<Result<byte>> 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}");
|
||||
|
|
|
@ -8,8 +8,8 @@ static class I2cAddr
|
|||
|
||||
const UInt32 Base = 0x6000_0000;
|
||||
|
||||
/// <summary>
|
||||
/// 0x0000_0000:
|
||||
/// <summary>
|
||||
/// 0x0000_0000:
|
||||
/// [7:0] 本次传输的i2c地址(最高位总为0);
|
||||
/// [8] 1为读,0为写;
|
||||
/// [16] 1为SCCB协议,0为I2C协议;
|
||||
|
@ -17,45 +17,45 @@ static class I2cAddr
|
|||
/// </summary>
|
||||
public const UInt32 BaseConfig = Base + 0x0000_0000;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 0x0000_0001:
|
||||
/// [15:0] 本次传输的数据量(以字节为单位,0为传1个字节);
|
||||
/// [31:16] 若本次传输为读的DUMMY数据量(字节为单位,0为传1个字节)
|
||||
/// </summary>
|
||||
public const UInt32 TranConfig = Base + 0x0000_0001;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 0x0000_0002: [0] cmd_done; [8] cmd_error;
|
||||
/// </summary>
|
||||
public const UInt32 Flag = Base + 0x0000_0002;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 0x0000_0003: FIFO写入口,仅低8位有效,只写
|
||||
/// </summary>
|
||||
public const UInt32 Write = Base + 0x0000_0003;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 0x0000_0004: FIFO读出口,仅低8位有效,只读
|
||||
/// </summary>
|
||||
public const UInt32 Read = Base + 0x0000_0004;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 0x0000_0005: [0] FIFO写入口清空;[8] FIFO读出口清空;
|
||||
/// </summary>
|
||||
public const UInt32 Clear = Base + 0x0000_0005;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// [TODO:Enum]
|
||||
/// </summary>
|
||||
public enum I2cProtocol
|
||||
{
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// [TODO:Enum]
|
||||
/// </summary>
|
||||
I2c = 0,
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// [TODO:Enum]
|
||||
/// </summary>
|
||||
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}");
|
||||
|
|
|
@ -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<bool> 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<bool> 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<bool> SetSampleRate(JpegSampleRate rate)
|
||||
{
|
||||
return await SetSampleRate((uint)rate);
|
||||
}
|
||||
|
||||
public async ValueTask<uint> 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<byte>()).Value;
|
||||
}
|
||||
|
||||
public async ValueTask<Optional<List<JpegInfo>>> 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<JpegInfo>();
|
||||
for (int i = 0; i < num; i++)
|
||||
{
|
||||
infos.Add(new JpegInfo(data[i..(i + 1)]));
|
||||
}
|
||||
return new(infos);
|
||||
}
|
||||
|
||||
public async ValueTask<bool> 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<Result<byte[]?>> 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<List<byte[]>> GetMultiFrames(uint offset, uint[] sizes)
|
||||
{
|
||||
var frames = new List<byte[]>();
|
||||
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<Result<List<byte[]>?>> 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<uint>? 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;
|
||||
}
|
||||
}
|
|
@ -12,7 +12,7 @@ static class AnalyzerAddr
|
|||
const UInt32 DMA1_BASE = 0x7000_0000;
|
||||
const UInt32 DDR_BASE = 0x0000_0000;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 0x0000_0000 R/W [ 0] capture on: 置1开始等待捕获,0停止捕获。捕获到信号后该位自动清零。 <br/>
|
||||
/// [ 8] capture force: 置1则强制捕获信号,自动置0。 <br/>
|
||||
/// [16] capture busy: 1为逻辑分析仪正在捕获信号。 <br/>
|
||||
|
@ -21,7 +21,7 @@ static class AnalyzerAddr
|
|||
/// </summary>
|
||||
public const UInt32 CAPTURE_MODE = BASE + 0x0000_0000;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 0x0000_0001 R/W [1:0] global trig mode: 00: 全局与 (&) <br/>
|
||||
/// 01: 全局或 (|) <br/>
|
||||
/// 10: 全局非与(~&) <br/>
|
||||
|
@ -29,7 +29,7 @@ static class AnalyzerAddr
|
|||
/// </summary>
|
||||
public const UInt32 GLOBAL_TRIG_MODE = BASE + 0x0000_0001;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 0x0000_0010 - 0x0000_0017 R/W [5:0] 信号M的触发操作符,共8路 <br/>
|
||||
/// [5:3] M's Operator: 000 == <br/>
|
||||
/// 001 != <br/>
|
||||
|
@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 0x0100_0000 - 0x0100_03FF 只读 32位波形存储,得到的32位数据中低八位最先捕获,高八位最后捕获。<br/>
|
||||
/// 共1024个地址,每个地址存储4组,深度为4096。<br/>
|
||||
/// </summary>
|
||||
|
@ -87,53 +87,53 @@ static class AnalyzerAddr
|
|||
[Flags]
|
||||
public enum CaptureStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 无状态标志
|
||||
/// </summary>
|
||||
None = 0,
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 捕获使能位,置1开始等待捕获,0停止捕获。捕获到信号后该位自动清零
|
||||
/// </summary>
|
||||
CaptureOn = 1 << 0, // [0] 捕获使能
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 强制捕获位,置1则强制捕获信号,自动置0
|
||||
/// </summary>
|
||||
CaptureForce = 1 << 8, // [8] 强制捕获
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 捕获忙碌位,1为逻辑分析仪正在捕获信号
|
||||
/// </summary>
|
||||
CaptureBusy = 1 << 16, // [16] 捕获进行中
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 捕获完成位,1为逻辑分析仪内存完整存储了此次捕获的信号
|
||||
/// </summary>
|
||||
CaptureDone = 1 << 24 // [24] 捕获完成
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 全局触发模式枚举,定义多路信号触发条件的逻辑组合方式
|
||||
/// </summary>
|
||||
public enum GlobalCaptureMode
|
||||
{
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 全局与模式,所有触发条件都必须满足
|
||||
/// </summary>
|
||||
AND = 0b00,
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 全局或模式,任一触发条件满足即可
|
||||
/// </summary>
|
||||
OR = 0b01,
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 全局非与模式,不是所有触发条件都满足
|
||||
/// </summary>
|
||||
NAND = 0b10,
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 全局非或模式,所有触发条件都不满足
|
||||
/// </summary>
|
||||
NOR = 0b11
|
||||
|
@ -144,32 +144,32 @@ public enum GlobalCaptureMode
|
|||
/// </summary>
|
||||
public enum AnalyzerClockDiv
|
||||
{
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 1分频
|
||||
/// </summary>
|
||||
DIV1 = 0x0000_0000,
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 2分频
|
||||
/// </summary>
|
||||
DIV2 = 0x0000_0001,
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 4分频
|
||||
/// </summary>
|
||||
DIV4 = 0x0000_0002,
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 8分频
|
||||
/// </summary>
|
||||
DIV8 = 0x0000_0003,
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 16分频
|
||||
/// </summary>
|
||||
DIV16 = 0x0000_0004,
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 32分频
|
||||
/// </summary>
|
||||
DIV32 = 0x0000_0005,
|
||||
|
@ -190,27 +190,27 @@ public enum AnalyzerClockDiv
|
|||
/// </summary>
|
||||
public enum SignalOperator : byte
|
||||
{
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 等于操作符
|
||||
/// </summary>
|
||||
Equal = 0b000, // ==
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 不等于操作符
|
||||
/// </summary>
|
||||
NotEqual = 0b001, // !=
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 小于操作符
|
||||
/// </summary>
|
||||
LessThan = 0b010, // <
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 小于等于操作符
|
||||
/// </summary>
|
||||
LessThanOrEqual = 0b011, // <=
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 大于操作符
|
||||
/// </summary>
|
||||
GreaterThan = 0b100, // >
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 大于等于操作符
|
||||
/// </summary>
|
||||
GreaterThanOrEqual = 0b101 // >=
|
||||
|
@ -221,35 +221,35 @@ public enum SignalOperator : byte
|
|||
/// </summary>
|
||||
public enum SignalValue : byte
|
||||
{
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 逻辑0电平
|
||||
/// </summary>
|
||||
Logic0 = 0b000, // LOGIC 0
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 逻辑1电平
|
||||
/// </summary>
|
||||
Logic1 = 0b001, // LOGIC 1
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 不关心该信号状态
|
||||
/// </summary>
|
||||
NotCare = 0b010, // X(not care)
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 上升沿触发
|
||||
/// </summary>
|
||||
Rise = 0b011, // RISE
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 下降沿触发
|
||||
/// </summary>
|
||||
Fall = 0b100, // FALL
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 上升沿或下降沿触发
|
||||
/// </summary>
|
||||
RiseOrFall = 0b101, // RISE OR FALL
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 信号无变化
|
||||
/// </summary>
|
||||
NoChange = 0b110, // NOCHANGE
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 特定数值
|
||||
/// </summary>
|
||||
SomeNumber = 0b111 // SOME NUMBER
|
||||
|
@ -260,11 +260,11 @@ public enum SignalValue : byte
|
|||
/// </summary>
|
||||
public enum AnalyzerChannelDiv
|
||||
{
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 1路
|
||||
/// </summary>
|
||||
ONE = 0x0000_0000,
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 2路
|
||||
/// </summary>
|
||||
TWO = 0x0000_0001,
|
||||
|
@ -366,7 +366,7 @@ public class Analyzer
|
|||
/// <returns>操作结果,成功返回寄存器值,否则返回异常信息</returns>
|
||||
public async ValueTask<Result<CaptureStatus>> 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}");
|
||||
|
|
|
@ -9,57 +9,57 @@ static class OscilloscopeAddr
|
|||
{
|
||||
const UInt32 BASE = 0x8000_0000;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 0x0000_0000:R/W[0] wave_run 启动捕获/关闭
|
||||
/// </summary>
|
||||
public const UInt32 START_CAPTURE = BASE + 0x0000_0000;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 0x0000_0001: R/W[7:0] trig_level 触发电平
|
||||
/// </summary>
|
||||
public const UInt32 TRIG_LEVEL = BASE + 0x0000_0001;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 0x0000_0002:R/W[0] trig_edge 触发边沿,0-下降沿,1-上升沿
|
||||
/// </summary>
|
||||
public const UInt32 TRIG_EDGE = BASE + 0x0000_0002;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 0x0000_0003: R/W[9:0] h shift 水平偏移量
|
||||
/// </summary>
|
||||
public const UInt32 H_SHIFT = BASE + 0x0000_0003;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 0x0000_0004: R/W[9:0] deci rate 抽样率,0—1023
|
||||
/// </summary>
|
||||
public const UInt32 DECI_RATE = BASE + 0x0000_0004;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 0x0000_0005:R/W[0] ram refresh RAM刷新
|
||||
/// </summary>
|
||||
public const UInt32 RAM_FRESH = BASE + 0x0000_0005;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 0x0000 0006:R[19: 0] ad_freq AD采样频率
|
||||
/// </summary>
|
||||
public const UInt32 AD_FREQ = BASE + 0x0000_0006;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// Ox0000_0007: R[7:0] ad_vpp AD采样幅度
|
||||
/// </summary>
|
||||
public const UInt32 AD_VPP = BASE + 0x0000_0007;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 0x0000_0008: R[7:0] ad max AD采样最大值
|
||||
/// </summary>
|
||||
public const UInt32 AD_MAX = BASE + 0x0000_0008;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 0x0000_0009: R[7:0] ad_min AD采样最小值
|
||||
/// </summary>
|
||||
public const UInt32 AD_MIN = BASE + 0x0000_0009;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 0x0000_1000-0x0000_13FF:R[7:0] wave_rd_data 共1024个字节
|
||||
/// </summary>
|
||||
public const UInt32 RD_DATA_ADDR = BASE + 0x0000_1000;
|
||||
|
@ -232,7 +232,7 @@ class Oscilloscope
|
|||
/// <returns>操作结果,成功返回采样频率值,否则返回异常信息</returns>
|
||||
public async ValueTask<Result<UInt32>> 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
|
|||
/// <returns>操作结果,成功返回采样幅度值,否则返回异常信息</returns>
|
||||
public async ValueTask<Result<byte>> 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
|
|||
/// <returns>操作结果,成功返回采样最大值,否则返回异常信息</returns>
|
||||
public async ValueTask<Result<byte>> 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
|
|||
/// <returns>操作结果,成功返回采样最小值,否则返回异常信息</returns>
|
||||
public async ValueTask<Result<byte>> 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}");
|
||||
|
|
|
@ -7,20 +7,20 @@ static class RemoteUpdaterAddr
|
|||
{
|
||||
public const UInt32 Base = 0x20_00_00_00;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// ADDR: 0X00: 写Flash-读写地址——控制位 <br/>
|
||||
/// [31:16]: wr_sector_num <br/>
|
||||
/// [15: 0]: {flash_wr_en,-,-,-, start_wr_sector} <br/>
|
||||
/// </summary>
|
||||
public const UInt32 WriteCtrl = Base + 0x00;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// ADDR: 0X01: 写Flash-只写地址——FIFO入口 <br/>
|
||||
/// [31:0]: 写比特流数据入口 <br/>
|
||||
/// </summary>
|
||||
public const UInt32 WriteFIFO = Base + 0x01;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// ADDR: 0X02: 写Flash-只读地址——标志位 <br/>
|
||||
/// [31:24]: {-, -, -, -, -, -, -, wr_fifo_full} <br/>
|
||||
/// [23:16]: {-, -, -, -, -, -, -, wr_fifo_empty} <br/>
|
||||
|
@ -29,14 +29,14 @@ static class RemoteUpdaterAddr
|
|||
/// </summary>
|
||||
public const UInt32 WriteSign = Base + 0x02;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// ADDR: 0X03: 读Flash-读写地址——控制位1 <br/>
|
||||
/// [31:16]: rd_sector_num <br/>
|
||||
/// [15: 0]: {flash_rd_en,-,-,-, start_rd_sub_sector} <br/>
|
||||
/// </summary>
|
||||
public const UInt32 ReadCtrl1 = Base + 0x03;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// ADDR: 0X04: 读Flash-读写地址——控制位2 <br/>
|
||||
/// [31:24]: { } <br/>
|
||||
/// [23:16]: {-, -, -, -, -, -,{ bs_crc32_ok }} <br/>
|
||||
|
@ -45,19 +45,19 @@ static class RemoteUpdaterAddr
|
|||
/// </summary>
|
||||
public const UInt32 ReadCtrl2 = Base + 0x04;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// ADDR: 0X05: 读Flash-只读地址——FIFO出口 <br/>
|
||||
/// [31:0]: 读比特流数据出口 <br/>
|
||||
/// </summary>
|
||||
public const UInt32 ReadFIFO = Base + 0x05;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// ADDR: 0X06: 读Flash-只读地址——CRC校验值 <br/>
|
||||
/// [31:0]: CRC校验值 bs_readback_crc <br/>
|
||||
/// </summary>
|
||||
public const UInt32 ReadCRC = Base + 0x06;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// ADDR: 0X07: 读Flash-只读地址——标志位 <br/>
|
||||
/// [31:24]: {-, -, -, -, -, -, -, rd_fifo_afull} <br/>
|
||||
/// [23:16]: {-, -, -, -, -, -, -, rd_fifo_empty} <br/>
|
||||
|
@ -66,14 +66,14 @@ static class RemoteUpdaterAddr
|
|||
/// </summary>
|
||||
public const UInt32 ReadSign = Base + 0x07;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// ADDR: 0X08: 热启动开关-读写地址——控制位 <br/>
|
||||
/// [31: 8]: hotreset_addr <br/>
|
||||
/// [ 7: 0]: {-, -, -, -, -, -, -, hotreset_en} <br/>
|
||||
/// </summary>
|
||||
public const UInt32 HotResetCtrl = Base + 0x08;
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// ADDR: 0X09: 只读地址 版本号 <br/>
|
||||
/// [31: 0]: FPGA_VERSION[31:0] <br/>
|
||||
/// </summary>
|
||||
|
@ -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;
|
||||
|
|
|
@ -223,22 +223,28 @@ public class UDPClientPool
|
|||
/// <param name="endPoint">IP端点(IP地址与端口)</param>
|
||||
/// <param name="taskID">任务ID</param>
|
||||
/// <param name="devAddr">设备地址</param>
|
||||
/// <param name="dataLength">数据长度(0~255)</param>
|
||||
/// <param name="timeout">超时时间(毫秒)</param>
|
||||
/// <returns>读取结果,包含接收到的数据包</returns>
|
||||
public static async ValueTask<Result<RecvDataPackage>> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取设备地址数据
|
||||
/// </summary>
|
||||
/// <param name="endPoint">IP端点(IP地址与端口)</param>
|
||||
/// <param name="taskID">任务ID</param>
|
||||
/// <param name="devAddr">设备地址</param>
|
||||
/// <param name="timeout">超时时间(毫秒)</param>
|
||||
/// <returns>读取结果,包含接收到的数据包</returns>
|
||||
public static async ValueTask<Result<RecvDataPackage>> ReadAddrByte(
|
||||
IPEndPoint endPoint, int taskID, uint devAddr, int timeout = 1000)
|
||||
{
|
||||
return await ReadAddr(endPoint, taskID, devAddr, 0, timeout);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取设备地址数据并校验结果
|
||||
/// </summary>
|
||||
|
@ -271,11 +291,11 @@ public class UDPClientPool
|
|||
/// <param name="timeout">超时时间(毫秒)</param>
|
||||
/// <returns>校验结果,true表示数据匹配期望值</returns>
|
||||
public static async ValueTask<Result<bool>> 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<byte>();
|
||||
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}");
|
||||
|
|
Loading…
Reference in New Issue