]> sigrok.org Git - libsigrok.git/blame - src/hardware/norma-dmm/api.c
Don't reset instance list in scan() callback
[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
dd5c48a6
LPC
44static struct sr_dev_driver norma_dmm_driver_info;
45static 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
c442ffda 68static GSList *scan(struct sr_dev_driver *drv, GSList *options)
bfd48770 69{
f8e76e2e 70 struct sr_dev_inst *sdi;
bfd48770 71 struct drv_context *drvc;
f8e76e2e
MH
72 struct dev_context *devc;
73 struct sr_config *src;
f8e76e2e
MH
74 struct sr_serial_dev_inst *serial;
75 GSList *l, *devices;
0cadb8a3 76 int len, cnt, auxtype;
f8e76e2e
MH
77 const char *conn, *serialcomm;
78 char *buf;
f8e76e2e 79 char req[10];
f8e76e2e 80
bfd48770 81 devices = NULL;
338143ea 82 drvc = drv->context;
f8e76e2e
MH
83 conn = serialcomm = NULL;
84
85 for (l = options; l; l = l->next) {
86 src = l->data;
87 switch (src->key) {
88 case SR_CONF_CONN:
89 conn = g_variant_get_string(src->data, NULL);
90 break;
91 case SR_CONF_SERIALCOMM:
92 serialcomm = g_variant_get_string(src->data, NULL);
93 break;
94 }
95 }
96 if (!conn)
97 return NULL;
98 if (!serialcomm)
99 serialcomm = SERIALCOMM;
100
91219afc 101 serial = sr_serial_dev_inst_new(conn, serialcomm);
f8e76e2e 102
d12ef776 103 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
f8e76e2e
MH
104 return NULL;
105
106 serial_flush(serial);
bfd48770 107
a95f142e 108 buf = g_malloc(BUF_MAX);
f8e76e2e
MH
109
110 snprintf(req, sizeof(req), "%s\r\n",
e790bd5c 111 nmadmm_requests[NMADMM_REQ_IDN].req_str);
a4e435eb 112 g_usleep(150 * 1000); /* Wait a little to allow serial port to settle. */
f8e76e2e 113 for (cnt = 0; cnt < 7; cnt++) {
d545c81c
UH
114 if (serial_write_blocking(serial, req, strlen(req),
115 serial_timeout(serial, strlen(req))) < 0) {
8849f45a 116 sr_err("Unable to send identification request.");
f8e76e2e
MH
117 return NULL;
118 }
119 len = BUF_MAX;
a4e435eb 120 serial_readline(serial, &buf, &len, NMADMM_TIMEOUT_MS);
f8e76e2e
MH
121 if (!len)
122 continue;
e790bd5c 123 buf[BUF_MAX - 1] = '\0';
f8e76e2e 124
0cadb8a3 125 /* Match ID string, e.g. "1834 065 V1.06,IF V1.02" (DM950). */
e790bd5c 126 if (g_regex_match_simple("^1834 [^,]*,IF V*", (char *)buf, 0, 0)) {
f8e76e2e 127 auxtype = xgittoint(buf[7]);
49c06128 128 sr_spew("%s %s DMM %s detected!", get_brandstr(drv), get_typestr(auxtype, drv), buf + 9);
f8e76e2e 129
aac29cc1 130 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
131 sdi->status = SR_ST_INACTIVE;
132 sdi->vendor = g_strdup(get_brandstr(drv));
133 sdi->model = g_strdup(get_typestr(auxtype, drv));
134 sdi->version = g_strdup(buf + 9);
f57d8ffe 135 devc = g_malloc0(sizeof(struct dev_context));
37dbffd1 136 sr_sw_limits_init(&devc->limits);
f8e76e2e
MH
137 devc->type = auxtype;
138 devc->version = g_strdup(&buf[9]);
f8e76e2e
MH
139 sdi->conn = serial;
140 sdi->priv = devc;
49c06128 141 sdi->driver = drv;
5e23fcab 142 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
f8e76e2e
MH
143 drvc->instances = g_slist_append(drvc->instances, sdi);
144 devices = g_slist_append(devices, sdi);
145 break;
146 }
e790bd5c
UH
147
148 /*
149 * The interface of the DM9x0 contains a cap that needs to
150 * charge for up to 10s before the interface works, if not
151 * powered externally. Therefore wait a little to improve
152 * chances.
153 */
f8e76e2e
MH
154 if (cnt == 3) {
155 sr_info("Waiting 5s to allow interface to settle.");
156 g_usleep(5 * 1000 * 1000);
157 }
158 }
159
160 g_free(buf);
161
162 serial_close(serial);
163 if (!devices)
164 sr_serial_dev_inst_free(serial);
bfd48770
MH
165
166 return devices;
167}
168
bfd48770
MH
169static int dev_close(struct sr_dev_inst *sdi)
170{
f8e76e2e 171 struct dev_context *devc;
bfd48770 172
bf2c987f 173 std_serial_dev_close(sdi);
bfd48770 174
e790bd5c 175 /* Free dynamically allocated resources. */
f8e76e2e
MH
176 if ((devc = sdi->priv) && devc->version) {
177 g_free(devc->version);
178 devc->version = NULL;
f8e76e2e 179 }
bfd48770
MH
180
181 return SR_OK;
182}
183
584560f1 184static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 185 const struct sr_channel_group *cg)
bfd48770 186{
f8e76e2e 187 struct dev_context *devc;
bfd48770 188
53b4680f 189 (void)cg;
d3c74a6f 190
bfd48770
MH
191 if (sdi->status != SR_ST_ACTIVE)
192 return SR_ERR_DEV_CLOSED;
193
b0baddef 194 devc = sdi->priv;
f8e76e2e 195
37dbffd1 196 return sr_sw_limits_config_set(&devc->limits, key, data);
bfd48770
MH
197}
198
584560f1 199static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 200 const struct sr_channel_group *cg)
bfd48770 201{
bfd48770 202 (void)sdi;
53b4680f 203 (void)cg;
bfd48770 204
bfd48770 205 switch (key) {
f8e76e2e 206 case SR_CONF_SCAN_OPTIONS:
584560f1 207 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
a0e0bb41 208 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
f8e76e2e
MH
209 break;
210 case SR_CONF_DEVICE_OPTIONS:
584560f1 211 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
f254bc4b 212 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
f8e76e2e 213 break;
bfd48770
MH
214 default:
215 return SR_ERR_NA;
216 }
217
f8e76e2e 218 return SR_OK;
bfd48770
MH
219}
220
695dc859 221static int dev_acquisition_start(const struct sr_dev_inst *sdi)
bfd48770 222{
f8e76e2e
MH
223 struct dev_context *devc;
224 struct sr_serial_dev_inst *serial;
225
bfd48770
MH
226 if (sdi->status != SR_ST_ACTIVE)
227 return SR_ERR_DEV_CLOSED;
228
208c1d35 229 devc = sdi->priv;
f8e76e2e 230
37dbffd1 231 sr_sw_limits_acquisition_start(&devc->limits);
695dc859 232 std_session_send_df_header(sdi, LOG_PREFIX);
f8e76e2e 233
f8e76e2e
MH
234 /* Poll every 100ms, or whenever some data comes in. */
235 serial = sdi->conn;
102f1239
BV
236 serial_source_add(sdi->session, serial, G_IO_IN, 100,
237 norma_dmm_receive_data, (void *)sdi);
bfd48770
MH
238
239 return SR_OK;
240}
241
695dc859 242static int dev_acquisition_stop(struct sr_dev_inst *sdi)
bfd48770 243{
6525d819 244 return std_serial_dev_acquisition_stop(sdi, dev_close,
f8e76e2e 245 sdi->conn, LOG_PREFIX);
bfd48770
MH
246}
247
dd5c48a6 248static struct sr_dev_driver norma_dmm_driver_info = {
bfd48770 249 .name = "norma-dmm",
49c06128
MH
250 .longname = "Norma DM9x0 DMMs",
251 .api_version = 1,
c2fdcc25 252 .init = std_init,
700d6b64 253 .cleanup = std_cleanup,
4f840ce9 254 .scan = scan,
c01bf34c 255 .dev_list = std_dev_list,
49c06128
MH
256 .dev_clear = NULL,
257 .config_get = NULL,
258 .config_set = config_set,
259 .config_list = config_list,
260 .dev_open = std_serial_dev_open,
261 .dev_close = dev_close,
262 .dev_acquisition_start = dev_acquisition_start,
263 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 264 .context = NULL,
49c06128 265};
dd5c48a6 266SR_REGISTER_DEV_DRIVER(norma_dmm_driver_info);
49c06128 267
dd5c48a6 268static struct sr_dev_driver siemens_b102x_driver_info = {
49c06128
MH
269 .name = "siemens-b102x",
270 .longname = "Siemens B102x DMMs",
bfd48770 271 .api_version = 1,
c2fdcc25 272 .init = std_init,
700d6b64 273 .cleanup = std_cleanup,
4f840ce9 274 .scan = scan,
c01bf34c 275 .dev_list = std_dev_list,
a6630742 276 .dev_clear = NULL,
f8e76e2e 277 .config_get = NULL,
bfd48770
MH
278 .config_set = config_set,
279 .config_list = config_list,
854434de 280 .dev_open = std_serial_dev_open,
bfd48770
MH
281 .dev_close = dev_close,
282 .dev_acquisition_start = dev_acquisition_start,
283 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 284 .context = NULL,
bfd48770 285};
dd5c48a6 286SR_REGISTER_DEV_DRIVER(siemens_b102x_driver_info);