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