]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/gmc-mh-1x-2x/api.c
Don't reset instance list in scan() callback
[libsigrok.git] / src / hardware / gmc-mh-1x-2x / api.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013, 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz>
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/** @file
21 * Gossen Metrawatt Metrahit 1x/2x drivers
22 * @internal
23 */
24
25#include <config.h>
26#include <string.h>
27#include "protocol.h"
28
29/* Serial communication parameters for Metrahit 1x/2x with 'RS232' adaptor */
30#define SERIALCOMM_1X_RS232 "8228/6n1/dtr=1/rts=1/flow=0" /* =8192, closer with divider */
31#define SERIALCOMM_2X_RS232 "9600/6n1/dtr=1/rts=1/flow=0"
32#define SERIALCOMM_2X "9600/8n1/dtr=1/rts=1/flow=0"
33#define VENDOR_GMC "Gossen Metrawatt"
34
35static const uint32_t scanopts[] = {
36 SR_CONF_CONN,
37 SR_CONF_SERIALCOMM,
38};
39
40/** Hardware capabilities for Metrahit 1x/2x devices in send mode. */
41static const uint32_t devopts_sm[] = {
42 SR_CONF_MULTIMETER,
43 SR_CONF_THERMOMETER, /**< All GMC 1x/2x multimeters seem to support this */
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};
48
49/** Hardware capabilities for Metrahit 2x devices in bidirectional Mode. */
50static const uint32_t devopts_bd[] = {
51 SR_CONF_MULTIMETER,
52 SR_CONF_THERMOMETER, /**< All GMC 1x/2x multimeters seem to support this */
53 SR_CONF_CONTINUOUS,
54 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
55 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
56 SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET,
57};
58
59/* TODO:
60 * - For the 29S SR_CONF_ENERGYMETER, too.
61 * - SR_CONF_PATTERN_MODE for some 2x devices
62 * - SR_CONF_DATALOG for 22M, 26M, 29S and storage adaptors.
63 * Need to implement device-specific lists.
64 */
65
66/**
67 * Read single byte from serial port.
68 *
69 * @retval -1 Timeout or error.
70 * @retval other Byte.
71 */
72static int read_byte(struct sr_serial_dev_inst *serial, gint64 timeout)
73{
74 uint8_t result = 0;
75 int rc = 0;
76
77 for (;;) {
78 rc = serial_read_nonblocking(serial, &result, 1);
79 if (rc == 1) {
80 sr_spew("read: 0x%02x/%d", result, result);
81 return result;
82 }
83 if (g_get_monotonic_time() > timeout)
84 return -1;
85 g_usleep(2000);
86 }
87}
88
89/**
90 * Try to detect GMC 1x/2x multimeter model in send mode for max. 1 second.
91 *
92 * @param serial Configured, open serial port.
93 *
94 * @retval NULL Detection failed.
95 * @retval other Model code.
96 */
97static enum model scan_model_sm(struct sr_serial_dev_inst *serial)
98{
99 int byte, bytecnt, cnt;
100 enum model model;
101 gint64 timeout_us;
102
103 model = METRAHIT_NONE;
104 timeout_us = g_get_monotonic_time() + (1 * 1000 * 1000);
105
106 /*
107 * Try to find message consisting of device code and several
108 * (at least 4) data bytes.
109 */
110 for (bytecnt = 0; bytecnt < 100; bytecnt++) {
111 byte = read_byte(serial, timeout_us);
112 if ((byte == -1) || (timeout_us < g_get_monotonic_time()))
113 break;
114 if ((byte & MSGID_MASK) == MSGID_INF) {
115 if (!(model = gmc_decode_model_sm(byte & MSGC_MASK)))
116 break;
117 /* Now expect (at least) 4 data bytes. */
118 for (cnt = 0; cnt < 4; cnt++) {
119 byte = read_byte(serial, timeout_us);
120 if ((byte == -1) ||
121 ((byte & MSGID_MASK) != MSGID_DATA))
122 {
123 model = METRAHIT_NONE;
124 bytecnt = 100;
125 break;
126 }
127 }
128 break;
129 }
130 }
131
132 return model;
133}
134
135/**
136 * Scan for Metrahit 1x and Metrahit 2x in send mode using Gossen Metrawatt
137 * 'RS232' interface.
138 *
139 * The older 1x models use 8192 baud and the newer 2x 9600 baud.
140 * The DMM usually sends up to about 20 messages per second. However, depending
141 * on configuration and measurement mode the intervals can be much larger and
142 * then the detection might not work.
143 */
144static GSList *scan_1x_2x_rs232(struct sr_dev_driver *di, GSList *options)
145{
146 struct sr_dev_inst *sdi;
147 struct drv_context *drvc;
148 struct dev_context *devc;
149 struct sr_config *src;
150 struct sr_serial_dev_inst *serial;
151 GSList *l, *devices;
152 const char *conn, *serialcomm;
153 enum model model;
154 gboolean serialcomm_given;
155
156 devices = NULL;
157 drvc = di->context;
158 conn = serialcomm = NULL;
159 serialcomm_given = FALSE;
160
161 sr_spew("scan_1x_2x_rs232() called!");
162
163 for (l = options; l; l = l->next) {
164 src = l->data;
165 switch (src->key) {
166 case SR_CONF_CONN:
167 conn = g_variant_get_string(src->data, NULL);
168 break;
169 case SR_CONF_SERIALCOMM:
170 serialcomm = g_variant_get_string(src->data, NULL);
171 serialcomm_given = TRUE;
172 break;
173 }
174 }
175 if (!conn)
176 return NULL;
177 if (!serialcomm)
178 serialcomm = SERIALCOMM_2X_RS232;
179
180 serial = sr_serial_dev_inst_new(conn, serialcomm);
181
182 if (serial_open(serial, SERIAL_RDWR) != SR_OK) {
183 sr_serial_dev_inst_free(serial);
184 return NULL;
185 }
186
187 serial_flush(serial);
188
189 model = scan_model_sm(serial);
190
191 /*
192 * If detection failed and no user-supplied parameters,
193 * try second baud rate.
194 */
195 if ((model == METRAHIT_NONE) && !serialcomm_given) {
196 serialcomm = SERIALCOMM_1X_RS232;
197 g_free(serial->serialcomm);
198 serial->serialcomm = g_strdup(serialcomm);
199 if (serial_set_paramstr(serial, serialcomm) == SR_OK) {
200 serial_flush(serial);
201 model = scan_model_sm(serial);
202 }
203 }
204
205 if (model != METRAHIT_NONE) {
206 sr_spew("%s %s detected!", VENDOR_GMC, gmc_model_str(model));
207 sdi = g_malloc0(sizeof(struct sr_dev_inst));
208 sdi->status = SR_ST_INACTIVE;
209 sdi->vendor = g_strdup(VENDOR_GMC);
210 sdi->model = g_strdup(gmc_model_str(model));
211 devc = g_malloc0(sizeof(struct dev_context));
212 devc->model = model;
213 devc->limit_samples = 0;
214 devc->limit_msec = 0;
215 devc->num_samples = 0;
216 devc->elapsed_msec = g_timer_new();
217 devc->settings_ok = FALSE;
218 sdi->conn = serial;
219 sdi->priv = devc;
220 sdi->driver = di;
221 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
222 drvc->instances = g_slist_append(drvc->instances, sdi);
223 devices = g_slist_append(devices, sdi);
224 }
225
226 return devices;
227}
228
229/**
230 * Scan for Metrahit 2x in a bidirectional mode using Gossen Metrawatt
231 * 'BD 232' interface.
232 */
233static GSList *scan_2x_bd232(struct sr_dev_driver *di, GSList *options)
234{
235 struct sr_dev_inst *sdi;
236 struct drv_context *drvc;
237 struct dev_context *devc;
238 struct sr_config *src;
239 struct sr_serial_dev_inst *serial;
240 GSList *l, *devices;
241 const char *conn, *serialcomm;
242 int cnt, byte;
243 gint64 timeout_us;
244
245 sdi = NULL;
246 devc = NULL;
247 conn = serialcomm = NULL;
248 devices = NULL;
249
250 drvc = di->context;
251
252 sr_spew("scan_2x_bd232() called!");
253
254 for (l = options; l; l = l->next) {
255 src = l->data;
256 switch (src->key) {
257 case SR_CONF_CONN:
258 conn = g_variant_get_string(src->data, NULL);
259 break;
260 case SR_CONF_SERIALCOMM:
261 serialcomm = g_variant_get_string(src->data, NULL);
262 break;
263 }
264 }
265 if (!conn)
266 return NULL;
267 if (!serialcomm)
268 serialcomm = SERIALCOMM_2X;
269
270 serial = sr_serial_dev_inst_new(conn, serialcomm);
271
272 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
273 goto exit_err;
274
275 devc = g_malloc0(sizeof(struct dev_context));
276
277 sdi = g_malloc0(sizeof(struct sr_dev_inst));
278 sdi->status = SR_ST_INACTIVE;
279 sdi->vendor = g_strdup(VENDOR_GMC);
280 sdi->priv = devc;
281
282 /* Send message 03 "Query multimeter version and status" */
283 sdi->conn = serial;
284 if (req_stat14(sdi, TRUE) != SR_OK)
285 goto exit_err;
286
287 /* Wait for reply from device(s) for up to 2s. */
288 timeout_us = g_get_monotonic_time() + (2 * 1000 * 1000);
289
290 while (timeout_us > g_get_monotonic_time()) {
291 /* Receive reply (14 bytes) */
292 devc->buflen = 0;
293 for (cnt = 0; cnt < GMC_REPLY_SIZE; cnt++) {
294 byte = read_byte(serial, timeout_us);
295 if (byte != -1)
296 devc->buf[devc->buflen++] = (byte & MASK_6BITS);
297 }
298
299 if (devc->buflen != GMC_REPLY_SIZE)
300 continue;
301
302 devc->addr = devc->buf[0];
303 process_msg14(sdi);
304 devc->buflen = 0;
305
306 if (devc->model != METRAHIT_NONE) {
307 sr_spew("%s %s detected!", VENDOR_GMC, gmc_model_str(devc->model));
308 devc->elapsed_msec = g_timer_new();
309 sdi->model = g_strdup(gmc_model_str(devc->model));
310 sdi->version = g_strdup_printf("Firmware %d.%d", devc->fw_ver_maj, devc->fw_ver_min);
311 sdi->conn = serial;
312 sdi->priv = devc;
313 sdi->driver = di;
314 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
315 drvc->instances = g_slist_append(drvc->instances, sdi);
316 devices = g_slist_append(devices, sdi);
317 devc = g_malloc0(sizeof(struct dev_context));
318 sdi = g_malloc0(sizeof(struct sr_dev_inst));
319 sdi->status = SR_ST_INACTIVE;
320 sdi->vendor = g_strdup(VENDOR_GMC);
321 }
322 };
323
324 /* Free last alloc if no device found */
325 if (devc->model == METRAHIT_NONE) {
326 g_free(devc);
327 sr_dev_inst_free(sdi);
328 }
329
330 return devices;
331
332exit_err:
333 sr_info("scan_2x_bd232(): Error!");
334
335 if (serial)
336 sr_serial_dev_inst_free(serial);
337 g_free(devc);
338 if (sdi)
339 sr_dev_inst_free(sdi);
340
341 return NULL;
342}
343
344static int dev_close(struct sr_dev_inst *sdi)
345{
346 struct dev_context *devc;
347
348 std_serial_dev_close(sdi);
349
350 sdi->status = SR_ST_INACTIVE;
351
352 /* Free dynamically allocated resources. */
353 if ((devc = sdi->priv) && devc->elapsed_msec) {
354 g_timer_destroy(devc->elapsed_msec);
355 devc->elapsed_msec = NULL;
356 devc->model = METRAHIT_NONE;
357 }
358
359 return SR_OK;
360}
361
362static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
363 const struct sr_channel_group *cg)
364{
365 int ret;
366 struct dev_context *devc;
367
368 (void)cg;
369
370 if (!sdi)
371 return SR_ERR_ARG;
372
373 devc = sdi->priv;
374
375 ret = SR_OK;
376 switch (key) {
377 case SR_CONF_LIMIT_SAMPLES:
378 *data = g_variant_new_uint64(devc->limit_samples);
379 break;
380 case SR_CONF_LIMIT_MSEC:
381 *data = g_variant_new_uint64(devc->limit_msec);
382 break;
383 case SR_CONF_POWER_OFF:
384 *data = g_variant_new_boolean(FALSE);
385 break;
386 default:
387 return SR_ERR_NA;
388 }
389
390 return ret;
391}
392
393/** Implementation of config_list, auxiliary function for common parts. */
394static int config_list_common(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
395 const struct sr_channel_group *cg)
396{
397 (void)sdi;
398 (void)cg;
399
400 switch (key) {
401 case SR_CONF_SCAN_OPTIONS:
402 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
403 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
404 break;
405 default:
406 return SR_ERR_NA;
407 }
408
409 return SR_OK;
410}
411
412/** Implementation of config_list for Metrahit 1x/2x send mode */
413static int config_list_sm(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
414 const struct sr_channel_group *cg)
415{
416 switch (key) {
417 case SR_CONF_DEVICE_OPTIONS:
418 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
419 devopts_sm, ARRAY_SIZE(devopts_sm), sizeof(uint32_t));
420 break;
421 default:
422 return config_list_common(key, data, sdi, cg);
423 }
424
425 return SR_OK;
426}
427
428/** Implementation of config_list for Metrahit 2x bidirectional mode */
429static int config_list_bd(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
430 const struct sr_channel_group *cg)
431{
432 switch (key) {
433 case SR_CONF_DEVICE_OPTIONS:
434 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
435 devopts_bd, ARRAY_SIZE(devopts_bd), sizeof(uint32_t));
436 break;
437 default:
438 return config_list_common(key, data, sdi, cg);
439 }
440
441 return SR_OK;
442}
443
444static int dev_acquisition_start_1x_2x_rs232(const struct sr_dev_inst *sdi)
445{
446 struct dev_context *devc;
447 struct sr_serial_dev_inst *serial;
448
449 if (sdi->status != SR_ST_ACTIVE)
450 return SR_ERR_DEV_CLOSED;
451
452 devc = sdi->priv;
453 devc->settings_ok = FALSE;
454 devc->buflen = 0;
455
456 std_session_send_df_header(sdi, LOG_PREFIX);
457
458 /* Start timer, if required. */
459 if (devc->limit_msec)
460 g_timer_start(devc->elapsed_msec);
461
462 /* Poll every 40ms, or whenever some data comes in. */
463 serial = sdi->conn;
464 serial_source_add(sdi->session, serial, G_IO_IN, 40,
465 gmc_mh_1x_2x_receive_data, (void *)sdi);
466
467 return SR_OK;
468}
469
470static int dev_acquisition_start_2x_bd232(const struct sr_dev_inst *sdi)
471{
472 struct dev_context *devc;
473 struct sr_serial_dev_inst *serial;
474
475 if (sdi->status != SR_ST_ACTIVE)
476 return SR_ERR_DEV_CLOSED;
477
478 devc = sdi->priv;
479 devc->settings_ok = FALSE;
480 devc->buflen = 0;
481
482 std_session_send_df_header(sdi, LOG_PREFIX);
483
484 /* Start timer, if required. */
485 if (devc->limit_msec)
486 g_timer_start(devc->elapsed_msec);
487
488 /* Poll every 40ms, or whenever some data comes in. */
489 serial = sdi->conn;
490 serial_source_add(sdi->session, serial, G_IO_IN, 40,
491 gmc_mh_2x_receive_data, (void *)sdi);
492
493 /* Send start message */
494 return req_meas14(sdi);
495}
496
497static int dev_acquisition_stop(struct sr_dev_inst *sdi)
498{
499 struct dev_context *devc;
500
501 /* Stop timer, if required. */
502 if (sdi && (devc = sdi->priv) && devc->limit_msec)
503 g_timer_stop(devc->elapsed_msec);
504
505 return std_serial_dev_acquisition_stop(sdi, dev_close,
506 sdi->conn, LOG_PREFIX);
507}
508
509static struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info = {
510 .name = "gmc-mh-1x-2x-rs232",
511 .longname = "Gossen Metrawatt Metrahit 1x/2x, RS232 interface",
512 .api_version = 1,
513 .init = std_init,
514 .cleanup = std_cleanup,
515 .scan = scan_1x_2x_rs232,
516 .dev_list = std_dev_list,
517 .dev_clear = NULL,
518 .config_get = config_get,
519 .config_set = config_set,
520 .config_list = config_list_sm,
521 .dev_open = std_serial_dev_open,
522 .dev_close = dev_close,
523 .dev_acquisition_start = dev_acquisition_start_1x_2x_rs232,
524 .dev_acquisition_stop = dev_acquisition_stop,
525 .context = NULL,
526};
527SR_REGISTER_DEV_DRIVER(gmc_mh_1x_2x_rs232_driver_info);
528
529static struct sr_dev_driver gmc_mh_2x_bd232_driver_info = {
530 .name = "gmc-mh-2x-bd232",
531 .longname = "Gossen Metrawatt Metrahit 2x, BD232/SI232-II interface",
532 .api_version = 1,
533 .init = std_init,
534 .cleanup = std_cleanup,
535 .scan = scan_2x_bd232,
536 .dev_list = std_dev_list,
537 .dev_clear = NULL,
538 .config_get = config_get,
539 .config_set = config_set,
540 .config_list = config_list_bd,
541 .dev_open = std_serial_dev_open,
542 .dev_close = dev_close,
543 .dev_acquisition_start = dev_acquisition_start_2x_bd232,
544 .dev_acquisition_stop = dev_acquisition_stop,
545 .context = NULL,
546};
547SR_REGISTER_DEV_DRIVER(gmc_mh_2x_bd232_driver_info);