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