Fifo.cs 5.7 KB

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