-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUSRPBackend.m
208 lines (188 loc) · 7.53 KB
/
USRPBackend.m
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
%% Copyright (c) 2017-2017, Yichao Yu <[email protected]>
%
% This library is free software; you can redistribute it and/or
% modify it under the terms of the GNU Lesser General Public
% License as published by the Free Software Foundation; either
% version 3.0 of the License, or (at your option) any later version.
% This library is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
% Lesser General Public License for more details.
% You should have received a copy of the GNU Lesser General Public
% License along with this library.
classdef USRPBackend < PulseBackend
properties(Hidden)
url = '';
poster = [];
type_cache = [];
num_cache = [];
code = '';
seq_id = 0;
end
properties(Constant, Hidden)
CH_AMP = 0;
CH_FREQ = 1;
CH_PHASE = 2;
end
methods
function self = USRPBackend(seq)
self = self@PulseBackend(seq);
%% Hard coded for now
self.url = seq.config.usrpUrls('USRP1');
end
function initDev(self, did)
%% Hard coded for now
if ~strcmp('USRP1', did)
error('Unknown USRP device "%s".', did);
end
end
function initChannel(self, cid)
if length(self.type_cache) >= cid && self.type_cache(cid) > 0
return;
end
name = channelName(self.seq, cid);
if ~strncmp('USRP1/', name, 5)
error('Unknown channel name "%s"', name);
end
name = name(7:end);
[chn_type, chn_num] = parseCId(self, name);
self.type_cache(cid) = chn_type;
self.num_cache(cid) = chn_num;
end
function val = getPriority(~)
val = 1;
end
function prepare(self, ~)
%% This should enable the FPGA backend and therefore the start trigger
findDriver(self.seq, 'FPGABackend2');
end
function generate(self, cids)
%% [n_pulses: 4B]
%% [[[chn_type: 4B][chn_id: 4B][t_start: 8B][t_len: 8B]
%% [[0: 4B][val: 8B] / [code_len: 4B][code: code_len x 4B]]] x n_pulses]
ir_ctx = self.seq.ir_ctx;
CH_AMP = self.CH_AMP;
CH_PHASE = self.CH_PHASE;
ircache = IRCache.get();
type_cache = self.type_cache;
num_cache = self.num_cache;
nchn = length(cids);
default_values = zeros(1, nchn);
n_pulses = 0;
for i = 1:nchn
cid = cids(i);
pulses = getPulses(self.seq, cid);
all_pulses{i} = pulses;
np = size(pulses, 1);
if np == 0
continue;
end
default_values(i) = getDefault(self.seq, cid);
if type_cache(cid) == CH_AMP && default_values(i) ~= 0
%% We do this for two reasons:
%% 1. The semantics of default value would be kind of surprising since we
%% do turn the channel off when it's not used in the sequence.
%% 2. I forgot to implement it on the other side and
%% am currently too lazy to do so... ;-p.
%% Fortunately it doesn't offer anything that can't be done with setting the value
%% directly in the sequence.
error('Non-zero default amplitude not supported for USRP channels');
end
end
code = int32([1, 0]);
targ = IRNode.getArg(1, ir_ctx);
oldarg = IRNode.getArg(2, ir_ctx);
for i = 1:nchn
cid = cids(i);
chn_type = type_cache(cid);
chn_num = num_cache(cid);
pulses = all_pulses{i};
for j = 1:size(pulses, 1)
pulse_obj = pulses{j, 3};
t_start = pulses{j, 1};
if isnumeric(pulse_obj)
val = pulse_obj;
n_pulses = n_pulses + 1;
code = [code, chn_type, chn_num, ...
typecast(double(t_start), 'int32'), ...
0, 0, 0, typecast(double(val), 'int32')];
continue;
end
if chn_type == CH_PHASE
error('Phase ramp not allowed.');
end
step_len = pulses{j, 2};
n_pulses = n_pulses + 1;
code = [code, chn_type, chn_num, ...
typecast(double(t_start), 'int32'), ...
typecast(double(step_len), 'int32')];
isir = 0;
if isa(pulse_obj, 'IRPulse')
irpulse_id = sprintf('%s::%d', pulse_obj.id, ...
typecast(double(step_len), 'int64'));
ir = getindex(ircache, irpulse_id);
if ~isempty(ir)
code = [code, ir];
continue;
end
isir = 1;
end
val = calcValue(pulse_obj, targ, step_len, oldarg);
if isnumeric(val) || islogical(val)
code = [code, 0, typecast(double(val), 'int32')];
else
func = IRFunc(IRNode.TyFloat64, [IRNode.TyFloat64, IRNode.TyFloat64]);
func.setCode(val);
ser = func.serialize();
ir = [length(ser), ser];
code = [code, ir];
if isir
setindex(ircache, ir, irpulse_id);
end
end
end
end
code(2) = n_pulses;
self.code = py.bytes(typecast(code, 'int8'));
self.poster = USRPPoster.get(self.url);
end
function res = getCode(self)
res = self.code;
end
function run(self)
self.seq_id = self.poster.post(self.code);
if self.seq_id == 0
error('USRP run failed');
end
end
function wait(self)
self.poster.wait(self.seq_id);
end
end
methods(Hidden)
function [chn_type, chn_num] = parseCId(self, cid)
cpath = strsplit(cid, '/');
if strncmp(cpath{1}, 'CH', 2)
if size(cpath, 2) ~= 2
error('Invalid USRP channel id "%s".', cid);
end
matches = regexp(cpath{1}, '^CH([1-9]\d*|0)$', 'tokens');
if isempty(matches)
error('No USRP channel number');
end
chn_num = str2double(matches{1}{1});
if strcmp(cpath{2}, 'FREQ')
chn_type = self.CH_FREQ;
elseif strcmp(cpath{2}, 'AMP')
chn_type = self.CH_AMP;
elseif strcmp(cpath{2}, 'PHASE')
chn_type = self.CH_PHASE;
else
error('Invalid USRP parameter name "%s".', cpath{2});
end
else
error('Unknown channel type "%s"', cpath{1});
end
end
end
end