]> sigrok.org Git - libsigrok.git/blob - src/hardware/manson-hcs-3xxx/api.c
4a253a18ebfc40587efce9b661d2645a73bcb35f
[libsigrok.git] / src / hardware / manson-hcs-3xxx / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 Uwe Hermann <uwe@hermann-uwe.de>
5  * Copyright (C) 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz>
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 2 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, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  */
21
22 /** @file
23   *  <em>Manson HCS-3xxx series</em> power supply driver
24   *  @internal
25   */
26
27 #include <config.h>
28 #include "protocol.h"
29
30 static const uint32_t drvopts[] = {
31         /* Device class */
32         SR_CONF_POWER_SUPPLY,
33 };
34
35 static const uint32_t scanopts[] = {
36         SR_CONF_CONN,
37         SR_CONF_SERIALCOMM,
38 };
39
40 static const uint32_t devopts[] = {
41         /* Device class */
42         SR_CONF_POWER_SUPPLY,
43         /* Acquisition modes. */
44         SR_CONF_CONTINUOUS,
45         SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
46         SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
47         /* Device configuration */
48         SR_CONF_VOLTAGE | SR_CONF_GET,
49         SR_CONF_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
50         SR_CONF_CURRENT | SR_CONF_GET,
51         SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
52         SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
53 };
54
55 /* Note: All models have one power supply output only. */
56 static const struct hcs_model models[] = {
57         { MANSON_HCS_3100, "HCS-3100",     "3100", { 1, 18, 0.1 }, { 0, 10,   0.10 } },
58         { MANSON_HCS_3102, "HCS-3102",     "3102", { 1, 36, 0.1 }, { 0,  5,   0.01 } },
59         { MANSON_HCS_3104, "HCS-3104",     "3104", { 1, 60, 0.1 }, { 0,  2.5, 0.01 } },
60         { MANSON_HCS_3150, "HCS-3150",     "3150", { 1, 18, 0.1 }, { 0, 15,   0.10 } },
61         { MANSON_HCS_3200, "HCS-3200",     "3200", { 1, 18, 0.1 }, { 0, 20,   0.10 } },
62         { MANSON_HCS_3202, "HCS-3202",     "3202", { 1, 36, 0.1 }, { 0, 10,   0.10 } },
63         { MANSON_HCS_3204, "HCS-3204",     "3204", { 1, 60, 0.1 }, { 0,  5,   0.01 } },
64         { MANSON_HCS_3300, "HCS-3300-USB", "3300", { 1, 16, 0.1 }, { 0, 30,   0.10 } },
65         { MANSON_HCS_3302, "HCS-3302-USB", "3302", { 1, 32, 0.1 }, { 0, 15,   0.10 } },
66         { MANSON_HCS_3304, "HCS-3304-USB", "3304", { 1, 60, 0.1 }, { 0,  8,   0.10 } },
67         { MANSON_HCS_3400, "HCS-3400-USB", "3400", { 1, 16, 0.1 }, { 0, 40,   0.10 } },
68         { MANSON_HCS_3402, "HCS-3402-USB", "3402", { 1, 32, 0.1 }, { 0, 20,   0.10 } },
69         { MANSON_HCS_3404, "HCS-3404-USB", "3404", { 1, 60, 0.1 }, { 0, 10,   0.10 } },
70         { MANSON_HCS_3600, "HCS-3600-USB", "3600", { 1, 16, 0.1 }, { 0, 60,   0.10 } },
71         { MANSON_HCS_3602, "HCS-3602-USB", "3602", { 1, 32, 0.1 }, { 0, 30,   0.10 } },
72         { MANSON_HCS_3604, "HCS-3604-USB", "3604", { 1, 60, 0.1 }, { 0, 15,   0.10 } },
73         ALL_ZERO
74 };
75
76 SR_PRIV struct sr_dev_driver manson_hcs_3xxx_driver_info;
77
78 static GSList *scan(struct sr_dev_driver *di, GSList *options)
79 {
80         int i, model_id;
81         struct drv_context *drvc;
82         struct dev_context *devc;
83         struct sr_dev_inst *sdi;
84         struct sr_config *src;
85         GSList *devices, *l;
86         const char *conn, *serialcomm;
87         struct sr_serial_dev_inst *serial;
88         char reply[50], **tokens, *dummy;
89
90         drvc = di->context;
91         drvc->instances = NULL;
92         devices = NULL;
93         conn = NULL;
94         serialcomm = NULL;
95         devc = NULL;
96
97         for (l = options; l; l = l->next) {
98                 src = l->data;
99                 switch (src->key) {
100                 case SR_CONF_CONN:
101                         conn = g_variant_get_string(src->data, NULL);
102                         break;
103                 case SR_CONF_SERIALCOMM:
104                         serialcomm = g_variant_get_string(src->data, NULL);
105                         break;
106                 default:
107                         sr_err("Unknown option %d, skipping.", src->key);
108                         break;
109                 }
110         }
111
112         if (!conn)
113                 return NULL;
114         if (!serialcomm)
115                 serialcomm = "9600/8n1";
116
117         serial = sr_serial_dev_inst_new(conn, serialcomm);
118
119         if (serial_open(serial, SERIAL_RDWR) != SR_OK)
120                 return NULL;
121
122         serial_flush(serial);
123
124         sr_info("Probing serial port %s.", conn);
125
126         /* Get the device model. */
127         memset(&reply, 0, sizeof(reply));
128         if ((hcs_send_cmd(serial, "GMOD\r") < 0) ||
129             (hcs_read_reply(serial, 2, reply, sizeof(reply)) < 0))
130                 return NULL;
131         tokens = g_strsplit((const gchar *)&reply, "\r", 2);
132
133         model_id = -1;
134         for (i = 0; models[i].id != NULL; i++) {
135                 if (!strcmp(models[i].id, tokens[0]))
136                         model_id = i;
137         }
138         if (model_id < 0) {
139                 sr_err("Unknown model ID '%s' detected, aborting.", tokens[0]);
140                 g_strfreev(tokens);
141                 return NULL;
142         }
143         g_strfreev(tokens);
144
145         /* Init device instance, etc. */
146         sdi = g_malloc0(sizeof(struct sr_dev_inst));
147         sdi->status = SR_ST_INACTIVE;
148         sdi->vendor = g_strdup("Manson");
149         sdi->model = g_strdup(models[model_id].name);
150         sdi->inst_type = SR_INST_SERIAL;
151         sdi->conn = serial;
152         sdi->driver = di;
153
154         sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "CH1");
155
156         devc = g_malloc0(sizeof(struct dev_context));
157         sr_sw_limits_init(&devc->limits);
158         devc->model = &models[model_id];
159
160         sdi->priv = devc;
161
162         /* Get current voltage, current, status. */
163         if ((hcs_send_cmd(serial, "GETD\r") < 0) ||
164             (hcs_read_reply(serial, 2, reply, sizeof(reply)) < 0))
165                 goto exit_err;
166         tokens = g_strsplit((const gchar *)&reply, "\r", 2);
167         if (hcs_parse_volt_curr_mode(sdi, tokens) < 0) {
168                 g_strfreev(tokens);
169                 goto exit_err;
170         }
171         g_strfreev(tokens);
172
173         /* Get max. voltage and current. */
174         if ((hcs_send_cmd(serial, "GMAX\r") < 0) ||
175             (hcs_read_reply(serial, 2, reply, sizeof(reply)) < 0))
176                 goto exit_err;
177         tokens = g_strsplit((const gchar *)&reply, "\r", 2);
178         devc->current_max_device = g_strtod(&tokens[0][3], &dummy) * devc->model->current[2];
179         tokens[0][3] = '\0';
180         devc->voltage_max_device = g_strtod(tokens[0], &dummy) * devc->model->voltage[2];
181         g_strfreev(tokens);
182
183         drvc->instances = g_slist_append(drvc->instances, sdi);
184         devices = g_slist_append(devices, sdi);
185
186         serial_close(serial);
187         if (!devices)
188                 sr_serial_dev_inst_free(serial);
189
190         return devices;
191
192 exit_err:
193         sr_dev_inst_free(sdi);
194         g_free(devc);
195
196         return NULL;
197 }
198
199 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
200                 const struct sr_channel_group *cg)
201 {
202         struct dev_context *devc;
203
204         (void)cg;
205
206         if (!sdi)
207                 return SR_ERR_ARG;
208
209         devc = sdi->priv;
210
211         switch (key) {
212         case SR_CONF_LIMIT_SAMPLES:
213         case SR_CONF_LIMIT_MSEC:
214                 return sr_sw_limits_config_get(&devc->limits, key, data);
215         case SR_CONF_VOLTAGE:
216                 *data = g_variant_new_double(devc->voltage);
217                 break;
218         case SR_CONF_VOLTAGE_TARGET:
219                 *data = g_variant_new_double(devc->voltage_max);
220                 break;
221         case SR_CONF_CURRENT:
222                 *data = g_variant_new_double(devc->current);
223                 break;
224         case SR_CONF_CURRENT_LIMIT:
225                 *data = g_variant_new_double(devc->current_max);
226                 break;
227         case SR_CONF_ENABLED:
228                 *data = g_variant_new_boolean(devc->output_enabled);
229                 break;
230         default:
231                 return SR_ERR_NA;
232         }
233
234         return SR_OK;
235 }
236
237 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
238                 const struct sr_channel_group *cg)
239 {
240         struct dev_context *devc;
241         gboolean bval;
242         gdouble dval;
243
244         (void)cg;
245
246         if (sdi->status != SR_ST_ACTIVE)
247                 return SR_ERR_DEV_CLOSED;
248
249         devc = sdi->priv;
250
251         switch (key) {
252         case SR_CONF_LIMIT_MSEC:
253                 return sr_sw_limits_config_set(&devc->limits, key, data);
254         case SR_CONF_VOLTAGE_TARGET:
255                 dval = g_variant_get_double(data);
256                 if (dval < devc->model->voltage[0] || dval > devc->voltage_max_device)
257                         return SR_ERR_ARG;
258
259                 if ((hcs_send_cmd(sdi->conn, "VOLT%03.0f\r",
260                         (dval / devc->model->voltage[2])) < 0) ||
261                     (hcs_read_reply(sdi->conn, 1, devc->buf, sizeof(devc->buf)) < 0))
262                         return SR_ERR;
263                 devc->voltage_max = dval;
264                 break;
265         case SR_CONF_CURRENT_LIMIT:
266                 dval = g_variant_get_double(data);
267                 if (dval < devc->model->current[0] || dval > devc->current_max_device)
268                         return SR_ERR_ARG;
269
270                 if ((hcs_send_cmd(sdi->conn, "CURR%03.0f\r",
271                         (dval / devc->model->current[2])) < 0) ||
272                     (hcs_read_reply(sdi->conn, 1, devc->buf, sizeof(devc->buf)) < 0))
273                         return SR_ERR;
274                 devc->current_max = dval;
275                 break;
276         case SR_CONF_ENABLED:
277                 bval = g_variant_get_boolean(data);
278                 if (bval == devc->output_enabled) /* Nothing to do. */
279                         break;
280                 if ((hcs_send_cmd(sdi->conn, "SOUT%1d\r", !bval) < 0) ||
281                     (hcs_read_reply(sdi->conn, 1, devc->buf, sizeof(devc->buf)) < 0))
282                         return SR_ERR;
283                 devc->output_enabled = bval;
284                 break;
285         default:
286                 return SR_ERR_NA;
287         }
288
289         return SR_OK;
290 }
291
292 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
293                 const struct sr_channel_group *cg)
294 {
295         struct dev_context *devc;
296         GVariant *gvar;
297         GVariantBuilder gvb;
298         double dval;
299         int idx;
300
301         (void)cg;
302
303         /* Always available (with or without sdi). */
304         if (key == SR_CONF_SCAN_OPTIONS) {
305                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
306                         scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
307                 return SR_OK;
308         }
309
310         /* Return drvopts without sdi (and devopts with sdi, see below). */
311         if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
312                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
313                                 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
314                 return SR_OK;
315         }
316
317         /* Every other key needs an sdi. */
318         if (!sdi)
319                 return SR_ERR_ARG;
320         devc = sdi->priv;
321
322         switch (key) {
323         case SR_CONF_DEVICE_OPTIONS:
324                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
325                                 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
326                 break;
327         case SR_CONF_VOLTAGE_TARGET:
328                 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
329                 /* Min, max, step. */
330                 for (idx = 0; idx < 3; idx++) {
331                         if (idx == 1)
332                                 dval = devc->voltage_max_device;
333                         else
334                                 dval = devc->model->voltage[idx];
335                         gvar = g_variant_new_double(dval);
336                         g_variant_builder_add_value(&gvb, gvar);
337                 }
338                 *data = g_variant_builder_end(&gvb);
339                 break;
340         case SR_CONF_CURRENT_LIMIT:
341                 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
342                 /* Min, max, step. */
343                 for (idx = 0; idx < 3; idx++) {
344                         if (idx == 1)
345                                 dval = devc->current_max_device;
346                         else
347                                 dval = devc->model->current[idx];
348                         gvar = g_variant_new_double(dval);
349                         g_variant_builder_add_value(&gvb, gvar);
350                 }
351                 *data = g_variant_builder_end(&gvb);
352                 break;
353         default:
354                 return SR_ERR_NA;
355         }
356
357         return SR_OK;
358 }
359
360 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
361 {
362         struct dev_context *devc;
363         struct sr_serial_dev_inst *serial;
364
365         if (sdi->status != SR_ST_ACTIVE)
366                 return SR_ERR_DEV_CLOSED;
367
368         devc = sdi->priv;
369
370         sr_sw_limits_acquisition_start(&devc->limits);
371         std_session_send_df_header(sdi, LOG_PREFIX);
372
373         devc->reply_pending = FALSE;
374         devc->req_sent_at = 0;
375
376         /* Poll every 10ms, or whenever some data comes in. */
377         serial = sdi->conn;
378         serial_source_add(sdi->session, serial, G_IO_IN, 10,
379                         hcs_receive_data, (void *)sdi);
380
381         return SR_OK;
382 }
383
384 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
385 {
386         return std_serial_dev_acquisition_stop(sdi,
387                         std_serial_dev_close, sdi->conn, LOG_PREFIX);
388 }
389
390 SR_PRIV struct sr_dev_driver manson_hcs_3xxx_driver_info = {
391         .name = "manson-hcs-3xxx",
392         .longname = "Manson HCS-3xxx",
393         .api_version = 1,
394         .init = std_init,
395         .cleanup = std_cleanup,
396         .scan = scan,
397         .dev_list = std_dev_list,
398         .config_get = config_get,
399         .config_set = config_set,
400         .config_list = config_list,
401         .dev_open = std_serial_dev_open,
402         .dev_close = std_serial_dev_close,
403         .dev_acquisition_start = dev_acquisition_start,
404         .dev_acquisition_stop = dev_acquisition_stop,
405         .context = NULL,
406 };