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"
34 #include "link-mso19.h"
36 #define USB_VENDOR "3195"
37 #define USB_PRODUCT "f190"
41 static int capabilities[] = {
42 SR_HWCAP_LOGIC_ANALYZER,
43 // SR_HWCAP_OSCILLOSCOPE,
44 // SR_HWCAP_PAT_GENERATOR,
47 // SR_HWCAP_CAPTURE_RATIO,
48 SR_HWCAP_LIMIT_SAMPLES,
52 static const char *probe_names[NUM_PROBES + 1] = {
64 static uint64_t supported_samplerates[] = {
88 static struct sr_samplerates samplerates = {
92 supported_samplerates,
95 static GSList *device_instances = NULL;
97 static int mso_send_control_message(struct sr_device_instance *sdi,
98 uint16_t payload[], int n)
100 int fd = sdi->serial->fd;
101 int i, w, ret, s = n * 2 + sizeof(mso_head) + sizeof(mso_foot);
109 if (!(buf = g_try_malloc(s))) {
110 sr_err("mso19: %s: buf malloc failed", __func__);
116 memcpy(p, mso_head, sizeof(mso_head));
117 p += sizeof(mso_head);
119 for (i = 0; i < n; i++) {
120 *(uint16_t *) p = htons(payload[i]);
123 memcpy(p, mso_foot, sizeof(mso_foot));
127 ret = serial_write(fd, buf + w, s - w);
141 static int mso_reset_adc(struct sr_device_instance *sdi)
143 struct mso *mso = sdi->priv;
146 ops[0] = mso_trans(REG_CTL1, (mso->ctlbase1 | BIT_CTL1_RESETADC));
147 ops[1] = mso_trans(REG_CTL1, mso->ctlbase1);
148 mso->ctlbase1 |= BIT_CTL1_ADC_UNKNOWN4;
150 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
153 static int mso_reset_fsm(struct sr_device_instance *sdi)
155 struct mso *mso = sdi->priv;
158 mso->ctlbase1 |= BIT_CTL1_RESETFSM;
159 ops[0] = mso_trans(REG_CTL1, mso->ctlbase1);
161 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
164 static int mso_toggle_led(struct sr_device_instance *sdi, int state)
166 struct mso *mso = sdi->priv;
169 mso->ctlbase1 &= BIT_CTL1_LED;
171 mso->ctlbase1 |= BIT_CTL1_LED;
172 ops[0] = mso_trans(REG_CTL1, mso->ctlbase1);
174 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
177 static int mso_check_trigger(struct sr_device_instance *sdi,
180 uint16_t ops[] = { mso_trans(REG_TRIGGER, 0) };
184 ret = mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
185 if (info == NULL || ret != SR_OK)
189 if (serial_read(sdi->serial->fd, buf, 1) != 1) /* FIXME: Need timeout */
196 static int mso_read_buffer(struct sr_device_instance *sdi)
198 uint16_t ops[] = { mso_trans(REG_BUFFER, 0) };
200 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
203 static int mso_arm(struct sr_device_instance *sdi)
205 struct mso *mso = sdi->priv;
207 mso_trans(REG_CTL1, mso->ctlbase1 | BIT_CTL1_RESETFSM),
208 mso_trans(REG_CTL1, mso->ctlbase1 | BIT_CTL1_ARM),
209 mso_trans(REG_CTL1, mso->ctlbase1),
212 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
215 static int mso_force_capture(struct sr_device_instance *sdi)
217 struct mso *mso = sdi->priv;
219 mso_trans(REG_CTL1, mso->ctlbase1 | 8),
220 mso_trans(REG_CTL1, mso->ctlbase1),
223 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
226 static int mso_dac_out(struct sr_device_instance *sdi, uint16_t val)
228 struct mso *mso = sdi->priv;
230 mso_trans(REG_DAC1, (val >> 8) & 0xff),
231 mso_trans(REG_DAC2, val & 0xff),
232 mso_trans(REG_CTL1, mso->ctlbase1 | BIT_CTL1_RESETADC),
235 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
238 static int mso_clkrate_out(struct sr_device_instance *sdi, uint16_t val)
241 mso_trans(REG_CLKRATE1, (val >> 8) & 0xff),
242 mso_trans(REG_CLKRATE2, val & 0xff),
245 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
248 static int mso_configure_rate(struct sr_device_instance *sdi,
251 struct mso *mso = sdi->priv;
255 for (i = 0; i < ARRAY_SIZE(rate_map); i++) {
256 if (rate_map[i].rate == rate) {
257 mso->ctlbase2 = rate_map[i].slowmode;
258 ret = mso_clkrate_out(sdi, rate_map[i].val);
260 mso->cur_rate = rate;
267 static inline uint16_t mso_calc_raw_from_mv(struct mso *mso)
269 return (uint16_t) (0x200 -
270 ((mso->dso_trigger_voltage / mso->dso_probe_attn) /
274 static int mso_configure_trigger(struct sr_device_instance *sdi)
276 struct mso *mso = sdi->priv;
278 uint16_t dso_trigger = mso_calc_raw_from_mv(mso);
280 dso_trigger &= 0x3ff;
281 if ((!mso->trigger_slope && mso->trigger_chan == 1) ||
282 (mso->trigger_slope &&
283 (mso->trigger_chan == 0 ||
284 mso->trigger_chan == 2 ||
285 mso->trigger_chan == 3)))
286 dso_trigger |= 0x400;
288 switch (mso->trigger_chan) {
290 dso_trigger |= 0xe000;
292 dso_trigger |= 0x4000;
295 dso_trigger |= 0x2000;
298 dso_trigger |= 0xa000;
301 dso_trigger |= 0x8000;
308 switch (mso->trigger_outsrc) {
310 dso_trigger |= 0x800;
313 dso_trigger |= 0x1000;
316 dso_trigger |= 0x1800;
321 ops[0] = mso_trans(5, mso->la_trigger);
322 ops[1] = mso_trans(6, mso->la_trigger_mask);
323 ops[2] = mso_trans(3, dso_trigger & 0xff);
324 ops[3] = mso_trans(4, (dso_trigger >> 8) & 0xff);
325 ops[4] = mso_trans(11,
326 mso->dso_trigger_width / SR_HZ_TO_NS(mso->cur_rate));
328 /* Select the SPI/I2C trigger config bank */
329 ops[5] = mso_trans(REG_CTL2, (mso->ctlbase2 | BITS_CTL2_BANK(2)));
330 /* Configure the SPI/I2C protocol trigger */
331 ops[6] = mso_trans(REG_PT_WORD(0), mso->protocol_trigger.word[0]);
332 ops[7] = mso_trans(REG_PT_WORD(1), mso->protocol_trigger.word[1]);
333 ops[8] = mso_trans(REG_PT_WORD(2), mso->protocol_trigger.word[2]);
334 ops[9] = mso_trans(REG_PT_WORD(3), mso->protocol_trigger.word[3]);
335 ops[10] = mso_trans(REG_PT_MASK(0), mso->protocol_trigger.mask[0]);
336 ops[11] = mso_trans(REG_PT_MASK(1), mso->protocol_trigger.mask[1]);
337 ops[12] = mso_trans(REG_PT_MASK(2), mso->protocol_trigger.mask[2]);
338 ops[13] = mso_trans(REG_PT_MASK(3), mso->protocol_trigger.mask[3]);
339 ops[14] = mso_trans(REG_PT_SPIMODE, mso->protocol_trigger.spimode);
340 /* Select the default config bank */
341 ops[15] = mso_trans(REG_CTL2, mso->ctlbase2);
343 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
346 static int mso_configure_threshold_level(struct sr_device_instance *sdi)
348 struct mso *mso = sdi->priv;
350 return mso_dac_out(sdi, la_threshold_map[mso->la_threshold]);
353 static int mso_parse_serial(const char *iSerial, const char *iProduct,
356 unsigned int u1, u2, u3, u4, u5, u6;
359 /* FIXME: This code is in the original app, but I think its
360 * used only for the GUI */
361 /* if (strstr(iProduct, "REV_02") || strstr(iProduct, "REV_03"))
362 mso->num_sample_rates = 0x16;
364 mso->num_sample_rates = 0x10; */
367 if (iSerial[0] != '4' || sscanf(iSerial, "%5u%3u%3u%1u%1u%6u",
368 &u1, &u2, &u3, &u4, &u5, &u6) != 6)
373 mso->vbit = u1 / 10000;
376 mso->dac_offset = u2;
377 if (mso->dac_offset == 0)
378 mso->dac_offset = 0x1ff;
379 mso->offset_range = u3;
380 if (mso->offset_range == 0)
381 mso->offset_range = 0x17d;
384 * FIXME: There is more code on the original software to handle
385 * bigger iSerial strings, but as I can't test on my device
386 * I will not implement it yet
392 static int hw_init(const char *deviceinfo)
394 struct sr_device_instance *sdi;
397 struct udev_enumerate *enumerate;
398 struct udev_list_entry *devices, *dev_list_entry;
401 deviceinfo = deviceinfo;
403 /* It's easier to map usb<->serial using udev */
405 * FIXME: On windows we can get the same information from the
406 * registry, add an #ifdef here later
410 sr_warn("Failed to initialize udev.");
413 enumerate = udev_enumerate_new(udev);
414 udev_enumerate_add_match_subsystem(enumerate, "usb-serial");
415 udev_enumerate_scan_devices(enumerate);
416 devices = udev_enumerate_get_list_entry(enumerate);
417 udev_list_entry_foreach(dev_list_entry, devices) {
418 const char *syspath, *sysname, *idVendor, *idProduct,
420 char path[32], manufacturer[32], product[32], hwrev[32];
421 struct udev_device *dev, *parent;
424 syspath = udev_list_entry_get_name(dev_list_entry);
425 dev = udev_device_new_from_syspath(udev, syspath);
426 sysname = udev_device_get_sysname(dev);
427 parent = udev_device_get_parent_with_subsystem_devtype(
428 dev, "usb", "usb_device");
430 sr_warn("Unable to find parent usb device for %s",
435 idVendor = udev_device_get_sysattr_value(parent, "idVendor");
436 idProduct = udev_device_get_sysattr_value(parent, "idProduct");
437 if (strcmp(USB_VENDOR, idVendor)
438 || strcmp(USB_PRODUCT, idProduct))
441 iSerial = udev_device_get_sysattr_value(parent, "serial");
442 iProduct = udev_device_get_sysattr_value(parent, "product");
444 snprintf(path, sizeof(path), "/dev/%s", sysname);
446 s = strcspn(iProduct, " ");
447 if (s > sizeof(product) ||
448 strlen(iProduct) - s > sizeof(manufacturer)) {
449 sr_warn("Could not parse iProduct: %s", iProduct);
452 strncpy(product, iProduct, s);
454 strcpy(manufacturer, iProduct + s);
456 if (!(mso = g_try_malloc0(sizeof(struct mso)))) {
457 sr_err("mso19: %s: mso malloc failed", __func__);
458 continue; /* TODO: Errors handled correctly? */
461 if (mso_parse_serial(iSerial, iProduct, mso) != SR_OK) {
462 sr_warn("Invalid iSerial: %s", iSerial);
465 sprintf(hwrev, "r%d", mso->hwrev);
467 /* hardware initial state */
470 /* Initialize the protocol trigger configuration */
472 for (i = 0; i < 4; i++)
474 mso->protocol_trigger.word[i] = 0;
475 mso->protocol_trigger.mask[i] = 0xff;
477 mso->protocol_trigger.spimode = 0;
480 sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING,
481 manufacturer, product, hwrev);
483 sr_warn("Unable to create device instance for %s",
488 /* save a pointer to our private instance data */
491 sdi->serial = sr_serial_device_instance_new(path, -1);
493 goto err_device_instance_free;
495 device_instances = g_slist_append(device_instances, sdi);
499 err_device_instance_free:
500 sr_device_instance_free(sdi);
505 udev_enumerate_unref(enumerate);
512 static void hw_cleanup(void)
515 struct sr_device_instance *sdi;
517 /* Properly close all devices. */
518 for (l = device_instances; l; l = l->next) {
520 if (sdi->serial->fd != -1)
521 serial_close(sdi->serial->fd);
522 if (sdi->priv != NULL)
524 sr_device_instance_free(sdi);
526 g_slist_free(device_instances);
527 device_instances = NULL;
530 static int hw_opendev(int device_index)
532 struct sr_device_instance *sdi;
536 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
540 sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
541 if (sdi->serial->fd == -1)
544 ret = serial_set_params(sdi->serial->fd, 460800, 8, 0, 1, 2);
548 sdi->status = SR_ST_ACTIVE;
550 /* FIXME: discard serial buffer */
552 mso_check_trigger(sdi, &mso->trigger_state);
553 // sr_warn("trigger state: %c", mso->trigger_state);
555 ret = mso_reset_adc(sdi);
559 mso_check_trigger(sdi, &mso->trigger_state);
560 // sr_warn("trigger state: %c", mso->trigger_state);
562 // ret = mso_reset_fsm(sdi);
570 static int hw_closedev(int device_index)
572 struct sr_device_instance *sdi;
574 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
575 sr_err("mso19: %s: sdi was NULL", __func__);
576 return SR_ERR; /* TODO: SR_ERR_ARG? */
580 if (sdi->serial->fd != -1) {
581 serial_close(sdi->serial->fd);
582 sdi->serial->fd = -1;
583 sdi->status = SR_ST_INACTIVE;
589 static void *hw_get_device_info(int device_index, int device_info_id)
591 struct sr_device_instance *sdi;
595 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
599 switch (device_info_id) {
603 case SR_DI_NUM_PROBES: /* FIXME: How to report analog probe? */
604 info = GINT_TO_POINTER(NUM_PROBES);
606 case SR_DI_PROBE_NAMES:
609 case SR_DI_SAMPLERATES:
612 case SR_DI_TRIGGER_TYPES:
613 info = "01"; /* FIXME */
615 case SR_DI_CUR_SAMPLERATE:
616 info = &mso->cur_rate;
622 static int hw_get_status(int device_index)
624 struct sr_device_instance *sdi;
626 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
627 return SR_ST_NOT_FOUND;
632 static int *hw_get_capabilities(void)
637 static int hw_set_configuration(int device_index, int capability, void *value)
639 struct sr_device_instance *sdi;
641 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
644 switch (capability) {
645 case SR_HWCAP_SAMPLERATE:
646 return mso_configure_rate(sdi, *(uint64_t *) value);
647 case SR_HWCAP_PROBECONFIG:
648 case SR_HWCAP_LIMIT_SAMPLES:
650 return SR_OK; /* FIXME */
654 #define MSO_TRIGGER_UNKNOWN '!'
655 #define MSO_TRIGGER_UNKNOWN1 '1'
656 #define MSO_TRIGGER_UNKNOWN2 '2'
657 #define MSO_TRIGGER_UNKNOWN3 '3'
658 #define MSO_TRIGGER_WAIT '4'
659 #define MSO_TRIGGER_FIRED '5'
660 #define MSO_TRIGGER_DATAREADY '6'
662 /* FIXME: Pass errors? */
663 static int receive_data(int fd, int revents, void *user_data)
665 struct sr_device_instance *sdi = user_data;
666 struct mso *mso = sdi->priv;
667 struct sr_datafeed_packet packet;
668 struct sr_datafeed_logic logic;
669 uint8_t in[1024], logic_out[1024];
670 double analog_out[1024];
675 s = serial_read(fd, in, sizeof(in));
680 if (mso->trigger_state != MSO_TRIGGER_DATAREADY) {
681 mso->trigger_state = in[0];
682 if (mso->trigger_state == MSO_TRIGGER_DATAREADY) {
683 mso_read_buffer(sdi);
686 mso_check_trigger(sdi, NULL);
691 /* the hardware always dumps 1024 samples, 24bits each */
692 if (mso->buffer_n < 3072) {
693 memcpy(mso->buffer + mso->buffer_n, in, s);
696 if (mso->buffer_n < 3072)
699 /* do the conversion */
700 for (i = 0; i < 1024; i++) {
701 /* FIXME: Need to do conversion to mV */
702 analog_out[i] = (mso->buffer[i * 3] & 0x3f) |
703 ((mso->buffer[i * 3 + 1] & 0xf) << 6);
704 logic_out[i] = ((mso->buffer[i * 3 + 1] & 0x30) >> 4) |
705 ((mso->buffer[i * 3 + 2] & 0x3f) << 2);
708 packet.type = SR_DF_LOGIC;
709 packet.payload = &logic;
712 logic.data = logic_out;
713 sr_session_bus(mso->session_id, &packet);
715 // Dont bother fixing this yet, keep it "old style"
717 packet.type = SR_DF_ANALOG;
718 packet.length = 1024;
719 packet.unitsize = sizeof(double);
720 packet.payload = analog_out;
721 sr_session_bus(mso->session_id, &packet);
724 packet.type = SR_DF_END;
725 sr_session_bus(mso->session_id, &packet);
730 static int hw_start_acquisition(int device_index, gpointer session_device_id)
732 struct sr_device_instance *sdi;
734 struct sr_datafeed_packet packet;
735 struct sr_datafeed_header header;
738 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
742 /* FIXME: No need to do full reconfigure every time */
743 // ret = mso_reset_fsm(sdi);
747 /* FIXME: ACDC Mode */
748 mso->ctlbase1 &= 0x7f;
749 // mso->ctlbase1 |= mso->acdcmode;
751 ret = mso_configure_rate(sdi, mso->cur_rate);
756 ret = mso_dac_out(sdi, mso->dac_offset);
760 ret = mso_configure_threshold_level(sdi);
764 ret = mso_configure_trigger(sdi);
768 /* FIXME: trigger_position */
771 /* END of config hardware part */
778 /* without trigger */
779 // ret = mso_force_capture(sdi);
783 mso_check_trigger(sdi, &mso->trigger_state);
784 ret = mso_check_trigger(sdi, NULL);
788 mso->session_id = session_device_id;
789 sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi);
791 packet.type = SR_DF_HEADER;
792 packet.payload = (unsigned char *) &header;
793 header.feed_version = 1;
794 gettimeofday(&header.starttime, NULL);
795 header.samplerate = mso->cur_rate;
796 header.num_analog_probes = 1;
797 header.num_logic_probes = 8;
798 sr_session_bus(session_device_id, &packet);
804 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
806 struct sr_datafeed_packet packet;
808 device_index = device_index;
810 packet.type = SR_DF_END;
811 sr_session_bus(session_device_id, &packet);
814 struct sr_device_plugin link_mso19_plugin_info = {
815 .name = "link-mso19",
816 .longname = "Link Instruments MSO-19",
819 .cleanup = hw_cleanup,
820 .opendev = hw_opendev,
821 .closedev = hw_closedev,
822 .get_device_info = hw_get_device_info,
823 .get_status = hw_get_status,
824 .get_capabilities = hw_get_capabilities,
825 .set_configuration = hw_set_configuration,
826 .start_acquisition = hw_start_acquisition,
827 .stop_acquisition = hw_stop_acquisition,