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>
31 #include "libsigrok.h"
32 #include "libsigrok-internal.h"
33 #include "link-mso19.h"
35 #define USB_VENDOR "3195"
36 #define USB_PRODUCT "f190"
40 static const 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,
52 * Probes are numbered 0 to 7.
54 * See also: http://www.linkinstruments.com/images/mso19_1113.gif
56 static const char *probe_names[NUM_PROBES + 1] = {
57 "0", "1", "2", "3", "4", "5", "6", "7",
61 static const uint64_t supported_samplerates[] = {
85 static const struct sr_samplerates samplerates = {
89 supported_samplerates,
92 static GSList *dev_insts = NULL;
94 static int mso_send_control_message(struct sr_dev_inst *sdi,
95 uint16_t payload[], int n)
97 int fd = sdi->serial->fd;
98 int i, w, ret, s = n * 2 + sizeof(mso_head) + sizeof(mso_foot);
106 if (!(buf = g_try_malloc(s))) {
107 sr_err("Failed to malloc message buffer.");
113 memcpy(p, mso_head, sizeof(mso_head));
114 p += sizeof(mso_head);
116 for (i = 0; i < n; i++) {
117 *(uint16_t *) p = htons(payload[i]);
120 memcpy(p, mso_foot, sizeof(mso_foot));
124 ret = serial_write(fd, buf + w, s - w);
138 static int mso_reset_adc(struct sr_dev_inst *sdi)
140 struct context *ctx = sdi->priv;
143 ops[0] = mso_trans(REG_CTL1, (ctx->ctlbase1 | BIT_CTL1_RESETADC));
144 ops[1] = mso_trans(REG_CTL1, ctx->ctlbase1);
145 ctx->ctlbase1 |= BIT_CTL1_ADC_UNKNOWN4;
147 sr_dbg("Requesting ADC reset.");
148 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
151 static int mso_reset_fsm(struct sr_dev_inst *sdi)
153 struct context *ctx = sdi->priv;
156 ctx->ctlbase1 |= BIT_CTL1_RESETFSM;
157 ops[0] = mso_trans(REG_CTL1, ctx->ctlbase1);
159 sr_dbg("Requesting ADC reset.");
160 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
163 static int mso_toggle_led(struct sr_dev_inst *sdi, int state)
165 struct context *ctx = sdi->priv;
168 ctx->ctlbase1 &= ~BIT_CTL1_LED;
170 ctx->ctlbase1 |= BIT_CTL1_LED;
171 ops[0] = mso_trans(REG_CTL1, ctx->ctlbase1);
173 sr_dbg("Requesting LED toggle.");
174 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
177 static int mso_check_trigger(struct sr_dev_inst *sdi, uint8_t *info)
179 uint16_t ops[] = { mso_trans(REG_TRIGGER, 0) };
183 sr_dbg("Requesting trigger state.");
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 */
193 sr_dbg("Trigger state is: 0x%x.", *info);
197 static int mso_read_buffer(struct sr_dev_inst *sdi)
199 uint16_t ops[] = { mso_trans(REG_BUFFER, 0) };
201 sr_dbg("Requesting buffer dump.");
202 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
205 static int mso_arm(struct sr_dev_inst *sdi)
207 struct context *ctx = sdi->priv;
209 mso_trans(REG_CTL1, ctx->ctlbase1 | BIT_CTL1_RESETFSM),
210 mso_trans(REG_CTL1, ctx->ctlbase1 | BIT_CTL1_ARM),
211 mso_trans(REG_CTL1, ctx->ctlbase1),
214 sr_dbg("Requesting trigger arm.");
215 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
218 static int mso_force_capture(struct sr_dev_inst *sdi)
220 struct context *ctx = sdi->priv;
222 mso_trans(REG_CTL1, ctx->ctlbase1 | 8),
223 mso_trans(REG_CTL1, ctx->ctlbase1),
226 sr_dbg("Requesting forced capture.");
227 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
230 static int mso_dac_out(struct sr_dev_inst *sdi, uint16_t val)
232 struct context *ctx = sdi->priv;
234 mso_trans(REG_DAC1, (val >> 8) & 0xff),
235 mso_trans(REG_DAC2, val & 0xff),
236 mso_trans(REG_CTL1, ctx->ctlbase1 | BIT_CTL1_RESETADC),
239 sr_dbg("Setting dac word to 0x%x.", val);
240 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
243 static int mso_clkrate_out(struct sr_dev_inst *sdi, uint16_t val)
246 mso_trans(REG_CLKRATE1, (val >> 8) & 0xff),
247 mso_trans(REG_CLKRATE2, val & 0xff),
250 sr_dbg("Setting clkrate word to 0x%x.", val);
251 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
254 static int mso_configure_rate(struct sr_dev_inst *sdi, uint32_t rate)
256 struct context *ctx = sdi->priv;
260 for (i = 0; i < ARRAY_SIZE(rate_map); i++) {
261 if (rate_map[i].rate == rate) {
262 ctx->ctlbase2 = rate_map[i].slowmode;
263 ret = mso_clkrate_out(sdi, rate_map[i].val);
265 ctx->cur_rate = rate;
272 static inline uint16_t mso_calc_raw_from_mv(struct context *ctx)
274 return (uint16_t) (0x200 -
275 ((ctx->dso_trigger_voltage / ctx->dso_probe_attn) /
279 static int mso_configure_trigger(struct sr_dev_inst *sdi)
281 struct context *ctx = sdi->priv;
283 uint16_t dso_trigger = mso_calc_raw_from_mv(ctx);
285 dso_trigger &= 0x3ff;
286 if ((!ctx->trigger_slope && ctx->trigger_chan == 1) ||
287 (ctx->trigger_slope &&
288 (ctx->trigger_chan == 0 ||
289 ctx->trigger_chan == 2 ||
290 ctx->trigger_chan == 3)))
291 dso_trigger |= 0x400;
293 switch (ctx->trigger_chan) {
295 dso_trigger |= 0xe000;
297 dso_trigger |= 0x4000;
300 dso_trigger |= 0x2000;
303 dso_trigger |= 0xa000;
306 dso_trigger |= 0x8000;
313 switch (ctx->trigger_outsrc) {
315 dso_trigger |= 0x800;
318 dso_trigger |= 0x1000;
321 dso_trigger |= 0x1800;
326 ops[0] = mso_trans(5, ctx->la_trigger);
327 ops[1] = mso_trans(6, ctx->la_trigger_mask);
328 ops[2] = mso_trans(3, dso_trigger & 0xff);
329 ops[3] = mso_trans(4, (dso_trigger >> 8) & 0xff);
330 ops[4] = mso_trans(11,
331 ctx->dso_trigger_width / SR_HZ_TO_NS(ctx->cur_rate));
333 /* Select the SPI/I2C trigger config bank */
334 ops[5] = mso_trans(REG_CTL2, (ctx->ctlbase2 | BITS_CTL2_BANK(2)));
335 /* Configure the SPI/I2C protocol trigger */
336 ops[6] = mso_trans(REG_PT_WORD(0), ctx->protocol_trigger.word[0]);
337 ops[7] = mso_trans(REG_PT_WORD(1), ctx->protocol_trigger.word[1]);
338 ops[8] = mso_trans(REG_PT_WORD(2), ctx->protocol_trigger.word[2]);
339 ops[9] = mso_trans(REG_PT_WORD(3), ctx->protocol_trigger.word[3]);
340 ops[10] = mso_trans(REG_PT_MASK(0), ctx->protocol_trigger.mask[0]);
341 ops[11] = mso_trans(REG_PT_MASK(1), ctx->protocol_trigger.mask[1]);
342 ops[12] = mso_trans(REG_PT_MASK(2), ctx->protocol_trigger.mask[2]);
343 ops[13] = mso_trans(REG_PT_MASK(3), ctx->protocol_trigger.mask[3]);
344 ops[14] = mso_trans(REG_PT_SPIMODE, ctx->protocol_trigger.spimode);
345 /* Select the default config bank */
346 ops[15] = mso_trans(REG_CTL2, ctx->ctlbase2);
348 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
351 static int mso_configure_threshold_level(struct sr_dev_inst *sdi)
353 struct context *ctx = sdi->priv;
355 return mso_dac_out(sdi, la_threshold_map[ctx->la_threshold]);
358 static int mso_parse_serial(const char *iSerial, const char *iProduct,
361 unsigned int u1, u2, u3, u4, u5, u6;
364 /* FIXME: This code is in the original app, but I think its
365 * used only for the GUI */
366 /* if (strstr(iProduct, "REV_02") || strstr(iProduct, "REV_03"))
367 ctx->num_sample_rates = 0x16;
369 ctx->num_sample_rates = 0x10; */
372 if (iSerial[0] != '4' || sscanf(iSerial, "%5u%3u%3u%1u%1u%6u",
373 &u1, &u2, &u3, &u4, &u5, &u6) != 6)
378 ctx->vbit = u1 / 10000;
381 ctx->dac_offset = u2;
382 if (ctx->dac_offset == 0)
383 ctx->dac_offset = 0x1ff;
384 ctx->offset_range = u3;
385 if (ctx->offset_range == 0)
386 ctx->offset_range = 0x17d;
389 * FIXME: There is more code on the original software to handle
390 * bigger iSerial strings, but as I can't test on my device
391 * I will not implement it yet
397 static int hw_init(struct sr_context *sr_ctx)
404 static int hw_scan(void)
406 struct sr_dev_inst *sdi;
409 struct udev_enumerate *enumerate;
410 struct udev_list_entry *devs, *dev_list_entry;
413 /* It's easier to map usb<->serial using udev */
415 * FIXME: On windows we can get the same information from the
416 * registry, add an #ifdef here later
420 sr_err("Failed to initialize udev.");
423 enumerate = udev_enumerate_new(udev);
424 udev_enumerate_add_match_subsystem(enumerate, "usb-serial");
425 udev_enumerate_scan_devices(enumerate);
426 devs = udev_enumerate_get_list_entry(enumerate);
427 udev_list_entry_foreach(dev_list_entry, devs) {
428 const char *syspath, *sysname, *idVendor, *idProduct,
430 char path[32], manufacturer[32], product[32], hwrev[32];
431 struct udev_device *dev, *parent;
434 syspath = udev_list_entry_get_name(dev_list_entry);
435 dev = udev_device_new_from_syspath(udev, syspath);
436 sysname = udev_device_get_sysname(dev);
437 parent = udev_device_get_parent_with_subsystem_devtype(
438 dev, "usb", "usb_device");
440 sr_err("Unable to find parent usb device for %s",
445 idVendor = udev_device_get_sysattr_value(parent, "idVendor");
446 idProduct = udev_device_get_sysattr_value(parent, "idProduct");
447 if (strcmp(USB_VENDOR, idVendor)
448 || strcmp(USB_PRODUCT, idProduct))
451 iSerial = udev_device_get_sysattr_value(parent, "serial");
452 iProduct = udev_device_get_sysattr_value(parent, "product");
454 snprintf(path, sizeof(path), "/dev/%s", sysname);
456 s = strcspn(iProduct, " ");
457 if (s > sizeof(product) ||
458 strlen(iProduct) - s > sizeof(manufacturer)) {
459 sr_err("Could not parse iProduct: %s.", iProduct);
462 strncpy(product, iProduct, s);
464 strcpy(manufacturer, iProduct + s);
466 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
467 sr_err("Context malloc failed.");
468 continue; /* TODO: Errors handled correctly? */
471 if (mso_parse_serial(iSerial, iProduct, ctx) != SR_OK) {
472 sr_err("Invalid iSerial: %s.", iSerial);
475 sprintf(hwrev, "r%d", ctx->hwrev);
477 /* hardware initial state */
480 /* Initialize the protocol trigger configuration */
482 for (i = 0; i < 4; i++) {
483 ctx->protocol_trigger.word[i] = 0;
484 ctx->protocol_trigger.mask[i] = 0xff;
486 ctx->protocol_trigger.spimode = 0;
489 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
490 manufacturer, product, hwrev);
492 sr_err("Unable to create device instance for %s",
497 /* save a pointer to our private instance data */
500 sdi->serial = sr_serial_dev_inst_new(path, -1);
502 goto err_dev_inst_free;
504 dev_insts = g_slist_append(dev_insts, sdi);
509 sr_dev_inst_free(sdi);
514 udev_enumerate_unref(enumerate);
521 static int hw_cleanup(void)
524 struct sr_dev_inst *sdi;
528 /* Properly close all devices. */
529 for (l = dev_insts; l; l = l->next) {
530 if (!(sdi = l->data)) {
531 /* Log error, but continue cleaning up the rest. */
532 sr_err("%s: sdi was NULL, continuing", __func__);
536 if (sdi->serial->fd != -1)
537 serial_close(sdi->serial->fd);
538 sr_dev_inst_free(sdi);
540 g_slist_free(dev_insts);
546 static int hw_dev_open(int dev_index)
548 struct sr_dev_inst *sdi;
552 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
556 sdi->serial->fd = serial_open(sdi->serial->port, SERIAL_RDWR);
557 if (sdi->serial->fd == -1)
560 ret = serial_set_params(sdi->serial->fd, 460800, 8, 0, 1, 2);
564 sdi->status = SR_ST_ACTIVE;
566 /* FIXME: discard serial buffer */
568 mso_check_trigger(sdi, &ctx->trigger_state);
569 sr_dbg("Trigger state: 0x%x.", ctx->trigger_state);
571 ret = mso_reset_adc(sdi);
575 mso_check_trigger(sdi, &ctx->trigger_state);
576 sr_dbg("Trigger state: 0x%x.", ctx->trigger_state);
578 // ret = mso_reset_fsm(sdi);
586 static int hw_dev_close(int dev_index)
588 struct sr_dev_inst *sdi;
590 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
591 sr_err("%s: sdi was NULL", __func__);
596 if (sdi->serial->fd != -1) {
597 serial_close(sdi->serial->fd);
598 sdi->serial->fd = -1;
599 sdi->status = SR_ST_INACTIVE;
605 static const void *hw_dev_info_get(int dev_index, int dev_info_id)
607 struct sr_dev_inst *sdi;
609 const void *info = NULL;
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 = &ctx->cur_rate;
638 static int hw_dev_status_get(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 const int *hw_hwcap_get_all(void)
653 static int hw_dev_config_set(int dev_index, int hwcap, const 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, *(const 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 *cb_data)
681 struct sr_dev_inst *sdi = cb_data;
682 struct context *ctx = 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 (ctx->trigger_state != MSO_TRIGGER_DATAREADY) {
697 ctx->trigger_state = in[0];
698 if (ctx->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 (ctx->buffer_n < 3072) {
709 memcpy(ctx->buffer + ctx->buffer_n, in, s);
712 if (ctx->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] = (ctx->buffer[i * 3] & 0x3f) |
719 ((ctx->buffer[i * 3 + 1] & 0xf) << 6);
720 logic_out[i] = ((ctx->buffer[i * 3 + 1] & 0x30) >> 4) |
721 ((ctx->buffer[i * 3 + 2] & 0x3f) << 2);
724 packet.type = SR_DF_LOGIC;
725 packet.payload = &logic;
728 logic.data = logic_out;
729 sr_session_send(ctx->session_dev_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_send(ctx->session_dev_id, &packet);
740 packet.type = SR_DF_END;
741 sr_session_send(ctx->session_dev_id, &packet);
746 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
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 ctx->ctlbase1 &= 0x7f;
765 // ctx->ctlbase1 |= ctx->acdcmode;
767 ret = mso_configure_rate(sdi, ctx->cur_rate);
772 ret = mso_dac_out(sdi, ctx->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, &ctx->trigger_state);
800 ret = mso_check_trigger(sdi, NULL);
804 ctx->session_dev_id = cb_data;
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 = ctx->cur_rate;
812 // header.num_analog_probes = 1;
813 header.num_logic_probes = 8;
814 sr_session_send(ctx->session_dev_id, &packet);
819 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
820 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
822 struct sr_datafeed_packet packet;
826 packet.type = SR_DF_END;
827 sr_session_send(cb_data, &packet);
832 SR_PRIV struct sr_dev_driver link_mso19_driver_info = {
833 .name = "link-mso19",
834 .longname = "Link Instruments MSO-19",
837 .cleanup = hw_cleanup,
839 .dev_open = hw_dev_open,
840 .dev_close = hw_dev_close,
841 .dev_info_get = hw_dev_info_get,
842 .dev_status_get = hw_dev_status_get,
843 .hwcap_get_all = hw_hwcap_get_all,
844 .dev_config_set = hw_dev_config_set,
845 .dev_acquisition_start = hw_dev_acquisition_start,
846 .dev_acquisition_stop = hw_dev_acquisition_stop,