2 * This file is part of the sigrok project.
4 * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com>
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.
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.
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/>.
30 #include <arpa/inet.h>
31 #include <sigrok-internal.h>
33 #include "link-mso19.h"
35 #define USB_VENDOR "3195"
36 #define USB_PRODUCT "f190"
38 static int capabilities[] = {
39 SR_HWCAP_LOGIC_ANALYZER,
40 // SR_HWCAP_OSCILLOSCOPE,
41 // SR_HWCAP_PAT_GENERATOR,
44 // SR_HWCAP_CAPTURE_RATIO,
45 SR_HWCAP_LIMIT_SAMPLES,
49 static uint64_t supported_samplerates[] = {
73 static struct sr_samplerates samplerates = {
77 supported_samplerates,
80 static GSList *device_instances = NULL;
82 static int mso_send_control_message(struct sr_device_instance *sdi,
83 uint16_t payload[], int n)
85 int fd = sdi->serial->fd;
86 int i, w, ret, s = n * 2 + sizeof(mso_head) + sizeof(mso_foot);
94 if (!(buf = g_try_malloc(s))) {
95 sr_err("mso19: %s: buf malloc failed", __func__);
101 memcpy(p, mso_head, sizeof(mso_head));
102 p += sizeof(mso_head);
104 for (i = 0; i < n; i++) {
105 *(uint16_t *) p = htons(payload[i]);
108 memcpy(p, mso_foot, sizeof(mso_foot));
112 ret = serial_write(fd, buf + w, s - w);
126 static int mso_reset_adc(struct sr_device_instance *sdi)
128 struct mso *mso = sdi->priv;
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;
135 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
138 static int mso_reset_fsm(struct sr_device_instance *sdi)
140 struct mso *mso = sdi->priv;
143 mso->ctlbase |= BIT_CTL_RESETFSM;
144 ops[0] = mso_trans(REG_CTL, mso->ctlbase);
146 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
149 static int mso_toggle_led(struct sr_device_instance *sdi, int state)
151 struct mso *mso = sdi->priv;
154 mso->ctlbase &= BIT_CTL_LED;
156 mso->ctlbase |= BIT_CTL_LED;
157 ops[0] = mso_trans(REG_CTL, mso->ctlbase);
159 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
162 static int mso_check_trigger(struct sr_device_instance *sdi,
165 uint16_t ops[] = { mso_trans(REG_TRIGGER, 0) };
169 ret = mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
170 if (info == NULL || ret != SR_OK)
174 if (serial_read(sdi->serial->fd, buf, 1) != 1) /* FIXME: Need timeout */
181 static int mso_read_buffer(struct sr_device_instance *sdi)
183 uint16_t ops[] = { mso_trans(REG_BUFFER, 0) };
185 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
188 static int mso_arm(struct sr_device_instance *sdi)
190 struct mso *mso = sdi->priv;
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),
197 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
200 static int mso_force_capture(struct sr_device_instance *sdi)
202 struct mso *mso = sdi->priv;
204 mso_trans(REG_CTL, mso->ctlbase | 8),
205 mso_trans(REG_CTL, mso->ctlbase),
208 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
211 static int mso_dac_out(struct sr_device_instance *sdi, uint16_t val)
213 struct mso *mso = sdi->priv;
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),
220 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
223 static int mso_clkrate_out(struct sr_device_instance *sdi, uint16_t val)
226 mso_trans(REG_CLKRATE1, (val >> 8) & 0xff),
227 mso_trans(REG_CLKRATE2, val & 0xff),
230 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
233 static int mso_configure_rate(struct sr_device_instance *sdi,
236 struct mso *mso = sdi->priv;
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);
245 mso->cur_rate = rate;
252 static inline uint16_t mso_calc_raw_from_mv(struct mso *mso)
254 return (uint16_t) (0x200 -
255 ((mso->dso_trigger_voltage / mso->dso_probe_attn) /
259 static int mso_configure_trigger(struct sr_device_instance *sdi)
261 struct mso *mso = sdi->priv;
263 uint16_t dso_trigger = mso_calc_raw_from_mv(mso);
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;
273 switch (mso->trigger_chan) {
275 dso_trigger |= 0xe000;
277 dso_trigger |= 0x4000;
280 dso_trigger |= 0x2000;
283 dso_trigger |= 0xa000;
286 dso_trigger |= 0x8000;
293 switch (mso->trigger_outsrc) {
295 dso_trigger |= 0x800;
298 dso_trigger |= 0x1000;
301 dso_trigger |= 0x1800;
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,
311 mso->dso_trigger_width / SR_HZ_TO_NS(mso->cur_rate));
312 ops[5] = mso_trans(15, (2 | mso->slowmode));
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);
326 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
329 static int mso_configure_threshold_level(struct sr_device_instance *sdi)
331 struct mso *mso = sdi->priv;
333 return mso_dac_out(sdi, la_threshold_map[mso->la_threshold]);
336 static int mso_parse_serial(const char *iSerial, const char *iProduct,
339 unsigned int u1, u2, u3, u4, u5, u6;
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;
347 mso->num_sample_rates = 0x10; */
350 if (iSerial[0] != '4' || sscanf(iSerial, "%5u%3u%3u%1u%1u%6u",
351 &u1, &u2, &u3, &u4, &u5, &u6) != 6)
356 mso->vbit = u1 / 10000;
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;
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
375 static int hw_init(const char *deviceinfo)
377 struct sr_device_instance *sdi;
380 struct udev_enumerate *enumerate;
381 struct udev_list_entry *devices, *dev_list_entry;
384 deviceinfo = deviceinfo;
386 /* It's easier to map usb<->serial using udev */
388 * FIXME: On windows we can get the same information from the
389 * registry, add an #ifdef here later
393 sr_warn("Failed to initialize udev.");
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,
403 char path[32], manufacturer[32], product[32], hwrev[32];
404 struct udev_device *dev, *parent;
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");
413 sr_warn("Unable to find parent usb device for %s",
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))
424 iSerial = udev_device_get_sysattr_value(parent, "serial");
425 iProduct = udev_device_get_sysattr_value(parent, "product");
427 snprintf(path, sizeof(path), "/dev/%s", sysname);
429 s = strcspn(iProduct, " ");
430 if (s > sizeof(product) ||
431 strlen(iProduct) - s > sizeof(manufacturer)) {
432 sr_warn("Could not parse iProduct: %s", iProduct);
435 strncpy(product, iProduct, s);
437 strcpy(manufacturer, iProduct + s);
438 sprintf(hwrev, "r%d", mso->hwrev);
440 if (!(mso = g_try_malloc0(sizeof(struct mso)))) {
441 sr_err("mso19: %s: mso malloc failed", __func__);
442 continue; /* TODO: Errors handled correctly? */
445 if (mso_parse_serial(iSerial, iProduct, mso) != SR_OK) {
446 sr_warn("Invalid iSerial: %s", iSerial);
449 /* hardware initial state */
452 sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING,
453 manufacturer, product, hwrev);
455 sr_warn("Unable to create device instance for %s",
460 /* save a pointer to our private instance data */
463 sdi->serial = sr_serial_device_instance_new(path, -1);
465 goto err_device_instance_free;
467 device_instances = g_slist_append(device_instances, sdi);
471 err_device_instance_free:
472 sr_device_instance_free(sdi);
477 udev_enumerate_unref(enumerate);
484 static void hw_cleanup(void)
487 struct sr_device_instance *sdi;
489 /* Properly close all devices. */
490 for (l = device_instances; l; l = l->next) {
492 if (sdi->serial->fd != -1)
493 serial_close(sdi->serial->fd);
494 if (sdi->priv != NULL)
496 sr_device_instance_free(sdi);
498 g_slist_free(device_instances);
499 device_instances = NULL;
502 static int hw_opendev(int device_index)
504 struct sr_device_instance *sdi;
508 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
512 sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
513 if (sdi->serial->fd == -1)
516 ret = serial_set_params(sdi->serial->fd, 460800, 8, 0, 1, 2);
520 sdi->status = SR_ST_ACTIVE;
522 /* FIXME: discard serial buffer */
524 mso_check_trigger(sdi, &mso->trigger_state);
525 // sr_warn("trigger state: %c", mso->trigger_state);
527 ret = mso_reset_adc(sdi);
531 mso_check_trigger(sdi, &mso->trigger_state);
532 // sr_warn("trigger state: %c", mso->trigger_state);
534 // ret = mso_reset_fsm(sdi);
542 static void hw_closedev(int device_index)
544 struct sr_device_instance *sdi;
546 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
549 if (sdi->serial->fd != -1) {
550 serial_close(sdi->serial->fd);
551 sdi->serial->fd = -1;
552 sdi->status = SR_ST_INACTIVE;
556 static void *hw_get_device_info(int device_index, int device_info_id)
558 struct sr_device_instance *sdi;
562 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
566 switch (device_info_id) {
570 case SR_DI_NUM_PROBES: /* FIXME: How to report analog probe? */
571 info = GINT_TO_POINTER(8);
573 case SR_DI_SAMPLERATES:
576 case SR_DI_TRIGGER_TYPES:
577 info = "01"; /* FIXME */
579 case SR_DI_CUR_SAMPLERATE:
580 info = &mso->cur_rate;
586 static int hw_get_status(int device_index)
588 struct sr_device_instance *sdi;
590 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
591 return SR_ST_NOT_FOUND;
596 static int *hw_get_capabilities(void)
601 static int hw_set_configuration(int device_index, int capability, void *value)
603 struct sr_device_instance *sdi;
605 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
608 switch (capability) {
609 case SR_HWCAP_SAMPLERATE:
610 return mso_configure_rate(sdi, *(uint64_t *) value);
611 case SR_HWCAP_PROBECONFIG:
612 case SR_HWCAP_LIMIT_SAMPLES:
614 return SR_OK; /* FIXME */
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'
626 /* FIXME: Pass errors? */
627 static int receive_data(int fd, int revents, void *user_data)
629 struct sr_device_instance *sdi = user_data;
630 struct mso *mso = sdi->priv;
631 struct sr_datafeed_packet packet;
632 uint8_t in[1024], logic_out[1024];
633 double analog_out[1024];
638 s = serial_read(fd, in, sizeof(in));
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);
649 mso_check_trigger(sdi, NULL);
654 /* the hardware always dumps 1024 samples, 24bits each */
655 if (mso->buffer_n < 3072) {
656 memcpy(mso->buffer + mso->buffer_n, in, s);
659 if (mso->buffer_n < 3072)
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);
671 packet.type = SR_DF_LOGIC;
672 packet.length = 1024;
674 packet.payload = logic_out;
675 sr_session_bus(mso->session_id, &packet);
678 packet.type = SR_DF_ANALOG;
679 packet.length = 1024;
680 packet.unitsize = sizeof(double);
681 packet.payload = analog_out;
682 sr_session_bus(mso->session_id, &packet);
684 packet.type = SR_DF_END;
685 sr_session_bus(mso->session_id, &packet);
690 static int hw_start_acquisition(int device_index, gpointer session_device_id)
692 struct sr_device_instance *sdi;
694 struct sr_datafeed_packet packet;
695 struct sr_datafeed_header header;
698 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
702 /* FIXME: No need to do full reconfigure every time */
703 // ret = mso_reset_fsm(sdi);
707 /* FIXME: ACDC Mode */
708 mso->ctlbase &= 0x7f;
709 // mso->ctlbase |= mso->acdcmode;
711 ret = mso_configure_rate(sdi, mso->cur_rate);
716 ret = mso_dac_out(sdi, mso->dac_offset);
720 ret = mso_configure_threshold_level(sdi);
724 ret = mso_configure_trigger(sdi);
728 /* FIXME: trigger_position */
731 /* END of config hardware part */
738 /* without trigger */
739 // ret = mso_force_capture(sdi);
743 mso_check_trigger(sdi, &mso->trigger_state);
744 ret = mso_check_trigger(sdi, NULL);
748 mso->session_id = session_device_id;
749 sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi);
751 packet.type = SR_DF_HEADER;
752 packet.length = sizeof(struct sr_datafeed_header);
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;
759 header.protocol_id = SR_PROTO_RAW;
760 sr_session_bus(session_device_id, &packet);
766 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
768 struct sr_datafeed_packet packet;
770 device_index = device_index;
772 packet.type = SR_DF_END;
773 sr_session_bus(session_device_id, &packet);
776 struct sr_device_plugin link_mso19_plugin_info = {
777 .name = "link-mso19",
778 .longname = "Link Instruments MSO-19",
781 .cleanup = hw_cleanup,
782 .opendev = hw_opendev,
783 .closedev = hw_closedev,
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,