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