diff --git a/server/src/Controllers/LogicAnalyzerController.cs b/server/src/Controllers/LogicAnalyzerController.cs new file mode 100644 index 0000000..7148252 --- /dev/null +++ b/server/src/Controllers/LogicAnalyzerController.cs @@ -0,0 +1,358 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Cors; +using Microsoft.AspNetCore.Mvc; +using Peripherals.LogicAnalyzerClient; + +namespace server.Controllers; + +/// +/// 逻辑分析仪控制器 +/// +[ApiController] +[Route("api/[controller]")] +[Authorize] +public class LogicAnalyzerController : ControllerBase +{ + private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); + + /// + /// 信号触发配置 + /// + public class SignalTriggerConfig + { + /// + /// 信号索引 (0-7) + /// + public int SignalIndex { get; set; } + + /// + /// 操作符 + /// + public SignalOperator Operator { get; set; } + + /// + /// 信号值 + /// + public SignalValue Value { get; set; } + } + + /// + /// 捕获配置 + /// + public class CaptureConfig + { + /// + /// 全局触发模式 + /// + public GlobalCaptureMode GlobalMode { get; set; } + + /// + /// 信号触发配置列表 + /// + public SignalTriggerConfig[] SignalConfigs { get; set; } = Array.Empty(); + } + + /// + /// 获取逻辑分析仪实例 + /// + private Analyzer? GetAnalyzer() + { + try + { + var userName = User.Identity?.Name; + if (string.IsNullOrEmpty(userName)) + return null; + + using var db = new Database.AppDataConnection(); + var userRet = db.GetUserByName(userName); + if (!userRet.IsSuccessful || !userRet.Value.HasValue) + return null; + + var user = userRet.Value.Value; + if (user.BoardID == Guid.Empty) + return null; + + var boardRet = db.GetBoardByID(user.BoardID); + if (!boardRet.IsSuccessful || !boardRet.Value.HasValue) + return null; + + var board = boardRet.Value.Value; + return new Analyzer(board.IpAddr, board.Port, 2); + } + catch (Exception ex) + { + logger.Error(ex, "获取逻辑分析仪实例时发生异常"); + return null; + } + } + + /// + /// 设置捕获模式 + /// + /// 是否开始捕获 + /// 是否强制捕获 + /// 操作结果 + [HttpPost("SetCaptureMode")] + [EnableCors("Users")] + [ProducesResponseType(typeof(bool), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + public async Task SetCaptureMode(bool captureOn, bool force = false) + { + try + { + var analyzer = GetAnalyzer(); + if (analyzer == null) + return BadRequest("用户未绑定有效的实验板"); + + var result = await analyzer.SetCaptureMode(captureOn, force); + if (!result.IsSuccessful) + { + logger.Error($"设置捕获模式失败: {result.Error}"); + return StatusCode(StatusCodes.Status500InternalServerError, "设置捕获模式失败"); + } + + return Ok(result.Value); + } + catch (Exception ex) + { + logger.Error(ex, "设置捕获模式时发生异常"); + return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试"); + } + } + + /// + /// 读取捕获状态 + /// + /// 捕获状态 + [HttpGet("GetCaptureStatus")] + [EnableCors("Users")] + [ProducesResponseType(typeof(CaptureStatus), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + public async Task GetCaptureStatus() + { + try + { + var analyzer = GetAnalyzer(); + if (analyzer == null) + return BadRequest("用户未绑定有效的实验板"); + + var result = await analyzer.ReadCaptureStatus(); + if (!result.IsSuccessful) + { + logger.Error($"读取捕获状态失败: {result.Error}"); + return StatusCode(StatusCodes.Status500InternalServerError, "读取捕获状态失败"); + } + + return Ok(result.Value); + } + catch (Exception ex) + { + logger.Error(ex, "读取捕获状态时发生异常"); + return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试"); + } + } + + /// + /// 设置全局触发模式 + /// + /// 全局触发模式 + /// 操作结果 + [HttpPost("SetGlobalTrigMode")] + [EnableCors("Users")] + [ProducesResponseType(typeof(bool), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + public async Task SetGlobalTrigMode(GlobalCaptureMode mode) + { + try + { + var analyzer = GetAnalyzer(); + if (analyzer == null) + return BadRequest("用户未绑定有效的实验板"); + + var result = await analyzer.SetGlobalTrigMode(mode); + if (!result.IsSuccessful) + { + logger.Error($"设置全局触发模式失败: {result.Error}"); + return StatusCode(StatusCodes.Status500InternalServerError, "设置全局触发模式失败"); + } + + return Ok(result.Value); + } + catch (Exception ex) + { + logger.Error(ex, "设置全局触发模式时发生异常"); + return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试"); + } + } + + /// + /// 设置信号触发模式 + /// + /// 信号索引 (0-7) + /// 操作符 + /// 信号值 + /// 操作结果 + [HttpPost("SetSignalTrigMode")] + [EnableCors("Users")] + [ProducesResponseType(typeof(bool), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + public async Task SetSignalTrigMode(int signalIndex, SignalOperator op, SignalValue val) + { + try + { + if (signalIndex < 0 || signalIndex > 7) + return BadRequest("信号索引必须在0-7之间"); + + var analyzer = GetAnalyzer(); + if (analyzer == null) + return BadRequest("用户未绑定有效的实验板"); + + var result = await analyzer.SetSignalTrigMode(signalIndex, op, val); + if (!result.IsSuccessful) + { + logger.Error($"设置信号触发模式失败: {result.Error}"); + return StatusCode(StatusCodes.Status500InternalServerError, "设置信号触发模式失败"); + } + + return Ok(result.Value); + } + catch (Exception ex) + { + logger.Error(ex, "设置信号触发模式时发生异常"); + return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试"); + } + } + + /// + /// 批量配置捕获参数 + /// + /// 捕获配置 + /// 操作结果 + [HttpPost("ConfigureCapture")] + [EnableCors("Users")] + [ProducesResponseType(typeof(bool), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + public async Task ConfigureCapture([FromBody] CaptureConfig config) + { + try + { + if (config == null) + return BadRequest("配置参数不能为空"); + + var analyzer = GetAnalyzer(); + if (analyzer == null) + return BadRequest("用户未绑定有效的实验板"); + + // 设置全局触发模式 + var globalResult = await analyzer.SetGlobalTrigMode(config.GlobalMode); + if (!globalResult.IsSuccessful) + { + logger.Error($"设置全局触发模式失败: {globalResult.Error}"); + return StatusCode(StatusCodes.Status500InternalServerError, "设置全局触发模式失败"); + } + + // 设置信号触发模式 + foreach (var signalConfig in config.SignalConfigs) + { + if (signalConfig.SignalIndex < 0 || signalConfig.SignalIndex > 7) + return BadRequest($"信号索引{signalConfig.SignalIndex}超出范围0-7"); + + var signalResult = await analyzer.SetSignalTrigMode( + signalConfig.SignalIndex, signalConfig.Operator, signalConfig.Value); + if (!signalResult.IsSuccessful) + { + logger.Error($"设置信号{signalConfig.SignalIndex}触发模式失败: {signalResult.Error}"); + return StatusCode(StatusCodes.Status500InternalServerError, + $"设置信号{signalConfig.SignalIndex}触发模式失败"); + } + } + + return Ok(true); + } + catch (Exception ex) + { + logger.Error(ex, "配置捕获参数时发生异常"); + return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试"); + } + } + + /// + /// 强制捕获 + /// + /// 操作结果 + [HttpPost("ForceCapture")] + [EnableCors("Users")] + [ProducesResponseType(typeof(bool), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + public async Task ForceCapture() + { + try + { + var analyzer = GetAnalyzer(); + if (analyzer == null) + return BadRequest("用户未绑定有效的实验板"); + + var result = await analyzer.SetCaptureMode(true, true); + if (!result.IsSuccessful) + { + logger.Error($"强制捕获失败: {result.Error}"); + return StatusCode(StatusCodes.Status500InternalServerError, "强制捕获失败"); + } + + return Ok(result.Value); + } + catch (Exception ex) + { + logger.Error(ex, "强制捕获时发生异常"); + return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试"); + } + } + + /// + /// 读取捕获数据 + /// + /// 捕获的波形数据(Base64编码) + [HttpGet("GetCaptureData")] + [EnableCors("Users")] + [ProducesResponseType(typeof(string), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + public async Task GetCaptureData() + { + try + { + var analyzer = GetAnalyzer(); + if (analyzer == null) + return BadRequest("用户未绑定有效的实验板"); + + var result = await analyzer.ReadCaptureData(); + if (!result.IsSuccessful) + { + logger.Error($"读取捕获数据失败: {result.Error}"); + return StatusCode(StatusCodes.Status500InternalServerError, "读取捕获数据失败"); + } + + // 将二进制数据编码为Base64字符串返回 + var base64Data = Convert.ToBase64String(result.Value); + return Ok(base64Data); + } + catch (Exception ex) + { + logger.Error(ex, "读取捕获数据时发生异常"); + return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试"); + } + } +} diff --git a/server/src/Peripherals/LogicAnalyzerClient.cs b/server/src/Peripherals/LogicAnalyzerClient.cs new file mode 100644 index 0000000..e4037a5 --- /dev/null +++ b/server/src/Peripherals/LogicAnalyzerClient.cs @@ -0,0 +1,352 @@ +using System.Collections; +using System.Net; +using DotNext; + +namespace Peripherals.LogicAnalyzerClient; + +static class AnalyzerAddr +{ + const UInt32 BASE = 0x9000_0000; + + /// + /// 0x0000_0000 R/W [ 0] capture on: 置1开始等待捕获,0停止捕获。捕获到信号后该位自动清零。
+ /// [ 8] capture force: 置1则强制捕获信号,自动置0。
+ /// [16] capture busy: 1为逻辑分析仪正在捕获信号。
+ /// [24] capture done: 1为逻辑分析仪内存完整存储了此次捕获的信号。
+ /// 配置顺序:若[0]为0,则将其置1,随后不断获取[0],若其变为0则表示触发成功。随后不断获取[24],若其为1则表示捕获完成。
+ ///
+ public const UInt32 CAPTURE_MODE = BASE + 0x0000_0000; + + /// + /// 0x0000_0001 R/W [1:0] global trig mode: 00: 全局与 (&)
+ /// 01: 全局或 (|)
+ /// 10: 全局非与(~&)
+ /// 11: 全局非或(~|)
+ ///
+ public const UInt32 GLOBAL_TRIG_MODE = BASE + 0x0000_0000; + + /// + /// 0x0000_0010 - 0x0000_0017 R/W [5:0] 信号M的触发操作符,共8路
+ /// [2:0] M's Operator: 000 ==
+ /// 001 !=
+ /// 010 <
+ /// 011 <=
+ /// 100 >
+ /// 101 >=
+ /// [5:3] M's Value: 000 LOGIC 0
+ /// 001 LOGIC 1
+ /// 010 X(not care)
+ /// 011 RISE
+ /// 100 FALL
+ /// 101 RISE OR FALL
+ /// 110 NOCHANGE
+ /// 111 SOME NUMBER
+ ///
+ public static readonly UInt32[] SIGNAL_TRIG_MODE = { + BASE + 0x0000_0010, + BASE + 0x0000_0011, + BASE + 0x0000_0012, + BASE + 0x0000_0013, + BASE + 0x0000_0014, + BASE + 0x0000_0015, + BASE + 0x0000_0016, + BASE + 0x0000_0017, + }; + + /// + /// 0x0100_0000 - 0x0100_03FF 只读 32位波形存储,得到的32位数据中低八位最先捕获,高八位最后捕获。
+ /// 共1024个地址,每个地址存储4组,深度为4096。
+ ///
+ public const UInt32 CAPTURE_DATA_ADDR = BASE + 0x0100_0000; + public const Int32 CAPTURE_DATA_LENGTH = 1024; +} + +/// +/// 逻辑分析仪运行状态枚举 +/// +[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 +} + +/// +/// 信号M的操作符枚举 +/// +public enum SignalOperator : byte +{ + /// + /// 等于操作符 + /// + Equal = 0b000, // == + /// + /// 不等于操作符 + /// + NotEqual = 0b001, // != + /// + /// 小于操作符 + /// + LessThan = 0b010, // < + /// + /// 小于等于操作符 + /// + LessThanOrEqual = 0b011, // <= + /// + /// 大于操作符 + /// + GreaterThan = 0b100, // > + /// + /// 大于等于操作符 + /// + GreaterThanOrEqual = 0b101 // >= +} + +/// +/// 信号M的值枚举 +/// +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 +} + +/// +/// FPGA逻辑分析仪客户端,用于控制FPGA上的逻辑分析仪模块进行信号捕获和分析 +/// +public class Analyzer +{ + + 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; + + /// + /// 初始化逻辑分析仪客户端 + /// + /// FPGA设备的IP地址 + /// 通信端口号 + /// 任务标识符 + /// 通信超时时间(毫秒),默认2000ms + /// 当timeout为负数时抛出 + public Analyzer(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; + } + + /// + /// 控制逻辑分析仪的捕获模式 + /// + /// 是否开始捕获 + /// 是否强制捕获 + /// 操作结果,成功返回true,否则返回异常信息 + public async ValueTask> SetCaptureMode(bool captureOn, bool force) + { + // 构造寄存器值 + UInt32 value = 0; + if (captureOn) value |= 1 << 0; + if (force) value |= 1 << 8; + + var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, AnalyzerAddr.CAPTURE_MODE, value, this.timeout); + if (!ret.IsSuccessful) + { + logger.Error($"Failed to set capture mode: {ret.Error}"); + return new(ret.Error); + } + if (!ret.Value) + { + logger.Error("WriteAddr to CAPTURE_MODE returned false"); + return new(new Exception("Failed to set capture mode")); + } + return true; + } + + /// + /// 读取逻辑分析仪捕获运行状态 + /// + /// 操作结果,成功返回寄存器值,否则返回异常信息 + public async ValueTask> ReadCaptureStatus() + { + var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, AnalyzerAddr.CAPTURE_MODE, this.timeout); + if (!ret.IsSuccessful) + { + logger.Error($"Failed to read capture status: {ret.Error}"); + return new(ret.Error); + } + if (ret.Value.Options.Data == null || ret.Value.Options.Data.Length < 4) + { + logger.Error("ReadAddr returned invalid data for capture status"); + return new(new Exception("Failed to read capture status")); + } + UInt32 status = BitConverter.ToUInt32(ret.Value.Options.Data, 0); + return (CaptureStatus)status; + } + + /// + /// 设置全局触发模式 + /// + /// 全局触发模式(0:与, 1:或, 2:非与, 3:非或) + /// 操作结果,成功返回true,否则返回异常信息 + public async ValueTask> SetGlobalTrigMode(GlobalCaptureMode mode) + { + var ret = await UDPClientPool.WriteAddr( + this.ep, this.taskID, AnalyzerAddr.GLOBAL_TRIG_MODE, (byte)mode, this.timeout); + if (!ret.IsSuccessful) + { + logger.Error($"Failed to set global trigger mode: {ret.Error}"); + return new(ret.Error); + } + if (!ret.Value) + { + logger.Error("WriteAddr to GLOBAL_TRIG_MODE returned false"); + return new(new Exception("Failed to set global trigger mode")); + } + return true; + } + + /// + /// 设置指定信号通道的触发模式 + /// + /// 信号通道索引(0-7) + /// 触发操作符 + /// 触发信号值 + /// 操作结果,成功返回true,否则返回异常信息 + public async ValueTask> SetSignalTrigMode(int signalIndex, SignalOperator op, SignalValue val) + { + if (signalIndex < 0 || signalIndex >= AnalyzerAddr.SIGNAL_TRIG_MODE.Length) + return new(new ArgumentException($"Signal index must be 0~{AnalyzerAddr.SIGNAL_TRIG_MODE.Length}")); + + // 计算模式值: [2:0] 操作符, [5:3] 信号值 + UInt32 mode = ((UInt32)val << 3) | (UInt32)op; + + var addr = AnalyzerAddr.SIGNAL_TRIG_MODE[signalIndex]; + var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, addr, mode, this.timeout); + if (!ret.IsSuccessful) + { + logger.Error($"Failed to set signal trigger mode: {ret.Error}"); + return new(ret.Error); + } + if (!ret.Value) + { + logger.Error("WriteAddr to SIGNAL_TRIG_MODE returned false"); + return new(new Exception("Failed to set signal trigger mode")); + } + return true; + } + + /// + /// 读取捕获的波形数据 + /// + /// 操作结果,成功返回byte[],否则返回异常信息 + public async ValueTask> ReadCaptureData() + { + var ret = await UDPClientPool.ReadAddr4BytesAsync( + this.ep, + this.taskID, + AnalyzerAddr.CAPTURE_DATA_ADDR, + AnalyzerAddr.CAPTURE_DATA_LENGTH, + this.timeout + ); + if (!ret.IsSuccessful) + { + logger.Error($"Failed to read capture data: {ret.Error}"); + return new(ret.Error); + } + var data = ret.Value; + if (data == null || data.Length != AnalyzerAddr.CAPTURE_DATA_LENGTH * 4) + { + logger.Error($"Capture data length mismatch: {data?.Length}"); + return new(new Exception("Capture data length mismatch")); + } + var reversed = Common.Number.ReverseBytes(data, 4).Value; + return reversed; + } +} diff --git a/src/views/Project/BottomBar.vue b/src/views/Project/BottomBar.vue index ec525cd..5c308ed 100644 --- a/src/views/Project/BottomBar.vue +++ b/src/views/Project/BottomBar.vue @@ -32,6 +32,16 @@ 示波器 +