From c1ff893b5894257509f973d2d697385536c2f741 Mon Sep 17 00:00:00 2001 From: SikongJueluo Date: Fri, 25 Apr 2025 16:10:51 +0800 Subject: [PATCH] add class jtag status reg --- server/src/Common.cs | 9 +++ server/src/Controllers.cs | 33 ++------ server/src/JtagClient.cs | 155 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+), 28 deletions(-) diff --git a/server/src/Common.cs b/server/src/Common.cs index 3d4bb1f..60bccc7 100644 --- a/server/src/Common.cs +++ b/server/src/Common.cs @@ -152,6 +152,15 @@ namespace Common return (srcBits & mask) == dstBits; } + public static Result ToBit(UInt32 srcBits, int location) + { + if (location < 0) + return new(new ArgumentException( + "Location can't be negetive", nameof(location))); + + return (srcBits & (1 << location)) == 1; + } + /// /// 字符串转二进制字节数组 diff --git a/server/src/Controllers.cs b/server/src/Controllers.cs index 43a2451..8c1c770 100644 --- a/server/src/Controllers.cs +++ b/server/src/Controllers.cs @@ -1,6 +1,6 @@ +using System.Buffers.Binary; using System.Net; using Common; -using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using WebProtocol; @@ -212,37 +212,13 @@ public class JtagController : ControllerBase if (ret.IsSuccessful) { var binaryValue = Common.String.Reverse(Convert.ToString(ret.Value, 2).PadLeft(32, '0')); - logger.Info($"Read device {address} Status Register: 0b{binaryValue}"); + var decodeValue = new JtagClient.JtagStatusReg(ret.Value); + logger.Info($"Read device {address} Status Register: \n\t 0b{binaryValue} \n\t {decodeValue}"); return TypedResults.Ok(new { original = ret.Value, binary = binaryValue, - id_err = binaryValue[0], - crc_err = binaryValue[1], - aut_err = binaryValue[2], - rbcrc_err = binaryValue[3], - timeout = binaryValue[4], - wakeup_over = binaryValue[5], - wakedown_over = binaryValue[6], - m = Common.String.Reverse(binaryValue[7..10]), - init_complete = binaryValue[10], - init_n = binaryValue[11], - done = binaryValue[12], - done_i = binaryValue[13], - glogen = binaryValue[14], - glogen_fb = binaryValue[15], - gouten = binaryValue[16], - grsn = binaryValue[17], - gwen = binaryValue[18], - pll_lock = binaryValue[19], - fallback = binaryValue[21], - ipal_m = Common.String.Reverse(binaryValue[22..24]), - flg_x8 = binaryValue[24], - flg_x16 = binaryValue[25], - flg_x32 = binaryValue[26], - over_temp = binaryValue[27], - prcfg_err = binaryValue[28], - prcfg_over = binaryValue[29], + decode = decodeValue, }); } else @@ -324,6 +300,7 @@ public class JtagController : ControllerBase while ((bytesRead = await fileStream.ReadAsync(buffer, 0, buffer.Length)) > 0) { var revBuffer = Common.Number.ReverseBytes(buffer, 4); + if (!revBuffer.IsSuccessful) return TypedResults.InternalServerError(revBuffer.Error); diff --git a/server/src/JtagClient.cs b/server/src/JtagClient.cs index b270f55..676bdf8 100644 --- a/server/src/JtagClient.cs +++ b/server/src/JtagClient.cs @@ -1,5 +1,6 @@ using System.Net; using DotNext; +using Newtonsoft.Json; using WebProtocol; namespace JtagClient; @@ -213,6 +214,160 @@ public static class JtagCmd public const UInt32 CMD_JTAG_IDLE_DELAY = 0b0101; } +/// +/// JTAG 状态寄存器 +/// +public class JtagStatusReg +{ + /// + /// ID 错误标志 + /// + public bool id_err; + /// + /// CRC 错误标志 + /// + public bool crc_err; + /// + /// 自动测试错误标志 + /// + public bool aut_err; + /// + /// 回读 CRC 错误标志 + /// + public bool rbcrc_err; + /// + /// 超时标志 + /// + public bool timeout; + /// + /// 唤醒完成标志 + /// + public bool wakeup_over; + /// + /// 休眠完成标志 + /// + public bool wakedown_over; + /// + /// 模式位 + /// + public byte m; + /// + /// 初始化完成标志 + /// + public bool init_complete; + /// + /// 初始化状态(低电平有效) + /// + public bool init_n; + /// + /// 完成标志 + /// + public bool done; + /// + /// 内部完成标志 + /// + public bool done_i; + /// + /// 全局逻辑使能标志 + /// + public bool glogen; + /// + /// 全局逻辑反馈标志 + /// + public bool glogen_fb; + /// + /// 全局输出使能标志 + /// + public bool gouten; + /// + /// 全局复位标志 + /// + public bool grsn; + /// + /// 全局写使能标志 + /// + public bool gwen; + /// + /// PLL 锁定标志 + /// + public bool pll_lock; + /// + /// 回退标志 + /// + public bool fallback; + /// + /// IPAL 模式位 + /// + public byte ipal_m; + /// + /// 8 位标志 + /// + public bool flg_x8; + /// + /// 16 位标志 + /// + public bool flg_x16; + /// + /// 32 位标志 + /// + public bool flg_x32; + /// + /// 过温标志 + /// + public bool over_temp; + /// + /// 配置错误标志 + /// + public bool prcfg_err; + /// + /// 配置完成标志 + /// + public bool prcfg_over; + + /// + /// 构造函数,从 32 位代码解析 JTAG 状态寄存器 + /// + /// 32 位状态寄存器值 + public JtagStatusReg(UInt32 code) + { + this.id_err = Common.Number.ToBit(code, 0).Value; + this.crc_err = Common.Number.ToBit(code, 1).Value; + this.aut_err = Common.Number.ToBit(code, 2).Value; + this.rbcrc_err = Common.Number.ToBit(code, 3).Value; + this.timeout = Common.Number.ToBit(code, 4).Value; + this.wakeup_over = Common.Number.ToBit(code, 5).Value; + this.wakedown_over = Common.Number.ToBit(code, 6).Value; + this.m = ((byte)(code & (0b111 << 7))); + this.init_complete = Common.Number.ToBit(code, 10).Value; + this.init_n = Common.Number.ToBit(code, 11).Value; + this.done = Common.Number.ToBit(code, 12).Value; + this.done_i = Common.Number.ToBit(code, 13).Value; + this.glogen = Common.Number.ToBit(code, 14).Value; + this.glogen_fb = Common.Number.ToBit(code, 15).Value; + this.gouten = Common.Number.ToBit(code, 16).Value; + this.grsn = Common.Number.ToBit(code, 17).Value; + this.gwen = Common.Number.ToBit(code, 18).Value; + this.pll_lock = Common.Number.ToBit(code, 19).Value; + this.fallback = Common.Number.ToBit(code, 21).Value; + this.ipal_m = ((byte)(code & (0b11 << 22))); + this.flg_x8 = Common.Number.ToBit(code, 24).Value; + this.flg_x16 = Common.Number.ToBit(code, 25).Value; + this.flg_x32 = Common.Number.ToBit(code, 26).Value; + this.over_temp = Common.Number.ToBit(code, 27).Value; + this.prcfg_err = Common.Number.ToBit(code, 28).Value; + this.prcfg_over = Common.Number.ToBit(code, 29).Value; + } + + /// + /// 转换为Json字符串 + /// + /// Json字符串 + public override string ToString() + { + return JsonConvert.SerializeObject(this); + } +} + /// /// Jtag控制器 ///