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>
32 #include "sigrok-internal.h"
33 #include "link-mso19.h"
35 #define USB_VENDOR "3195"
36 #define USB_PRODUCT "f190"
40 static 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,
51 static const char *probe_names[NUM_PROBES + 1] = {
63 static uint64_t supported_samplerates[] = {
87 static struct sr_samplerates samplerates = {
91 supported_samplerates,
94 static GSList *dev_insts = NULL;
96 static int mso_send_control_message(struct sr_dev_inst *sdi,
97 uint16_t payload[], int n)
99 int fd = sdi->serial->fd;
100 int i, w, ret, s = n * 2 + sizeof(mso_head) + sizeof(mso_foot);
108 if (!(buf = g_try_malloc(s))) {
109 sr_err("mso19: %s: buf malloc failed", __func__);
115 memcpy(p, mso_head, sizeof(mso_head));
116 p += sizeof(mso_head);
118 for (i = 0; i < n; i++) {
119 *(uint16_t *) p = htons(payload[i]);
122 memcpy(p, mso_foot, sizeof(mso_foot));
126 ret = serial_write(fd, buf + w, s - w);
140 static int mso_reset_adc(struct sr_dev_inst *sdi)
142 struct mso *mso = sdi->priv;
145 ops[0] = mso_trans(REG_CTL1, (mso->ctlbase1 | BIT_CTL1_RESETADC));
146 ops[1] = mso_trans(REG_CTL1, mso->ctlbase1);
147 mso->ctlbase1 |= BIT_CTL1_ADC_UNKNOWN4;
149 sr_dbg("mso19: Requesting ADC reset");
150 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
153 static int mso_reset_fsm(struct sr_dev_inst *sdi)
155 struct mso *mso = sdi->priv;
158 mso->ctlbase1 |= BIT_CTL1_RESETFSM;
159 ops[0] = mso_trans(REG_CTL1, mso->ctlbase1);
161 sr_dbg("mso19: Requesting ADC reset");
162 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
165 static int mso_toggle_led(struct sr_dev_inst *sdi, int state)
167 struct mso *mso = sdi->priv;
170 mso->ctlbase1 &= ~BIT_CTL1_LED;
172 mso->ctlbase1 |= BIT_CTL1_LED;
173 ops[0] = mso_trans(REG_CTL1, mso->ctlbase1);
175 sr_dbg("mso19: Requesting LED toggle");
176 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
179 static int mso_check_trigger(struct sr_dev_inst *sdi, uint8_t *info)
181 uint16_t ops[] = { mso_trans(REG_TRIGGER, 0) };
185 sr_dbg("mso19: Requesting trigger state");
186 ret = mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
187 if (info == NULL || ret != SR_OK)
191 if (serial_read(sdi->serial->fd, buf, 1) != 1) /* FIXME: Need timeout */
195 sr_dbg("mso19: Trigger state is: 0x%x", *info);
199 static int mso_read_buffer(struct sr_dev_inst *sdi)
201 uint16_t ops[] = { mso_trans(REG_BUFFER, 0) };
203 sr_dbg("mso19: Requesting buffer dump");
204 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
207 static int mso_arm(struct sr_dev_inst *sdi)
209 struct mso *mso = sdi->priv;
211 mso_trans(REG_CTL1, mso->ctlbase1 | BIT_CTL1_RESETFSM),
212 mso_trans(REG_CTL1, mso->ctlbase1 | BIT_CTL1_ARM),
213 mso_trans(REG_CTL1, mso->ctlbase1),
216 sr_dbg("mso19: Requesting trigger arm");
217 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
220 static int mso_force_capture(struct sr_dev_inst *sdi)
222 struct mso *mso = sdi->priv;
224 mso_trans(REG_CTL1, mso->ctlbase1 | 8),
225 mso_trans(REG_CTL1, mso->ctlbase1),
228 sr_dbg("mso19: Requesting forced capture");
229 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
232 static int mso_dac_out(struct sr_dev_inst *sdi, uint16_t val)
234 struct mso *mso = sdi->priv;
236 mso_trans(REG_DAC1, (val >> 8) & 0xff),
237 mso_trans(REG_DAC2, val & 0xff),
238 mso_trans(REG_CTL1, mso->ctlbase1 | BIT_CTL1_RESETADC),
241 sr_dbg("mso19: Setting dac word to 0x%x", val);
242 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
245 static int mso_clkrate_out(struct sr_dev_inst *sdi, uint16_t val)
248 mso_trans(REG_CLKRATE1, (val >> 8) & 0xff),
249 mso_trans(REG_CLKRATE2, val & 0xff),
252 sr_dbg("mso19: Setting clkrate word to 0x%x", val);
253 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
256 static int mso_configure_rate(struct sr_dev_inst *sdi, uint32_t rate)
258 struct mso *mso = sdi->priv;
262 for (i = 0; i < ARRAY_SIZE(rate_map); i++) {
263 if (rate_map[i].rate == rate) {
264 mso->ctlbase2 = rate_map[i].slowmode;
265 ret = mso_clkrate_out(sdi, rate_map[i].val);
267 mso->cur_rate = rate;
274 static inline uint16_t mso_calc_raw_from_mv(struct mso *mso)
276 return (uint16_t) (0x200 -
277 ((mso->dso_trigger_voltage / mso->dso_probe_attn) /
281 static int mso_configure_trigger(struct sr_dev_inst *sdi)
283 struct mso *mso = sdi->priv;
285 uint16_t dso_trigger = mso_calc_raw_from_mv(mso);
287 dso_trigger &= 0x3ff;
288 if ((!mso->trigger_slope && mso->trigger_chan == 1) ||
289 (mso->trigger_slope &&
290 (mso->trigger_chan == 0 ||
291 mso->trigger_chan == 2 ||
292 mso->trigger_chan == 3)))
293 dso_trigger |= 0x400;
295 switch (mso->trigger_chan) {
297 dso_trigger |= 0xe000;
299 dso_trigger |= 0x4000;
302 dso_trigger |= 0x2000;
305 dso_trigger |= 0xa000;
308 dso_trigger |= 0x8000;
315 switch (mso->trigger_outsrc) {
317 dso_trigger |= 0x800;
320 dso_trigger |= 0x1000;
323 dso_trigger |= 0x1800;
328 ops[0] = mso_trans(5, mso->la_trigger);
329 ops[1] = mso_trans(6, mso->la_trigger_mask);
330 ops[2] = mso_trans(3, dso_trigger & 0xff);
331 ops[3] = mso_trans(4, (dso_trigger >> 8) & 0xff);
332 ops[4] = mso_trans(11,
333 mso->dso_trigger_width / SR_HZ_TO_NS(mso->cur_rate));
335 /* Select the SPI/I2C trigger config bank */
336 ops[5] = mso_trans(REG_CTL2, (mso->ctlbase2 | BITS_CTL2_BANK(2)));
337 /* Configure the SPI/I2C protocol trigger */
338 ops[6] = mso_trans(REG_PT_WORD(0), mso->protocol_trigger.word[0]);
339 ops[7] = mso_trans(REG_PT_WORD(1), mso->protocol_trigger.word[1]);
340 ops[8] = mso_trans(REG_PT_WORD(2), mso->protocol_trigger.word[2]);
341 ops[9] = mso_trans(REG_PT_WORD(3), mso->protocol_trigger.word[3]);
342 ops[10] = mso_trans(REG_PT_MASK(0), mso->protocol_trigger.mask[0]);
343 ops[11] = mso_trans(REG_PT_MASK(1), mso->protocol_trigger.mask[1]);
344 ops[12] = mso_trans(REG_PT_MASK(2), mso->protocol_trigger.mask[2]);
345 ops[13] = mso_trans(REG_PT_MASK(3), mso->protocol_trigger.mask[3]);
346 ops[14] = mso_trans(REG_PT_SPIMODE, mso->protocol_trigger.spimode);
347 /* Select the default config bank */
348 ops[15] = mso_trans(REG_CTL2, mso->ctlbase2);
350 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
353 static int mso_configure_threshold_level(struct sr_dev_inst *sdi)
355 struct mso *mso = sdi->priv;
357 return mso_dac_out(sdi, la_threshold_map[mso->la_threshold]);
360 static int mso_parse_serial(const char *iSerial, const char *iProduct,
363 unsigned int u1, u2, u3, u4, u5, u6;
366 /* FIXME: This code is in the original app, but I think its
367 * used only for the GUI */
368 /* if (strstr(iProduct, "REV_02") || strstr(iProduct, "REV_03"))
369 mso->num_sample_rates = 0x16;
371 mso->num_sample_rates = 0x10; */
374 if (iSerial[0] != '4' || sscanf(iSerial, "%5u%3u%3u%1u%1u%6u",
375 &u1, &u2, &u3, &u4, &u5, &u6) != 6)
380 mso->vbit = u1 / 10000;
383 mso->dac_offset = u2;
384 if (mso->dac_offset == 0)
385 mso->dac_offset = 0x1ff;
386 mso->offset_range = u3;
387 if (mso->offset_range == 0)
388 mso->offset_range = 0x17d;
391 * FIXME: There is more code on the original software to handle
392 * bigger iSerial strings, but as I can't test on my device
393 * I will not implement it yet
399 static int hw_init(const char *devinfo)
401 struct sr_dev_inst *sdi;
404 struct udev_enumerate *enumerate;
405 struct udev_list_entry *devs, *dev_list_entry;
410 /* It's easier to map usb<->serial using udev */
412 * FIXME: On windows we can get the same information from the
413 * registry, add an #ifdef here later
417 sr_err("mso19: Failed to initialize udev.");
420 enumerate = udev_enumerate_new(udev);
421 udev_enumerate_add_match_subsystem(enumerate, "usb-serial");
422 udev_enumerate_scan_devices(enumerate);
423 devs = udev_enumerate_get_list_entry(enumerate);
424 udev_list_entry_foreach(dev_list_entry, devs) {
425 const char *syspath, *sysname, *idVendor, *idProduct,
427 char path[32], manufacturer[32], product[32], hwrev[32];
428 struct udev_device *dev, *parent;
431 syspath = udev_list_entry_get_name(dev_list_entry);
432 dev = udev_device_new_from_syspath(udev, syspath);
433 sysname = udev_device_get_sysname(dev);
434 parent = udev_device_get_parent_with_subsystem_devtype(
435 dev, "usb", "usb_device");
437 sr_err("mso19: Unable to find parent usb device for %s",
442 idVendor = udev_device_get_sysattr_value(parent, "idVendor");
443 idProduct = udev_device_get_sysattr_value(parent, "idProduct");
444 if (strcmp(USB_VENDOR, idVendor)
445 || strcmp(USB_PRODUCT, idProduct))
448 iSerial = udev_device_get_sysattr_value(parent, "serial");
449 iProduct = udev_device_get_sysattr_value(parent, "product");
451 snprintf(path, sizeof(path), "/dev/%s", sysname);
453 s = strcspn(iProduct, " ");
454 if (s > sizeof(product) ||
455 strlen(iProduct) - s > sizeof(manufacturer)) {
456 sr_err("mso19: Could not parse iProduct: %s", iProduct);
459 strncpy(product, iProduct, s);
461 strcpy(manufacturer, iProduct + s);
463 if (!(mso = g_try_malloc0(sizeof(struct mso)))) {
464 sr_err("mso19: %s: mso malloc failed", __func__);
465 continue; /* TODO: Errors handled correctly? */
468 if (mso_parse_serial(iSerial, iProduct, mso) != SR_OK) {
469 sr_err("mso19: Invalid iSerial: %s", iSerial);
472 sprintf(hwrev, "r%d", mso->hwrev);
474 /* hardware initial state */
477 /* Initialize the protocol trigger configuration */
479 for (i = 0; i < 4; i++) {
480 mso->protocol_trigger.word[i] = 0;
481 mso->protocol_trigger.mask[i] = 0xff;
483 mso->protocol_trigger.spimode = 0;
486 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
487 manufacturer, product, hwrev);
489 sr_err("mso19: Unable to create device instance for %s",
494 /* save a pointer to our private instance data */
497 sdi->serial = sr_serial_dev_inst_new(path, -1);
499 goto err_dev_inst_free;
501 dev_insts = g_slist_append(dev_insts, sdi);
506 sr_dev_inst_free(sdi);
511 udev_enumerate_unref(enumerate);
518 static int hw_cleanup(void)
521 struct sr_dev_inst *sdi;
525 /* Properly close all devices. */
526 for (l = dev_insts; l; l = l->next) {
527 if (!(sdi = l->data)) {
528 /* Log error, but continue cleaning up the rest. */
529 sr_err("mso19: %s: sdi was NULL, continuing", __func__);
533 if (sdi->serial->fd != -1)
534 serial_close(sdi->serial->fd);
535 sr_dev_inst_free(sdi);
537 g_slist_free(dev_insts);
543 static int hw_opendev(int dev_index)
545 struct sr_dev_inst *sdi;
549 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
553 sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
554 if (sdi->serial->fd == -1)
557 ret = serial_set_params(sdi->serial->fd, 460800, 8, 0, 1, 2);
561 sdi->status = SR_ST_ACTIVE;
563 /* FIXME: discard serial buffer */
565 mso_check_trigger(sdi, &mso->trigger_state);
566 sr_dbg("mso19: trigger state: 0x%x", mso->trigger_state);
568 ret = mso_reset_adc(sdi);
572 mso_check_trigger(sdi, &mso->trigger_state);
573 sr_dbg("mso19: trigger state: 0x%x", mso->trigger_state);
575 // ret = mso_reset_fsm(sdi);
579 sr_dbg("mso19: Finished %s", __func__);
585 static int hw_closedev(int dev_index)
587 struct sr_dev_inst *sdi;
589 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
590 sr_err("mso19: %s: sdi was NULL", __func__);
591 return SR_ERR; /* TODO: SR_ERR_ARG? */
595 if (sdi->serial->fd != -1) {
596 serial_close(sdi->serial->fd);
597 sdi->serial->fd = -1;
598 sdi->status = SR_ST_INACTIVE;
601 sr_dbg("mso19: finished %s", __func__);
605 static void *hw_get_dev_info(int dev_index, int dev_info_id)
607 struct sr_dev_inst *sdi;
611 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
615 switch (dev_info_id) {
619 case SR_DI_NUM_PROBES: /* FIXME: How to report analog probe? */
620 info = GINT_TO_POINTER(NUM_PROBES);
622 case SR_DI_PROBE_NAMES:
625 case SR_DI_SAMPLERATES:
628 case SR_DI_TRIGGER_TYPES:
629 info = "01"; /* FIXME */
631 case SR_DI_CUR_SAMPLERATE:
632 info = &mso->cur_rate;
638 static int hw_get_status(int dev_index)
640 struct sr_dev_inst *sdi;
642 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
643 return SR_ST_NOT_FOUND;
648 static int *hw_hwcap_get_all(void)
653 static int hw_set_configuration(int dev_index, int hwcap, void *value)
655 struct sr_dev_inst *sdi;
657 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
661 case SR_HWCAP_SAMPLERATE:
662 return mso_configure_rate(sdi, *(uint64_t *) value);
663 case SR_HWCAP_PROBECONFIG:
664 case SR_HWCAP_LIMIT_SAMPLES:
666 return SR_OK; /* FIXME */
670 #define MSO_TRIGGER_UNKNOWN '!'
671 #define MSO_TRIGGER_UNKNOWN1 '1'
672 #define MSO_TRIGGER_UNKNOWN2 '2'
673 #define MSO_TRIGGER_UNKNOWN3 '3'
674 #define MSO_TRIGGER_WAIT '4'
675 #define MSO_TRIGGER_FIRED '5'
676 #define MSO_TRIGGER_DATAREADY '6'
678 /* FIXME: Pass errors? */
679 static int receive_data(int fd, int revents, void *user_data)
681 struct sr_dev_inst *sdi = user_data;
682 struct mso *mso = sdi->priv;
683 struct sr_datafeed_packet packet;
684 struct sr_datafeed_logic logic;
685 uint8_t in[1024], logic_out[1024];
686 double analog_out[1024];
691 s = serial_read(fd, in, sizeof(in));
696 if (mso->trigger_state != MSO_TRIGGER_DATAREADY) {
697 mso->trigger_state = in[0];
698 if (mso->trigger_state == MSO_TRIGGER_DATAREADY) {
699 mso_read_buffer(sdi);
702 mso_check_trigger(sdi, NULL);
707 /* the hardware always dumps 1024 samples, 24bits each */
708 if (mso->buffer_n < 3072) {
709 memcpy(mso->buffer + mso->buffer_n, in, s);
712 if (mso->buffer_n < 3072)
715 /* do the conversion */
716 for (i = 0; i < 1024; i++) {
717 /* FIXME: Need to do conversion to mV */
718 analog_out[i] = (mso->buffer[i * 3] & 0x3f) |
719 ((mso->buffer[i * 3 + 1] & 0xf) << 6);
720 logic_out[i] = ((mso->buffer[i * 3 + 1] & 0x30) >> 4) |
721 ((mso->buffer[i * 3 + 2] & 0x3f) << 2);
724 packet.type = SR_DF_LOGIC;
725 packet.payload = &logic;
728 logic.data = logic_out;
729 sr_session_bus(mso->session_id, &packet);
731 // Dont bother fixing this yet, keep it "old style"
733 packet.type = SR_DF_ANALOG;
734 packet.length = 1024;
735 packet.unitsize = sizeof(double);
736 packet.payload = analog_out;
737 sr_session_bus(mso->session_id, &packet);
740 packet.type = SR_DF_END;
741 sr_session_bus(mso->session_id, &packet);
746 static int hw_start_acquisition(int dev_index, gpointer session_dev_id)
748 struct sr_dev_inst *sdi;
750 struct sr_datafeed_packet packet;
751 struct sr_datafeed_header header;
754 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
758 /* FIXME: No need to do full reconfigure every time */
759 // ret = mso_reset_fsm(sdi);
763 /* FIXME: ACDC Mode */
764 mso->ctlbase1 &= 0x7f;
765 // mso->ctlbase1 |= mso->acdcmode;
767 ret = mso_configure_rate(sdi, mso->cur_rate);
772 ret = mso_dac_out(sdi, mso->dac_offset);
776 ret = mso_configure_threshold_level(sdi);
780 ret = mso_configure_trigger(sdi);
784 /* FIXME: trigger_position */
787 /* END of config hardware part */
794 /* without trigger */
795 // ret = mso_force_capture(sdi);
799 mso_check_trigger(sdi, &mso->trigger_state);
800 ret = mso_check_trigger(sdi, NULL);
804 mso->session_id = session_dev_id;
805 sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi);
807 packet.type = SR_DF_HEADER;
808 packet.payload = (unsigned char *) &header;
809 header.feed_version = 1;
810 gettimeofday(&header.starttime, NULL);
811 header.samplerate = mso->cur_rate;
812 // header.num_analog_probes = 1;
813 header.num_logic_probes = 8;
814 sr_session_bus(session_dev_id, &packet);
820 static int hw_stop_acquisition(int dev_index, gpointer session_dev_id)
822 struct sr_datafeed_packet packet;
824 dev_index = dev_index;
826 packet.type = SR_DF_END;
827 sr_session_bus(session_dev_id, &packet);
832 SR_PRIV struct sr_dev_plugin link_mso19_plugin_info = {
833 .name = "link-mso19",
834 .longname = "Link Instruments MSO-19",
837 .cleanup = hw_cleanup,
838 .opendev = hw_opendev,
839 .closedev = hw_closedev,
840 .get_dev_info = hw_get_dev_info,
841 .get_status = hw_get_status,
842 .hwcap_get_all = hw_hwcap_get_all,
843 .set_configuration = hw_set_configuration,
844 .start_acquisition = hw_start_acquisition,
845 .stop_acquisition = hw_stop_acquisition,