This repository was archived by the owner on Mar 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDigital.cs
More file actions
171 lines (154 loc) · 7.44 KB
/
Digital.cs
File metadata and controls
171 lines (154 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using Ivi.Driver;
using NationalInstruments.ModularInstruments.NIDigital;
using System;
using System.IO;
namespace NationalInstruments.ReferenceDesignLibraries
{
public static class Digital
{
#region Type Definitions
public struct ProjectFiles
{
public string PinMapFile;
public string[] SpecificationsFiles;
public string[] PinLevelsFiles;
public string[] TimingFiles;
public string[] DigitalPatternFiles;
}
public struct SourcePinConfiguration
{
public string PinName;
public PpmuOutputFunction PinOutputFunction;
public double Voltage_V;
public double Current_A;
public static SourcePinConfiguration GetDefault()
{
return new SourcePinConfiguration
{
PinName = "",
PinOutputFunction = PpmuOutputFunction.DCVoltage,
Voltage_V = 1.8,
Current_A = 0.2,
};
}
}
public struct TriggerConfiguration
{
public TriggerType BurstTriggerType;
public DigitalEdge DigitalEdgeType;
public string DigitalEdgeSource;
public static TriggerConfiguration GetDefault()
{
return new TriggerConfiguration
{
BurstTriggerType = TriggerType.None,
DigitalEdgeType = DigitalEdge.Rising,
DigitalEdgeSource = "PXI_Trig0",
};
}
}
#endregion
#region Configuration
public static void LoadProjectFiles(NIDigital nIDigital, ProjectFiles projectFiles)
{
if (string.IsNullOrEmpty(projectFiles.PinMapFile))
{
throw new ArgumentException("A pin map file is required by the instrument", "ProjectFiles.PinMapFile");
}
else nIDigital.LoadPinMap(projectFiles.PinMapFile);
if (projectFiles.SpecificationsFiles.Length > 0)
{
nIDigital.LoadSpecifications(projectFiles.SpecificationsFiles);
} //Specifications sheets are not required by the instrument
if (projectFiles.PinLevelsFiles.Length > 0)
{
nIDigital.LoadLevels(projectFiles.PinLevelsFiles);
}
else throw new ArgumentException("At least one levels sheet must be supplied to the instrument", "PinLevelsFiles");
if (projectFiles.TimingFiles.Length > 0)
{
nIDigital.LoadTiming(projectFiles.TimingFiles);
}
else throw new ArgumentException("At least one timing sheet must be supplied to the instrument", "TimingFiles");
foreach (string path in projectFiles.DigitalPatternFiles) nIDigital.LoadPattern(path);
if (projectFiles.PinLevelsFiles.Length == 1 && projectFiles.TimingFiles.Length == 1)
{
nIDigital.ApplyLevelsAndTiming("", projectFiles.PinLevelsFiles[0], projectFiles.TimingFiles[0]);
}
}
public static void ConfigureAndSourcePin(NIDigital nIDigital, SourcePinConfiguration sourceConfig)
{
DigitalPinSet pinSet = nIDigital.PinAndChannelMap.GetPinSet(sourceConfig.PinName);
pinSet.Ppmu.OutputFunction = sourceConfig.PinOutputFunction;
switch (sourceConfig.PinOutputFunction)
{
case PpmuOutputFunction.DCCurrent:
pinSet.Ppmu.DCCurrent.CurrentLevel = sourceConfig.Current_A;
break;
case PpmuOutputFunction.DCVoltage:
pinSet.Ppmu.DCVoltage.VoltageLevel = sourceConfig.Voltage_V;
break;
}
pinSet.Ppmu.Source();
}
public static void ApplyPinTDROffset(NIDigital nIDigital, string pinName, double cableDelay_s)
{
//Create a new PrecisionTimeSpan array with the cable delay value
PrecisionTimeSpan[] offsets = new PrecisionTimeSpan[1] { PrecisionTimeSpan.FromSeconds(cableDelay_s) };
nIDigital.PinAndChannelMap.GetPinSet(pinName).ApplyTdrOffsets(offsets);
}
public static void InitiatePatternGeneration(NIDigital nIDigital, string patternStartLabel, TriggerConfiguration triggerConfig)
{
switch (triggerConfig.BurstTriggerType)
{
case TriggerType.Software:
nIDigital.Trigger.StartTrigger.Software.Configure();
// This call to BurstPattern returns immediately because waitUntilDone is 'false.'
nIDigital.PatternControl.BurstPattern(string.Empty, patternStartLabel, true, false, TimeSpan.FromSeconds(10));
break;
case TriggerType.DigitalEdge:
nIDigital.Trigger.StartTrigger.DigitalEdge.Configure(triggerConfig.DigitalEdgeSource, triggerConfig.DigitalEdgeType);
// This call to BurstPattern returns immediately because waitUntilDone is 'false.'
nIDigital.PatternControl.BurstPattern("", patternStartLabel, true, false, TimeSpan.FromSeconds(10));
break;
case TriggerType.None:
nIDigital.PatternControl.BurstPattern(string.Empty, patternStartLabel, true, TimeSpan.FromSeconds(10));
break;
}
}
public static void DisconnectAndClose(NIDigital nIDigital)
{
//Disconnect all pins before closing
nIDigital.PinAndChannelMap.GetPinSet("").SelectedFunction = SelectedFunction.Disconnect;
nIDigital.Close();
}
#endregion
#region Results
#endregion
public static class Utilities
{
public static ProjectFiles SearchForProjectFiles(string searchDirectory, bool recursiveSearch)
{
ProjectFiles results = new ProjectFiles();
if (!Directory.Exists(searchDirectory))
throw new DirectoryNotFoundException();
//Setup search options for the file search
SearchOption searchOpt;
if (recursiveSearch) searchOpt = SearchOption.AllDirectories;
else searchOpt = SearchOption.TopDirectoryOnly;
string[] pinMapFiles = Directory.GetFiles(searchDirectory, "*.pinmap", searchOpt);
if (pinMapFiles.Length > 1)
{
throw new ArgumentOutOfRangeException("More than one Pin Map files were" +
"found in the specified search directory. The instrument can only load one at a time");
}
else if (pinMapFiles.Length == 1) results.PinMapFile = pinMapFiles[0];
results.DigitalPatternFiles = Directory.GetFiles(searchDirectory, "*.digipat", searchOpt);
results.PinLevelsFiles = Directory.GetFiles(searchDirectory, "*.digilevels", searchOpt);
results.SpecificationsFiles = Directory.GetFiles(searchDirectory, "*.specs", searchOpt);
results.TimingFiles = Directory.GetFiles(searchDirectory, "*.digitiming", searchOpt);
return results;
}
}
}
}