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/>.
29 #include <arpa/inet.h>
31 #include "sigrok-internal.h"
33 #include "link-mso19.h"
35 #define USB_VENDOR "3195"
36 #define USB_PRODUCT "f190"
40 static int capabilities[] = {
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 *device_instances = NULL;
96 static int mso_send_control_message(struct sr_device_instance *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_device_instance *sdi)
142 struct mso *mso = sdi->priv;
145 ops[0] = mso_trans(REG_CTL, (mso->ctlbase | BIT_CTL_RESETADC));
146 ops[1] = mso_trans(REG_CTL, mso->ctlbase);
147 mso->ctlbase |= BIT_CTL_ADC_UNKNOWN4;
149 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
152 static int mso_reset_fsm(struct sr_device_instance *sdi)
154 struct mso *mso = sdi->priv;
157 mso->ctlbase |= BIT_CTL_RESETFSM;
158 ops[0] = mso_trans(REG_CTL, mso->ctlbase);
160 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
163 static int mso_toggle_led(struct sr_device_instance *sdi, int state)
165 struct mso *mso = sdi->priv;
168 mso->ctlbase &= BIT_CTL_LED;
170 mso->ctlbase |= BIT_CTL_LED;
171 ops[0] = mso_trans(REG_CTL, mso->ctlbase);
173 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
176 static int mso_check_trigger(struct sr_device_instance *sdi,
179 uint16_t ops[] = { mso_trans(REG_TRIGGER, 0) };
183 ret = mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
184 if (info == NULL || ret != SR_OK)
188 if (serial_read(sdi->serial->fd, buf, 1) != 1) /* FIXME: Need timeout */
195 static int mso_read_buffer(struct sr_device_instance *sdi)
197 uint16_t ops[] = { mso_trans(REG_BUFFER, 0) };
199 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
202 static int mso_arm(struct sr_device_instance *sdi)
204 struct mso *mso = sdi->priv;
206 mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_RESETFSM),
207 mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_ARM),
208 mso_trans(REG_CTL, mso->ctlbase),
211 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
214 static int mso_force_capture(struct sr_device_instance *sdi)
216 struct mso *mso = sdi->priv;
218 mso_trans(REG_CTL, mso->ctlbase | 8),
219 mso_trans(REG_CTL, mso->ctlbase),
222 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
225 static int mso_dac_out(struct sr_device_instance *sdi, uint16_t val)
227 struct mso *mso = sdi->priv;
229 mso_trans(REG_DAC1, (val >> 8) & 0xff),
230 mso_trans(REG_DAC2, val & 0xff),
231 mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_RESETADC),
234 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
237 static int mso_clkrate_out(struct sr_device_instance *sdi, uint16_t val)
240 mso_trans(REG_CLKRATE1, (val >> 8) & 0xff),
241 mso_trans(REG_CLKRATE2, val & 0xff),
244 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
247 static int mso_configure_rate(struct sr_device_instance *sdi,
250 struct mso *mso = sdi->priv;
254 for (i = 0; i < ARRAY_SIZE(rate_map); i++) {
255 if (rate_map[i].rate == rate) {
256 mso->slowmode = rate_map[i].slowmode;
257 ret = mso_clkrate_out(sdi, rate_map[i].val);
259 mso->cur_rate = rate;
266 static inline uint16_t mso_calc_raw_from_mv(struct mso *mso)
268 return (uint16_t) (0x200 -
269 ((mso->dso_trigger_voltage / mso->dso_probe_attn) /
273 static int mso_configure_trigger(struct sr_device_instance *sdi)
275 struct mso *mso = sdi->priv;
277 uint16_t dso_trigger = mso_calc_raw_from_mv(mso);
279 dso_trigger &= 0x3ff;
280 if ((!mso->trigger_slope && mso->trigger_chan == 1) ||
281 (mso->trigger_slope &&
282 (mso->trigger_chan == 0 ||
283 mso->trigger_chan == 2 ||
284 mso->trigger_chan == 3)))
285 dso_trigger |= 0x400;
287 switch (mso->trigger_chan) {
289 dso_trigger |= 0xe000;
291 dso_trigger |= 0x4000;
294 dso_trigger |= 0x2000;
297 dso_trigger |= 0xa000;
300 dso_trigger |= 0x8000;
307 switch (mso->trigger_outsrc) {
309 dso_trigger |= 0x800;
312 dso_trigger |= 0x1000;
315 dso_trigger |= 0x1800;
320 ops[0] = mso_trans(5, mso->la_trigger);
321 ops[1] = mso_trans(6, mso->la_trigger_mask);
322 ops[2] = mso_trans(3, dso_trigger & 0xff);
323 ops[3] = mso_trans(4, (dso_trigger >> 8) & 0xff);
324 ops[4] = mso_trans(11,
325 mso->dso_trigger_width / SR_HZ_TO_NS(mso->cur_rate));
326 ops[5] = mso_trans(15, (2 | mso->slowmode));
328 /* FIXME SPI/I2C Triggers */
329 ops[6] = mso_trans(0, 0);
330 ops[7] = mso_trans(1, 0);
331 ops[8] = mso_trans(2, 0);
332 ops[9] = mso_trans(3, 0);
333 ops[10] = mso_trans(4, 0xff);
334 ops[11] = mso_trans(5, 0xff);
335 ops[12] = mso_trans(6, 0xff);
336 ops[13] = mso_trans(7, 0xff);
337 ops[14] = mso_trans(8, mso->trigger_spimode);
338 ops[15] = mso_trans(15, mso->slowmode);
340 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
343 static int mso_configure_threshold_level(struct sr_device_instance *sdi)
345 struct mso *mso = sdi->priv;
347 return mso_dac_out(sdi, la_threshold_map[mso->la_threshold]);
350 static int mso_parse_serial(const char *iSerial, const char *iProduct,
353 unsigned int u1, u2, u3, u4, u5, u6;
356 /* FIXME: This code is in the original app, but I think its
357 * used only for the GUI */
358 /* if (strstr(iProduct, "REV_02") || strstr(iProduct, "REV_03"))
359 mso->num_sample_rates = 0x16;
361 mso->num_sample_rates = 0x10; */
364 if (iSerial[0] != '4' || sscanf(iSerial, "%5u%3u%3u%1u%1u%6u",
365 &u1, &u2, &u3, &u4, &u5, &u6) != 6)
370 mso->vbit = u1 / 10000;
373 mso->dac_offset = u2;
374 if (mso->dac_offset == 0)
375 mso->dac_offset = 0x1ff;
376 mso->offset_range = u3;
377 if (mso->offset_range == 0)
378 mso->offset_range = 0x17d;
381 * FIXME: There is more code on the original software to handle
382 * bigger iSerial strings, but as I can't test on my device
383 * I will not implement it yet
389 static int hw_init(const char *deviceinfo)
391 struct sr_device_instance *sdi;
394 struct udev_enumerate *enumerate;
395 struct udev_list_entry *devices, *dev_list_entry;
398 deviceinfo = deviceinfo;
400 /* It's easier to map usb<->serial using udev */
402 * FIXME: On windows we can get the same information from the
403 * registry, add an #ifdef here later
407 sr_warn("Failed to initialize udev.");
410 enumerate = udev_enumerate_new(udev);
411 udev_enumerate_add_match_subsystem(enumerate, "usb-serial");
412 udev_enumerate_scan_devices(enumerate);
413 devices = udev_enumerate_get_list_entry(enumerate);
414 udev_list_entry_foreach(dev_list_entry, devices) {
415 const char *syspath, *sysname, *idVendor, *idProduct,
417 char path[32], manufacturer[32], product[32], hwrev[32];
418 struct udev_device *dev, *parent;
421 syspath = udev_list_entry_get_name(dev_list_entry);
422 dev = udev_device_new_from_syspath(udev, syspath);
423 sysname = udev_device_get_sysname(dev);
424 parent = udev_device_get_parent_with_subsystem_devtype(
425 dev, "usb", "usb_device");
427 sr_warn("Unable to find parent usb device for %s",
432 idVendor = udev_device_get_sysattr_value(parent, "idVendor");
433 idProduct = udev_device_get_sysattr_value(parent, "idProduct");
434 if (strcmp(USB_VENDOR, idVendor)
435 || strcmp(USB_PRODUCT, idProduct))
438 iSerial = udev_device_get_sysattr_value(parent, "serial");
439 iProduct = udev_device_get_sysattr_value(parent, "product");
441 snprintf(path, sizeof(path), "/dev/%s", sysname);
443 s = strcspn(iProduct, " ");
444 if (s > sizeof(product) ||
445 strlen(iProduct) - s > sizeof(manufacturer)) {
446 sr_warn("Could not parse iProduct: %s", iProduct);
449 strncpy(product, iProduct, s);
451 strcpy(manufacturer, iProduct + s);
452 sprintf(hwrev, "r%d", mso->hwrev);
454 if (!(mso = g_try_malloc0(sizeof(struct mso)))) {
455 sr_err("mso19: %s: mso malloc failed", __func__);
456 continue; /* TODO: Errors handled correctly? */
459 if (mso_parse_serial(iSerial, iProduct, mso) != SR_OK) {
460 sr_warn("Invalid iSerial: %s", iSerial);
463 /* hardware initial state */
466 sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING,
467 manufacturer, product, hwrev);
469 sr_warn("Unable to create device instance for %s",
474 /* save a pointer to our private instance data */
477 sdi->serial = sr_serial_device_instance_new(path, -1);
479 goto err_device_instance_free;
481 device_instances = g_slist_append(device_instances, sdi);
485 err_device_instance_free:
486 sr_device_instance_free(sdi);
491 udev_enumerate_unref(enumerate);
498 static void hw_cleanup(void)
501 struct sr_device_instance *sdi;
503 /* Properly close all devices. */
504 for (l = device_instances; l; l = l->next) {
506 if (sdi->serial->fd != -1)
507 serial_close(sdi->serial->fd);
508 if (sdi->priv != NULL)
510 sr_device_instance_free(sdi);
512 g_slist_free(device_instances);
513 device_instances = NULL;
516 static int hw_opendev(int device_index)
518 struct sr_device_instance *sdi;
522 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
526 sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
527 if (sdi->serial->fd == -1)
530 ret = serial_set_params(sdi->serial->fd, 460800, 8, 0, 1, 2);
534 sdi->status = SR_ST_ACTIVE;
536 /* FIXME: discard serial buffer */
538 mso_check_trigger(sdi, &mso->trigger_state);
539 // sr_warn("trigger state: %c", mso->trigger_state);
541 ret = mso_reset_adc(sdi);
545 mso_check_trigger(sdi, &mso->trigger_state);
546 // sr_warn("trigger state: %c", mso->trigger_state);
548 // ret = mso_reset_fsm(sdi);
556 static int hw_closedev(int device_index)
558 struct sr_device_instance *sdi;
560 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
561 sr_err("mso19: %s: sdi was NULL", __func__);
562 return SR_ERR; /* TODO: SR_ERR_ARG? */
566 if (sdi->serial->fd != -1) {
567 serial_close(sdi->serial->fd);
568 sdi->serial->fd = -1;
569 sdi->status = SR_ST_INACTIVE;
575 static void *hw_get_device_info(int device_index, int device_info_id)
577 struct sr_device_instance *sdi;
581 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
585 switch (device_info_id) {
589 case SR_DI_NUM_PROBES: /* FIXME: How to report analog probe? */
590 info = GINT_TO_POINTER(NUM_PROBES);
592 case SR_DI_PROBE_NAMES:
595 case SR_DI_SAMPLERATES:
598 case SR_DI_TRIGGER_TYPES:
599 info = "01"; /* FIXME */
601 case SR_DI_CUR_SAMPLERATE:
602 info = &mso->cur_rate;
608 static int hw_get_status(int device_index)
610 struct sr_device_instance *sdi;
612 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
613 return SR_ST_NOT_FOUND;
618 static int *hw_get_capabilities(void)
623 static int hw_set_configuration(int device_index, int capability, void *value)
625 struct sr_device_instance *sdi;
627 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
630 switch (capability) {
631 case SR_HWCAP_SAMPLERATE:
632 return mso_configure_rate(sdi, *(uint64_t *) value);
633 case SR_HWCAP_PROBECONFIG:
634 case SR_HWCAP_LIMIT_SAMPLES:
636 return SR_OK; /* FIXME */
640 #define MSO_TRIGGER_UNKNOWN '!'
641 #define MSO_TRIGGER_UNKNOWN1 '1'
642 #define MSO_TRIGGER_UNKNOWN2 '2'
643 #define MSO_TRIGGER_UNKNOWN3 '3'
644 #define MSO_TRIGGER_WAIT '4'
645 #define MSO_TRIGGER_FIRED '5'
646 #define MSO_TRIGGER_DATAREADY '6'
648 /* FIXME: Pass errors? */
649 static int receive_data(int fd, int revents, void *user_data)
651 struct sr_device_instance *sdi = user_data;
652 struct mso *mso = sdi->priv;
653 struct sr_datafeed_packet packet;
654 uint8_t in[1024], logic_out[1024];
655 double analog_out[1024];
660 s = serial_read(fd, in, sizeof(in));
665 if (mso->trigger_state != MSO_TRIGGER_DATAREADY) {
666 mso->trigger_state = in[0];
667 if (mso->trigger_state == MSO_TRIGGER_DATAREADY) {
668 mso_read_buffer(sdi);
671 mso_check_trigger(sdi, NULL);
676 /* the hardware always dumps 1024 samples, 24bits each */
677 if (mso->buffer_n < 3072) {
678 memcpy(mso->buffer + mso->buffer_n, in, s);
681 if (mso->buffer_n < 3072)
684 /* do the conversion */
685 for (i = 0; i < 1024; i++) {
686 /* FIXME: Need to do conversion to mV */
687 analog_out[i] = (mso->buffer[i * 3] & 0x3f) |
688 ((mso->buffer[i * 3 + 1] & 0xf) << 6);
689 logic_out[i] = ((mso->buffer[i * 3 + 1] & 0x30) >> 4) |
690 ((mso->buffer[i * 3 + 2] & 0x3f) << 2);
693 packet.type = SR_DF_LOGIC;
694 packet.length = 1024;
696 packet.payload = logic_out;
697 sr_session_bus(mso->session_id, &packet);
700 packet.type = SR_DF_ANALOG;
701 packet.length = 1024;
702 packet.unitsize = sizeof(double);
703 packet.payload = analog_out;
704 sr_session_bus(mso->session_id, &packet);
706 packet.type = SR_DF_END;
707 sr_session_bus(mso->session_id, &packet);
712 static int hw_start_acquisition(int device_index, gpointer session_device_id)
714 struct sr_device_instance *sdi;
716 struct sr_datafeed_packet packet;
717 struct sr_datafeed_header header;
720 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
724 /* FIXME: No need to do full reconfigure every time */
725 // ret = mso_reset_fsm(sdi);
729 /* FIXME: ACDC Mode */
730 mso->ctlbase &= 0x7f;
731 // mso->ctlbase |= mso->acdcmode;
733 ret = mso_configure_rate(sdi, mso->cur_rate);
738 ret = mso_dac_out(sdi, mso->dac_offset);
742 ret = mso_configure_threshold_level(sdi);
746 ret = mso_configure_trigger(sdi);
750 /* FIXME: trigger_position */
753 /* END of config hardware part */
760 /* without trigger */
761 // ret = mso_force_capture(sdi);
765 mso_check_trigger(sdi, &mso->trigger_state);
766 ret = mso_check_trigger(sdi, NULL);
770 mso->session_id = session_device_id;
771 sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi);
773 packet.type = SR_DF_HEADER;
774 packet.length = sizeof(struct sr_datafeed_header);
775 packet.payload = (unsigned char *) &header;
776 header.feed_version = 1;
777 gettimeofday(&header.starttime, NULL);
778 header.samplerate = mso->cur_rate;
779 header.num_analog_probes = 1;
780 header.num_logic_probes = 8;
781 header.protocol_id = SR_PROTO_RAW;
782 sr_session_bus(session_device_id, &packet);
788 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
790 struct sr_datafeed_packet packet;
792 device_index = device_index;
794 packet.type = SR_DF_END;
795 sr_session_bus(session_device_id, &packet);
798 struct sr_device_plugin link_mso19_plugin_info = {
799 .name = "link-mso19",
800 .longname = "Link Instruments MSO-19",
803 .cleanup = hw_cleanup,
804 .opendev = hw_opendev,
805 .closedev = hw_closedev,
806 .get_device_info = hw_get_device_info,
807 .get_status = hw_get_status,
808 .get_capabilities = hw_get_capabilities,
809 .set_configuration = hw_set_configuration,
810 .start_acquisition = hw_start_acquisition,
811 .stop_acquisition = hw_stop_acquisition,