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