Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. #![allow(non_camel_case_types)]
  2. #![allow(non_snake_case)]
  3. #![allow(non_upper_case_globals)]
  4. use std::ffi::CString;
  5. mod NiFpga {
  6. use std::fmt;
  7. use std::os::raw::c_char;
  8. pub type Session = u32;
  9. #[derive(PartialEq, Eq)]
  10. #[repr(C)]
  11. pub struct Status(i32);
  12. impl Status {
  13. pub const Success: Status = Status(0);
  14. }
  15. impl fmt::Display for Status {
  16. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  17. write!(
  18. f,
  19. "{} ({})",
  20. match *self {
  21. Status::Success => "Success",
  22. _ => "Unknown",
  23. },
  24. self.0,
  25. )
  26. }
  27. }
  28. #[link(name = "NiFpga")]
  29. #[link(name = "NiRioSrv")]
  30. #[link(name = "niriodevenum")]
  31. #[link(name = "niriosession")]
  32. extern "C" {
  33. #[link_name = "NiFpgaDll_Open"]
  34. pub fn Open(
  35. bitfile: *const c_char,
  36. signature: *const c_char,
  37. resource: *const c_char,
  38. attribute: u32,
  39. session: *mut Session,
  40. ) -> Status;
  41. #[link_name = "NiFpgaDll_Close"]
  42. pub fn Close(session: Session, attribute: u32) -> Status;
  43. #[link_name = "NiFpgaDll_ReadU32"]
  44. pub fn ReadU16(session: Session, indicator: u32, value: *mut u16) -> Status;
  45. }
  46. }
  47. pub trait Register {
  48. type Datatype;
  49. const OFFSET: isize;
  50. const SIZE_IN_BITS: usize;
  51. fn read() -> Self::Datatype;
  52. fn write(data: Self::Datatype);
  53. }
  54. #[no_mangle]
  55. static mut BITFILE_HANDLE: Option<NiFpga::Session> = None;
  56. struct Bitfile {
  57. private_int: i32,
  58. }
  59. impl Bitfile {
  60. #[inline]
  61. pub fn take() -> Option<Self> {
  62. if unsafe { BITFILE_HANDLE.is_some() } {
  63. None
  64. } else {
  65. Some(unsafe { Bitfile::steal() })
  66. }
  67. }
  68. pub unsafe fn steal() -> Self {
  69. assert!(BITFILE_HANDLE.is_none());
  70. let mut handle: NiFpga::Session = 0;
  71. let status: NiFpga::Status = NiFpga::Open(
  72. CString::new("/boot/user.lvbitx").unwrap().as_ptr(),
  73. CString::new("C571384F0C3E586B64ADFE11551DAAD0")
  74. .unwrap()
  75. .as_ptr(),
  76. CString::new("RIO0").unwrap().as_ptr(),
  77. 0,
  78. &mut handle as *mut NiFpga::Session,
  79. );
  80. assert!(status == NiFpga::Status::Success);
  81. BITFILE_HANDLE = Some(handle);
  82. Bitfile { private_int: 0 }
  83. }
  84. }
  85. impl Drop for Bitfile {
  86. fn drop(&mut self) {
  87. assert!(
  88. unsafe {
  89. NiFpga::Close(
  90. BITFILE_HANDLE.expect("Bitfile was dropped but BITFILE_HANDLE is None!"),
  91. 0,
  92. )
  93. } == NiFpga::Status::Success
  94. );
  95. unsafe {
  96. BITFILE_HANDLE = None;
  97. }
  98. }
  99. }
  100. fn main() {
  101. Bitfile { private_int: 1234 };
  102. let bitfile = Bitfile::take();
  103. debug_assert!(bitfile.is_some());
  104. debug_assert!(Bitfile::take().is_none());
  105. let mut version: u16 = 1234;
  106. let status: NiFpga::Status = unsafe {
  107. NiFpga::ReadU16(
  108. BITFILE_HANDLE.expect("Bitfile in use but BITFILE_HANDLE is None!"),
  109. 98314,
  110. &mut version as *mut u16,
  111. )
  112. };
  113. println!("Status: {}", status);
  114. println!("Version: {}", version);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement