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