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