]>
Commit | Line | Data |
---|---|---|
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 | ||
d9251a2c UH |
20 | /** |
21 | * @file | |
22 | * | |
23 | * Gossen Metrawatt Metrahit 1x/2x drivers | |
24 | * | |
25 | * @internal | |
6392d599 MH |
26 | */ |
27 | ||
6ec6c43b | 28 | #include <config.h> |
f5792417 | 29 | #include <string.h> |
7b4edcb6 MH |
30 | #include "protocol.h" |
31 | ||
f5792417 MH |
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" | |
6392d599 | 35 | #define SERIALCOMM_2X "9600/8n1/dtr=1/rts=1/flow=0" |
f5792417 MH |
36 | #define VENDOR_GMC "Gossen Metrawatt" |
37 | ||
a0e0bb41 | 38 | static const uint32_t scanopts[] = { |
f5792417 MH |
39 | SR_CONF_CONN, |
40 | SR_CONF_SERIALCOMM, | |
41 | }; | |
42 | ||
05199c0a | 43 | static const uint32_t drvopts[] = { |
f5792417 | 44 | SR_CONF_MULTIMETER, |
d9251a2c | 45 | SR_CONF_THERMOMETER, /**< All GMC 1x/2x multimeters seem to support this */ |
05199c0a UH |
46 | }; |
47 | ||
48 | /** Hardware capabilities for Metrahit 1x/2x devices in send mode. */ | |
49 | static const uint32_t devopts_sm[] = { | |
f5792417 | 50 | SR_CONF_CONTINUOUS, |
5827f61b BV |
51 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, |
52 | SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, | |
f5792417 MH |
53 | }; |
54 | ||
c90beca7 | 55 | /** Hardware capabilities for Metrahit 2x devices in bidirectional Mode. */ |
f254bc4b | 56 | static const uint32_t devopts_bd[] = { |
c90beca7 | 57 | SR_CONF_CONTINUOUS, |
5827f61b BV |
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, | |
c90beca7 MH |
61 | }; |
62 | ||
6392d599 MH |
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 | ||
fc348b77 UH |
70 | /** |
71 | * Read single byte from serial port. | |
72 | * | |
73 | * @retval -1 Timeout or error. | |
74 | * @retval other Byte. | |
f5792417 MH |
75 | */ |
76 | static int read_byte(struct sr_serial_dev_inst *serial, gint64 timeout) | |
7b4edcb6 | 77 | { |
fc348b77 | 78 | uint8_t result = 0; |
f5792417 MH |
79 | int rc = 0; |
80 | ||
81 | for (;;) { | |
0714a010 | 82 | rc = serial_read_nonblocking(serial, &result, 1); |
f5792417 MH |
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 | ||
fc348b77 UH |
93 | /** |
94 | * Try to detect GMC 1x/2x multimeter model in send mode for max. 1 second. | |
f5792417 | 95 | * |
fc348b77 UH |
96 | * @param serial Configured, open serial port. |
97 | * | |
98 | * @retval NULL Detection failed. | |
99 | * @retval other Model code. | |
f5792417 MH |
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 | ||
873e0c12 | 107 | model = METRAHIT_NONE; |
1a46cc62 | 108 | timeout_us = g_get_monotonic_time() + (1 * 1000 * 1000); |
f5792417 | 109 | |
fc348b77 UH |
110 | /* |
111 | * Try to find message consisting of device code and several | |
112 | * (at least 4) data bytes. | |
113 | */ | |
f5792417 MH |
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) { | |
873e0c12 | 119 | if (!(model = gmc_decode_model_sm(byte & MSGC_MASK))) |
f5792417 | 120 | break; |
fc348b77 | 121 | /* Now expect (at least) 4 data bytes. */ |
f5792417 MH |
122 | for (cnt = 0; cnt < 4; cnt++) { |
123 | byte = read_byte(serial, timeout_us); | |
124 | if ((byte == -1) || | |
d9251a2c | 125 | ((byte & MSGID_MASK) != MSGID_DATA)) |
f5792417 | 126 | { |
873e0c12 | 127 | model = METRAHIT_NONE; |
f5792417 MH |
128 | bytecnt = 100; |
129 | break; | |
130 | } | |
131 | } | |
132 | break; | |
133 | } | |
134 | } | |
7b4edcb6 | 135 | |
f5792417 MH |
136 | return model; |
137 | } | |
138 | ||
fc348b77 UH |
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. | |
f5792417 | 147 | */ |
4f840ce9 | 148 | static GSList *scan_1x_2x_rs232(struct sr_dev_driver *di, GSList *options) |
f5792417 MH |
149 | { |
150 | struct sr_dev_inst *sdi; | |
f5792417 MH |
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; | |
f5792417 | 160 | conn = serialcomm = NULL; |
f5792417 MH |
161 | serialcomm_given = FALSE; |
162 | ||
f5792417 MH |
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 | ||
91219afc | 180 | serial = sr_serial_dev_inst_new(conn, serialcomm); |
f5792417 | 181 | |
e13e354f | 182 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) { |
f5792417 MH |
183 | sr_serial_dev_inst_free(serial); |
184 | return NULL; | |
185 | } | |
7b4edcb6 | 186 | |
f5792417 MH |
187 | serial_flush(serial); |
188 | ||
189 | model = scan_model_sm(serial); | |
190 | ||
fc348b77 UH |
191 | /* |
192 | * If detection failed and no user-supplied parameters, | |
193 | * try second baud rate. | |
194 | */ | |
873e0c12 | 195 | if ((model == METRAHIT_NONE) && !serialcomm_given) { |
f5792417 MH |
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 | ||
873e0c12 UH |
205 | if (model != METRAHIT_NONE) { |
206 | sr_spew("%s %s detected!", VENDOR_GMC, gmc_model_str(model)); | |
aac29cc1 | 207 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be UH |
208 | sdi->status = SR_ST_INACTIVE; |
209 | sdi->vendor = g_strdup(VENDOR_GMC); | |
210 | sdi->model = g_strdup(gmc_model_str(model)); | |
f57d8ffe | 211 | devc = g_malloc0(sizeof(struct dev_context)); |
3c413879 | 212 | sr_sw_limits_init(&devc->limits); |
f5792417 | 213 | devc->model = model; |
f5792417 | 214 | devc->settings_ok = FALSE; |
f5792417 MH |
215 | sdi->conn = serial; |
216 | sdi->priv = devc; | |
5e23fcab | 217 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1"); |
f5792417 MH |
218 | devices = g_slist_append(devices, sdi); |
219 | } | |
7b4edcb6 | 220 | |
15a5bfe4 | 221 | return std_scan_complete(di, devices); |
7b4edcb6 MH |
222 | } |
223 | ||
1a863916 UH |
224 | /** |
225 | * Scan for Metrahit 2x in a bidirectional mode using Gossen Metrawatt | |
226 | * 'BD 232' interface. | |
c90beca7 | 227 | */ |
4f840ce9 | 228 | static GSList *scan_2x_bd232(struct sr_dev_driver *di, GSList *options) |
c90beca7 MH |
229 | { |
230 | struct sr_dev_inst *sdi; | |
c90beca7 MH |
231 | struct dev_context *devc; |
232 | struct sr_config *src; | |
c90beca7 MH |
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 | ||
c90beca7 MH |
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 | ||
91219afc | 260 | serial = sr_serial_dev_inst_new(conn, serialcomm); |
c90beca7 | 261 | |
e13e354f | 262 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) |
c90beca7 MH |
263 | goto exit_err; |
264 | ||
f57d8ffe | 265 | devc = g_malloc0(sizeof(struct dev_context)); |
c90beca7 | 266 | |
aac29cc1 | 267 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be UH |
268 | sdi->status = SR_ST_INACTIVE; |
269 | sdi->vendor = g_strdup(VENDOR_GMC); | |
c90beca7 MH |
270 | sdi->priv = devc; |
271 | ||
272 | /* Send message 03 "Query multimeter version and status" */ | |
273 | sdi->conn = serial; | |
c90beca7 MH |
274 | if (req_stat14(sdi, TRUE) != SR_OK) |
275 | goto exit_err; | |
276 | ||
277 | /* Wait for reply from device(s) for up to 2s. */ | |
1a46cc62 | 278 | timeout_us = g_get_monotonic_time() + (2 * 1000 * 1000); |
c90beca7 MH |
279 | |
280 | while (timeout_us > g_get_monotonic_time()) { | |
281 | /* Receive reply (14 bytes) */ | |
282 | devc->buflen = 0; | |
07ffa5b3 | 283 | for (cnt = 0; cnt < GMC_REPLY_SIZE; cnt++) { |
c90beca7 MH |
284 | byte = read_byte(serial, timeout_us); |
285 | if (byte != -1) | |
286 | devc->buf[devc->buflen++] = (byte & MASK_6BITS); | |
287 | } | |
288 | ||
07ffa5b3 | 289 | if (devc->buflen != GMC_REPLY_SIZE) |
c90beca7 MH |
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)); | |
3c413879 | 298 | sr_sw_limits_init(&devc->limits); |
c90beca7 MH |
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; | |
5e23fcab | 303 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1"); |
c90beca7 | 304 | devices = g_slist_append(devices, sdi); |
f57d8ffe | 305 | devc = g_malloc0(sizeof(struct dev_context)); |
aac29cc1 | 306 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be UH |
307 | sdi->status = SR_ST_INACTIVE; |
308 | sdi->vendor = g_strdup(VENDOR_GMC); | |
c90beca7 MH |
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 | ||
15a5bfe4 | 318 | return std_scan_complete(di, devices); |
c90beca7 MH |
319 | |
320 | exit_err: | |
04891a99 | 321 | sr_serial_dev_inst_free(serial); |
b1f83103 | 322 | g_free(devc); |
4bf93988 | 323 | sr_dev_inst_free(sdi); |
c90beca7 MH |
324 | |
325 | return NULL; | |
326 | } | |
327 | ||
7b4edcb6 MH |
328 | static int dev_close(struct sr_dev_inst *sdi) |
329 | { | |
f5792417 | 330 | struct dev_context *devc; |
7b4edcb6 | 331 | |
f1ba6b4b | 332 | devc = sdi->priv; |
7b4edcb6 | 333 | |
f1ba6b4b | 334 | devc->model = METRAHIT_NONE; |
f5792417 | 335 | |
f1ba6b4b | 336 | return std_serial_dev_close(sdi); |
7b4edcb6 MH |
337 | } |
338 | ||
584560f1 BV |
339 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
340 | const struct sr_channel_group *cg) | |
7b4edcb6 | 341 | { |
fadd0707 | 342 | struct dev_context *devc; |
c90beca7 | 343 | |
53b4680f | 344 | (void)cg; |
c90beca7 | 345 | |
709468ba | 346 | if (!sdi) |
6392d599 MH |
347 | return SR_ERR_ARG; |
348 | ||
709468ba UH |
349 | devc = sdi->priv; |
350 | ||
7b4edcb6 | 351 | switch (key) { |
6392d599 | 352 | case SR_CONF_LIMIT_SAMPLES: |
6392d599 | 353 | case SR_CONF_LIMIT_MSEC: |
3c413879 | 354 | return sr_sw_limits_config_get(&devc->limits, key, data); |
c90beca7 MH |
355 | case SR_CONF_POWER_OFF: |
356 | *data = g_variant_new_boolean(FALSE); | |
357 | break; | |
7b4edcb6 MH |
358 | default: |
359 | return SR_ERR_NA; | |
360 | } | |
361 | ||
a9010323 | 362 | return SR_OK; |
7b4edcb6 MH |
363 | } |
364 | ||
c90beca7 | 365 | /** Implementation of config_list for Metrahit 1x/2x send mode */ |
584560f1 | 366 | static int config_list_sm(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 367 | const struct sr_channel_group *cg) |
7b4edcb6 | 368 | { |
05199c0a | 369 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts_sm); |
c90beca7 MH |
370 | } |
371 | ||
372 | /** Implementation of config_list for Metrahit 2x bidirectional mode */ | |
584560f1 | 373 | static int config_list_bd(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 374 | const struct sr_channel_group *cg) |
c90beca7 | 375 | { |
05199c0a | 376 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts_bd); |
7b4edcb6 MH |
377 | } |
378 | ||
695dc859 | 379 | static int dev_acquisition_start_1x_2x_rs232(const struct sr_dev_inst *sdi) |
7b4edcb6 | 380 | { |
f5792417 MH |
381 | struct dev_context *devc; |
382 | struct sr_serial_dev_inst *serial; | |
383 | ||
208c1d35 | 384 | devc = sdi->priv; |
f5792417 MH |
385 | devc->settings_ok = FALSE; |
386 | devc->buflen = 0; | |
387 | ||
3c413879 | 388 | sr_sw_limits_acquisition_start(&devc->limits); |
bee2b016 | 389 | std_session_send_df_header(sdi); |
f5792417 | 390 | |
f5792417 MH |
391 | /* Poll every 40ms, or whenever some data comes in. */ |
392 | serial = sdi->conn; | |
102f1239 BV |
393 | serial_source_add(sdi->session, serial, G_IO_IN, 40, |
394 | gmc_mh_1x_2x_receive_data, (void *)sdi); | |
7b4edcb6 MH |
395 | |
396 | return SR_OK; | |
397 | } | |
398 | ||
695dc859 | 399 | static int dev_acquisition_start_2x_bd232(const struct sr_dev_inst *sdi) |
c90beca7 MH |
400 | { |
401 | struct dev_context *devc; | |
402 | struct sr_serial_dev_inst *serial; | |
403 | ||
208c1d35 | 404 | devc = sdi->priv; |
c90beca7 MH |
405 | devc->settings_ok = FALSE; |
406 | devc->buflen = 0; | |
407 | ||
3c413879 | 408 | sr_sw_limits_acquisition_start(&devc->limits); |
bee2b016 | 409 | std_session_send_df_header(sdi); |
c90beca7 | 410 | |
c90beca7 MH |
411 | /* Poll every 40ms, or whenever some data comes in. */ |
412 | serial = sdi->conn; | |
102f1239 BV |
413 | serial_source_add(sdi->session, serial, G_IO_IN, 40, |
414 | gmc_mh_2x_receive_data, (void *)sdi); | |
c90beca7 MH |
415 | |
416 | /* Send start message */ | |
417 | return req_meas14(sdi); | |
418 | } | |
419 | ||
dd5c48a6 | 420 | static struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info = { |
7b4edcb6 | 421 | .name = "gmc-mh-1x-2x-rs232", |
a90061e5 | 422 | .longname = "Gossen Metrawatt Metrahit 1x/2x, RS232 interface", |
7b4edcb6 | 423 | .api_version = 1, |
c2fdcc25 | 424 | .init = std_init, |
700d6b64 | 425 | .cleanup = std_cleanup, |
c90beca7 | 426 | .scan = scan_1x_2x_rs232, |
c01bf34c | 427 | .dev_list = std_dev_list, |
f778bf02 | 428 | .dev_clear = std_dev_clear, |
c90beca7 MH |
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, | |
4b1a9d5d | 435 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, |
41812aca | 436 | .context = NULL, |
c90beca7 | 437 | }; |
dd5c48a6 | 438 | SR_REGISTER_DEV_DRIVER(gmc_mh_1x_2x_rs232_driver_info); |
c90beca7 | 439 | |
dd5c48a6 | 440 | static struct sr_dev_driver gmc_mh_2x_bd232_driver_info = { |
c90beca7 | 441 | .name = "gmc-mh-2x-bd232", |
a90061e5 | 442 | .longname = "Gossen Metrawatt Metrahit 2x, BD232/SI232-II interface", |
c90beca7 | 443 | .api_version = 1, |
c2fdcc25 | 444 | .init = std_init, |
700d6b64 | 445 | .cleanup = std_cleanup, |
c90beca7 | 446 | .scan = scan_2x_bd232, |
c01bf34c | 447 | .dev_list = std_dev_list, |
f778bf02 | 448 | .dev_clear = std_dev_clear, |
7b4edcb6 MH |
449 | .config_get = config_get, |
450 | .config_set = config_set, | |
c90beca7 | 451 | .config_list = config_list_bd, |
854434de | 452 | .dev_open = std_serial_dev_open, |
7b4edcb6 | 453 | .dev_close = dev_close, |
c90beca7 | 454 | .dev_acquisition_start = dev_acquisition_start_2x_bd232, |
4b1a9d5d | 455 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, |
41812aca | 456 | .context = NULL, |
7b4edcb6 | 457 | }; |
dd5c48a6 | 458 | SR_REGISTER_DEV_DRIVER(gmc_mh_2x_bd232_driver_info); |