Acquisition.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Uestc.Auto6.Dso.ComModel;
  8. namespace Uestc.Auto6.Dso.Hardware.Driver
  9. {
  10. internal record PerChannelAcqParams
  11. {
  12. public UInt64 AcqBdExtractNum;
  13. public UInt32 AcqBdInterpolationNum;
  14. public UInt32 ProcBdInterpolationNum;
  15. public ulong DdrPointPerDataByfs;
  16. public ulong DmaPointPerDataByfs;
  17. }
  18. internal class AcquireAttribute
  19. {
  20. #region 通道带宽模式切换
  21. /// <summary>
  22. /// bit8 :Full BandWidth=0,Other BandWidth=1;bit7~bit0:channel 1~channel8 的激活状态
  23. /// </summary>
  24. public Int32 CurrChBWModeAndActiveState = 0;
  25. /// <summary>
  26. /// bit8 :Full BandWidth=0,Other BandWidth=1;bit7~bit0:channel 1~channel8 的激活状态
  27. /// </summary>
  28. public Int32 OldChBWModeAndActiveState = -1;
  29. #endregion
  30. //public List<PerChannelAcqParams> ChannelAcqParams = new List<PerChannelAcqParams>();
  31. /// <summary>
  32. /// 存储模式:长存储,普通存储
  33. /// </summary>
  34. public AnaChnlStorageMode AcqStorageMode { get; set; }
  35. public bool bIsLongStorageMode
  36. {
  37. get => AcqStorageMode == AnaChnlStorageMode.Long;
  38. }
  39. public UInt64 StorageLengthOfDots { get; set; }
  40. /// <summary>
  41. /// 只对长存储有用
  42. /// </summary>
  43. public UInt32 StorageStartAddress { get; set; }
  44. /// <summary>
  45. /// 最高采样率(合并后的)下,单一抽取器来计算的。
  46. /// </summary>
  47. public UInt64 HardwareExtractNum { get; set; }
  48. public UInt64 Dpx_HardwareExtractNum { get; set; }
  49. public UInt64 Dpx_PerDataByfs_AtStorage { get; set; }
  50. /// <summary>
  51. /// 在硬件端存储数据的采样率
  52. /// </summary>
  53. public double PerDataByfs_AtStorage { get; set; }
  54. /// <summary>
  55. /// 读取回来的数据的采样率。对长存储来说,是读回来的数据。因为在读取的过程中好,可能存在二次抽取。
  56. /// </summary>
  57. public double PerDataByfs_Readback { get; set; }
  58. /// <summary>
  59. /// 送给对外接口时的采样率。可能存在软件端的插值处理。如果没有软件端的插值,就是PerDataByfs
  60. /// </summary>
  61. public double PerDataByfs_AfterPostProcess { get; set; }
  62. public UInt32 InterpolationNum { get; set; }
  63. /// <summary>
  64. /// 设置的触发时间
  65. /// </summary>
  66. public Int64 SettingTrigPositionByfs { get; set; }
  67. public UInt32 SoftwareFifoLength { get; set; }
  68. public UInt32 AnaWaveDMALength { get; set; }
  69. /// <summary>
  70. /// 多少个10GADC拼合的,10G=1,20G=2;
  71. /// </summary>
  72. public UInt32 AcqBoardAdcMegerCount { get; set; } = 2;
  73. /// <summary>
  74. /// 处理板拼合采集几个板的数据形成一个通道的数据。目前只有HB8G项目在40G模式下是2,在20G模式下是1.
  75. /// 其与PerDataByfs_AtStorage、HardwareExtractNum的计算有关。HB8G项目在40G模式下,PerDataByfs_AtStorage与HardwareExtractNum存在2倍关系
  76. /// </summary>
  77. public UInt32 ProcBoardMergeAcqBoardRoadCount
  78. {
  79. get;
  80. set;
  81. } = 1;
  82. public UInt32 AcqAdcMergeRoadCount
  83. {
  84. get;
  85. set;
  86. } = 80;
  87. public UInt32 LS_SegmentCount { get; set; }
  88. public UInt32 LS_SegmentDots { get; set; }
  89. public UInt32 LS_SegmentAddrCount { get; set; }
  90. public UInt32 LS_ChannelMode { get; set; }
  91. public UInt32 LS_Addr_TrigTable { get; set; }
  92. public UInt32 LS_Addr_FirstSegment { get; set; }
  93. public UInt32 LS_Addr_PreTrig { get; set; }
  94. public UInt32 LS_CurrSegmentIndex { get; set; }
  95. public UInt32 LS_AcqTimeStamp { get; set; }
  96. public void CloneTo(AcquireAttribute dest)
  97. {
  98. dest.AcqStorageMode = this.AcqStorageMode;
  99. dest.AnaWaveDMALength = this.AnaWaveDMALength;
  100. dest.HardwareExtractNum = this.HardwareExtractNum;
  101. dest.InterpolationNum = this.InterpolationNum;
  102. dest.PerDataByfs_AfterPostProcess = this.PerDataByfs_AfterPostProcess;
  103. dest.PerDataByfs_AtStorage = this.PerDataByfs_AtStorage;
  104. dest.PerDataByfs_Readback = this.PerDataByfs_Readback;
  105. dest.SettingTrigPositionByfs = this.SettingTrigPositionByfs;
  106. dest.SoftwareFifoLength = this.SoftwareFifoLength;
  107. dest.StorageLengthOfDots = this.StorageLengthOfDots;
  108. dest.StorageStartAddress = this.StorageStartAddress;
  109. dest.CurrChBWModeAndActiveState = this.CurrChBWModeAndActiveState;
  110. dest.OldChBWModeAndActiveState = this.OldChBWModeAndActiveState;
  111. dest.LS_SegmentCount = this.LS_SegmentCount;
  112. dest.LS_SegmentDots = this.LS_SegmentDots;
  113. dest.LS_ChannelMode = this.LS_ChannelMode;
  114. dest.LS_Addr_FirstSegment = this.LS_Addr_FirstSegment;
  115. dest.LS_Addr_PreTrig = this.LS_Addr_PreTrig;
  116. dest.LS_SegmentAddrCount = this.LS_SegmentAddrCount;
  117. dest.LS_CurrSegmentIndex = this.LS_CurrSegmentIndex;
  118. dest.LS_AcqTimeStamp = this.LS_AcqTimeStamp;
  119. dest.LS_Addr_TrigTable = this.LS_Addr_TrigTable;
  120. dest.Dpx_HardwareExtractNum = this.Dpx_HardwareExtractNum;
  121. dest.Dpx_PerDataByfs_AtStorage = this.Dpx_PerDataByfs_AtStorage;
  122. //dest.ChannelAcqParams.Clear();
  123. //foreach(var v in ChannelAcqParams)
  124. // dest.ChannelAcqParams.Add(v);
  125. }
  126. }
  127. internal enum ChannelBandWidthMode
  128. {
  129. /// <summary>
  130. /// 全带宽模式
  131. /// </summary>
  132. Full,
  133. /// <summary>
  134. /// 对一般的情况,是降带宽模式,对多域项目来讲,是4G带宽
  135. /// </summary>
  136. Mode2,
  137. /// <summary>
  138. /// 目前仅仅对多域有效,指射频通道
  139. /// </summary>
  140. Mode3
  141. }
  142. internal class Acquisition
  143. {
  144. internal static void CreateAcquirer(AbstractAcquirer?[] usedAcquirerList)
  145. {
  146. allAcquirer.AddRange(usedAcquirerList);
  147. Stopwatch_AmplitudeTemperatureCompensate.Start();
  148. }
  149. private static List<AbstractAcquirer?> allAcquirer = new List<AbstractAcquirer?>();
  150. internal static void Init()
  151. {
  152. foreach (AbstractAcquirer? acquirer in allAcquirer)
  153. acquirer?.Init();
  154. }
  155. private static void StartingGunPang()
  156. {
  157. #region 通路选择
  158. HdIO.WriteReg(PcieBdReg.W.RST_CTRL_PcieReset, 0x1);
  159. HdIO.WriteReg(PcieBdReg.W.RST_CTRL_PcieReset, 0x0);
  160. //HdIO.WriteReg(ProcBdReg.W.debug_pro_debug_mode, 0x1);
  161. //HdIO.WriteReg(ProcBdReg.W.LA_SoftReset, 1);
  162. // HdIO.WriteReg(ProcBdReg.W.LA_SoftReset, 0);
  163. Hd.currProduct?.AcqBd?.WriteToAllFpga(AcqBdReg.W.Dpo_ParallelReset, 1);
  164. Hd.currProduct?.AcqBd?.WriteToAllFpga(AcqBdReg.W.Dpo_ParallelReset, 0);
  165. HdIO.WriteReg(PcieBdReg.W.RST_CTRL_SysResetFromPcie, 0x0);
  166. #endregion
  167. HdIO.WriteReg(ProcBdReg.W.FifoCtrl_AcqWriteEnable, 1);
  168. if (Hd.CurrHdMessage!.Timebase!.IsScan)
  169. {
  170. int channelCount = ChannelIdExt.AnaChnlNum;
  171. if (Hd.currProduct!.ProductType == ProductType.B21_DBI16G)
  172. channelCount = 4;
  173. while (AcqedDataPool.AnalogChData.AllChannelData.Count < channelCount)
  174. AcqedDataPool.AnalogChData.AllChannelData.Add(new List<ushort>());
  175. for (int iChannelID = 0; iChannelID < channelCount; iChannelID++)
  176. AcqedDataPool.AnalogChData.AllChannelData[iChannelID].Clear();
  177. }
  178. //HdIO.WriteReg(PcieBdReg.W.DataPath_pcie_linkdemux_select, (UInt32)DMAReadSourceMuxType.AnalogChanneData);
  179. // DDR要求必须在触发使能之后才能下发DDRde写使能,且触发源采集板必须最后发写使能,这样才能保证600W的触发频率
  180. ////FPGA 的严重Bug 导致 此 垃圾代码,很危险!!!!!!!!!!!
  181. UInt32 trigSource = Hd.currProduct!.Ctrl_Trigger!.CurrentTrigSource();
  182. if (trigSource >= (ChannelIdExt.AnaChnlNum))
  183. trigSource = (UInt32)ChannelIdExt.AnaChnlNum-1;
  184. ChannelBdAdcInputDefine? v = Hd.AnalogChannel?.GetChannelAcqBdAdcInputCorresponding((Int32)trigSource);
  185. if (v != null)
  186. {
  187. Hd.currProduct?.AcqBd?.WriteToAllFpga(AcqBdReg.W.LSCtrl_WriteEnable, v.BdNo, 0);//写使能
  188. Hd.currProduct?.AcqBd?.WriteToAllFpga(AcqBdReg.W.LSCtrl_WriteEnable, v.BdNo, 1);
  189. }
  190. }
  191. internal const Int32 PhyAnalogChAmplitudeTemperaturesCompensationIntervalByMs = 2 * 60 * 1000;
  192. static Stopwatch Stopwatch_AmplitudeTemperatureCompensate = new Stopwatch();
  193. private static void DoAmplitudeTemperatureCompensate()
  194. {
  195. if (Hd.CurrDebugVarints.bEnable_AmplitudeTemperatureCompensate)
  196. {
  197. if (Stopwatch_AmplitudeTemperatureCompensate.ElapsedMilliseconds > (PhyAnalogChAmplitudeTemperaturesCompensationIntervalByMs))
  198. {
  199. SysMonitor.Default.ReadAnalogChannelTemperatures();
  200. if (Hd.currProduct.Acquirer_AnalogChannel!.GetPhyAnalogChAmplitudeTemperaturesCompensationCoefficient(out _))
  201. {
  202. Stopwatch_AmplitudeTemperatureCompensate.Restart();
  203. AbstractController_AnalogChannel.CtrlGainByFpga();
  204. }
  205. }
  206. }
  207. }
  208. internal static void InitAcq(bool bForce)
  209. {
  210. if (bForce)
  211. {
  212. DoAmplitudeTemperatureCompensate();
  213. HdIO.WriteReg(ProcBdReg.W.FifoCtrl_AcqWriteEnable, 0);
  214. HdIO.WriteReg(PcieBdReg.W.RST_CTRL_SysResetFromPcie, 0x1);//处于复位中(态),为0时开始工作
  215. Hd.currProduct?.AcqBd?.WriteToAllFpga(AcqBdReg.W.Scan_AcqScanEnable, Hd.CurrHdMessage!.Timebase!.IsScan ? 1U : 0);//采集板SCAN模式开启1,关闭0
  216. HdIO.WriteReg(ProcBdReg.W.Scan_ProScanEnable, Hd.CurrHdMessage!.Timebase!.IsScan ? 1U : 0); //处理板SCAN模式开启1,关闭0
  217. foreach (AbstractAcquirer? acquirer in allAcquirer)
  218. acquirer?.InitAcq();
  219. //发令枪
  220. StartingGunPang();
  221. }
  222. }
  223. internal static void CreateAcquireAttribute()
  224. {
  225. foreach (AbstractAcquirer? acquirer in allAcquirer)
  226. acquirer?.CreateAcquireAttribute();
  227. }
  228. internal static DateTime lastSimulateScanTime = DateTime.Now;
  229. internal static Boolean Acquire()
  230. {
  231. Hd.trigState = AbstractController_Misc.ReadTrigStatus();
  232. bool bAcqOk = false;
  233. if (HdIO.CurrDriver == null || !HdIO.CurrDriver.bOpen || Hd.bPowerOff)
  234. {
  235. if ((Hd.CurrHdMessage?.Timebase?.IsScan ?? false))
  236. {
  237. //模拟Scan
  238. double xDiv = Hd.CurrHdMessage!.Timebase.TmbScale / 1000;
  239. if ((DateTime.Now - lastSimulateScanTime).TotalMilliseconds < xDiv)
  240. {
  241. Hd.bAcqedNewData = false;
  242. bAcqOk = false;
  243. }
  244. else
  245. {
  246. lastSimulateScanTime = DateTime.Now;
  247. Hd.bAcqedNewData = true;
  248. bAcqOk = true;
  249. }
  250. }
  251. else
  252. bAcqOk = true;
  253. }
  254. else
  255. bAcqOk = AbstractController_Misc.AcqIsFulled();
  256. if (bAcqOk)
  257. {
  258. foreach (AbstractAcquirer? acquirer in allAcquirer)
  259. {
  260. if (acquirer?.ReadAcqData() ?? false)
  261. acquirer?.PostProcess();
  262. }
  263. InitAcq(!Hd.CurrHdMessage?.Timebase?.IsScan ?? true);
  264. }
  265. return bAcqOk;
  266. }
  267. internal record FifoDepthDefine()
  268. {
  269. public UInt32 Proc_FullProgDepth = 12288;
  270. public UInt32 Pcie_FullProgDepth = 12288;
  271. public UInt32 Pcie_ReadFromFIFO_Count = 15000;
  272. }
  273. private static Dictionary<DMAReadSourceMuxType, FifoDepthDefine> allDataTypeDepthDefine = new Dictionary<DMAReadSourceMuxType, FifoDepthDefine>()
  274. {
  275. [DMAReadSourceMuxType.AnalogChanneData] = new() { Pcie_FullProgDepth = 12288, Pcie_ReadFromFIFO_Count = 15000, Proc_FullProgDepth = 12288 },
  276. [DMAReadSourceMuxType.Decoder] = new() { Pcie_FullProgDepth = 12288, Pcie_ReadFromFIFO_Count = 15000, Proc_FullProgDepth = 12288 },
  277. [DMAReadSourceMuxType.Dpx] = new() { Pcie_FullProgDepth = 12288, Pcie_ReadFromFIFO_Count = 15000, Proc_FullProgDepth = 12288 },
  278. [DMAReadSourceMuxType.LA] = new() { Pcie_FullProgDepth = 12288, Pcie_ReadFromFIFO_Count = 15000, Proc_FullProgDepth = 12288 },
  279. [DMAReadSourceMuxType.FreqDomain] = new() { Pcie_FullProgDepth = 12288, Pcie_ReadFromFIFO_Count = 15000, Proc_FullProgDepth = 12288 },
  280. [DMAReadSourceMuxType.DDRFast] = new() { Pcie_FullProgDepth = 12288, Pcie_ReadFromFIFO_Count = 15000, Proc_FullProgDepth = 12288 },
  281. };
  282. internal static void SwitchDataPathMuxTo(DMAReadSourceMuxType toType, FifoDepthDefine? fifoDepthDefine = null)
  283. {
  284. FifoDepthDefine ourFifoDepthDefine = fifoDepthDefine == null ? allDataTypeDepthDefine[toType] : fifoDepthDefine;
  285. HdIO.WriteReg(ProcBdReg.W.FifoCtrl_FullProgDepth, ourFifoDepthDefine.Proc_FullProgDepth);
  286. HdIO.WriteReg(PcieBdReg.W.FifoCtrl_FullProgDepth, ourFifoDepthDefine.Pcie_FullProgDepth);
  287. HdIO.WriteReg(PcieBdReg.W.FifoCtrl_ReadFromFIFO_Num, ourFifoDepthDefine.Pcie_ReadFromFIFO_Count);
  288. Hd.currProduct?.AcqBd?.WriteToAllFpga(AcqBdReg.W.DataPath_acq_linkmux_select, (UInt32)toType);
  289. HdIO.WriteReg(ProcBdReg.W.DataPath_pro_linkmux_select, (UInt32)toType);
  290. HdIO.WriteReg(ProcBdReg.W.DataPath_pro_linkdemux_select, (UInt32)toType);
  291. HdIO.WriteReg(PcieBdReg.W.DataPath_pcie_linkdemux_select, (UInt32)toType);
  292. }
  293. }
  294. }