]> sigrok.org Git - libsigrok.git/blame - src/hardware/gmc-mh-1x-2x/api.c
std_serial_dev_acquisition_stop(): Remove serial parameter
[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;
f5792417
MH
147 struct dev_context *devc;
148 struct sr_config *src;
f5792417
MH
149 struct sr_serial_dev_inst *serial;
150 GSList *l, *devices;
151 const char *conn, *serialcomm;
152 enum model model;
153 gboolean serialcomm_given;
7b4edcb6
MH
154
155 devices = NULL;
f5792417 156 conn = serialcomm = NULL;
f5792417
MH
157 serialcomm_given = FALSE;
158
c90beca7 159 sr_spew("scan_1x_2x_rs232() called!");
f5792417
MH
160
161 for (l = options; l; l = l->next) {
162 src = l->data;
163 switch (src->key) {
164 case SR_CONF_CONN:
165 conn = g_variant_get_string(src->data, NULL);
166 break;
167 case SR_CONF_SERIALCOMM:
168 serialcomm = g_variant_get_string(src->data, NULL);
169 serialcomm_given = TRUE;
170 break;
171 }
172 }
173 if (!conn)
174 return NULL;
175 if (!serialcomm)
176 serialcomm = SERIALCOMM_2X_RS232;
177
91219afc 178 serial = sr_serial_dev_inst_new(conn, serialcomm);
f5792417 179
e13e354f 180 if (serial_open(serial, SERIAL_RDWR) != SR_OK) {
f5792417
MH
181 sr_serial_dev_inst_free(serial);
182 return NULL;
183 }
7b4edcb6 184
f5792417
MH
185 serial_flush(serial);
186
187 model = scan_model_sm(serial);
188
fc348b77
UH
189 /*
190 * If detection failed and no user-supplied parameters,
191 * try second baud rate.
192 */
873e0c12 193 if ((model == METRAHIT_NONE) && !serialcomm_given) {
f5792417
MH
194 serialcomm = SERIALCOMM_1X_RS232;
195 g_free(serial->serialcomm);
196 serial->serialcomm = g_strdup(serialcomm);
197 if (serial_set_paramstr(serial, serialcomm) == SR_OK) {
198 serial_flush(serial);
199 model = scan_model_sm(serial);
200 }
201 }
202
873e0c12
UH
203 if (model != METRAHIT_NONE) {
204 sr_spew("%s %s detected!", VENDOR_GMC, gmc_model_str(model));
aac29cc1 205 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
206 sdi->status = SR_ST_INACTIVE;
207 sdi->vendor = g_strdup(VENDOR_GMC);
208 sdi->model = g_strdup(gmc_model_str(model));
f57d8ffe 209 devc = g_malloc0(sizeof(struct dev_context));
3c413879 210 sr_sw_limits_init(&devc->limits);
f5792417 211 devc->model = model;
f5792417 212 devc->settings_ok = FALSE;
f5792417
MH
213 sdi->conn = serial;
214 sdi->priv = devc;
5e23fcab 215 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
f5792417
MH
216 devices = g_slist_append(devices, sdi);
217 }
7b4edcb6 218
15a5bfe4 219 return std_scan_complete(di, devices);
7b4edcb6
MH
220}
221
1a863916
UH
222/**
223 * Scan for Metrahit 2x in a bidirectional mode using Gossen Metrawatt
224 * 'BD 232' interface.
c90beca7 225 */
4f840ce9 226static GSList *scan_2x_bd232(struct sr_dev_driver *di, GSList *options)
c90beca7
MH
227{
228 struct sr_dev_inst *sdi;
c90beca7
MH
229 struct dev_context *devc;
230 struct sr_config *src;
c90beca7
MH
231 struct sr_serial_dev_inst *serial;
232 GSList *l, *devices;
233 const char *conn, *serialcomm;
234 int cnt, byte;
235 gint64 timeout_us;
236
237 sdi = NULL;
238 devc = NULL;
239 conn = serialcomm = NULL;
240 devices = NULL;
241
c90beca7
MH
242 sr_spew("scan_2x_bd232() called!");
243
244 for (l = options; l; l = l->next) {
245 src = l->data;
246 switch (src->key) {
247 case SR_CONF_CONN:
248 conn = g_variant_get_string(src->data, NULL);
249 break;
250 case SR_CONF_SERIALCOMM:
251 serialcomm = g_variant_get_string(src->data, NULL);
252 break;
253 }
254 }
255 if (!conn)
256 return NULL;
257 if (!serialcomm)
258 serialcomm = SERIALCOMM_2X;
259
91219afc 260 serial = sr_serial_dev_inst_new(conn, serialcomm);
c90beca7 261
e13e354f 262 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
c90beca7
MH
263 goto exit_err;
264
f57d8ffe 265 devc = g_malloc0(sizeof(struct dev_context));
c90beca7 266
aac29cc1 267 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
268 sdi->status = SR_ST_INACTIVE;
269 sdi->vendor = g_strdup(VENDOR_GMC);
c90beca7
MH
270 sdi->priv = devc;
271
272 /* Send message 03 "Query multimeter version and status" */
273 sdi->conn = serial;
c90beca7
MH
274 if (req_stat14(sdi, TRUE) != SR_OK)
275 goto exit_err;
276
277 /* Wait for reply from device(s) for up to 2s. */
1a46cc62 278 timeout_us = g_get_monotonic_time() + (2 * 1000 * 1000);
c90beca7
MH
279
280 while (timeout_us > g_get_monotonic_time()) {
281 /* Receive reply (14 bytes) */
282 devc->buflen = 0;
07ffa5b3 283 for (cnt = 0; cnt < GMC_REPLY_SIZE; cnt++) {
c90beca7
MH
284 byte = read_byte(serial, timeout_us);
285 if (byte != -1)
286 devc->buf[devc->buflen++] = (byte & MASK_6BITS);
287 }
288
07ffa5b3 289 if (devc->buflen != GMC_REPLY_SIZE)
c90beca7
MH
290 continue;
291
292 devc->addr = devc->buf[0];
293 process_msg14(sdi);
294 devc->buflen = 0;
295
296 if (devc->model != METRAHIT_NONE) {
297 sr_spew("%s %s detected!", VENDOR_GMC, gmc_model_str(devc->model));
3c413879 298 sr_sw_limits_init(&devc->limits);
c90beca7
MH
299 sdi->model = g_strdup(gmc_model_str(devc->model));
300 sdi->version = g_strdup_printf("Firmware %d.%d", devc->fw_ver_maj, devc->fw_ver_min);
301 sdi->conn = serial;
302 sdi->priv = devc;
5e23fcab 303 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
c90beca7 304 devices = g_slist_append(devices, sdi);
f57d8ffe 305 devc = g_malloc0(sizeof(struct dev_context));
aac29cc1 306 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
307 sdi->status = SR_ST_INACTIVE;
308 sdi->vendor = g_strdup(VENDOR_GMC);
c90beca7
MH
309 }
310 };
311
312 /* Free last alloc if no device found */
313 if (devc->model == METRAHIT_NONE) {
314 g_free(devc);
315 sr_dev_inst_free(sdi);
316 }
317
15a5bfe4 318 return std_scan_complete(di, devices);
c90beca7
MH
319
320exit_err:
321 sr_info("scan_2x_bd232(): Error!");
322
323 if (serial)
324 sr_serial_dev_inst_free(serial);
b1f83103 325 g_free(devc);
c90beca7
MH
326 if (sdi)
327 sr_dev_inst_free(sdi);
328
329 return NULL;
330}
331
7b4edcb6
MH
332static int dev_close(struct sr_dev_inst *sdi)
333{
f5792417 334 struct dev_context *devc;
7b4edcb6 335
bf2c987f 336 std_serial_dev_close(sdi);
7b4edcb6
MH
337
338 sdi->status = SR_ST_INACTIVE;
3c413879 339 if ((devc = sdi->priv))
873e0c12 340 devc->model = METRAHIT_NONE;
f5792417 341
7b4edcb6
MH
342 return SR_OK;
343}
344
584560f1
BV
345static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
346 const struct sr_channel_group *cg)
7b4edcb6
MH
347{
348 int ret;
fadd0707 349 struct dev_context *devc;
c90beca7 350
53b4680f 351 (void)cg;
c90beca7 352
709468ba 353 if (!sdi)
6392d599
MH
354 return SR_ERR_ARG;
355
709468ba
UH
356 devc = sdi->priv;
357
7b4edcb6
MH
358 ret = SR_OK;
359 switch (key) {
6392d599 360 case SR_CONF_LIMIT_SAMPLES:
6392d599 361 case SR_CONF_LIMIT_MSEC:
3c413879 362 return sr_sw_limits_config_get(&devc->limits, key, data);
c90beca7
MH
363 case SR_CONF_POWER_OFF:
364 *data = g_variant_new_boolean(FALSE);
365 break;
7b4edcb6
MH
366 default:
367 return SR_ERR_NA;
368 }
369
370 return ret;
371}
372
1a863916 373/** Implementation of config_list, auxiliary function for common parts. */
584560f1
BV
374static int config_list_common(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
375 const struct sr_channel_group *cg)
7b4edcb6 376{
c90beca7 377 (void)sdi;
53b4680f 378 (void)cg;
7b4edcb6 379
7b4edcb6 380 switch (key) {
c90beca7 381 case SR_CONF_SCAN_OPTIONS:
584560f1 382 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
5827f61b 383 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
f5792417 384 break;
7b4edcb6 385 default:
f5792417 386 return SR_ERR_NA;
7b4edcb6
MH
387 }
388
f5792417 389 return SR_OK;
7b4edcb6
MH
390}
391
c90beca7 392/** Implementation of config_list for Metrahit 1x/2x send mode */
584560f1 393static int config_list_sm(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 394 const struct sr_channel_group *cg)
7b4edcb6 395{
7b4edcb6 396 switch (key) {
c90beca7 397 case SR_CONF_DEVICE_OPTIONS:
584560f1 398 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
5827f61b 399 devopts_sm, ARRAY_SIZE(devopts_sm), sizeof(uint32_t));
f5792417 400 break;
c90beca7 401 default:
53b4680f 402 return config_list_common(key, data, sdi, cg);
c90beca7
MH
403 }
404
405 return SR_OK;
406}
407
408/** Implementation of config_list for Metrahit 2x bidirectional mode */
584560f1 409static int config_list_bd(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 410 const struct sr_channel_group *cg)
c90beca7 411{
c90beca7 412 switch (key) {
f5792417 413 case SR_CONF_DEVICE_OPTIONS:
584560f1 414 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
5827f61b 415 devopts_bd, ARRAY_SIZE(devopts_bd), sizeof(uint32_t));
f5792417 416 break;
7b4edcb6 417 default:
53b4680f 418 return config_list_common(key, data, sdi, cg);
7b4edcb6
MH
419 }
420
f5792417 421 return SR_OK;
7b4edcb6
MH
422}
423
695dc859 424static int dev_acquisition_start_1x_2x_rs232(const struct sr_dev_inst *sdi)
7b4edcb6 425{
f5792417
MH
426 struct dev_context *devc;
427 struct sr_serial_dev_inst *serial;
428
7b4edcb6
MH
429 if (sdi->status != SR_ST_ACTIVE)
430 return SR_ERR_DEV_CLOSED;
431
208c1d35 432 devc = sdi->priv;
f5792417
MH
433 devc->settings_ok = FALSE;
434 devc->buflen = 0;
435
3c413879 436 sr_sw_limits_acquisition_start(&devc->limits);
bee2b016 437 std_session_send_df_header(sdi);
f5792417 438
f5792417
MH
439 /* Poll every 40ms, or whenever some data comes in. */
440 serial = sdi->conn;
102f1239
BV
441 serial_source_add(sdi->session, serial, G_IO_IN, 40,
442 gmc_mh_1x_2x_receive_data, (void *)sdi);
7b4edcb6
MH
443
444 return SR_OK;
445}
446
695dc859 447static int dev_acquisition_start_2x_bd232(const struct sr_dev_inst *sdi)
c90beca7
MH
448{
449 struct dev_context *devc;
450 struct sr_serial_dev_inst *serial;
451
c90beca7
MH
452 if (sdi->status != SR_ST_ACTIVE)
453 return SR_ERR_DEV_CLOSED;
454
208c1d35 455 devc = sdi->priv;
c90beca7
MH
456 devc->settings_ok = FALSE;
457 devc->buflen = 0;
458
3c413879 459 sr_sw_limits_acquisition_start(&devc->limits);
bee2b016 460 std_session_send_df_header(sdi);
c90beca7 461
c90beca7
MH
462 /* Poll every 40ms, or whenever some data comes in. */
463 serial = sdi->conn;
102f1239
BV
464 serial_source_add(sdi->session, serial, G_IO_IN, 40,
465 gmc_mh_2x_receive_data, (void *)sdi);
c90beca7
MH
466
467 /* Send start message */
468 return req_meas14(sdi);
469}
470
695dc859 471static int dev_acquisition_stop(struct sr_dev_inst *sdi)
7b4edcb6 472{
15f96409 473 return std_serial_dev_acquisition_stop(sdi, dev_close);
7b4edcb6
MH
474}
475
dd5c48a6 476static struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info = {
7b4edcb6 477 .name = "gmc-mh-1x-2x-rs232",
a90061e5 478 .longname = "Gossen Metrawatt Metrahit 1x/2x, RS232 interface",
7b4edcb6 479 .api_version = 1,
c2fdcc25 480 .init = std_init,
700d6b64 481 .cleanup = std_cleanup,
c90beca7 482 .scan = scan_1x_2x_rs232,
c01bf34c 483 .dev_list = std_dev_list,
a6630742 484 .dev_clear = NULL,
c90beca7
MH
485 .config_get = config_get,
486 .config_set = config_set,
487 .config_list = config_list_sm,
488 .dev_open = std_serial_dev_open,
489 .dev_close = dev_close,
490 .dev_acquisition_start = dev_acquisition_start_1x_2x_rs232,
491 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 492 .context = NULL,
c90beca7 493};
dd5c48a6 494SR_REGISTER_DEV_DRIVER(gmc_mh_1x_2x_rs232_driver_info);
c90beca7 495
dd5c48a6 496static struct sr_dev_driver gmc_mh_2x_bd232_driver_info = {
c90beca7 497 .name = "gmc-mh-2x-bd232",
a90061e5 498 .longname = "Gossen Metrawatt Metrahit 2x, BD232/SI232-II interface",
c90beca7 499 .api_version = 1,
c2fdcc25 500 .init = std_init,
700d6b64 501 .cleanup = std_cleanup,
c90beca7 502 .scan = scan_2x_bd232,
c01bf34c 503 .dev_list = std_dev_list,
a6630742 504 .dev_clear = NULL,
7b4edcb6
MH
505 .config_get = config_get,
506 .config_set = config_set,
c90beca7 507 .config_list = config_list_bd,
854434de 508 .dev_open = std_serial_dev_open,
7b4edcb6 509 .dev_close = dev_close,
c90beca7 510 .dev_acquisition_start = dev_acquisition_start_2x_bd232,
27fd2eaa 511 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 512 .context = NULL,
7b4edcb6 513};
dd5c48a6 514SR_REGISTER_DEV_DRIVER(gmc_mh_2x_bd232_driver_info);