Scope
FPGAIO5771.cpp
1 #include "stdafx.h"
2 #include "FPGAIO5771.h"
3 
4 namespace scope {
5 
6 FPGAIO5771::FPGAIO5771(const uint32_t& _user_command_idle
7  , const uint32_t& _pll_locked
8  , const uint32_t& _configured
9  , const uint32_t& _user_error
10  , const uint32_t& _user_command_status
11  , const uint32_t& _user_command_control
12  , const uint32_t& _user_data_0_control
13  , const uint32_t& _user_data_1_control
14  , const uint32_t& _user_command_commit)
15  : user_command_idle_indicator(_user_command_idle)
16  , pll_locked_indicator(_pll_locked)
17  , configured_indicator(_configured)
18  , user_error_indicator(_user_error)
19  , user_command_status_indicator(_user_command_status)
20  , user_command_control(_user_command_control)
21  , user_data_0_control(_user_data_0_control)
22  , user_data_1_control(_user_data_1_control)
23  , user_command_commit_control(_user_command_commit) {
24 
25 }
26 
27 void FPGAIO5771::WaitForIdle(NiFpga_Session _session) {
28  NiFpga_Bool idle = false;
29  uint32_t waitcounter = 0;
30 
31  // Wait until idle indicator is true
32  do {
33  iostatus = NiFpga_ReadBool(_session, user_command_idle_indicator, &idle);
34  std::this_thread::sleep_for(std::chrono::milliseconds(20)); // Wait 20 milliseconds
35  waitcounter += 20;
36  } while ( !idle && (waitcounter < 2000) );
37  if ( waitcounter >= 2000 )
38  throw FPGAException(-1, "SetClockSource timeout");
39 }
40 
41 void FPGAIO5771::SetClockSource(NiFpga_Session _session, const uint8_t& _clock_source) {
42  NiFpga_Bool configured = false;
43  uint32_t waitcounter = 0;
44 
45  // Wait until moduled has configured itself
46  do {
47  iostatus = NiFpga_ReadBool(_session, configured_indicator, &configured);
48  std::this_thread::sleep_for(std::chrono::milliseconds(20)); // Wait 20 milliseconds
49  waitcounter += 20;
50  } while ( !configured && (waitcounter < 2000) );
51  if ( waitcounter >= 5000 )
52  throw FPGAException(-1, "SetClockSource adapter module not configured");
53 
54  // Write user command
55  iostatus = NiFpga_WriteU8(_session, user_command_control, 0);
56  iostatus = NiFpga_WriteU16(_session, user_data_0_control, static_cast<uint16_t>(_clock_source));
57  iostatus = NiFpga_WriteU8(_session, user_data_1_control, 0);
58 
59  WaitForIdle(_session);
60 
61  // Commit user command
62  iostatus = NiFpga_WriteBool(_session, user_command_commit_control, 0);
63  iostatus = NiFpga_WriteBool(_session, user_command_commit_control, 1);
64  iostatus = NiFpga_WriteBool(_session, user_command_commit_control, 0);
65 
66  WaitForIdle(_session);
67 
68  // Check for error
69  uint8_t err = 0;
70  iostatus = NiFpga_ReadU8(_session, user_error_indicator, &err);
71  if ( err != 0 )
72  throw FPGAException(-1, "SetClockSource user error");
73 
74  // If PLL on, wait for lock
75  if ( (_clock_source == 1) || (_clock_source == 3) ) {
76  NiFpga_Bool locked = false;
77  uint32_t waitcounter = 0;
78 
79  do {
80  iostatus = NiFpga_ReadBool(_session, pll_locked_indicator, &locked);
81  std::this_thread::sleep_for(std::chrono::milliseconds(20)); // Wait 20 milliseconds
82  waitcounter += 20;
83  } while ( !locked && (waitcounter < 5000) );
84  if ( waitcounter >= 5000 )
85  throw FPGAException(-1, "SetClockSource PLL not locked");
86 
87  // Wait 6 seconds for the lock to settle (! this is copied from NI's example Labview code...)
88  std::this_thread::sleep_for(std::chrono::seconds(6));
89  }
90 }
91 
92 bool FPGAIO5771::CheckIOModule(NiFpga_Session _session) {
93  return true;
94 }
95 
96 }
const uint32_t user_error_indicator
the User Error indicator on the FPGA
Definition: FPGAIO5771.h:24
const uint32_t user_command_control
the User Command control on the FPGA
Definition: FPGAIO5771.h:30
An exception for FPGA stuff.
Definition: FPGAException.h:9
bool CheckIOModule(NiFpga_Session _session)
Necessary calls at the moment not supported by NI FPGA API 12.0, see CheckIOModule.vi for what to do.
Definition: FPGAIO5771.cpp:92
const uint32_t user_data_1_control
the User Data 1 control on the FPGA
Definition: FPGAIO5771.h:36
const uint32_t configured_indicator
the Configure? indicator on the FPGA
Definition: FPGAIO5771.h:21
const uint32_t pll_locked_indicator
the PLL locked indicator on the FPGA
Definition: FPGAIO5771.h:18
This is the include file for standard system include files, or project specific include files that ar...
const uint32_t user_command_idle_indicator
the User Command Idle indicator on the FPGA
Definition: FPGAIO5771.h:15
void SetClockSource(NiFpga_Session _session, const uint8_t &_clock_source=0)
Possible clock sources (see adapter modules help and Configure Clock.vi from NI 5771 Clock Select exa...
Definition: FPGAIO5771.cpp:41
const uint32_t user_data_0_control
the User Data 0 control on the FPGA
Definition: FPGAIO5771.h:33
FPGAStatusSafe iostatus
Current status of the module.
Definition: FPGAIO5771.h:43
const uint32_t user_command_commit_control
the User Command Commit control on the FPGA
Definition: FPGAIO5771.h:39
FPGAIO5771(const uint32_t &_user_command_idle, const uint32_t &_pll_locked, const uint32_t &_configured, const uint32_t &_user_error, const uint32_t &_user_command_status, const uint32_t &_user_command_control, const uint32_t &_user_data_0_control, const uint32_t &_user_data_1_control, const uint32_t &_user_command_commit)
Get the FPGA VI control/indicator ids.
Definition: FPGAIO5771.cpp:6
void WaitForIdle(NiFpga_Session _session)
Wait for user command idle.
Definition: FPGAIO5771.cpp:27