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