ReadFifo.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Http.Headers;
  5. using System.Reflection;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using FxpConvert.Common;
  10. using NIFPGA.lvbitx;
  11. namespace NIFPGA
  12. {
  13. public class ReadFifo<T>:Fifo
  14. where T : unmanaged
  15. {
  16. private object _Lock = new object();
  17. internal ReadFifo(FPGASession session, DMA fifosession) : base(session, fifosession)
  18. {
  19. }
  20. public void Read(ref T value,uint count, uint timeout, ref uint elementsRemaining)
  21. {
  22. lock (_Lock)
  23. {
  24. switch (value)
  25. {
  26. case bool:
  27. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoBool>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  28. break;
  29. case byte:
  30. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoU8>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  31. break;
  32. case sbyte:
  33. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoI8>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  34. break;
  35. case ushort:
  36. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoU16>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  37. break;
  38. case short:
  39. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoI16>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  40. break;
  41. case int:
  42. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoI32>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  43. break;
  44. case uint:
  45. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoU32>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  46. break;
  47. case long:
  48. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoI64>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  49. break;
  50. case ulong:
  51. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoU64>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  52. break;
  53. case float:
  54. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoSgl>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  55. break;
  56. case double:
  57. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoDbl>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  58. break;
  59. }
  60. }
  61. }
  62. public void ReadAcquire(uint count, uint timeout,ref uint elementsAcquired, ref uint elementsRemaining,ref nint ptr)
  63. {
  64. lock (_Lock)
  65. {
  66. T value = default;
  67. switch (value)
  68. {
  69. case bool:
  70. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_AcquireFifoReadElementsBool>()(_Session.Session, FifoSession, ref ptr, count, timeout,ref elementsAcquired, ref elementsRemaining));
  71. break;
  72. case byte:
  73. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_AcquireFifoReadElementsU8>()(_Session.Session, FifoSession, ref ptr, count, timeout,ref elementsAcquired, ref elementsRemaining));
  74. break;
  75. case sbyte:
  76. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_AcquireFifoReadElementsI8>()(_Session.Session, FifoSession, ref ptr, count, timeout,ref elementsAcquired, ref elementsRemaining));
  77. break;
  78. case ushort:
  79. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_AcquireFifoReadElementsU16>()(_Session.Session, FifoSession, ref ptr, count, timeout,ref elementsAcquired, ref elementsRemaining));
  80. break;
  81. case short:
  82. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_AcquireFifoReadElementsI16>()(_Session.Session, FifoSession, ref ptr, count, timeout,ref elementsAcquired, ref elementsRemaining));
  83. break;
  84. case int:
  85. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_AcquireFifoReadElementsI32>()(_Session.Session, FifoSession, ref ptr, count, timeout,ref elementsAcquired, ref elementsRemaining));
  86. break;
  87. case uint:
  88. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_AcquireFifoReadElementsU32>()(_Session.Session, FifoSession, ref ptr, count, timeout,ref elementsAcquired, ref elementsRemaining));
  89. break;
  90. case long:
  91. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_AcquireFifoReadElementsI64>()(_Session.Session, FifoSession, ref ptr, count, timeout,ref elementsAcquired, ref elementsRemaining));
  92. break;
  93. case ulong:
  94. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_AcquireFifoReadElementsU64>()(_Session.Session, FifoSession, ref ptr, count, timeout,ref elementsAcquired, ref elementsRemaining));
  95. break;
  96. case float:
  97. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_AcquireFifoReadElementsSgl>()(_Session.Session, FifoSession, ref ptr, count, timeout,ref elementsAcquired, ref elementsRemaining));
  98. break;
  99. case double:
  100. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_AcquireFifoReadElementsDbl>()(_Session.Session, FifoSession, ref ptr, count, timeout,ref elementsAcquired, ref elementsRemaining));
  101. break;
  102. }
  103. }
  104. }
  105. public uint ElementsRemaining
  106. {
  107. get
  108. {
  109. T val = default;
  110. uint elementsRemaining = 0;
  111. Read(ref val, 0, 10, ref elementsRemaining);
  112. return elementsRemaining;
  113. }
  114. }
  115. }
  116. public class ReadFXPFifo : ReadFifo<ulong>
  117. {
  118. private NiFpga_FxpTypeInfo _TypeInfo;
  119. private IFxpConvert _Convert;
  120. internal ReadFXPFifo(FPGASession session, DMA fifosession, IFxpConvert convert) : base(session, fifosession)
  121. {
  122. if(convert ==null)throw new ArgumentNullException(nameof(convert));
  123. _Convert = convert;
  124. _TypeInfo = fifosession.FxpTypeInfo;
  125. }
  126. public void Read(ref float value,uint count, uint timeout, ref uint elementsRemaining)
  127. {
  128. if (count == 0)
  129. {
  130. ulong[] temp = new ulong[1];
  131. base.Read(ref temp[0], count, timeout, ref elementsRemaining);
  132. }
  133. else
  134. {
  135. ulong[] temp = new ulong[count];
  136. base.Read(ref temp[0], count, timeout, ref elementsRemaining);
  137. _Convert.FxpConvertToFloat(ref temp[0], _TypeInfo, ref value, count);
  138. }
  139. }
  140. public unsafe void ReadAcquire(ref float value, uint count, uint timeout, ref uint elementsRemaining)
  141. {
  142. if (count == 0)
  143. {
  144. ulong[] temp = new ulong[1];
  145. base.Read(ref temp[0], count, timeout, ref elementsRemaining);
  146. }
  147. else
  148. {
  149. nint ptr = 0;
  150. uint c = 0;
  151. base.ReadAcquire(count, timeout,ref c, ref elementsRemaining, ref ptr);
  152. if (ptr != 0 && c!=0)
  153. {
  154. _Convert.FxpConvertToFloat(ref Unsafe.AsRef<ulong>((void*)ptr), _TypeInfo, ref value, count);
  155. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReleaseFifoElements>()(_Session.Session, FifoSession, c));
  156. }
  157. }
  158. }
  159. }
  160. }