]> sigrok.org Git - libsigrok.git/blob - hardware/gmc-mh-1x-2x/api.c
acdf8413b078c18fc2fce0b01ce298806d6c984e
[libsigrok.git] / hardware / gmc-mh-1x-2x / api.c
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
20 #include <string.h>
21
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 VENDOR_GMC "Gossen Metrawatt"
28
29 SR_PRIV struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info;
30
31 static const int32_t hwopts[] = {
32         SR_CONF_CONN,
33         SR_CONF_SERIALCOMM,
34 };
35
36 static const int32_t hwcaps[] = {
37         SR_CONF_MULTIMETER,
38         SR_CONF_LIMIT_SAMPLES,
39         SR_CONF_LIMIT_MSEC,
40         SR_CONF_CONTINUOUS,
41 };
42
43 /** Driver init function */
44 static int init_1x_2x_rs232(struct sr_context *sr_ctx)
45 {
46         return std_init(sr_ctx, &gmc_mh_1x_2x_rs232_driver_info, LOG_PREFIX);
47 }
48
49
50 /** Read single byte from serial port.
51  *  \retval -1 Timeout or error.
52  *  \retval other Byte.
53  */
54 static int read_byte(struct sr_serial_dev_inst *serial, gint64 timeout)
55 {
56         guint8 result = 0;
57         int rc = 0;
58
59         for (;;) {
60                 rc = serial_read(serial, &result, 1);
61                 if (rc == 1) {
62                         sr_spew("read: 0x%02x/%d", result, result);
63                         return result;
64                 }
65                 if (g_get_monotonic_time() > timeout)
66                         return -1;
67                 g_usleep(2000);
68         }
69 }
70
71 /** Try to detect GMC 1x/2x multimeter model in send mode for max. 1 second.
72  *  \param serial Configured, open serial port.
73  *
74  *  \retval NULL Detection failed.
75  *  \retval other Model code.
76  */
77 static enum model scan_model_sm(struct sr_serial_dev_inst *serial)
78 {
79         int byte, bytecnt, cnt;
80         enum model model;
81         gint64 timeout_us;
82
83         model = SR_METRAHIT_NONE;
84         timeout_us = g_get_monotonic_time() + 1*1000*1000;
85
86         /* Try to find message consisting of device code and several
87          * (at least 4) data bytes. */
88         for (bytecnt = 0; bytecnt < 100; bytecnt++) {
89                 byte = read_byte(serial, timeout_us);
90                 if ((byte == -1) || (timeout_us < g_get_monotonic_time()))
91                         break;
92                 if ((byte & MSGID_MASK) == MSGID_INF) {
93                         if (!(model = sr_gmc_decode_model_sm(byte & MSGC_MASK)))
94                                 break;
95                         /* Now expect (at least) 4 data bytes */
96                         for (cnt = 0; cnt < 4; cnt++) {
97                                 byte = read_byte(serial, timeout_us);
98                                 if ((byte == -1) ||
99                                         ((byte & MSGID_MASK) != MSGID_DATA))
100                                 {
101                                         model = SR_METRAHIT_NONE;
102                                         bytecnt = 100;
103                                         break;
104                                 }
105                         }
106                         break;
107                 }
108         }
109
110         return model;
111 }
112
113 /** Scan for Metrahit 1x and Metrahit 2x in send mode using Gossen Metrawatt
114  *  'RS232' interface. The older 1x models use 8192 and the newer 2x 9600 baud.
115  *  The DMM usually sends up to about 20 messages per second. However, depending
116  *  on configuration and measurement mode the intervals can be much larger and
117  *  then the detection might not work.
118  */
119 static GSList *scan_1x_2x_rs232(GSList *options)
120 {
121         struct sr_dev_inst *sdi;
122         struct drv_context *drvc;
123         struct dev_context *devc;
124         struct sr_config *src;
125         struct sr_probe *probe;
126         struct sr_serial_dev_inst *serial;
127         GSList *l, *devices;
128         const char *conn, *serialcomm;
129         enum model model;
130         gboolean serialcomm_given;
131         int cnt;
132
133         devices = NULL;
134         drvc = (&gmc_mh_1x_2x_rs232_driver_info)->priv;
135         drvc->instances = NULL;
136         conn = serialcomm = NULL;
137         model = SR_METRAHIT_NONE;
138         serialcomm_given = FALSE;
139
140         sr_spew("scan_1x_2x_rs232() called!");
141
142         for (l = options; l; l = l->next) {
143                 src = l->data;
144                 switch (src->key) {
145                 case SR_CONF_CONN:
146                         conn = g_variant_get_string(src->data, NULL);
147                         break;
148                 case SR_CONF_SERIALCOMM:
149                         serialcomm = g_variant_get_string(src->data, NULL);
150                         serialcomm_given = TRUE;
151                         break;
152                 }
153         }
154         if (!conn)
155                 return NULL;
156         if (!serialcomm)
157                 serialcomm = SERIALCOMM_2X_RS232;
158
159         if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
160                 return NULL;
161
162         if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) {
163                 sr_serial_dev_inst_free(serial);
164                 return NULL;
165         }
166
167         serial_flush(serial);
168
169         model = scan_model_sm(serial);
170
171         /* If detection failed and no user-supplied parameters,
172          * try second baud rate. */
173         if ((model == SR_METRAHIT_NONE) && !serialcomm_given) {
174                 serialcomm = SERIALCOMM_1X_RS232;
175                 g_free(serial->serialcomm);
176                 serial->serialcomm = g_strdup(serialcomm);
177                 if (serial_set_paramstr(serial, serialcomm) == SR_OK) {
178                         serial_flush(serial);
179                         model = scan_model_sm(serial);
180                 }
181         }
182
183         if (model != SR_METRAHIT_NONE) {
184                 sr_spew("%s %s detected!", VENDOR_GMC, sr_gmc_model_str(model));
185                 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, VENDOR_GMC,
186                                 sr_gmc_model_str(model), "")))
187                         return NULL;
188                 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
189                         sr_err("Device context malloc failed.");
190                         return NULL;
191                 }
192                 devc->model = model;
193                 devc->limit_samples = 0;
194                 devc->limit_msec = 0;
195                 devc->num_samples = 0;
196                 devc->elapsed_msec = g_timer_new();
197                 devc->settings_ok = FALSE;
198
199                 sdi->conn = serial;
200                 sdi->priv = devc;
201                 sdi->driver = &gmc_mh_1x_2x_rs232_driver_info;
202                 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
203                         return NULL;
204                 sdi->probes = g_slist_append(sdi->probes, probe);
205                 drvc->instances = g_slist_append(drvc->instances, sdi);
206                 devices = g_slist_append(devices, sdi);
207         }
208
209         return devices;
210 }
211
212 /** Driver device list function */
213 static GSList *dev_list_1x_2x_rs232(void)
214 {
215         return ((struct drv_context *)(gmc_mh_1x_2x_rs232_driver_info.priv))
216                         ->instances;
217 }
218
219 static int dev_clear_1x_2x_rs232(void)
220 {
221         return std_dev_clear(&gmc_mh_1x_2x_rs232_driver_info, NULL);
222 }
223
224 static int dev_open(struct sr_dev_inst *sdi)
225 {
226         struct sr_serial_dev_inst *serial = sdi->conn;
227         if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
228                 return SR_ERR;
229
230         sdi->status = SR_ST_ACTIVE;
231
232         return SR_OK;
233 }
234
235 static int dev_close(struct sr_dev_inst *sdi)
236 {
237         struct dev_context *devc;
238
239         std_serial_dev_close(sdi);
240
241         sdi->status = SR_ST_INACTIVE;
242
243         /* Free dynamically allocated resources. */
244         if ((devc = sdi->priv) && devc->elapsed_msec) {
245                 g_timer_destroy(devc->elapsed_msec);
246                 devc->elapsed_msec = NULL;
247                 devc->model = SR_METRAHIT_NONE;
248         }
249
250         return SR_OK;
251 }
252
253 static int cleanup_sm_rs232(void)
254 {
255         return dev_clear_1x_2x_rs232();
256 }
257
258 /** TODO */
259 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
260                 const struct sr_probe_group *probe_group)
261 {
262         int ret;
263
264         (void)sdi;
265         (void)data;
266         (void)probe_group;
267
268         ret = SR_OK;
269         switch (key) {
270         /* TODO */
271         default:
272                 return SR_ERR_NA;
273         }
274
275         return ret;
276 }
277
278 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
279                 const struct sr_probe_group *probe_group)
280 {
281         struct dev_context *devc;
282
283         (void)probe_group;
284
285         if (sdi->status != SR_ST_ACTIVE)
286                 return SR_ERR_DEV_CLOSED;
287
288         if (!(devc = sdi->priv)) {
289                 sr_err("sdi->priv was NULL.");
290                 return SR_ERR_BUG;
291         }
292
293         switch (key) {
294         case SR_CONF_LIMIT_MSEC:
295                 if (g_variant_get_uint64(data) == 0) {
296                         sr_err("LIMIT_MSEC can't be 0.");
297                         return SR_ERR;
298                 }
299                 devc->limit_msec = g_variant_get_uint64(data);
300                 sr_dbg("Setting time limit to %" PRIu64 "ms.",
301                        devc->limit_msec);
302                 break;
303         case SR_CONF_LIMIT_SAMPLES:
304                 devc->limit_samples = g_variant_get_uint64(data);
305                 sr_dbg("Setting sample limit to %" PRIu64 ".",
306                        devc->limit_samples);
307                 break;
308         default:
309                 return SR_ERR_NA;
310         }
311
312         return SR_OK;
313 }
314
315 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
316                 const struct sr_probe_group *probe_group)
317 {
318         (void)sdi;
319
320         switch (key) {
321         case SR_CONF_SCAN_OPTIONS:
322                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
323                                 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
324                 break;
325         case SR_CONF_DEVICE_OPTIONS:
326                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
327                                 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
328                 break;
329         default:
330                 return SR_ERR_NA;
331         }
332
333         return SR_OK;
334 }
335
336 static int dev_acq_start_1x_2x_rs232(const struct sr_dev_inst *sdi,
337                                     void *cb_data)
338 {
339         struct dev_context *devc;
340         struct sr_serial_dev_inst *serial;
341
342         if (!sdi || !cb_data || !(devc = sdi->priv))
343                 return SR_ERR_BUG;
344
345         if (sdi->status != SR_ST_ACTIVE)
346                 return SR_ERR_DEV_CLOSED;
347
348         devc->cb_data = cb_data;
349         devc->settings_ok = FALSE;
350         devc->buflen = 0;
351
352         /* Send header packet to the session bus. */
353         std_session_send_df_header(cb_data, LOG_PREFIX);
354
355         /* Start timer, if required. */
356         if (devc->limit_msec)
357                 g_timer_start(devc->elapsed_msec);
358
359         /* Poll every 40ms, or whenever some data comes in. */
360         serial = sdi->conn;
361         serial_source_add(serial, G_IO_IN, 40, gmc_mh_1x_2x_receive_data,
362                 (void *)sdi);
363
364         return SR_OK;
365 }
366
367 static int dev_acq_stop(struct sr_dev_inst *sdi, void *cb_data)
368 {
369         struct dev_context *devc;
370
371         /* Stop timer, if required. */
372         if (sdi && (devc = sdi->priv) && devc->limit_msec)
373                 g_timer_stop(devc->elapsed_msec);
374
375         return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
376                         sdi->conn, LOG_PREFIX);
377 }
378
379 SR_PRIV struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info = {
380         .name = "gmc-mh-1x-2x-rs232",
381         .longname =
382                 "Gossen Metrawatt Metrahit 1x/2x DMMs, 'RS232' Interface",
383         .api_version = 1,
384         .init = init_1x_2x_rs232,
385         .cleanup = cleanup_sm_rs232,
386         .scan = scan_1x_2x_rs232,
387         .dev_list = dev_list_1x_2x_rs232,
388         .dev_clear = dev_clear_1x_2x_rs232,
389         .config_get = config_get,
390         .config_set = config_set,
391         .config_list = config_list,
392         .dev_open = dev_open,
393         .dev_close = dev_close,
394         .dev_acquisition_start = dev_acq_start_1x_2x_rs232,
395         .dev_acquisition_stop = dev_acq_stop,
396         .priv = NULL,
397 };