ReadFifo.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 uint ElementsRemaining
  63. {
  64. get
  65. {
  66. T val = default;
  67. uint elementsRemaining = 0;
  68. Read(ref val, 0, 10, ref elementsRemaining);
  69. return elementsRemaining;
  70. }
  71. }
  72. }
  73. public class ReadFXPFifo : ReadFifo<ulong>
  74. {
  75. private NiFpga_FxpTypeInfo _TypeInfo;
  76. private IFxpConvert _Convert;
  77. internal ReadFXPFifo(FPGASession session, DMA fifosession, IFxpConvert convert) : base(session, fifosession)
  78. {
  79. if(convert ==null)throw new ArgumentNullException(nameof(convert));
  80. _Convert = convert;
  81. _TypeInfo = fifosession.FxpTypeInfo;
  82. }
  83. public void Read(ref float value,uint count, uint timeout, ref uint elementsRemaining)
  84. {
  85. if (count == 0)
  86. {
  87. ulong[] temp = new ulong[1];
  88. base.Read(ref temp[0], count, timeout, ref elementsRemaining);
  89. }
  90. else
  91. {
  92. ulong[] temp = new ulong[count];
  93. base.Read(ref temp[0], count, timeout, ref elementsRemaining);
  94. _Convert.FxpConvertToFloat(ref temp[0], _TypeInfo, ref value, count);
  95. }
  96. }
  97. }
  98. }