]> sigrok.org Git - libsigrok.git/blob - src/hardware/gwinstek-gpd/api.c
rigol-ds: improve robustness in samplerate getting code path
[libsigrok.git] / src / hardware / gwinstek-gpd / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2018 Bastian Schmitz <bastian.schmitz@udo.edu>
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
20 #include <config.h>
21 #include <string.h>
22 #include "protocol.h"
23
24 #define IDN_RETRIES 3 /* at least 2 */
25
26 static const uint32_t scanopts[] = {
27         SR_CONF_CONN,
28         SR_CONF_SERIALCOMM,
29 };
30
31 static const uint32_t drvopts[] = {
32         SR_CONF_POWER_SUPPLY,
33 };
34
35 static const uint32_t devopts[] = {
36         SR_CONF_CONTINUOUS,
37         SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
38         SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
39         SR_CONF_CHANNEL_CONFIG | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
40         SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
41 };
42
43 static const uint32_t devopts_cg[] = {
44         SR_CONF_VOLTAGE | SR_CONF_GET,
45         SR_CONF_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
46         SR_CONF_CURRENT | SR_CONF_GET,
47         SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
48 };
49
50 static const char *channel_modes[] = {
51         "Independent",
52 };
53
54 static const char *gpd_serialcomms[] = {
55         "9600/8n1",
56         "57600/8n1",
57         "115200/8n1"
58 };
59
60 static const struct gpd_model models[] = {
61         { GPD_2303S, "GPD-2303S",
62                 CHANMODE_INDEPENDENT,
63                 2,
64                 {
65                         /* Channel 1 */
66                         { { 0, 30, 0.001 }, { 0, 3, 0.001 } },
67                         /* Channel 2 */
68                         { { 0, 30, 0.001 }, { 0, 3, 0.001 } },
69                 },
70         },
71         { GPD_3303S, "GPD-3303S",
72                 CHANMODE_INDEPENDENT,
73                 2,
74                 {
75                         /* Channel 1 */
76                         { { 0, 32, 0.001 }, { 0, 3.2, 0.001 } },
77                         /* Channel 2 */
78                         { { 0, 32, 0.001 }, { 0, 3.2, 0.001 } },
79                 },
80         },
81 };
82
83 static GSList *scan(struct sr_dev_driver *di, GSList *options)
84 {
85         const char *conn, *serialcomm, **serialcomms;
86         const struct gpd_model *model;
87         const struct sr_config *src;
88         struct sr_channel *ch;
89         struct sr_channel_group *cg;
90         GSList *l;
91         struct sr_serial_dev_inst *serial;
92         struct sr_dev_inst *sdi;
93         char reply[100];
94         unsigned int i, b, serialcomms_count;
95         struct dev_context *devc;
96         char channel[10];
97         GRegex *regex;
98         GMatchInfo *match_info;
99         unsigned int cc_cv_ch1, cc_cv_ch2, track1, track2, beep, baud1, baud2;
100
101         serial = NULL;
102         match_info = NULL;
103         regex = NULL;
104         conn = NULL;
105         serialcomm = NULL;
106
107         for (l = options; l; l = l->next) {
108                 src = l->data;
109                 switch (src->key) {
110                 case SR_CONF_CONN:
111                         conn = g_variant_get_string(src->data, NULL);
112                         break;
113                 case SR_CONF_SERIALCOMM:
114                         serialcomm = g_variant_get_string(src->data, NULL);
115                         break;
116                 }
117         }
118
119         if (!conn)
120                 return NULL;
121         if (serialcomm) {
122                 serialcomms = &serialcomm;
123                 serialcomms_count = 1;
124         } else {
125                 serialcomms = gpd_serialcomms;
126                 serialcomms_count = sizeof(gpd_serialcomms) / sizeof(gpd_serialcomms[0]);
127         }
128
129         for( b = 0; b < serialcomms_count; b++) {
130                 serialcomm = serialcomms[b];
131                 sr_info("Probing serial port %s @ %s", conn, serialcomm);
132                 serial = sr_serial_dev_inst_new(conn, serialcomm);
133                 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
134                         continue;
135
136                 /*
137                  * Problem: we need to clear the GPD receive buffer before we
138                  * can expect it to process commands correctly.
139                  *
140                  * Do not just send a newline, since that may cause it to
141                  * execute a currently buffered command.
142                  *
143                  * Solution: Send identification request a few times.
144                  * The first should corrupt any previous buffered command if present
145                  * and respond with "Invalid Character." or respond directly with
146                  * an identification string.
147                  */
148                 for (i = 0; i < IDN_RETRIES; i++) {
149                         /* Request the GPD to identify itself */
150                         gpd_send_cmd(serial, "*IDN?\n");
151                         if (gpd_receive_reply(serial, reply, sizeof(reply)) == SR_OK) {
152                                 if (0 == strncmp(reply, "GW INSTEK", 9)) {
153                                         break;
154                                 }
155                         }
156                 }
157                 if (i == IDN_RETRIES) {
158                         sr_err("Device did not reply to identification request.");
159                         serial_flush(serial);
160                         goto error;
161                 }
162
163                 /*
164                  * Returned identification string is for example:
165                  * "GW INSTEK,GPD-2303S,SN:ER915277,V2.10"
166                  */
167                 regex = g_regex_new("GW INSTEK,(.+),SN:(.+),(V.+)", 0, 0, NULL);
168                 if (!g_regex_match(regex, reply, 0, &match_info)) {
169                         sr_err("Unsupported model '%s'.", reply);
170                         goto error;
171                 }
172
173                 model = NULL;
174                 for (i = 0; i < ARRAY_SIZE(models); i++) {
175                         if (!strcmp(g_match_info_fetch(match_info, 1), models[i].name)) {
176                                 model = &models[i];
177                                 break;
178                         }
179                 }
180                 if (!model) {
181                         sr_err("Unsupported model '%s'.", reply);
182                         goto error;
183                 }
184
185                 sr_info("Detected model '%s'.", model->name);
186
187                 sdi = g_malloc0(sizeof(struct sr_dev_inst));
188                 sdi->status = SR_ST_INACTIVE;
189                 sdi->vendor = g_strdup("GW Instek");
190                 sdi->model = g_strdup(model->name);
191                 sdi->inst_type = SR_INST_SERIAL;
192                 sdi->conn = serial;
193
194                 for (i = 0; i < model->num_channels; i++) {
195                         snprintf(channel, sizeof(channel), "CH%d", i + 1);
196                         ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel);
197                         cg = g_malloc(sizeof(struct sr_channel_group));
198                         cg->name = g_strdup(channel);
199                         cg->channels = g_slist_append(NULL, ch);
200                         cg->priv = NULL;
201                         sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
202                 }
203
204                 devc = g_malloc0(sizeof(struct dev_context));
205                 sr_sw_limits_init(&devc->limits);
206                 devc->model = model;
207                 devc->config = g_malloc0(sizeof(struct per_channel_config)
208                                          * model->num_channels);
209                 sdi->priv = devc;
210
211                 serial_flush(serial);
212                 gpd_send_cmd(serial, "STATUS?\n");
213                 gpd_receive_reply(serial, reply, sizeof(reply));
214
215                 if (sscanf(reply, "%1u%1u%1u%1u%1u%1u%1u%1u", &cc_cv_ch1,
216                                 &cc_cv_ch2, &track1, &track2, &beep,
217                                 &devc->output_enabled, &baud1, &baud2) != 8) {
218                         /* old firmware (< 2.00?) responds with different format */
219                         if (sscanf(reply, "%1u %1u %1u %1u %1u X %1u X", &cc_cv_ch1,
220                                    &cc_cv_ch2, &track1, &track2, &beep,
221                                    &devc->output_enabled) != 6) {
222                                 sr_err("Invalid reply to STATUS: '%s'.", reply);
223                                 goto error;
224                         }
225                         /* ignore remaining two lines of status message */
226                         gpd_receive_reply(serial, reply, sizeof(reply));
227                         gpd_receive_reply(serial, reply, sizeof(reply));
228                 }
229
230                 for (i = 0; i < model->num_channels; i++) {
231                         gpd_send_cmd(serial, "ISET%d?\n", i + 1);
232                         gpd_receive_reply(serial, reply, sizeof(reply));
233                         if (sscanf(reply, "%f", &devc->config[i].output_current_max) != 1) {
234                                 sr_err("Invalid reply to ISETn?: '%s'.", reply);
235                                 goto error;
236                         }
237
238                         gpd_send_cmd(serial, "VSET%d?\n", i + 1);
239                         gpd_receive_reply(serial, reply, sizeof(reply));
240                         if (sscanf(reply, "%f", &devc->config[i].output_voltage_max) != 1) {
241                                 sr_err("Invalid reply to VSETn?: '%s'.", reply);
242                                 goto error;
243                         }
244                         gpd_send_cmd(serial, "IOUT%d?\n", i + 1);
245                         gpd_receive_reply(serial, reply, sizeof(reply));
246                         if (sscanf(reply, "%f", &devc->config[i].output_current_last) != 1) {
247                                 sr_err("Invalid reply to IOUTn?: '%s'.", reply);
248                                 goto error;
249                         }
250                         gpd_send_cmd(serial, "VOUT%d?\n", i + 1);
251                         gpd_receive_reply(serial, reply, sizeof(reply));
252                         if (sscanf(reply, "%f", &devc->config[i].output_voltage_last) != 1) {
253                                 sr_err("Invalid reply to VOUTn?: '%s'.", reply);
254                                 goto error;
255                         }
256                 }
257
258                 return std_scan_complete(di, g_slist_append(NULL, sdi));
259
260         error:
261                 if (match_info)
262                         g_match_info_free(match_info);
263                 if (regex)
264                         g_regex_unref(regex);
265                 if (serial)
266                         serial_close(serial);
267         }
268
269         return NULL;
270 }
271
272 static int config_get(uint32_t key, GVariant **data,
273         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
274 {
275         int channel;
276         const struct dev_context *devc;
277         const struct sr_channel *ch;
278
279         if (!sdi)
280                 return SR_ERR_ARG;
281
282         devc = sdi->priv;
283
284         if (!cg) {
285                 switch (key) {
286                 case SR_CONF_LIMIT_SAMPLES:
287                 case SR_CONF_LIMIT_MSEC:
288                         return sr_sw_limits_config_get(&devc->limits, key, data);
289                 case SR_CONF_CHANNEL_CONFIG:
290                         *data = g_variant_new_string(
291                                 channel_modes[devc->channel_mode]);
292                         break;
293                 case SR_CONF_ENABLED:
294                         *data = g_variant_new_boolean(devc->output_enabled);
295                         break;
296                 default:
297                         return SR_ERR_NA;
298                 }
299         } else {
300                 ch = cg->channels->data;
301                 channel = ch->index;
302                 switch (key) {
303                 case SR_CONF_VOLTAGE:
304                         *data = g_variant_new_double(
305                                 devc->config[channel].output_voltage_last);
306                         break;
307                 case SR_CONF_VOLTAGE_TARGET:
308                         *data = g_variant_new_double(
309                                 devc->config[channel].output_voltage_max);
310                         break;
311                 case SR_CONF_CURRENT:
312                         *data = g_variant_new_double(
313                                 devc->config[channel].output_current_last);
314                         break;
315                 case SR_CONF_CURRENT_LIMIT:
316                         *data = g_variant_new_double(
317                                 devc->config[channel].output_current_max);
318                         break;
319                 default:
320                         return SR_ERR_NA;
321                 }
322         }
323
324         return SR_OK;
325 }
326
327 static int config_set(uint32_t key, GVariant *data,
328         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
329 {
330         int ret, channel;
331         const struct sr_channel *ch;
332         double dval;
333         gboolean bval;
334         struct dev_context *devc;
335
336         devc = sdi->priv;
337
338         ret = SR_OK;
339
340         switch (key) {
341         case SR_CONF_LIMIT_MSEC:
342         case SR_CONF_LIMIT_SAMPLES:
343                 return sr_sw_limits_config_set(&devc->limits, key, data);
344         case SR_CONF_ENABLED:
345                 bval = g_variant_get_boolean(data);
346                 gpd_send_cmd(sdi->conn, "OUT%c\n", bval ? '1' : '0');
347                 devc->output_enabled = bval;
348                 break;
349         case SR_CONF_VOLTAGE_TARGET:
350                 ch = cg->channels->data;
351                 channel = ch->index;
352                 dval = g_variant_get_double(data);
353                 if (dval < devc->model->channels[channel].voltage[0]
354                     || dval > devc->model->channels[channel].voltage[1])
355                         return SR_ERR_ARG;
356                 gpd_send_cmd(sdi->conn, "VSET%d:%05.3lf\n", channel + 1, dval);
357                 devc->config[channel].output_voltage_max = dval;
358                 break;
359         case SR_CONF_CURRENT_LIMIT:
360                 ch = cg->channels->data;
361                 channel = ch->index;
362                 dval = g_variant_get_double(data);
363                 if (dval < devc->model->channels[channel].current[0]
364                     || dval > devc->model->channels[channel].current[1])
365                         return SR_ERR_ARG;
366                 gpd_send_cmd(sdi->conn, "ISET%d:%05.3lf\n", channel + 1, dval);
367                 devc->config[channel].output_current_max = dval;
368                 break;
369         default:
370                 ret = SR_ERR_NA;
371                 break;
372         }
373
374         return ret;
375 }
376
377 static int config_list(uint32_t key, GVariant **data,
378         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
379 {
380         const struct dev_context *devc;
381         const struct sr_channel *ch;
382         int channel;
383
384         devc = (sdi) ? sdi->priv : NULL;
385
386         if (!cg) {
387                 switch (key) {
388                 case SR_CONF_SCAN_OPTIONS:
389                 case SR_CONF_DEVICE_OPTIONS:
390                         return STD_CONFIG_LIST(key, data, sdi, cg, scanopts,
391                                                drvopts, devopts);
392                 case SR_CONF_CHANNEL_CONFIG:
393                         *data = g_variant_new_strv(ARRAY_AND_SIZE(channel_modes));
394                         break;
395                 default:
396                         return SR_ERR_NA;
397                 }
398         } else {
399                 ch = cg->channels->data;
400                 channel = ch->index;
401
402                 switch (key) {
403                 case SR_CONF_DEVICE_OPTIONS:
404                         *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg));
405                         break;
406                 case SR_CONF_VOLTAGE_TARGET:
407                         *data = std_gvar_min_max_step_array(
408                                 devc->model->channels[channel].voltage);
409                         break;
410                 case SR_CONF_CURRENT_LIMIT:
411                         *data = std_gvar_min_max_step_array(
412                                 devc->model->channels[channel].current);
413                         break;
414                 default:
415                         return SR_ERR_NA;
416                 }
417         }
418
419         return SR_OK;
420 }
421
422 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
423 {
424         struct dev_context *devc;
425         struct sr_serial_dev_inst *serial;
426
427         devc = sdi->priv;
428
429         sr_sw_limits_acquisition_start(&devc->limits);
430         std_session_send_df_header(sdi);
431
432         devc->reply_pending = FALSE;
433         devc->req_sent_at = 0;
434         serial = sdi->conn;
435         serial_source_add(sdi->session, serial, G_IO_IN, 100,
436                           gpd_receive_data, (void *)sdi);
437
438         return SR_OK;
439 }
440
441 static struct sr_dev_driver gwinstek_gpd_driver_info = {
442         .name = "gwinstek-gpd",
443         .longname = "GW Instek GPD",
444         .api_version = 1,
445         .init = std_init,
446         .cleanup = std_cleanup,
447         .scan = scan,
448         .dev_list = std_dev_list,
449         .dev_clear = std_dev_clear,
450         .config_get = config_get,
451         .config_set = config_set,
452         .config_list = config_list,
453         .dev_open = std_serial_dev_open,
454         .dev_close = std_serial_dev_close,
455         .dev_acquisition_start = dev_acquisition_start,
456         .dev_acquisition_stop = std_serial_dev_acquisition_stop,
457         .context = NULL,
458 };
459 SR_REGISTER_DEV_DRIVER(gwinstek_gpd_driver_info);