]>
Commit | Line | Data |
---|---|---|
7dc55d93 AG |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> | |
5 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <glib.h> | |
7dc55d93 AG |
22 | #include <sys/types.h> |
23 | #include <sys/stat.h> | |
24 | #include <fcntl.h> | |
25 | #include <string.h> | |
26 | #include <errno.h> | |
bbabddbd UH |
27 | #include "libsigrok.h" |
28 | #include "libsigrok-internal.h" | |
29 | #include "protocol.h" | |
7dc55d93 AG |
30 | |
31 | static const int hwopts[] = { | |
32 | SR_HWOPT_CONN, | |
33 | SR_HWOPT_SERIALCOMM, | |
34 | 0, | |
35 | }; | |
36 | ||
37 | static const int hwcaps[] = { | |
38 | SR_HWCAP_MULTIMETER, | |
39 | SR_HWCAP_LIMIT_SAMPLES, | |
40 | SR_HWCAP_CONTINUOUS, | |
41 | 0, | |
42 | }; | |
43 | ||
44 | static const char *probe_names[] = { | |
45 | "Probe", | |
46 | NULL, | |
47 | }; | |
48 | ||
bbabddbd UH |
49 | SR_PRIV struct sr_dev_driver tekpower_dmm_driver_info; |
50 | static struct sr_dev_driver *di = &tekpower_dmm_driver_info; | |
7dc55d93 AG |
51 | |
52 | /* Properly close and free all devices. */ | |
53 | static int clear_instances(void) | |
54 | { | |
55 | struct sr_dev_inst *sdi; | |
56 | struct drv_context *drvc; | |
57 | struct dev_context *devc; | |
58 | GSList *l; | |
59 | ||
60 | if (!(drvc = di->priv)) | |
61 | return SR_OK; | |
62 | ||
63 | drvc = di->priv; | |
64 | for (l = drvc->instances; l; l = l->next) { | |
65 | if (!(sdi = l->data)) | |
66 | continue; | |
67 | if (!(devc = sdi->priv)) | |
68 | continue; | |
69 | sr_serial_dev_inst_free(devc->serial); | |
70 | sr_dev_inst_free(sdi); | |
71 | } | |
72 | g_slist_free(drvc->instances); | |
73 | drvc->instances = NULL; | |
74 | ||
75 | return SR_OK; | |
76 | } | |
77 | ||
78 | static int hw_init(void) | |
79 | { | |
80 | struct drv_context *drvc; | |
81 | ||
82 | if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { | |
bbabddbd UH |
83 | sr_err("Driver context malloc failed."); |
84 | return SR_ERR_MALLOC; | |
7dc55d93 AG |
85 | } |
86 | ||
87 | di->priv = drvc; | |
88 | ||
89 | return SR_OK; | |
90 | } | |
91 | ||
7dc55d93 AG |
92 | static GSList *lcd14_scan(const char *conn, const char *serialcomm) |
93 | { | |
94 | struct sr_dev_inst *sdi; | |
95 | struct drv_context *drvc; | |
96 | struct dev_context *devc; | |
97 | struct sr_probe *probe; | |
98 | GSList *devices; | |
bbabddbd | 99 | int i, len, fd, retry, good_packets = 0, dropped, ret; |
8c1adf37 | 100 | uint8_t buf[128], *b; |
7dc55d93 | 101 | |
bbabddbd UH |
102 | if ((fd = serial_open(conn, O_RDONLY | O_NONBLOCK)) == -1) { |
103 | sr_err("Unable to open %s: %s.", conn, strerror(errno)); | |
7dc55d93 AG |
104 | return NULL; |
105 | } | |
bbabddbd UH |
106 | if ((ret = serial_set_paramstr(fd, serialcomm)) != SR_OK) { |
107 | sr_err("Unable to set serial parameters: %d", ret); | |
7dc55d93 AG |
108 | return NULL; |
109 | } | |
110 | ||
bbabddbd | 111 | sr_info("Probing port %s readonly.", conn); |
7dc55d93 AG |
112 | |
113 | drvc = di->priv; | |
114 | b = buf; | |
115 | retry = 0; | |
116 | devices = NULL; | |
117 | serial_flush(fd); | |
bbabddbd UH |
118 | |
119 | /* | |
120 | * There's no way to get an ID from the multimeter. It just sends data | |
121 | * periodically, so the best we can do is check if the packets match | |
122 | * the expected format. | |
123 | */ | |
124 | while (!devices && retry < 3) { | |
7dc55d93 AG |
125 | retry++; |
126 | ||
bbabddbd | 127 | /* Let's get a bit of data and see if we can find a packet. */ |
7dc55d93 | 128 | len = sizeof(buf); |
8c1adf37 | 129 | serial_readline(fd, (char **)&b, &len, 500); |
be5c1d3b | 130 | if ((len == 0) || (len < FS9721_PACKET_SIZE)) { |
bbabddbd | 131 | /* Not enough data received, is the DMM connected? */ |
7dc55d93 AG |
132 | continue; |
133 | } | |
134 | ||
135 | /* Let's treat our buffer like a stream, and find any | |
136 | * valid packets */ | |
be5c1d3b | 137 | for (i = 0; i < len - FS9721_PACKET_SIZE + 1;) { |
8c1adf37 | 138 | if (!sr_fs9721_packet_valid(&buf[i])) { |
7dc55d93 AG |
139 | i++; |
140 | continue; | |
141 | } | |
142 | good_packets++; | |
be5c1d3b | 143 | i += FS9721_PACKET_SIZE; |
7dc55d93 AG |
144 | } |
145 | ||
bbabddbd UH |
146 | /* |
147 | * If we dropped more than two packets worth of data, | |
148 | * something is wrong. | |
149 | */ | |
be5c1d3b AG |
150 | dropped = len - (good_packets * FS9721_PACKET_SIZE); |
151 | if (dropped > 2 * FS9721_PACKET_SIZE) | |
7dc55d93 AG |
152 | continue; |
153 | ||
bbabddbd | 154 | /* Let's see if we have anything good. */ |
7dc55d93 AG |
155 | if (good_packets == 0) |
156 | continue; | |
157 | ||
bbabddbd | 158 | sr_info("Found device on port %s.", conn); |
7dc55d93 AG |
159 | |
160 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "TekPower", | |
161 | "TP4000ZC", ""))) | |
162 | return NULL; | |
163 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
bbabddbd | 164 | sr_err("Device context malloc failed."); |
7dc55d93 AG |
165 | return NULL; |
166 | } | |
167 | ||
7dc55d93 AG |
168 | devc->serial = sr_serial_dev_inst_new(conn, -1); |
169 | devc->serialcomm = g_strdup(serialcomm); | |
170 | ||
171 | sdi->priv = devc; | |
172 | sdi->driver = di; | |
173 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
174 | return NULL; | |
175 | sdi->probes = g_slist_append(sdi->probes, probe); | |
176 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
177 | devices = g_slist_append(devices, sdi); | |
178 | break; | |
179 | } | |
180 | ||
181 | serial_close(fd); | |
182 | return devices; | |
183 | } | |
184 | ||
185 | static GSList *hw_scan(GSList *options) | |
186 | { | |
187 | struct sr_hwopt *opt; | |
188 | GSList *l, *devices; | |
189 | const char *conn, *serialcomm; | |
190 | ||
191 | conn = serialcomm = NULL; | |
192 | for (l = options; l; l = l->next) { | |
193 | opt = l->data; | |
194 | switch (opt->hwopt) { | |
195 | case SR_HWOPT_CONN: | |
196 | conn = opt->value; | |
197 | break; | |
198 | case SR_HWOPT_SERIALCOMM: | |
199 | serialcomm = opt->value; | |
200 | break; | |
201 | } | |
202 | } | |
203 | if (!conn) | |
204 | return NULL; | |
205 | ||
206 | if (serialcomm) { | |
207 | /* Use the provided comm specs. */ | |
208 | devices = lcd14_scan(conn, serialcomm); | |
209 | } else { | |
bbabddbd | 210 | /* Try the default 2400/8n1. */ |
7dc55d93 AG |
211 | devices = lcd14_scan(conn, "2400/8n1"); |
212 | } | |
213 | ||
214 | return devices; | |
215 | } | |
216 | ||
217 | static GSList *hw_dev_list(void) | |
218 | { | |
219 | struct drv_context *drvc; | |
220 | ||
221 | drvc = di->priv; | |
222 | ||
223 | return drvc->instances; | |
224 | } | |
225 | ||
226 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
227 | { | |
bbabddbd | 228 | int ret; |
7dc55d93 AG |
229 | struct dev_context *devc; |
230 | ||
231 | if (!(devc = sdi->priv)) { | |
232 | sr_err("sdi->priv was NULL."); | |
233 | return SR_ERR_BUG; | |
234 | } | |
235 | ||
236 | devc->serial->fd = serial_open(devc->serial->port, O_RDONLY); | |
237 | if (devc->serial->fd == -1) { | |
bbabddbd | 238 | sr_err("Couldn't open serial port '%s'.", devc->serial->port); |
7dc55d93 AG |
239 | return SR_ERR; |
240 | } | |
bbabddbd UH |
241 | |
242 | ret = serial_set_paramstr(devc->serial->fd, devc->serialcomm); | |
243 | if (ret != SR_OK) { | |
244 | sr_err("Unable to set serial parameters: %d.", ret); | |
7dc55d93 AG |
245 | return SR_ERR; |
246 | } | |
247 | sdi->status = SR_ST_ACTIVE; | |
248 | ||
249 | return SR_OK; | |
250 | } | |
251 | ||
252 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
253 | { | |
254 | struct dev_context *devc; | |
255 | ||
256 | if (!(devc = sdi->priv)) { | |
257 | sr_err("sdi->priv was NULL."); | |
258 | return SR_ERR_BUG; | |
259 | } | |
260 | ||
261 | if (devc->serial && devc->serial->fd != -1) { | |
262 | serial_close(devc->serial->fd); | |
263 | devc->serial->fd = -1; | |
264 | sdi->status = SR_ST_INACTIVE; | |
265 | } | |
266 | ||
267 | return SR_OK; | |
268 | } | |
269 | ||
270 | static int hw_cleanup(void) | |
271 | { | |
272 | clear_instances(); | |
273 | ||
274 | return SR_OK; | |
275 | } | |
276 | ||
277 | static int hw_info_get(int info_id, const void **data, | |
bbabddbd | 278 | const struct sr_dev_inst *sdi) |
7dc55d93 | 279 | { |
bbabddbd | 280 | (void)sdi; |
7dc55d93 AG |
281 | |
282 | switch (info_id) { | |
283 | case SR_DI_HWOPTS: | |
284 | *data = hwopts; | |
285 | break; | |
286 | case SR_DI_HWCAPS: | |
287 | *data = hwcaps; | |
288 | break; | |
289 | case SR_DI_NUM_PROBES: | |
290 | *data = GINT_TO_POINTER(1); | |
291 | break; | |
292 | case SR_DI_PROBE_NAMES: | |
293 | *data = probe_names; | |
294 | break; | |
295 | default: | |
296 | return SR_ERR_ARG; | |
297 | } | |
298 | ||
299 | return SR_OK; | |
300 | } | |
301 | ||
302 | static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, | |
bbabddbd | 303 | const void *value) |
7dc55d93 AG |
304 | { |
305 | struct dev_context *devc; | |
306 | ||
307 | if (sdi->status != SR_ST_ACTIVE) | |
308 | return SR_ERR; | |
309 | ||
310 | if (!(devc = sdi->priv)) { | |
311 | sr_err("sdi->priv was NULL."); | |
312 | return SR_ERR_BUG; | |
313 | } | |
314 | ||
315 | switch (hwcap) { | |
316 | case SR_HWCAP_LIMIT_SAMPLES: | |
317 | devc->limit_samples = *(const uint64_t *)value; | |
318 | sr_dbg("Setting sample limit to %" PRIu64 ".", | |
319 | devc->limit_samples); | |
320 | break; | |
321 | default: | |
322 | sr_err("Unknown capability: %d.", hwcap); | |
323 | return SR_ERR; | |
324 | break; | |
325 | } | |
326 | ||
327 | return SR_OK; | |
328 | } | |
329 | ||
330 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
bbabddbd | 331 | void *cb_data) |
7dc55d93 AG |
332 | { |
333 | struct sr_datafeed_packet packet; | |
334 | struct sr_datafeed_header header; | |
335 | struct sr_datafeed_meta_analog meta; | |
336 | struct dev_context *devc; | |
337 | ||
338 | if (!(devc = sdi->priv)) { | |
339 | sr_err("sdi->priv was NULL."); | |
340 | return SR_ERR_BUG; | |
341 | } | |
342 | ||
343 | sr_dbg("Starting acquisition."); | |
344 | ||
345 | devc->cb_data = cb_data; | |
346 | ||
bbabddbd UH |
347 | /* |
348 | * Reset the number of samples to take. If we've already collected our | |
7dc55d93 | 349 | * quota, but we start a new session, and don't reset this, we'll just |
bbabddbd UH |
350 | * quit without acquiring any new samples. |
351 | */ | |
7dc55d93 AG |
352 | devc->num_samples = 0; |
353 | ||
354 | /* Send header packet to the session bus. */ | |
355 | sr_dbg("Sending SR_DF_HEADER."); | |
356 | packet.type = SR_DF_HEADER; | |
357 | packet.payload = (uint8_t *)&header; | |
358 | header.feed_version = 1; | |
359 | gettimeofday(&header.starttime, NULL); | |
360 | sr_session_send(devc->cb_data, &packet); | |
361 | ||
362 | /* Send metadata about the SR_DF_ANALOG packets to come. */ | |
363 | sr_dbg("Sending SR_DF_META_ANALOG."); | |
364 | packet.type = SR_DF_META_ANALOG; | |
365 | packet.payload = &meta; | |
366 | meta.num_probes = 1; | |
367 | sr_session_send(devc->cb_data, &packet); | |
368 | ||
bbabddbd | 369 | /* Poll every 50ms, or whenever some data comes in. */ |
7dc55d93 | 370 | sr_source_add(devc->serial->fd, G_IO_IN, 50, |
bbabddbd | 371 | tekpower_dmm_receive_data, (void *)sdi); |
7dc55d93 AG |
372 | |
373 | return SR_OK; | |
374 | } | |
375 | ||
69b07d14 | 376 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
7dc55d93 AG |
377 | { |
378 | struct sr_datafeed_packet packet; | |
379 | struct dev_context *devc; | |
380 | ||
381 | if (sdi->status != SR_ST_ACTIVE) | |
382 | return SR_ERR; | |
383 | ||
384 | if (!(devc = sdi->priv)) { | |
385 | sr_err("sdi->priv was NULL."); | |
386 | return SR_ERR_BUG; | |
387 | } | |
388 | ||
389 | sr_dbg("Stopping acquisition."); | |
390 | ||
391 | sr_source_remove(devc->serial->fd); | |
392 | hw_dev_close((struct sr_dev_inst *)sdi); | |
393 | ||
394 | /* Send end packet to the session bus. */ | |
395 | sr_dbg("Sending SR_DF_END."); | |
396 | packet.type = SR_DF_END; | |
397 | sr_session_send(cb_data, &packet); | |
398 | ||
399 | return SR_OK; | |
400 | } | |
401 | ||
bbabddbd | 402 | SR_PRIV struct sr_dev_driver tekpower_dmm_driver_info = { |
7dc55d93 | 403 | .name = "tekpower-dmm", |
bbabddbd | 404 | .longname = "TekPower/Digitek TP4000ZC/DT4000ZC DMM", |
7dc55d93 AG |
405 | .api_version = 1, |
406 | .init = hw_init, | |
407 | .cleanup = hw_cleanup, | |
408 | .scan = hw_scan, | |
409 | .dev_list = hw_dev_list, | |
410 | .dev_clear = clear_instances, | |
411 | .dev_open = hw_dev_open, | |
412 | .dev_close = hw_dev_close, | |
413 | .info_get = hw_info_get, | |
414 | .dev_config_set = hw_dev_config_set, | |
415 | .dev_acquisition_start = hw_dev_acquisition_start, | |
416 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
417 | .priv = NULL, | |
418 | }; |