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