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