2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Uwe Hermann <uwe@hermann-uwe.de>
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 2 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 static const int32_t hwopts[] = {
28 static const int32_t devopts[] = {
30 SR_CONF_LIMIT_SAMPLES,
33 SR_CONF_OUTPUT_VOLTAGE,
34 SR_CONF_OUTPUT_CURRENT,
37 /* Note: All models have one power supply output only. */
38 static struct hcs_model models[] = {
39 { MANSON_HCS_3200, "HCS-3200", { 1, 18, 0.1 }, { 0, 20, 0.01 } },
40 { MANSON_HCS_3202, "HCS-3202", { 1, 36, 0.1 }, { 0, 10, 0.01 } },
41 { MANSON_HCS_3204, "HCS-3204", { 1, 60, 0.1 }, { 0, 5, 0.01 } },
42 { 0, NULL, { 0, 0, 0 }, { 0, 0, 0 }, },
45 SR_PRIV struct sr_dev_driver manson_hcs_3xxx_driver_info;
46 static struct sr_dev_driver *di = &manson_hcs_3xxx_driver_info;
48 static int dev_clear(void)
50 return std_dev_clear(di, NULL);
53 static int init(struct sr_context *sr_ctx)
55 return std_init(sr_ctx, di, LOG_PREFIX);
58 static GSList *scan(GSList *options)
61 struct drv_context *drvc;
62 struct dev_context *devc;
63 struct sr_dev_inst *sdi;
64 struct sr_config *src;
65 struct sr_channel *ch;
67 const char *conn, *serialcomm;
68 struct sr_serial_dev_inst *serial;
69 char reply[50], **tokens;
72 drvc->instances = NULL;
77 for (l = options; l; l = l->next) {
81 conn = g_variant_get_string(src->data, NULL);
83 case SR_CONF_SERIALCOMM:
84 serialcomm = g_variant_get_string(src->data, NULL);
87 sr_err("Unknown option %d, skipping.", src->key);
95 serialcomm = "9600/8n1";
97 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
100 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
103 serial_flush(serial);
105 sr_info("Probing serial port %s.", conn);
107 /* Get the device model. */
108 memset(&reply, 0, sizeof(reply));
109 serial_write_blocking(serial, "GMOD\r", 5);
110 serial_read_blocking(serial, &reply, 8);
111 tokens = g_strsplit((const gchar *)&reply, "\r", 2);
114 for (i = 0; models[i].name != NULL; i++) {
115 if (!strcmp(models[i].name + 4, tokens[0]))
119 sr_err("Unknown model '%s' detected, aborting.", tokens[0]);
123 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Manson",
124 models[model_id].name, NULL))) {
125 sr_err("Failed to create device instance.");
131 sdi->inst_type = SR_INST_SERIAL;
135 if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "CH1"))) {
136 sr_err("Failed to create channel.");
139 sdi->channels = g_slist_append(sdi->channels, ch);
141 devc = g_malloc0(sizeof(struct dev_context));
142 devc->model = &models[model_id];
146 drvc->instances = g_slist_append(drvc->instances, sdi);
147 devices = g_slist_append(devices, sdi);
149 serial_close(serial);
151 sr_serial_dev_inst_free(serial);
156 static GSList *dev_list(void)
158 return ((struct drv_context *)(di->priv))->instances;
161 static int cleanup(void)
166 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
167 const struct sr_channel_group *cg)
169 struct dev_context *devc;
179 case SR_CONF_LIMIT_SAMPLES:
180 *data = g_variant_new_uint64(devc->limit_samples);
182 case SR_CONF_LIMIT_MSEC:
183 *data = g_variant_new_uint64(devc->limit_msec);
185 case SR_CONF_OUTPUT_VOLTAGE:
186 *data = g_variant_new_double(devc->voltage);
188 case SR_CONF_OUTPUT_CURRENT:
189 *data = g_variant_new_double(devc->current);
198 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
199 const struct sr_channel_group *cg)
201 struct dev_context *devc;
205 if (sdi->status != SR_ST_ACTIVE)
206 return SR_ERR_DEV_CLOSED;
211 case SR_CONF_LIMIT_MSEC:
212 if (g_variant_get_uint64(data) == 0)
214 devc->limit_msec = g_variant_get_uint64(data);
216 case SR_CONF_LIMIT_SAMPLES:
217 if (g_variant_get_uint64(data) == 0)
219 devc->limit_samples = g_variant_get_uint64(data);
228 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
229 const struct sr_channel_group *cg)
235 case SR_CONF_SCAN_OPTIONS:
236 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
237 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
239 case SR_CONF_DEVICE_OPTIONS:
240 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
241 devopts, ARRAY_SIZE(devopts), sizeof(int32_t));
250 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
252 struct dev_context *devc;
253 struct sr_serial_dev_inst *serial;
255 if (sdi->status != SR_ST_ACTIVE)
256 return SR_ERR_DEV_CLOSED;
259 devc->cb_data = cb_data;
261 /* Send header packet to the session bus. */
262 std_session_send_df_header(cb_data, LOG_PREFIX);
264 devc->starttime = g_get_monotonic_time();
265 devc->num_samples = 0;
266 devc->reply_pending = FALSE;
267 devc->req_sent_at = 0;
269 /* Poll every 10ms, or whenever some data comes in. */
271 serial_source_add(serial, G_IO_IN, 10, hcs_receive_data, (void *)sdi);
276 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
278 return std_serial_dev_acquisition_stop(sdi, cb_data,
279 std_serial_dev_close, sdi->conn, LOG_PREFIX);
282 SR_PRIV struct sr_dev_driver manson_hcs_3xxx_driver_info = {
283 .name = "manson-hcs-3xxx",
284 .longname = "Manson HCS-3xxx",
289 .dev_list = dev_list,
290 .dev_clear = dev_clear,
291 .config_get = config_get,
292 .config_set = config_set,
293 .config_list = config_list,
294 .dev_open = std_serial_dev_open,
295 .dev_close = std_serial_dev_close,
296 .dev_acquisition_start = dev_acquisition_start,
297 .dev_acquisition_stop = dev_acquisition_stop,