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