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