]> sigrok.org Git - libsigrok.git/blob - src/hardware/yokogawa-dlm/api.c
drivers: Drop unneeded or duplicate comments.
[libsigrok.git] / src / hardware / yokogawa-dlm / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 abraxa (Soeren Apel) <soeren@apelpie.net>
5  * Based on the Hameg HMO driver by poljar (Damir Jelić) <poljarinho@gmail.com>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22 #include <stdlib.h>
23 #include "scpi.h"
24 #include "protocol.h"
25
26 static struct sr_dev_driver yokogawa_dlm_driver_info;
27
28 static const char *MANUFACTURER_ID = "YOKOGAWA";
29 static const char *MANUFACTURER_NAME = "Yokogawa";
30
31 static const uint32_t dlm_scanopts[] = {
32         SR_CONF_CONN,
33 };
34
35 static const uint32_t dlm_drvopts[] = {
36         SR_CONF_LOGIC_ANALYZER,
37         SR_CONF_OSCILLOSCOPE,
38 };
39
40 static const uint32_t dlm_devopts[] = {
41         SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
42         SR_CONF_SAMPLERATE | SR_CONF_GET,
43         SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
44         SR_CONF_NUM_HDIV | SR_CONF_GET,
45         SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
46         SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
47         SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
48 };
49
50 static const uint32_t dlm_analog_devopts[] = {
51         SR_CONF_NUM_VDIV | SR_CONF_GET,
52         SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
53         SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
54 };
55
56 static const uint32_t dlm_digital_devopts[] = {
57 };
58
59 enum {
60         CG_INVALID = -1,
61         CG_NONE,
62         CG_ANALOG,
63         CG_DIGITAL,
64 };
65
66 static struct sr_dev_inst *probe_usbtmc_device(struct sr_scpi_dev_inst *scpi)
67 {
68         struct sr_dev_inst *sdi;
69         struct dev_context *devc;
70         struct sr_scpi_hw_info *hw_info;
71         char *model_name;
72         int model_index;
73
74         sdi = NULL;
75         devc = NULL;
76         hw_info = NULL;
77
78         if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
79                 sr_info("Couldn't get IDN response.");
80                 goto fail;
81         }
82
83         if (strcmp(hw_info->manufacturer, MANUFACTURER_ID) != 0)
84                 goto fail;
85
86         if (dlm_model_get(hw_info->model, &model_name, &model_index) != SR_OK)
87                 goto fail;
88
89         sdi = g_malloc0(sizeof(struct sr_dev_inst));
90         sdi->vendor = g_strdup(MANUFACTURER_NAME);
91         sdi->model = g_strdup(model_name);
92         sdi->version = g_strdup(hw_info->firmware_version);
93
94         sdi->serial_num = g_strdup(hw_info->serial_number);
95
96         sr_scpi_hw_info_free(hw_info);
97         hw_info = NULL;
98
99         devc = g_malloc0(sizeof(struct dev_context));
100
101         sdi->driver = &yokogawa_dlm_driver_info;
102         sdi->priv = devc;
103         sdi->inst_type = SR_INST_SCPI;
104         sdi->conn = scpi;
105
106         if (dlm_device_init(sdi, model_index) != SR_OK)
107                 goto fail;
108
109         return sdi;
110
111 fail:
112         sr_scpi_hw_info_free(hw_info);
113         sr_dev_inst_free(sdi);
114         g_free(devc);
115
116         return NULL;
117 }
118
119 static GSList *scan(struct sr_dev_driver *di, GSList *options)
120 {
121         return sr_scpi_scan(di->context, options, probe_usbtmc_device);
122 }
123
124 static void clear_helper(struct dev_context *devc)
125 {
126         dlm_scope_state_destroy(devc->model_state);
127         g_free(devc->analog_groups);
128         g_free(devc->digital_groups);
129 }
130
131 static int dev_clear(const struct sr_dev_driver *di)
132 {
133         return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
134 }
135
136 static int dev_open(struct sr_dev_inst *sdi)
137 {
138         if (sr_scpi_open(sdi->conn) != SR_OK)
139                 return SR_ERR;
140
141         if (dlm_scope_state_query(sdi) != SR_OK)
142                 return SR_ERR;
143
144         return SR_OK;
145 }
146
147 static int dev_close(struct sr_dev_inst *sdi)
148 {
149         return sr_scpi_close(sdi->conn);
150 }
151
152 /**
153  * Check which category a given channel group belongs to.
154  *
155  * @param devc Our internal device context.
156  * @param cg The channel group to check.
157  *
158  * @retval CG_NONE cg is NULL
159  * @retval CG_ANALOG cg is an analog group
160  * @retval CG_DIGITAL cg is a digital group
161  * @retval CG_INVALID cg is something else
162  */
163 static int check_channel_group(struct dev_context *devc,
164                         const struct sr_channel_group *cg)
165 {
166         unsigned int i;
167         const struct scope_config *model;
168
169         model = devc->model_config;
170
171         if (!cg)
172                 return CG_NONE;
173
174         for (i = 0; i < model->analog_channels; i++)
175                 if (cg == devc->analog_groups[i])
176                         return CG_ANALOG;
177
178         for (i = 0; i < model->pods; i++)
179                 if (cg == devc->digital_groups[i])
180                         return CG_DIGITAL;
181
182         sr_err("Invalid channel group specified.");
183         return CG_INVALID;
184 }
185
186 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
187                 const struct sr_channel_group *cg)
188 {
189         int ret, cg_type;
190         unsigned int i;
191         struct dev_context *devc;
192         const struct scope_config *model;
193         struct scope_state *state;
194
195         if (!sdi)
196                 return SR_ERR_ARG;
197
198         devc = sdi->priv;
199
200         if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
201                 return SR_ERR;
202
203         model = devc->model_config;
204         state = devc->model_state;
205
206         switch (key) {
207         case SR_CONF_NUM_HDIV:
208                 *data = g_variant_new_int32(model->num_xdivs);
209                 ret = SR_OK;
210                 break;
211         case SR_CONF_TIMEBASE:
212                 *data = g_variant_new("(tt)",
213                                 dlm_timebases[state->timebase][0],
214                                 dlm_timebases[state->timebase][1]);
215                 ret = SR_OK;
216                 break;
217         case SR_CONF_NUM_VDIV:
218                 if (cg_type == CG_NONE) {
219                         sr_err("No channel group specified.");
220                         return SR_ERR_CHANNEL_GROUP;
221                 } else if (cg_type == CG_ANALOG) {
222                                 *data = g_variant_new_int32(model->num_ydivs);
223                                 ret = SR_OK;
224                                 break;
225                 } else {
226                         ret = SR_ERR_NA;
227                 }
228                 break;
229         case SR_CONF_VDIV:
230                 ret = SR_ERR_NA;
231                 if (cg_type == CG_NONE) {
232                         sr_err("No channel group specified.");
233                         return SR_ERR_CHANNEL_GROUP;
234                 } else if (cg_type != CG_ANALOG)
235                         break;
236
237                 for (i = 0; i < model->analog_channels; i++) {
238                         if (cg != devc->analog_groups[i])
239                                 continue;
240                         *data = g_variant_new("(tt)",
241                                         dlm_vdivs[state->analog_states[i].vdiv][0],
242                                         dlm_vdivs[state->analog_states[i].vdiv][1]);
243                         ret = SR_OK;
244                         break;
245                 }
246                 break;
247         case SR_CONF_TRIGGER_SOURCE:
248                 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
249                 ret = SR_OK;
250                 break;
251         case SR_CONF_TRIGGER_SLOPE:
252                 *data = g_variant_new_string(dlm_trigger_slopes[state->trigger_slope]);
253                 ret = SR_OK;
254                 break;
255         case SR_CONF_HORIZ_TRIGGERPOS:
256                 *data = g_variant_new_double(state->horiz_triggerpos);
257                 ret = SR_OK;
258                 break;
259         case SR_CONF_COUPLING:
260                 ret = SR_ERR_NA;
261                 if (cg_type == CG_NONE) {
262                         sr_err("No channel group specified.");
263                         return SR_ERR_CHANNEL_GROUP;
264                 } else if (cg_type != CG_ANALOG)
265                         break;
266
267                 for (i = 0; i < model->analog_channels; i++) {
268                         if (cg != devc->analog_groups[i])
269                                 continue;
270                         *data = g_variant_new_string((*model->coupling_options)[state->analog_states[i].coupling]);
271                         ret = SR_OK;
272                         break;
273                 }
274                 break;
275         case SR_CONF_SAMPLERATE:
276                 *data = g_variant_new_uint64(state->sample_rate);
277                 ret = SR_OK;
278                 break;
279         default:
280                 ret = SR_ERR_NA;
281         }
282
283         return ret;
284 }
285
286 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
287 {
288         unsigned int i;
289         GVariant *rational[2];
290         GVariantBuilder gvb;
291
292         g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
293
294         for (i = 0; i < n; i++) {
295                 rational[0] = g_variant_new_uint64((*array)[i][0]);
296                 rational[1] = g_variant_new_uint64((*array)[i][1]);
297
298                 /* FIXME: Valgrind reports a memory leak here. */
299                 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
300         }
301
302         return g_variant_builder_end(&gvb);
303 }
304
305 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
306                 const struct sr_channel_group *cg)
307 {
308         int ret, cg_type;
309         unsigned int i, j;
310         char float_str[30];
311         struct dev_context *devc;
312         const struct scope_config *model;
313         struct scope_state *state;
314         const char *tmp;
315         uint64_t p, q;
316         double tmp_d;
317         gboolean update_sample_rate;
318
319         if (!sdi || !(devc = sdi->priv))
320                 return SR_ERR_ARG;
321
322         if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
323                 return SR_ERR;
324
325         model = devc->model_config;
326         state = devc->model_state;
327         update_sample_rate = FALSE;
328
329         ret = SR_ERR_NA;
330
331         switch (key) {
332         case SR_CONF_LIMIT_FRAMES:
333                 devc->frame_limit = g_variant_get_uint64(data);
334                 ret = SR_OK;
335                 break;
336         case SR_CONF_TRIGGER_SOURCE:
337                 tmp = g_variant_get_string(data, NULL);
338                 for (i = 0; (*model->trigger_sources)[i]; i++) {
339                         if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
340                                 continue;
341                         state->trigger_source = i;
342                         /* TODO: A and B trigger support possible? */
343                         ret = dlm_trigger_source_set(sdi->conn, (*model->trigger_sources)[i]);
344                         break;
345                 }
346                 break;
347         case SR_CONF_VDIV:
348                 if (cg_type == CG_NONE) {
349                         sr_err("No channel group specified.");
350                         return SR_ERR_CHANNEL_GROUP;
351                 }
352
353                 g_variant_get(data, "(tt)", &p, &q);
354
355                 for (i = 0; i < ARRAY_SIZE(dlm_vdivs); i++) {
356                         if (p != dlm_vdivs[i][0] ||
357                                         q != dlm_vdivs[i][1])
358                                 continue;
359                         for (j = 1; j <= model->analog_channels; j++) {
360                                 if (cg != devc->analog_groups[j - 1])
361                                         continue;
362                                 state->analog_states[j - 1].vdiv = i;
363                                 g_ascii_formatd(float_str, sizeof(float_str),
364                                                 "%E", (float) p / q);
365                                 if (dlm_analog_chan_vdiv_set(sdi->conn, j, float_str) != SR_OK ||
366                                                 sr_scpi_get_opc(sdi->conn) != SR_OK)
367                                         return SR_ERR;
368
369                                 break;
370                         }
371
372                         ret = SR_OK;
373                         break;
374                 }
375                 break;
376         case SR_CONF_TIMEBASE:
377                 g_variant_get(data, "(tt)", &p, &q);
378
379                 for (i = 0; i < ARRAY_SIZE(dlm_timebases); i++) {
380                         if (p != dlm_timebases[i][0] ||
381                                         q != dlm_timebases[i][1])
382                                 continue;
383                         state->timebase = i;
384                         g_ascii_formatd(float_str, sizeof(float_str),
385                                         "%E", (float) p / q);
386                         ret = dlm_timebase_set(sdi->conn, float_str);
387                         update_sample_rate = TRUE;
388                         break;
389                 }
390                 break;
391         case SR_CONF_HORIZ_TRIGGERPOS:
392                 tmp_d = g_variant_get_double(data);
393
394                 /* TODO: Check if the calculation makes sense for the DLM. */
395                 if (tmp_d < 0.0 || tmp_d > 1.0)
396                         return SR_ERR;
397
398                 state->horiz_triggerpos = tmp_d;
399                 tmp_d = -(tmp_d - 0.5) *
400                                 ((double) dlm_timebases[state->timebase][0] /
401                                 dlm_timebases[state->timebase][1])
402                                 * model->num_xdivs;
403
404                 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
405                 ret = dlm_horiz_trigger_pos_set(sdi->conn, float_str);
406                 break;
407         case SR_CONF_TRIGGER_SLOPE:
408                 tmp = g_variant_get_string(data, NULL);
409
410                 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
411                         return SR_ERR_ARG;
412
413                 /* Note: See dlm_trigger_slopes[] in protocol.c. */
414                 state->trigger_slope = (tmp[0] == 'r') ?
415                                 SLOPE_POSITIVE : SLOPE_NEGATIVE;
416
417                 ret = dlm_trigger_slope_set(sdi->conn, state->trigger_slope);
418                 break;
419         case SR_CONF_COUPLING:
420                 if (cg_type == CG_NONE) {
421                         sr_err("No channel group specified.");
422                         return SR_ERR_CHANNEL_GROUP;
423                 }
424
425                 tmp = g_variant_get_string(data, NULL);
426
427                 for (i = 0; (*model->coupling_options)[i]; i++) {
428                         if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
429                                 continue;
430                         for (j = 1; j <= model->analog_channels; j++) {
431                                 if (cg != devc->analog_groups[j - 1])
432                                         continue;
433                                 state->analog_states[j-1].coupling = i;
434
435                                 if (dlm_analog_chan_coupl_set(sdi->conn, j, tmp) != SR_OK ||
436                                                 sr_scpi_get_opc(sdi->conn) != SR_OK)
437                                         return SR_ERR;
438                                 break;
439                         }
440
441                         ret = SR_OK;
442                         break;
443                 }
444                 break;
445         default:
446                 ret = SR_ERR_NA;
447                 break;
448         }
449
450         if (ret == SR_OK)
451                 ret = sr_scpi_get_opc(sdi->conn);
452
453         if (ret == SR_OK && update_sample_rate)
454                 ret = dlm_sample_rate_query(sdi);
455
456         return ret;
457 }
458
459 static int config_channel_set(const struct sr_dev_inst *sdi,
460                         struct sr_channel *ch, unsigned int changes)
461 {
462         /* Currently we only handle SR_CHANNEL_SET_ENABLED. */
463         if (changes != SR_CHANNEL_SET_ENABLED)
464                 return SR_ERR_NA;
465
466         return dlm_channel_state_set(sdi, ch->index, ch->enabled);
467 }
468
469 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
470                 const struct sr_channel_group *cg)
471 {
472         int cg_type = CG_NONE;
473         struct dev_context *devc;
474         const struct scope_config *model;
475
476         devc = (sdi) ? sdi->priv : NULL;
477         model = (devc) ? devc->model_config : NULL;
478
479         if (!cg) {
480                 switch (key) {
481                 case SR_CONF_SCAN_OPTIONS:
482                 case SR_CONF_DEVICE_OPTIONS:
483                         return STD_CONFIG_LIST(key, data, sdi, cg, dlm_scanopts, dlm_drvopts, dlm_devopts);
484                 case SR_CONF_TIMEBASE:
485                         *data = build_tuples(&dlm_timebases, ARRAY_SIZE(dlm_timebases));
486                         return SR_OK;
487                 case SR_CONF_TRIGGER_SOURCE:
488                         if (!model)
489                                 return SR_ERR_ARG;
490                         *data = g_variant_new_strv(*model->trigger_sources,
491                                         g_strv_length((char **)*model->trigger_sources));
492                         return SR_OK;
493                 case SR_CONF_TRIGGER_SLOPE:
494                         *data = g_variant_new_strv(dlm_trigger_slopes,
495                                         g_strv_length((char **)dlm_trigger_slopes));
496                         return SR_OK;
497                 case SR_CONF_NUM_HDIV:
498                         *data = g_variant_new_uint32(model->num_xdivs);
499                         return SR_OK;
500                 default:
501                         return SR_ERR_NA;
502                 }
503         }
504
505         if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
506                 return SR_ERR;
507
508         switch (key) {
509         case SR_CONF_DEVICE_OPTIONS:
510                 if (cg_type == CG_ANALOG) {
511                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
512                                 dlm_analog_devopts, ARRAY_SIZE(dlm_analog_devopts), sizeof(uint32_t));
513                 } else if (cg_type == CG_DIGITAL) {
514                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
515                                 dlm_digital_devopts, ARRAY_SIZE(dlm_digital_devopts), sizeof(uint32_t));
516                 } else {
517                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
518                                 NULL, 0, sizeof(uint32_t));
519                 }
520                 break;
521         case SR_CONF_COUPLING:
522                 if (cg_type == CG_NONE)
523                         return SR_ERR_CHANNEL_GROUP;
524                 *data = g_variant_new_strv(*model->coupling_options,
525                                 g_strv_length((char **)*model->coupling_options));
526                 break;
527         case SR_CONF_VDIV:
528                 if (cg_type == CG_NONE)
529                         return SR_ERR_CHANNEL_GROUP;
530                 *data = build_tuples(&dlm_vdivs, ARRAY_SIZE(dlm_vdivs));
531                 break;
532         default:
533                 return SR_ERR_NA;
534         }
535
536         return SR_OK;
537 }
538
539 static int dlm_check_channels(GSList *channels)
540 {
541         GSList *l;
542         struct sr_channel *ch;
543         gboolean enabled_pod1, enabled_chan4;
544
545         enabled_pod1 = enabled_chan4 = FALSE;
546
547         /* Note: On the DLM2000, CH4 and Logic are shared. */
548         /* TODO Handle non-DLM2000 models. */
549         for (l = channels; l; l = l->next) {
550                 ch = l->data;
551                 switch (ch->type) {
552                 case SR_CHANNEL_ANALOG:
553                         if (ch->index == 3)
554                                 enabled_chan4 = TRUE;
555                         break;
556                 case SR_CHANNEL_LOGIC:
557                         enabled_pod1 = TRUE;
558                         break;
559                 default:
560                         return SR_ERR;
561                 }
562         }
563
564         if (enabled_pod1 && enabled_chan4)
565                 return SR_ERR;
566
567         return SR_OK;
568 }
569
570 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
571 {
572         GSList *l;
573         gboolean digital_added;
574         struct sr_channel *ch;
575         struct dev_context *devc;
576         struct sr_scpi_dev_inst *scpi;
577
578         scpi = sdi->conn;
579         devc = sdi->priv;
580         digital_added = FALSE;
581
582         g_slist_free(devc->enabled_channels);
583         devc->enabled_channels = NULL;
584
585         for (l = sdi->channels; l; l = l->next) {
586                 ch = l->data;
587                 if (!ch->enabled)
588                         continue;
589                 /* Only add a single digital channel. */
590                 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
591                         devc->enabled_channels = g_slist_append(
592                                 devc->enabled_channels, ch);
593                         if (ch->type == SR_CHANNEL_LOGIC)
594                                 digital_added = TRUE;
595                 }
596         }
597
598         if (!devc->enabled_channels)
599                 return SR_ERR;
600
601         if (dlm_check_channels(devc->enabled_channels) != SR_OK) {
602                 sr_err("Invalid channel configuration specified!");
603                 return SR_ERR_NA;
604         }
605
606         /* Request data for the first enabled channel. */
607         devc->current_channel = devc->enabled_channels;
608         dlm_channel_data_request(sdi);
609
610         sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 5,
611                         dlm_data_receive, (void *)sdi);
612
613         return SR_OK;
614 }
615
616 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
617 {
618         struct dev_context *devc;
619
620         std_session_send_df_end(sdi);
621
622         devc = sdi->priv;
623
624         devc->num_frames = 0;
625         g_slist_free(devc->enabled_channels);
626         devc->enabled_channels = NULL;
627
628         sr_scpi_source_remove(sdi->session, sdi->conn);
629
630         return SR_OK;
631 }
632
633 static struct sr_dev_driver yokogawa_dlm_driver_info = {
634         .name = "yokogawa-dlm",
635         .longname = "Yokogawa DL/DLM",
636         .api_version = 1,
637         .init = std_init,
638         .cleanup = std_cleanup,
639         .scan = scan,
640         .dev_list = std_dev_list,
641         .dev_clear = dev_clear,
642         .config_get = config_get,
643         .config_set = config_set,
644         .config_channel_set = config_channel_set,
645         .config_list = config_list,
646         .dev_open = dev_open,
647         .dev_close = dev_close,
648         .dev_acquisition_start = dev_acquisition_start,
649         .dev_acquisition_stop = dev_acquisition_stop,
650         .context = NULL,
651 };
652 SR_REGISTER_DEV_DRIVER(yokogawa_dlm_driver_info);