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