30 #include <arpa/inet.h>
32 #include "sigrok-internal.h"
33 #include "link-mso19.h"
35 #define USB_VENDOR "3195"
36 #define USB_PRODUCT "f190"
40 static int hwcaps[] = {
56 static const char *probe_names[
NUM_PROBES + 1] = {
68 static uint64_t supported_samplerates[] = {
96 supported_samplerates,
99 static GSList *dev_insts = NULL;
101 static int mso_send_control_message(
struct sr_dev_inst *sdi,
102 uint16_t payload[],
int n)
104 int fd = sdi->serial->fd;
105 int i, w, ret, s = n * 2 +
sizeof(mso_head) +
sizeof(mso_foot);
113 if (!(buf = g_try_malloc(s))) {
114 sr_err(
"mso19: %s: buf malloc failed", __func__);
120 memcpy(p, mso_head,
sizeof(mso_head));
121 p +=
sizeof(mso_head);
123 for (i = 0; i < n; i++) {
124 *(uint16_t *) p = htons(payload[i]);
127 memcpy(p, mso_foot,
sizeof(mso_foot));
150 ops[0] = mso_trans(REG_CTL1, (ctx->ctlbase1 | BIT_CTL1_RESETADC));
151 ops[1] = mso_trans(REG_CTL1, ctx->ctlbase1);
152 ctx->ctlbase1 |= BIT_CTL1_ADC_UNKNOWN4;
154 sr_dbg(
"mso19: Requesting ADC reset");
155 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
163 ctx->ctlbase1 |= BIT_CTL1_RESETFSM;
164 ops[0] = mso_trans(REG_CTL1, ctx->ctlbase1);
166 sr_dbg(
"mso19: Requesting ADC reset");
167 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
175 ctx->ctlbase1 &= ~BIT_CTL1_LED;
177 ctx->ctlbase1 |= BIT_CTL1_LED;
178 ops[0] = mso_trans(REG_CTL1, ctx->ctlbase1);
180 sr_dbg(
"mso19: Requesting LED toggle");
181 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
184 static int mso_check_trigger(
struct sr_dev_inst *sdi, uint8_t *info)
186 uint16_t ops[] = { mso_trans(REG_TRIGGER, 0) };
190 sr_dbg(
"mso19: Requesting trigger state");
191 ret = mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
192 if (info == NULL || ret !=
SR_OK)
200 sr_dbg(
"mso19: Trigger state is: 0x%x", *info);
204 static int mso_read_buffer(
struct sr_dev_inst *sdi)
206 uint16_t ops[] = { mso_trans(REG_BUFFER, 0) };
208 sr_dbg(
"mso19: Requesting buffer dump");
209 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
216 mso_trans(REG_CTL1, ctx->ctlbase1 | BIT_CTL1_RESETFSM),
217 mso_trans(REG_CTL1, ctx->ctlbase1 | BIT_CTL1_ARM),
218 mso_trans(REG_CTL1, ctx->ctlbase1),
221 sr_dbg(
"mso19: Requesting trigger arm");
222 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
225 static int mso_force_capture(
struct sr_dev_inst *sdi)
229 mso_trans(REG_CTL1, ctx->ctlbase1 | 8),
230 mso_trans(REG_CTL1, ctx->ctlbase1),
233 sr_dbg(
"mso19: Requesting forced capture");
234 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
237 static int mso_dac_out(
struct sr_dev_inst *sdi, uint16_t val)
241 mso_trans(REG_DAC1, (val >> 8) & 0xff),
242 mso_trans(REG_DAC2, val & 0xff),
243 mso_trans(REG_CTL1, ctx->ctlbase1 | BIT_CTL1_RESETADC),
246 sr_dbg(
"mso19: Setting dac word to 0x%x", val);
247 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
250 static int mso_clkrate_out(
struct sr_dev_inst *sdi, uint16_t val)
253 mso_trans(REG_CLKRATE1, (val >> 8) & 0xff),
254 mso_trans(REG_CLKRATE2, val & 0xff),
257 sr_dbg(
"mso19: Setting clkrate word to 0x%x", val);
258 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
261 static int mso_configure_rate(
struct sr_dev_inst *sdi, uint32_t rate)
267 for (i = 0; i < ARRAY_SIZE(rate_map); i++) {
268 if (rate_map[i].rate == rate) {
269 ctx->ctlbase2 = rate_map[i].slowmode;
270 ret = mso_clkrate_out(sdi, rate_map[i].val);
279 static inline uint16_t mso_calc_raw_from_mv(
struct context *ctx)
281 return (uint16_t) (0x200 -
282 ((ctx->dso_trigger_voltage / ctx->dso_probe_attn) /
286 static int mso_configure_trigger(
struct sr_dev_inst *sdi)
290 uint16_t dso_trigger = mso_calc_raw_from_mv(ctx);
292 dso_trigger &= 0x3ff;
293 if ((!ctx->trigger_slope && ctx->trigger_chan == 1) ||
294 (ctx->trigger_slope &&
295 (ctx->trigger_chan == 0 ||
296 ctx->trigger_chan == 2 ||
297 ctx->trigger_chan == 3)))
298 dso_trigger |= 0x400;
300 switch (ctx->trigger_chan) {
302 dso_trigger |= 0xe000;
304 dso_trigger |= 0x4000;
307 dso_trigger |= 0x2000;
310 dso_trigger |= 0xa000;
313 dso_trigger |= 0x8000;
320 switch (ctx->trigger_outsrc) {
322 dso_trigger |= 0x800;
325 dso_trigger |= 0x1000;
328 dso_trigger |= 0x1800;
333 ops[0] = mso_trans(5, ctx->la_trigger);
334 ops[1] = mso_trans(6, ctx->la_trigger_mask);
335 ops[2] = mso_trans(3, dso_trigger & 0xff);
336 ops[3] = mso_trans(4, (dso_trigger >> 8) & 0xff);
337 ops[4] = mso_trans(11,
341 ops[5] = mso_trans(REG_CTL2, (ctx->ctlbase2 | BITS_CTL2_BANK(2)));
343 ops[6] = mso_trans(REG_PT_WORD(0), ctx->protocol_trigger.word[0]);
344 ops[7] = mso_trans(REG_PT_WORD(1), ctx->protocol_trigger.word[1]);
345 ops[8] = mso_trans(REG_PT_WORD(2), ctx->protocol_trigger.word[2]);
346 ops[9] = mso_trans(REG_PT_WORD(3), ctx->protocol_trigger.word[3]);
347 ops[10] = mso_trans(REG_PT_MASK(0), ctx->protocol_trigger.mask[0]);
348 ops[11] = mso_trans(REG_PT_MASK(1), ctx->protocol_trigger.mask[1]);
349 ops[12] = mso_trans(REG_PT_MASK(2), ctx->protocol_trigger.mask[2]);
350 ops[13] = mso_trans(REG_PT_MASK(3), ctx->protocol_trigger.mask[3]);
351 ops[14] = mso_trans(REG_PT_SPIMODE, ctx->protocol_trigger.spimode);
353 ops[15] = mso_trans(REG_CTL2, ctx->ctlbase2);
355 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
358 static int mso_configure_threshold_level(
struct sr_dev_inst *sdi)
362 return mso_dac_out(sdi, la_threshold_map[ctx->la_threshold]);
365 static int mso_parse_serial(
const char *iSerial,
const char *iProduct,
368 unsigned int u1, u2, u3, u4, u5, u6;
379 if (iSerial[0] !=
'4' || sscanf(iSerial,
"%5u%3u%3u%1u%1u%6u",
380 &u1, &u2, &u3, &u4, &u5, &u6) != 6)
385 ctx->vbit = u1 / 10000;
388 ctx->dac_offset = u2;
389 if (ctx->dac_offset == 0)
390 ctx->dac_offset = 0x1ff;
391 ctx->offset_range = u3;
392 if (ctx->offset_range == 0)
393 ctx->offset_range = 0x17d;
404 static int hw_init(
const char *devinfo)
409 struct udev_enumerate *enumerate;
410 struct udev_list_entry *devs, *dev_list_entry;
422 sr_err(
"mso19: Failed to initialize udev.");
425 enumerate = udev_enumerate_new(udev);
426 udev_enumerate_add_match_subsystem(enumerate,
"usb-serial");
427 udev_enumerate_scan_devices(enumerate);
428 devs = udev_enumerate_get_list_entry(enumerate);
429 udev_list_entry_foreach(dev_list_entry, devs) {
430 const char *syspath, *sysname, *idVendor, *idProduct,
432 char path[32], manufacturer[32], product[32], hwrev[32];
433 struct udev_device *dev, *parent;
436 syspath = udev_list_entry_get_name(dev_list_entry);
437 dev = udev_device_new_from_syspath(udev, syspath);
438 sysname = udev_device_get_sysname(dev);
439 parent = udev_device_get_parent_with_subsystem_devtype(
440 dev,
"usb",
"usb_device");
442 sr_err(
"mso19: Unable to find parent usb device for %s",
447 idVendor = udev_device_get_sysattr_value(parent,
"idVendor");
448 idProduct = udev_device_get_sysattr_value(parent,
"idProduct");
453 iSerial = udev_device_get_sysattr_value(parent,
"serial");
454 iProduct = udev_device_get_sysattr_value(parent,
"product");
456 snprintf(path,
sizeof(path),
"/dev/%s", sysname);
458 s = strcspn(iProduct,
" ");
459 if (s >
sizeof(product) ||
460 strlen(iProduct) - s >
sizeof(manufacturer)) {
461 sr_err(
"mso19: Could not parse iProduct: %s", iProduct);
464 strncpy(product, iProduct, s);
466 strcpy(manufacturer, iProduct + s);
468 if (!(ctx = g_try_malloc0(
sizeof(
struct context)))) {
469 sr_err(
"mso19: %s: ctx malloc failed", __func__);
473 if (mso_parse_serial(iSerial, iProduct, ctx) !=
SR_OK) {
474 sr_err(
"mso19: Invalid iSerial: %s", iSerial);
477 sprintf(hwrev,
"r%d", ctx->hwrev);
484 for (i = 0; i < 4; i++) {
485 ctx->protocol_trigger.word[i] = 0;
486 ctx->protocol_trigger.mask[i] = 0xff;
488 ctx->protocol_trigger.spimode = 0;
492 manufacturer, product, hwrev);
494 sr_err(
"mso19: Unable to create device instance for %s",
504 goto err_dev_inst_free;
506 dev_insts = g_slist_append(dev_insts, sdi);
516 udev_enumerate_unref(enumerate);
523 static int hw_cleanup(
void)
531 for (l = dev_insts; l; l = l->next) {
532 if (!(sdi = l->data)) {
534 sr_err(
"mso19: %s: sdi was NULL, continuing", __func__);
538 if (sdi->serial->fd != -1)
542 g_slist_free(dev_insts);
548 static int hw_dev_open(
int dev_index)
558 sdi->serial->fd =
serial_open(sdi->serial->port, O_RDWR);
559 if (sdi->serial->fd == -1)
570 mso_check_trigger(sdi, &ctx->trigger_state);
571 sr_dbg(
"mso19: trigger state: 0x%x", ctx->trigger_state);
573 ret = mso_reset_adc(sdi);
577 mso_check_trigger(sdi, &ctx->trigger_state);
578 sr_dbg(
"mso19: trigger state: 0x%x", ctx->trigger_state);
584 sr_dbg(
"mso19: Finished %s", __func__);
590 static int hw_dev_close(
int dev_index)
595 sr_err(
"mso19: %s: sdi was NULL", __func__);
600 if (sdi->serial->fd != -1) {
602 sdi->serial->fd = -1;
606 sr_dbg(
"mso19: finished %s", __func__);
610 static void *hw_dev_info_get(
int dev_index,
int dev_info_id)
620 switch (dev_info_id) {
643 static int hw_dev_status_get(
int dev_index)
653 static int *hw_hwcap_get_all(
void)
658 static int hw_dev_config_set(
int dev_index,
int hwcap,
void *value)
667 return mso_configure_rate(sdi, *(uint64_t *) value);
675 #define MSO_TRIGGER_UNKNOWN '!'
676 #define MSO_TRIGGER_UNKNOWN1 '1'
677 #define MSO_TRIGGER_UNKNOWN2 '2'
678 #define MSO_TRIGGER_UNKNOWN3 '3'
679 #define MSO_TRIGGER_WAIT '4'
680 #define MSO_TRIGGER_FIRED '5'
681 #define MSO_TRIGGER_DATAREADY '6'
684 static int receive_data(
int fd,
int revents,
void *cb_data)
690 uint8_t in[1024], logic_out[1024];
691 double analog_out[1024];
703 ctx->trigger_state = in[0];
705 mso_read_buffer(sdi);
708 mso_check_trigger(sdi, NULL);
714 if (ctx->buffer_n < 3072) {
715 memcpy(ctx->buffer + ctx->buffer_n, in, s);
718 if (ctx->buffer_n < 3072)
722 for (i = 0; i < 1024; i++) {
724 analog_out[i] = (ctx->buffer[i * 3] & 0x3f) |
725 ((ctx->buffer[i * 3 + 1] & 0xf) << 6);
726 logic_out[i] = ((ctx->buffer[i * 3 + 1] & 0x30) >> 4) |
727 ((ctx->buffer[i * 3 + 2] & 0x3f) << 2);
734 logic.
data = logic_out;
752 static int hw_dev_acquisition_start(
int dev_index,
void *cb_data)
770 ctx->ctlbase1 &= 0x7f;
773 ret = mso_configure_rate(sdi, ctx->
cur_rate);
778 ret = mso_dac_out(sdi, ctx->dac_offset);
782 ret = mso_configure_threshold_level(sdi);
786 ret = mso_configure_trigger(sdi);
805 mso_check_trigger(sdi, &ctx->trigger_state);
806 ret = mso_check_trigger(sdi, NULL);
811 sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi);
814 packet.payload = (
unsigned char *) &header;
815 header.feed_version = 1;
816 gettimeofday(&header.starttime, NULL);
819 header.num_logic_probes = 8;
826 static int hw_dev_acquisition_stop(
int dev_index,
void *cb_data)
840 .
name =
"link-mso19",
841 .longname =
"Link Instruments MSO-19",
844 .cleanup = hw_cleanup,
845 .dev_open = hw_dev_open,
846 .dev_close = hw_dev_close,
847 .dev_info_get = hw_dev_info_get,
848 .dev_status_get = hw_dev_status_get,
849 .hwcap_get_all = hw_hwcap_get_all,
850 .dev_config_set = hw_dev_config_set,
851 .dev_acquisition_start = hw_dev_acquisition_start,
852 .dev_acquisition_stop = hw_dev_acquisition_stop,