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[] = {
50 100, 200, 500, KHZ(1), KHZ(2), KHZ(5), KHZ(10), KHZ(20),
51 KHZ(50), KHZ(100), KHZ(200), KHZ(500), MHZ(1), MHZ(2), MHZ(5),
52 MHZ(10), MHZ(20), MHZ(50), MHZ(100), MHZ(200), 0
55 static struct sr_samplerates samplerates = {
56 100, MHZ(200), 0, supported_samplerates,
59 static GSList *device_instances = NULL;
61 static int mso_send_control_message(struct sr_device_instance *sdi,
62 uint16_t payload[], int n)
64 int fd = sdi->serial->fd;
65 int i, w, ret, s = n * 2 + sizeof(mso_head) + sizeof(mso_foot);
76 memcpy(p, mso_head, sizeof(mso_head));
77 p += sizeof(mso_head);
79 for (i = 0; i < n; i++) {
80 *(uint16_t *) p = htons(payload[i]);
83 memcpy(p, mso_foot, sizeof(mso_foot));
87 ret = serial_write(fd, buf + w, s - w);
101 static int mso_reset_adc(struct sr_device_instance *sdi)
103 struct mso *mso = sdi->priv;
106 ops[0] = mso_trans(REG_CTL, (mso->ctlbase | BIT_CTL_RESETADC));
107 ops[1] = mso_trans(REG_CTL, mso->ctlbase);
108 mso->ctlbase |= BIT_CTL_ADC_UNKNOWN4;
110 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
113 static int mso_reset_fsm(struct sr_device_instance *sdi)
115 struct mso *mso = sdi->priv;
118 mso->ctlbase |= BIT_CTL_RESETFSM;
119 ops[0] = mso_trans(REG_CTL, mso->ctlbase);
121 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
124 static int mso_toggle_led(struct sr_device_instance *sdi, int state)
126 struct mso *mso = sdi->priv;
129 mso->ctlbase &= BIT_CTL_LED;
131 mso->ctlbase |= BIT_CTL_LED;
132 ops[0] = mso_trans(REG_CTL, mso->ctlbase);
134 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
137 static int mso_check_trigger(struct sr_device_instance *sdi,
140 uint16_t ops[] = { mso_trans(REG_TRIGGER, 0) };
144 ret = mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
145 if (info == NULL || ret != SR_OK)
149 if (serial_read(sdi->serial->fd, buf, 1) != 1) /* FIXME: Need timeout */
156 static int mso_read_buffer(struct sr_device_instance *sdi)
158 uint16_t ops[] = { mso_trans(REG_BUFFER, 0) };
160 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
163 static int mso_arm(struct sr_device_instance *sdi)
165 struct mso *mso = sdi->priv;
167 mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_RESETFSM),
168 mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_ARM),
169 mso_trans(REG_CTL, mso->ctlbase),
172 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
175 static int mso_force_capture(struct sr_device_instance *sdi)
177 struct mso *mso = sdi->priv;
179 mso_trans(REG_CTL, mso->ctlbase | 8),
180 mso_trans(REG_CTL, mso->ctlbase),
183 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
186 static int mso_dac_out(struct sr_device_instance *sdi, uint16_t val)
188 struct mso *mso = sdi->priv;
190 mso_trans(REG_DAC1, (val >> 8) & 0xff),
191 mso_trans(REG_DAC2, val & 0xff),
192 mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_RESETADC),
195 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
198 static int mso_clkrate_out(struct sr_device_instance *sdi, uint16_t val)
201 mso_trans(REG_CLKRATE1, (val >> 8) & 0xff),
202 mso_trans(REG_CLKRATE2, val & 0xff),
205 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
208 static int mso_configure_rate(struct sr_device_instance *sdi,
211 struct mso *mso = sdi->priv;
215 for (i = 0; i < ARRAY_SIZE(rate_map); i++) {
216 if (rate_map[i].rate == rate) {
217 mso->slowmode = rate_map[i].slowmode;
218 ret = mso_clkrate_out(sdi, rate_map[i].val);
220 mso->cur_rate = rate;
228 static inline uint16_t mso_calc_raw_from_mv(struct mso *mso)
230 return (uint16_t) (0x200 -
231 ((mso->dso_trigger_voltage / mso->dso_probe_attn) /
235 static int mso_configure_trigger(struct sr_device_instance *sdi)
237 struct mso *mso = sdi->priv;
239 uint16_t dso_trigger = mso_calc_raw_from_mv(mso);
241 dso_trigger &= 0x3ff;
242 if ((!mso->trigger_slope && mso->trigger_chan == 1) ||
243 (mso->trigger_slope &&
244 (mso->trigger_chan == 0 ||
245 mso->trigger_chan == 2 ||
246 mso->trigger_chan == 3)))
247 dso_trigger |= 0x400;
249 switch (mso->trigger_chan) {
251 dso_trigger |= 0xe000;
253 dso_trigger |= 0x4000;
256 dso_trigger |= 0x2000;
259 dso_trigger |= 0xa000;
262 dso_trigger |= 0x8000;
269 switch (mso->trigger_outsrc) {
271 dso_trigger |= 0x800;
274 dso_trigger |= 0x1000;
277 dso_trigger |= 0x1800;
282 ops[0] = mso_trans(5, mso->la_trigger);
283 ops[1] = mso_trans(6, mso->la_trigger_mask);
284 ops[2] = mso_trans(3, dso_trigger & 0xff);
285 ops[3] = mso_trans(4, (dso_trigger >> 8) & 0xff);
286 ops[4] = mso_trans(11,
287 mso->dso_trigger_width / HZ_TO_NS(mso->cur_rate));
288 ops[5] = mso_trans(15, (2 | mso->slowmode));
290 /* FIXME SPI/I2C Triggers */
291 ops[6] = mso_trans(0, 0);
292 ops[7] = mso_trans(1, 0);
293 ops[8] = mso_trans(2, 0);
294 ops[9] = mso_trans(3, 0);
295 ops[10] = mso_trans(4, 0xff);
296 ops[11] = mso_trans(5, 0xff);
297 ops[12] = mso_trans(6, 0xff);
298 ops[13] = mso_trans(7, 0xff);
299 ops[14] = mso_trans(8, mso->trigger_spimode);
300 ops[15] = mso_trans(15, mso->slowmode);
302 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
305 static int mso_configure_threshold_level(struct sr_device_instance *sdi)
307 struct mso *mso = sdi->priv;
309 return mso_dac_out(sdi, la_threshold_map[mso->la_threshold]);
312 static int mso_parse_serial(const char *iSerial, const char *iProduct,
315 unsigned int u1, u2, u3, u4, u5, u6;
318 /* FIXME: This code is in the original app, but I think its
319 * used only for the GUI */
320 /* if (strstr(iProduct, "REV_02") || strstr(iProduct, "REV_03"))
321 mso->num_sample_rates = 0x16;
323 mso->num_sample_rates = 0x10; */
326 if (iSerial[0] != '4' || sscanf(iSerial, "%5u%3u%3u%1u%1u%6u",
327 &u1, &u2, &u3, &u4, &u5, &u6) != 6)
332 mso->vbit = u1 / 10000;
335 mso->dac_offset = u2;
336 if (mso->dac_offset == 0)
337 mso->dac_offset = 0x1ff;
338 mso->offset_range = u3;
339 if (mso->offset_range == 0)
340 mso->offset_range = 0x17d;
343 * FIXME: There is more code on the original software to handle
344 * bigger iSerial strings, but as I can't test on my device
345 * I will not implement it yet
351 static int hw_init(char *deviceinfo)
353 struct sr_device_instance *sdi;
356 struct udev_enumerate *enumerate;
357 struct udev_list_entry *devices, *dev_list_entry;
360 deviceinfo = deviceinfo;
362 /* It's easier to map usb<->serial using udev */
364 * FIXME: On windows we can get the same information from the
365 * registry, add an #ifdef here later
369 g_warning("Failed to initialize udev.");
372 enumerate = udev_enumerate_new(udev);
373 udev_enumerate_add_match_subsystem(enumerate, "usb-serial");
374 udev_enumerate_scan_devices(enumerate);
375 devices = udev_enumerate_get_list_entry(enumerate);
376 udev_list_entry_foreach(dev_list_entry, devices) {
377 const char *syspath, *sysname, *idVendor, *idProduct,
379 char path[32], manufacturer[32], product[32], hwrev[32];
380 struct udev_device *dev, *parent;
383 syspath = udev_list_entry_get_name(dev_list_entry);
384 dev = udev_device_new_from_syspath(udev, syspath);
385 sysname = udev_device_get_sysname(dev);
386 parent = udev_device_get_parent_with_subsystem_devtype(
387 dev, "usb", "usb_device");
389 g_warning("Unable to find parent usb device for %s",
394 idVendor = udev_device_get_sysattr_value(parent, "idVendor");
395 idProduct = udev_device_get_sysattr_value(parent, "idProduct");
396 if (strcmp(USB_VENDOR, idVendor)
397 || strcmp(USB_PRODUCT, idProduct))
400 iSerial = udev_device_get_sysattr_value(parent, "serial");
401 iProduct = udev_device_get_sysattr_value(parent, "product");
403 snprintf(path, sizeof(path), "/dev/%s", sysname);
405 s = strcspn(iProduct, " ");
406 if (s > sizeof(product) ||
407 strlen(iProduct) - s > sizeof(manufacturer)) {
408 g_warning("Could not parse iProduct: %s", iProduct);
411 strncpy(product, iProduct, s);
413 strcpy(manufacturer, iProduct + s);
414 sprintf(hwrev, "r%d", mso->hwrev);
416 mso = malloc(sizeof(struct mso));
419 memset(mso, 0, sizeof(struct mso));
421 if (mso_parse_serial(iSerial, iProduct, mso) != SR_OK) {
422 g_warning("Invalid iSerial: %s", iSerial);
425 /* hardware initial state */
428 sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING,
429 manufacturer, product, hwrev);
431 g_warning("Unable to create device instance for %s",
436 /* save a pointer to our private instance data */
439 sdi->serial = sr_serial_device_instance_new(path, -1);
441 goto err_device_instance_free;
443 device_instances = g_slist_append(device_instances, sdi);
447 err_device_instance_free:
448 sr_device_instance_free(sdi);
453 udev_enumerate_unref(enumerate);
460 static void hw_cleanup(void)
463 struct sr_device_instance *sdi;
465 /* Properly close all devices. */
466 for (l = device_instances; l; l = l->next) {
468 if (sdi->serial->fd != -1)
469 serial_close(sdi->serial->fd);
470 if (sdi->priv != NULL)
472 sr_device_instance_free(sdi);
474 g_slist_free(device_instances);
475 device_instances = NULL;
478 static int hw_opendev(int device_index)
480 struct sr_device_instance *sdi;
484 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
488 sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
489 if (sdi->serial->fd == -1)
492 ret = serial_set_params(sdi->serial->fd, 460800, 8, 0, 1, 2);
496 sdi->status = SR_ST_ACTIVE;
498 /* FIXME: discard serial buffer */
500 mso_check_trigger(sdi, &mso->trigger_state);
501 // g_warning("trigger state: %c", mso->trigger_state);
503 ret = mso_reset_adc(sdi);
507 mso_check_trigger(sdi, &mso->trigger_state);
508 // g_warning("trigger state: %c", mso->trigger_state);
510 // ret = mso_reset_fsm(sdi);
518 static void hw_closedev(int device_index)
520 struct sr_device_instance *sdi;
522 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
525 if (sdi->serial->fd != -1) {
526 serial_close(sdi->serial->fd);
527 sdi->serial->fd = -1;
528 sdi->status = SR_ST_INACTIVE;
532 static void *hw_get_device_info(int device_index, int device_info_id)
534 struct sr_device_instance *sdi;
538 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
542 switch (device_info_id) {
546 case SR_DI_NUM_PROBES: /* FIXME: How to report analog probe? */
547 info = GINT_TO_POINTER(8);
549 case SR_DI_SAMPLERATES:
552 case SR_DI_TRIGGER_TYPES:
553 info = "01"; /* FIXME */
555 case SR_DI_CUR_SAMPLERATE:
556 info = &mso->cur_rate;
562 static int hw_get_status(int device_index)
564 struct sr_device_instance *sdi;
566 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
567 return SR_ST_NOT_FOUND;
572 static int *hw_get_capabilities(void)
577 static int hw_set_configuration(int device_index, int capability, void *value)
579 struct sr_device_instance *sdi;
581 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
584 switch (capability) {
585 case SR_HWCAP_SAMPLERATE:
586 return mso_configure_rate(sdi, *(uint64_t *) value);
587 case SR_HWCAP_PROBECONFIG:
588 case SR_HWCAP_LIMIT_SAMPLES:
590 return SR_OK; /* FIXME */
595 #define MSO_TRIGGER_UNKNOWN '!'
596 #define MSO_TRIGGER_UNKNOWN1 '1'
597 #define MSO_TRIGGER_UNKNOWN2 '2'
598 #define MSO_TRIGGER_UNKNOWN3 '3'
599 #define MSO_TRIGGER_WAIT '4'
600 #define MSO_TRIGGER_FIRED '5'
601 #define MSO_TRIGGER_DATAREADY '6'
603 /* FIXME: Pass errors? */
604 static int receive_data(int fd, int revents, void *user_data)
606 struct sr_device_instance *sdi = user_data;
607 struct mso *mso = sdi->priv;
608 struct sr_datafeed_packet packet;
609 uint8_t in[1024], logic_out[1024];
610 double analog_out[1024];
615 s = serial_read(fd, in, sizeof(in));
620 if (mso->trigger_state != MSO_TRIGGER_DATAREADY) {
621 mso->trigger_state = in[0];
622 if (mso->trigger_state == MSO_TRIGGER_DATAREADY) {
623 mso_read_buffer(sdi);
626 mso_check_trigger(sdi, NULL);
631 /* the hardware always dumps 1024 samples, 24bits each */
632 if (mso->buffer_n < 3072) {
633 memcpy(mso->buffer + mso->buffer_n, in, s);
636 if (mso->buffer_n < 3072)
639 /* do the conversion */
640 for (i = 0; i < 1024; i++) {
641 /* FIXME: Need to do conversion to mV */
642 analog_out[i] = (mso->buffer[i * 3] & 0x3f) |
643 ((mso->buffer[i * 3 + 1] & 0xf) << 6);
644 logic_out[i] = ((mso->buffer[i * 3 + 1] & 0x30) >> 4) |
645 ((mso->buffer[i * 3 + 2] & 0x3f) << 2);
648 packet.type = SR_DF_LOGIC;
649 packet.length = 1024;
651 packet.payload = logic_out;
652 sr_session_bus(mso->session_id, &packet);
655 packet.type = SR_DF_ANALOG;
656 packet.length = 1024;
657 packet.unitsize = sizeof(double);
658 packet.payload = analog_out;
659 sr_session_bus(mso->session_id, &packet);
661 packet.type = SR_DF_END;
662 sr_session_bus(mso->session_id, &packet);
667 static int hw_start_acquisition(int device_index, gpointer session_device_id)
669 struct sr_device_instance *sdi;
671 struct sr_datafeed_packet packet;
672 struct sr_datafeed_header header;
675 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
679 /* FIXME: No need to do full reconfigure every time */
680 // ret = mso_reset_fsm(sdi);
684 /* FIXME: ACDC Mode */
685 mso->ctlbase &= 0x7f;
686 // mso->ctlbase |= mso->acdcmode;
688 ret = mso_configure_rate(sdi, mso->cur_rate);
693 ret = mso_dac_out(sdi, mso->dac_offset);
697 ret = mso_configure_threshold_level(sdi);
701 ret = mso_configure_trigger(sdi);
705 /* FIXME: trigger_position */
708 /* END of config hardware part */
715 /* without trigger */
716 // ret = mso_force_capture(sdi);
720 mso_check_trigger(sdi, &mso->trigger_state);
721 ret = mso_check_trigger(sdi, NULL);
725 mso->session_id = session_device_id;
726 source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi);
728 packet.type = SR_DF_HEADER;
729 packet.length = sizeof(struct sr_datafeed_header);
730 packet.payload = (unsigned char *) &header;
731 header.feed_version = 1;
732 gettimeofday(&header.starttime, NULL);
733 header.samplerate = mso->cur_rate;
734 header.num_analog_probes = 1;
735 header.num_logic_probes = 8;
736 header.protocol_id = SR_PROTO_RAW;
737 sr_session_bus(session_device_id, &packet);
743 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
745 struct sr_datafeed_packet packet;
747 device_index = device_index;
749 packet.type = SR_DF_END;
750 sr_session_bus(session_device_id, &packet);
753 struct sr_device_plugin link_mso19_plugin_info = {
754 .name = "link-mso19",
755 .longname = "Link Instruments MSO-19",
758 .cleanup = hw_cleanup,
760 .close = hw_closedev,
761 .get_device_info = hw_get_device_info,
762 .get_status = hw_get_status,
763 .get_capabilities = hw_get_capabilities,
764 .set_configuration = hw_set_configuration,
765 .start_acquisition = hw_start_acquisition,
766 .stop_acquisition = hw_stop_acquisition,