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