]>
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 | #include <config.h> | |
21 | #include <string.h> | |
22 | #include "protocol.h" | |
23 | ||
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" | |
27 | #define SERIALCOMM_2X "9600/8n1/dtr=1/rts=1/flow=0" | |
28 | #define VENDOR_GMC "Gossen Metrawatt" | |
29 | ||
30 | static const uint32_t scanopts[] = { | |
31 | SR_CONF_CONN, | |
32 | SR_CONF_SERIALCOMM, | |
33 | }; | |
34 | ||
35 | static const uint32_t drvopts[] = { | |
36 | SR_CONF_MULTIMETER, | |
37 | SR_CONF_THERMOMETER, /**< All GMC 1x/2x multimeters seem to support this */ | |
38 | }; | |
39 | ||
40 | /** Hardware capabilities for Metrahit 1x/2x devices in send mode. */ | |
41 | static const uint32_t devopts_sm[] = { | |
42 | SR_CONF_CONTINUOUS, | |
43 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
44 | SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, | |
45 | }; | |
46 | ||
47 | /** Hardware capabilities for Metrahit 2x devices in bidirectional Mode. */ | |
48 | static const uint32_t devopts_bd[] = { | |
49 | SR_CONF_CONTINUOUS, | |
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, | |
53 | }; | |
54 | ||
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 | ||
62 | /** | |
63 | * Read single byte from serial port. | |
64 | * | |
65 | * @retval -1 Timeout or error. | |
66 | * @retval other Byte. | |
67 | */ | |
68 | static int read_byte(struct sr_serial_dev_inst *serial, gint64 timeout) | |
69 | { | |
70 | uint8_t result = 0; | |
71 | int rc = 0; | |
72 | ||
73 | for (;;) { | |
74 | rc = serial_read_nonblocking(serial, &result, 1); | |
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 | ||
85 | /** | |
86 | * Try to detect GMC 1x/2x multimeter model in send mode for max. 1 second. | |
87 | * | |
88 | * @param serial Configured, open serial port. | |
89 | * | |
90 | * @retval NULL Detection failed. | |
91 | * @retval other Model code. | |
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 | ||
99 | model = METRAHIT_NONE; | |
100 | timeout_us = g_get_monotonic_time() + (1 * 1000 * 1000); | |
101 | ||
102 | /* | |
103 | * Try to find message consisting of device code and several | |
104 | * (at least 4) data bytes. | |
105 | */ | |
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) { | |
111 | if (!(model = gmc_decode_model_sm(byte & MSGC_MASK))) | |
112 | break; | |
113 | /* Now expect (at least) 4 data bytes. */ | |
114 | for (cnt = 0; cnt < 4; cnt++) { | |
115 | byte = read_byte(serial, timeout_us); | |
116 | if ((byte == -1) || | |
117 | ((byte & MSGID_MASK) != MSGID_DATA)) | |
118 | { | |
119 | model = METRAHIT_NONE; | |
120 | bytecnt = 100; | |
121 | break; | |
122 | } | |
123 | } | |
124 | break; | |
125 | } | |
126 | } | |
127 | ||
128 | return model; | |
129 | } | |
130 | ||
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. | |
139 | */ | |
140 | static GSList *scan_1x_2x_rs232(struct sr_dev_driver *di, GSList *options) | |
141 | { | |
142 | struct sr_dev_inst *sdi; | |
143 | struct dev_context *devc; | |
144 | struct sr_config *src; | |
145 | struct sr_serial_dev_inst *serial; | |
146 | GSList *l, *devices; | |
147 | const char *conn, *serialcomm; | |
148 | enum model model; | |
149 | gboolean serialcomm_given; | |
150 | ||
151 | devices = NULL; | |
152 | conn = serialcomm = NULL; | |
153 | serialcomm_given = FALSE; | |
154 | ||
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 | ||
172 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
173 | ||
174 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) { | |
175 | sr_serial_dev_inst_free(serial); | |
176 | return NULL; | |
177 | } | |
178 | ||
179 | serial_flush(serial); | |
180 | ||
181 | model = scan_model_sm(serial); | |
182 | ||
183 | /* | |
184 | * If detection failed and no user-supplied parameters, | |
185 | * try second baud rate. | |
186 | */ | |
187 | if ((model == METRAHIT_NONE) && !serialcomm_given) { | |
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 | ||
197 | if (model != METRAHIT_NONE) { | |
198 | sr_spew("%s %s detected!", VENDOR_GMC, gmc_model_str(model)); | |
199 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
200 | sdi->status = SR_ST_INACTIVE; | |
201 | sdi->vendor = g_strdup(VENDOR_GMC); | |
202 | sdi->model = g_strdup(gmc_model_str(model)); | |
203 | devc = g_malloc0(sizeof(struct dev_context)); | |
204 | sr_sw_limits_init(&devc->limits); | |
205 | devc->model = model; | |
206 | devc->settings_ok = FALSE; | |
207 | sdi->conn = serial; | |
208 | sdi->priv = devc; | |
209 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1"); | |
210 | devices = g_slist_append(devices, sdi); | |
211 | } | |
212 | ||
213 | return std_scan_complete(di, devices); | |
214 | } | |
215 | ||
216 | /** | |
217 | * Scan for Metrahit 2x in a bidirectional mode using Gossen Metrawatt | |
218 | * 'BD 232' interface. | |
219 | */ | |
220 | static GSList *scan_2x_bd232(struct sr_dev_driver *di, GSList *options) | |
221 | { | |
222 | struct sr_dev_inst *sdi; | |
223 | struct dev_context *devc; | |
224 | struct sr_config *src; | |
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 | ||
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 | ||
252 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
253 | ||
254 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
255 | goto exit_err; | |
256 | ||
257 | devc = g_malloc0(sizeof(struct dev_context)); | |
258 | ||
259 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
260 | sdi->status = SR_ST_INACTIVE; | |
261 | sdi->vendor = g_strdup(VENDOR_GMC); | |
262 | sdi->priv = devc; | |
263 | ||
264 | /* Send message 03 "Query multimeter version and status" */ | |
265 | sdi->conn = serial; | |
266 | if (req_stat14(sdi, TRUE) != SR_OK) | |
267 | goto exit_err; | |
268 | ||
269 | /* Wait for reply from device(s) for up to 2s. */ | |
270 | timeout_us = g_get_monotonic_time() + (2 * 1000 * 1000); | |
271 | ||
272 | while (timeout_us > g_get_monotonic_time()) { | |
273 | /* Receive reply (14 bytes) */ | |
274 | devc->buflen = 0; | |
275 | for (cnt = 0; cnt < GMC_REPLY_SIZE; cnt++) { | |
276 | byte = read_byte(serial, timeout_us); | |
277 | if (byte != -1) | |
278 | devc->buf[devc->buflen++] = (byte & MASK_6BITS); | |
279 | } | |
280 | ||
281 | if (devc->buflen != GMC_REPLY_SIZE) | |
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)); | |
290 | sr_sw_limits_init(&devc->limits); | |
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; | |
295 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1"); | |
296 | devices = g_slist_append(devices, sdi); | |
297 | devc = g_malloc0(sizeof(struct dev_context)); | |
298 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
299 | sdi->status = SR_ST_INACTIVE; | |
300 | sdi->vendor = g_strdup(VENDOR_GMC); | |
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 | ||
310 | return std_scan_complete(di, devices); | |
311 | ||
312 | exit_err: | |
313 | sr_serial_dev_inst_free(serial); | |
314 | g_free(devc); | |
315 | sr_dev_inst_free(sdi); | |
316 | ||
317 | return NULL; | |
318 | } | |
319 | ||
320 | static int dev_close(struct sr_dev_inst *sdi) | |
321 | { | |
322 | struct dev_context *devc; | |
323 | ||
324 | devc = sdi->priv; | |
325 | ||
326 | devc->model = METRAHIT_NONE; | |
327 | ||
328 | return std_serial_dev_close(sdi); | |
329 | } | |
330 | ||
331 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
332 | const struct sr_channel_group *cg) | |
333 | { | |
334 | struct dev_context *devc; | |
335 | ||
336 | (void)cg; | |
337 | ||
338 | if (!sdi) | |
339 | return SR_ERR_ARG; | |
340 | ||
341 | devc = sdi->priv; | |
342 | ||
343 | switch (key) { | |
344 | case SR_CONF_LIMIT_SAMPLES: | |
345 | case SR_CONF_LIMIT_MSEC: | |
346 | return sr_sw_limits_config_get(&devc->limits, key, data); | |
347 | case SR_CONF_POWER_OFF: | |
348 | *data = g_variant_new_boolean(FALSE); | |
349 | break; | |
350 | default: | |
351 | return SR_ERR_NA; | |
352 | } | |
353 | ||
354 | return SR_OK; | |
355 | } | |
356 | ||
357 | /** Implementation of config_list for Metrahit 1x/2x send mode */ | |
358 | static int config_list_sm(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
359 | const struct sr_channel_group *cg) | |
360 | { | |
361 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts_sm); | |
362 | } | |
363 | ||
364 | /** Implementation of config_list for Metrahit 2x bidirectional mode */ | |
365 | static int config_list_bd(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
366 | const struct sr_channel_group *cg) | |
367 | { | |
368 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts_bd); | |
369 | } | |
370 | ||
371 | static int dev_acquisition_start_1x_2x_rs232(const struct sr_dev_inst *sdi) | |
372 | { | |
373 | struct dev_context *devc; | |
374 | struct sr_serial_dev_inst *serial; | |
375 | ||
376 | devc = sdi->priv; | |
377 | devc->settings_ok = FALSE; | |
378 | devc->buflen = 0; | |
379 | ||
380 | sr_sw_limits_acquisition_start(&devc->limits); | |
381 | std_session_send_df_header(sdi); | |
382 | ||
383 | serial = sdi->conn; | |
384 | serial_source_add(sdi->session, serial, G_IO_IN, 40, | |
385 | gmc_mh_1x_2x_receive_data, (void *)sdi); | |
386 | ||
387 | return SR_OK; | |
388 | } | |
389 | ||
390 | static int dev_acquisition_start_2x_bd232(const struct sr_dev_inst *sdi) | |
391 | { | |
392 | struct dev_context *devc; | |
393 | struct sr_serial_dev_inst *serial; | |
394 | ||
395 | devc = sdi->priv; | |
396 | devc->settings_ok = FALSE; | |
397 | devc->buflen = 0; | |
398 | ||
399 | sr_sw_limits_acquisition_start(&devc->limits); | |
400 | std_session_send_df_header(sdi); | |
401 | ||
402 | serial = sdi->conn; | |
403 | serial_source_add(sdi->session, serial, G_IO_IN, 40, | |
404 | gmc_mh_2x_receive_data, (void *)sdi); | |
405 | ||
406 | /* Send start message */ | |
407 | return req_meas14(sdi); | |
408 | } | |
409 | ||
410 | static struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info = { | |
411 | .name = "gmc-mh-1x-2x-rs232", | |
412 | .longname = "Gossen Metrawatt Metrahit 1x/2x, RS232 interface", | |
413 | .api_version = 1, | |
414 | .init = std_init, | |
415 | .cleanup = std_cleanup, | |
416 | .scan = scan_1x_2x_rs232, | |
417 | .dev_list = std_dev_list, | |
418 | .dev_clear = std_dev_clear, | |
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, | |
425 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, | |
426 | .context = NULL, | |
427 | }; | |
428 | SR_REGISTER_DEV_DRIVER(gmc_mh_1x_2x_rs232_driver_info); | |
429 | ||
430 | static struct sr_dev_driver gmc_mh_2x_bd232_driver_info = { | |
431 | .name = "gmc-mh-2x-bd232", | |
432 | .longname = "Gossen Metrawatt Metrahit 2x, BD232/SI232-II interface", | |
433 | .api_version = 1, | |
434 | .init = std_init, | |
435 | .cleanup = std_cleanup, | |
436 | .scan = scan_2x_bd232, | |
437 | .dev_list = std_dev_list, | |
438 | .dev_clear = std_dev_clear, | |
439 | .config_get = config_get, | |
440 | .config_set = config_set, | |
441 | .config_list = config_list_bd, | |
442 | .dev_open = std_serial_dev_open, | |
443 | .dev_close = dev_close, | |
444 | .dev_acquisition_start = dev_acquisition_start_2x_bd232, | |
445 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, | |
446 | .context = NULL, | |
447 | }; | |
448 | SR_REGISTER_DEV_DRIVER(gmc_mh_2x_bd232_driver_info); |