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