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