]>
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 | ||
226363c4 | 32 | static const int32_t scanopts[] = { |
13f2b9d7 DJ |
33 | SR_CONF_CONN, |
34 | SR_CONF_SERIALCOMM, | |
35 | }; | |
36 | ||
719eff68 | 37 | enum { |
61e77667 UH |
38 | CG_INVALID = -1, |
39 | CG_NONE, | |
40 | CG_ANALOG, | |
41 | CG_DIGITAL, | |
719eff68 UH |
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) |
a68bf88e | 136 | g_slist_free(devc->analog_groups[i].channels); |
13f2b9d7 DJ |
137 | |
138 | for (i = 0; i < model->digital_pods; ++i) { | |
a68bf88e | 139 | g_slist_free(devc->digital_groups[i].channels); |
13f2b9d7 DJ |
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 | ||
660e398f | 186 | static int check_channel_group(struct dev_context *devc, |
53b4680f | 187 | const struct sr_channel_group *cg) |
13f2b9d7 DJ |
188 | { |
189 | unsigned int i; | |
190 | struct scope_config *model; | |
191 | ||
192 | model = devc->model_config; | |
193 | ||
53b4680f | 194 | if (!cg) |
61e77667 | 195 | return CG_NONE; |
13f2b9d7 DJ |
196 | |
197 | for (i = 0; i < model->analog_channels; ++i) | |
53b4680f | 198 | if (cg == &devc->analog_groups[i]) |
61e77667 | 199 | return CG_ANALOG; |
13f2b9d7 DJ |
200 | |
201 | for (i = 0; i < model->digital_pods; ++i) | |
53b4680f | 202 | if (cg == &devc->digital_groups[i]) |
61e77667 | 203 | return CG_DIGITAL; |
13f2b9d7 | 204 | |
660e398f | 205 | sr_err("Invalid channel group specified."); |
89280b1a | 206 | |
61e77667 | 207 | return CG_INVALID; |
13f2b9d7 DJ |
208 | } |
209 | ||
210 | static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, | |
53b4680f | 211 | const struct sr_channel_group *cg) |
06a3e78a | 212 | { |
61e77667 | 213 | int ret, cg_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 | ||
61e77667 | 222 | if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID) |
13f2b9d7 | 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; | |
3fd2dca2 DJ |
234 | case SR_CONF_TIMEBASE: |
235 | *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0], | |
236 | (*model->timebases)[state->timebase][1]); | |
237 | ret = SR_OK; | |
238 | break; | |
13f2b9d7 | 239 | case SR_CONF_NUM_VDIV: |
61e77667 | 240 | if (cg_type == CG_NONE) { |
660e398f UH |
241 | sr_err("No channel group specified."); |
242 | return SR_ERR_CHANNEL_GROUP; | |
61e77667 | 243 | } else if (cg_type == CG_ANALOG) { |
13f2b9d7 | 244 | for (i = 0; i < model->analog_channels; ++i) { |
53b4680f | 245 | if (cg != &devc->analog_groups[i]) |
082972e8 UH |
246 | continue; |
247 | *data = g_variant_new_int32(model->num_ydivs); | |
248 | ret = SR_OK; | |
249 | break; | |
13f2b9d7 DJ |
250 | } |
251 | ||
3fd2dca2 DJ |
252 | } else { |
253 | ret = SR_ERR_NA; | |
254 | } | |
255 | break; | |
256 | case SR_CONF_VDIV: | |
61e77667 UH |
257 | if (cg_type == CG_NONE) { |
258 | sr_err("No channel group specified."); | |
259 | return SR_ERR_CHANNEL_GROUP; | |
260 | } else if (cg_type == CG_ANALOG) { | |
3fd2dca2 | 261 | for (i = 0; i < model->analog_channels; ++i) { |
61e77667 | 262 | if (cg != &devc->analog_groups[i]) |
3fd2dca2 DJ |
263 | continue; |
264 | *data = g_variant_new("(tt)", | |
265 | (*model->vdivs)[state->analog_channels[i].vdiv][0], | |
266 | (*model->vdivs)[state->analog_channels[i].vdiv][1]); | |
267 | ret = SR_OK; | |
268 | break; | |
269 | } | |
270 | ||
ccf14618 DJ |
271 | } else { |
272 | ret = SR_ERR_NA; | |
273 | } | |
274 | break; | |
275 | case SR_CONF_TRIGGER_SOURCE: | |
276 | *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]); | |
277 | ret = SR_OK; | |
278 | break; | |
3fd2dca2 DJ |
279 | case SR_CONF_TRIGGER_SLOPE: |
280 | *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]); | |
281 | ret = SR_OK; | |
282 | break; | |
283 | case SR_CONF_HORIZ_TRIGGERPOS: | |
284 | *data = g_variant_new_double(state->horiz_triggerpos); | |
285 | ret = SR_OK; | |
286 | break; | |
ccf14618 | 287 | case SR_CONF_COUPLING: |
61e77667 | 288 | if (cg_type == CG_NONE) { |
660e398f UH |
289 | sr_err("No channel group specified."); |
290 | return SR_ERR_CHANNEL_GROUP; | |
61e77667 | 291 | } else if (cg_type == CG_ANALOG) { |
ccf14618 | 292 | for (i = 0; i < model->analog_channels; ++i) { |
53b4680f | 293 | if (cg != &devc->analog_groups[i]) |
ccf14618 DJ |
294 | continue; |
295 | *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]); | |
296 | ret = SR_OK; | |
297 | break; | |
298 | } | |
299 | ||
13f2b9d7 DJ |
300 | } else { |
301 | ret = SR_ERR_NA; | |
302 | } | |
303 | break; | |
14a2f74d DJ |
304 | case SR_CONF_SAMPLERATE: |
305 | *data = g_variant_new_uint64(state->sample_rate); | |
306 | ret = SR_OK; | |
307 | break; | |
06a3e78a | 308 | default: |
13f2b9d7 | 309 | ret = SR_ERR_NA; |
06a3e78a DJ |
310 | } |
311 | ||
312 | return ret; | |
313 | } | |
314 | ||
13f2b9d7 DJ |
315 | static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n) |
316 | { | |
317 | unsigned int i; | |
13f2b9d7 DJ |
318 | GVariant *rational[2]; |
319 | GVariantBuilder gvb; | |
320 | ||
321 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
322 | ||
323 | for (i = 0; i < n; i++) { | |
324 | rational[0] = g_variant_new_uint64((*array)[i][0]); | |
325 | rational[1] = g_variant_new_uint64((*array)[i][1]); | |
326 | ||
89280b1a | 327 | /* FIXME: Valgrind reports a memory leak here. */ |
13f2b9d7 DJ |
328 | g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2)); |
329 | } | |
330 | ||
331 | return g_variant_builder_end(&gvb); | |
332 | } | |
333 | ||
334 | static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi, | |
53b4680f | 335 | const struct sr_channel_group *cg) |
06a3e78a | 336 | { |
61e77667 | 337 | int ret, cg_type; |
89280b1a | 338 | unsigned int i, j; |
965b463d | 339 | char command[MAX_COMMAND_SIZE], float_str[30]; |
13f2b9d7 DJ |
340 | struct dev_context *devc; |
341 | struct scope_config *model; | |
342 | struct scope_state *state; | |
89280b1a | 343 | const char *tmp; |
ca9b9f48 | 344 | uint64_t p, q; |
89280b1a | 345 | double tmp_d; |
23e1ea7a | 346 | gboolean update_sample_rate; |
06a3e78a | 347 | |
13f2b9d7 DJ |
348 | if (!sdi || !(devc = sdi->priv)) |
349 | return SR_ERR_ARG; | |
350 | ||
61e77667 | 351 | if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID) |
13f2b9d7 DJ |
352 | return SR_ERR; |
353 | ||
354 | model = devc->model_config; | |
355 | state = devc->model_state; | |
23e1ea7a | 356 | update_sample_rate = FALSE; |
13f2b9d7 DJ |
357 | |
358 | ret = SR_ERR_NA; | |
06a3e78a | 359 | |
06a3e78a | 360 | switch (key) { |
13f2b9d7 DJ |
361 | case SR_CONF_LIMIT_FRAMES: |
362 | devc->frame_limit = g_variant_get_uint64(data); | |
363 | ret = SR_OK; | |
364 | break; | |
13f2b9d7 | 365 | case SR_CONF_TRIGGER_SOURCE: |
13f2b9d7 | 366 | tmp = g_variant_get_string(data, NULL); |
13f2b9d7 | 367 | for (i = 0; (*model->trigger_sources)[i]; i++) { |
082972e8 UH |
368 | if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0) |
369 | continue; | |
370 | state->trigger_source = i; | |
371 | g_snprintf(command, sizeof(command), | |
372 | (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE], | |
373 | (*model->trigger_sources)[i]); | |
13f2b9d7 | 374 | |
082972e8 UH |
375 | ret = sr_scpi_send(sdi->conn, command); |
376 | break; | |
13f2b9d7 | 377 | } |
89280b1a | 378 | break; |
13f2b9d7 | 379 | case SR_CONF_VDIV: |
61e77667 | 380 | if (cg_type == CG_NONE) { |
660e398f UH |
381 | sr_err("No channel group specified."); |
382 | return SR_ERR_CHANNEL_GROUP; | |
13f2b9d7 DJ |
383 | } |
384 | ||
385 | g_variant_get(data, "(tt)", &p, &q); | |
386 | ||
387 | for (i = 0; i < model->num_vdivs; i++) { | |
082972e8 UH |
388 | if (p != (*model->vdivs)[i][0] || |
389 | q != (*model->vdivs)[i][1]) | |
390 | continue; | |
391 | for (j = 1; j <= model->analog_channels; ++j) { | |
53b4680f | 392 | if (cg != &devc->analog_groups[j - 1]) |
082972e8 | 393 | continue; |
8de2dc3b | 394 | state->analog_channels[j - 1].vdiv = i; |
965b463d | 395 | g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q); |
082972e8 UH |
396 | g_snprintf(command, sizeof(command), |
397 | (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV], | |
965b463d | 398 | j, float_str); |
082972e8 UH |
399 | |
400 | if (sr_scpi_send(sdi->conn, command) != SR_OK || | |
401 | sr_scpi_get_opc(sdi->conn) != SR_OK) | |
402 | return SR_ERR; | |
13f2b9d7 | 403 | |
13f2b9d7 DJ |
404 | break; |
405 | } | |
082972e8 UH |
406 | |
407 | ret = SR_OK; | |
408 | break; | |
13f2b9d7 | 409 | } |
89280b1a | 410 | break; |
13f2b9d7 | 411 | case SR_CONF_TIMEBASE: |
13f2b9d7 DJ |
412 | g_variant_get(data, "(tt)", &p, &q); |
413 | ||
414 | for (i = 0; i < model->num_timebases; i++) { | |
082972e8 UH |
415 | if (p != (*model->timebases)[i][0] || |
416 | q != (*model->timebases)[i][1]) | |
417 | continue; | |
8de2dc3b | 418 | state->timebase = i; |
965b463d | 419 | g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q); |
082972e8 UH |
420 | g_snprintf(command, sizeof(command), |
421 | (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE], | |
965b463d | 422 | float_str); |
13f2b9d7 | 423 | |
082972e8 | 424 | ret = sr_scpi_send(sdi->conn, command); |
23e1ea7a | 425 | update_sample_rate = TRUE; |
082972e8 | 426 | break; |
13f2b9d7 | 427 | } |
89280b1a | 428 | break; |
13f2b9d7 | 429 | case SR_CONF_HORIZ_TRIGGERPOS: |
89280b1a | 430 | tmp_d = g_variant_get_double(data); |
13f2b9d7 | 431 | |
89280b1a | 432 | if (tmp_d < 0.0 || tmp_d > 1.0) |
13f2b9d7 DJ |
433 | return SR_ERR; |
434 | ||
422a1c0d DJ |
435 | state->horiz_triggerpos = tmp_d; |
436 | tmp_d = -(tmp_d - 0.5) * | |
437 | ((double) (*model->timebases)[state->timebase][0] / | |
438 | (*model->timebases)[state->timebase][1]) | |
439 | * model->num_xdivs; | |
440 | ||
441 | g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d); | |
13f2b9d7 DJ |
442 | g_snprintf(command, sizeof(command), |
443 | (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS], | |
422a1c0d | 444 | float_str); |
13f2b9d7 DJ |
445 | |
446 | ret = sr_scpi_send(sdi->conn, command); | |
89280b1a | 447 | break; |
13f2b9d7 | 448 | case SR_CONF_TRIGGER_SLOPE: |
ca9b9f48 | 449 | tmp = g_variant_get_string(data, NULL); |
13f2b9d7 | 450 | |
ca9b9f48 DE |
451 | if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r')) |
452 | return SR_ERR_ARG; | |
13f2b9d7 | 453 | |
ca9b9f48 | 454 | state->trigger_slope = (tmp[0] == 'r') ? 0 : 1; |
13f2b9d7 DJ |
455 | |
456 | g_snprintf(command, sizeof(command), | |
457 | (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE], | |
ca9b9f48 | 458 | (state->trigger_slope == 0) ? "POS" : "NEG"); |
13f2b9d7 DJ |
459 | |
460 | ret = sr_scpi_send(sdi->conn, command); | |
89280b1a | 461 | break; |
13f2b9d7 | 462 | case SR_CONF_COUPLING: |
61e77667 | 463 | if (cg_type == CG_NONE) { |
660e398f UH |
464 | sr_err("No channel group specified."); |
465 | return SR_ERR_CHANNEL_GROUP; | |
13f2b9d7 DJ |
466 | } |
467 | ||
468 | tmp = g_variant_get_string(data, NULL); | |
469 | ||
470 | for (i = 0; (*model->coupling_options)[i]; i++) { | |
082972e8 UH |
471 | if (strcmp(tmp, (*model->coupling_options)[i]) != 0) |
472 | continue; | |
473 | for (j = 1; j <= model->analog_channels; ++j) { | |
53b4680f | 474 | if (cg != &devc->analog_groups[j - 1]) |
082972e8 UH |
475 | continue; |
476 | state->analog_channels[j-1].coupling = i; | |
13f2b9d7 | 477 | |
082972e8 UH |
478 | g_snprintf(command, sizeof(command), |
479 | (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING], | |
480 | j, tmp); | |
481 | ||
482 | if (sr_scpi_send(sdi->conn, command) != SR_OK || | |
483 | sr_scpi_get_opc(sdi->conn) != SR_OK) | |
484 | return SR_ERR; | |
13f2b9d7 DJ |
485 | break; |
486 | } | |
082972e8 UH |
487 | |
488 | ret = SR_OK; | |
489 | break; | |
13f2b9d7 | 490 | } |
89280b1a | 491 | break; |
06a3e78a DJ |
492 | default: |
493 | ret = SR_ERR_NA; | |
13f2b9d7 | 494 | break; |
06a3e78a DJ |
495 | } |
496 | ||
13f2b9d7 DJ |
497 | if (ret == SR_OK) |
498 | ret = sr_scpi_get_opc(sdi->conn); | |
499 | ||
23e1ea7a DJ |
500 | if (ret == SR_OK && update_sample_rate) |
501 | ret = hmo_update_sample_rate(sdi); | |
502 | ||
06a3e78a DJ |
503 | return ret; |
504 | } | |
505 | ||
13f2b9d7 | 506 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 507 | const struct sr_channel_group *cg) |
06a3e78a | 508 | { |
61e77667 | 509 | int cg_type; |
13f2b9d7 DJ |
510 | struct dev_context *devc; |
511 | struct scope_config *model; | |
512 | ||
513 | if (!sdi || !(devc = sdi->priv)) | |
514 | return SR_ERR_ARG; | |
06a3e78a | 515 | |
61e77667 | 516 | if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID) |
13f2b9d7 DJ |
517 | return SR_ERR; |
518 | ||
519 | model = devc->model_config; | |
06a3e78a | 520 | |
06a3e78a | 521 | switch (key) { |
226363c4 BV |
522 | case SR_CONF_SCAN_OPTIONS: |
523 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
524 | scanopts, ARRAY_SIZE(scanopts), sizeof(int32_t)); | |
525 | break; | |
13f2b9d7 | 526 | case SR_CONF_DEVICE_OPTIONS: |
61e77667 | 527 | if (cg_type == CG_NONE) { |
13f2b9d7 | 528 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
89280b1a UH |
529 | model->hw_caps, model->num_hwcaps, |
530 | sizeof(int32_t)); | |
61e77667 | 531 | } else if (cg_type == CG_ANALOG) { |
13f2b9d7 | 532 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
89280b1a UH |
533 | model->analog_hwcaps, model->num_analog_hwcaps, |
534 | sizeof(int32_t)); | |
13f2b9d7 DJ |
535 | } else { |
536 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
89280b1a | 537 | NULL, 0, sizeof(int32_t)); |
13f2b9d7 DJ |
538 | } |
539 | break; | |
13f2b9d7 | 540 | case SR_CONF_COUPLING: |
61e77667 | 541 | if (cg_type == CG_NONE) |
660e398f | 542 | return SR_ERR_CHANNEL_GROUP; |
13f2b9d7 | 543 | *data = g_variant_new_strv(*model->coupling_options, |
89280b1a | 544 | g_strv_length((char **)*model->coupling_options)); |
13f2b9d7 | 545 | break; |
13f2b9d7 DJ |
546 | case SR_CONF_TRIGGER_SOURCE: |
547 | *data = g_variant_new_strv(*model->trigger_sources, | |
89280b1a | 548 | g_strv_length((char **)*model->trigger_sources)); |
13f2b9d7 | 549 | break; |
bbabdaf1 DJ |
550 | case SR_CONF_TRIGGER_SLOPE: |
551 | *data = g_variant_new_strv(*model->trigger_slopes, | |
552 | g_strv_length((char **)*model->trigger_slopes)); | |
553 | break; | |
13f2b9d7 DJ |
554 | case SR_CONF_TIMEBASE: |
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 DJ |
573 | struct dev_context *devc; |
574 | struct scope_config *model; | |
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 DJ |
641 | struct scope_state *state; |
642 | 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 | }; |