]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com> | |
5 | * Copyright (C) 2012 Renato Caldas <rmsc@fe.up.pt> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <unistd.h> | |
25 | #include <fcntl.h> | |
26 | #include <sys/time.h> | |
27 | #include <inttypes.h> | |
28 | #include <glib.h> | |
29 | #include <libudev.h> | |
30 | #include <arpa/inet.h> | |
31 | #include "sigrok.h" | |
32 | #include "sigrok-internal.h" | |
33 | #include "config.h" | |
34 | #include "link-mso19.h" | |
35 | ||
36 | #define USB_VENDOR "3195" | |
37 | #define USB_PRODUCT "f190" | |
38 | ||
39 | #define NUM_PROBES 8 | |
40 | ||
41 | static int capabilities[] = { | |
42 | SR_HWCAP_LOGIC_ANALYZER, | |
43 | // SR_HWCAP_OSCILLOSCOPE, | |
44 | // SR_HWCAP_PAT_GENERATOR, | |
45 | ||
46 | SR_HWCAP_SAMPLERATE, | |
47 | // SR_HWCAP_CAPTURE_RATIO, | |
48 | SR_HWCAP_LIMIT_SAMPLES, | |
49 | 0, | |
50 | }; | |
51 | ||
52 | static const char *probe_names[NUM_PROBES + 1] = { | |
53 | "0", | |
54 | "1", | |
55 | "2", | |
56 | "3", | |
57 | "4", | |
58 | "5", | |
59 | "6", | |
60 | "7", | |
61 | NULL, | |
62 | }; | |
63 | ||
64 | static uint64_t supported_samplerates[] = { | |
65 | SR_HZ(100), | |
66 | SR_HZ(200), | |
67 | SR_HZ(500), | |
68 | SR_KHZ(1), | |
69 | SR_KHZ(2), | |
70 | SR_KHZ(5), | |
71 | SR_KHZ(10), | |
72 | SR_KHZ(20), | |
73 | SR_KHZ(50), | |
74 | SR_KHZ(100), | |
75 | SR_KHZ(200), | |
76 | SR_KHZ(500), | |
77 | SR_MHZ(1), | |
78 | SR_MHZ(2), | |
79 | SR_MHZ(5), | |
80 | SR_MHZ(10), | |
81 | SR_MHZ(20), | |
82 | SR_MHZ(50), | |
83 | SR_MHZ(100), | |
84 | SR_MHZ(200), | |
85 | 0, | |
86 | }; | |
87 | ||
88 | static struct sr_samplerates samplerates = { | |
89 | SR_HZ(100), | |
90 | SR_MHZ(200), | |
91 | SR_HZ(0), | |
92 | supported_samplerates, | |
93 | }; | |
94 | ||
95 | static GSList *device_instances = NULL; | |
96 | ||
97 | static int mso_send_control_message(struct sr_device_instance *sdi, | |
98 | uint16_t payload[], int n) | |
99 | { | |
100 | int fd = sdi->serial->fd; | |
101 | int i, w, ret, s = n * 2 + sizeof(mso_head) + sizeof(mso_foot); | |
102 | char *p, *buf; | |
103 | ||
104 | ret = SR_ERR; | |
105 | ||
106 | if (fd < 0) | |
107 | goto ret; | |
108 | ||
109 | if (!(buf = g_try_malloc(s))) { | |
110 | sr_err("mso19: %s: buf malloc failed", __func__); | |
111 | ret = SR_ERR_MALLOC; | |
112 | goto ret; | |
113 | } | |
114 | ||
115 | p = buf; | |
116 | memcpy(p, mso_head, sizeof(mso_head)); | |
117 | p += sizeof(mso_head); | |
118 | ||
119 | for (i = 0; i < n; i++) { | |
120 | *(uint16_t *) p = htons(payload[i]); | |
121 | p += 2; | |
122 | } | |
123 | memcpy(p, mso_foot, sizeof(mso_foot)); | |
124 | ||
125 | w = 0; | |
126 | while (w < s) { | |
127 | ret = serial_write(fd, buf + w, s - w); | |
128 | if (ret < 0) { | |
129 | ret = SR_ERR; | |
130 | goto free; | |
131 | } | |
132 | w += ret; | |
133 | } | |
134 | ret = SR_OK; | |
135 | free: | |
136 | g_free(buf); | |
137 | ret: | |
138 | return ret; | |
139 | } | |
140 | ||
141 | static int mso_reset_adc(struct sr_device_instance *sdi) | |
142 | { | |
143 | struct mso *mso = sdi->priv; | |
144 | uint16_t ops[2]; | |
145 | ||
146 | ops[0] = mso_trans(REG_CTL1, (mso->ctlbase1 | BIT_CTL1_RESETADC)); | |
147 | ops[1] = mso_trans(REG_CTL1, mso->ctlbase1); | |
148 | mso->ctlbase1 |= BIT_CTL1_ADC_UNKNOWN4; | |
149 | ||
150 | sr_dbg("Requesting ADC reset"); | |
151 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
152 | } | |
153 | ||
154 | static int mso_reset_fsm(struct sr_device_instance *sdi) | |
155 | { | |
156 | struct mso *mso = sdi->priv; | |
157 | uint16_t ops[1]; | |
158 | ||
159 | mso->ctlbase1 |= BIT_CTL1_RESETFSM; | |
160 | ops[0] = mso_trans(REG_CTL1, mso->ctlbase1); | |
161 | ||
162 | sr_dbg("Requesting ADC reset"); | |
163 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
164 | } | |
165 | ||
166 | static int mso_toggle_led(struct sr_device_instance *sdi, int state) | |
167 | { | |
168 | struct mso *mso = sdi->priv; | |
169 | uint16_t ops[1]; | |
170 | ||
171 | mso->ctlbase1 &= ~BIT_CTL1_LED; | |
172 | if (state) | |
173 | mso->ctlbase1 |= BIT_CTL1_LED; | |
174 | ops[0] = mso_trans(REG_CTL1, mso->ctlbase1); | |
175 | ||
176 | sr_dbg("Requesting LED toggle"); | |
177 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
178 | } | |
179 | ||
180 | static int mso_check_trigger(struct sr_device_instance *sdi, | |
181 | uint8_t *info) | |
182 | { | |
183 | uint16_t ops[] = { mso_trans(REG_TRIGGER, 0) }; | |
184 | char buf[1]; | |
185 | int ret; | |
186 | ||
187 | sr_dbg("Requesting trigger state"); | |
188 | ret = mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
189 | if (info == NULL || ret != SR_OK) | |
190 | return ret; | |
191 | ||
192 | buf[0] = 0; | |
193 | if (serial_read(sdi->serial->fd, buf, 1) != 1) /* FIXME: Need timeout */ | |
194 | ret = SR_ERR; | |
195 | *info = buf[0]; | |
196 | ||
197 | sr_dbg("Trigger state is: 0x%x", *info); | |
198 | return ret; | |
199 | } | |
200 | ||
201 | static int mso_read_buffer(struct sr_device_instance *sdi) | |
202 | { | |
203 | uint16_t ops[] = { mso_trans(REG_BUFFER, 0) }; | |
204 | ||
205 | sr_dbg("Requesting buffer dump"); | |
206 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
207 | } | |
208 | ||
209 | static int mso_arm(struct sr_device_instance *sdi) | |
210 | { | |
211 | struct mso *mso = sdi->priv; | |
212 | uint16_t ops[] = { | |
213 | mso_trans(REG_CTL1, mso->ctlbase1 | BIT_CTL1_RESETFSM), | |
214 | mso_trans(REG_CTL1, mso->ctlbase1 | BIT_CTL1_ARM), | |
215 | mso_trans(REG_CTL1, mso->ctlbase1), | |
216 | }; | |
217 | ||
218 | sr_dbg("Requesting trigger arm"); | |
219 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
220 | } | |
221 | ||
222 | static int mso_force_capture(struct sr_device_instance *sdi) | |
223 | { | |
224 | struct mso *mso = sdi->priv; | |
225 | uint16_t ops[] = { | |
226 | mso_trans(REG_CTL1, mso->ctlbase1 | 8), | |
227 | mso_trans(REG_CTL1, mso->ctlbase1), | |
228 | }; | |
229 | ||
230 | sr_dbg("Requesting forced capture"); | |
231 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
232 | } | |
233 | ||
234 | static int mso_dac_out(struct sr_device_instance *sdi, uint16_t val) | |
235 | { | |
236 | struct mso *mso = sdi->priv; | |
237 | uint16_t ops[] = { | |
238 | mso_trans(REG_DAC1, (val >> 8) & 0xff), | |
239 | mso_trans(REG_DAC2, val & 0xff), | |
240 | mso_trans(REG_CTL1, mso->ctlbase1 | BIT_CTL1_RESETADC), | |
241 | }; | |
242 | ||
243 | sr_dbg("Setting dac word to 0x%x", val); | |
244 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
245 | } | |
246 | ||
247 | static int mso_clkrate_out(struct sr_device_instance *sdi, uint16_t val) | |
248 | { | |
249 | uint16_t ops[] = { | |
250 | mso_trans(REG_CLKRATE1, (val >> 8) & 0xff), | |
251 | mso_trans(REG_CLKRATE2, val & 0xff), | |
252 | }; | |
253 | ||
254 | sr_dbg("Setting clkrate word to 0x%x", val); | |
255 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
256 | } | |
257 | ||
258 | static int mso_configure_rate(struct sr_device_instance *sdi, | |
259 | uint32_t rate) | |
260 | { | |
261 | struct mso *mso = sdi->priv; | |
262 | unsigned int i; | |
263 | int ret = SR_ERR; | |
264 | ||
265 | for (i = 0; i < ARRAY_SIZE(rate_map); i++) { | |
266 | if (rate_map[i].rate == rate) { | |
267 | mso->ctlbase2 = rate_map[i].slowmode; | |
268 | ret = mso_clkrate_out(sdi, rate_map[i].val); | |
269 | if (ret == SR_OK) | |
270 | mso->cur_rate = rate; | |
271 | return ret; | |
272 | } | |
273 | } | |
274 | return ret; | |
275 | } | |
276 | ||
277 | static inline uint16_t mso_calc_raw_from_mv(struct mso *mso) | |
278 | { | |
279 | return (uint16_t) (0x200 - | |
280 | ((mso->dso_trigger_voltage / mso->dso_probe_attn) / | |
281 | mso->vbit)); | |
282 | } | |
283 | ||
284 | static int mso_configure_trigger(struct sr_device_instance *sdi) | |
285 | { | |
286 | struct mso *mso = sdi->priv; | |
287 | uint16_t ops[16]; | |
288 | uint16_t dso_trigger = mso_calc_raw_from_mv(mso); | |
289 | ||
290 | dso_trigger &= 0x3ff; | |
291 | if ((!mso->trigger_slope && mso->trigger_chan == 1) || | |
292 | (mso->trigger_slope && | |
293 | (mso->trigger_chan == 0 || | |
294 | mso->trigger_chan == 2 || | |
295 | mso->trigger_chan == 3))) | |
296 | dso_trigger |= 0x400; | |
297 | ||
298 | switch (mso->trigger_chan) { | |
299 | case 1: | |
300 | dso_trigger |= 0xe000; | |
301 | case 2: | |
302 | dso_trigger |= 0x4000; | |
303 | break; | |
304 | case 3: | |
305 | dso_trigger |= 0x2000; | |
306 | break; | |
307 | case 4: | |
308 | dso_trigger |= 0xa000; | |
309 | break; | |
310 | case 5: | |
311 | dso_trigger |= 0x8000; | |
312 | break; | |
313 | default: | |
314 | case 0: | |
315 | break; | |
316 | } | |
317 | ||
318 | switch (mso->trigger_outsrc) { | |
319 | case 1: | |
320 | dso_trigger |= 0x800; | |
321 | break; | |
322 | case 2: | |
323 | dso_trigger |= 0x1000; | |
324 | break; | |
325 | case 3: | |
326 | dso_trigger |= 0x1800; | |
327 | break; | |
328 | ||
329 | } | |
330 | ||
331 | ops[0] = mso_trans(5, mso->la_trigger); | |
332 | ops[1] = mso_trans(6, mso->la_trigger_mask); | |
333 | ops[2] = mso_trans(3, dso_trigger & 0xff); | |
334 | ops[3] = mso_trans(4, (dso_trigger >> 8) & 0xff); | |
335 | ops[4] = mso_trans(11, | |
336 | mso->dso_trigger_width / SR_HZ_TO_NS(mso->cur_rate)); | |
337 | ||
338 | /* Select the SPI/I2C trigger config bank */ | |
339 | ops[5] = mso_trans(REG_CTL2, (mso->ctlbase2 | BITS_CTL2_BANK(2))); | |
340 | /* Configure the SPI/I2C protocol trigger */ | |
341 | ops[6] = mso_trans(REG_PT_WORD(0), mso->protocol_trigger.word[0]); | |
342 | ops[7] = mso_trans(REG_PT_WORD(1), mso->protocol_trigger.word[1]); | |
343 | ops[8] = mso_trans(REG_PT_WORD(2), mso->protocol_trigger.word[2]); | |
344 | ops[9] = mso_trans(REG_PT_WORD(3), mso->protocol_trigger.word[3]); | |
345 | ops[10] = mso_trans(REG_PT_MASK(0), mso->protocol_trigger.mask[0]); | |
346 | ops[11] = mso_trans(REG_PT_MASK(1), mso->protocol_trigger.mask[1]); | |
347 | ops[12] = mso_trans(REG_PT_MASK(2), mso->protocol_trigger.mask[2]); | |
348 | ops[13] = mso_trans(REG_PT_MASK(3), mso->protocol_trigger.mask[3]); | |
349 | ops[14] = mso_trans(REG_PT_SPIMODE, mso->protocol_trigger.spimode); | |
350 | /* Select the default config bank */ | |
351 | ops[15] = mso_trans(REG_CTL2, mso->ctlbase2); | |
352 | ||
353 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
354 | } | |
355 | ||
356 | static int mso_configure_threshold_level(struct sr_device_instance *sdi) | |
357 | { | |
358 | struct mso *mso = sdi->priv; | |
359 | ||
360 | return mso_dac_out(sdi, la_threshold_map[mso->la_threshold]); | |
361 | } | |
362 | ||
363 | static int mso_parse_serial(const char *iSerial, const char *iProduct, | |
364 | struct mso *mso) | |
365 | { | |
366 | unsigned int u1, u2, u3, u4, u5, u6; | |
367 | ||
368 | iProduct = iProduct; | |
369 | /* FIXME: This code is in the original app, but I think its | |
370 | * used only for the GUI */ | |
371 | /* if (strstr(iProduct, "REV_02") || strstr(iProduct, "REV_03")) | |
372 | mso->num_sample_rates = 0x16; | |
373 | else | |
374 | mso->num_sample_rates = 0x10; */ | |
375 | ||
376 | /* parse iSerial */ | |
377 | if (iSerial[0] != '4' || sscanf(iSerial, "%5u%3u%3u%1u%1u%6u", | |
378 | &u1, &u2, &u3, &u4, &u5, &u6) != 6) | |
379 | return SR_ERR; | |
380 | mso->hwmodel = u4; | |
381 | mso->hwrev = u5; | |
382 | mso->serial = u6; | |
383 | mso->vbit = u1 / 10000; | |
384 | if (mso->vbit == 0) | |
385 | mso->vbit = 4.19195; | |
386 | mso->dac_offset = u2; | |
387 | if (mso->dac_offset == 0) | |
388 | mso->dac_offset = 0x1ff; | |
389 | mso->offset_range = u3; | |
390 | if (mso->offset_range == 0) | |
391 | mso->offset_range = 0x17d; | |
392 | ||
393 | /* | |
394 | * FIXME: There is more code on the original software to handle | |
395 | * bigger iSerial strings, but as I can't test on my device | |
396 | * I will not implement it yet | |
397 | */ | |
398 | ||
399 | return SR_OK; | |
400 | } | |
401 | ||
402 | static int hw_init(const char *deviceinfo) | |
403 | { | |
404 | struct sr_device_instance *sdi; | |
405 | int devcnt = 0; | |
406 | struct udev *udev; | |
407 | struct udev_enumerate *enumerate; | |
408 | struct udev_list_entry *devices, *dev_list_entry; | |
409 | struct mso *mso; | |
410 | ||
411 | deviceinfo = deviceinfo; | |
412 | ||
413 | /* It's easier to map usb<->serial using udev */ | |
414 | /* | |
415 | * FIXME: On windows we can get the same information from the | |
416 | * registry, add an #ifdef here later | |
417 | */ | |
418 | udev = udev_new(); | |
419 | if (!udev) { | |
420 | sr_warn("Failed to initialize udev."); | |
421 | goto ret; | |
422 | } | |
423 | enumerate = udev_enumerate_new(udev); | |
424 | udev_enumerate_add_match_subsystem(enumerate, "usb-serial"); | |
425 | udev_enumerate_scan_devices(enumerate); | |
426 | devices = udev_enumerate_get_list_entry(enumerate); | |
427 | udev_list_entry_foreach(dev_list_entry, devices) { | |
428 | const char *syspath, *sysname, *idVendor, *idProduct, | |
429 | *iSerial, *iProduct; | |
430 | char path[32], manufacturer[32], product[32], hwrev[32]; | |
431 | struct udev_device *dev, *parent; | |
432 | size_t s; | |
433 | ||
434 | syspath = udev_list_entry_get_name(dev_list_entry); | |
435 | dev = udev_device_new_from_syspath(udev, syspath); | |
436 | sysname = udev_device_get_sysname(dev); | |
437 | parent = udev_device_get_parent_with_subsystem_devtype( | |
438 | dev, "usb", "usb_device"); | |
439 | if (!parent) { | |
440 | sr_warn("Unable to find parent usb device for %s", | |
441 | sysname); | |
442 | continue; | |
443 | } | |
444 | ||
445 | idVendor = udev_device_get_sysattr_value(parent, "idVendor"); | |
446 | idProduct = udev_device_get_sysattr_value(parent, "idProduct"); | |
447 | if (strcmp(USB_VENDOR, idVendor) | |
448 | || strcmp(USB_PRODUCT, idProduct)) | |
449 | continue; | |
450 | ||
451 | iSerial = udev_device_get_sysattr_value(parent, "serial"); | |
452 | iProduct = udev_device_get_sysattr_value(parent, "product"); | |
453 | ||
454 | snprintf(path, sizeof(path), "/dev/%s", sysname); | |
455 | ||
456 | s = strcspn(iProduct, " "); | |
457 | if (s > sizeof(product) || | |
458 | strlen(iProduct) - s > sizeof(manufacturer)) { | |
459 | sr_warn("Could not parse iProduct: %s", iProduct); | |
460 | continue; | |
461 | } | |
462 | strncpy(product, iProduct, s); | |
463 | product[s] = 0; | |
464 | strcpy(manufacturer, iProduct + s); | |
465 | ||
466 | if (!(mso = g_try_malloc0(sizeof(struct mso)))) { | |
467 | sr_err("mso19: %s: mso malloc failed", __func__); | |
468 | continue; /* TODO: Errors handled correctly? */ | |
469 | } | |
470 | ||
471 | if (mso_parse_serial(iSerial, iProduct, mso) != SR_OK) { | |
472 | sr_warn("Invalid iSerial: %s", iSerial); | |
473 | goto err_free_mso; | |
474 | } | |
475 | sprintf(hwrev, "r%d", mso->hwrev); | |
476 | ||
477 | /* hardware initial state */ | |
478 | mso->ctlbase1 = 0; | |
479 | { | |
480 | /* Initialize the protocol trigger configuration */ | |
481 | int i; | |
482 | for (i = 0; i < 4; i++) | |
483 | { | |
484 | mso->protocol_trigger.word[i] = 0; | |
485 | mso->protocol_trigger.mask[i] = 0xff; | |
486 | } | |
487 | mso->protocol_trigger.spimode = 0; | |
488 | } | |
489 | ||
490 | sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING, | |
491 | manufacturer, product, hwrev); | |
492 | if (!sdi) { | |
493 | sr_warn("Unable to create device instance for %s", | |
494 | sysname); | |
495 | goto err_free_mso; | |
496 | } | |
497 | ||
498 | /* save a pointer to our private instance data */ | |
499 | sdi->priv = mso; | |
500 | ||
501 | sdi->serial = sr_serial_device_instance_new(path, -1); | |
502 | if (!sdi->serial) | |
503 | goto err_device_instance_free; | |
504 | ||
505 | device_instances = g_slist_append(device_instances, sdi); | |
506 | devcnt++; | |
507 | continue; | |
508 | ||
509 | err_device_instance_free: | |
510 | sr_device_instance_free(sdi); | |
511 | err_free_mso: | |
512 | free(mso); | |
513 | } | |
514 | ||
515 | udev_enumerate_unref(enumerate); | |
516 | udev_unref(udev); | |
517 | ||
518 | ret: | |
519 | return devcnt; | |
520 | } | |
521 | ||
522 | static void hw_cleanup(void) | |
523 | { | |
524 | GSList *l; | |
525 | struct sr_device_instance *sdi; | |
526 | ||
527 | /* Properly close all devices. */ | |
528 | for (l = device_instances; l; l = l->next) { | |
529 | sdi = l->data; | |
530 | if (sdi->serial->fd != -1) | |
531 | serial_close(sdi->serial->fd); | |
532 | if (sdi->priv != NULL) | |
533 | { | |
534 | free(sdi->priv); | |
535 | sdi->priv = NULL; | |
536 | } | |
537 | sr_device_instance_free(sdi); | |
538 | } | |
539 | g_slist_free(device_instances); | |
540 | device_instances = NULL; | |
541 | } | |
542 | ||
543 | static int hw_opendev(int device_index) | |
544 | { | |
545 | struct sr_device_instance *sdi; | |
546 | struct mso *mso; | |
547 | int ret = SR_ERR; | |
548 | ||
549 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) | |
550 | return ret; | |
551 | ||
552 | mso = sdi->priv; | |
553 | sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR); | |
554 | if (sdi->serial->fd == -1) | |
555 | return ret; | |
556 | ||
557 | ret = serial_set_params(sdi->serial->fd, 460800, 8, 0, 1, 2); | |
558 | if (ret != SR_OK) | |
559 | return ret; | |
560 | ||
561 | sdi->status = SR_ST_ACTIVE; | |
562 | ||
563 | /* FIXME: discard serial buffer */ | |
564 | ||
565 | mso_check_trigger(sdi, &mso->trigger_state); | |
566 | sr_dbg("trigger state: 0x%x", mso->trigger_state); | |
567 | ||
568 | ret = mso_reset_adc(sdi); | |
569 | if (ret != SR_OK) | |
570 | return ret; | |
571 | ||
572 | mso_check_trigger(sdi, &mso->trigger_state); | |
573 | sr_dbg("trigger state: 0x%x", mso->trigger_state); | |
574 | ||
575 | // ret = mso_reset_fsm(sdi); | |
576 | // if (ret != SR_OK) | |
577 | // return ret; | |
578 | ||
579 | sr_dbg("Finished %s", __func__); | |
580 | ||
581 | // return SR_ERR; | |
582 | return SR_OK; | |
583 | } | |
584 | ||
585 | static int hw_closedev(int device_index) | |
586 | { | |
587 | struct sr_device_instance *sdi; | |
588 | ||
589 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) { | |
590 | sr_err("mso19: %s: sdi was NULL", __func__); | |
591 | return SR_ERR; /* TODO: SR_ERR_ARG? */ | |
592 | } | |
593 | ||
594 | /* TODO */ | |
595 | if (sdi->serial->fd != -1) { | |
596 | serial_close(sdi->serial->fd); | |
597 | sdi->serial->fd = -1; | |
598 | sdi->status = SR_ST_INACTIVE; | |
599 | } | |
600 | ||
601 | sr_dbg("finished %s", __func__); | |
602 | return SR_OK; | |
603 | } | |
604 | ||
605 | static void *hw_get_device_info(int device_index, int device_info_id) | |
606 | { | |
607 | struct sr_device_instance *sdi; | |
608 | struct mso *mso; | |
609 | void *info = NULL; | |
610 | ||
611 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) | |
612 | return NULL; | |
613 | mso = sdi->priv; | |
614 | ||
615 | switch (device_info_id) { | |
616 | case SR_DI_INSTANCE: | |
617 | info = sdi; | |
618 | break; | |
619 | case SR_DI_NUM_PROBES: /* FIXME: How to report analog probe? */ | |
620 | info = GINT_TO_POINTER(NUM_PROBES); | |
621 | break; | |
622 | case SR_DI_PROBE_NAMES: | |
623 | info = probe_names; | |
624 | break; | |
625 | case SR_DI_SAMPLERATES: | |
626 | info = &samplerates; | |
627 | break; | |
628 | case SR_DI_TRIGGER_TYPES: | |
629 | info = "01"; /* FIXME */ | |
630 | break; | |
631 | case SR_DI_CUR_SAMPLERATE: | |
632 | info = &mso->cur_rate; | |
633 | break; | |
634 | } | |
635 | return info; | |
636 | } | |
637 | ||
638 | static int hw_get_status(int device_index) | |
639 | { | |
640 | struct sr_device_instance *sdi; | |
641 | ||
642 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) | |
643 | return SR_ST_NOT_FOUND; | |
644 | ||
645 | return sdi->status; | |
646 | } | |
647 | ||
648 | static int *hw_get_capabilities(void) | |
649 | { | |
650 | return capabilities; | |
651 | } | |
652 | ||
653 | static int hw_set_configuration(int device_index, int capability, void *value) | |
654 | { | |
655 | struct sr_device_instance *sdi; | |
656 | ||
657 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) | |
658 | return SR_ERR; | |
659 | ||
660 | switch (capability) { | |
661 | case SR_HWCAP_SAMPLERATE: | |
662 | return mso_configure_rate(sdi, *(uint64_t *) value); | |
663 | case SR_HWCAP_PROBECONFIG: | |
664 | case SR_HWCAP_LIMIT_SAMPLES: | |
665 | default: | |
666 | return SR_OK; /* FIXME */ | |
667 | } | |
668 | } | |
669 | ||
670 | #define MSO_TRIGGER_UNKNOWN '!' | |
671 | #define MSO_TRIGGER_UNKNOWN1 '1' | |
672 | #define MSO_TRIGGER_UNKNOWN2 '2' | |
673 | #define MSO_TRIGGER_UNKNOWN3 '3' | |
674 | #define MSO_TRIGGER_WAIT '4' | |
675 | #define MSO_TRIGGER_FIRED '5' | |
676 | #define MSO_TRIGGER_DATAREADY '6' | |
677 | ||
678 | /* FIXME: Pass errors? */ | |
679 | static int receive_data(int fd, int revents, void *user_data) | |
680 | { | |
681 | struct sr_device_instance *sdi = user_data; | |
682 | struct mso *mso = sdi->priv; | |
683 | struct sr_datafeed_packet packet; | |
684 | struct sr_datafeed_logic logic; | |
685 | uint8_t in[1024], logic_out[1024]; | |
686 | double analog_out[1024]; | |
687 | size_t i, s; | |
688 | ||
689 | revents = revents; | |
690 | ||
691 | s = serial_read(fd, in, sizeof(in)); | |
692 | if (s <= 0) | |
693 | return FALSE; | |
694 | ||
695 | /* No samples */ | |
696 | if (mso->trigger_state != MSO_TRIGGER_DATAREADY) { | |
697 | mso->trigger_state = in[0]; | |
698 | if (mso->trigger_state == MSO_TRIGGER_DATAREADY) { | |
699 | mso_read_buffer(sdi); | |
700 | mso->buffer_n = 0; | |
701 | } else { | |
702 | mso_check_trigger(sdi, NULL); | |
703 | } | |
704 | return FALSE; | |
705 | } | |
706 | ||
707 | /* the hardware always dumps 1024 samples, 24bits each */ | |
708 | if (mso->buffer_n < 3072) { | |
709 | memcpy(mso->buffer + mso->buffer_n, in, s); | |
710 | mso->buffer_n += s; | |
711 | } | |
712 | if (mso->buffer_n < 3072) | |
713 | return FALSE; | |
714 | ||
715 | /* do the conversion */ | |
716 | for (i = 0; i < 1024; i++) { | |
717 | /* FIXME: Need to do conversion to mV */ | |
718 | analog_out[i] = (mso->buffer[i * 3] & 0x3f) | | |
719 | ((mso->buffer[i * 3 + 1] & 0xf) << 6); | |
720 | logic_out[i] = ((mso->buffer[i * 3 + 1] & 0x30) >> 4) | | |
721 | ((mso->buffer[i * 3 + 2] & 0x3f) << 2); | |
722 | } | |
723 | ||
724 | packet.type = SR_DF_LOGIC; | |
725 | packet.payload = &logic; | |
726 | logic.length = 1024; | |
727 | logic.unitsize = 1; | |
728 | logic.data = logic_out; | |
729 | sr_session_bus(mso->session_id, &packet); | |
730 | ||
731 | // Dont bother fixing this yet, keep it "old style" | |
732 | /* | |
733 | packet.type = SR_DF_ANALOG; | |
734 | packet.length = 1024; | |
735 | packet.unitsize = sizeof(double); | |
736 | packet.payload = analog_out; | |
737 | sr_session_bus(mso->session_id, &packet); | |
738 | */ | |
739 | ||
740 | packet.type = SR_DF_END; | |
741 | sr_session_bus(mso->session_id, &packet); | |
742 | ||
743 | return TRUE; | |
744 | } | |
745 | ||
746 | static int hw_start_acquisition(int device_index, gpointer session_device_id) | |
747 | { | |
748 | struct sr_device_instance *sdi; | |
749 | struct mso *mso; | |
750 | struct sr_datafeed_packet packet; | |
751 | struct sr_datafeed_header header; | |
752 | int ret = SR_ERR; | |
753 | ||
754 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) | |
755 | return ret; | |
756 | mso = sdi->priv; | |
757 | ||
758 | /* FIXME: No need to do full reconfigure every time */ | |
759 | // ret = mso_reset_fsm(sdi); | |
760 | // if (ret != SR_OK) | |
761 | // return ret; | |
762 | ||
763 | /* FIXME: ACDC Mode */ | |
764 | mso->ctlbase1 &= 0x7f; | |
765 | // mso->ctlbase1 |= mso->acdcmode; | |
766 | ||
767 | ret = mso_configure_rate(sdi, mso->cur_rate); | |
768 | if (ret != SR_OK) | |
769 | return ret; | |
770 | ||
771 | /* set dac offset */ | |
772 | ret = mso_dac_out(sdi, mso->dac_offset); | |
773 | if (ret != SR_OK) | |
774 | return ret; | |
775 | ||
776 | ret = mso_configure_threshold_level(sdi); | |
777 | if (ret != SR_OK) | |
778 | return ret; | |
779 | ||
780 | ret = mso_configure_trigger(sdi); | |
781 | if (ret != SR_OK) | |
782 | return ret; | |
783 | ||
784 | /* FIXME: trigger_position */ | |
785 | ||
786 | ||
787 | /* END of config hardware part */ | |
788 | ||
789 | /* with trigger */ | |
790 | ret = mso_arm(sdi); | |
791 | if (ret != SR_OK) | |
792 | return ret; | |
793 | ||
794 | /* without trigger */ | |
795 | // ret = mso_force_capture(sdi); | |
796 | // if (ret != SR_OK) | |
797 | // return ret; | |
798 | ||
799 | mso_check_trigger(sdi, &mso->trigger_state); | |
800 | ret = mso_check_trigger(sdi, NULL); | |
801 | if (ret != SR_OK) | |
802 | return ret; | |
803 | ||
804 | mso->session_id = session_device_id; | |
805 | sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi); | |
806 | ||
807 | packet.type = SR_DF_HEADER; | |
808 | packet.payload = (unsigned char *) &header; | |
809 | header.feed_version = 1; | |
810 | gettimeofday(&header.starttime, NULL); | |
811 | header.samplerate = mso->cur_rate; | |
812 | header.num_analog_probes = 1; | |
813 | header.num_logic_probes = 8; | |
814 | sr_session_bus(session_device_id, &packet); | |
815 | ||
816 | return ret; | |
817 | } | |
818 | ||
819 | /* FIXME */ | |
820 | static void hw_stop_acquisition(int device_index, gpointer session_device_id) | |
821 | { | |
822 | struct sr_datafeed_packet packet; | |
823 | ||
824 | device_index = device_index; | |
825 | ||
826 | packet.type = SR_DF_END; | |
827 | sr_session_bus(session_device_id, &packet); | |
828 | } | |
829 | ||
830 | struct sr_device_plugin link_mso19_plugin_info = { | |
831 | .name = "link-mso19", | |
832 | .longname = "Link Instruments MSO-19", | |
833 | .api_version = 1, | |
834 | .init = hw_init, | |
835 | .cleanup = hw_cleanup, | |
836 | .opendev = hw_opendev, | |
837 | .closedev = hw_closedev, | |
838 | .get_device_info = hw_get_device_info, | |
839 | .get_status = hw_get_status, | |
840 | .get_capabilities = hw_get_capabilities, | |
841 | .set_configuration = hw_set_configuration, | |
842 | .start_acquisition = hw_start_acquisition, | |
843 | .stop_acquisition = hw_stop_acquisition, | |
844 | }; |