]>
Commit | Line | Data |
---|---|---|
df92e5cf | 1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> | |
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 | ||
20 | #include "protocol.h" | |
21 | ||
df92e5cf | 22 | static const int hwcaps[] = { |
23 | SR_HWCAP_LOGIC_ANALYZER, | |
24 | SR_HWCAP_SAMPLERATE, | |
087a9161 | 25 | SR_HWCAP_TRIGGER_SLOPE, |
26 | SR_HWCAP_HORIZ_TRIGGERPOS, | |
df92e5cf | 27 | // SR_HWCAP_CAPTURE_RATIO, |
28 | SR_HWCAP_LIMIT_SAMPLES, | |
29 | // SR_HWCAP_RLE, | |
30 | 0, | |
31 | }; | |
32 | ||
33 | /* | |
34 | * Probes are numbered 0 to 7. | |
35 | * | |
36 | * See also: http://www.linkinstruments.com/images/mso19_1113.gif | |
37 | */ | |
38 | SR_PRIV const char *mso19_probe_names[NUM_PROBES + 1] = { | |
39 | "0", "1", "2", "3", "4", "5", "6", "7", NULL | |
40 | }; | |
41 | ||
42 | /*supported samplerates */ | |
43 | static const struct sr_samplerates samplerates = { | |
44 | SR_HZ(100), | |
df92e5cf | 45 | SR_MHZ(200), |
5a24e89c | 46 | SR_HZ(100), |
df92e5cf | 47 | NULL, |
48 | }; | |
49 | ||
50 | SR_PRIV struct sr_dev_driver link_mso19_driver_info; | |
51 | static struct sr_dev_driver *di = &link_mso19_driver_info; | |
52 | ||
53 | static int hw_init(struct sr_context *sr_ctx) | |
54 | { | |
df92e5cf | 55 | struct drv_context *drvc; |
56 | ||
57 | if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { | |
58 | sr_err("Driver context malloc failed."); | |
59 | return SR_ERR_MALLOC; | |
60 | } | |
61 | drvc->sr_ctx = sr_ctx; | |
62 | di->priv = drvc; | |
63 | ||
64 | return SR_OK; | |
65 | } | |
66 | ||
67 | static GSList *hw_scan(GSList *options) | |
68 | { | |
df92e5cf | 69 | int i; |
70 | ||
71 | (void)options; | |
72 | GSList *devices = NULL; | |
df92e5cf | 73 | |
74 | const char* conn = NULL; | |
75 | const char* serialcomm = NULL; | |
76 | GSList *l; | |
77 | for (l = options; l; l = l->next) { | |
78 | struct sr_hwopt* opt = l->data; | |
79 | switch (opt->hwopt) { | |
80 | case SR_HWOPT_CONN: | |
81 | conn = opt->value; | |
82 | break; | |
83 | case SR_HWOPT_SERIALCOMM: | |
84 | serialcomm = opt->value; | |
85 | break; | |
86 | } | |
87 | } | |
88 | if (!conn) | |
89 | conn = SERIALCONN; | |
90 | if (serialcomm == NULL) | |
91 | serialcomm = SERIALCOMM; | |
92 | ||
93 | struct udev *udev = udev_new(); | |
94 | if (!udev) { | |
95 | sr_err("Failed to initialize udev."); | |
96 | } | |
97 | struct udev_enumerate *enumerate = udev_enumerate_new(udev); | |
98 | udev_enumerate_add_match_subsystem(enumerate, "usb-serial"); | |
99 | udev_enumerate_scan_devices(enumerate); | |
100 | struct udev_list_entry *devs = udev_enumerate_get_list_entry(enumerate); | |
101 | struct udev_list_entry *dev_list_entry; | |
102 | for (dev_list_entry = devs; | |
103 | dev_list_entry != NULL; | |
104 | dev_list_entry = udev_list_entry_get_next(dev_list_entry)) | |
105 | { | |
106 | const char *syspath = udev_list_entry_get_name(dev_list_entry); | |
107 | struct udev_device *dev = udev_device_new_from_syspath(udev, syspath); | |
108 | const char *sysname = udev_device_get_sysname(dev); | |
109 | struct udev_device *parent = udev_device_get_parent_with_subsystem_devtype( | |
110 | dev, "usb", "usb_device"); | |
111 | ||
112 | if (!parent) { | |
113 | sr_err("Unable to find parent usb device for %s", | |
114 | sysname); | |
115 | continue; | |
116 | } | |
117 | ||
118 | const char *idVendor = udev_device_get_sysattr_value(parent, "idVendor"); | |
119 | const char *idProduct = udev_device_get_sysattr_value(parent, "idProduct"); | |
120 | if (strcmp(USB_VENDOR, idVendor) | |
121 | || strcmp(USB_PRODUCT, idProduct)) | |
122 | continue; | |
123 | ||
124 | const char* iSerial = udev_device_get_sysattr_value(parent, "serial"); | |
125 | const char* iProduct = udev_device_get_sysattr_value(parent, "product"); | |
126 | ||
127 | char path[32]; | |
128 | snprintf(path, sizeof(path), "/dev/%s", sysname); | |
5a24e89c | 129 | conn = path; |
df92e5cf | 130 | |
131 | size_t s = strcspn(iProduct, " "); | |
132 | char product[32]; | |
133 | char manufacturer[32]; | |
134 | if (s > sizeof(product) || | |
135 | strlen(iProduct) - s > sizeof(manufacturer)) { | |
136 | sr_err("Could not parse iProduct: %s.", iProduct); | |
137 | continue; | |
138 | } | |
139 | strncpy(product, iProduct, s); | |
140 | product[s] = 0; | |
4db2aaff | 141 | strcpy(manufacturer, iProduct + s + 1); |
df92e5cf | 142 | |
143 | //Create the device context and set its params | |
144 | struct dev_context *devc; | |
145 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
146 | sr_err("Device context malloc failed."); | |
147 | return devices; | |
148 | } | |
149 | ||
150 | if (mso_parse_serial(iSerial, iProduct, devc) != SR_OK) { | |
151 | sr_err("Invalid iSerial: %s.", iSerial); | |
152 | g_free(devc); | |
153 | return devices; | |
154 | } | |
155 | ||
156 | char hwrev[32]; | |
157 | sprintf(hwrev, "r%d", devc->hwrev); | |
158 | devc->ctlbase1 = 0; | |
159 | devc->protocol_trigger.spimode = 0; | |
160 | for (i = 0; i < 4; i++) { | |
161 | devc->protocol_trigger.word[i] = 0; | |
162 | devc->protocol_trigger.mask[i] = 0xff; | |
163 | } | |
164 | ||
165 | if (!(devc->serial = sr_serial_dev_inst_new(conn, serialcomm))) | |
166 | { | |
167 | g_free(devc); | |
168 | return devices; | |
169 | } | |
170 | ||
171 | struct sr_dev_inst *sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, | |
172 | manufacturer, product, hwrev); | |
173 | ||
174 | if (!sdi) { | |
175 | sr_err("Unable to create device instance for %s", | |
176 | sysname); | |
177 | sr_dev_inst_free(sdi); | |
178 | g_free(devc); | |
179 | return devices; | |
180 | } | |
181 | ||
df92e5cf | 182 | sdi->driver = di; |
183 | sdi->priv = devc; | |
4db2aaff | 184 | |
185 | for (i = 0; i < NUM_PROBES; i++) { | |
186 | struct sr_probe *probe; | |
187 | if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, | |
188 | mso19_probe_names[i]))) | |
189 | return 0; | |
190 | sdi->probes = g_slist_append(sdi->probes, probe); | |
191 | } | |
192 | ||
df92e5cf | 193 | //Add the driver |
194 | struct drv_context *drvc = di->priv; | |
195 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
196 | devices = g_slist_append(devices, sdi); | |
197 | } | |
5a24e89c | 198 | |
df92e5cf | 199 | return devices; |
200 | } | |
201 | ||
202 | static GSList *hw_dev_list(void) | |
203 | { | |
df92e5cf | 204 | struct drv_context *drvc; |
205 | ||
206 | drvc = di->priv; | |
207 | ||
208 | return drvc->instances; | |
209 | } | |
210 | ||
211 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
212 | { | |
df92e5cf | 213 | struct dev_context *devc; |
214 | ||
215 | devc = sdi->priv; | |
216 | ||
217 | if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK) | |
218 | return SR_ERR; | |
219 | ||
220 | sdi->status = SR_ST_ACTIVE; | |
221 | ||
222 | /* FIXME: discard serial buffer */ | |
223 | mso_check_trigger(devc->serial, &devc->trigger_state); | |
224 | sr_dbg("Trigger state: 0x%x.", devc->trigger_state); | |
225 | ||
226 | int ret = mso_reset_adc(sdi); | |
227 | if (ret != SR_OK) | |
228 | return ret; | |
229 | ||
230 | mso_check_trigger(devc->serial, &devc->trigger_state); | |
231 | sr_dbg("Trigger state: 0x%x.", devc->trigger_state); | |
232 | ||
233 | // ret = mso_reset_fsm(sdi); | |
234 | // if (ret != SR_OK) | |
235 | // return ret; | |
236 | // return SR_ERR; | |
237 | ||
238 | return SR_OK; | |
239 | } | |
240 | ||
241 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
242 | { | |
df92e5cf | 243 | struct dev_context *devc; |
244 | ||
245 | devc = sdi->priv; | |
246 | ||
247 | if (devc->serial && devc->serial->fd != -1) { | |
248 | serial_close(devc->serial); | |
249 | sdi->status = SR_ST_INACTIVE; | |
250 | } | |
251 | ||
252 | return SR_OK; | |
253 | } | |
254 | ||
255 | static int hw_cleanup(void) | |
256 | { | |
df92e5cf | 257 | GSList *l; |
258 | struct sr_dev_inst *sdi; | |
259 | struct drv_context *drvc; | |
260 | struct dev_context *devc; | |
261 | int ret = SR_OK; | |
262 | ||
263 | if (!(drvc = di->priv)) | |
264 | return SR_OK; | |
265 | ||
266 | /* Properly close and free all devices. */ | |
267 | for (l = drvc->instances; l; l = l->next) { | |
268 | if (!(sdi = l->data)) { | |
269 | /* Log error, but continue cleaning up the rest. */ | |
270 | sr_err("%s: sdi was NULL, continuing", __func__); | |
271 | ret = SR_ERR_BUG; | |
272 | continue; | |
273 | } | |
274 | if (!(devc = sdi->priv)) { | |
275 | /* Log error, but continue cleaning up the rest. */ | |
276 | sr_err("%s: sdi->priv was NULL, continuing", __func__); | |
277 | ret = SR_ERR_BUG; | |
278 | continue; | |
279 | } | |
280 | hw_dev_close(sdi); | |
281 | sr_serial_dev_inst_free(devc->serial); | |
282 | sr_dev_inst_free(sdi); | |
283 | } | |
284 | g_slist_free(drvc->instances); | |
285 | drvc->instances = NULL; | |
286 | ||
287 | return ret; | |
288 | } | |
289 | ||
290 | static int hw_info_get(int info_id, const void **data, | |
291 | const struct sr_dev_inst *sdi) | |
292 | { | |
293 | struct dev_context *devc; | |
294 | ||
df92e5cf | 295 | switch (info_id) { |
296 | case SR_DI_HWCAPS: | |
297 | *data = hwcaps; | |
298 | break; | |
299 | case SR_DI_NUM_PROBES: | |
300 | *data = GINT_TO_POINTER(1); | |
301 | break; | |
302 | case SR_DI_PROBE_NAMES: | |
303 | *data = mso19_probe_names; | |
304 | break; | |
305 | case SR_DI_SAMPLERATES: | |
306 | *data = &samplerates; | |
307 | break; | |
308 | case SR_DI_TRIGGER_TYPES: | |
309 | *data = (char *)TRIGGER_TYPES; | |
310 | break; | |
311 | case SR_DI_CUR_SAMPLERATE: | |
312 | if (sdi) { | |
313 | devc = sdi->priv; | |
314 | *data = &devc->cur_rate; | |
315 | } else | |
316 | return SR_ERR; | |
317 | break; | |
318 | default: | |
319 | return SR_ERR_ARG; | |
320 | } | |
321 | ||
322 | return SR_OK; | |
323 | } | |
324 | ||
325 | static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, | |
326 | const void *value) | |
327 | { | |
df92e5cf | 328 | int ret; |
df92e5cf | 329 | |
087a9161 | 330 | struct dev_context *devc; |
331 | devc = sdi->priv; | |
332 | ||
df92e5cf | 333 | if (sdi->status != SR_ST_ACTIVE) |
334 | return SR_ERR; | |
335 | ||
336 | switch (hwcap) { | |
337 | case SR_HWCAP_SAMPLERATE: | |
338 | return mso_configure_rate(sdi, *(const uint64_t *) value); | |
339 | ret = SR_OK; | |
340 | break; | |
341 | case SR_HWCAP_LIMIT_SAMPLES: | |
eb913174 | 342 | { |
343 | const uint64_t num_samples = *(const uint64_t *)value; | |
344 | if (num_samples < 1024) | |
345 | { | |
346 | sr_err("minimum of 1024 samples required"); | |
347 | ret = SR_ERR_ARG; | |
348 | ||
349 | } else { | |
350 | devc->limit_samples = num_samples; | |
351 | sr_dbg("setting limit_samples to %i\n", num_samples); | |
352 | ret = SR_OK; | |
353 | } | |
354 | } | |
df92e5cf | 355 | break; |
356 | case SR_HWCAP_CAPTURE_RATIO: | |
357 | ret = SR_OK; | |
358 | break; | |
087a9161 | 359 | case SR_HWCAP_TRIGGER_SLOPE: |
360 | { | |
eb913174 | 361 | const uint64_t slope = *(const uint64_t *)value; |
087a9161 | 362 | if (slope != SLOPE_NEGATIVE && slope != SLOPE_POSITIVE) |
363 | { | |
364 | sr_err("Invalid trigger slope"); | |
365 | ret = SR_ERR_ARG; | |
366 | } else { | |
367 | devc->trigger_slope = slope; | |
368 | ret = SR_OK; | |
369 | } | |
370 | } | |
371 | break; | |
372 | case SR_HWCAP_HORIZ_TRIGGERPOS: | |
373 | { | |
eb913174 | 374 | const float pos = *(const float *)value; |
087a9161 | 375 | if (pos < 0 || pos > 255) { |
376 | sr_err("Trigger position (%f) should be between 0 and 255.", pos); | |
377 | ret = SR_ERR_ARG; | |
378 | } else { | |
379 | int trigger_pos = (int)pos; | |
087a9161 | 380 | devc->trigger_holdoff[0] = trigger_pos&0xff; |
381 | ret = SR_OK; | |
382 | } | |
383 | } | |
384 | break; | |
385 | ||
df92e5cf | 386 | case SR_HWCAP_RLE: |
387 | ret = SR_OK; | |
388 | break; | |
389 | default: | |
390 | ret = SR_ERR; | |
391 | } | |
392 | ||
393 | return ret; | |
394 | } | |
395 | ||
396 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
397 | void *cb_data) | |
398 | { | |
399 | struct sr_datafeed_packet *packet; | |
400 | struct sr_datafeed_header *header; | |
401 | struct sr_datafeed_meta_logic meta; | |
402 | struct dev_context *devc; | |
df92e5cf | 403 | int ret = SR_ERR; |
404 | ||
405 | ||
df92e5cf | 406 | devc = sdi->priv; |
407 | ||
408 | if (sdi->status != SR_ST_ACTIVE) | |
409 | return SR_ERR; | |
410 | ||
5a24e89c | 411 | if (mso_configure_probes(sdi) != SR_OK) { |
412 | sr_err("Failed to configure probes."); | |
413 | return SR_ERR; | |
414 | } | |
df92e5cf | 415 | |
df92e5cf | 416 | /* FIXME: No need to do full reconfigure every time */ |
417 | // ret = mso_reset_fsm(sdi); | |
418 | // if (ret != SR_OK) | |
419 | // return ret; | |
420 | ||
421 | /* FIXME: ACDC Mode */ | |
422 | devc->ctlbase1 &= 0x7f; | |
423 | // devc->ctlbase1 |= devc->acdcmode; | |
424 | ||
425 | ret = mso_configure_rate(sdi, devc->cur_rate); | |
426 | if (ret != SR_OK) | |
427 | return ret; | |
428 | ||
429 | /* set dac offset */ | |
430 | ret = mso_dac_out(sdi, devc->dac_offset); | |
431 | if (ret != SR_OK) | |
432 | return ret; | |
433 | ||
434 | ret = mso_configure_threshold_level(sdi); | |
435 | if (ret != SR_OK) | |
436 | return ret; | |
437 | ||
087a9161 | 438 | |
df92e5cf | 439 | ret = mso_configure_trigger(sdi); |
440 | if (ret != SR_OK) | |
441 | return ret; | |
442 | ||
df92e5cf | 443 | |
444 | /* END of config hardware part */ | |
087a9161 | 445 | ret = mso_arm(sdi); |
446 | if (ret != SR_OK) | |
447 | return ret; | |
df92e5cf | 448 | |
449 | /* Start acquisition on the device. */ | |
4db2aaff | 450 | mso_check_trigger(devc->serial, &devc->trigger_state); |
451 | ret = mso_check_trigger(devc->serial, NULL); | |
df92e5cf | 452 | if (ret != SR_OK) |
453 | return ret; | |
454 | ||
455 | sr_source_add(devc->serial->fd, G_IO_IN, -1, mso_receive_data, cb_data); | |
456 | ||
457 | if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) { | |
458 | sr_err("Datafeed packet malloc failed."); | |
459 | return SR_ERR_MALLOC; | |
460 | } | |
461 | ||
462 | if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) { | |
463 | sr_err("Datafeed header malloc failed."); | |
464 | g_free(packet); | |
465 | return SR_ERR_MALLOC; | |
466 | } | |
467 | ||
eb913174 | 468 | |
df92e5cf | 469 | packet->type = SR_DF_HEADER; |
470 | packet->payload = (unsigned char *)header; | |
471 | header->feed_version = 1; | |
472 | gettimeofday(&header->starttime, NULL); | |
473 | sr_session_send(cb_data, packet); | |
474 | ||
475 | packet->type = SR_DF_META_LOGIC; | |
476 | packet->payload = &meta; | |
477 | meta.samplerate = devc->cur_rate; | |
478 | meta.num_probes = NUM_PROBES; | |
479 | sr_session_send(cb_data, packet); | |
480 | ||
481 | g_free(header); | |
482 | g_free(packet); | |
483 | ||
484 | return SR_OK; | |
485 | } | |
486 | ||
087a9161 | 487 | /* This stops acquisition on ALL devices, ignoring dev_index. */ |
df92e5cf | 488 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
489 | { | |
df92e5cf | 490 | /* Avoid compiler warnings. */ |
491 | (void)cb_data; | |
492 | ||
493 | stop_acquisition(sdi); | |
494 | ||
495 | return SR_OK; | |
496 | } | |
497 | ||
498 | SR_PRIV struct sr_dev_driver link_mso19_driver_info = { | |
499 | .name = "link-mso19", | |
500 | .longname = "Link Instruments MSO-19", | |
501 | .api_version = 1, | |
502 | .init = hw_init, | |
503 | .cleanup = hw_cleanup, | |
504 | .scan = hw_scan, | |
505 | .dev_list = hw_dev_list, | |
506 | .dev_clear = hw_cleanup, | |
507 | .dev_open = hw_dev_open, | |
508 | .dev_close = hw_dev_close, | |
509 | .info_get = hw_info_get, | |
510 | .dev_config_set = hw_dev_config_set, | |
511 | .dev_acquisition_start = hw_dev_acquisition_start, | |
512 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
513 | .priv = NULL, | |
514 | }; |