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