]>
Commit | Line | Data |
---|---|---|
7b4edcb6 MH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 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 | ||
6392d599 MH |
20 | /** @file |
21 | * Gossen Metrawatt Metrahit 1x/2x drivers | |
22 | * @private | |
23 | */ | |
24 | ||
f5792417 | 25 | #include <string.h> |
7b4edcb6 MH |
26 | #include "protocol.h" |
27 | ||
f5792417 MH |
28 | /* Serial communication parameters for Metrahit 1x/2x with 'RS232' adaptor */ |
29 | #define SERIALCOMM_1X_RS232 "8228/6n1/dtr=1/rts=1/flow=0" /* =8192, closer with divider */ | |
30 | #define SERIALCOMM_2X_RS232 "9600/6n1/dtr=1/rts=1/flow=0" | |
6392d599 | 31 | #define SERIALCOMM_2X "9600/8n1/dtr=1/rts=1/flow=0" |
f5792417 MH |
32 | #define VENDOR_GMC "Gossen Metrawatt" |
33 | ||
7b4edcb6 | 34 | SR_PRIV struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info; |
7b4edcb6 | 35 | |
f5792417 MH |
36 | static const int32_t hwopts[] = { |
37 | SR_CONF_CONN, | |
38 | SR_CONF_SERIALCOMM, | |
39 | }; | |
40 | ||
41 | static const int32_t hwcaps[] = { | |
42 | SR_CONF_MULTIMETER, | |
6392d599 | 43 | SR_CONF_THERMOMETER, /**< All GMC 1x/2x multimeters seem to support this */ |
f5792417 MH |
44 | SR_CONF_LIMIT_SAMPLES, |
45 | SR_CONF_LIMIT_MSEC, | |
46 | SR_CONF_CONTINUOUS, | |
47 | }; | |
48 | ||
6392d599 MH |
49 | /* TODO: |
50 | * - For the 29S SR_CONF_ENERGYMETER, too. | |
51 | * - SR_CONF_PATTERN_MODE for some 2x devices | |
52 | * - SR_CONF_DATALOG for 22M, 26M, 29S and storage adaptors. | |
53 | * Need to implement device-specific lists. | |
54 | */ | |
55 | ||
56 | /** Init driver gmc_mh_1x_2x_rs232. */ | |
27fd2eaa | 57 | static int init(struct sr_context *sr_ctx) |
7b4edcb6 | 58 | { |
6392d599 | 59 | return std_init(sr_ctx, &gmc_mh_1x_2x_rs232_driver_info, LOG_PREFIX); |
7b4edcb6 MH |
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 (;;) { | |
74 | rc = serial_read(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 | ||
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; |
fc348b77 | 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) || | |
117 | ((byte & MSGID_MASK) != MSGID_DATA)) | |
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 | */ |
27fd2eaa | 140 | static GSList *scan(GSList *options) |
f5792417 MH |
141 | { |
142 | struct sr_dev_inst *sdi; | |
143 | struct drv_context *drvc; | |
144 | struct dev_context *devc; | |
145 | struct sr_config *src; | |
146 | struct sr_probe *probe; | |
147 | struct sr_serial_dev_inst *serial; | |
148 | GSList *l, *devices; | |
149 | const char *conn, *serialcomm; | |
150 | enum model model; | |
151 | gboolean serialcomm_given; | |
7b4edcb6 MH |
152 | |
153 | devices = NULL; | |
6392d599 | 154 | drvc = (&gmc_mh_1x_2x_rs232_driver_info)->priv; |
7b4edcb6 | 155 | drvc->instances = NULL; |
f5792417 | 156 | conn = serialcomm = NULL; |
873e0c12 | 157 | model = METRAHIT_NONE; |
f5792417 MH |
158 | serialcomm_given = FALSE; |
159 | ||
27fd2eaa | 160 | sr_spew("scan() called!"); |
f5792417 MH |
161 | |
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 | ||
179 | if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) | |
180 | return NULL; | |
181 | ||
182 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) { | |
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)); | |
f5792417 | 207 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, VENDOR_GMC, |
873e0c12 | 208 | gmc_model_str(model), ""))) |
f5792417 MH |
209 | return NULL; |
210 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
211 | sr_err("Device context malloc failed."); | |
212 | return NULL; | |
213 | } | |
214 | devc->model = model; | |
215 | devc->limit_samples = 0; | |
216 | devc->limit_msec = 0; | |
217 | devc->num_samples = 0; | |
218 | devc->elapsed_msec = g_timer_new(); | |
219 | devc->settings_ok = FALSE; | |
220 | ||
221 | sdi->conn = serial; | |
222 | sdi->priv = devc; | |
6392d599 | 223 | sdi->driver = &gmc_mh_1x_2x_rs232_driver_info; |
f5792417 MH |
224 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) |
225 | return NULL; | |
226 | sdi->probes = g_slist_append(sdi->probes, probe); | |
227 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
228 | devices = g_slist_append(devices, sdi); | |
229 | } | |
7b4edcb6 MH |
230 | |
231 | return devices; | |
232 | } | |
233 | ||
27fd2eaa | 234 | static GSList *dev_list(void) |
7b4edcb6 | 235 | { |
6392d599 | 236 | return ((struct drv_context *)(gmc_mh_1x_2x_rs232_driver_info.priv))->instances; |
7b4edcb6 MH |
237 | } |
238 | ||
27fd2eaa | 239 | static int dev_clear(void) |
7b4edcb6 | 240 | { |
6392d599 | 241 | return std_dev_clear(&gmc_mh_1x_2x_rs232_driver_info, NULL); |
7b4edcb6 MH |
242 | } |
243 | ||
7b4edcb6 MH |
244 | static int dev_close(struct sr_dev_inst *sdi) |
245 | { | |
f5792417 | 246 | struct dev_context *devc; |
7b4edcb6 | 247 | |
bf2c987f | 248 | std_serial_dev_close(sdi); |
7b4edcb6 MH |
249 | |
250 | sdi->status = SR_ST_INACTIVE; | |
251 | ||
f5792417 MH |
252 | /* Free dynamically allocated resources. */ |
253 | if ((devc = sdi->priv) && devc->elapsed_msec) { | |
254 | g_timer_destroy(devc->elapsed_msec); | |
255 | devc->elapsed_msec = NULL; | |
873e0c12 | 256 | devc->model = METRAHIT_NONE; |
f5792417 MH |
257 | } |
258 | ||
7b4edcb6 MH |
259 | return SR_OK; |
260 | } | |
261 | ||
27fd2eaa | 262 | static int cleanup(void) |
7b4edcb6 | 263 | { |
27fd2eaa | 264 | return dev_clear(); |
7b4edcb6 MH |
265 | } |
266 | ||
6392d599 | 267 | /** Get value of configuration item */ |
7b4edcb6 MH |
268 | static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, |
269 | const struct sr_probe_group *probe_group) | |
270 | { | |
271 | int ret; | |
6392d599 MH |
272 | ret = SR_OK; |
273 | struct dev_context *devc; | |
7b4edcb6 | 274 | |
7b4edcb6 MH |
275 | (void)probe_group; |
276 | ||
6392d599 MH |
277 | if (!sdi || !(devc = sdi->priv)) |
278 | return SR_ERR_ARG; | |
279 | ||
7b4edcb6 MH |
280 | ret = SR_OK; |
281 | switch (key) { | |
6392d599 MH |
282 | case SR_CONF_LIMIT_SAMPLES: |
283 | *data = g_variant_new_uint64(devc->limit_samples); | |
284 | break; | |
285 | case SR_CONF_LIMIT_MSEC: | |
286 | *data = g_variant_new_uint64(devc->limit_msec); | |
287 | break; | |
7b4edcb6 MH |
288 | default: |
289 | return SR_ERR_NA; | |
290 | } | |
291 | ||
292 | return ret; | |
293 | } | |
294 | ||
295 | static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi, | |
296 | const struct sr_probe_group *probe_group) | |
297 | { | |
f5792417 | 298 | struct dev_context *devc; |
7b4edcb6 | 299 | |
7b4edcb6 MH |
300 | (void)probe_group; |
301 | ||
302 | if (sdi->status != SR_ST_ACTIVE) | |
303 | return SR_ERR_DEV_CLOSED; | |
304 | ||
f5792417 MH |
305 | if (!(devc = sdi->priv)) { |
306 | sr_err("sdi->priv was NULL."); | |
307 | return SR_ERR_BUG; | |
308 | } | |
309 | ||
7b4edcb6 | 310 | switch (key) { |
f5792417 MH |
311 | case SR_CONF_LIMIT_MSEC: |
312 | if (g_variant_get_uint64(data) == 0) { | |
313 | sr_err("LIMIT_MSEC can't be 0."); | |
314 | return SR_ERR; | |
315 | } | |
316 | devc->limit_msec = g_variant_get_uint64(data); | |
317 | sr_dbg("Setting time limit to %" PRIu64 "ms.", | |
318 | devc->limit_msec); | |
319 | break; | |
320 | case SR_CONF_LIMIT_SAMPLES: | |
321 | devc->limit_samples = g_variant_get_uint64(data); | |
322 | sr_dbg("Setting sample limit to %" PRIu64 ".", | |
323 | devc->limit_samples); | |
324 | break; | |
7b4edcb6 | 325 | default: |
f5792417 | 326 | return SR_ERR_NA; |
7b4edcb6 MH |
327 | } |
328 | ||
f5792417 | 329 | return SR_OK; |
7b4edcb6 MH |
330 | } |
331 | ||
332 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, | |
333 | const struct sr_probe_group *probe_group) | |
334 | { | |
7b4edcb6 | 335 | (void)sdi; |
ec29a878 | 336 | (void)probe_group; |
7b4edcb6 | 337 | |
7b4edcb6 | 338 | switch (key) { |
f5792417 MH |
339 | case SR_CONF_SCAN_OPTIONS: |
340 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
341 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
342 | break; | |
343 | case SR_CONF_DEVICE_OPTIONS: | |
344 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
345 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
346 | break; | |
7b4edcb6 MH |
347 | default: |
348 | return SR_ERR_NA; | |
349 | } | |
350 | ||
f5792417 | 351 | return SR_OK; |
7b4edcb6 MH |
352 | } |
353 | ||
27fd2eaa | 354 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
7b4edcb6 | 355 | { |
f5792417 MH |
356 | struct dev_context *devc; |
357 | struct sr_serial_dev_inst *serial; | |
358 | ||
359 | if (!sdi || !cb_data || !(devc = sdi->priv)) | |
360 | return SR_ERR_BUG; | |
7b4edcb6 MH |
361 | |
362 | if (sdi->status != SR_ST_ACTIVE) | |
363 | return SR_ERR_DEV_CLOSED; | |
364 | ||
f5792417 MH |
365 | devc->cb_data = cb_data; |
366 | devc->settings_ok = FALSE; | |
367 | devc->buflen = 0; | |
368 | ||
369 | /* Send header packet to the session bus. */ | |
370 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
371 | ||
372 | /* Start timer, if required. */ | |
373 | if (devc->limit_msec) | |
374 | g_timer_start(devc->elapsed_msec); | |
375 | ||
376 | /* Poll every 40ms, or whenever some data comes in. */ | |
377 | serial = sdi->conn; | |
abc4b335 | 378 | serial_source_add(serial, G_IO_IN, 40, gmc_mh_1x_2x_receive_data, |
f5792417 | 379 | (void *)sdi); |
7b4edcb6 MH |
380 | |
381 | return SR_OK; | |
382 | } | |
383 | ||
27fd2eaa | 384 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
7b4edcb6 | 385 | { |
f5792417 | 386 | struct dev_context *devc; |
7b4edcb6 | 387 | |
f5792417 MH |
388 | /* Stop timer, if required. */ |
389 | if (sdi && (devc = sdi->priv) && devc->limit_msec) | |
390 | g_timer_stop(devc->elapsed_msec); | |
7b4edcb6 | 391 | |
d43b0908 | 392 | return std_serial_dev_acquisition_stop(sdi, cb_data, dev_close, |
f5792417 | 393 | sdi->conn, LOG_PREFIX); |
7b4edcb6 MH |
394 | } |
395 | ||
396 | SR_PRIV struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info = { | |
397 | .name = "gmc-mh-1x-2x-rs232", | |
27fd2eaa | 398 | .longname = "Gossen Metrawatt Metrahit 1x/2x, 'RS232' interface", |
7b4edcb6 | 399 | .api_version = 1, |
27fd2eaa UH |
400 | .init = init, |
401 | .cleanup = cleanup, | |
402 | .scan = scan, | |
403 | .dev_list = dev_list, | |
404 | .dev_clear = dev_clear, | |
7b4edcb6 MH |
405 | .config_get = config_get, |
406 | .config_set = config_set, | |
407 | .config_list = config_list, | |
854434de | 408 | .dev_open = std_serial_dev_open, |
7b4edcb6 | 409 | .dev_close = dev_close, |
27fd2eaa UH |
410 | .dev_acquisition_start = dev_acquisition_start, |
411 | .dev_acquisition_stop = dev_acquisition_stop, | |
7b4edcb6 MH |
412 | .priv = NULL, |
413 | }; |