Fifo.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using NativeLibraryLoader;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Net.Http.Headers;
  7. using System.Reflection;
  8. using System.Runtime.CompilerServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace NIFPGA
  12. {
  13. public abstract class FPGABase
  14. {
  15. protected private FPGASession _Session;
  16. internal FPGABase(FPGASession session)
  17. {
  18. _Session = session;
  19. }
  20. }
  21. public abstract class Fifo : FPGABase
  22. {
  23. private NiFpga_HostBufferType _HostBufferType;
  24. private uint _BytesPerElement;
  25. internal Fifo(FPGASession session,uint fifosession) : base(session)
  26. {
  27. FifoSession= fifosession;
  28. if (_Session.Session == 0) return;
  29. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_GetFifoPropertyI32>()(_Session.Session, fifosession, Interop.NiFpga_FifoProperty.NiFpga_FifoProperty_HostBufferType, ref Unsafe.As<NiFpga_HostBufferType,int>(ref _HostBufferType)));
  30. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_GetFifoPropertyU32>()(_Session.Session, fifosession, Interop.NiFpga_FifoProperty.NiFpga_FifoProperty_BytesPerElement, ref _BytesPerElement));
  31. }
  32. public void GetDmaBuffer(ref nint ptr)
  33. {
  34. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_GetFifoPropertyPtr>()(_Session.Session, FifoSession, Interop.NiFpga_FifoProperty.NiFpga_FifoProperty_HostBuffer, ref ptr));
  35. }
  36. public ulong ElementsCurrentlyAcquired =>GetElementsCurrentlyAcquired();
  37. private ulong GetElementsCurrentlyAcquired()
  38. {
  39. ulong value = 0;
  40. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_GetFifoPropertyU64>()(_Session.Session, FifoSession, Interop.NiFpga_FifoProperty.NiFpga_FifoProperty_ElementsCurrentlyAcquired, ref value));
  41. return value;
  42. }
  43. public string Name { get; init; } = string.Empty;
  44. private uint GetHostBufferAllocationGranularity()
  45. {
  46. uint value = 0;
  47. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_GetFifoPropertyU32>()(_Session.Session, FifoSession, Interop.NiFpga_FifoProperty.NiFpga_FifoProperty_HostBufferAllocationGranularity, ref value));
  48. return value;
  49. }
  50. private void SetHostBufferAllocationGranularity(uint value)
  51. {
  52. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_SetFifoPropertyU32>()(_Session.Session, FifoSession, Interop.NiFpga_FifoProperty.NiFpga_FifoProperty_HostBufferAllocationGranularity, value));
  53. }
  54. public uint HostBufferAllocationGranularity
  55. {
  56. get=>GetHostBufferAllocationGranularity();set=>SetHostBufferAllocationGranularity(value);
  57. }
  58. private ulong GetHostBufferSize()
  59. {
  60. ulong value = 0;
  61. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_GetFifoPropertyU64>()(_Session.Session, FifoSession, Interop.NiFpga_FifoProperty.NiFpga_FifoProperty_HostBufferSize, ref value));
  62. return value;
  63. }
  64. private void SetHostBufferSize(ulong value)
  65. {
  66. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_SetFifoPropertyU64>()(_Session.Session, FifoSession, Interop.NiFpga_FifoProperty.NiFpga_FifoProperty_HostBufferSize, value));
  67. }
  68. private ulong GetHostBufferMirrorSize()
  69. {
  70. ulong value = 0;
  71. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_GetFifoPropertyU64>()(_Session.Session, FifoSession, Interop.NiFpga_FifoProperty.NiFpga_FifoProperty_HostBufferMirrorSize, ref value));
  72. return value;
  73. }
  74. private void SetHostBufferMirrorSize(ulong value)
  75. {
  76. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_SetFifoPropertyU64>()(_Session.Session, FifoSession, Interop.NiFpga_FifoProperty.NiFpga_FifoProperty_HostBufferMirrorSize, value));
  77. }
  78. public ulong HostBufferSize { get => GetHostBufferSize(); set => SetHostBufferSize(value); }
  79. public ulong HostBufferMirrorSize { get=>GetHostBufferMirrorSize(); set=>SetHostBufferMirrorSize(value); }
  80. public UInt32 FifoSession { get; }
  81. public NiFpga_FifoFlowControl FifoFlowControl { get=>GetFlowControl(); set=>SetFlowControl(value); }
  82. public NiFpga_HostBufferType HostBufferType => _HostBufferType;
  83. public uint BytesPerElement => _BytesPerElement;
  84. private NiFpga_FifoFlowControl GetFlowControl()
  85. {
  86. int value = 0;
  87. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_GetFifoPropertyI32>()(_Session.Session, FifoSession, Interop.NiFpga_FifoProperty.NiFpga_FifoProperty_FlowControl, ref value));
  88. return (NiFpga_FifoFlowControl)value;
  89. }
  90. private void SetFlowControl(NiFpga_FifoFlowControl flowControl)
  91. {
  92. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_SetFifoPropertyI32>()(_Session.Session, FifoSession, Interop.NiFpga_FifoProperty.NiFpga_FifoProperty_FlowControl, (int)flowControl));
  93. }
  94. public void Start()
  95. {
  96. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_StartFifo>()(_Session.Session, FifoSession));
  97. }
  98. public void Stop()
  99. {
  100. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_StopFifo>()(_Session.Session, FifoSession));
  101. }
  102. public void Unreserve()
  103. {
  104. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_UnreserveFifo>()(_Session.Session, FifoSession));
  105. }
  106. public void CommitConfiguration()
  107. {
  108. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_CommitFifoConfiguration>()(_Session.Session, FifoSession));
  109. }
  110. public override string ToString()
  111. {
  112. return Name;
  113. }
  114. }
  115. }