]>
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 | /** | |
21 | * @file | |
22 | * | |
23 | * Gossen Metrawatt Metrahit 1x/2x drivers | |
24 | * | |
25 | * @internal | |
26 | */ | |
27 | ||
28 | #include <config.h> | |
29 | #include <string.h> | |
30 | #include "protocol.h" | |
31 | ||
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" | |
35 | #define SERIALCOMM_2X "9600/8n1/dtr=1/rts=1/flow=0" | |
36 | #define VENDOR_GMC "Gossen Metrawatt" | |
37 | ||
38 | static const uint32_t scanopts[] = { | |
39 | SR_CONF_CONN, | |
40 | SR_CONF_SERIALCOMM, | |
41 | }; | |
42 | ||
43 | /** Hardware capabilities for Metrahit 1x/2x devices in send mode. */ | |
44 | static const uint32_t devopts_sm[] = { | |
45 | SR_CONF_MULTIMETER, | |
46 | SR_CONF_THERMOMETER, /**< All GMC 1x/2x multimeters seem to support this */ | |
47 | SR_CONF_CONTINUOUS, | |
48 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
49 | SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, | |
50 | }; | |
51 | ||
52 | /** Hardware capabilities for Metrahit 2x devices in bidirectional Mode. */ | |
53 | static const uint32_t devopts_bd[] = { | |
54 | SR_CONF_MULTIMETER, | |
55 | SR_CONF_THERMOMETER, /**< All GMC 1x/2x multimeters seem to support this */ | |
56 | SR_CONF_CONTINUOUS, | |
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, | |
60 | }; | |
61 | ||
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 | ||
69 | /** | |
70 | * Read single byte from serial port. | |
71 | * | |
72 | * @retval -1 Timeout or error. | |
73 | * @retval other Byte. | |
74 | */ | |
75 | static int read_byte(struct sr_serial_dev_inst *serial, gint64 timeout) | |
76 | { | |
77 | uint8_t result = 0; | |
78 | int rc = 0; | |
79 | ||
80 | for (;;) { | |
81 | rc = serial_read_nonblocking(serial, &result, 1); | |
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 | ||
92 | /** | |
93 | * Try to detect GMC 1x/2x multimeter model in send mode for max. 1 second. | |
94 | * | |
95 | * @param serial Configured, open serial port. | |
96 | * | |
97 | * @retval NULL Detection failed. | |
98 | * @retval other Model code. | |
99 | */ | |
100 | static 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 | ||
106 | model = METRAHIT_NONE; | |
107 | timeout_us = g_get_monotonic_time() + (1 * 1000 * 1000); | |
108 | ||
109 | /* | |
110 | * Try to find message consisting of device code and several | |
111 | * (at least 4) data bytes. | |
112 | */ | |
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) { | |
118 | if (!(model = gmc_decode_model_sm(byte & MSGC_MASK))) | |
119 | break; | |
120 | /* Now expect (at least) 4 data bytes. */ | |
121 | for (cnt = 0; cnt < 4; cnt++) { | |
122 | byte = read_byte(serial, timeout_us); | |
123 | if ((byte == -1) || | |
124 | ((byte & MSGID_MASK) != MSGID_DATA)) | |
125 | { | |
126 | model = METRAHIT_NONE; | |
127 | bytecnt = 100; | |
128 | break; | |
129 | } | |
130 | } | |
131 | break; | |
132 | } | |
133 | } | |
134 | ||
135 | return model; | |
136 | } | |
137 | ||
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. | |
146 | */ | |
147 | static GSList *scan_1x_2x_rs232(struct sr_dev_driver *di, GSList *options) | |
148 | { | |
149 | struct sr_dev_inst *sdi; | |
150 | struct dev_context *devc; | |
151 | struct sr_config *src; | |
152 | struct sr_serial_dev_inst *serial; | |
153 | GSList *l, *devices; | |
154 | const char *conn, *serialcomm; | |
155 | enum model model; | |
156 | gboolean serialcomm_given; | |
157 | ||
158 | devices = 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 | sr_sw_limits_init(&devc->limits); | |
214 | devc->model = model; | |
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 | sr_sw_limits_init(&devc->limits); | |
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 | sr_dev_inst_free(sdi); | |
330 | ||
331 | return NULL; | |
332 | } | |
333 | ||
334 | static int dev_close(struct sr_dev_inst *sdi) | |
335 | { | |
336 | struct dev_context *devc; | |
337 | ||
338 | std_serial_dev_close(sdi); | |
339 | ||
340 | sdi->status = SR_ST_INACTIVE; | |
341 | if ((devc = sdi->priv)) | |
342 | devc->model = METRAHIT_NONE; | |
343 | ||
344 | return SR_OK; | |
345 | } | |
346 | ||
347 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
348 | const struct sr_channel_group *cg) | |
349 | { | |
350 | int ret; | |
351 | struct dev_context *devc; | |
352 | ||
353 | (void)cg; | |
354 | ||
355 | if (!sdi) | |
356 | return SR_ERR_ARG; | |
357 | ||
358 | devc = sdi->priv; | |
359 | ||
360 | ret = SR_OK; | |
361 | switch (key) { | |
362 | case SR_CONF_LIMIT_SAMPLES: | |
363 | case SR_CONF_LIMIT_MSEC: | |
364 | return sr_sw_limits_config_get(&devc->limits, key, data); | |
365 | case SR_CONF_POWER_OFF: | |
366 | *data = g_variant_new_boolean(FALSE); | |
367 | break; | |
368 | default: | |
369 | return SR_ERR_NA; | |
370 | } | |
371 | ||
372 | return ret; | |
373 | } | |
374 | ||
375 | /** Implementation of config_list, auxiliary function for common parts. */ | |
376 | static int config_list_common(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
377 | const struct sr_channel_group *cg) | |
378 | { | |
379 | (void)sdi; | |
380 | (void)cg; | |
381 | ||
382 | switch (key) { | |
383 | case SR_CONF_SCAN_OPTIONS: | |
384 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
385 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
386 | break; | |
387 | default: | |
388 | return SR_ERR_NA; | |
389 | } | |
390 | ||
391 | return SR_OK; | |
392 | } | |
393 | ||
394 | /** Implementation of config_list for Metrahit 1x/2x send mode */ | |
395 | static int config_list_sm(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
396 | const struct sr_channel_group *cg) | |
397 | { | |
398 | switch (key) { | |
399 | case SR_CONF_DEVICE_OPTIONS: | |
400 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
401 | devopts_sm, ARRAY_SIZE(devopts_sm), sizeof(uint32_t)); | |
402 | break; | |
403 | default: | |
404 | return config_list_common(key, data, sdi, cg); | |
405 | } | |
406 | ||
407 | return SR_OK; | |
408 | } | |
409 | ||
410 | /** Implementation of config_list for Metrahit 2x bidirectional mode */ | |
411 | static int config_list_bd(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
412 | const struct sr_channel_group *cg) | |
413 | { | |
414 | switch (key) { | |
415 | case SR_CONF_DEVICE_OPTIONS: | |
416 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
417 | devopts_bd, ARRAY_SIZE(devopts_bd), sizeof(uint32_t)); | |
418 | break; | |
419 | default: | |
420 | return config_list_common(key, data, sdi, cg); | |
421 | } | |
422 | ||
423 | return SR_OK; | |
424 | } | |
425 | ||
426 | static int dev_acquisition_start_1x_2x_rs232(const struct sr_dev_inst *sdi) | |
427 | { | |
428 | struct dev_context *devc; | |
429 | struct sr_serial_dev_inst *serial; | |
430 | ||
431 | if (sdi->status != SR_ST_ACTIVE) | |
432 | return SR_ERR_DEV_CLOSED; | |
433 | ||
434 | devc = sdi->priv; | |
435 | devc->settings_ok = FALSE; | |
436 | devc->buflen = 0; | |
437 | ||
438 | sr_sw_limits_acquisition_start(&devc->limits); | |
439 | std_session_send_df_header(sdi); | |
440 | ||
441 | /* Poll every 40ms, or whenever some data comes in. */ | |
442 | serial = sdi->conn; | |
443 | serial_source_add(sdi->session, serial, G_IO_IN, 40, | |
444 | gmc_mh_1x_2x_receive_data, (void *)sdi); | |
445 | ||
446 | return SR_OK; | |
447 | } | |
448 | ||
449 | static int dev_acquisition_start_2x_bd232(const struct sr_dev_inst *sdi) | |
450 | { | |
451 | struct dev_context *devc; | |
452 | struct sr_serial_dev_inst *serial; | |
453 | ||
454 | if (sdi->status != SR_ST_ACTIVE) | |
455 | return SR_ERR_DEV_CLOSED; | |
456 | ||
457 | devc = sdi->priv; | |
458 | devc->settings_ok = FALSE; | |
459 | devc->buflen = 0; | |
460 | ||
461 | sr_sw_limits_acquisition_start(&devc->limits); | |
462 | std_session_send_df_header(sdi); | |
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_2x_receive_data, (void *)sdi); | |
468 | ||
469 | /* Send start message */ | |
470 | return req_meas14(sdi); | |
471 | } | |
472 | ||
473 | static struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info = { | |
474 | .name = "gmc-mh-1x-2x-rs232", | |
475 | .longname = "Gossen Metrawatt Metrahit 1x/2x, RS232 interface", | |
476 | .api_version = 1, | |
477 | .init = std_init, | |
478 | .cleanup = std_cleanup, | |
479 | .scan = scan_1x_2x_rs232, | |
480 | .dev_list = std_dev_list, | |
481 | .dev_clear = NULL, | |
482 | .config_get = config_get, | |
483 | .config_set = config_set, | |
484 | .config_list = config_list_sm, | |
485 | .dev_open = std_serial_dev_open, | |
486 | .dev_close = dev_close, | |
487 | .dev_acquisition_start = dev_acquisition_start_1x_2x_rs232, | |
488 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, | |
489 | .context = NULL, | |
490 | }; | |
491 | SR_REGISTER_DEV_DRIVER(gmc_mh_1x_2x_rs232_driver_info); | |
492 | ||
493 | static struct sr_dev_driver gmc_mh_2x_bd232_driver_info = { | |
494 | .name = "gmc-mh-2x-bd232", | |
495 | .longname = "Gossen Metrawatt Metrahit 2x, BD232/SI232-II interface", | |
496 | .api_version = 1, | |
497 | .init = std_init, | |
498 | .cleanup = std_cleanup, | |
499 | .scan = scan_2x_bd232, | |
500 | .dev_list = std_dev_list, | |
501 | .dev_clear = NULL, | |
502 | .config_get = config_get, | |
503 | .config_set = config_set, | |
504 | .config_list = config_list_bd, | |
505 | .dev_open = std_serial_dev_open, | |
506 | .dev_close = dev_close, | |
507 | .dev_acquisition_start = dev_acquisition_start_2x_bd232, | |
508 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, | |
509 | .context = NULL, | |
510 | }; | |
511 | SR_REGISTER_DEV_DRIVER(gmc_mh_2x_bd232_driver_info); |