]> sigrok.org Git - libsigrok.git/blob - src/hardware/gmc-mh-1x-2x/api.c
uni-t-ut181a: silence compiler warning, use of uninitialized variable
[libsigrok.git] / src / hardware / gmc-mh-1x-2x / api.c
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
29 static const uint32_t scanopts[] = {
30         SR_CONF_CONN,
31         SR_CONF_SERIALCOMM,
32 };
33
34 static 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. */
40 static 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. */
47 static 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  */
67 static 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  */
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
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         serial_flush(serial);
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         model = scan_model_sm(serial);
180
181         /*
182          * If detection failed and no user-supplied parameters,
183          * try second baud rate.
184          */
185         if ((model == METRAHIT_NONE) && !serialcomm_given) {
186                 serialcomm = SERIALCOMM_1X_RS232;
187                 g_free(serial->serialcomm);
188                 serial->serialcomm = g_strdup(serialcomm);
189                 if (serial_set_paramstr(serial, serialcomm) == SR_OK)
190                         model = scan_model_sm(serial);
191         }
192
193         if (model != METRAHIT_NONE) {
194                 sr_spew("%s detected!", gmc_model_str(model));
195                 sdi = g_malloc0(sizeof(struct sr_dev_inst));
196                 sdi->status = SR_ST_INACTIVE;
197                 sdi->vendor = g_strdup("Gossen Metrawatt");
198                 sdi->model = g_strdup(gmc_model_str(model));
199                 devc = g_malloc0(sizeof(struct dev_context));
200                 sr_sw_limits_init(&devc->limits);
201                 devc->model = model;
202                 devc->settings_ok = FALSE;
203                 sdi->conn = serial;
204                 sdi->priv = devc;
205                 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
206                 devices = g_slist_append(devices, sdi);
207         }
208
209         return std_scan_complete(di, devices);
210 }
211
212 /**
213  * Scan for Metrahit 2x in a bidirectional mode using Gossen Metrawatt
214  * 'BD 232' interface.
215  */
216 static GSList *scan_2x_bd232(struct sr_dev_driver *di, GSList *options)
217 {
218         struct sr_dev_inst *sdi;
219         struct dev_context *devc;
220         struct sr_config *src;
221         struct sr_serial_dev_inst *serial;
222         GSList *l, *devices;
223         const char *conn, *serialcomm;
224         int cnt, byte;
225         gint64 timeout_us;
226
227         sdi = NULL;
228         devc = NULL;
229         conn = serialcomm = NULL;
230         devices = NULL;
231
232         for (l = options; l; l = l->next) {
233                 src = l->data;
234                 switch (src->key) {
235                 case SR_CONF_CONN:
236                         conn = g_variant_get_string(src->data, NULL);
237                         break;
238                 case SR_CONF_SERIALCOMM:
239                         serialcomm = g_variant_get_string(src->data, NULL);
240                         break;
241                 }
242         }
243         if (!conn)
244                 return NULL;
245         if (!serialcomm)
246                 serialcomm = SERIALCOMM_2X;
247
248         serial = sr_serial_dev_inst_new(conn, serialcomm);
249
250         if (serial_open(serial, SERIAL_RDWR) != SR_OK)
251                 goto exit_err;
252
253         devc = g_malloc0(sizeof(struct dev_context));
254
255         sdi = g_malloc0(sizeof(struct sr_dev_inst));
256         sdi->status = SR_ST_INACTIVE;
257         sdi->vendor = g_strdup("Gossen Metrawatt");
258         sdi->priv = devc;
259
260         /* Send message 03 "Query multimeter version and status" */
261         sdi->conn = serial;
262         if (req_stat14(sdi, TRUE) != SR_OK)
263                 goto exit_err;
264
265         /* Wait for reply from device(s) for up to 2s. */
266         timeout_us = g_get_monotonic_time() + (2 * 1000 * 1000);
267
268         while (timeout_us > g_get_monotonic_time()) {
269                 /* Receive reply (14 bytes) */
270                 devc->buflen = 0;
271                 for (cnt = 0; cnt < GMC_REPLY_SIZE; cnt++) {
272                         byte = read_byte(serial, timeout_us);
273                         if (byte != -1)
274                                 devc->buf[devc->buflen++] = (byte & MASK_6BITS);
275                 }
276
277                 if (devc->buflen != GMC_REPLY_SIZE)
278                         continue;
279
280                 devc->addr = devc->buf[0];
281                 process_msg14(sdi);
282                 devc->buflen = 0;
283
284                 if (devc->model != METRAHIT_NONE) {
285                         sr_spew("%s detected!", gmc_model_str(devc->model));
286                         sr_sw_limits_init(&devc->limits);
287                         sdi->model = g_strdup(gmc_model_str(devc->model));
288                         sdi->version = g_strdup_printf("Firmware %d.%d", devc->fw_ver_maj, devc->fw_ver_min);
289                         sdi->conn = serial;
290                         sdi->priv = devc;
291                         sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
292                         devices = g_slist_append(devices, sdi);
293                         devc = g_malloc0(sizeof(struct dev_context));
294                         sdi = g_malloc0(sizeof(struct sr_dev_inst));
295                         sdi->status = SR_ST_INACTIVE;
296                         sdi->vendor = g_strdup("Gossen Metrawatt");
297                 }
298         };
299
300         /* Free last alloc that was done in preparation. */
301         g_free(devc);
302         sr_dev_inst_free(sdi);
303
304         return std_scan_complete(di, devices);
305
306 exit_err:
307         sr_serial_dev_inst_free(serial);
308         g_free(devc);
309         sr_dev_inst_free(sdi);
310
311         return NULL;
312 }
313
314 static int dev_close(struct sr_dev_inst *sdi)
315 {
316         struct dev_context *devc;
317
318         devc = sdi->priv;
319
320         devc->model = METRAHIT_NONE;
321
322         return std_serial_dev_close(sdi);
323 }
324
325 static int config_get(uint32_t key, GVariant **data,
326         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
327 {
328         struct dev_context *devc;
329
330         (void)cg;
331
332         if (!sdi)
333                 return SR_ERR_ARG;
334
335         devc = sdi->priv;
336
337         switch (key) {
338         case SR_CONF_LIMIT_SAMPLES:
339         case SR_CONF_LIMIT_MSEC:
340                 return sr_sw_limits_config_get(&devc->limits, key, data);
341         case SR_CONF_POWER_OFF:
342                 *data = g_variant_new_boolean(FALSE);
343                 break;
344         default:
345                 return SR_ERR_NA;
346         }
347
348         return SR_OK;
349 }
350
351 /** Implementation of config_list for Metrahit 1x/2x send mode */
352 static int config_list_sm(uint32_t key, GVariant **data,
353         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
354 {
355         return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts_sm);
356 }
357
358 /** Implementation of config_list for Metrahit 2x bidirectional mode */
359 static int config_list_bd(uint32_t key, GVariant **data,
360         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
361 {
362         return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts_bd);
363 }
364
365 static int dev_acquisition_start_1x_2x_rs232(const struct sr_dev_inst *sdi)
366 {
367         struct dev_context *devc;
368         struct sr_serial_dev_inst *serial;
369
370         devc = sdi->priv;
371         devc->settings_ok = FALSE;
372         devc->buflen = 0;
373
374         sr_sw_limits_acquisition_start(&devc->limits);
375         std_session_send_df_header(sdi);
376
377         serial = sdi->conn;
378         serial_source_add(sdi->session, serial, G_IO_IN, 40,
379                         gmc_mh_1x_2x_receive_data, (void *)sdi);
380
381         return SR_OK;
382 }
383
384 static int dev_acquisition_start_2x_bd232(const struct sr_dev_inst *sdi)
385 {
386         struct dev_context *devc;
387         struct sr_serial_dev_inst *serial;
388
389         devc = sdi->priv;
390         devc->settings_ok = FALSE;
391         devc->buflen = 0;
392
393         sr_sw_limits_acquisition_start(&devc->limits);
394         std_session_send_df_header(sdi);
395
396         serial = sdi->conn;
397         serial_source_add(sdi->session, serial, G_IO_IN, 40,
398                         gmc_mh_2x_receive_data, (void *)sdi);
399
400         /* Send start message */
401         return req_meas14(sdi);
402 }
403
404 static struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info = {
405         .name = "gmc-mh-1x-2x-rs232",
406         .longname = "Gossen Metrawatt Metrahit 1x/2x, RS232 interface",
407         .api_version = 1,
408         .init = std_init,
409         .cleanup = std_cleanup,
410         .scan = scan_1x_2x_rs232,
411         .dev_list = std_dev_list,
412         .dev_clear = std_dev_clear,
413         .config_get = config_get,
414         .config_set = config_set,
415         .config_list = config_list_sm,
416         .dev_open = std_serial_dev_open,
417         .dev_close = dev_close,
418         .dev_acquisition_start = dev_acquisition_start_1x_2x_rs232,
419         .dev_acquisition_stop = std_serial_dev_acquisition_stop,
420         .context = NULL,
421 };
422 SR_REGISTER_DEV_DRIVER(gmc_mh_1x_2x_rs232_driver_info);
423
424 static struct sr_dev_driver gmc_mh_2x_bd232_driver_info = {
425         .name = "gmc-mh-2x-bd232",
426         .longname = "Gossen Metrawatt Metrahit 2x, BD232/SI232-II interface",
427         .api_version = 1,
428         .init = std_init,
429         .cleanup = std_cleanup,
430         .scan = scan_2x_bd232,
431         .dev_list = std_dev_list,
432         .dev_clear = std_dev_clear,
433         .config_get = config_get,
434         .config_set = config_set,
435         .config_list = config_list_bd,
436         .dev_open = std_serial_dev_open,
437         .dev_close = dev_close,
438         .dev_acquisition_start = dev_acquisition_start_2x_bd232,
439         .dev_acquisition_stop = std_serial_dev_acquisition_stop,
440         .context = NULL,
441 };
442 SR_REGISTER_DEV_DRIVER(gmc_mh_2x_bd232_driver_info);