]>
Commit | Line | Data |
---|---|---|
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 | * 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 <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 <sigrok-internal.h> | |
32 | #include "config.h" | |
33 | #include "link-mso19.h" | |
34 | ||
35 | #define USB_VENDOR "3195" | |
36 | #define USB_PRODUCT "f190" | |
37 | ||
38 | static int capabilities[] = { | |
39 | SR_HWCAP_LOGIC_ANALYZER, | |
40 | // SR_HWCAP_OSCILLOSCOPE, | |
41 | // SR_HWCAP_PAT_GENERATOR, | |
42 | ||
43 | SR_HWCAP_SAMPLERATE, | |
44 | // SR_HWCAP_CAPTURE_RATIO, | |
45 | SR_HWCAP_LIMIT_SAMPLES, | |
46 | 0, | |
47 | }; | |
48 | ||
49 | static uint64_t supported_samplerates[] = { | |
50 | 100, 200, 500, KHZ(1), KHZ(2), KHZ(5), KHZ(10), KHZ(20), | |
51 | KHZ(50), KHZ(100), KHZ(200), KHZ(500), MHZ(1), MHZ(2), MHZ(5), | |
52 | MHZ(10), MHZ(20), MHZ(50), MHZ(100), MHZ(200), 0 | |
53 | }; | |
54 | ||
55 | static struct sr_samplerates samplerates = { | |
56 | 100, MHZ(200), 0, supported_samplerates, | |
57 | }; | |
58 | ||
59 | static GSList *device_instances = NULL; | |
60 | ||
61 | static int mso_send_control_message(struct sr_device_instance *sdi, | |
62 | uint16_t payload[], int n) | |
63 | { | |
64 | int fd = sdi->serial->fd; | |
65 | int i, w, ret, s = n * 2 + sizeof(mso_head) + sizeof(mso_foot); | |
66 | char *p, *buf; | |
67 | ||
68 | if (fd < 0) | |
69 | goto ret; | |
70 | ||
71 | buf = malloc(s); | |
72 | if (!buf) | |
73 | goto ret; | |
74 | ||
75 | p = buf; | |
76 | memcpy(p, mso_head, sizeof(mso_head)); | |
77 | p += sizeof(mso_head); | |
78 | ||
79 | for (i = 0; i < n; i++) { | |
80 | *(uint16_t *) p = htons(payload[i]); | |
81 | p += 2; | |
82 | } | |
83 | memcpy(p, mso_foot, sizeof(mso_foot)); | |
84 | ||
85 | w = 0; | |
86 | while (w < s) { | |
87 | ret = serial_write(fd, buf + w, s - w); | |
88 | if (ret < 0) { | |
89 | ret = SR_ERR; | |
90 | goto free; | |
91 | } | |
92 | w += ret; | |
93 | } | |
94 | ret = SR_OK; | |
95 | free: | |
96 | free(buf); | |
97 | ret: | |
98 | return ret; | |
99 | } | |
100 | ||
101 | static int mso_reset_adc(struct sr_device_instance *sdi) | |
102 | { | |
103 | struct mso *mso = sdi->priv; | |
104 | uint16_t ops[2]; | |
105 | ||
106 | ops[0] = mso_trans(REG_CTL, (mso->ctlbase | BIT_CTL_RESETADC)); | |
107 | ops[1] = mso_trans(REG_CTL, mso->ctlbase); | |
108 | mso->ctlbase |= BIT_CTL_ADC_UNKNOWN4; | |
109 | ||
110 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
111 | } | |
112 | ||
113 | static int mso_reset_fsm(struct sr_device_instance *sdi) | |
114 | { | |
115 | struct mso *mso = sdi->priv; | |
116 | uint16_t ops[1]; | |
117 | ||
118 | mso->ctlbase |= BIT_CTL_RESETFSM; | |
119 | ops[0] = mso_trans(REG_CTL, mso->ctlbase); | |
120 | ||
121 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
122 | } | |
123 | ||
124 | static int mso_toggle_led(struct sr_device_instance *sdi, int state) | |
125 | { | |
126 | struct mso *mso = sdi->priv; | |
127 | uint16_t ops[1]; | |
128 | ||
129 | mso->ctlbase &= BIT_CTL_LED; | |
130 | if (state) | |
131 | mso->ctlbase |= BIT_CTL_LED; | |
132 | ops[0] = mso_trans(REG_CTL, mso->ctlbase); | |
133 | ||
134 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
135 | } | |
136 | ||
137 | static int mso_check_trigger(struct sr_device_instance *sdi, | |
138 | uint8_t *info) | |
139 | { | |
140 | uint16_t ops[] = { mso_trans(REG_TRIGGER, 0) }; | |
141 | char buf[1]; | |
142 | int ret; | |
143 | ||
144 | ret = mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
145 | if (info == NULL || ret != SR_OK) | |
146 | return ret; | |
147 | ||
148 | buf[0] = 0; | |
149 | if (serial_read(sdi->serial->fd, buf, 1) != 1) /* FIXME: Need timeout */ | |
150 | ret = SR_ERR; | |
151 | *info = buf[0]; | |
152 | ||
153 | return ret; | |
154 | } | |
155 | ||
156 | static int mso_read_buffer(struct sr_device_instance *sdi) | |
157 | { | |
158 | uint16_t ops[] = { mso_trans(REG_BUFFER, 0) }; | |
159 | ||
160 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
161 | } | |
162 | ||
163 | static int mso_arm(struct sr_device_instance *sdi) | |
164 | { | |
165 | struct mso *mso = sdi->priv; | |
166 | uint16_t ops[] = { | |
167 | mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_RESETFSM), | |
168 | mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_ARM), | |
169 | mso_trans(REG_CTL, mso->ctlbase), | |
170 | }; | |
171 | ||
172 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
173 | } | |
174 | ||
175 | static int mso_force_capture(struct sr_device_instance *sdi) | |
176 | { | |
177 | struct mso *mso = sdi->priv; | |
178 | uint16_t ops[] = { | |
179 | mso_trans(REG_CTL, mso->ctlbase | 8), | |
180 | mso_trans(REG_CTL, mso->ctlbase), | |
181 | }; | |
182 | ||
183 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
184 | } | |
185 | ||
186 | static int mso_dac_out(struct sr_device_instance *sdi, uint16_t val) | |
187 | { | |
188 | struct mso *mso = sdi->priv; | |
189 | uint16_t ops[] = { | |
190 | mso_trans(REG_DAC1, (val >> 8) & 0xff), | |
191 | mso_trans(REG_DAC2, val & 0xff), | |
192 | mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_RESETADC), | |
193 | }; | |
194 | ||
195 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
196 | } | |
197 | ||
198 | static int mso_clkrate_out(struct sr_device_instance *sdi, uint16_t val) | |
199 | { | |
200 | uint16_t ops[] = { | |
201 | mso_trans(REG_CLKRATE1, (val >> 8) & 0xff), | |
202 | mso_trans(REG_CLKRATE2, val & 0xff), | |
203 | }; | |
204 | ||
205 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
206 | } | |
207 | ||
208 | static int mso_configure_rate(struct sr_device_instance *sdi, | |
209 | uint32_t rate) | |
210 | { | |
211 | struct mso *mso = sdi->priv; | |
212 | unsigned int i; | |
213 | int ret = SR_ERR; | |
214 | ||
215 | for (i = 0; i < ARRAY_SIZE(rate_map); i++) { | |
216 | if (rate_map[i].rate == rate) { | |
217 | mso->slowmode = rate_map[i].slowmode; | |
218 | ret = mso_clkrate_out(sdi, rate_map[i].val); | |
219 | if (ret == SR_OK) | |
220 | mso->cur_rate = rate; | |
221 | return ret; | |
222 | } | |
223 | } | |
224 | return ret; | |
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 sr_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 sr_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 SR_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 SR_OK; | |
348 | } | |
349 | ||
350 | static int hw_init(char *deviceinfo) | |
351 | { | |
352 | struct sr_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) != SR_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 = sr_device_instance_new(devcnt, SR_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 = sr_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 | sr_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 sr_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 | sr_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 sr_device_instance *sdi; | |
480 | struct mso *mso; | |
481 | int ret = SR_ERR; | |
482 | ||
483 | if (!(sdi = sr_get_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 != SR_OK) | |
493 | return ret; | |
494 | ||
495 | sdi->status = SR_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 != SR_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 != SR_OK) | |
511 | // return ret; | |
512 | ||
513 | // return SR_ERR; | |
514 | return SR_OK; | |
515 | } | |
516 | ||
517 | static void hw_closedev(int device_index) | |
518 | { | |
519 | struct sr_device_instance *sdi; | |
520 | ||
521 | if (!(sdi = sr_get_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 = SR_ST_INACTIVE; | |
528 | } | |
529 | } | |
530 | ||
531 | static void *hw_get_device_info(int device_index, int device_info_id) | |
532 | { | |
533 | struct sr_device_instance *sdi; | |
534 | struct mso *mso; | |
535 | void *info = NULL; | |
536 | ||
537 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) | |
538 | return NULL; | |
539 | mso = sdi->priv; | |
540 | ||
541 | switch (device_info_id) { | |
542 | case SR_DI_INSTANCE: | |
543 | info = sdi; | |
544 | break; | |
545 | case SR_DI_NUM_PROBES: /* FIXME: How to report analog probe? */ | |
546 | info = GINT_TO_POINTER(8); | |
547 | break; | |
548 | case SR_DI_SAMPLERATES: | |
549 | info = &samplerates; | |
550 | break; | |
551 | case SR_DI_TRIGGER_TYPES: | |
552 | info = "01"; /* FIXME */ | |
553 | break; | |
554 | case SR_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 sr_device_instance *sdi; | |
564 | ||
565 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) | |
566 | return SR_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 sr_device_instance *sdi; | |
579 | ||
580 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) | |
581 | return SR_ERR; | |
582 | ||
583 | switch (capability) { | |
584 | case SR_HWCAP_SAMPLERATE: | |
585 | return mso_configure_rate(sdi, *(uint64_t *) value); | |
586 | case SR_HWCAP_PROBECONFIG: | |
587 | case SR_HWCAP_LIMIT_SAMPLES: | |
588 | default: | |
589 | return SR_OK; /* FIXME */ | |
590 | } | |
591 | } | |
592 | ||
593 | #define MSO_TRIGGER_UNKNOWN '!' | |
594 | #define MSO_TRIGGER_UNKNOWN1 '1' | |
595 | #define MSO_TRIGGER_UNKNOWN2 '2' | |
596 | #define MSO_TRIGGER_UNKNOWN3 '3' | |
597 | #define MSO_TRIGGER_WAIT '4' | |
598 | #define MSO_TRIGGER_FIRED '5' | |
599 | #define MSO_TRIGGER_DATAREADY '6' | |
600 | ||
601 | /* FIXME: Pass errors? */ | |
602 | static int receive_data(int fd, int revents, void *user_data) | |
603 | { | |
604 | struct sr_device_instance *sdi = user_data; | |
605 | struct mso *mso = sdi->priv; | |
606 | struct sr_datafeed_packet packet; | |
607 | uint8_t in[1024], logic_out[1024]; | |
608 | double analog_out[1024]; | |
609 | size_t i, s; | |
610 | ||
611 | revents = revents; | |
612 | ||
613 | s = serial_read(fd, in, sizeof(in)); | |
614 | if (s <= 0) | |
615 | return FALSE; | |
616 | ||
617 | /* No samples */ | |
618 | if (mso->trigger_state != MSO_TRIGGER_DATAREADY) { | |
619 | mso->trigger_state = in[0]; | |
620 | if (mso->trigger_state == MSO_TRIGGER_DATAREADY) { | |
621 | mso_read_buffer(sdi); | |
622 | mso->buffer_n = 0; | |
623 | } else { | |
624 | mso_check_trigger(sdi, NULL); | |
625 | } | |
626 | return FALSE; | |
627 | } | |
628 | ||
629 | /* the hardware always dumps 1024 samples, 24bits each */ | |
630 | if (mso->buffer_n < 3072) { | |
631 | memcpy(mso->buffer + mso->buffer_n, in, s); | |
632 | mso->buffer_n += s; | |
633 | } | |
634 | if (mso->buffer_n < 3072) | |
635 | return FALSE; | |
636 | ||
637 | /* do the conversion */ | |
638 | for (i = 0; i < 1024; i++) { | |
639 | /* FIXME: Need to do conversion to mV */ | |
640 | analog_out[i] = (mso->buffer[i * 3] & 0x3f) | | |
641 | ((mso->buffer[i * 3 + 1] & 0xf) << 6); | |
642 | logic_out[i] = ((mso->buffer[i * 3 + 1] & 0x30) >> 4) | | |
643 | ((mso->buffer[i * 3 + 2] & 0x3f) << 2); | |
644 | } | |
645 | ||
646 | packet.type = SR_DF_LOGIC; | |
647 | packet.length = 1024; | |
648 | packet.unitsize = 1; | |
649 | packet.payload = logic_out; | |
650 | sr_session_bus(mso->session_id, &packet); | |
651 | ||
652 | ||
653 | packet.type = SR_DF_ANALOG; | |
654 | packet.length = 1024; | |
655 | packet.unitsize = sizeof(double); | |
656 | packet.payload = analog_out; | |
657 | sr_session_bus(mso->session_id, &packet); | |
658 | ||
659 | packet.type = SR_DF_END; | |
660 | sr_session_bus(mso->session_id, &packet); | |
661 | ||
662 | return TRUE; | |
663 | } | |
664 | ||
665 | static int hw_start_acquisition(int device_index, gpointer session_device_id) | |
666 | { | |
667 | struct sr_device_instance *sdi; | |
668 | struct mso *mso; | |
669 | struct sr_datafeed_packet packet; | |
670 | struct sr_datafeed_header header; | |
671 | int ret = SR_ERR; | |
672 | ||
673 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) | |
674 | return ret; | |
675 | mso = sdi->priv; | |
676 | ||
677 | /* FIXME: No need to do full reconfigure every time */ | |
678 | // ret = mso_reset_fsm(sdi); | |
679 | // if (ret != SR_OK) | |
680 | // return ret; | |
681 | ||
682 | /* FIXME: ACDC Mode */ | |
683 | mso->ctlbase &= 0x7f; | |
684 | // mso->ctlbase |= mso->acdcmode; | |
685 | ||
686 | ret = mso_configure_rate(sdi, mso->cur_rate); | |
687 | if (ret != SR_OK) | |
688 | return ret; | |
689 | ||
690 | /* set dac offset */ | |
691 | ret = mso_dac_out(sdi, mso->dac_offset); | |
692 | if (ret != SR_OK) | |
693 | return ret; | |
694 | ||
695 | ret = mso_configure_threshold_level(sdi); | |
696 | if (ret != SR_OK) | |
697 | return ret; | |
698 | ||
699 | ret = mso_configure_trigger(sdi); | |
700 | if (ret != SR_OK) | |
701 | return ret; | |
702 | ||
703 | /* FIXME: trigger_position */ | |
704 | ||
705 | ||
706 | /* END of config hardware part */ | |
707 | ||
708 | /* with trigger */ | |
709 | ret = mso_arm(sdi); | |
710 | if (ret != SR_OK) | |
711 | return ret; | |
712 | ||
713 | /* without trigger */ | |
714 | // ret = mso_force_capture(sdi); | |
715 | // if (ret != SR_OK) | |
716 | // return ret; | |
717 | ||
718 | mso_check_trigger(sdi, &mso->trigger_state); | |
719 | ret = mso_check_trigger(sdi, NULL); | |
720 | if (ret != SR_OK) | |
721 | return ret; | |
722 | ||
723 | mso->session_id = session_device_id; | |
724 | sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi); | |
725 | ||
726 | packet.type = SR_DF_HEADER; | |
727 | packet.length = sizeof(struct sr_datafeed_header); | |
728 | packet.payload = (unsigned char *) &header; | |
729 | header.feed_version = 1; | |
730 | gettimeofday(&header.starttime, NULL); | |
731 | header.samplerate = mso->cur_rate; | |
732 | header.num_analog_probes = 1; | |
733 | header.num_logic_probes = 8; | |
734 | header.protocol_id = SR_PROTO_RAW; | |
735 | sr_session_bus(session_device_id, &packet); | |
736 | ||
737 | return ret; | |
738 | } | |
739 | ||
740 | /* FIXME */ | |
741 | static void hw_stop_acquisition(int device_index, gpointer session_device_id) | |
742 | { | |
743 | struct sr_datafeed_packet packet; | |
744 | ||
745 | device_index = device_index; | |
746 | ||
747 | packet.type = SR_DF_END; | |
748 | sr_session_bus(session_device_id, &packet); | |
749 | } | |
750 | ||
751 | struct sr_device_plugin link_mso19_plugin_info = { | |
752 | .name = "link-mso19", | |
753 | .longname = "Link Instruments MSO-19", | |
754 | .api_version = 1, | |
755 | .init = hw_init, | |
756 | .cleanup = hw_cleanup, | |
757 | .open = hw_opendev, | |
758 | .close = hw_closedev, | |
759 | .get_device_info = hw_get_device_info, | |
760 | .get_status = hw_get_status, | |
761 | .get_capabilities = hw_get_capabilities, | |
762 | .set_configuration = hw_set_configuration, | |
763 | .start_acquisition = hw_start_acquisition, | |
764 | .stop_acquisition = hw_stop_acquisition, | |
765 | }; |