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[] = {
73 static struct sr_samplerates samplerates = {
77 supported_samplerates,
80 static GSList *device_instances = NULL;
82 static int mso_send_control_message(struct sr_device_instance *sdi,
83 uint16_t payload[], int n)
85 int fd = sdi->serial->fd;
86 int i, w, ret, s = n * 2 + sizeof(mso_head) + sizeof(mso_foot);
97 memcpy(p, mso_head, sizeof(mso_head));
98 p += sizeof(mso_head);
100 for (i = 0; i < n; i++) {
101 *(uint16_t *) p = htons(payload[i]);
104 memcpy(p, mso_foot, sizeof(mso_foot));
108 ret = serial_write(fd, buf + w, s - w);
122 static int mso_reset_adc(struct sr_device_instance *sdi)
124 struct mso *mso = sdi->priv;
127 ops[0] = mso_trans(REG_CTL, (mso->ctlbase | BIT_CTL_RESETADC));
128 ops[1] = mso_trans(REG_CTL, mso->ctlbase);
129 mso->ctlbase |= BIT_CTL_ADC_UNKNOWN4;
131 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
134 static int mso_reset_fsm(struct sr_device_instance *sdi)
136 struct mso *mso = sdi->priv;
139 mso->ctlbase |= BIT_CTL_RESETFSM;
140 ops[0] = mso_trans(REG_CTL, mso->ctlbase);
142 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
145 static int mso_toggle_led(struct sr_device_instance *sdi, int state)
147 struct mso *mso = sdi->priv;
150 mso->ctlbase &= BIT_CTL_LED;
152 mso->ctlbase |= BIT_CTL_LED;
153 ops[0] = mso_trans(REG_CTL, mso->ctlbase);
155 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
158 static int mso_check_trigger(struct sr_device_instance *sdi,
161 uint16_t ops[] = { mso_trans(REG_TRIGGER, 0) };
165 ret = mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
166 if (info == NULL || ret != SR_OK)
170 if (serial_read(sdi->serial->fd, buf, 1) != 1) /* FIXME: Need timeout */
177 static int mso_read_buffer(struct sr_device_instance *sdi)
179 uint16_t ops[] = { mso_trans(REG_BUFFER, 0) };
181 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
184 static int mso_arm(struct sr_device_instance *sdi)
186 struct mso *mso = sdi->priv;
188 mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_RESETFSM),
189 mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_ARM),
190 mso_trans(REG_CTL, mso->ctlbase),
193 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
196 static int mso_force_capture(struct sr_device_instance *sdi)
198 struct mso *mso = sdi->priv;
200 mso_trans(REG_CTL, mso->ctlbase | 8),
201 mso_trans(REG_CTL, mso->ctlbase),
204 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
207 static int mso_dac_out(struct sr_device_instance *sdi, uint16_t val)
209 struct mso *mso = sdi->priv;
211 mso_trans(REG_DAC1, (val >> 8) & 0xff),
212 mso_trans(REG_DAC2, val & 0xff),
213 mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_RESETADC),
216 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
219 static int mso_clkrate_out(struct sr_device_instance *sdi, uint16_t val)
222 mso_trans(REG_CLKRATE1, (val >> 8) & 0xff),
223 mso_trans(REG_CLKRATE2, val & 0xff),
226 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
229 static int mso_configure_rate(struct sr_device_instance *sdi,
232 struct mso *mso = sdi->priv;
236 for (i = 0; i < ARRAY_SIZE(rate_map); i++) {
237 if (rate_map[i].rate == rate) {
238 mso->slowmode = rate_map[i].slowmode;
239 ret = mso_clkrate_out(sdi, rate_map[i].val);
241 mso->cur_rate = rate;
248 static inline uint16_t mso_calc_raw_from_mv(struct mso *mso)
250 return (uint16_t) (0x200 -
251 ((mso->dso_trigger_voltage / mso->dso_probe_attn) /
255 static int mso_configure_trigger(struct sr_device_instance *sdi)
257 struct mso *mso = sdi->priv;
259 uint16_t dso_trigger = mso_calc_raw_from_mv(mso);
261 dso_trigger &= 0x3ff;
262 if ((!mso->trigger_slope && mso->trigger_chan == 1) ||
263 (mso->trigger_slope &&
264 (mso->trigger_chan == 0 ||
265 mso->trigger_chan == 2 ||
266 mso->trigger_chan == 3)))
267 dso_trigger |= 0x400;
269 switch (mso->trigger_chan) {
271 dso_trigger |= 0xe000;
273 dso_trigger |= 0x4000;
276 dso_trigger |= 0x2000;
279 dso_trigger |= 0xa000;
282 dso_trigger |= 0x8000;
289 switch (mso->trigger_outsrc) {
291 dso_trigger |= 0x800;
294 dso_trigger |= 0x1000;
297 dso_trigger |= 0x1800;
302 ops[0] = mso_trans(5, mso->la_trigger);
303 ops[1] = mso_trans(6, mso->la_trigger_mask);
304 ops[2] = mso_trans(3, dso_trigger & 0xff);
305 ops[3] = mso_trans(4, (dso_trigger >> 8) & 0xff);
306 ops[4] = mso_trans(11,
307 mso->dso_trigger_width / SR_HZ_TO_NS(mso->cur_rate));
308 ops[5] = mso_trans(15, (2 | mso->slowmode));
310 /* FIXME SPI/I2C Triggers */
311 ops[6] = mso_trans(0, 0);
312 ops[7] = mso_trans(1, 0);
313 ops[8] = mso_trans(2, 0);
314 ops[9] = mso_trans(3, 0);
315 ops[10] = mso_trans(4, 0xff);
316 ops[11] = mso_trans(5, 0xff);
317 ops[12] = mso_trans(6, 0xff);
318 ops[13] = mso_trans(7, 0xff);
319 ops[14] = mso_trans(8, mso->trigger_spimode);
320 ops[15] = mso_trans(15, mso->slowmode);
322 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
325 static int mso_configure_threshold_level(struct sr_device_instance *sdi)
327 struct mso *mso = sdi->priv;
329 return mso_dac_out(sdi, la_threshold_map[mso->la_threshold]);
332 static int mso_parse_serial(const char *iSerial, const char *iProduct,
335 unsigned int u1, u2, u3, u4, u5, u6;
338 /* FIXME: This code is in the original app, but I think its
339 * used only for the GUI */
340 /* if (strstr(iProduct, "REV_02") || strstr(iProduct, "REV_03"))
341 mso->num_sample_rates = 0x16;
343 mso->num_sample_rates = 0x10; */
346 if (iSerial[0] != '4' || sscanf(iSerial, "%5u%3u%3u%1u%1u%6u",
347 &u1, &u2, &u3, &u4, &u5, &u6) != 6)
352 mso->vbit = u1 / 10000;
355 mso->dac_offset = u2;
356 if (mso->dac_offset == 0)
357 mso->dac_offset = 0x1ff;
358 mso->offset_range = u3;
359 if (mso->offset_range == 0)
360 mso->offset_range = 0x17d;
363 * FIXME: There is more code on the original software to handle
364 * bigger iSerial strings, but as I can't test on my device
365 * I will not implement it yet
371 static int hw_init(const char *deviceinfo)
373 struct sr_device_instance *sdi;
376 struct udev_enumerate *enumerate;
377 struct udev_list_entry *devices, *dev_list_entry;
380 deviceinfo = deviceinfo;
382 /* It's easier to map usb<->serial using udev */
384 * FIXME: On windows we can get the same information from the
385 * registry, add an #ifdef here later
389 sr_warn("Failed to initialize udev.");
392 enumerate = udev_enumerate_new(udev);
393 udev_enumerate_add_match_subsystem(enumerate, "usb-serial");
394 udev_enumerate_scan_devices(enumerate);
395 devices = udev_enumerate_get_list_entry(enumerate);
396 udev_list_entry_foreach(dev_list_entry, devices) {
397 const char *syspath, *sysname, *idVendor, *idProduct,
399 char path[32], manufacturer[32], product[32], hwrev[32];
400 struct udev_device *dev, *parent;
403 syspath = udev_list_entry_get_name(dev_list_entry);
404 dev = udev_device_new_from_syspath(udev, syspath);
405 sysname = udev_device_get_sysname(dev);
406 parent = udev_device_get_parent_with_subsystem_devtype(
407 dev, "usb", "usb_device");
409 sr_warn("Unable to find parent usb device for %s",
414 idVendor = udev_device_get_sysattr_value(parent, "idVendor");
415 idProduct = udev_device_get_sysattr_value(parent, "idProduct");
416 if (strcmp(USB_VENDOR, idVendor)
417 || strcmp(USB_PRODUCT, idProduct))
420 iSerial = udev_device_get_sysattr_value(parent, "serial");
421 iProduct = udev_device_get_sysattr_value(parent, "product");
423 snprintf(path, sizeof(path), "/dev/%s", sysname);
425 s = strcspn(iProduct, " ");
426 if (s > sizeof(product) ||
427 strlen(iProduct) - s > sizeof(manufacturer)) {
428 sr_warn("Could not parse iProduct: %s", iProduct);
431 strncpy(product, iProduct, s);
433 strcpy(manufacturer, iProduct + s);
434 sprintf(hwrev, "r%d", mso->hwrev);
436 mso = malloc(sizeof(struct mso));
439 memset(mso, 0, sizeof(struct mso));
441 if (mso_parse_serial(iSerial, iProduct, mso) != SR_OK) {
442 sr_warn("Invalid iSerial: %s", iSerial);
445 /* hardware initial state */
448 sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING,
449 manufacturer, product, hwrev);
451 sr_warn("Unable to create device instance for %s",
456 /* save a pointer to our private instance data */
459 sdi->serial = sr_serial_device_instance_new(path, -1);
461 goto err_device_instance_free;
463 device_instances = g_slist_append(device_instances, sdi);
467 err_device_instance_free:
468 sr_device_instance_free(sdi);
473 udev_enumerate_unref(enumerate);
480 static void hw_cleanup(void)
483 struct sr_device_instance *sdi;
485 /* Properly close all devices. */
486 for (l = device_instances; l; l = l->next) {
488 if (sdi->serial->fd != -1)
489 serial_close(sdi->serial->fd);
490 if (sdi->priv != NULL)
492 sr_device_instance_free(sdi);
494 g_slist_free(device_instances);
495 device_instances = NULL;
498 static int hw_opendev(int device_index)
500 struct sr_device_instance *sdi;
504 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
508 sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
509 if (sdi->serial->fd == -1)
512 ret = serial_set_params(sdi->serial->fd, 460800, 8, 0, 1, 2);
516 sdi->status = SR_ST_ACTIVE;
518 /* FIXME: discard serial buffer */
520 mso_check_trigger(sdi, &mso->trigger_state);
521 // sr_warn("trigger state: %c", mso->trigger_state);
523 ret = mso_reset_adc(sdi);
527 mso_check_trigger(sdi, &mso->trigger_state);
528 // sr_warn("trigger state: %c", mso->trigger_state);
530 // ret = mso_reset_fsm(sdi);
538 static void hw_closedev(int device_index)
540 struct sr_device_instance *sdi;
542 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
545 if (sdi->serial->fd != -1) {
546 serial_close(sdi->serial->fd);
547 sdi->serial->fd = -1;
548 sdi->status = SR_ST_INACTIVE;
552 static void *hw_get_device_info(int device_index, int device_info_id)
554 struct sr_device_instance *sdi;
558 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
562 switch (device_info_id) {
566 case SR_DI_NUM_PROBES: /* FIXME: How to report analog probe? */
567 info = GINT_TO_POINTER(8);
569 case SR_DI_SAMPLERATES:
572 case SR_DI_TRIGGER_TYPES:
573 info = "01"; /* FIXME */
575 case SR_DI_CUR_SAMPLERATE:
576 info = &mso->cur_rate;
582 static int hw_get_status(int device_index)
584 struct sr_device_instance *sdi;
586 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
587 return SR_ST_NOT_FOUND;
592 static int *hw_get_capabilities(void)
597 static int hw_set_configuration(int device_index, int capability, void *value)
599 struct sr_device_instance *sdi;
601 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
604 switch (capability) {
605 case SR_HWCAP_SAMPLERATE:
606 return mso_configure_rate(sdi, *(uint64_t *) value);
607 case SR_HWCAP_PROBECONFIG:
608 case SR_HWCAP_LIMIT_SAMPLES:
610 return SR_OK; /* FIXME */
614 #define MSO_TRIGGER_UNKNOWN '!'
615 #define MSO_TRIGGER_UNKNOWN1 '1'
616 #define MSO_TRIGGER_UNKNOWN2 '2'
617 #define MSO_TRIGGER_UNKNOWN3 '3'
618 #define MSO_TRIGGER_WAIT '4'
619 #define MSO_TRIGGER_FIRED '5'
620 #define MSO_TRIGGER_DATAREADY '6'
622 /* FIXME: Pass errors? */
623 static int receive_data(int fd, int revents, void *user_data)
625 struct sr_device_instance *sdi = user_data;
626 struct mso *mso = sdi->priv;
627 struct sr_datafeed_packet packet;
628 uint8_t in[1024], logic_out[1024];
629 double analog_out[1024];
634 s = serial_read(fd, in, sizeof(in));
639 if (mso->trigger_state != MSO_TRIGGER_DATAREADY) {
640 mso->trigger_state = in[0];
641 if (mso->trigger_state == MSO_TRIGGER_DATAREADY) {
642 mso_read_buffer(sdi);
645 mso_check_trigger(sdi, NULL);
650 /* the hardware always dumps 1024 samples, 24bits each */
651 if (mso->buffer_n < 3072) {
652 memcpy(mso->buffer + mso->buffer_n, in, s);
655 if (mso->buffer_n < 3072)
658 /* do the conversion */
659 for (i = 0; i < 1024; i++) {
660 /* FIXME: Need to do conversion to mV */
661 analog_out[i] = (mso->buffer[i * 3] & 0x3f) |
662 ((mso->buffer[i * 3 + 1] & 0xf) << 6);
663 logic_out[i] = ((mso->buffer[i * 3 + 1] & 0x30) >> 4) |
664 ((mso->buffer[i * 3 + 2] & 0x3f) << 2);
667 packet.type = SR_DF_LOGIC;
668 packet.length = 1024;
670 packet.payload = logic_out;
671 sr_session_bus(mso->session_id, &packet);
674 packet.type = SR_DF_ANALOG;
675 packet.length = 1024;
676 packet.unitsize = sizeof(double);
677 packet.payload = analog_out;
678 sr_session_bus(mso->session_id, &packet);
680 packet.type = SR_DF_END;
681 sr_session_bus(mso->session_id, &packet);
686 static int hw_start_acquisition(int device_index, gpointer session_device_id)
688 struct sr_device_instance *sdi;
690 struct sr_datafeed_packet packet;
691 struct sr_datafeed_header header;
694 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
698 /* FIXME: No need to do full reconfigure every time */
699 // ret = mso_reset_fsm(sdi);
703 /* FIXME: ACDC Mode */
704 mso->ctlbase &= 0x7f;
705 // mso->ctlbase |= mso->acdcmode;
707 ret = mso_configure_rate(sdi, mso->cur_rate);
712 ret = mso_dac_out(sdi, mso->dac_offset);
716 ret = mso_configure_threshold_level(sdi);
720 ret = mso_configure_trigger(sdi);
724 /* FIXME: trigger_position */
727 /* END of config hardware part */
734 /* without trigger */
735 // ret = mso_force_capture(sdi);
739 mso_check_trigger(sdi, &mso->trigger_state);
740 ret = mso_check_trigger(sdi, NULL);
744 mso->session_id = session_device_id;
745 sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi);
747 packet.type = SR_DF_HEADER;
748 packet.length = sizeof(struct sr_datafeed_header);
749 packet.payload = (unsigned char *) &header;
750 header.feed_version = 1;
751 gettimeofday(&header.starttime, NULL);
752 header.samplerate = mso->cur_rate;
753 header.num_analog_probes = 1;
754 header.num_logic_probes = 8;
755 header.protocol_id = SR_PROTO_RAW;
756 sr_session_bus(session_device_id, &packet);
762 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
764 struct sr_datafeed_packet packet;
766 device_index = device_index;
768 packet.type = SR_DF_END;
769 sr_session_bus(session_device_id, &packet);
772 struct sr_device_plugin link_mso19_plugin_info = {
773 .name = "link-mso19",
774 .longname = "Link Instruments MSO-19",
777 .cleanup = hw_cleanup,
779 .close = hw_closedev,
780 .get_device_info = hw_get_device_info,
781 .get_status = hw_get_status,
782 .get_capabilities = hw_get_capabilities,
783 .set_configuration = hw_set_configuration,
784 .start_acquisition = hw_start_acquisition,
785 .stop_acquisition = hw_stop_acquisition,