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