2 * This file is part of the sigrok project.
4 * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com>
5 * Copyright (C) 2012 Renato Caldas <rmsc@fe.up.pt>
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.
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.
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/>.
30 #include <arpa/inet.h>
31 #include "libsigrok.h"
32 #include "libsigrok-internal.h"
33 #include "link-mso19.h"
35 #define USB_VENDOR "3195"
36 #define USB_PRODUCT "f190"
40 static const int hwcaps[] = {
41 SR_HWCAP_LOGIC_ANALYZER,
42 // SR_HWCAP_OSCILLOSCOPE,
43 // SR_HWCAP_PAT_GENERATOR,
46 // SR_HWCAP_CAPTURE_RATIO,
47 SR_HWCAP_LIMIT_SAMPLES,
52 * Probes are numbered 0 to 7.
54 * See also: http://www.linkinstruments.com/images/mso19_1113.gif
56 static const char *probe_names[NUM_PROBES + 1] = {
68 static const uint64_t supported_samplerates[] = {
92 static const struct sr_samplerates samplerates = {
96 supported_samplerates,
99 static GSList *dev_insts = NULL;
101 static int mso_send_control_message(struct sr_dev_inst *sdi,
102 uint16_t payload[], int n)
104 int fd = sdi->serial->fd;
105 int i, w, ret, s = n * 2 + sizeof(mso_head) + sizeof(mso_foot);
113 if (!(buf = g_try_malloc(s))) {
114 sr_err("Failed to malloc message buffer.");
120 memcpy(p, mso_head, sizeof(mso_head));
121 p += sizeof(mso_head);
123 for (i = 0; i < n; i++) {
124 *(uint16_t *) p = htons(payload[i]);
127 memcpy(p, mso_foot, sizeof(mso_foot));
131 ret = serial_write(fd, buf + w, s - w);
145 static int mso_reset_adc(struct sr_dev_inst *sdi)
147 struct context *ctx = sdi->priv;
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;
154 sr_dbg("Requesting ADC reset.");
155 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
158 static int mso_reset_fsm(struct sr_dev_inst *sdi)
160 struct context *ctx = sdi->priv;
163 ctx->ctlbase1 |= BIT_CTL1_RESETFSM;
164 ops[0] = mso_trans(REG_CTL1, ctx->ctlbase1);
166 sr_dbg("Requesting ADC reset.");
167 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
170 static int mso_toggle_led(struct sr_dev_inst *sdi, int state)
172 struct context *ctx = sdi->priv;
175 ctx->ctlbase1 &= ~BIT_CTL1_LED;
177 ctx->ctlbase1 |= BIT_CTL1_LED;
178 ops[0] = mso_trans(REG_CTL1, ctx->ctlbase1);
180 sr_dbg("Requesting LED toggle.");
181 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
184 static int mso_check_trigger(struct sr_dev_inst *sdi, uint8_t *info)
186 uint16_t ops[] = { mso_trans(REG_TRIGGER, 0) };
190 sr_dbg("Requesting trigger state.");
191 ret = mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
192 if (info == NULL || ret != SR_OK)
196 if (serial_read(sdi->serial->fd, buf, 1) != 1) /* FIXME: Need timeout */
200 sr_dbg("Trigger state is: 0x%x.", *info);
204 static int mso_read_buffer(struct sr_dev_inst *sdi)
206 uint16_t ops[] = { mso_trans(REG_BUFFER, 0) };
208 sr_dbg("Requesting buffer dump.");
209 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
212 static int mso_arm(struct sr_dev_inst *sdi)
214 struct context *ctx = sdi->priv;
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),
221 sr_dbg("Requesting trigger arm.");
222 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
225 static int mso_force_capture(struct sr_dev_inst *sdi)
227 struct context *ctx = sdi->priv;
229 mso_trans(REG_CTL1, ctx->ctlbase1 | 8),
230 mso_trans(REG_CTL1, ctx->ctlbase1),
233 sr_dbg("Requesting forced capture.");
234 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
237 static int mso_dac_out(struct sr_dev_inst *sdi, uint16_t val)
239 struct context *ctx = sdi->priv;
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),
246 sr_dbg("Setting dac word to 0x%x.", val);
247 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
250 static int mso_clkrate_out(struct sr_dev_inst *sdi, uint16_t val)
253 mso_trans(REG_CLKRATE1, (val >> 8) & 0xff),
254 mso_trans(REG_CLKRATE2, val & 0xff),
257 sr_dbg("Setting clkrate word to 0x%x.", val);
258 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
261 static int mso_configure_rate(struct sr_dev_inst *sdi, uint32_t rate)
263 struct context *ctx = sdi->priv;
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);
272 ctx->cur_rate = rate;
279 static inline uint16_t mso_calc_raw_from_mv(struct context *ctx)
281 return (uint16_t) (0x200 -
282 ((ctx->dso_trigger_voltage / ctx->dso_probe_attn) /
286 static int mso_configure_trigger(struct sr_dev_inst *sdi)
288 struct context *ctx = sdi->priv;
290 uint16_t dso_trigger = mso_calc_raw_from_mv(ctx);
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;
300 switch (ctx->trigger_chan) {
302 dso_trigger |= 0xe000;
304 dso_trigger |= 0x4000;
307 dso_trigger |= 0x2000;
310 dso_trigger |= 0xa000;
313 dso_trigger |= 0x8000;
320 switch (ctx->trigger_outsrc) {
322 dso_trigger |= 0x800;
325 dso_trigger |= 0x1000;
328 dso_trigger |= 0x1800;
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));
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);
355 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
358 static int mso_configure_threshold_level(struct sr_dev_inst *sdi)
360 struct context *ctx = sdi->priv;
362 return mso_dac_out(sdi, la_threshold_map[ctx->la_threshold]);
365 static int mso_parse_serial(const char *iSerial, const char *iProduct,
368 unsigned int u1, u2, u3, u4, u5, u6;
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;
376 ctx->num_sample_rates = 0x10; */
379 if (iSerial[0] != '4' || sscanf(iSerial, "%5u%3u%3u%1u%1u%6u",
380 &u1, &u2, &u3, &u4, &u5, &u6) != 6)
385 ctx->vbit = u1 / 10000;
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;
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
404 static int hw_init(void)
411 static int hw_scan(void)
413 struct sr_dev_inst *sdi;
416 struct udev_enumerate *enumerate;
417 struct udev_list_entry *devs, *dev_list_entry;
420 /* It's easier to map usb<->serial using udev */
422 * FIXME: On windows we can get the same information from the
423 * registry, add an #ifdef here later
427 sr_err("Failed to initialize udev.");
430 enumerate = udev_enumerate_new(udev);
431 udev_enumerate_add_match_subsystem(enumerate, "usb-serial");
432 udev_enumerate_scan_devices(enumerate);
433 devs = udev_enumerate_get_list_entry(enumerate);
434 udev_list_entry_foreach(dev_list_entry, devs) {
435 const char *syspath, *sysname, *idVendor, *idProduct,
437 char path[32], manufacturer[32], product[32], hwrev[32];
438 struct udev_device *dev, *parent;
441 syspath = udev_list_entry_get_name(dev_list_entry);
442 dev = udev_device_new_from_syspath(udev, syspath);
443 sysname = udev_device_get_sysname(dev);
444 parent = udev_device_get_parent_with_subsystem_devtype(
445 dev, "usb", "usb_device");
447 sr_err("Unable to find parent usb device for %s",
452 idVendor = udev_device_get_sysattr_value(parent, "idVendor");
453 idProduct = udev_device_get_sysattr_value(parent, "idProduct");
454 if (strcmp(USB_VENDOR, idVendor)
455 || strcmp(USB_PRODUCT, idProduct))
458 iSerial = udev_device_get_sysattr_value(parent, "serial");
459 iProduct = udev_device_get_sysattr_value(parent, "product");
461 snprintf(path, sizeof(path), "/dev/%s", sysname);
463 s = strcspn(iProduct, " ");
464 if (s > sizeof(product) ||
465 strlen(iProduct) - s > sizeof(manufacturer)) {
466 sr_err("Could not parse iProduct: %s.", iProduct);
469 strncpy(product, iProduct, s);
471 strcpy(manufacturer, iProduct + s);
473 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
474 sr_err("Context malloc failed.");
475 continue; /* TODO: Errors handled correctly? */
478 if (mso_parse_serial(iSerial, iProduct, ctx) != SR_OK) {
479 sr_err("Invalid iSerial: %s.", iSerial);
482 sprintf(hwrev, "r%d", ctx->hwrev);
484 /* hardware initial state */
487 /* Initialize the protocol trigger configuration */
489 for (i = 0; i < 4; i++) {
490 ctx->protocol_trigger.word[i] = 0;
491 ctx->protocol_trigger.mask[i] = 0xff;
493 ctx->protocol_trigger.spimode = 0;
496 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
497 manufacturer, product, hwrev);
499 sr_err("Unable to create device instance for %s",
504 /* save a pointer to our private instance data */
507 sdi->serial = sr_serial_dev_inst_new(path, -1);
509 goto err_dev_inst_free;
511 dev_insts = g_slist_append(dev_insts, sdi);
516 sr_dev_inst_free(sdi);
521 udev_enumerate_unref(enumerate);
528 static int hw_cleanup(void)
531 struct sr_dev_inst *sdi;
535 /* Properly close all devices. */
536 for (l = dev_insts; l; l = l->next) {
537 if (!(sdi = l->data)) {
538 /* Log error, but continue cleaning up the rest. */
539 sr_err("%s: sdi was NULL, continuing", __func__);
543 if (sdi->serial->fd != -1)
544 serial_close(sdi->serial->fd);
545 sr_dev_inst_free(sdi);
547 g_slist_free(dev_insts);
553 static int hw_dev_open(int dev_index)
555 struct sr_dev_inst *sdi;
559 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
563 sdi->serial->fd = serial_open(sdi->serial->port, SERIAL_RDWR);
564 if (sdi->serial->fd == -1)
567 ret = serial_set_params(sdi->serial->fd, 460800, 8, 0, 1, 2);
571 sdi->status = SR_ST_ACTIVE;
573 /* FIXME: discard serial buffer */
575 mso_check_trigger(sdi, &ctx->trigger_state);
576 sr_dbg("Trigger state: 0x%x.", ctx->trigger_state);
578 ret = mso_reset_adc(sdi);
582 mso_check_trigger(sdi, &ctx->trigger_state);
583 sr_dbg("Trigger state: 0x%x.", ctx->trigger_state);
585 // ret = mso_reset_fsm(sdi);
593 static int hw_dev_close(int dev_index)
595 struct sr_dev_inst *sdi;
597 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
598 sr_err("%s: sdi was NULL", __func__);
603 if (sdi->serial->fd != -1) {
604 serial_close(sdi->serial->fd);
605 sdi->serial->fd = -1;
606 sdi->status = SR_ST_INACTIVE;
612 static const void *hw_dev_info_get(int dev_index, int dev_info_id)
614 struct sr_dev_inst *sdi;
616 const void *info = NULL;
618 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
622 switch (dev_info_id) {
626 case SR_DI_NUM_PROBES: /* FIXME: How to report analog probe? */
627 info = GINT_TO_POINTER(NUM_PROBES);
629 case SR_DI_PROBE_NAMES:
632 case SR_DI_SAMPLERATES:
635 case SR_DI_TRIGGER_TYPES:
636 info = "01"; /* FIXME */
638 case SR_DI_CUR_SAMPLERATE:
639 info = &ctx->cur_rate;
645 static int hw_dev_status_get(int dev_index)
647 struct sr_dev_inst *sdi;
649 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
650 return SR_ST_NOT_FOUND;
655 static const int *hw_hwcap_get_all(void)
660 static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
662 struct sr_dev_inst *sdi;
664 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
668 case SR_HWCAP_SAMPLERATE:
669 return mso_configure_rate(sdi, *(const uint64_t *) value);
670 case SR_HWCAP_PROBECONFIG:
671 case SR_HWCAP_LIMIT_SAMPLES:
673 return SR_OK; /* FIXME */
677 #define MSO_TRIGGER_UNKNOWN '!'
678 #define MSO_TRIGGER_UNKNOWN1 '1'
679 #define MSO_TRIGGER_UNKNOWN2 '2'
680 #define MSO_TRIGGER_UNKNOWN3 '3'
681 #define MSO_TRIGGER_WAIT '4'
682 #define MSO_TRIGGER_FIRED '5'
683 #define MSO_TRIGGER_DATAREADY '6'
685 /* FIXME: Pass errors? */
686 static int receive_data(int fd, int revents, void *cb_data)
688 struct sr_dev_inst *sdi = cb_data;
689 struct context *ctx = sdi->priv;
690 struct sr_datafeed_packet packet;
691 struct sr_datafeed_logic logic;
692 uint8_t in[1024], logic_out[1024];
693 double analog_out[1024];
698 s = serial_read(fd, in, sizeof(in));
703 if (ctx->trigger_state != MSO_TRIGGER_DATAREADY) {
704 ctx->trigger_state = in[0];
705 if (ctx->trigger_state == MSO_TRIGGER_DATAREADY) {
706 mso_read_buffer(sdi);
709 mso_check_trigger(sdi, NULL);
714 /* the hardware always dumps 1024 samples, 24bits each */
715 if (ctx->buffer_n < 3072) {
716 memcpy(ctx->buffer + ctx->buffer_n, in, s);
719 if (ctx->buffer_n < 3072)
722 /* do the conversion */
723 for (i = 0; i < 1024; i++) {
724 /* FIXME: Need to do conversion to mV */
725 analog_out[i] = (ctx->buffer[i * 3] & 0x3f) |
726 ((ctx->buffer[i * 3 + 1] & 0xf) << 6);
727 logic_out[i] = ((ctx->buffer[i * 3 + 1] & 0x30) >> 4) |
728 ((ctx->buffer[i * 3 + 2] & 0x3f) << 2);
731 packet.type = SR_DF_LOGIC;
732 packet.payload = &logic;
735 logic.data = logic_out;
736 sr_session_send(ctx->session_dev_id, &packet);
738 // Dont bother fixing this yet, keep it "old style"
740 packet.type = SR_DF_ANALOG;
741 packet.length = 1024;
742 packet.unitsize = sizeof(double);
743 packet.payload = analog_out;
744 sr_session_send(ctx->session_dev_id, &packet);
747 packet.type = SR_DF_END;
748 sr_session_send(ctx->session_dev_id, &packet);
753 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
755 struct sr_dev_inst *sdi;
757 struct sr_datafeed_packet packet;
758 struct sr_datafeed_header header;
761 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
765 /* FIXME: No need to do full reconfigure every time */
766 // ret = mso_reset_fsm(sdi);
770 /* FIXME: ACDC Mode */
771 ctx->ctlbase1 &= 0x7f;
772 // ctx->ctlbase1 |= ctx->acdcmode;
774 ret = mso_configure_rate(sdi, ctx->cur_rate);
779 ret = mso_dac_out(sdi, ctx->dac_offset);
783 ret = mso_configure_threshold_level(sdi);
787 ret = mso_configure_trigger(sdi);
791 /* FIXME: trigger_position */
794 /* END of config hardware part */
801 /* without trigger */
802 // ret = mso_force_capture(sdi);
806 mso_check_trigger(sdi, &ctx->trigger_state);
807 ret = mso_check_trigger(sdi, NULL);
811 ctx->session_dev_id = cb_data;
812 sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi);
814 packet.type = SR_DF_HEADER;
815 packet.payload = (unsigned char *) &header;
816 header.feed_version = 1;
817 gettimeofday(&header.starttime, NULL);
818 header.samplerate = ctx->cur_rate;
819 // header.num_analog_probes = 1;
820 header.num_logic_probes = 8;
821 sr_session_send(ctx->session_dev_id, &packet);
826 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
827 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
829 struct sr_datafeed_packet packet;
833 packet.type = SR_DF_END;
834 sr_session_send(cb_data, &packet);
839 SR_PRIV struct sr_dev_driver link_mso19_driver_info = {
840 .name = "link-mso19",
841 .longname = "Link Instruments MSO-19",
844 .cleanup = hw_cleanup,
846 .dev_open = hw_dev_open,
847 .dev_close = hw_dev_close,
848 .dev_info_get = hw_dev_info_get,
849 .dev_status_get = hw_dev_status_get,
850 .hwcap_get_all = hw_hwcap_get_all,
851 .dev_config_set = hw_dev_config_set,
852 .dev_acquisition_start = hw_dev_acquisition_start,
853 .dev_acquisition_stop = hw_dev_acquisition_stop,