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