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