WriteFifo.cs 5.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.Tracing;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using FxpConvert.Common;
  9. using NIFPGA.lvbitx;
  10. namespace NIFPGA
  11. {
  12. public class WriteFifo<T>:Fifo
  13. where T : unmanaged
  14. {
  15. private object _Locker = new object();
  16. internal WriteFifo(FPGASession session, DMA fifosession) : base(session, fifosession)
  17. {
  18. }
  19. public void Write(T[] value,uint timeout,ref uint emptyElementsRemaining)
  20. {
  21. lock (_Locker)
  22. {
  23. if (value.Length == 0) return;
  24. switch (value[0])
  25. {
  26. case bool:
  27. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoBool>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  28. break;
  29. case byte:
  30. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoU8>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  31. break;
  32. case sbyte:
  33. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoI8>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  34. break;
  35. case short:
  36. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoI16>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  37. break;
  38. case ushort:
  39. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoU16>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  40. break;
  41. case int:
  42. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoI32>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  43. break;
  44. case uint:
  45. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoU32>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  46. break;
  47. case long:
  48. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoI64>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  49. break;
  50. case ulong:
  51. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoU64>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  52. break;
  53. case float:
  54. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoSgl>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  55. break;
  56. case double:
  57. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoDbl>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  58. break;
  59. }
  60. }
  61. }
  62. }
  63. public class WriteFXPFifo : WriteFifo<ulong>
  64. {
  65. private IFxpConvert _Convert;
  66. private NiFpga_FxpTypeInfo _TypeInfo;
  67. internal WriteFXPFifo(FPGASession session, DMA fifosession,IFxpConvert convert) : base(session, fifosession)
  68. {
  69. if(convert == null) throw new ArgumentNullException(nameof(convert));
  70. _Convert = convert;
  71. _TypeInfo = fifosession.FxpTypeInfo;
  72. }
  73. public void Write(ref float value, uint timeout, uint length, ref uint emptyElementsRemaining)
  74. {
  75. if (length == 0) return;
  76. ulong[] temp= new ulong[length];
  77. _Convert.FloatConvertToDxp(ref value, _TypeInfo, ref temp[0], (uint)length);
  78. base.Write(temp, timeout, ref emptyElementsRemaining);
  79. }
  80. public void Write(ref double value, uint timeout, uint length, ref uint emptyElementsRemaining)
  81. {
  82. if (length == 0) return;
  83. ulong[] temp = new ulong[length];
  84. _Convert.DoubleConvertToDxp(ref value, _TypeInfo, ref temp[0], (uint)length);
  85. base.Write(temp, timeout, ref emptyElementsRemaining);
  86. }
  87. }
  88. }