FPGASession.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System.Collections.Concurrent;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. namespace NIFPGA
  5. {
  6. internal sealed class FPGASession
  7. {
  8. private readonly string windowsdllname = "NiFpga";
  9. private readonly string linuxdllname = "libNiFpga.so";
  10. private NativeLibraryLoader.NativeLoader _NativeLoader;
  11. public Boolean Error { get; private set; } = false;
  12. public String Message { get; private set; } = string.Empty;
  13. private uint session;
  14. public uint Session => session;
  15. internal FPGASession()
  16. {
  17. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) _NativeLoader = new NativeLibraryLoader.NativeLoader(windowsdllname);
  18. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  19. {
  20. _NativeLoader = new NativeLibraryLoader.NativeLoader(linuxdllname);
  21. _NativeLoader.SearchDirectories.Add("/usr/lib/x86_64-linux-gnu/");
  22. }
  23. else
  24. {
  25. throw new PlatformNotSupportedException();
  26. }
  27. _NativeLoader.Init();
  28. }
  29. public void Close()
  30. {
  31. CheckResult(GetDelegate<Interop.NiFpgaDll_Close>()(session, Interop.NiFpga_CloseAttribute.NiFpga_CloseAttribute_NoResetIfLastSession));
  32. session = 0;
  33. }
  34. public void Open(string bitfile,string signature,string resource,NiFpga_OpenAttribute attribute)
  35. {
  36. CheckResult(GetDelegate<Interop.NiFpgaDll_Open>()(bitfile, signature, resource, attribute, ref session));
  37. }
  38. public T GetDelegate<T>() where T : Delegate
  39. {
  40. return _NativeLoader.LoadFunction<T>(typeof(T).Name);
  41. }
  42. public void CheckResult(NiFpga_Status result)
  43. {
  44. Error = result < NiFpga_Status.Success;
  45. Message = result.ToString();
  46. }
  47. }
  48. }