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