From 42ddd0fcdbe4c4be6859418ccfedcbcc076a4277 Mon Sep 17 00:00:00 2001 From: SikongJueluo Date: Fri, 25 Apr 2025 17:14:34 +0800 Subject: [PATCH] fix download bitstream and add json convert for jtag status reg --- server.test/CommonTest.cs | 9 ++++++ server/Program.cs | 2 +- server/src/Common.cs | 2 +- server/src/Controllers.cs | 20 ++++++++----- server/src/JtagClient.cs | 62 +++++++++++++++++++++------------------ 5 files changed, 58 insertions(+), 37 deletions(-) diff --git a/server.test/CommonTest.cs b/server.test/CommonTest.cs index a6cb5e5..782a428 100644 --- a/server.test/CommonTest.cs +++ b/server.test/CommonTest.cs @@ -36,4 +36,13 @@ public class CommonTest }; Assert.Equal(Number.ReverseBytes(bytesArray, 4).Value, rev4Bytes); } + + [Fact] + public void ToBitTest() + { + Assert.Equal(Number.ToBit(0xFF, 3).Value, true); + Assert.Equal(Number.ToBit(0x00, 3).Value, false); + Assert.Equal(Number.ToBit(0xAA, 3).Value, true); + Assert.Equal(Number.ToBit(0xAA, 2).Value, false); + } } diff --git a/server/Program.cs b/server/Program.cs index cfba4f5..02fd163 100644 --- a/server/Program.cs +++ b/server/Program.cs @@ -16,7 +16,7 @@ try // Services Settings // Add services to the container. - builder.Services.AddControllersWithViews(); + // builder.Services.AddControllersWithViews(); // NLog: Setup NLog for Dependency injection builder.Logging.ClearProviders(); diff --git a/server/src/Common.cs b/server/src/Common.cs index 60bccc7..194b409 100644 --- a/server/src/Common.cs +++ b/server/src/Common.cs @@ -158,7 +158,7 @@ namespace Common return new(new ArgumentException( "Location can't be negetive", nameof(location))); - return (srcBits & (1 << location)) == 1; + return ((srcBits >> location) & ((UInt32)0b1)) == 1; } diff --git a/server/src/Controllers.cs b/server/src/Controllers.cs index 8c1c770..42405b2 100644 --- a/server/src/Controllers.cs +++ b/server/src/Controllers.cs @@ -217,8 +217,8 @@ public class JtagController : ControllerBase return TypedResults.Ok(new { original = ret.Value, - binary = binaryValue, - decode = decodeValue, + binaryValue, + decodeValue, }); } else @@ -291,6 +291,7 @@ public class JtagController : ControllerBase // 定义缓冲区大小: 32KB byte[] buffer = new byte[32 * 1024]; + byte[] revBuffer = new byte[32 * 1024]; long totalBytesRead = 0; // 使用异步流读取文件 @@ -299,12 +300,17 @@ public class JtagController : ControllerBase int bytesRead; while ((bytesRead = await fileStream.ReadAsync(buffer, 0, buffer.Length)) > 0) { - var revBuffer = Common.Number.ReverseBytes(buffer, 4); + // 反转 32bits + var retBuffer = Common.Number.ReverseBytes(buffer, 4); + if (!retBuffer.IsSuccessful) + return TypedResults.InternalServerError(retBuffer.Error); + revBuffer = retBuffer.Value; + for (int i = 0; i < buffer.Length; i++) + { + revBuffer[i] = BinaryPrimitives.ReverseEndianness(revBuffer[i]); + } - if (!revBuffer.IsSuccessful) - return TypedResults.InternalServerError(revBuffer.Error); - - await memoryStream.WriteAsync(revBuffer.Value, 0, bytesRead); + await memoryStream.WriteAsync(revBuffer, 0, bytesRead); totalBytesRead += bytesRead; } diff --git a/server/src/JtagClient.cs b/server/src/JtagClient.cs index 676bdf8..cca5b27 100644 --- a/server/src/JtagClient.cs +++ b/server/src/JtagClient.cs @@ -222,107 +222,107 @@ public class JtagStatusReg /// /// ID 错误标志 /// - public bool id_err; + public bool id_err { get; set; } /// /// CRC 错误标志 /// - public bool crc_err; + public bool crc_err { get; set; } /// /// 自动测试错误标志 /// - public bool aut_err; + public bool aut_err { get; set; } /// /// 回读 CRC 错误标志 /// - public bool rbcrc_err; + public bool rbcrc_err { get; set; } /// /// 超时标志 /// - public bool timeout; + public bool timeout { get; set; } /// /// 唤醒完成标志 /// - public bool wakeup_over; + public bool wakeup_over { get; set; } /// /// 休眠完成标志 /// - public bool wakedown_over; + public bool wakedown_over { get; set; } /// /// 模式位 /// - public byte m; + public string m { get; set; } /// /// 初始化完成标志 /// - public bool init_complete; + public bool init_complete { get; set; } /// /// 初始化状态(低电平有效) /// - public bool init_n; + public bool init_n { get; set; } /// /// 完成标志 /// - public bool done; + public bool done { get; set; } /// /// 内部完成标志 /// - public bool done_i; + public bool done_i { get; set; } /// /// 全局逻辑使能标志 /// - public bool glogen; + public bool glogen { get; set; } /// /// 全局逻辑反馈标志 /// - public bool glogen_fb; + public bool glogen_fb { get; set; } /// /// 全局输出使能标志 /// - public bool gouten; + public bool gouten { get; set; } /// /// 全局复位标志 /// - public bool grsn; + public bool grsn { get; set; } /// /// 全局写使能标志 /// - public bool gwen; + public bool gwen { get; set; } /// /// PLL 锁定标志 /// - public bool pll_lock; + public bool pll_lock { get; set; } /// /// 回退标志 /// - public bool fallback; + public bool fallback { get; set; } /// /// IPAL 模式位 /// - public byte ipal_m; + public string ipal_m { get; set; } /// /// 8 位标志 /// - public bool flg_x8; + public bool flg_x8 { get; set; } /// /// 16 位标志 /// - public bool flg_x16; + public bool flg_x16 { get; set; } /// /// 32 位标志 /// - public bool flg_x32; + public bool flg_x32 { get; set; } /// /// 过温标志 /// - public bool over_temp; + public bool over_temp { get; set; } /// /// 配置错误标志 /// - public bool prcfg_err; + public bool prcfg_err { get; set; } /// /// 配置完成标志 /// - public bool prcfg_over; + public bool prcfg_over { get; set; } /// /// 构造函数,从 32 位代码解析 JTAG 状态寄存器 @@ -337,7 +337,7 @@ public class JtagStatusReg 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.m = Convert.ToString(((code >> 7) & 0b111), 2); 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; @@ -349,7 +349,7 @@ public class JtagStatusReg 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.ipal_m = Convert.ToString(((code >> 22) & 0b11), 2); 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; @@ -887,6 +887,12 @@ public class Jtag if (!ret.IsSuccessful) return new(ret.Error); else if (!ret.Value) return new(new Exception("Jtag IDLE Delay Failed")); + var retCode = await ReadStatusReg(); + if (!retCode.IsSuccessful) return new(retCode.Error); + var jtagStatus = new JtagStatusReg(retCode.Value); + if (!(jtagStatus.done && jtagStatus.wakeup_over && jtagStatus.init_complete)) + return new(new Exception("Jtag download bitstream failed")); + ret = await CloseTest(); if (!ret.IsSuccessful) return new(ret.Error); else if (!ret.Value) return new(new Exception("Jtag Close Test Failed"));