]>
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 | ||
13f2b9d7 | 20 | #include <stdlib.h> |
06a3e78a DJ |
21 | #include "protocol.h" |
22 | ||
13f2b9d7 DJ |
23 | #define SERIALCOMM "115200/8n1/flow=1" |
24 | ||
e9a62139 DJ |
25 | SR_PRIV struct sr_dev_driver hameg_hmo_driver_info; |
26 | static struct sr_dev_driver *di = &hameg_hmo_driver_info; | |
27 | ||
28 | static const char *manufacturers[] = { | |
29 | "HAMEG", | |
30 | }; | |
31 | ||
13f2b9d7 DJ |
32 | static const int32_t hwopts[] = { |
33 | SR_CONF_CONN, | |
34 | SR_CONF_SERIALCOMM, | |
35 | }; | |
36 | ||
719eff68 UH |
37 | enum { |
38 | PG_INVALID = -1, | |
39 | PG_NONE, | |
40 | PG_ANALOG, | |
41 | PG_DIGITAL, | |
42 | }; | |
43 | ||
06a3e78a DJ |
44 | static int init(struct sr_context *sr_ctx) |
45 | { | |
46 | return std_init(sr_ctx, di, LOG_PREFIX); | |
47 | } | |
48 | ||
e9a62139 DJ |
49 | static int check_manufacturer(const char *manufacturer) |
50 | { | |
51 | unsigned int i; | |
52 | ||
53 | for (i = 0; i < ARRAY_SIZE(manufacturers); ++i) | |
54 | if (!strcmp(manufacturer, manufacturers[i])) | |
55 | return SR_OK; | |
56 | ||
57 | return SR_ERR; | |
58 | } | |
59 | ||
ca28abd6 | 60 | static struct sr_dev_inst *hmo_probe_serial_device(struct sr_scpi_dev_inst *scpi) |
e9a62139 DJ |
61 | { |
62 | struct sr_dev_inst *sdi; | |
63 | struct dev_context *devc; | |
64 | struct sr_scpi_hw_info *hw_info; | |
e9a62139 DJ |
65 | |
66 | sdi = NULL; | |
67 | devc = NULL; | |
e9a62139 DJ |
68 | hw_info = NULL; |
69 | ||
e9a62139 DJ |
70 | if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) { |
71 | sr_info("Couldn't get IDN response."); | |
72 | goto fail; | |
73 | } | |
74 | ||
75 | if (check_manufacturer(hw_info->manufacturer) != SR_OK) | |
76 | goto fail; | |
77 | ||
78 | if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, | |
79 | hw_info->manufacturer, hw_info->model, | |
80 | hw_info->firmware_version))) { | |
81 | goto fail; | |
82 | } | |
83 | sr_scpi_hw_info_free(hw_info); | |
84 | hw_info = NULL; | |
85 | ||
86 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) | |
87 | goto fail; | |
88 | ||
89 | sdi->driver = di; | |
90 | sdi->priv = devc; | |
91 | sdi->inst_type = SR_INST_SCPI; | |
92 | sdi->conn = scpi; | |
93 | ||
94 | if (hmo_init_device(sdi) != SR_OK) | |
95 | goto fail; | |
96 | ||
68e3d070 DJ |
97 | sr_scpi_close(sdi->conn); |
98 | ||
99 | sdi->status = SR_ST_INACTIVE; | |
100 | ||
e9a62139 DJ |
101 | return sdi; |
102 | ||
103 | fail: | |
104 | if (hw_info) | |
105 | sr_scpi_hw_info_free(hw_info); | |
e9a62139 DJ |
106 | if (sdi) |
107 | sr_dev_inst_free(sdi); | |
108 | if (devc) | |
109 | g_free(devc); | |
110 | ||
111 | return NULL; | |
112 | } | |
113 | ||
06a3e78a DJ |
114 | static GSList *scan(GSList *options) |
115 | { | |
ca28abd6 | 116 | return sr_scpi_scan(di->priv, options, hmo_probe_serial_device); |
06a3e78a DJ |
117 | } |
118 | ||
119 | static GSList *dev_list(void) | |
120 | { | |
121 | return ((struct drv_context *)(di->priv))->instances; | |
122 | } | |
123 | ||
13f2b9d7 DJ |
124 | static void clear_helper(void *priv) |
125 | { | |
126 | unsigned int i; | |
13f2b9d7 DJ |
127 | struct dev_context *devc; |
128 | struct scope_config *model; | |
129 | ||
130 | devc = priv; | |
131 | model = devc->model_config; | |
132 | ||
719eff68 | 133 | hmo_scope_state_free(devc->model_state); |
13f2b9d7 | 134 | |
89280b1a | 135 | for (i = 0; i < model->analog_channels; ++i) |
13f2b9d7 | 136 | g_slist_free(devc->analog_groups[i].probes); |
13f2b9d7 DJ |
137 | |
138 | for (i = 0; i < model->digital_pods; ++i) { | |
139 | g_slist_free(devc->digital_groups[i].probes); | |
140 | g_free(devc->digital_groups[i].name); | |
141 | } | |
142 | ||
143 | g_free(devc->analog_groups); | |
144 | g_free(devc->digital_groups); | |
145 | ||
146 | g_free(devc); | |
147 | } | |
148 | ||
06a3e78a DJ |
149 | static int dev_clear(void) |
150 | { | |
13f2b9d7 | 151 | return std_dev_clear(di, clear_helper); |
06a3e78a DJ |
152 | } |
153 | ||
154 | static int dev_open(struct sr_dev_inst *sdi) | |
155 | { | |
23f43dff | 156 | if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK) |
13f2b9d7 | 157 | return SR_ERR; |
06a3e78a | 158 | |
719eff68 | 159 | if (hmo_scope_state_get(sdi) != SR_OK) |
13f2b9d7 | 160 | return SR_ERR; |
06a3e78a DJ |
161 | |
162 | sdi->status = SR_ST_ACTIVE; | |
163 | ||
164 | return SR_OK; | |
165 | } | |
166 | ||
167 | static int dev_close(struct sr_dev_inst *sdi) | |
168 | { | |
13f2b9d7 DJ |
169 | if (sdi->status == SR_ST_INACTIVE) |
170 | return SR_OK; | |
06a3e78a | 171 | |
23f43dff | 172 | sr_scpi_close(sdi->conn); |
06a3e78a DJ |
173 | |
174 | sdi->status = SR_ST_INACTIVE; | |
175 | ||
176 | return SR_OK; | |
177 | } | |
178 | ||
179 | static int cleanup(void) | |
180 | { | |
181 | dev_clear(); | |
182 | ||
06a3e78a DJ |
183 | return SR_OK; |
184 | } | |
185 | ||
13f2b9d7 DJ |
186 | static int check_probe_group(struct dev_context *devc, |
187 | const struct sr_probe_group *probe_group) | |
188 | { | |
189 | unsigned int i; | |
190 | struct scope_config *model; | |
191 | ||
192 | model = devc->model_config; | |
193 | ||
194 | if (!probe_group) | |
195 | return PG_NONE; | |
196 | ||
197 | for (i = 0; i < model->analog_channels; ++i) | |
198 | if (probe_group == &devc->analog_groups[i]) | |
199 | return PG_ANALOG; | |
200 | ||
201 | for (i = 0; i < model->digital_pods; ++i) | |
202 | if (probe_group == &devc->digital_groups[i]) | |
203 | return PG_DIGITAL; | |
204 | ||
205 | sr_err("Invalid probe group specified."); | |
89280b1a | 206 | |
13f2b9d7 DJ |
207 | return PG_INVALID; |
208 | } | |
209 | ||
210 | static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, | |
211 | const struct sr_probe_group *probe_group) | |
06a3e78a | 212 | { |
89280b1a | 213 | int ret, pg_type; |
13f2b9d7 | 214 | unsigned int i; |
13f2b9d7 DJ |
215 | struct dev_context *devc; |
216 | struct scope_config *model; | |
14a2f74d | 217 | struct scope_state *state; |
13f2b9d7 DJ |
218 | |
219 | if (!sdi || !(devc = sdi->priv)) | |
220 | return SR_ERR_ARG; | |
221 | ||
222 | if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID) | |
223 | return SR_ERR; | |
06a3e78a | 224 | |
13f2b9d7 DJ |
225 | ret = SR_ERR_NA; |
226 | model = devc->model_config; | |
14a2f74d | 227 | state = devc->model_state; |
06a3e78a | 228 | |
06a3e78a | 229 | switch (key) { |
13f2b9d7 DJ |
230 | case SR_CONF_NUM_TIMEBASE: |
231 | *data = g_variant_new_int32(model->num_xdivs); | |
232 | ret = SR_OK; | |
233 | break; | |
13f2b9d7 DJ |
234 | case SR_CONF_NUM_VDIV: |
235 | if (pg_type == PG_NONE) { | |
236 | sr_err("No probe group specified."); | |
237 | return SR_ERR_PROBE_GROUP; | |
13f2b9d7 DJ |
238 | } else if (pg_type == PG_ANALOG) { |
239 | for (i = 0; i < model->analog_channels; ++i) { | |
082972e8 UH |
240 | if (probe_group != &devc->analog_groups[i]) |
241 | continue; | |
242 | *data = g_variant_new_int32(model->num_ydivs); | |
243 | ret = SR_OK; | |
244 | break; | |
13f2b9d7 DJ |
245 | } |
246 | ||
ccf14618 DJ |
247 | } else { |
248 | ret = SR_ERR_NA; | |
249 | } | |
250 | break; | |
251 | case SR_CONF_TRIGGER_SOURCE: | |
252 | *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]); | |
253 | ret = SR_OK; | |
254 | break; | |
255 | case SR_CONF_COUPLING: | |
256 | if (pg_type == PG_NONE) { | |
257 | sr_err("No probe group specified."); | |
258 | return SR_ERR_PROBE_GROUP; | |
259 | } else if (pg_type == PG_ANALOG) { | |
260 | for (i = 0; i < model->analog_channels; ++i) { | |
261 | if (probe_group != &devc->analog_groups[i]) | |
262 | continue; | |
263 | *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]); | |
264 | ret = SR_OK; | |
265 | break; | |
266 | } | |
267 | ||
13f2b9d7 DJ |
268 | } else { |
269 | ret = SR_ERR_NA; | |
270 | } | |
271 | break; | |
14a2f74d DJ |
272 | case SR_CONF_SAMPLERATE: |
273 | *data = g_variant_new_uint64(state->sample_rate); | |
274 | ret = SR_OK; | |
275 | break; | |
06a3e78a | 276 | default: |
13f2b9d7 | 277 | ret = SR_ERR_NA; |
06a3e78a DJ |
278 | } |
279 | ||
280 | return ret; | |
281 | } | |
282 | ||
13f2b9d7 DJ |
283 | static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n) |
284 | { | |
285 | unsigned int i; | |
13f2b9d7 DJ |
286 | GVariant *rational[2]; |
287 | GVariantBuilder gvb; | |
288 | ||
289 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
290 | ||
291 | for (i = 0; i < n; i++) { | |
292 | rational[0] = g_variant_new_uint64((*array)[i][0]); | |
293 | rational[1] = g_variant_new_uint64((*array)[i][1]); | |
294 | ||
89280b1a | 295 | /* FIXME: Valgrind reports a memory leak here. */ |
13f2b9d7 DJ |
296 | g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2)); |
297 | } | |
298 | ||
299 | return g_variant_builder_end(&gvb); | |
300 | } | |
301 | ||
302 | static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi, | |
303 | const struct sr_probe_group *probe_group) | |
06a3e78a | 304 | { |
89280b1a UH |
305 | int ret, pg_type; |
306 | unsigned int i, j; | |
965b463d | 307 | char command[MAX_COMMAND_SIZE], float_str[30]; |
13f2b9d7 DJ |
308 | struct dev_context *devc; |
309 | struct scope_config *model; | |
310 | struct scope_state *state; | |
89280b1a | 311 | const char *tmp; |
ca9b9f48 | 312 | uint64_t p, q; |
89280b1a | 313 | double tmp_d; |
23e1ea7a | 314 | gboolean update_sample_rate; |
06a3e78a | 315 | |
13f2b9d7 DJ |
316 | if (!sdi || !(devc = sdi->priv)) |
317 | return SR_ERR_ARG; | |
318 | ||
319 | if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID) | |
320 | return SR_ERR; | |
321 | ||
322 | model = devc->model_config; | |
323 | state = devc->model_state; | |
23e1ea7a | 324 | update_sample_rate = FALSE; |
13f2b9d7 DJ |
325 | |
326 | ret = SR_ERR_NA; | |
06a3e78a | 327 | |
06a3e78a | 328 | switch (key) { |
13f2b9d7 DJ |
329 | case SR_CONF_LIMIT_FRAMES: |
330 | devc->frame_limit = g_variant_get_uint64(data); | |
331 | ret = SR_OK; | |
332 | break; | |
13f2b9d7 | 333 | case SR_CONF_TRIGGER_SOURCE: |
13f2b9d7 | 334 | tmp = g_variant_get_string(data, NULL); |
13f2b9d7 | 335 | for (i = 0; (*model->trigger_sources)[i]; i++) { |
082972e8 UH |
336 | if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0) |
337 | continue; | |
338 | state->trigger_source = i; | |
339 | g_snprintf(command, sizeof(command), | |
340 | (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE], | |
341 | (*model->trigger_sources)[i]); | |
13f2b9d7 | 342 | |
082972e8 UH |
343 | ret = sr_scpi_send(sdi->conn, command); |
344 | break; | |
13f2b9d7 | 345 | } |
89280b1a | 346 | break; |
13f2b9d7 | 347 | case SR_CONF_VDIV: |
13f2b9d7 DJ |
348 | if (pg_type == PG_NONE) { |
349 | sr_err("No probe group specified."); | |
350 | return SR_ERR_PROBE_GROUP; | |
351 | } | |
352 | ||
353 | g_variant_get(data, "(tt)", &p, &q); | |
354 | ||
355 | for (i = 0; i < model->num_vdivs; i++) { | |
082972e8 UH |
356 | if (p != (*model->vdivs)[i][0] || |
357 | q != (*model->vdivs)[i][1]) | |
358 | continue; | |
359 | for (j = 1; j <= model->analog_channels; ++j) { | |
360 | if (probe_group != &devc->analog_groups[j - 1]) | |
361 | continue; | |
8de2dc3b | 362 | state->analog_channels[j - 1].vdiv = i; |
965b463d | 363 | g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q); |
082972e8 UH |
364 | g_snprintf(command, sizeof(command), |
365 | (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV], | |
965b463d | 366 | j, float_str); |
082972e8 UH |
367 | |
368 | if (sr_scpi_send(sdi->conn, command) != SR_OK || | |
369 | sr_scpi_get_opc(sdi->conn) != SR_OK) | |
370 | return SR_ERR; | |
13f2b9d7 | 371 | |
13f2b9d7 DJ |
372 | break; |
373 | } | |
082972e8 UH |
374 | |
375 | ret = SR_OK; | |
376 | break; | |
13f2b9d7 | 377 | } |
89280b1a | 378 | break; |
13f2b9d7 | 379 | case SR_CONF_TIMEBASE: |
13f2b9d7 DJ |
380 | g_variant_get(data, "(tt)", &p, &q); |
381 | ||
382 | for (i = 0; i < model->num_timebases; i++) { | |
082972e8 UH |
383 | if (p != (*model->timebases)[i][0] || |
384 | q != (*model->timebases)[i][1]) | |
385 | continue; | |
8de2dc3b | 386 | state->timebase = i; |
965b463d | 387 | g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q); |
082972e8 UH |
388 | g_snprintf(command, sizeof(command), |
389 | (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE], | |
965b463d | 390 | float_str); |
13f2b9d7 | 391 | |
082972e8 | 392 | ret = sr_scpi_send(sdi->conn, command); |
23e1ea7a | 393 | update_sample_rate = TRUE; |
082972e8 | 394 | break; |
13f2b9d7 | 395 | } |
89280b1a | 396 | break; |
13f2b9d7 | 397 | case SR_CONF_HORIZ_TRIGGERPOS: |
89280b1a | 398 | tmp_d = g_variant_get_double(data); |
13f2b9d7 | 399 | |
89280b1a | 400 | if (tmp_d < 0.0 || tmp_d > 1.0) |
13f2b9d7 DJ |
401 | return SR_ERR; |
402 | ||
422a1c0d DJ |
403 | state->horiz_triggerpos = tmp_d; |
404 | tmp_d = -(tmp_d - 0.5) * | |
405 | ((double) (*model->timebases)[state->timebase][0] / | |
406 | (*model->timebases)[state->timebase][1]) | |
407 | * model->num_xdivs; | |
408 | ||
409 | g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d); | |
13f2b9d7 DJ |
410 | g_snprintf(command, sizeof(command), |
411 | (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS], | |
422a1c0d | 412 | float_str); |
13f2b9d7 DJ |
413 | |
414 | ret = sr_scpi_send(sdi->conn, command); | |
89280b1a | 415 | break; |
13f2b9d7 | 416 | case SR_CONF_TRIGGER_SLOPE: |
ca9b9f48 | 417 | tmp = g_variant_get_string(data, NULL); |
13f2b9d7 | 418 | |
ca9b9f48 DE |
419 | if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r')) |
420 | return SR_ERR_ARG; | |
13f2b9d7 | 421 | |
ca9b9f48 | 422 | state->trigger_slope = (tmp[0] == 'r') ? 0 : 1; |
13f2b9d7 DJ |
423 | |
424 | g_snprintf(command, sizeof(command), | |
425 | (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE], | |
ca9b9f48 | 426 | (state->trigger_slope == 0) ? "POS" : "NEG"); |
13f2b9d7 DJ |
427 | |
428 | ret = sr_scpi_send(sdi->conn, command); | |
89280b1a | 429 | break; |
13f2b9d7 | 430 | case SR_CONF_COUPLING: |
13f2b9d7 DJ |
431 | if (pg_type == PG_NONE) { |
432 | sr_err("No probe group specified."); | |
433 | return SR_ERR_PROBE_GROUP; | |
434 | } | |
435 | ||
436 | tmp = g_variant_get_string(data, NULL); | |
437 | ||
438 | for (i = 0; (*model->coupling_options)[i]; i++) { | |
082972e8 UH |
439 | if (strcmp(tmp, (*model->coupling_options)[i]) != 0) |
440 | continue; | |
441 | for (j = 1; j <= model->analog_channels; ++j) { | |
442 | if (probe_group != &devc->analog_groups[j - 1]) | |
443 | continue; | |
444 | state->analog_channels[j-1].coupling = i; | |
13f2b9d7 | 445 | |
082972e8 UH |
446 | g_snprintf(command, sizeof(command), |
447 | (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING], | |
448 | j, tmp); | |
449 | ||
450 | if (sr_scpi_send(sdi->conn, command) != SR_OK || | |
451 | sr_scpi_get_opc(sdi->conn) != SR_OK) | |
452 | return SR_ERR; | |
13f2b9d7 DJ |
453 | break; |
454 | } | |
082972e8 UH |
455 | |
456 | ret = SR_OK; | |
457 | break; | |
13f2b9d7 | 458 | } |
89280b1a | 459 | break; |
06a3e78a DJ |
460 | default: |
461 | ret = SR_ERR_NA; | |
13f2b9d7 | 462 | break; |
06a3e78a DJ |
463 | } |
464 | ||
13f2b9d7 DJ |
465 | if (ret == SR_OK) |
466 | ret = sr_scpi_get_opc(sdi->conn); | |
467 | ||
23e1ea7a DJ |
468 | if (ret == SR_OK && update_sample_rate) |
469 | ret = hmo_update_sample_rate(sdi); | |
470 | ||
06a3e78a DJ |
471 | return ret; |
472 | } | |
473 | ||
13f2b9d7 | 474 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, |
89280b1a | 475 | const struct sr_probe_group *probe_group) |
06a3e78a | 476 | { |
13f2b9d7 | 477 | int pg_type; |
13f2b9d7 DJ |
478 | struct dev_context *devc; |
479 | struct scope_config *model; | |
480 | ||
481 | if (!sdi || !(devc = sdi->priv)) | |
482 | return SR_ERR_ARG; | |
06a3e78a | 483 | |
13f2b9d7 DJ |
484 | if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID) |
485 | return SR_ERR; | |
486 | ||
487 | model = devc->model_config; | |
06a3e78a | 488 | |
06a3e78a | 489 | switch (key) { |
13f2b9d7 DJ |
490 | case SR_CONF_DEVICE_OPTIONS: |
491 | if (pg_type == PG_NONE) { | |
492 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
89280b1a UH |
493 | model->hw_caps, model->num_hwcaps, |
494 | sizeof(int32_t)); | |
13f2b9d7 DJ |
495 | } else if (pg_type == PG_ANALOG) { |
496 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
89280b1a UH |
497 | model->analog_hwcaps, model->num_analog_hwcaps, |
498 | sizeof(int32_t)); | |
13f2b9d7 DJ |
499 | } else { |
500 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
89280b1a | 501 | NULL, 0, sizeof(int32_t)); |
13f2b9d7 DJ |
502 | } |
503 | break; | |
13f2b9d7 DJ |
504 | case SR_CONF_COUPLING: |
505 | if (pg_type == PG_NONE) | |
506 | return SR_ERR_PROBE_GROUP; | |
13f2b9d7 | 507 | *data = g_variant_new_strv(*model->coupling_options, |
89280b1a | 508 | g_strv_length((char **)*model->coupling_options)); |
13f2b9d7 | 509 | break; |
13f2b9d7 DJ |
510 | case SR_CONF_TRIGGER_SOURCE: |
511 | *data = g_variant_new_strv(*model->trigger_sources, | |
89280b1a | 512 | g_strv_length((char **)*model->trigger_sources)); |
13f2b9d7 | 513 | break; |
13f2b9d7 DJ |
514 | case SR_CONF_TIMEBASE: |
515 | *data = build_tuples(model->timebases, model->num_timebases); | |
516 | break; | |
13f2b9d7 DJ |
517 | case SR_CONF_VDIV: |
518 | if (pg_type == PG_NONE) | |
519 | return SR_ERR_PROBE_GROUP; | |
13f2b9d7 DJ |
520 | *data = build_tuples(model->vdivs, model->num_vdivs); |
521 | break; | |
06a3e78a DJ |
522 | default: |
523 | return SR_ERR_NA; | |
524 | } | |
525 | ||
13f2b9d7 | 526 | return SR_OK; |
06a3e78a DJ |
527 | } |
528 | ||
13f2b9d7 | 529 | SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi) |
06a3e78a | 530 | { |
13f2b9d7 | 531 | char command[MAX_COMMAND_SIZE]; |
13f2b9d7 DJ |
532 | struct sr_probe *probe; |
533 | struct dev_context *devc; | |
534 | struct scope_config *model; | |
535 | ||
536 | devc = sdi->priv; | |
537 | model = devc->model_config; | |
538 | ||
539 | probe = devc->current_probe->data; | |
540 | ||
541 | switch (probe->type) { | |
542 | case SR_PROBE_ANALOG: | |
543 | g_snprintf(command, sizeof(command), | |
544 | (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA], | |
545 | probe->index + 1); | |
546 | break; | |
547 | case SR_PROBE_LOGIC: | |
548 | g_snprintf(command, sizeof(command), | |
549 | (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA], | |
550 | probe->index < 8 ? 1 : 2); | |
551 | break; | |
552 | default: | |
89280b1a | 553 | sr_err("Invalid probe type."); |
13f2b9d7 DJ |
554 | break; |
555 | } | |
556 | ||
557 | return sr_scpi_send(sdi->conn, command); | |
558 | } | |
559 | ||
560 | static int hmo_check_probes(GSList *probes) | |
561 | { | |
562 | GSList *l; | |
13f2b9d7 | 563 | struct sr_probe *probe; |
89280b1a | 564 | gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4; |
13f2b9d7 | 565 | |
89280b1a | 566 | enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE; |
13f2b9d7 DJ |
567 | |
568 | for (l = probes; l; l = l->next) { | |
569 | probe = l->data; | |
13f2b9d7 DJ |
570 | switch (probe->type) { |
571 | case SR_PROBE_ANALOG: | |
572 | if (probe->index == 2) | |
573 | enabled_chan3 = TRUE; | |
574 | else if (probe->index == 3) | |
575 | enabled_chan4 = TRUE; | |
576 | break; | |
13f2b9d7 DJ |
577 | case SR_PROBE_LOGIC: |
578 | if (probe->index < 8) | |
579 | enabled_pod1 = TRUE; | |
580 | else | |
581 | enabled_pod2 = TRUE; | |
582 | break; | |
13f2b9d7 DJ |
583 | default: |
584 | return SR_ERR; | |
585 | } | |
586 | } | |
587 | ||
588 | if ((enabled_pod1 && enabled_chan3) || | |
589 | (enabled_pod2 && enabled_chan4)) | |
590 | return SR_ERR; | |
591 | ||
592 | return SR_OK; | |
593 | } | |
594 | ||
595 | static int hmo_setup_probes(const struct sr_dev_inst *sdi) | |
596 | { | |
597 | GSList *l; | |
598 | unsigned int i; | |
23e1ea7a | 599 | gboolean *pod_enabled, setup_changed; |
13f2b9d7 | 600 | char command[MAX_COMMAND_SIZE]; |
13f2b9d7 DJ |
601 | struct scope_state *state; |
602 | struct scope_config *model; | |
13f2b9d7 DJ |
603 | struct sr_probe *probe; |
604 | struct dev_context *devc; | |
23f43dff | 605 | struct sr_scpi_dev_inst *scpi; |
13f2b9d7 DJ |
606 | |
607 | devc = sdi->priv; | |
23f43dff | 608 | scpi = sdi->conn; |
13f2b9d7 DJ |
609 | state = devc->model_state; |
610 | model = devc->model_config; | |
23e1ea7a | 611 | setup_changed = FALSE; |
13f2b9d7 DJ |
612 | |
613 | pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods); | |
614 | ||
615 | for (l = sdi->probes; l; l = l->next) { | |
616 | probe = l->data; | |
13f2b9d7 DJ |
617 | switch (probe->type) { |
618 | case SR_PROBE_ANALOG: | |
082972e8 UH |
619 | if (probe->enabled == state->analog_channels[probe->index].state) |
620 | break; | |
621 | g_snprintf(command, sizeof(command), | |
622 | (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE], | |
623 | probe->index + 1, probe->enabled); | |
13f2b9d7 | 624 | |
23f43dff | 625 | if (sr_scpi_send(scpi, command) != SR_OK) |
082972e8 UH |
626 | return SR_ERR; |
627 | state->analog_channels[probe->index].state = probe->enabled; | |
23e1ea7a | 628 | setup_changed = TRUE; |
89280b1a | 629 | break; |
13f2b9d7 | 630 | case SR_PROBE_LOGIC: |
13f2b9d7 DJ |
631 | /* |
632 | * A digital POD needs to be enabled for every group of | |
633 | * 8 probes. | |
634 | */ | |
635 | if (probe->enabled) | |
636 | pod_enabled[probe->index < 8 ? 0 : 1] = TRUE; | |
637 | ||
082972e8 UH |
638 | if (probe->enabled == state->digital_channels[probe->index]) |
639 | break; | |
640 | g_snprintf(command, sizeof(command), | |
641 | (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE], | |
642 | probe->index, probe->enabled); | |
13f2b9d7 | 643 | |
23f43dff | 644 | if (sr_scpi_send(scpi, command) != SR_OK) |
082972e8 | 645 | return SR_ERR; |
13f2b9d7 | 646 | |
082972e8 | 647 | state->digital_channels[probe->index] = probe->enabled; |
23e1ea7a | 648 | setup_changed = TRUE; |
89280b1a | 649 | break; |
13f2b9d7 DJ |
650 | default: |
651 | return SR_ERR; | |
652 | } | |
653 | } | |
654 | ||
655 | for (i = 1; i <= model->digital_pods; ++i) { | |
082972e8 UH |
656 | if (state->digital_pods[i - 1] == pod_enabled[i - 1]) |
657 | continue; | |
658 | g_snprintf(command, sizeof(command), | |
659 | (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE], | |
660 | i, pod_enabled[i - 1]); | |
23f43dff | 661 | if (sr_scpi_send(scpi, command) != SR_OK) |
082972e8 UH |
662 | return SR_ERR; |
663 | state->digital_pods[i - 1] = pod_enabled[i - 1]; | |
23e1ea7a | 664 | setup_changed = TRUE; |
13f2b9d7 DJ |
665 | } |
666 | ||
667 | g_free(pod_enabled); | |
668 | ||
23e1ea7a DJ |
669 | if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK) |
670 | return SR_ERR; | |
671 | ||
13f2b9d7 DJ |
672 | return SR_OK; |
673 | } | |
674 | ||
675 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
676 | { | |
677 | GSList *l; | |
678 | gboolean digital_added; | |
13f2b9d7 DJ |
679 | struct sr_probe *probe; |
680 | struct dev_context *devc; | |
23f43dff | 681 | struct sr_scpi_dev_inst *scpi; |
06a3e78a DJ |
682 | |
683 | if (sdi->status != SR_ST_ACTIVE) | |
684 | return SR_ERR_DEV_CLOSED; | |
685 | ||
23f43dff | 686 | scpi = sdi->conn; |
13f2b9d7 DJ |
687 | devc = sdi->priv; |
688 | digital_added = FALSE; | |
689 | ||
690 | for (l = sdi->probes; l; l = l->next) { | |
691 | probe = l->data; | |
082972e8 UH |
692 | if (!probe->enabled) |
693 | continue; | |
694 | /* Only add a single digital probe. */ | |
695 | if (probe->type != SR_PROBE_LOGIC || !digital_added) { | |
696 | devc->enabled_probes = g_slist_append( | |
697 | devc->enabled_probes, probe); | |
698 | if (probe->type == SR_PROBE_LOGIC) | |
699 | digital_added = TRUE; | |
13f2b9d7 DJ |
700 | } |
701 | } | |
06a3e78a | 702 | |
13f2b9d7 DJ |
703 | if (!devc->enabled_probes) |
704 | return SR_ERR; | |
705 | ||
706 | if (hmo_check_probes(devc->enabled_probes) != SR_OK) { | |
707 | sr_err("Invalid probe configuration specified!"); | |
708 | return SR_ERR_NA; | |
709 | } | |
710 | ||
711 | if (hmo_setup_probes(sdi) != SR_OK) { | |
712 | sr_err("Failed to setup probe configuration!"); | |
713 | return SR_ERR; | |
714 | } | |
715 | ||
23f43dff | 716 | sr_scpi_source_add(scpi, G_IO_IN, 50, hmo_receive_data, (void *)sdi); |
13f2b9d7 DJ |
717 | |
718 | /* Send header packet to the session bus. */ | |
719 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
720 | ||
721 | devc->current_probe = devc->enabled_probes; | |
722 | ||
723 | return hmo_request_data(sdi); | |
06a3e78a DJ |
724 | } |
725 | ||
726 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
727 | { | |
13f2b9d7 | 728 | struct dev_context *devc; |
23f43dff | 729 | struct sr_scpi_dev_inst *scpi; |
66e3219d | 730 | struct sr_datafeed_packet packet; |
13f2b9d7 | 731 | |
06a3e78a DJ |
732 | (void)cb_data; |
733 | ||
66e3219d DJ |
734 | packet.type = SR_DF_END; |
735 | packet.payload = NULL; | |
736 | sr_session_send(sdi, &packet); | |
737 | ||
06a3e78a DJ |
738 | if (sdi->status != SR_ST_ACTIVE) |
739 | return SR_ERR_DEV_CLOSED; | |
740 | ||
13f2b9d7 DJ |
741 | devc = sdi->priv; |
742 | ||
ef1a346b | 743 | devc->num_frames = 0; |
13f2b9d7 DJ |
744 | g_slist_free(devc->enabled_probes); |
745 | devc->enabled_probes = NULL; | |
23f43dff ML |
746 | scpi = sdi->conn; |
747 | sr_scpi_source_remove(scpi); | |
06a3e78a DJ |
748 | |
749 | return SR_OK; | |
750 | } | |
751 | ||
752 | SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = { | |
753 | .name = "hameg-hmo", | |
89280b1a | 754 | .longname = "Hameg HMO", |
06a3e78a DJ |
755 | .api_version = 1, |
756 | .init = init, | |
757 | .cleanup = cleanup, | |
758 | .scan = scan, | |
759 | .dev_list = dev_list, | |
760 | .dev_clear = dev_clear, | |
761 | .config_get = config_get, | |
762 | .config_set = config_set, | |
763 | .config_list = config_list, | |
764 | .dev_open = dev_open, | |
765 | .dev_close = dev_close, | |
766 | .dev_acquisition_start = dev_acquisition_start, | |
767 | .dev_acquisition_stop = dev_acquisition_stop, | |
768 | .priv = NULL, | |
769 | }; |