]>
Commit | Line | Data |
---|---|---|
06a3e78a DJ |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
6ec6c43b | 20 | #include <config.h> |
13f2b9d7 | 21 | #include <stdlib.h> |
5a1afc09 | 22 | #include "scpi.h" |
06a3e78a DJ |
23 | #include "protocol.h" |
24 | ||
13f2b9d7 DJ |
25 | #define SERIALCOMM "115200/8n1/flow=1" |
26 | ||
dd5c48a6 | 27 | static struct sr_dev_driver hameg_hmo_driver_info; |
e9a62139 DJ |
28 | |
29 | static const char *manufacturers[] = { | |
30 | "HAMEG", | |
da1726cc | 31 | "Rohde&Schwarz", |
e9a62139 DJ |
32 | }; |
33 | ||
584560f1 | 34 | static const uint32_t scanopts[] = { |
13f2b9d7 DJ |
35 | SR_CONF_CONN, |
36 | SR_CONF_SERIALCOMM, | |
37 | }; | |
38 | ||
55fb76b3 UH |
39 | static const uint32_t drvopts[] = { |
40 | SR_CONF_OSCILLOSCOPE, | |
41 | }; | |
42 | ||
719eff68 | 43 | enum { |
61e77667 UH |
44 | CG_INVALID = -1, |
45 | CG_NONE, | |
46 | CG_ANALOG, | |
47 | CG_DIGITAL, | |
719eff68 UH |
48 | }; |
49 | ||
373e92a4 | 50 | static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) |
e9a62139 DJ |
51 | { |
52 | struct sr_dev_inst *sdi; | |
53 | struct dev_context *devc; | |
54 | struct sr_scpi_hw_info *hw_info; | |
e9a62139 DJ |
55 | |
56 | sdi = NULL; | |
57 | devc = NULL; | |
e9a62139 DJ |
58 | hw_info = NULL; |
59 | ||
e9a62139 DJ |
60 | if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) { |
61 | sr_info("Couldn't get IDN response."); | |
62 | goto fail; | |
63 | } | |
64 | ||
697fb6dd | 65 | if (std_str_idx_s(hw_info->manufacturer, ARRAY_AND_SIZE(manufacturers)) < 0) |
e9a62139 DJ |
66 | goto fail; |
67 | ||
aac29cc1 | 68 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be UH |
69 | sdi->vendor = g_strdup(hw_info->manufacturer); |
70 | sdi->model = g_strdup(hw_info->model); | |
71 | sdi->version = g_strdup(hw_info->firmware_version); | |
72 | sdi->serial_num = g_strdup(hw_info->serial_number); | |
4f840ce9 | 73 | sdi->driver = &hameg_hmo_driver_info; |
b33db61c SA |
74 | sdi->inst_type = SR_INST_SCPI; |
75 | sdi->conn = scpi; | |
b33db61c | 76 | |
e9a62139 DJ |
77 | sr_scpi_hw_info_free(hw_info); |
78 | hw_info = NULL; | |
79 | ||
f57d8ffe | 80 | devc = g_malloc0(sizeof(struct dev_context)); |
e9a62139 | 81 | |
e9a62139 | 82 | sdi->priv = devc; |
e9a62139 DJ |
83 | |
84 | if (hmo_init_device(sdi) != SR_OK) | |
85 | goto fail; | |
86 | ||
87 | return sdi; | |
88 | ||
89 | fail: | |
7b365c47 | 90 | sr_scpi_hw_info_free(hw_info); |
4bf93988 | 91 | sr_dev_inst_free(sdi); |
b1f83103 | 92 | g_free(devc); |
e9a62139 DJ |
93 | |
94 | return NULL; | |
95 | } | |
96 | ||
4f840ce9 | 97 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
06a3e78a | 98 | { |
373e92a4 | 99 | return sr_scpi_scan(di->context, options, probe_device); |
06a3e78a DJ |
100 | } |
101 | ||
3553451f | 102 | static void clear_helper(struct dev_context *devc) |
13f2b9d7 | 103 | { |
719eff68 | 104 | hmo_scope_state_free(devc->model_state); |
13f2b9d7 DJ |
105 | g_free(devc->analog_groups); |
106 | g_free(devc->digital_groups); | |
13f2b9d7 DJ |
107 | } |
108 | ||
4f840ce9 | 109 | static int dev_clear(const struct sr_dev_driver *di) |
06a3e78a | 110 | { |
3553451f | 111 | return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper); |
06a3e78a DJ |
112 | } |
113 | ||
114 | static int dev_open(struct sr_dev_inst *sdi) | |
115 | { | |
6402c379 | 116 | if (sr_scpi_open(sdi->conn) != SR_OK) |
13f2b9d7 | 117 | return SR_ERR; |
06a3e78a | 118 | |
719eff68 | 119 | if (hmo_scope_state_get(sdi) != SR_OK) |
13f2b9d7 | 120 | return SR_ERR; |
06a3e78a | 121 | |
06a3e78a DJ |
122 | return SR_OK; |
123 | } | |
124 | ||
125 | static int dev_close(struct sr_dev_inst *sdi) | |
126 | { | |
f1ba6b4b | 127 | return sr_scpi_close(sdi->conn); |
06a3e78a DJ |
128 | } |
129 | ||
660e398f | 130 | static int check_channel_group(struct dev_context *devc, |
53b4680f | 131 | const struct sr_channel_group *cg) |
13f2b9d7 | 132 | { |
329733d9 | 133 | const struct scope_config *model; |
13f2b9d7 DJ |
134 | |
135 | model = devc->model_config; | |
136 | ||
53b4680f | 137 | if (!cg) |
61e77667 | 138 | return CG_NONE; |
13f2b9d7 | 139 | |
fcd6a8bd UH |
140 | if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) >= 0) |
141 | return CG_ANALOG; | |
13f2b9d7 | 142 | |
fcd6a8bd UH |
143 | if (std_cg_idx(cg, devc->digital_groups, model->digital_pods) >= 0) |
144 | return CG_DIGITAL; | |
13f2b9d7 | 145 | |
660e398f | 146 | sr_err("Invalid channel group specified."); |
89280b1a | 147 | |
61e77667 | 148 | return CG_INVALID; |
13f2b9d7 DJ |
149 | } |
150 | ||
dd7a72ea UH |
151 | static int config_get(uint32_t key, GVariant **data, |
152 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
06a3e78a | 153 | { |
fcd6a8bd | 154 | int cg_type, idx; |
13f2b9d7 | 155 | struct dev_context *devc; |
329733d9 | 156 | const struct scope_config *model; |
14a2f74d | 157 | struct scope_state *state; |
13f2b9d7 | 158 | |
709468ba | 159 | if (!sdi) |
13f2b9d7 DJ |
160 | return SR_ERR_ARG; |
161 | ||
709468ba UH |
162 | devc = sdi->priv; |
163 | ||
61e77667 | 164 | if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID) |
13f2b9d7 | 165 | return SR_ERR; |
06a3e78a | 166 | |
13f2b9d7 | 167 | model = devc->model_config; |
14a2f74d | 168 | state = devc->model_state; |
06a3e78a | 169 | |
06a3e78a | 170 | switch (key) { |
bf622e6d | 171 | case SR_CONF_NUM_HDIV: |
13f2b9d7 | 172 | *data = g_variant_new_int32(model->num_xdivs); |
13f2b9d7 | 173 | break; |
3fd2dca2 DJ |
174 | case SR_CONF_TIMEBASE: |
175 | *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0], | |
176 | (*model->timebases)[state->timebase][1]); | |
3fd2dca2 | 177 | break; |
13f2b9d7 | 178 | case SR_CONF_NUM_VDIV: |
3782e571 | 179 | if (!cg) |
660e398f | 180 | return SR_ERR_CHANNEL_GROUP; |
3782e571 | 181 | if (cg_type != CG_ANALOG) |
758906aa | 182 | return SR_ERR_NA; |
3782e571 UH |
183 | if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) < 0) |
184 | return SR_ERR_ARG; | |
185 | *data = g_variant_new_int32(model->num_ydivs); | |
3fd2dca2 DJ |
186 | break; |
187 | case SR_CONF_VDIV: | |
3782e571 | 188 | if (!cg) |
61e77667 | 189 | return SR_ERR_CHANNEL_GROUP; |
3782e571 | 190 | if (cg_type != CG_ANALOG) |
758906aa | 191 | return SR_ERR_NA; |
3782e571 UH |
192 | if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0) |
193 | return SR_ERR_ARG; | |
194 | *data = g_variant_new("(tt)", | |
195 | (*model->vdivs)[state->analog_channels[idx].vdiv][0], | |
196 | (*model->vdivs)[state->analog_channels[idx].vdiv][1]); | |
ccf14618 DJ |
197 | break; |
198 | case SR_CONF_TRIGGER_SOURCE: | |
199 | *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]); | |
ccf14618 | 200 | break; |
3fd2dca2 DJ |
201 | case SR_CONF_TRIGGER_SLOPE: |
202 | *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]); | |
3fd2dca2 DJ |
203 | break; |
204 | case SR_CONF_HORIZ_TRIGGERPOS: | |
205 | *data = g_variant_new_double(state->horiz_triggerpos); | |
3fd2dca2 | 206 | break; |
ccf14618 | 207 | case SR_CONF_COUPLING: |
3782e571 | 208 | if (!cg) |
660e398f | 209 | return SR_ERR_CHANNEL_GROUP; |
3782e571 | 210 | if (cg_type != CG_ANALOG) |
758906aa | 211 | return SR_ERR_NA; |
3782e571 UH |
212 | if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0) |
213 | return SR_ERR_ARG; | |
214 | *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[idx].coupling]); | |
13f2b9d7 | 215 | break; |
14a2f74d DJ |
216 | case SR_CONF_SAMPLERATE: |
217 | *data = g_variant_new_uint64(state->sample_rate); | |
14a2f74d | 218 | break; |
06a3e78a | 219 | default: |
758906aa | 220 | return SR_ERR_NA; |
06a3e78a DJ |
221 | } |
222 | ||
758906aa | 223 | return SR_OK; |
06a3e78a DJ |
224 | } |
225 | ||
dd7a72ea UH |
226 | static int config_set(uint32_t key, GVariant *data, |
227 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
06a3e78a | 228 | { |
fcd6a8bd | 229 | int ret, cg_type, idx, j; |
965b463d | 230 | char command[MAX_COMMAND_SIZE], float_str[30]; |
13f2b9d7 | 231 | struct dev_context *devc; |
329733d9 | 232 | const struct scope_config *model; |
13f2b9d7 | 233 | struct scope_state *state; |
89280b1a | 234 | double tmp_d; |
23e1ea7a | 235 | gboolean update_sample_rate; |
06a3e78a | 236 | |
b0baddef | 237 | if (!sdi) |
13f2b9d7 DJ |
238 | return SR_ERR_ARG; |
239 | ||
b0baddef UH |
240 | devc = sdi->priv; |
241 | ||
61e77667 | 242 | if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID) |
13f2b9d7 DJ |
243 | return SR_ERR; |
244 | ||
245 | model = devc->model_config; | |
246 | state = devc->model_state; | |
23e1ea7a | 247 | update_sample_rate = FALSE; |
13f2b9d7 | 248 | |
06a3e78a | 249 | switch (key) { |
13f2b9d7 DJ |
250 | case SR_CONF_LIMIT_FRAMES: |
251 | devc->frame_limit = g_variant_get_uint64(data); | |
252 | ret = SR_OK; | |
253 | break; | |
13f2b9d7 | 254 | case SR_CONF_TRIGGER_SOURCE: |
bd633efa UH |
255 | if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0) |
256 | return SR_ERR_ARG; | |
257 | state->trigger_source = idx; | |
258 | g_snprintf(command, sizeof(command), | |
259 | (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE], | |
260 | (*model->trigger_sources)[idx]); | |
261 | ret = sr_scpi_send(sdi->conn, command); | |
89280b1a | 262 | break; |
13f2b9d7 | 263 | case SR_CONF_VDIV: |
61233697 | 264 | if (!cg) |
660e398f | 265 | return SR_ERR_CHANNEL_GROUP; |
697fb6dd UH |
266 | if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0) |
267 | return SR_ERR_ARG; | |
fcd6a8bd UH |
268 | if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0) |
269 | return SR_ERR_ARG; | |
270 | state->analog_channels[j].vdiv = idx; | |
271 | g_ascii_formatd(float_str, sizeof(float_str), "%E", | |
272 | (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]); | |
273 | g_snprintf(command, sizeof(command), | |
274 | (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV], | |
275 | j + 1, float_str); | |
276 | if (sr_scpi_send(sdi->conn, command) != SR_OK || | |
277 | sr_scpi_get_opc(sdi->conn) != SR_OK) | |
278 | return SR_ERR; | |
697fb6dd | 279 | ret = SR_OK; |
89280b1a | 280 | break; |
13f2b9d7 | 281 | case SR_CONF_TIMEBASE: |
697fb6dd UH |
282 | if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0) |
283 | return SR_ERR_ARG; | |
284 | state->timebase = idx; | |
285 | g_ascii_formatd(float_str, sizeof(float_str), "%E", | |
286 | (float) (*model->timebases)[idx][0] / (*model->timebases)[idx][1]); | |
287 | g_snprintf(command, sizeof(command), | |
288 | (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE], | |
289 | float_str); | |
290 | ret = sr_scpi_send(sdi->conn, command); | |
291 | update_sample_rate = TRUE; | |
89280b1a | 292 | break; |
13f2b9d7 | 293 | case SR_CONF_HORIZ_TRIGGERPOS: |
89280b1a | 294 | tmp_d = g_variant_get_double(data); |
89280b1a | 295 | if (tmp_d < 0.0 || tmp_d > 1.0) |
13f2b9d7 | 296 | return SR_ERR; |
422a1c0d DJ |
297 | state->horiz_triggerpos = tmp_d; |
298 | tmp_d = -(tmp_d - 0.5) * | |
299 | ((double) (*model->timebases)[state->timebase][0] / | |
300 | (*model->timebases)[state->timebase][1]) | |
301 | * model->num_xdivs; | |
422a1c0d | 302 | g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d); |
13f2b9d7 DJ |
303 | g_snprintf(command, sizeof(command), |
304 | (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS], | |
422a1c0d | 305 | float_str); |
13f2b9d7 | 306 | ret = sr_scpi_send(sdi->conn, command); |
89280b1a | 307 | break; |
13f2b9d7 | 308 | case SR_CONF_TRIGGER_SLOPE: |
bd633efa UH |
309 | if ((idx = std_str_idx(data, *model->trigger_slopes, model->num_trigger_slopes)) < 0) |
310 | return SR_ERR_ARG; | |
311 | state->trigger_slope = idx; | |
312 | g_snprintf(command, sizeof(command), | |
313 | (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE], | |
314 | (*model->trigger_slopes)[idx]); | |
315 | ret = sr_scpi_send(sdi->conn, command); | |
89280b1a | 316 | break; |
13f2b9d7 | 317 | case SR_CONF_COUPLING: |
61233697 | 318 | if (!cg) |
660e398f | 319 | return SR_ERR_CHANNEL_GROUP; |
bd633efa UH |
320 | if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0) |
321 | return SR_ERR_ARG; | |
fcd6a8bd UH |
322 | if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0) |
323 | return SR_ERR_ARG; | |
324 | state->analog_channels[j].coupling = idx; | |
325 | g_snprintf(command, sizeof(command), | |
326 | (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING], | |
327 | j + 1, (*model->coupling_options)[idx]); | |
328 | if (sr_scpi_send(sdi->conn, command) != SR_OK || | |
329 | sr_scpi_get_opc(sdi->conn) != SR_OK) | |
330 | return SR_ERR; | |
bd633efa | 331 | ret = SR_OK; |
89280b1a | 332 | break; |
06a3e78a DJ |
333 | default: |
334 | ret = SR_ERR_NA; | |
13f2b9d7 | 335 | break; |
06a3e78a DJ |
336 | } |
337 | ||
13f2b9d7 DJ |
338 | if (ret == SR_OK) |
339 | ret = sr_scpi_get_opc(sdi->conn); | |
340 | ||
23e1ea7a DJ |
341 | if (ret == SR_OK && update_sample_rate) |
342 | ret = hmo_update_sample_rate(sdi); | |
343 | ||
06a3e78a DJ |
344 | return ret; |
345 | } | |
346 | ||
dd7a72ea UH |
347 | static int config_list(uint32_t key, GVariant **data, |
348 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
06a3e78a | 349 | { |
6ec3ef9b AJ |
350 | int cg_type = CG_NONE; |
351 | struct dev_context *devc = NULL; | |
329733d9 | 352 | const struct scope_config *model = NULL; |
13f2b9d7 | 353 | |
4d399734 UH |
354 | if (sdi) { |
355 | devc = sdi->priv; | |
6ec3ef9b AJ |
356 | if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID) |
357 | return SR_ERR; | |
13f2b9d7 | 358 | |
6ec3ef9b AJ |
359 | model = devc->model_config; |
360 | } | |
06a3e78a | 361 | |
06a3e78a | 362 | switch (key) { |
226363c4 | 363 | case SR_CONF_SCAN_OPTIONS: |
53012da6 | 364 | *data = std_gvar_array_u32(ARRAY_AND_SIZE(scanopts)); |
226363c4 | 365 | break; |
13f2b9d7 | 366 | case SR_CONF_DEVICE_OPTIONS: |
61233697 | 367 | if (!cg) { |
6ec3ef9b | 368 | if (model) |
105df674 | 369 | *data = std_gvar_array_u32((const uint32_t *)model->devopts, model->num_devopts); |
6ec3ef9b | 370 | else |
53012da6 | 371 | *data = std_gvar_array_u32(ARRAY_AND_SIZE(drvopts)); |
61e77667 | 372 | } else if (cg_type == CG_ANALOG) { |
105df674 | 373 | *data = std_gvar_array_u32((const uint32_t *)model->devopts_cg_analog, model->num_devopts_cg_analog); |
13f2b9d7 | 374 | } else { |
105df674 | 375 | *data = std_gvar_array_u32(NULL, 0); |
13f2b9d7 DJ |
376 | } |
377 | break; | |
13f2b9d7 | 378 | case SR_CONF_COUPLING: |
61233697 | 379 | if (!cg) |
660e398f | 380 | return SR_ERR_CHANNEL_GROUP; |
b0e80e9a GS |
381 | if (!model) |
382 | return SR_ERR_ARG; | |
692716f5 | 383 | *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options); |
13f2b9d7 | 384 | break; |
13f2b9d7 | 385 | case SR_CONF_TRIGGER_SOURCE: |
6ec3ef9b AJ |
386 | if (!model) |
387 | return SR_ERR_ARG; | |
692716f5 | 388 | *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources); |
13f2b9d7 | 389 | break; |
bbabdaf1 | 390 | case SR_CONF_TRIGGER_SLOPE: |
6ec3ef9b AJ |
391 | if (!model) |
392 | return SR_ERR_ARG; | |
692716f5 | 393 | *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes); |
bbabdaf1 | 394 | break; |
13f2b9d7 | 395 | case SR_CONF_TIMEBASE: |
6ec3ef9b AJ |
396 | if (!model) |
397 | return SR_ERR_ARG; | |
58ffcf97 | 398 | *data = std_gvar_tuple_array(*model->timebases, model->num_timebases); |
13f2b9d7 | 399 | break; |
13f2b9d7 | 400 | case SR_CONF_VDIV: |
61233697 | 401 | if (!cg) |
660e398f | 402 | return SR_ERR_CHANNEL_GROUP; |
b0e80e9a GS |
403 | if (!model) |
404 | return SR_ERR_ARG; | |
58ffcf97 | 405 | *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs); |
13f2b9d7 | 406 | break; |
06a3e78a DJ |
407 | default: |
408 | return SR_ERR_NA; | |
409 | } | |
410 | ||
13f2b9d7 | 411 | return SR_OK; |
06a3e78a DJ |
412 | } |
413 | ||
13f2b9d7 | 414 | SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi) |
06a3e78a | 415 | { |
13f2b9d7 | 416 | char command[MAX_COMMAND_SIZE]; |
ba7dd8bb | 417 | struct sr_channel *ch; |
13f2b9d7 | 418 | struct dev_context *devc; |
329733d9 | 419 | const struct scope_config *model; |
13f2b9d7 DJ |
420 | |
421 | devc = sdi->priv; | |
422 | model = devc->model_config; | |
423 | ||
ba7dd8bb | 424 | ch = devc->current_channel->data; |
13f2b9d7 | 425 | |
ba7dd8bb | 426 | switch (ch->type) { |
3f239f08 | 427 | case SR_CHANNEL_ANALOG: |
13f2b9d7 DJ |
428 | g_snprintf(command, sizeof(command), |
429 | (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA], | |
65a6794e GS |
430 | #ifdef WORDS_BIGENDIAN |
431 | "MSBF", | |
432 | #else | |
433 | "LSBF", | |
434 | #endif | |
ba7dd8bb | 435 | ch->index + 1); |
13f2b9d7 | 436 | break; |
3f239f08 | 437 | case SR_CHANNEL_LOGIC: |
13f2b9d7 DJ |
438 | g_snprintf(command, sizeof(command), |
439 | (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA], | |
ba7dd8bb | 440 | ch->index < 8 ? 1 : 2); |
13f2b9d7 DJ |
441 | break; |
442 | default: | |
ba7dd8bb | 443 | sr_err("Invalid channel type."); |
13f2b9d7 DJ |
444 | break; |
445 | } | |
446 | ||
447 | return sr_scpi_send(sdi->conn, command); | |
448 | } | |
449 | ||
ba7dd8bb | 450 | static int hmo_check_channels(GSList *channels) |
13f2b9d7 DJ |
451 | { |
452 | GSList *l; | |
ba7dd8bb | 453 | struct sr_channel *ch; |
4889acef GS |
454 | gboolean enabled_chan[MAX_ANALOG_CHANNEL_COUNT]; |
455 | gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT]; | |
456 | size_t idx; | |
457 | ||
458 | /* Preset "not enabled" for all channels / pods. */ | |
459 | for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++) | |
460 | enabled_chan[idx] = FALSE; | |
461 | for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++) | |
462 | enabled_pod[idx] = FALSE; | |
463 | ||
464 | /* | |
465 | * Determine which channels / pods are required for the caller's | |
466 | * specified configuration. | |
467 | */ | |
ba7dd8bb UH |
468 | for (l = channels; l; l = l->next) { |
469 | ch = l->data; | |
470 | switch (ch->type) { | |
3f239f08 | 471 | case SR_CHANNEL_ANALOG: |
4889acef GS |
472 | idx = ch->index; |
473 | if (idx < ARRAY_SIZE(enabled_chan)) | |
474 | enabled_chan[idx] = TRUE; | |
13f2b9d7 | 475 | break; |
3f239f08 | 476 | case SR_CHANNEL_LOGIC: |
4889acef GS |
477 | idx = ch->index / 8; |
478 | if (idx < ARRAY_SIZE(enabled_pod)) | |
479 | enabled_pod[idx] = TRUE; | |
13f2b9d7 | 480 | break; |
13f2b9d7 DJ |
481 | default: |
482 | return SR_ERR; | |
483 | } | |
484 | } | |
485 | ||
4889acef GS |
486 | /* |
487 | * Check for resource conflicts. Some channels can be either | |
488 | * analog or digital, but never both at the same time. | |
489 | * | |
490 | * Note that the constraints might depend on the specific model. | |
491 | * These tests might need some adjustment when support for more | |
492 | * models gets added to the driver. | |
493 | */ | |
494 | if (enabled_pod[0] && enabled_chan[2]) | |
495 | return SR_ERR; | |
496 | if (enabled_pod[1] && enabled_chan[3]) | |
13f2b9d7 | 497 | return SR_ERR; |
13f2b9d7 DJ |
498 | return SR_OK; |
499 | } | |
500 | ||
ba7dd8bb | 501 | static int hmo_setup_channels(const struct sr_dev_inst *sdi) |
13f2b9d7 DJ |
502 | { |
503 | GSList *l; | |
504 | unsigned int i; | |
23e1ea7a | 505 | gboolean *pod_enabled, setup_changed; |
13f2b9d7 | 506 | char command[MAX_COMMAND_SIZE]; |
13f2b9d7 | 507 | struct scope_state *state; |
329733d9 | 508 | const struct scope_config *model; |
ba7dd8bb | 509 | struct sr_channel *ch; |
13f2b9d7 | 510 | struct dev_context *devc; |
23f43dff | 511 | struct sr_scpi_dev_inst *scpi; |
13f2b9d7 DJ |
512 | |
513 | devc = sdi->priv; | |
23f43dff | 514 | scpi = sdi->conn; |
13f2b9d7 DJ |
515 | state = devc->model_state; |
516 | model = devc->model_config; | |
23e1ea7a | 517 | setup_changed = FALSE; |
13f2b9d7 DJ |
518 | |
519 | pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods); | |
520 | ||
ba7dd8bb UH |
521 | for (l = sdi->channels; l; l = l->next) { |
522 | ch = l->data; | |
523 | switch (ch->type) { | |
3f239f08 | 524 | case SR_CHANNEL_ANALOG: |
ba7dd8bb | 525 | if (ch->enabled == state->analog_channels[ch->index].state) |
082972e8 UH |
526 | break; |
527 | g_snprintf(command, sizeof(command), | |
528 | (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE], | |
ba7dd8bb | 529 | ch->index + 1, ch->enabled); |
13f2b9d7 | 530 | |
23f43dff | 531 | if (sr_scpi_send(scpi, command) != SR_OK) |
082972e8 | 532 | return SR_ERR; |
ba7dd8bb | 533 | state->analog_channels[ch->index].state = ch->enabled; |
23e1ea7a | 534 | setup_changed = TRUE; |
89280b1a | 535 | break; |
3f239f08 | 536 | case SR_CHANNEL_LOGIC: |
13f2b9d7 DJ |
537 | /* |
538 | * A digital POD needs to be enabled for every group of | |
ba7dd8bb | 539 | * 8 channels. |
13f2b9d7 | 540 | */ |
ba7dd8bb UH |
541 | if (ch->enabled) |
542 | pod_enabled[ch->index < 8 ? 0 : 1] = TRUE; | |
13f2b9d7 | 543 | |
ba7dd8bb | 544 | if (ch->enabled == state->digital_channels[ch->index]) |
082972e8 UH |
545 | break; |
546 | g_snprintf(command, sizeof(command), | |
547 | (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE], | |
ba7dd8bb | 548 | ch->index, ch->enabled); |
13f2b9d7 | 549 | |
23f43dff | 550 | if (sr_scpi_send(scpi, command) != SR_OK) |
082972e8 | 551 | return SR_ERR; |
13f2b9d7 | 552 | |
ba7dd8bb | 553 | state->digital_channels[ch->index] = ch->enabled; |
23e1ea7a | 554 | setup_changed = TRUE; |
89280b1a | 555 | break; |
13f2b9d7 | 556 | default: |
b0e80e9a | 557 | g_free(pod_enabled); |
13f2b9d7 DJ |
558 | return SR_ERR; |
559 | } | |
560 | } | |
561 | ||
d1ac53cc UH |
562 | for (i = 0; i < model->digital_pods; i++) { |
563 | if (state->digital_pods[i] == pod_enabled[i]) | |
082972e8 UH |
564 | continue; |
565 | g_snprintf(command, sizeof(command), | |
566 | (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE], | |
d1ac53cc | 567 | i + 1, pod_enabled[i]); |
23f43dff | 568 | if (sr_scpi_send(scpi, command) != SR_OK) |
082972e8 | 569 | return SR_ERR; |
d1ac53cc | 570 | state->digital_pods[i] = pod_enabled[i]; |
23e1ea7a | 571 | setup_changed = TRUE; |
13f2b9d7 DJ |
572 | } |
573 | ||
574 | g_free(pod_enabled); | |
575 | ||
23e1ea7a DJ |
576 | if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK) |
577 | return SR_ERR; | |
578 | ||
13f2b9d7 DJ |
579 | return SR_OK; |
580 | } | |
581 | ||
695dc859 | 582 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
13f2b9d7 DJ |
583 | { |
584 | GSList *l; | |
1b0f62df | 585 | gboolean digital_added[MAX_DIGITAL_GROUP_COUNT]; |
e06875b2 | 586 | size_t group, pod_count; |
ba7dd8bb | 587 | struct sr_channel *ch; |
13f2b9d7 | 588 | struct dev_context *devc; |
23f43dff | 589 | struct sr_scpi_dev_inst *scpi; |
40a75c8e | 590 | int ret; |
06a3e78a | 591 | |
23f43dff | 592 | scpi = sdi->conn; |
13f2b9d7 | 593 | devc = sdi->priv; |
13f2b9d7 | 594 | |
1b0f62df GS |
595 | /* Preset empty results. */ |
596 | for (group = 0; group < ARRAY_SIZE(digital_added); group++) | |
597 | digital_added[group] = FALSE; | |
db81fbb5 SA |
598 | g_slist_free(devc->enabled_channels); |
599 | devc->enabled_channels = NULL; | |
600 | ||
1b0f62df | 601 | /* |
e06875b2 GS |
602 | * Contruct the list of enabled channels. Determine the highest |
603 | * number of digital pods involved in the acquisition. | |
1b0f62df | 604 | */ |
e06875b2 | 605 | pod_count = 0; |
ba7dd8bb UH |
606 | for (l = sdi->channels; l; l = l->next) { |
607 | ch = l->data; | |
608 | if (!ch->enabled) | |
082972e8 | 609 | continue; |
1b0f62df GS |
610 | /* Only add a single digital channel per group (pod). */ |
611 | group = ch->index / 8; | |
612 | if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) { | |
ba7dd8bb UH |
613 | devc->enabled_channels = g_slist_append( |
614 | devc->enabled_channels, ch); | |
e06875b2 | 615 | if (ch->type == SR_CHANNEL_LOGIC) { |
1b0f62df | 616 | digital_added[group] = TRUE; |
e06875b2 GS |
617 | if (pod_count < group + 1) |
618 | pod_count = group + 1; | |
619 | } | |
13f2b9d7 DJ |
620 | } |
621 | } | |
ba7dd8bb | 622 | if (!devc->enabled_channels) |
13f2b9d7 | 623 | return SR_ERR; |
e06875b2 GS |
624 | devc->pod_count = pod_count; |
625 | devc->logic_data = NULL; | |
13f2b9d7 | 626 | |
1b0f62df GS |
627 | /* |
628 | * Check constraints. Some channels can be either analog or | |
629 | * digital, but not both at the same time. | |
630 | */ | |
ba7dd8bb UH |
631 | if (hmo_check_channels(devc->enabled_channels) != SR_OK) { |
632 | sr_err("Invalid channel configuration specified!"); | |
40a75c8e GS |
633 | ret = SR_ERR_NA; |
634 | goto free_enabled; | |
13f2b9d7 DJ |
635 | } |
636 | ||
1b0f62df GS |
637 | /* |
638 | * Configure the analog and digital channels and the | |
639 | * corresponding digital pods. | |
640 | */ | |
ba7dd8bb UH |
641 | if (hmo_setup_channels(sdi) != SR_OK) { |
642 | sr_err("Failed to setup channel configuration!"); | |
40a75c8e GS |
643 | ret = SR_ERR; |
644 | goto free_enabled; | |
13f2b9d7 DJ |
645 | } |
646 | ||
1b0f62df GS |
647 | /* |
648 | * Start acquisition on the first enabled channel. The | |
649 | * receive routine will continue driving the acquisition. | |
650 | */ | |
102f1239 BV |
651 | sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50, |
652 | hmo_receive_data, (void *)sdi); | |
13f2b9d7 | 653 | |
bee2b016 | 654 | std_session_send_df_header(sdi); |
13f2b9d7 | 655 | |
ba7dd8bb | 656 | devc->current_channel = devc->enabled_channels; |
13f2b9d7 DJ |
657 | |
658 | return hmo_request_data(sdi); | |
40a75c8e GS |
659 | |
660 | free_enabled: | |
661 | g_slist_free(devc->enabled_channels); | |
662 | devc->enabled_channels = NULL; | |
663 | return ret; | |
06a3e78a DJ |
664 | } |
665 | ||
695dc859 | 666 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
06a3e78a | 667 | { |
13f2b9d7 | 668 | struct dev_context *devc; |
23f43dff | 669 | struct sr_scpi_dev_inst *scpi; |
13f2b9d7 | 670 | |
bee2b016 | 671 | std_session_send_df_end(sdi); |
66e3219d | 672 | |
13f2b9d7 DJ |
673 | devc = sdi->priv; |
674 | ||
ef1a346b | 675 | devc->num_frames = 0; |
ba7dd8bb UH |
676 | g_slist_free(devc->enabled_channels); |
677 | devc->enabled_channels = NULL; | |
23f43dff | 678 | scpi = sdi->conn; |
102f1239 | 679 | sr_scpi_source_remove(sdi->session, scpi); |
06a3e78a DJ |
680 | |
681 | return SR_OK; | |
682 | } | |
683 | ||
dd5c48a6 | 684 | static struct sr_dev_driver hameg_hmo_driver_info = { |
06a3e78a | 685 | .name = "hameg-hmo", |
89280b1a | 686 | .longname = "Hameg HMO", |
06a3e78a | 687 | .api_version = 1, |
c2fdcc25 | 688 | .init = std_init, |
700d6b64 | 689 | .cleanup = std_cleanup, |
06a3e78a | 690 | .scan = scan, |
c01bf34c | 691 | .dev_list = std_dev_list, |
06a3e78a DJ |
692 | .dev_clear = dev_clear, |
693 | .config_get = config_get, | |
694 | .config_set = config_set, | |
695 | .config_list = config_list, | |
696 | .dev_open = dev_open, | |
697 | .dev_close = dev_close, | |
698 | .dev_acquisition_start = dev_acquisition_start, | |
699 | .dev_acquisition_stop = dev_acquisition_stop, | |
41812aca | 700 | .context = NULL, |
06a3e78a | 701 | }; |
dd5c48a6 | 702 | SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info); |