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