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;
227 static inline uint16_t mso_calc_raw_from_mv(struct mso *mso)
229 return (uint16_t) (0x200 -
230 ((mso->dso_trigger_voltage / mso->dso_probe_attn) /
234 static int mso_configure_trigger(struct sr_device_instance *sdi)
236 struct mso *mso = sdi->priv;
238 uint16_t dso_trigger = mso_calc_raw_from_mv(mso);
240 dso_trigger &= 0x3ff;
241 if ((!mso->trigger_slope && mso->trigger_chan == 1) ||
242 (mso->trigger_slope &&
243 (mso->trigger_chan == 0 ||
244 mso->trigger_chan == 2 ||
245 mso->trigger_chan == 3)))
246 dso_trigger |= 0x400;
248 switch (mso->trigger_chan) {
250 dso_trigger |= 0xe000;
252 dso_trigger |= 0x4000;
255 dso_trigger |= 0x2000;
258 dso_trigger |= 0xa000;
261 dso_trigger |= 0x8000;
268 switch (mso->trigger_outsrc) {
270 dso_trigger |= 0x800;
273 dso_trigger |= 0x1000;
276 dso_trigger |= 0x1800;
281 ops[0] = mso_trans(5, mso->la_trigger);
282 ops[1] = mso_trans(6, mso->la_trigger_mask);
283 ops[2] = mso_trans(3, dso_trigger & 0xff);
284 ops[3] = mso_trans(4, (dso_trigger >> 8) & 0xff);
285 ops[4] = mso_trans(11,
286 mso->dso_trigger_width / HZ_TO_NS(mso->cur_rate));
287 ops[5] = mso_trans(15, (2 | mso->slowmode));
289 /* FIXME SPI/I2C Triggers */
290 ops[6] = mso_trans(0, 0);
291 ops[7] = mso_trans(1, 0);
292 ops[8] = mso_trans(2, 0);
293 ops[9] = mso_trans(3, 0);
294 ops[10] = mso_trans(4, 0xff);
295 ops[11] = mso_trans(5, 0xff);
296 ops[12] = mso_trans(6, 0xff);
297 ops[13] = mso_trans(7, 0xff);
298 ops[14] = mso_trans(8, mso->trigger_spimode);
299 ops[15] = mso_trans(15, mso->slowmode);
301 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
304 static int mso_configure_threshold_level(struct sr_device_instance *sdi)
306 struct mso *mso = sdi->priv;
308 return mso_dac_out(sdi, la_threshold_map[mso->la_threshold]);
311 static int mso_parse_serial(const char *iSerial, const char *iProduct,
314 unsigned int u1, u2, u3, u4, u5, u6;
317 /* FIXME: This code is in the original app, but I think its
318 * used only for the GUI */
319 /* if (strstr(iProduct, "REV_02") || strstr(iProduct, "REV_03"))
320 mso->num_sample_rates = 0x16;
322 mso->num_sample_rates = 0x10; */
325 if (iSerial[0] != '4' || sscanf(iSerial, "%5u%3u%3u%1u%1u%6u",
326 &u1, &u2, &u3, &u4, &u5, &u6) != 6)
331 mso->vbit = u1 / 10000;
334 mso->dac_offset = u2;
335 if (mso->dac_offset == 0)
336 mso->dac_offset = 0x1ff;
337 mso->offset_range = u3;
338 if (mso->offset_range == 0)
339 mso->offset_range = 0x17d;
342 * FIXME: There is more code on the original software to handle
343 * bigger iSerial strings, but as I can't test on my device
344 * I will not implement it yet
350 static int hw_init(char *deviceinfo)
352 struct sr_device_instance *sdi;
355 struct udev_enumerate *enumerate;
356 struct udev_list_entry *devices, *dev_list_entry;
359 deviceinfo = deviceinfo;
361 /* It's easier to map usb<->serial using udev */
363 * FIXME: On windows we can get the same information from the
364 * registry, add an #ifdef here later
368 g_warning("Failed to initialize udev.");
371 enumerate = udev_enumerate_new(udev);
372 udev_enumerate_add_match_subsystem(enumerate, "usb-serial");
373 udev_enumerate_scan_devices(enumerate);
374 devices = udev_enumerate_get_list_entry(enumerate);
375 udev_list_entry_foreach(dev_list_entry, devices) {
376 const char *syspath, *sysname, *idVendor, *idProduct,
378 char path[32], manufacturer[32], product[32], hwrev[32];
379 struct udev_device *dev, *parent;
382 syspath = udev_list_entry_get_name(dev_list_entry);
383 dev = udev_device_new_from_syspath(udev, syspath);
384 sysname = udev_device_get_sysname(dev);
385 parent = udev_device_get_parent_with_subsystem_devtype(
386 dev, "usb", "usb_device");
388 g_warning("Unable to find parent usb device for %s",
393 idVendor = udev_device_get_sysattr_value(parent, "idVendor");
394 idProduct = udev_device_get_sysattr_value(parent, "idProduct");
395 if (strcmp(USB_VENDOR, idVendor)
396 || strcmp(USB_PRODUCT, idProduct))
399 iSerial = udev_device_get_sysattr_value(parent, "serial");
400 iProduct = udev_device_get_sysattr_value(parent, "product");
402 snprintf(path, sizeof(path), "/dev/%s", sysname);
404 s = strcspn(iProduct, " ");
405 if (s > sizeof(product) ||
406 strlen(iProduct) - s > sizeof(manufacturer)) {
407 g_warning("Could not parse iProduct: %s", iProduct);
410 strncpy(product, iProduct, s);
412 strcpy(manufacturer, iProduct + s);
413 sprintf(hwrev, "r%d", mso->hwrev);
415 mso = malloc(sizeof(struct mso));
418 memset(mso, 0, sizeof(struct mso));
420 if (mso_parse_serial(iSerial, iProduct, mso) != SR_OK) {
421 g_warning("Invalid iSerial: %s", iSerial);
424 /* hardware initial state */
427 sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING,
428 manufacturer, product, hwrev);
430 g_warning("Unable to create device instance for %s",
435 /* save a pointer to our private instance data */
438 sdi->serial = sr_serial_device_instance_new(path, -1);
440 goto err_device_instance_free;
442 device_instances = g_slist_append(device_instances, sdi);
446 err_device_instance_free:
447 sr_device_instance_free(sdi);
452 udev_enumerate_unref(enumerate);
459 static void hw_cleanup(void)
462 struct sr_device_instance *sdi;
464 /* Properly close all devices. */
465 for (l = device_instances; l; l = l->next) {
467 if (sdi->serial->fd != -1)
468 serial_close(sdi->serial->fd);
469 if (sdi->priv != NULL)
471 sr_device_instance_free(sdi);
473 g_slist_free(device_instances);
474 device_instances = NULL;
477 static int hw_opendev(int device_index)
479 struct sr_device_instance *sdi;
483 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
487 sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
488 if (sdi->serial->fd == -1)
491 ret = serial_set_params(sdi->serial->fd, 460800, 8, 0, 1, 2);
495 sdi->status = SR_ST_ACTIVE;
497 /* FIXME: discard serial buffer */
499 mso_check_trigger(sdi, &mso->trigger_state);
500 // g_warning("trigger state: %c", mso->trigger_state);
502 ret = mso_reset_adc(sdi);
506 mso_check_trigger(sdi, &mso->trigger_state);
507 // g_warning("trigger state: %c", mso->trigger_state);
509 // ret = mso_reset_fsm(sdi);
517 static void hw_closedev(int device_index)
519 struct sr_device_instance *sdi;
521 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
524 if (sdi->serial->fd != -1) {
525 serial_close(sdi->serial->fd);
526 sdi->serial->fd = -1;
527 sdi->status = SR_ST_INACTIVE;
531 static void *hw_get_device_info(int device_index, int device_info_id)
533 struct sr_device_instance *sdi;
537 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
541 switch (device_info_id) {
545 case SR_DI_NUM_PROBES: /* FIXME: How to report analog probe? */
546 info = GINT_TO_POINTER(8);
548 case SR_DI_SAMPLERATES:
551 case SR_DI_TRIGGER_TYPES:
552 info = "01"; /* FIXME */
554 case SR_DI_CUR_SAMPLERATE:
555 info = &mso->cur_rate;
561 static int hw_get_status(int device_index)
563 struct sr_device_instance *sdi;
565 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
566 return SR_ST_NOT_FOUND;
571 static int *hw_get_capabilities(void)
576 static int hw_set_configuration(int device_index, int capability, void *value)
578 struct sr_device_instance *sdi;
580 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
583 switch (capability) {
584 case SR_HWCAP_SAMPLERATE:
585 return mso_configure_rate(sdi, *(uint64_t *) value);
586 case SR_HWCAP_PROBECONFIG:
587 case SR_HWCAP_LIMIT_SAMPLES:
589 return SR_OK; /* FIXME */
593 #define MSO_TRIGGER_UNKNOWN '!'
594 #define MSO_TRIGGER_UNKNOWN1 '1'
595 #define MSO_TRIGGER_UNKNOWN2 '2'
596 #define MSO_TRIGGER_UNKNOWN3 '3'
597 #define MSO_TRIGGER_WAIT '4'
598 #define MSO_TRIGGER_FIRED '5'
599 #define MSO_TRIGGER_DATAREADY '6'
601 /* FIXME: Pass errors? */
602 static int receive_data(int fd, int revents, void *user_data)
604 struct sr_device_instance *sdi = user_data;
605 struct mso *mso = sdi->priv;
606 struct sr_datafeed_packet packet;
607 uint8_t in[1024], logic_out[1024];
608 double analog_out[1024];
613 s = serial_read(fd, in, sizeof(in));
618 if (mso->trigger_state != MSO_TRIGGER_DATAREADY) {
619 mso->trigger_state = in[0];
620 if (mso->trigger_state == MSO_TRIGGER_DATAREADY) {
621 mso_read_buffer(sdi);
624 mso_check_trigger(sdi, NULL);
629 /* the hardware always dumps 1024 samples, 24bits each */
630 if (mso->buffer_n < 3072) {
631 memcpy(mso->buffer + mso->buffer_n, in, s);
634 if (mso->buffer_n < 3072)
637 /* do the conversion */
638 for (i = 0; i < 1024; i++) {
639 /* FIXME: Need to do conversion to mV */
640 analog_out[i] = (mso->buffer[i * 3] & 0x3f) |
641 ((mso->buffer[i * 3 + 1] & 0xf) << 6);
642 logic_out[i] = ((mso->buffer[i * 3 + 1] & 0x30) >> 4) |
643 ((mso->buffer[i * 3 + 2] & 0x3f) << 2);
646 packet.type = SR_DF_LOGIC;
647 packet.length = 1024;
649 packet.payload = logic_out;
650 sr_session_bus(mso->session_id, &packet);
653 packet.type = SR_DF_ANALOG;
654 packet.length = 1024;
655 packet.unitsize = sizeof(double);
656 packet.payload = analog_out;
657 sr_session_bus(mso->session_id, &packet);
659 packet.type = SR_DF_END;
660 sr_session_bus(mso->session_id, &packet);
665 static int hw_start_acquisition(int device_index, gpointer session_device_id)
667 struct sr_device_instance *sdi;
669 struct sr_datafeed_packet packet;
670 struct sr_datafeed_header header;
673 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
677 /* FIXME: No need to do full reconfigure every time */
678 // ret = mso_reset_fsm(sdi);
682 /* FIXME: ACDC Mode */
683 mso->ctlbase &= 0x7f;
684 // mso->ctlbase |= mso->acdcmode;
686 ret = mso_configure_rate(sdi, mso->cur_rate);
691 ret = mso_dac_out(sdi, mso->dac_offset);
695 ret = mso_configure_threshold_level(sdi);
699 ret = mso_configure_trigger(sdi);
703 /* FIXME: trigger_position */
706 /* END of config hardware part */
713 /* without trigger */
714 // ret = mso_force_capture(sdi);
718 mso_check_trigger(sdi, &mso->trigger_state);
719 ret = mso_check_trigger(sdi, NULL);
723 mso->session_id = session_device_id;
724 sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi);
726 packet.type = SR_DF_HEADER;
727 packet.length = sizeof(struct sr_datafeed_header);
728 packet.payload = (unsigned char *) &header;
729 header.feed_version = 1;
730 gettimeofday(&header.starttime, NULL);
731 header.samplerate = mso->cur_rate;
732 header.num_analog_probes = 1;
733 header.num_logic_probes = 8;
734 header.protocol_id = SR_PROTO_RAW;
735 sr_session_bus(session_device_id, &packet);
741 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
743 struct sr_datafeed_packet packet;
745 device_index = device_index;
747 packet.type = SR_DF_END;
748 sr_session_bus(session_device_id, &packet);
751 struct sr_device_plugin link_mso19_plugin_info = {
752 .name = "link-mso19",
753 .longname = "Link Instruments MSO-19",
756 .cleanup = hw_cleanup,
758 .close = hw_closedev,
759 .get_device_info = hw_get_device_info,
760 .get_status = hw_get_status,
761 .get_capabilities = hw_get_capabilities,
762 .set_configuration = hw_set_configuration,
763 .start_acquisition = hw_start_acquisition,
764 .stop_acquisition = hw_stop_acquisition,