-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUSTCADC.m
More file actions
289 lines (284 loc) · 11.6 KB
/
USTCADC.m
File metadata and controls
289 lines (284 loc) · 11.6 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
% FileName:USTCADC.m
% Author:GuoCheng
% E-mail:fortune@mail.ustc.edu.cn
% All right reserved @ GuoCheng.
% Modified: 2017.9.11
% Description:The class of ADC
classdef USTCADC < handle
properties(SetAccess = private)
name = 'Unnamed'; % ADC name.
netcard = 1; % net card number.
isopen = 0; % open flag.
status = 'close'; % open state.
id = []; % id of adc.
dstmac = '00-00-00-00-00-00'; % mac of dstination.
srcmac = '00-00-00-00-00-00'; % mac address of pc.
isdemod = 0; % is run demod mode.
sample_rate = 1e9; % ADC sample rate.
channel_amount = 2; % ADC channel amount, I & Q.
channel_gain = [80,80]; % channel gain.
sample_depth = 2000; % ADC sample depth.
trig_count = 1; % ADC accept trigger count.
window_start = 0; % start position of demod window.
window_width = 2000; % demod window width.
demod_freq = 100e6; % demod frequency.
end
properties (GetAccess = private,Constant = true)
driver = 'USTCADCDriver';
driverh = 'USTCADCDriver.h';
driverdll = 'USTCADCDriver.dll';
end
methods(Static = true)
function LoadLibrary()
if(~libisloaded(USTCADC.driver))
loadlibrary(USTCADC.driverdll,USTCADC.driverh);
end
end
function info = GetDriverInformation()
str = libpointer('cstring',blanks(1024));
[ErrorCode,info] = calllib(USTCADC.driver,'GetSoftInformation',str);
USTCADC.DispError('USTCDAC:GetDriverInformation:',ErrorCode);
end
function list = ListAdapter()
list = blanks(2048);
str = libpointer('cstring',blanks(2048));
[ErrorCode,info] = calllib(USTCADC.driver,'GetAdapterList',str);
if(ErrorCode == 0)
info = regexp(info,'\n', 'split');pos = 1;
for index = 1:length(info)
info{index} = [num2str(index),' : ',info{index}];
list(pos:pos + length(info{index})) = [info{index},10];
pos = pos + length(info{index}) + 1;
end
end
USTCADC.DispError('USTCADC:ListAdapter',ErrorCode);
end
function DispError(MsgID,errorcode,id)
if nargin == 2
id = 0;
end
if(errorcode ~= 0)
str = libpointer('cstring',blanks(1024));
[~,info] = calllib(USTCADC.driver,'GetErrorMsg',int32(id),int32(errorcode),str);
msg = ['Error code:',num2str(errorcode),' --> ',info];
WriteErrorLog([MsgID,' ',msg]);
error(MsgID,[MsgID,' ',msg]);
end
end
end
methods
function obj = USTCADC(srcmac,dstmac)
obj.srcmac = srcmac;
obj.dstmac = dstmac;
obj.isopen = false;
obj.status = 'close';
end
function Open(obj)
if ~obj.isopen
obj.LoadLibrary();
pMacSrc = libpointer('string',obj.srcmac);
pMacDst = libpointer('string',obj.dstmac);
[ErrorCode,obj.id] = calllib(obj.driver,'OpenADC',0,pMacSrc,pMacDst);
obj.DispError('USTCADC:Open',ErrorCode,obj.id);
obj.status = 'open';
obj.isopen = true;
end
end
function Close(obj)
if(obj.isopen == true)
ErrorCode = calllib(obj.driver,'CloseADC',int32(obj.id));
obj.DispError('USTCADC:Close',ErrorCode,obj.id);
obj.status = 'close';
obj.isopen = false;
obj.id = [];
end
end
function Init(obj)
obj.GetMacAddr(0); % Acquire mac address of PC from dll.
obj.SetMacAddr(); % Set PC's mac address as ADC's destination address.
obj.SetSampleDepth();
obj.SetTrigCount();
obj.SetMode();
obj.SetWindowWidth();
obj.SetWindowStart();
obj.SetDemoFre();
obj.SetGain();
end
function EnableADC(obj)
data = [0,3,238,238,238,238,238,238];
pdata = libpointer('uint8Ptr', data);
[ErrorCode,~] = calllib(obj.driver,'SendData',int32(obj.id),int32(8),pdata);
obj.DispError('USTCADC:EnableADC',ErrorCode,obj.id);
end
function ForceTrig(obj)
data = [0,1,238,238,238,238,238,238];
pdata = libpointer('uint8Ptr', data);
[ErrorCode,~] = calllib(obj.driver,'SendData',int32(obj.id),int32(8),pdata);
obj.DispError('USTCADC:ForceTrig',ErrorCode,obj.id);
end
function GetMacAddr(obj,isDst)
data = zeros(1,6);
pdata = libpointer('uint8Ptr', data);
[ErrorCode,data] = calllib(obj.driver,'GetMacAddress',int32(obj.id),int32(isDst),pdata);
obj.DispError('USTCADC:GetMacAddr',ErrorCode,obj.id);
str(1:17) = '-';data = dec2hex(data);
for k = 1:6
str(3*k-2:3*k-1) = data(k,1:2);
end
if(isDst == false)
obj.srcmac = str;
else
obj.dstmac = str;
end
end
function SetMacAddr(obj,srcmac)
if(nargin == 2)
obj.srcmac = srcmac;
end
if(obj.isopen)
macdata = regexp(obj.srcmac,'-', 'split');
macdata = hex2dec(macdata)';
data = [0,17,macdata];
pdata = libpointer('uint8Ptr', data);
[ErrorCode,~] = calllib(obj.driver,'SendData',int32(obj.id),int32(length(data)),pdata);
obj.DispError('USTCADC:SetMacAddr',ErrorCode,obj.id);
end
end
function SetSampleDepth(obj,depth)
if(nargin == 2)
obj.sample_depth = depth;
end
if(obj.isopen)
data = [0,18,obj.sample_depth/256,mod(obj.sample_depth,256)];
pdata = libpointer('uint8Ptr', data);
[ErrorCode,~] = calllib(obj.driver,'SendData',int32(obj.id),int32(4),pdata);
obj.DispError('USTCADC:SetSampleDepth',ErrorCode,obj.id);
end
end
function SetTrigCount(obj,count)
if(nargin == 2)
obj.trig_count = count;
end
if(obj.isopen)
data = [0,19,obj.trig_count/256,mod(obj.trig_count,256)];
pdata = libpointer('uint8Ptr', data);
[ErrorCode,~] = calllib(obj.driver,'SendData',int32(obj.id),int32(4),pdata);
obj.DispError('USTCADC:SetTrigCount',ErrorCode,obj.id);
end
end
function SetMode(obj,isdemod)
if(nargin == 2)
obj.isdemod = isdemod;
end
if(obj.isopen)
if(obj.isdemod == 0)
data = [1,1,17,17,17,17,17,17];
else
data = [1,1,34,34,34,34,34,34];
end
pdata = libpointer('uint8Ptr', data);
[ErrorCode,~] = calllib(obj.driver,'SendData',int32(obj.id),int32(8),pdata);
obj.DispError('USTCADC:SetMode',ErrorCode,obj.id);
end
end
function SetWindowWidth(obj,length)
if(nargin == 2)
obj.window_width = length;
end
if(obj.isopen)
data = [0,20,floor(obj.window_width/256),mod(obj.window_width,256),0,0,0,0];
pdata = libpointer('uint8Ptr', data);
[ErrorCode,~] = calllib(obj.driver,'SendData',int32(obj.id),int32(8),pdata);
obj.DispError('USTCADC:SetWindowLength',ErrorCode,obj.id);
end
end
function SetWindowStart(obj,pos)
if(nargin == 2)
obj.window_start = pos;
end
if(obj.isopen)
data = [0,21,floor(obj.window_start/256),mod(obj.window_start,256),0,0,0,0];
pdata = libpointer('uint8Ptr', data);
[ErrorCode,~] = calllib(obj.driver,'SendData',int32(obj.id),int32(8),pdata);
obj.DispError('USTCADC:SetWindowStart',ErrorCode,obj.id);
end
end
function SetDemoFre(obj,fre)
if(nargin == 2)
obj.demod_freq = fre;
end
if(obj.isopen)
for k = 1:length(obj.demod_freq)
step = floor(obj.demod_freq(k)/1e9*16777216 + 0.5);
data1 = 0;
if(step < 0)
data1 = 128;
step = abs(step);
end
data1 = data1 + floor(step/131072);
data2 = mod(floor(step/512),256);
data3 = mod(floor(step/2),256);
data4 = mod(step,2)*128;
data = [0,22,data1,data2,data3,data4,0,0];
pdata = libpointer('uint8Ptr', data);
[ErrorCode,~] = calllib(obj.driver,'SendData',int32(obj.id),int32(8),pdata);
obj.DispError('USTCADC:SetDemoFre',ErrorCode,obj.id);
end
end
end
function SetGain(obj,gain)
if(nargin == 2)
if(length(gain) == obj.channel_amount)
obj.channel_gain = gain;
end
end
if(obj.isopen)
data = [0,23,obj.channel_gain(1),obj.channel_gain(2),0,0,0,0];
pdata = libpointer('uint8Ptr',data);
[ErrorCode,~] = calllib(obj.driver,'SendData',int32(obj.id),int32(8),pdata);
obj.DispError('USTCADC:SetGain',ErrorCode,obj.id);
end
end
function CommitDemodSet(obj,module)
if(obj.isopen)
data = [0,24,module*16,0,0,0,0,0];
pdata = libpointer('uint8Ptr',data);
[ErrorCode,~] = calllib(obj.driver,'SendData',int32(obj.id),int32(8),pdata);
obj.DispError('USTCADC:CommitDemodSet',ErrorCode,obj.id);
end
end
function SetADName(obj,name)
obj.name = name;
end
function SetSampleFreq(obj,freq)
obj.sample_rate = freq;
end
function SetChannelNum(obj,num)
if(~obj.isopen)
if(num ~= obj.channel_amount)
obj.channel_amount = num;
obj.channel_gain = zeros(1,num) + 80;
end
end
end
function [ret,I,Q] = RecvData(obj)
if(obj.isdemod)
IQ = zeros(24*obj.trig_count,1);
pIQ = libpointer('int32Ptr', IQ);
[ret,IQ] = calllib(obj.driver,'RecvDemo',int32(obj.id),int32(obj.trig_count),pIQ);
I = IQ(1:2:length(IQ));
I = reshape(I,12,obj.trig_count);
Q = IQ(2:2:length(IQ));
Q = reshape(Q,12,obj.trig_count);
else
I = zeros(obj.trig_count*obj.sample_depth,1);
Q = zeros(obj.trig_count*obj.sample_depth,1);
pI = libpointer('uint8Ptr', I);
pQ = libpointer('uint8Ptr', Q);
[ret,I,Q] = calllib(obj.driver,'RecvData',int32(obj.id),int32(obj.trig_count),int32(obj.sample_depth),pI,pQ);
I = (reshape(I,[obj.sample_depth,obj.trig_count]))';
Q = (reshape(Q,[obj.sample_depth,obj.trig_count]))';
end
end
end
end