]> sigrok.org Git - libsigrok.git/blame - src/hardware/gmc-mh-1x-2x/api.c
Add helper function for scan completion
[libsigrok.git] / src / hardware / gmc-mh-1x-2x / api.c
CommitLineData
7b4edcb6
MH
1/*
2 * This file is part of the libsigrok project.
3 *
c90beca7 4 * Copyright (C) 2013, 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz>
7b4edcb6
MH
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
6392d599
MH
20/** @file
21 * Gossen Metrawatt Metrahit 1x/2x drivers
c90beca7 22 * @internal
6392d599
MH
23 */
24
6ec6c43b 25#include <config.h>
f5792417 26#include <string.h>
7b4edcb6
MH
27#include "protocol.h"
28
f5792417
MH
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"
6392d599 32#define SERIALCOMM_2X "9600/8n1/dtr=1/rts=1/flow=0"
f5792417
MH
33#define VENDOR_GMC "Gossen Metrawatt"
34
a0e0bb41 35static const uint32_t scanopts[] = {
f5792417
MH
36 SR_CONF_CONN,
37 SR_CONF_SERIALCOMM,
38};
39
c90beca7 40/** Hardware capabilities for Metrahit 1x/2x devices in send mode. */
f254bc4b 41static const uint32_t devopts_sm[] = {
f5792417 42 SR_CONF_MULTIMETER,
6392d599 43 SR_CONF_THERMOMETER, /**< All GMC 1x/2x multimeters seem to support this */
f5792417 44 SR_CONF_CONTINUOUS,
5827f61b
BV
45 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
46 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
f5792417
MH
47};
48
c90beca7 49/** Hardware capabilities for Metrahit 2x devices in bidirectional Mode. */
f254bc4b 50static const uint32_t devopts_bd[] = {
c90beca7
MH
51 SR_CONF_MULTIMETER,
52 SR_CONF_THERMOMETER, /**< All GMC 1x/2x multimeters seem to support this */
c90beca7 53 SR_CONF_CONTINUOUS,
5827f61b
BV
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,
c90beca7
MH
57};
58
6392d599
MH
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
fc348b77
UH
66/**
67 * Read single byte from serial port.
68 *
69 * @retval -1 Timeout or error.
70 * @retval other Byte.
f5792417
MH
71 */
72static int read_byte(struct sr_serial_dev_inst *serial, gint64 timeout)
7b4edcb6 73{
fc348b77 74 uint8_t result = 0;
f5792417
MH
75 int rc = 0;
76
77 for (;;) {
0714a010 78 rc = serial_read_nonblocking(serial, &result, 1);
f5792417
MH
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
fc348b77
UH
89/**
90 * Try to detect GMC 1x/2x multimeter model in send mode for max. 1 second.
f5792417 91 *
fc348b77
UH
92 * @param serial Configured, open serial port.
93 *
94 * @retval NULL Detection failed.
95 * @retval other Model code.
f5792417
MH
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
873e0c12 103 model = METRAHIT_NONE;
1a46cc62 104 timeout_us = g_get_monotonic_time() + (1 * 1000 * 1000);
f5792417 105
fc348b77
UH
106 /*
107 * Try to find message consisting of device code and several
108 * (at least 4) data bytes.
109 */
f5792417
MH
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) {
873e0c12 115 if (!(model = gmc_decode_model_sm(byte & MSGC_MASK)))
f5792417 116 break;
fc348b77 117 /* Now expect (at least) 4 data bytes. */
f5792417
MH
118 for (cnt = 0; cnt < 4; cnt++) {
119 byte = read_byte(serial, timeout_us);
120 if ((byte == -1) ||
c90beca7 121 ((byte & MSGID_MASK) != MSGID_DATA))
f5792417 122 {
873e0c12 123 model = METRAHIT_NONE;
f5792417
MH
124 bytecnt = 100;
125 break;
126 }
127 }
128 break;
129 }
130 }
7b4edcb6 131
f5792417
MH
132 return model;
133}
134
fc348b77
UH
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.
f5792417 143 */
4f840ce9 144static GSList *scan_1x_2x_rs232(struct sr_dev_driver *di, GSList *options)
f5792417
MH
145{
146 struct sr_dev_inst *sdi;
147 struct drv_context *drvc;
148 struct dev_context *devc;
149 struct sr_config *src;
f5792417
MH
150 struct sr_serial_dev_inst *serial;
151 GSList *l, *devices;
152 const char *conn, *serialcomm;
153 enum model model;
154 gboolean serialcomm_given;
7b4edcb6
MH
155
156 devices = NULL;
41812aca 157 drvc = di->context;
f5792417 158 conn = serialcomm = NULL;
f5792417
MH
159 serialcomm_given = FALSE;
160
c90beca7 161 sr_spew("scan_1x_2x_rs232() called!");
f5792417
MH
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
91219afc 180 serial = sr_serial_dev_inst_new(conn, serialcomm);
f5792417 181
e13e354f 182 if (serial_open(serial, SERIAL_RDWR) != SR_OK) {
f5792417
MH
183 sr_serial_dev_inst_free(serial);
184 return NULL;
185 }
7b4edcb6 186
f5792417
MH
187 serial_flush(serial);
188
189 model = scan_model_sm(serial);
190
fc348b77
UH
191 /*
192 * If detection failed and no user-supplied parameters,
193 * try second baud rate.
194 */
873e0c12 195 if ((model == METRAHIT_NONE) && !serialcomm_given) {
f5792417
MH
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
873e0c12
UH
205 if (model != METRAHIT_NONE) {
206 sr_spew("%s %s detected!", VENDOR_GMC, gmc_model_str(model));
aac29cc1 207 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
208 sdi->status = SR_ST_INACTIVE;
209 sdi->vendor = g_strdup(VENDOR_GMC);
210 sdi->model = g_strdup(gmc_model_str(model));
f57d8ffe 211 devc = g_malloc0(sizeof(struct dev_context));
f5792417
MH
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;
f5792417
MH
218 sdi->conn = serial;
219 sdi->priv = devc;
5e23fcab 220 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
f5792417
MH
221 devices = g_slist_append(devices, sdi);
222 }
7b4edcb6 223
15a5bfe4 224 return std_scan_complete(di, devices);
7b4edcb6
MH
225}
226
1a863916
UH
227/**
228 * Scan for Metrahit 2x in a bidirectional mode using Gossen Metrawatt
229 * 'BD 232' interface.
c90beca7 230 */
4f840ce9 231static GSList *scan_2x_bd232(struct sr_dev_driver *di, GSList *options)
c90beca7
MH
232{
233 struct sr_dev_inst *sdi;
234 struct drv_context *drvc;
235 struct dev_context *devc;
236 struct sr_config *src;
c90beca7
MH
237 struct sr_serial_dev_inst *serial;
238 GSList *l, *devices;
239 const char *conn, *serialcomm;
240 int cnt, byte;
241 gint64 timeout_us;
242
243 sdi = NULL;
244 devc = NULL;
245 conn = serialcomm = NULL;
246 devices = NULL;
247
41812aca 248 drvc = di->context;
c90beca7
MH
249
250 sr_spew("scan_2x_bd232() called!");
251
252 for (l = options; l; l = l->next) {
253 src = l->data;
254 switch (src->key) {
255 case SR_CONF_CONN:
256 conn = g_variant_get_string(src->data, NULL);
257 break;
258 case SR_CONF_SERIALCOMM:
259 serialcomm = g_variant_get_string(src->data, NULL);
260 break;
261 }
262 }
263 if (!conn)
264 return NULL;
265 if (!serialcomm)
266 serialcomm = SERIALCOMM_2X;
267
91219afc 268 serial = sr_serial_dev_inst_new(conn, serialcomm);
c90beca7 269
e13e354f 270 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
c90beca7
MH
271 goto exit_err;
272
f57d8ffe 273 devc = g_malloc0(sizeof(struct dev_context));
c90beca7 274
aac29cc1 275 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
276 sdi->status = SR_ST_INACTIVE;
277 sdi->vendor = g_strdup(VENDOR_GMC);
c90beca7
MH
278 sdi->priv = devc;
279
280 /* Send message 03 "Query multimeter version and status" */
281 sdi->conn = serial;
c90beca7
MH
282 if (req_stat14(sdi, TRUE) != SR_OK)
283 goto exit_err;
284
285 /* Wait for reply from device(s) for up to 2s. */
1a46cc62 286 timeout_us = g_get_monotonic_time() + (2 * 1000 * 1000);
c90beca7
MH
287
288 while (timeout_us > g_get_monotonic_time()) {
289 /* Receive reply (14 bytes) */
290 devc->buflen = 0;
07ffa5b3 291 for (cnt = 0; cnt < GMC_REPLY_SIZE; cnt++) {
c90beca7
MH
292 byte = read_byte(serial, timeout_us);
293 if (byte != -1)
294 devc->buf[devc->buflen++] = (byte & MASK_6BITS);
295 }
296
07ffa5b3 297 if (devc->buflen != GMC_REPLY_SIZE)
c90beca7
MH
298 continue;
299
300 devc->addr = devc->buf[0];
301 process_msg14(sdi);
302 devc->buflen = 0;
303
304 if (devc->model != METRAHIT_NONE) {
305 sr_spew("%s %s detected!", VENDOR_GMC, gmc_model_str(devc->model));
c90beca7 306 devc->elapsed_msec = g_timer_new();
c90beca7
MH
307 sdi->model = g_strdup(gmc_model_str(devc->model));
308 sdi->version = g_strdup_printf("Firmware %d.%d", devc->fw_ver_maj, devc->fw_ver_min);
309 sdi->conn = serial;
310 sdi->priv = devc;
5e23fcab 311 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
c90beca7 312 devices = g_slist_append(devices, sdi);
f57d8ffe 313 devc = g_malloc0(sizeof(struct dev_context));
aac29cc1 314 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
315 sdi->status = SR_ST_INACTIVE;
316 sdi->vendor = g_strdup(VENDOR_GMC);
c90beca7
MH
317 }
318 };
319
320 /* Free last alloc if no device found */
321 if (devc->model == METRAHIT_NONE) {
322 g_free(devc);
323 sr_dev_inst_free(sdi);
324 }
325
15a5bfe4 326 return std_scan_complete(di, devices);
c90beca7
MH
327
328exit_err:
329 sr_info("scan_2x_bd232(): Error!");
330
331 if (serial)
332 sr_serial_dev_inst_free(serial);
b1f83103 333 g_free(devc);
c90beca7
MH
334 if (sdi)
335 sr_dev_inst_free(sdi);
336
337 return NULL;
338}
339
7b4edcb6
MH
340static int dev_close(struct sr_dev_inst *sdi)
341{
f5792417 342 struct dev_context *devc;
7b4edcb6 343
bf2c987f 344 std_serial_dev_close(sdi);
7b4edcb6
MH
345
346 sdi->status = SR_ST_INACTIVE;
347
f5792417
MH
348 /* Free dynamically allocated resources. */
349 if ((devc = sdi->priv) && devc->elapsed_msec) {
350 g_timer_destroy(devc->elapsed_msec);
351 devc->elapsed_msec = NULL;
873e0c12 352 devc->model = METRAHIT_NONE;
f5792417
MH
353 }
354
7b4edcb6
MH
355 return SR_OK;
356}
357
584560f1
BV
358static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
359 const struct sr_channel_group *cg)
7b4edcb6
MH
360{
361 int ret;
fadd0707 362 struct dev_context *devc;
c90beca7 363
53b4680f 364 (void)cg;
c90beca7 365
709468ba 366 if (!sdi)
6392d599
MH
367 return SR_ERR_ARG;
368
709468ba
UH
369 devc = sdi->priv;
370
7b4edcb6
MH
371 ret = SR_OK;
372 switch (key) {
6392d599
MH
373 case SR_CONF_LIMIT_SAMPLES:
374 *data = g_variant_new_uint64(devc->limit_samples);
375 break;
376 case SR_CONF_LIMIT_MSEC:
377 *data = g_variant_new_uint64(devc->limit_msec);
378 break;
c90beca7
MH
379 case SR_CONF_POWER_OFF:
380 *data = g_variant_new_boolean(FALSE);
381 break;
7b4edcb6
MH
382 default:
383 return SR_ERR_NA;
384 }
385
386 return ret;
387}
388
1a863916 389/** Implementation of config_list, auxiliary function for common parts. */
584560f1
BV
390static int config_list_common(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
391 const struct sr_channel_group *cg)
7b4edcb6 392{
c90beca7 393 (void)sdi;
53b4680f 394 (void)cg;
7b4edcb6 395
7b4edcb6 396 switch (key) {
c90beca7 397 case SR_CONF_SCAN_OPTIONS:
584560f1 398 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
5827f61b 399 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
f5792417 400 break;
7b4edcb6 401 default:
f5792417 402 return SR_ERR_NA;
7b4edcb6
MH
403 }
404
f5792417 405 return SR_OK;
7b4edcb6
MH
406}
407
c90beca7 408/** Implementation of config_list for Metrahit 1x/2x send mode */
584560f1 409static int config_list_sm(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 410 const struct sr_channel_group *cg)
7b4edcb6 411{
7b4edcb6 412 switch (key) {
c90beca7 413 case SR_CONF_DEVICE_OPTIONS:
584560f1 414 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
5827f61b 415 devopts_sm, ARRAY_SIZE(devopts_sm), sizeof(uint32_t));
f5792417 416 break;
c90beca7 417 default:
53b4680f 418 return config_list_common(key, data, sdi, cg);
c90beca7
MH
419 }
420
421 return SR_OK;
422}
423
424/** Implementation of config_list for Metrahit 2x bidirectional mode */
584560f1 425static int config_list_bd(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 426 const struct sr_channel_group *cg)
c90beca7 427{
c90beca7 428 switch (key) {
f5792417 429 case SR_CONF_DEVICE_OPTIONS:
584560f1 430 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
5827f61b 431 devopts_bd, ARRAY_SIZE(devopts_bd), sizeof(uint32_t));
f5792417 432 break;
7b4edcb6 433 default:
53b4680f 434 return config_list_common(key, data, sdi, cg);
7b4edcb6
MH
435 }
436
f5792417 437 return SR_OK;
7b4edcb6
MH
438}
439
695dc859 440static int dev_acquisition_start_1x_2x_rs232(const struct sr_dev_inst *sdi)
7b4edcb6 441{
f5792417
MH
442 struct dev_context *devc;
443 struct sr_serial_dev_inst *serial;
444
7b4edcb6
MH
445 if (sdi->status != SR_ST_ACTIVE)
446 return SR_ERR_DEV_CLOSED;
447
208c1d35 448 devc = sdi->priv;
f5792417
MH
449 devc->settings_ok = FALSE;
450 devc->buflen = 0;
451
695dc859 452 std_session_send_df_header(sdi, LOG_PREFIX);
f5792417
MH
453
454 /* Start timer, if required. */
455 if (devc->limit_msec)
456 g_timer_start(devc->elapsed_msec);
457
458 /* Poll every 40ms, or whenever some data comes in. */
459 serial = sdi->conn;
102f1239
BV
460 serial_source_add(sdi->session, serial, G_IO_IN, 40,
461 gmc_mh_1x_2x_receive_data, (void *)sdi);
7b4edcb6
MH
462
463 return SR_OK;
464}
465
695dc859 466static int dev_acquisition_start_2x_bd232(const struct sr_dev_inst *sdi)
c90beca7
MH
467{
468 struct dev_context *devc;
469 struct sr_serial_dev_inst *serial;
470
c90beca7
MH
471 if (sdi->status != SR_ST_ACTIVE)
472 return SR_ERR_DEV_CLOSED;
473
208c1d35 474 devc = sdi->priv;
c90beca7
MH
475 devc->settings_ok = FALSE;
476 devc->buflen = 0;
477
695dc859 478 std_session_send_df_header(sdi, LOG_PREFIX);
c90beca7
MH
479
480 /* Start timer, if required. */
481 if (devc->limit_msec)
482 g_timer_start(devc->elapsed_msec);
483
484 /* Poll every 40ms, or whenever some data comes in. */
485 serial = sdi->conn;
102f1239
BV
486 serial_source_add(sdi->session, serial, G_IO_IN, 40,
487 gmc_mh_2x_receive_data, (void *)sdi);
c90beca7
MH
488
489 /* Send start message */
490 return req_meas14(sdi);
491}
492
695dc859 493static int dev_acquisition_stop(struct sr_dev_inst *sdi)
7b4edcb6 494{
f5792417 495 struct dev_context *devc;
7b4edcb6 496
f5792417
MH
497 /* Stop timer, if required. */
498 if (sdi && (devc = sdi->priv) && devc->limit_msec)
499 g_timer_stop(devc->elapsed_msec);
7b4edcb6 500
6525d819 501 return std_serial_dev_acquisition_stop(sdi, dev_close,
f5792417 502 sdi->conn, LOG_PREFIX);
7b4edcb6
MH
503}
504
dd5c48a6 505static struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info = {
7b4edcb6 506 .name = "gmc-mh-1x-2x-rs232",
a90061e5 507 .longname = "Gossen Metrawatt Metrahit 1x/2x, RS232 interface",
7b4edcb6 508 .api_version = 1,
c2fdcc25 509 .init = std_init,
700d6b64 510 .cleanup = std_cleanup,
c90beca7 511 .scan = scan_1x_2x_rs232,
c01bf34c 512 .dev_list = std_dev_list,
a6630742 513 .dev_clear = NULL,
c90beca7
MH
514 .config_get = config_get,
515 .config_set = config_set,
516 .config_list = config_list_sm,
517 .dev_open = std_serial_dev_open,
518 .dev_close = dev_close,
519 .dev_acquisition_start = dev_acquisition_start_1x_2x_rs232,
520 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 521 .context = NULL,
c90beca7 522};
dd5c48a6 523SR_REGISTER_DEV_DRIVER(gmc_mh_1x_2x_rs232_driver_info);
c90beca7 524
dd5c48a6 525static struct sr_dev_driver gmc_mh_2x_bd232_driver_info = {
c90beca7 526 .name = "gmc-mh-2x-bd232",
a90061e5 527 .longname = "Gossen Metrawatt Metrahit 2x, BD232/SI232-II interface",
c90beca7 528 .api_version = 1,
c2fdcc25 529 .init = std_init,
700d6b64 530 .cleanup = std_cleanup,
c90beca7 531 .scan = scan_2x_bd232,
c01bf34c 532 .dev_list = std_dev_list,
a6630742 533 .dev_clear = NULL,
7b4edcb6
MH
534 .config_get = config_get,
535 .config_set = config_set,
c90beca7 536 .config_list = config_list_bd,
854434de 537 .dev_open = std_serial_dev_open,
7b4edcb6 538 .dev_close = dev_close,
c90beca7 539 .dev_acquisition_start = dev_acquisition_start_2x_bd232,
27fd2eaa 540 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 541 .context = NULL,
7b4edcb6 542};
dd5c48a6 543SR_REGISTER_DEV_DRIVER(gmc_mh_2x_bd232_driver_info);