]>
Commit | Line | Data |
---|---|---|
01cf8814 DR |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.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 | ||
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
14 | #include <string.h> | |
15 | #include <unistd.h> | |
16 | #include <fcntl.h> | |
17 | #include <sys/time.h> | |
18 | #include <inttypes.h> | |
19 | #include <glib.h> | |
20 | #include <libudev.h> | |
21 | #include <sigrok.h> | |
22 | #include <arpa/inet.h> | |
23 | #include "config.h" | |
24 | #include "link-mso19.h" | |
25 | ||
26 | #define USB_VENDOR "3195" | |
27 | #define USB_PRODUCT "f190" | |
28 | ||
29 | static int capabilities[] = { | |
30 | HWCAP_LOGIC_ANALYZER, | |
31 | // HWCAP_OSCILLOSCOPE, | |
32 | // HWCAP_PAT_GENERATOR, | |
33 | ||
34 | HWCAP_SAMPLERATE, | |
35 | // HWCAP_CAPTURE_RATIO, | |
36 | HWCAP_LIMIT_SAMPLES, | |
37 | 0, | |
38 | }; | |
39 | ||
40 | static uint64_t supported_samplerates[] = { | |
41 | 100, 200, 500, KHZ(1), KHZ(2), KHZ(5), KHZ(10), KHZ(20), | |
42 | KHZ(50), KHZ(100), KHZ(200), KHZ(500), MHZ(1), MHZ(2), MHZ(5), | |
43 | MHZ(10), MHZ(20), MHZ(50), MHZ(100), MHZ(200), 0 | |
44 | }; | |
45 | ||
46 | static struct samplerates samplerates = { | |
47 | 100, MHZ(200), 0, supported_samplerates, | |
48 | }; | |
49 | ||
50 | static GSList *device_instances = NULL; | |
51 | ||
52 | static int mso_send_control_message(struct sigrok_device_instance *sdi, | |
53 | uint16_t payload[], int n) | |
54 | { | |
55 | int fd = sdi->serial->fd; | |
56 | int i, w, ret, s = n * 2 + sizeof(mso_head) + sizeof(mso_foot); | |
57 | char *p, *buf; | |
58 | ||
59 | if (fd < 0) | |
60 | goto ret; | |
61 | ||
62 | buf = malloc(s); | |
63 | if (!buf) | |
64 | goto ret; | |
65 | ||
66 | p = buf; | |
67 | memcpy(p, mso_head, sizeof(mso_head)); | |
68 | p += sizeof(mso_head); | |
69 | ||
70 | for (i = 0; i < n; i++) { | |
71 | *(uint16_t *) p = htons(payload[i]); | |
72 | p += 2; | |
73 | } | |
74 | memcpy(p, mso_foot, sizeof(mso_foot)); | |
75 | ||
76 | w = 0; | |
77 | while (w < s) { | |
78 | ret = write(fd, buf + w, s - w); | |
79 | if (ret < 0) { | |
80 | ret = SIGROK_ERR; | |
81 | goto free; | |
82 | } | |
83 | w += ret; | |
84 | } | |
85 | ret = SIGROK_OK; | |
86 | free: | |
87 | free(buf); | |
88 | ret: | |
89 | return ret; | |
90 | } | |
91 | ||
92 | static int mso_reset_adc(struct sigrok_device_instance *sdi) | |
93 | { | |
94 | struct mso *mso = sdi->priv; | |
95 | uint16_t ops[2]; | |
96 | ||
97 | ops[0] = mso_trans(REG_CTL, (mso->ctlbase | BIT_CTL_RESETADC)); | |
98 | ops[1] = mso_trans(REG_CTL, mso->ctlbase); | |
99 | mso->ctlbase |= BIT_CTL_ADC_UNKNOWN4; | |
100 | ||
101 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
102 | } | |
103 | ||
104 | static int mso_reset_fsm(struct sigrok_device_instance *sdi) | |
105 | { | |
106 | struct mso *mso = sdi->priv; | |
107 | uint16_t ops[1]; | |
108 | ||
109 | mso->ctlbase |= BIT_CTL_RESETFSM; | |
110 | ops[0] = mso_trans(REG_CTL, mso->ctlbase); | |
111 | ||
112 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
113 | } | |
114 | ||
115 | static int mso_toggle_led(struct sigrok_device_instance *sdi, int state) | |
116 | { | |
117 | struct mso *mso = sdi->priv; | |
118 | uint16_t ops[1]; | |
119 | ||
120 | mso->ctlbase &= BIT_CTL_LED; | |
121 | if (state) | |
122 | mso->ctlbase |= BIT_CTL_LED; | |
123 | ops[0] = mso_trans(REG_CTL, mso->ctlbase); | |
124 | ||
125 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
126 | } | |
127 | ||
128 | static int mso_check_trigger(struct sigrok_device_instance *sdi, | |
129 | uint8_t *info) | |
130 | { | |
131 | uint16_t ops[] = { mso_trans(REG_TRIGGER, 0) }; | |
132 | char buf[1]; | |
133 | int ret; | |
134 | ||
135 | ret = mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
136 | if (info == NULL || ret != SIGROK_OK) | |
137 | return ret; | |
138 | ||
139 | buf[0] = 0; | |
140 | if (read(sdi->serial->fd, buf, 1) != 1) /* FIXME: Need timeout */ | |
141 | ret = SIGROK_ERR; | |
142 | *info = buf[0]; | |
143 | ||
144 | return ret; | |
145 | } | |
146 | ||
147 | static int mso_read_buffer(struct sigrok_device_instance *sdi) | |
148 | { | |
149 | uint16_t ops[] = { mso_trans(REG_BUFFER, 0) }; | |
150 | ||
151 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
152 | } | |
153 | ||
154 | static int mso_arm(struct sigrok_device_instance *sdi) | |
155 | { | |
156 | struct mso *mso = sdi->priv; | |
157 | uint16_t ops[] = { | |
158 | mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_RESETFSM), | |
159 | mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_ARM), | |
160 | mso_trans(REG_CTL, mso->ctlbase), | |
161 | }; | |
162 | ||
163 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
164 | } | |
165 | ||
166 | static int mso_force_capture(struct sigrok_device_instance *sdi) | |
167 | { | |
168 | struct mso *mso = sdi->priv; | |
169 | uint16_t ops[] = { | |
170 | mso_trans(REG_CTL, mso->ctlbase | 8), | |
171 | mso_trans(REG_CTL, mso->ctlbase), | |
172 | }; | |
173 | ||
174 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
175 | } | |
176 | ||
177 | static int mso_dac_out(struct sigrok_device_instance *sdi, uint16_t val) | |
178 | { | |
179 | struct mso *mso = sdi->priv; | |
180 | uint16_t ops[] = { | |
181 | mso_trans(REG_DAC1, (val >> 8) & 0xff), | |
182 | mso_trans(REG_DAC2, val & 0xff), | |
183 | mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_RESETADC), | |
184 | }; | |
185 | ||
186 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
187 | } | |
188 | ||
189 | static int mso_clkrate_out(struct sigrok_device_instance *sdi, uint16_t val) | |
190 | { | |
191 | uint16_t ops[] = { | |
192 | mso_trans(REG_CLKRATE1, (val >> 8) & 0xff), | |
193 | mso_trans(REG_CLKRATE2, val & 0xff), | |
194 | }; | |
195 | ||
196 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
197 | } | |
198 | ||
199 | static int mso_configure_rate(struct sigrok_device_instance *sdi, | |
200 | uint32_t rate) | |
201 | { | |
202 | struct mso *mso = sdi->priv; | |
203 | unsigned int i; | |
204 | int ret = SIGROK_ERR; | |
205 | ||
206 | for (i = 0; i < ARRAY_SIZE(rate_map); i++) { | |
207 | if (rate_map[i].rate == rate) { | |
208 | mso->slowmode = rate_map[i].slowmode; | |
209 | ret = mso_clkrate_out(sdi, rate_map[i].val); | |
210 | if (ret == SIGROK_OK) | |
211 | mso->cur_rate = rate; | |
212 | return ret; | |
213 | } | |
214 | } | |
215 | return ret; | |
216 | } | |
217 | ||
218 | ||
219 | static inline uint16_t mso_calc_raw_from_mv(struct mso *mso) | |
220 | { | |
221 | return (uint16_t) (0x200 - | |
222 | ((mso->dso_trigger_voltage / mso->dso_probe_attn) / | |
223 | mso->vbit)); | |
224 | } | |
225 | ||
226 | static int mso_configure_trigger(struct sigrok_device_instance *sdi) | |
227 | { | |
228 | struct mso *mso = sdi->priv; | |
229 | uint16_t ops[16]; | |
230 | uint16_t dso_trigger = mso_calc_raw_from_mv(mso); | |
231 | ||
232 | dso_trigger &= 0x3ff; | |
233 | if ((!mso->trigger_slope && mso->trigger_chan == 1) || | |
234 | (mso->trigger_slope && | |
235 | (mso->trigger_chan == 0 || | |
236 | mso->trigger_chan == 2 || | |
237 | mso->trigger_chan == 3))) | |
238 | dso_trigger |= 0x400; | |
239 | ||
240 | switch (mso->trigger_chan) { | |
241 | case 1: | |
242 | dso_trigger |= 0xe000; | |
243 | case 2: | |
244 | dso_trigger |= 0x4000; | |
245 | break; | |
246 | case 3: | |
247 | dso_trigger |= 0x2000; | |
248 | break; | |
249 | case 4: | |
250 | dso_trigger |= 0xa000; | |
251 | break; | |
252 | case 5: | |
253 | dso_trigger |= 0x8000; | |
254 | break; | |
255 | default: | |
256 | case 0: | |
257 | break; | |
258 | } | |
259 | ||
260 | switch (mso->trigger_outsrc) { | |
261 | case 1: | |
262 | dso_trigger |= 0x800; | |
263 | break; | |
264 | case 2: | |
265 | dso_trigger |= 0x1000; | |
266 | break; | |
267 | case 3: | |
268 | dso_trigger |= 0x1800; | |
269 | break; | |
270 | ||
271 | } | |
272 | ||
273 | ops[0] = mso_trans(5, mso->la_trigger); | |
274 | ops[1] = mso_trans(6, mso->la_trigger_mask); | |
275 | ops[2] = mso_trans(3, dso_trigger & 0xff); | |
276 | ops[3] = mso_trans(4, (dso_trigger >> 8) & 0xff); | |
277 | ops[4] = mso_trans(11, | |
278 | mso->dso_trigger_width / HZ_TO_NS(mso->cur_rate)); | |
279 | ops[5] = mso_trans(15, (2 | mso->slowmode)); | |
280 | ||
281 | /* FIXME SPI/I2C Triggers */ | |
282 | ops[6] = mso_trans(0, 0); | |
283 | ops[7] = mso_trans(1, 0); | |
284 | ops[8] = mso_trans(2, 0); | |
285 | ops[9] = mso_trans(3, 0); | |
286 | ops[10] = mso_trans(4, 0xff); | |
287 | ops[11] = mso_trans(5, 0xff); | |
288 | ops[12] = mso_trans(6, 0xff); | |
289 | ops[13] = mso_trans(7, 0xff); | |
290 | ops[14] = mso_trans(8, mso->trigger_spimode); | |
291 | ops[15] = mso_trans(15, mso->slowmode); | |
292 | ||
293 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
294 | } | |
295 | ||
296 | static int mso_configure_threshold_level(struct sigrok_device_instance *sdi) | |
297 | { | |
298 | struct mso *mso = sdi->priv; | |
299 | ||
300 | return mso_dac_out(sdi, la_threshold_map[mso->la_threshold]); | |
301 | } | |
302 | ||
303 | static int mso_parse_serial(const char *iSerial, const char *iProduct, | |
304 | struct mso *mso) | |
305 | { | |
306 | unsigned int u1, u2, u3, u4, u5, u6; | |
307 | ||
308 | iProduct = iProduct; | |
309 | /* FIXME: This code is in the original app, but I think its | |
310 | * used only for the GUI */ | |
311 | /* if (strstr(iProduct, "REV_02") || strstr(iProduct, "REV_03")) | |
312 | mso->num_sample_rates = 0x16; | |
313 | else | |
314 | mso->num_sample_rates = 0x10; */ | |
315 | ||
316 | /* parse iSerial */ | |
317 | if (iSerial[0] != '4' || sscanf(iSerial, "%5u%3u%3u%1u%1u%6u", | |
318 | &u1, &u2, &u3, &u4, &u5, &u6) != 6) | |
319 | return SIGROK_ERR; | |
320 | mso->hwmodel = u4; | |
321 | mso->hwrev = u5; | |
322 | mso->serial = u6; | |
323 | mso->vbit = u1 / 10000; | |
324 | if (mso->vbit == 0) | |
325 | mso->vbit = 4.19195; | |
326 | mso->dac_offset = u2; | |
327 | if (mso->dac_offset == 0) | |
328 | mso->dac_offset = 0x1ff; | |
329 | mso->offset_range = u3; | |
330 | if (mso->offset_range == 0) | |
331 | mso->offset_range = 0x17d; | |
332 | ||
333 | /* | |
334 | * FIXME: There is more code on the original software to handle | |
335 | * bigger iSerial strings, but as I can't test on my device | |
336 | * I will not implement it yet | |
337 | */ | |
338 | ||
339 | return SIGROK_OK; | |
340 | } | |
341 | ||
342 | static int hw_init(char *deviceinfo) | |
343 | { | |
344 | struct sigrok_device_instance *sdi; | |
345 | int devcnt = 0; | |
346 | struct udev *udev; | |
347 | struct udev_enumerate *enumerate; | |
348 | struct udev_list_entry *devices, *dev_list_entry; | |
349 | struct mso *mso; | |
350 | ||
351 | deviceinfo = deviceinfo; | |
352 | ||
353 | /* It's easier to map usb<->serial using udev */ | |
354 | /* | |
355 | * FIXME: On windows we can get the same information from the | |
356 | * registry, add an #ifdef here later | |
357 | */ | |
358 | udev = udev_new(); | |
359 | if (!udev) { | |
360 | g_warning("Failed to initialize udev."); | |
361 | goto ret; | |
362 | } | |
363 | enumerate = udev_enumerate_new(udev); | |
364 | udev_enumerate_add_match_subsystem(enumerate, "usb-serial"); | |
365 | udev_enumerate_scan_devices(enumerate); | |
366 | devices = udev_enumerate_get_list_entry(enumerate); | |
367 | udev_list_entry_foreach(dev_list_entry, devices) { | |
368 | const char *syspath, *sysname, *idVendor, *idProduct, | |
369 | *iSerial, *iProduct; | |
370 | char path[32], manufacturer[32], product[32], hwrev[32]; | |
371 | struct udev_device *dev, *parent; | |
372 | size_t s; | |
373 | ||
374 | syspath = udev_list_entry_get_name(dev_list_entry); | |
375 | dev = udev_device_new_from_syspath(udev, syspath); | |
376 | sysname = udev_device_get_sysname(dev); | |
377 | parent = udev_device_get_parent_with_subsystem_devtype( | |
378 | dev, "usb", "usb_device"); | |
379 | if (!parent) { | |
380 | g_warning("Unable to find parent usb device for %s", | |
381 | sysname); | |
382 | continue; | |
383 | } | |
384 | ||
385 | idVendor = udev_device_get_sysattr_value(parent, "idVendor"); | |
386 | idProduct = udev_device_get_sysattr_value(parent, "idProduct"); | |
387 | if (strcmp(USB_VENDOR, idVendor) | |
388 | || strcmp(USB_PRODUCT, idProduct)) | |
389 | continue; | |
390 | ||
391 | iSerial = udev_device_get_sysattr_value(parent, "serial"); | |
392 | iProduct = udev_device_get_sysattr_value(parent, "product"); | |
393 | ||
394 | snprintf(path, sizeof(path), "/dev/%s", sysname); | |
395 | ||
396 | s = strcspn(iProduct, " "); | |
397 | if (s > sizeof(product) || | |
398 | strlen(iProduct) - s > sizeof(manufacturer)) { | |
399 | g_warning("Could not parse iProduct: %s", iProduct); | |
400 | continue; | |
401 | } | |
402 | strncpy(product, iProduct, s); | |
403 | product[s] = 0; | |
404 | strcpy(manufacturer, iProduct + s); | |
405 | sprintf(hwrev, "r%d", mso->hwrev); | |
406 | ||
407 | mso = malloc(sizeof(struct mso)); | |
408 | if (!mso) | |
409 | continue; | |
410 | memset(mso, 0, sizeof(struct mso)); | |
411 | ||
412 | if (mso_parse_serial(iSerial, iProduct, mso) != SIGROK_OK) { | |
413 | g_warning("Invalid iSerial: %s", iSerial); | |
414 | goto err_free_mso; | |
415 | } | |
416 | /* hardware initial state */ | |
417 | mso->ctlbase = 0; | |
418 | ||
419 | sdi = sigrok_device_instance_new(devcnt, ST_INITIALIZING, | |
420 | manufacturer, product, hwrev); | |
421 | if (!sdi) { | |
422 | g_warning("Unable to create device instance for %s", | |
423 | sysname); | |
424 | goto err_free_mso; | |
425 | } | |
426 | ||
427 | /* save a pointer to our private instance data */ | |
428 | sdi->priv = mso; | |
429 | ||
430 | sdi->serial = serial_device_instance_new(path, -1); | |
431 | if (!sdi->serial) | |
432 | goto err_device_instance_free; | |
433 | ||
434 | device_instances = g_slist_append(device_instances, sdi); | |
435 | devcnt++; | |
436 | continue; | |
437 | ||
438 | err_device_instance_free: | |
439 | sigrok_device_instance_free(sdi); | |
440 | err_free_mso: | |
441 | free(mso); | |
442 | } | |
443 | ||
444 | udev_enumerate_unref(enumerate); | |
445 | udev_unref(udev); | |
446 | ||
447 | ret: | |
448 | return devcnt; | |
449 | } | |
450 | ||
451 | static void hw_cleanup(void) | |
452 | { | |
453 | GSList *l; | |
454 | struct sigrok_device_instance *sdi; | |
455 | ||
456 | /* Properly close all devices. */ | |
457 | for (l = device_instances; l; l = l->next) { | |
458 | sdi = l->data; | |
459 | if (sdi->serial->fd != -1) | |
460 | serial_close(sdi->serial->fd); | |
461 | if (sdi->priv != NULL) | |
462 | free(sdi->priv); | |
463 | sigrok_device_instance_free(sdi); | |
464 | } | |
465 | g_slist_free(device_instances); | |
466 | device_instances = NULL; | |
467 | } | |
468 | ||
469 | static int hw_opendev(int device_index) | |
470 | { | |
471 | struct sigrok_device_instance *sdi; | |
472 | struct mso *mso; | |
473 | int ret = SIGROK_ERR; | |
474 | ||
475 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
476 | return ret; | |
477 | ||
478 | mso = sdi->priv; | |
479 | sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR); | |
480 | if (sdi->serial->fd == -1) | |
481 | return ret; | |
482 | ||
483 | ret = serial_set_params(sdi->serial->fd, 460800, 8, 0, 1, 2); | |
484 | if (ret != SIGROK_OK) | |
485 | return ret; | |
486 | ||
487 | sdi->status = ST_ACTIVE; | |
488 | ||
489 | /* FIXME: discard serial buffer */ | |
490 | ||
491 | mso_check_trigger(sdi, &mso->trigger_state); | |
492 | // g_warning("trigger state: %c", mso->trigger_state); | |
493 | ||
494 | ret = mso_reset_adc(sdi); | |
495 | if (ret != SIGROK_OK) | |
496 | return ret; | |
497 | ||
498 | mso_check_trigger(sdi, &mso->trigger_state); | |
499 | // g_warning("trigger state: %c", mso->trigger_state); | |
500 | ||
501 | // ret = mso_reset_fsm(sdi); | |
502 | // if (ret != SIGROK_OK) | |
503 | // return ret; | |
504 | ||
505 | // return SIGROK_ERR; | |
506 | return SIGROK_OK; | |
507 | } | |
508 | ||
509 | static void hw_closedev(int device_index) | |
510 | { | |
511 | struct sigrok_device_instance *sdi; | |
512 | ||
513 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
514 | return; | |
515 | ||
516 | if (sdi->serial->fd != -1) { | |
517 | serial_close(sdi->serial->fd); | |
518 | sdi->serial->fd = -1; | |
519 | sdi->status = ST_INACTIVE; | |
520 | } | |
521 | } | |
522 | ||
523 | static void *hw_get_device_info(int device_index, int device_info_id) | |
524 | { | |
525 | struct sigrok_device_instance *sdi; | |
526 | struct mso *mso; | |
527 | void *info = NULL; | |
528 | ||
529 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
530 | return NULL; | |
531 | mso = sdi->priv; | |
532 | ||
533 | switch (device_info_id) { | |
534 | case DI_INSTANCE: | |
535 | info = sdi; | |
536 | break; | |
537 | case DI_NUM_PROBES: /* FIXME: How to report analog probe? */ | |
538 | info = GINT_TO_POINTER(8); | |
539 | break; | |
540 | case DI_SAMPLERATES: | |
541 | info = &samplerates; | |
542 | break; | |
543 | case DI_TRIGGER_TYPES: | |
544 | info = "01"; /* FIXME */ | |
545 | break; | |
546 | case DI_CUR_SAMPLERATE: | |
547 | info = &mso->cur_rate; | |
548 | break; | |
549 | } | |
550 | return info; | |
551 | } | |
552 | ||
553 | static int hw_get_status(int device_index) | |
554 | { | |
555 | struct sigrok_device_instance *sdi; | |
556 | ||
557 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
558 | return ST_NOT_FOUND; | |
559 | ||
560 | return sdi->status; | |
561 | } | |
562 | ||
563 | static int *hw_get_capabilities(void) | |
564 | { | |
565 | return capabilities; | |
566 | } | |
567 | ||
568 | static int hw_set_configuration(int device_index, int capability, void *value) | |
569 | { | |
570 | struct sigrok_device_instance *sdi; | |
571 | ||
572 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
573 | return SIGROK_ERR; | |
574 | ||
575 | switch (capability) { | |
576 | case HWCAP_SAMPLERATE: | |
577 | return mso_configure_rate(sdi, *(uint64_t *) value); | |
578 | case HWCAP_PROBECONFIG: | |
579 | case HWCAP_LIMIT_SAMPLES: | |
580 | default: | |
581 | return SIGROK_OK; /* FIXME */ | |
582 | } | |
583 | ||
584 | } | |
585 | ||
586 | #define MSO_TRIGGER_UNKNOWN '!' | |
587 | #define MSO_TRIGGER_UNKNOWN1 '1' | |
588 | #define MSO_TRIGGER_UNKNOWN2 '2' | |
589 | #define MSO_TRIGGER_UNKNOWN3 '3' | |
590 | #define MSO_TRIGGER_WAIT '4' | |
591 | #define MSO_TRIGGER_FIRED '5' | |
592 | #define MSO_TRIGGER_DATAREADY '6' | |
593 | ||
594 | /* FIXME: Pass errors? */ | |
595 | static int receive_data(int fd, int revents, void *user_data) | |
596 | { | |
597 | struct sigrok_device_instance *sdi = user_data; | |
598 | struct mso *mso = sdi->priv; | |
599 | struct datafeed_packet packet; | |
600 | uint8_t in[1024], logic_out[1024]; | |
601 | double analog_out[1024]; | |
602 | size_t i, s; | |
603 | ||
604 | revents = revents; | |
605 | ||
606 | s = read(fd, in, sizeof(in)); | |
607 | if (s <= 0) | |
608 | return FALSE; | |
609 | ||
610 | /* No samples */ | |
611 | if (mso->trigger_state != MSO_TRIGGER_DATAREADY) { | |
612 | mso->trigger_state = in[0]; | |
613 | if (mso->trigger_state == MSO_TRIGGER_DATAREADY) { | |
614 | mso_read_buffer(sdi); | |
615 | mso->buffer_n = 0; | |
616 | } else { | |
617 | mso_check_trigger(sdi, NULL); | |
618 | } | |
619 | return FALSE; | |
620 | } | |
621 | ||
622 | /* the hardware always dumps 1024 samples, 24bits each */ | |
623 | if (mso->buffer_n < 3072) { | |
624 | memcpy(mso->buffer + mso->buffer_n, in, s); | |
625 | mso->buffer_n += s; | |
626 | } | |
627 | if (mso->buffer_n < 3072) | |
628 | return FALSE; | |
629 | ||
630 | /* do the conversion */ | |
631 | for (i = 0; i < 1024; i++) { | |
632 | /* FIXME: Need to do conversion to mV */ | |
633 | analog_out[i] = (mso->buffer[i * 3] & 0x3f) | | |
634 | ((mso->buffer[i * 3 + 1] & 0xf) << 6); | |
635 | logic_out[i] = ((mso->buffer[i * 3 + 1] & 0x30) >> 4) | | |
636 | ((mso->buffer[i * 3 + 2] & 0x3f) << 2); | |
637 | } | |
638 | ||
639 | packet.type = DF_LOGIC; | |
640 | packet.length = 1024; | |
641 | packet.unitsize = 1; | |
642 | packet.payload = logic_out; | |
643 | session_bus(mso->session_id, &packet); | |
644 | ||
645 | ||
646 | packet.type = DF_ANALOG; | |
647 | packet.length = 1024; | |
648 | packet.unitsize = sizeof(double); | |
649 | packet.payload = analog_out; | |
650 | session_bus(mso->session_id, &packet); | |
651 | ||
652 | packet.type = DF_END; | |
653 | session_bus(mso->session_id, &packet); | |
654 | ||
655 | return TRUE; | |
656 | } | |
657 | ||
658 | static int hw_start_acquisition(int device_index, gpointer session_device_id) | |
659 | { | |
660 | struct sigrok_device_instance *sdi; | |
661 | struct mso *mso; | |
662 | struct datafeed_packet packet; | |
663 | struct datafeed_header header; | |
664 | int ret = SIGROK_ERR; | |
665 | ||
666 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
667 | return ret; | |
668 | mso = sdi->priv; | |
669 | ||
670 | /* FIXME: No need to do full reconfigure every time */ | |
671 | // ret = mso_reset_fsm(sdi); | |
672 | // if (ret != SIGROK_OK) | |
673 | // return ret; | |
674 | ||
675 | /* FIXME: ACDC Mode */ | |
676 | mso->ctlbase &= 0x7f; | |
677 | // mso->ctlbase |= mso->acdcmode; | |
678 | ||
679 | ret = mso_configure_rate(sdi, mso->cur_rate); | |
680 | if (ret != SIGROK_OK) | |
681 | return ret; | |
682 | ||
683 | /* set dac offset */ | |
684 | ret = mso_dac_out(sdi, mso->dac_offset); | |
685 | if (ret != SIGROK_OK) | |
686 | return ret; | |
687 | ||
688 | ret = mso_configure_threshold_level(sdi); | |
689 | if (ret != SIGROK_OK) | |
690 | return ret; | |
691 | ||
692 | ret = mso_configure_trigger(sdi); | |
693 | if (ret != SIGROK_OK) | |
694 | return ret; | |
695 | ||
696 | /* FIXME: trigger_position */ | |
697 | ||
698 | ||
699 | /* END of config hardware part */ | |
700 | ||
701 | /* with trigger */ | |
702 | ret = mso_arm(sdi); | |
703 | if (ret != SIGROK_OK) | |
704 | return ret; | |
705 | ||
706 | /* without trigger */ | |
707 | // ret = mso_force_capture(sdi); | |
708 | // if (ret != SIGROK_OK) | |
709 | // return ret; | |
710 | ||
711 | mso_check_trigger(sdi, &mso->trigger_state); | |
712 | ret = mso_check_trigger(sdi, NULL); | |
713 | if (ret != SIGROK_OK) | |
714 | return ret; | |
715 | ||
716 | mso->session_id = session_device_id; | |
717 | source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi); | |
718 | ||
719 | packet.type = DF_HEADER; | |
720 | packet.length = sizeof(struct datafeed_header); | |
721 | packet.payload = (unsigned char *) &header; | |
722 | header.feed_version = 1; | |
723 | gettimeofday(&header.starttime, NULL); | |
724 | header.samplerate = mso->cur_rate; | |
725 | header.num_analog_probes = 1; | |
726 | header.num_logic_probes = 8; | |
727 | header.protocol_id = PROTO_RAW; | |
728 | session_bus(session_device_id, &packet); | |
729 | ||
730 | return ret; | |
731 | } | |
732 | ||
733 | /* FIXME */ | |
734 | static void hw_stop_acquisition(int device_index, gpointer session_device_id) | |
735 | { | |
736 | struct datafeed_packet packet; | |
737 | ||
738 | device_index = device_index; | |
739 | ||
740 | packet.type = DF_END; | |
741 | session_bus(session_device_id, &packet); | |
742 | } | |
743 | ||
744 | struct device_plugin link_mso19_plugin_info = { | |
745 | .name = "link-mso19", | |
746 | .api_version = 1, | |
747 | .init = hw_init, | |
748 | .cleanup = hw_cleanup, | |
749 | ||
750 | .open = hw_opendev, | |
751 | .close = hw_closedev, | |
752 | .get_device_info = hw_get_device_info, | |
753 | .get_status = hw_get_status, | |
754 | .get_capabilities = hw_get_capabilities, | |
755 | .set_configuration = hw_set_configuration, | |
756 | .start_acquisition = hw_start_acquisition, | |
757 | .stop_acquisition = hw_stop_acquisition, | |
758 | }; |