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