]> sigrok.org Git - libsigrok.git/blob - src/hardware/gwinstek-gpd/api.c
gwinstek-gpd: Add support for GPD-3303S.
[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[50];
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.", conn);
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                 sr_err("Invalid reply to STATUS: '%s'.", reply);
183                 goto error;
184         }
185
186         for (i = 0; i < model->num_channels; ++i) {
187                 gpd_send_cmd(serial, "ISET%d?\n", i + 1);
188                 gpd_receive_reply(serial, reply, sizeof(reply));
189                 if (sscanf(reply, "%f", &devc->config[i].output_current_max) != 1) {
190                         sr_err("Invalid reply to ISETn?: '%s'.", reply);
191                         goto error;
192                 }
193
194                 gpd_send_cmd(serial, "VSET%d?\n", i + 1);
195                 gpd_receive_reply(serial, reply, sizeof(reply));
196                 if (sscanf(reply, "%f", &devc->config[i].output_voltage_max) != 1) {
197                         sr_err("Invalid reply to VSETn?: '%s'.", reply);
198                         goto error;
199                 }
200                 gpd_send_cmd(serial, "IOUT%d?\n", i + 1);
201                 gpd_receive_reply(serial, reply, sizeof(reply));
202                 if (sscanf(reply, "%f", &devc->config[i].output_current_last) != 1) {
203                         sr_err("Invalid reply to IOUTn?: '%s'.", reply);
204                         goto error;
205                 }
206                 gpd_send_cmd(serial, "VOUT%d?\n", i + 1);
207                 gpd_receive_reply(serial, reply, sizeof(reply));
208                 if (sscanf(reply, "%f", &devc->config[i].output_voltage_last) != 1) {
209                         sr_err("Invalid reply to VOUTn?: '%s'.", reply);
210                         goto error;
211                 }
212         }
213
214         serial_close(serial);
215
216         return std_scan_complete(di, g_slist_append(NULL, sdi));
217
218 error:
219         if (match_info)
220                 g_match_info_free(match_info);
221         if (regex)
222                 g_regex_unref(regex);
223         if (serial)
224                 serial_close(serial);
225
226         return NULL;
227 }
228
229 static int config_get(uint32_t key, GVariant **data,
230         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
231 {
232         int channel;
233         const struct dev_context *devc;
234         const struct sr_channel *ch;
235
236         if (!sdi)
237                 return SR_ERR_ARG;
238
239         devc = sdi->priv;
240
241         if (!cg) {
242                 switch (key) {
243                 case SR_CONF_CHANNEL_CONFIG:
244                         *data = g_variant_new_string(
245                                 channel_modes[devc->channel_mode]);
246                         break;
247                 case SR_CONF_ENABLED:
248                         *data = g_variant_new_boolean(devc->output_enabled);
249                         break;
250                 default:
251                         return SR_ERR_NA;
252                 }
253         } else {
254                 ch = cg->channels->data;
255                 channel = ch->index;
256                 switch (key) {
257                 case SR_CONF_VOLTAGE:
258                         *data = g_variant_new_double(
259                                 devc->config[channel].output_voltage_last);
260                         break;
261                 case SR_CONF_VOLTAGE_TARGET:
262                         *data = g_variant_new_double(
263                                 devc->config[channel].output_voltage_max);
264                         break;
265                 case SR_CONF_CURRENT:
266                         *data = g_variant_new_double(
267                                 devc->config[channel].output_current_last);
268                         break;
269                 case SR_CONF_CURRENT_LIMIT:
270                         *data = g_variant_new_double(
271                                 devc->config[channel].output_current_max);
272                         break;
273                 default:
274                         return SR_ERR_NA;
275                 }
276         }
277
278         return SR_OK;
279 }
280
281 static int config_set(uint32_t key, GVariant *data,
282         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
283 {
284         int ret, channel;
285         const struct sr_channel *ch;
286         double dval;
287         gboolean bval;
288         struct dev_context *devc;
289
290         devc = sdi->priv;
291
292         ret = SR_OK;
293
294         switch (key) {
295         case SR_CONF_LIMIT_MSEC:
296         case SR_CONF_LIMIT_SAMPLES:
297                 return sr_sw_limits_config_set(&devc->limits, key, data);
298         case SR_CONF_ENABLED:
299                 bval = g_variant_get_boolean(data);
300                 gpd_send_cmd(sdi->conn, "OUT%c\n", bval ? '1' : '0');
301                 devc->output_enabled = bval;
302                 break;
303         case SR_CONF_VOLTAGE_TARGET:
304                 ch = cg->channels->data;
305                 channel = ch->index;
306                 dval = g_variant_get_double(data);
307                 if (dval < devc->model->channels[channel].voltage[0]
308                     || dval > devc->model->channels[channel].voltage[1])
309                         return SR_ERR_ARG;
310                 gpd_send_cmd(sdi->conn, "VSET%d:%05.3lf\n", channel + 1, dval);
311                 devc->config[channel].output_voltage_max = dval;
312                 break;
313         case SR_CONF_CURRENT_LIMIT:
314                 ch = cg->channels->data;
315                 channel = ch->index;
316                 dval = g_variant_get_double(data);
317                 if (dval < devc->model->channels[channel].current[0]
318                     || dval > devc->model->channels[channel].current[1])
319                         return SR_ERR_ARG;
320                 gpd_send_cmd(sdi->conn, "ISET%d:%05.3lf\n", channel + 1, dval);
321                 devc->config[channel].output_current_max = dval;
322                 break;
323         default:
324                 ret = SR_ERR_NA;
325                 break;
326         }
327
328         return ret;
329 }
330
331 static int config_list(uint32_t key, GVariant **data,
332         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
333 {
334         const struct dev_context *devc;
335         const struct sr_channel *ch;
336         int channel;
337
338         devc = (sdi) ? sdi->priv : NULL;
339
340         if (!cg) {
341                 switch (key) {
342                 case SR_CONF_SCAN_OPTIONS:
343                 case SR_CONF_DEVICE_OPTIONS:
344                         return STD_CONFIG_LIST(key, data, sdi, cg, scanopts,
345                                                drvopts, devopts);
346                 case SR_CONF_CHANNEL_CONFIG:
347                         *data = g_variant_new_strv(ARRAY_AND_SIZE(channel_modes));
348                         break;
349                 default:
350                         return SR_ERR_NA;
351                 }
352         } else {
353                 ch = cg->channels->data;
354                 channel = ch->index;
355
356                 switch (key) {
357                 case SR_CONF_DEVICE_OPTIONS:
358                         *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg));
359                         break;
360                 case SR_CONF_VOLTAGE_TARGET:
361                         *data = std_gvar_min_max_step_array(
362                                 devc->model->channels[channel].voltage);
363                         break;
364                 case SR_CONF_CURRENT_LIMIT:
365                         *data = std_gvar_min_max_step_array(
366                                 devc->model->channels[channel].current);
367                         break;
368                 default:
369                         return SR_ERR_NA;
370                 }
371         }
372
373         return SR_OK;
374 }
375
376 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
377 {
378         struct dev_context *devc;
379         struct sr_serial_dev_inst *serial;
380
381         devc = sdi->priv;
382
383         sr_sw_limits_acquisition_start(&devc->limits);
384         std_session_send_df_header(sdi);
385
386         devc->reply_pending = FALSE;
387         devc->req_sent_at = 0;
388         serial = sdi->conn;
389         serial_source_add(sdi->session, serial, G_IO_IN, 100,
390                           gpd_receive_data, (void *)sdi);
391
392         return SR_OK;
393 }
394
395 static struct sr_dev_driver gwinstek_gpd_driver_info = {
396         .name = "gwinstek-gpd",
397         .longname = "GW Instek GPD",
398         .api_version = 1,
399         .init = std_init,
400         .cleanup = std_cleanup,
401         .scan = scan,
402         .dev_list = std_dev_list,
403         .dev_clear = std_dev_clear,
404         .config_get = config_get,
405         .config_set = config_set,
406         .config_list = config_list,
407         .dev_open = std_serial_dev_open,
408         .dev_close = std_serial_dev_close,
409         .dev_acquisition_start = dev_acquisition_start,
410         .dev_acquisition_stop = std_serial_dev_acquisition_stop,
411         .context = NULL,
412 };
413 SR_REGISTER_DEV_DRIVER(gwinstek_gpd_driver_info);