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