]> sigrok.org Git - libsigrok.git/blame - src/hardware/norma-dmm/api.c
Improve readability and clarity of some numbers.
[libsigrok.git] / src / hardware / norma-dmm / api.c
CommitLineData
bfd48770
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
a4e435eb
MH
20/** @file
21 * Norma DM9x0/Siemens B102x DMMs driver.
22 * @internal
23 */
24
bfd48770
MH
25#include "protocol.h"
26
a0e0bb41 27static const uint32_t scanopts[] = {
f8e76e2e
MH
28 SR_CONF_CONN,
29 SR_CONF_SERIALCOMM,
30};
31
f254bc4b 32static const uint32_t devopts[] = {
f8e76e2e 33 SR_CONF_MULTIMETER,
f8e76e2e 34 SR_CONF_CONTINUOUS,
5827f61b
BV
35 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
36 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
f8e76e2e
MH
37};
38
e790bd5c 39#define BUF_MAX 50
f8e76e2e
MH
40
41#define SERIALCOMM "4800/8n1/dtr=1/rts=0/flow=1"
42
bfd48770 43SR_PRIV struct sr_dev_driver norma_dmm_driver_info;
49c06128 44SR_PRIV struct sr_dev_driver siemens_b102x_driver_info;
bfd48770 45
49c06128 46static const char* get_brandstr(struct sr_dev_driver* drv)
bfd48770 47{
49c06128
MH
48 if (drv == &norma_dmm_driver_info)
49 return "Norma";
50 else
51 return "Siemens";
bfd48770
MH
52}
53
49c06128
MH
54static const char* get_typestr(int type, struct sr_dev_driver* drv)
55{
56 static const char* nameref[5][2] = {
57 {"DM910", "B1024"},
58 {"DM920", "B1025"},
59 {"DM930", "B1026"},
60 {"DM940", "B1027"},
61 {"DM950", "B1028"}};
62
63 if ((type < 1) || (type > 5))
64 return "Unknown type!";
65
66 return nameref[type-1][(drv == &siemens_b102x_driver_info)];
67}
68
4f840ce9 69static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
49c06128 70{
4f840ce9 71 return std_init(sr_ctx, di, LOG_PREFIX);
49c06128
MH
72}
73
4f840ce9 74static GSList *scan(struct sr_dev_driver* drv, GSList *options)
bfd48770 75{
f8e76e2e 76 struct sr_dev_inst *sdi;
bfd48770 77 struct drv_context *drvc;
f8e76e2e
MH
78 struct dev_context *devc;
79 struct sr_config *src;
f8e76e2e
MH
80 struct sr_serial_dev_inst *serial;
81 GSList *l, *devices;
82 int len, cnt;
83 const char *conn, *serialcomm;
84 char *buf;
f8e76e2e
MH
85 char req[10];
86 int auxtype;
87
bfd48770 88 devices = NULL;
49c06128 89 drvc = drv->priv;
bfd48770 90 drvc->instances = NULL;
f8e76e2e
MH
91 conn = serialcomm = NULL;
92
93 for (l = options; l; l = l->next) {
94 src = l->data;
95 switch (src->key) {
96 case SR_CONF_CONN:
97 conn = g_variant_get_string(src->data, NULL);
98 break;
99 case SR_CONF_SERIALCOMM:
100 serialcomm = g_variant_get_string(src->data, NULL);
101 break;
102 }
103 }
104 if (!conn)
105 return NULL;
106 if (!serialcomm)
107 serialcomm = SERIALCOMM;
108
91219afc 109 serial = sr_serial_dev_inst_new(conn, serialcomm);
f8e76e2e 110
d12ef776 111 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
f8e76e2e
MH
112 return NULL;
113
114 serial_flush(serial);
bfd48770 115
e790bd5c 116 if (!(buf = g_try_malloc(BUF_MAX))) {
f8e76e2e
MH
117 sr_err("Serial buffer malloc failed.");
118 return NULL;
119 }
120
121 snprintf(req, sizeof(req), "%s\r\n",
e790bd5c 122 nmadmm_requests[NMADMM_REQ_IDN].req_str);
a4e435eb 123 g_usleep(150 * 1000); /* Wait a little to allow serial port to settle. */
f8e76e2e 124 for (cnt = 0; cnt < 7; cnt++) {
eead2782 125 if (serial_write_blocking(serial, req, strlen(req), 0) < 0) {
8849f45a 126 sr_err("Unable to send identification request.");
f8e76e2e
MH
127 return NULL;
128 }
129 len = BUF_MAX;
a4e435eb 130 serial_readline(serial, &buf, &len, NMADMM_TIMEOUT_MS);
f8e76e2e
MH
131 if (!len)
132 continue;
e790bd5c 133 buf[BUF_MAX - 1] = '\0';
f8e76e2e 134
e790bd5c
UH
135 /* Match ID string, e.g. "1834 065 V1.06,IF V1.02" (DM950) */
136 if (g_regex_match_simple("^1834 [^,]*,IF V*", (char *)buf, 0, 0)) {
f8e76e2e 137 auxtype = xgittoint(buf[7]);
49c06128 138 sr_spew("%s %s DMM %s detected!", get_brandstr(drv), get_typestr(auxtype, drv), buf + 9);
f8e76e2e 139
aac29cc1 140 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
141 sdi->status = SR_ST_INACTIVE;
142 sdi->vendor = g_strdup(get_brandstr(drv));
143 sdi->model = g_strdup(get_typestr(auxtype, drv));
144 sdi->version = g_strdup(buf + 9);
f57d8ffe 145 devc = g_malloc0(sizeof(struct dev_context));
f8e76e2e
MH
146 devc->type = auxtype;
147 devc->version = g_strdup(&buf[9]);
148 devc->elapsed_msec = g_timer_new();
f8e76e2e
MH
149 sdi->conn = serial;
150 sdi->priv = devc;
49c06128 151 sdi->driver = drv;
5e23fcab 152 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
f8e76e2e
MH
153 drvc->instances = g_slist_append(drvc->instances, sdi);
154 devices = g_slist_append(devices, sdi);
155 break;
156 }
e790bd5c
UH
157
158 /*
159 * The interface of the DM9x0 contains a cap that needs to
160 * charge for up to 10s before the interface works, if not
161 * powered externally. Therefore wait a little to improve
162 * chances.
163 */
f8e76e2e
MH
164 if (cnt == 3) {
165 sr_info("Waiting 5s to allow interface to settle.");
166 g_usleep(5 * 1000 * 1000);
167 }
168 }
169
170 g_free(buf);
171
172 serial_close(serial);
173 if (!devices)
174 sr_serial_dev_inst_free(serial);
bfd48770
MH
175
176 return devices;
177}
178
4f840ce9 179static GSList *dev_list(const struct sr_dev_driver *di)
49c06128 180{
4f840ce9 181 return ((struct drv_context *)(di->priv))->instances;
bfd48770
MH
182}
183
bfd48770
MH
184static int dev_close(struct sr_dev_inst *sdi)
185{
f8e76e2e 186 struct dev_context *devc;
bfd48770 187
bf2c987f 188 std_serial_dev_close(sdi);
bfd48770 189
e790bd5c 190 /* Free dynamically allocated resources. */
f8e76e2e
MH
191 if ((devc = sdi->priv) && devc->version) {
192 g_free(devc->version);
193 devc->version = NULL;
194 g_timer_destroy(devc->elapsed_msec);
195 }
bfd48770
MH
196
197 return SR_OK;
198}
199
4f840ce9 200static int cleanup(const struct sr_dev_driver *di)
bfd48770 201{
4f840ce9 202 return std_dev_clear(di, NULL);
bfd48770
MH
203}
204
584560f1 205static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 206 const struct sr_channel_group *cg)
bfd48770 207{
f8e76e2e 208 struct dev_context *devc;
bfd48770 209
53b4680f 210 (void)cg;
d3c74a6f 211
bfd48770
MH
212 if (sdi->status != SR_ST_ACTIVE)
213 return SR_ERR_DEV_CLOSED;
214
f8e76e2e
MH
215 if (!(devc = sdi->priv)) {
216 sr_err("sdi->priv was NULL.");
217 return SR_ERR_BUG;
218 }
219
bfd48770 220 switch (key) {
f8e76e2e 221 case SR_CONF_LIMIT_MSEC:
f8e76e2e 222 devc->limit_msec = g_variant_get_uint64(data);
f8e76e2e
MH
223 break;
224 case SR_CONF_LIMIT_SAMPLES:
225 devc->limit_samples = g_variant_get_uint64(data);
f8e76e2e 226 break;
bfd48770 227 default:
f8e76e2e 228 return SR_ERR_NA;
bfd48770
MH
229 }
230
f8e76e2e 231 return SR_OK;
bfd48770
MH
232}
233
584560f1 234static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 235 const struct sr_channel_group *cg)
bfd48770 236{
bfd48770 237 (void)sdi;
53b4680f 238 (void)cg;
bfd48770 239
bfd48770 240 switch (key) {
f8e76e2e 241 case SR_CONF_SCAN_OPTIONS:
584560f1 242 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
a0e0bb41 243 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
f8e76e2e
MH
244 break;
245 case SR_CONF_DEVICE_OPTIONS:
584560f1 246 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
f254bc4b 247 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
f8e76e2e 248 break;
bfd48770
MH
249 default:
250 return SR_ERR_NA;
251 }
252
f8e76e2e 253 return SR_OK;
bfd48770
MH
254}
255
256static int dev_acquisition_start(const struct sr_dev_inst *sdi,
257 void *cb_data)
258{
f8e76e2e
MH
259 struct dev_context *devc;
260 struct sr_serial_dev_inst *serial;
261
f8e76e2e
MH
262 if (!sdi || !cb_data || !(devc = sdi->priv))
263 return SR_ERR_BUG;
264
bfd48770
MH
265 if (sdi->status != SR_ST_ACTIVE)
266 return SR_ERR_DEV_CLOSED;
267
f8e76e2e
MH
268 devc->cb_data = cb_data;
269
270 /* Send header packet to the session bus. */
271 std_session_send_df_header(cb_data, LOG_PREFIX);
272
273 /* Start timer, if required. */
274 if (devc->limit_msec)
275 g_timer_start(devc->elapsed_msec);
276
277 /* Poll every 100ms, or whenever some data comes in. */
278 serial = sdi->conn;
102f1239
BV
279 serial_source_add(sdi->session, serial, G_IO_IN, 100,
280 norma_dmm_receive_data, (void *)sdi);
bfd48770
MH
281
282 return SR_OK;
283}
284
285static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
286{
f8e76e2e 287 struct dev_context *devc;
bfd48770 288
f8e76e2e
MH
289 /* Stop timer, if required. */
290 if (sdi && (devc = sdi->priv) && devc->limit_msec)
291 g_timer_stop(devc->elapsed_msec);
bfd48770 292
d43b0908 293 return std_serial_dev_acquisition_stop(sdi, cb_data, dev_close,
f8e76e2e 294 sdi->conn, LOG_PREFIX);
bfd48770
MH
295}
296
297SR_PRIV struct sr_dev_driver norma_dmm_driver_info = {
298 .name = "norma-dmm",
49c06128
MH
299 .longname = "Norma DM9x0 DMMs",
300 .api_version = 1,
4f840ce9
ML
301 .init = init,
302 .cleanup = cleanup,
303 .scan = scan,
304 .dev_list = dev_list,
49c06128
MH
305 .dev_clear = NULL,
306 .config_get = NULL,
307 .config_set = config_set,
308 .config_list = config_list,
309 .dev_open = std_serial_dev_open,
310 .dev_close = dev_close,
311 .dev_acquisition_start = dev_acquisition_start,
312 .dev_acquisition_stop = dev_acquisition_stop,
313 .priv = NULL,
314};
315
49c06128
MH
316SR_PRIV struct sr_dev_driver siemens_b102x_driver_info = {
317 .name = "siemens-b102x",
318 .longname = "Siemens B102x DMMs",
bfd48770 319 .api_version = 1,
4f840ce9
ML
320 .init = init,
321 .cleanup = cleanup,
322 .scan = scan,
323 .dev_list = dev_list,
a6630742 324 .dev_clear = NULL,
f8e76e2e 325 .config_get = NULL,
bfd48770
MH
326 .config_set = config_set,
327 .config_list = config_list,
854434de 328 .dev_open = std_serial_dev_open,
bfd48770
MH
329 .dev_close = dev_close,
330 .dev_acquisition_start = dev_acquisition_start,
331 .dev_acquisition_stop = dev_acquisition_stop,
332 .priv = NULL,
333};