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