X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Flink-mso19%2Flink-mso19.c;h=138ffe970125de629fd56b528c716552fc9cd045;hb=5c64390e5ac8a0052d8b7eeb49c781d86027c814;hp=1c371cac06b2775c1ea025e982f669730b7c1d2b;hpb=b9c735a275512ce82da7f66275c7cb62eaf66b60;p=libsigrok.git diff --git a/hardware/link-mso19/link-mso19.c b/hardware/link-mso19/link-mso19.c index 1c371cac..138ffe97 100644 --- a/hardware/link-mso19/link-mso19.c +++ b/hardware/link-mso19/link-mso19.c @@ -2,6 +2,7 @@ * This file is part of the sigrok project. * * Copyright (C) 2011 Daniel Ribeiro + * Copyright (C) 2012 Renato Caldas * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,34 +27,68 @@ #include #include #include -#include #include -#include -#include "config.h" +#include "sigrok.h" +#include "sigrok-internal.h" #include "link-mso19.h" #define USB_VENDOR "3195" #define USB_PRODUCT "f190" +#define NUM_PROBES 8 + static int capabilities[] = { - HWCAP_LOGIC_ANALYZER, -// HWCAP_OSCILLOSCOPE, -// HWCAP_PAT_GENERATOR, + SR_HWCAP_LOGIC_ANALYZER, +// SR_HWCAP_OSCILLOSCOPE, +// SR_HWCAP_PAT_GENERATOR, - HWCAP_SAMPLERATE, -// HWCAP_CAPTURE_RATIO, - HWCAP_LIMIT_SAMPLES, + SR_HWCAP_SAMPLERATE, +// SR_HWCAP_CAPTURE_RATIO, + SR_HWCAP_LIMIT_SAMPLES, 0, }; +static const char *probe_names[NUM_PROBES + 1] = { + "0", + "1", + "2", + "3", + "4", + "5", + "6", + "7", + NULL, +}; + static uint64_t supported_samplerates[] = { - 100, 200, 500, KHZ(1), KHZ(2), KHZ(5), KHZ(10), KHZ(20), - KHZ(50), KHZ(100), KHZ(200), KHZ(500), MHZ(1), MHZ(2), MHZ(5), - MHZ(10), MHZ(20), MHZ(50), MHZ(100), MHZ(200), 0 + SR_HZ(100), + SR_HZ(200), + SR_HZ(500), + SR_KHZ(1), + SR_KHZ(2), + SR_KHZ(5), + SR_KHZ(10), + SR_KHZ(20), + SR_KHZ(50), + SR_KHZ(100), + SR_KHZ(200), + SR_KHZ(500), + SR_MHZ(1), + SR_MHZ(2), + SR_MHZ(5), + SR_MHZ(10), + SR_MHZ(20), + SR_MHZ(50), + SR_MHZ(100), + SR_MHZ(200), + 0, }; -static struct samplerates samplerates = { - 100, MHZ(200), 0, supported_samplerates, +static struct sr_samplerates samplerates = { + SR_HZ(100), + SR_MHZ(200), + SR_HZ(0), + supported_samplerates, }; static GSList *device_instances = NULL; @@ -65,12 +100,16 @@ static int mso_send_control_message(struct sr_device_instance *sdi, int i, w, ret, s = n * 2 + sizeof(mso_head) + sizeof(mso_foot); char *p, *buf; + ret = SR_ERR; + if (fd < 0) goto ret; - buf = malloc(s); - if (!buf) + if (!(buf = g_try_malloc(s))) { + sr_err("mso19: %s: buf malloc failed", __func__); + ret = SR_ERR_MALLOC; goto ret; + } p = buf; memcpy(p, mso_head, sizeof(mso_head)); @@ -93,7 +132,7 @@ static int mso_send_control_message(struct sr_device_instance *sdi, } ret = SR_OK; free: - free(buf); + g_free(buf); ret: return ret; } @@ -103,10 +142,11 @@ static int mso_reset_adc(struct sr_device_instance *sdi) struct mso *mso = sdi->priv; uint16_t ops[2]; - ops[0] = mso_trans(REG_CTL, (mso->ctlbase | BIT_CTL_RESETADC)); - ops[1] = mso_trans(REG_CTL, mso->ctlbase); - mso->ctlbase |= BIT_CTL_ADC_UNKNOWN4; + ops[0] = mso_trans(REG_CTL1, (mso->ctlbase1 | BIT_CTL1_RESETADC)); + ops[1] = mso_trans(REG_CTL1, mso->ctlbase1); + mso->ctlbase1 |= BIT_CTL1_ADC_UNKNOWN4; + sr_dbg("Requesting ADC reset"); return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); } @@ -115,9 +155,10 @@ static int mso_reset_fsm(struct sr_device_instance *sdi) struct mso *mso = sdi->priv; uint16_t ops[1]; - mso->ctlbase |= BIT_CTL_RESETFSM; - ops[0] = mso_trans(REG_CTL, mso->ctlbase); + mso->ctlbase1 |= BIT_CTL1_RESETFSM; + ops[0] = mso_trans(REG_CTL1, mso->ctlbase1); + sr_dbg("Requesting ADC reset"); return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); } @@ -126,11 +167,12 @@ static int mso_toggle_led(struct sr_device_instance *sdi, int state) struct mso *mso = sdi->priv; uint16_t ops[1]; - mso->ctlbase &= BIT_CTL_LED; + mso->ctlbase1 &= ~BIT_CTL1_LED; if (state) - mso->ctlbase |= BIT_CTL_LED; - ops[0] = mso_trans(REG_CTL, mso->ctlbase); + mso->ctlbase1 |= BIT_CTL1_LED; + ops[0] = mso_trans(REG_CTL1, mso->ctlbase1); + sr_dbg("Requesting LED toggle"); return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); } @@ -141,6 +183,7 @@ static int mso_check_trigger(struct sr_device_instance *sdi, char buf[1]; int ret; + sr_dbg("Requesting trigger state"); ret = mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); if (info == NULL || ret != SR_OK) return ret; @@ -150,6 +193,7 @@ static int mso_check_trigger(struct sr_device_instance *sdi, ret = SR_ERR; *info = buf[0]; + sr_dbg("Trigger state is: 0x%x", *info); return ret; } @@ -157,6 +201,7 @@ static int mso_read_buffer(struct sr_device_instance *sdi) { uint16_t ops[] = { mso_trans(REG_BUFFER, 0) }; + sr_dbg("Requesting buffer dump"); return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); } @@ -164,11 +209,12 @@ static int mso_arm(struct sr_device_instance *sdi) { struct mso *mso = sdi->priv; uint16_t ops[] = { - mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_RESETFSM), - mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_ARM), - mso_trans(REG_CTL, mso->ctlbase), + mso_trans(REG_CTL1, mso->ctlbase1 | BIT_CTL1_RESETFSM), + mso_trans(REG_CTL1, mso->ctlbase1 | BIT_CTL1_ARM), + mso_trans(REG_CTL1, mso->ctlbase1), }; + sr_dbg("Requesting trigger arm"); return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); } @@ -176,10 +222,11 @@ static int mso_force_capture(struct sr_device_instance *sdi) { struct mso *mso = sdi->priv; uint16_t ops[] = { - mso_trans(REG_CTL, mso->ctlbase | 8), - mso_trans(REG_CTL, mso->ctlbase), + mso_trans(REG_CTL1, mso->ctlbase1 | 8), + mso_trans(REG_CTL1, mso->ctlbase1), }; + sr_dbg("Requesting forced capture"); return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); } @@ -189,9 +236,10 @@ static int mso_dac_out(struct sr_device_instance *sdi, uint16_t val) uint16_t ops[] = { mso_trans(REG_DAC1, (val >> 8) & 0xff), mso_trans(REG_DAC2, val & 0xff), - mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_RESETADC), + mso_trans(REG_CTL1, mso->ctlbase1 | BIT_CTL1_RESETADC), }; + sr_dbg("Setting dac word to 0x%x", val); return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); } @@ -202,6 +250,7 @@ static int mso_clkrate_out(struct sr_device_instance *sdi, uint16_t val) mso_trans(REG_CLKRATE2, val & 0xff), }; + sr_dbg("Setting clkrate word to 0x%x", val); return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); } @@ -214,7 +263,7 @@ static int mso_configure_rate(struct sr_device_instance *sdi, for (i = 0; i < ARRAY_SIZE(rate_map); i++) { if (rate_map[i].rate == rate) { - mso->slowmode = rate_map[i].slowmode; + mso->ctlbase2 = rate_map[i].slowmode; ret = mso_clkrate_out(sdi, rate_map[i].val); if (ret == SR_OK) mso->cur_rate = rate; @@ -224,7 +273,6 @@ static int mso_configure_rate(struct sr_device_instance *sdi, return ret; } - static inline uint16_t mso_calc_raw_from_mv(struct mso *mso) { return (uint16_t) (0x200 - @@ -284,20 +332,22 @@ static int mso_configure_trigger(struct sr_device_instance *sdi) ops[2] = mso_trans(3, dso_trigger & 0xff); ops[3] = mso_trans(4, (dso_trigger >> 8) & 0xff); ops[4] = mso_trans(11, - mso->dso_trigger_width / HZ_TO_NS(mso->cur_rate)); - ops[5] = mso_trans(15, (2 | mso->slowmode)); - - /* FIXME SPI/I2C Triggers */ - ops[6] = mso_trans(0, 0); - ops[7] = mso_trans(1, 0); - ops[8] = mso_trans(2, 0); - ops[9] = mso_trans(3, 0); - ops[10] = mso_trans(4, 0xff); - ops[11] = mso_trans(5, 0xff); - ops[12] = mso_trans(6, 0xff); - ops[13] = mso_trans(7, 0xff); - ops[14] = mso_trans(8, mso->trigger_spimode); - ops[15] = mso_trans(15, mso->slowmode); + mso->dso_trigger_width / SR_HZ_TO_NS(mso->cur_rate)); + + /* Select the SPI/I2C trigger config bank */ + ops[5] = mso_trans(REG_CTL2, (mso->ctlbase2 | BITS_CTL2_BANK(2))); + /* Configure the SPI/I2C protocol trigger */ + ops[6] = mso_trans(REG_PT_WORD(0), mso->protocol_trigger.word[0]); + ops[7] = mso_trans(REG_PT_WORD(1), mso->protocol_trigger.word[1]); + ops[8] = mso_trans(REG_PT_WORD(2), mso->protocol_trigger.word[2]); + ops[9] = mso_trans(REG_PT_WORD(3), mso->protocol_trigger.word[3]); + ops[10] = mso_trans(REG_PT_MASK(0), mso->protocol_trigger.mask[0]); + ops[11] = mso_trans(REG_PT_MASK(1), mso->protocol_trigger.mask[1]); + ops[12] = mso_trans(REG_PT_MASK(2), mso->protocol_trigger.mask[2]); + ops[13] = mso_trans(REG_PT_MASK(3), mso->protocol_trigger.mask[3]); + ops[14] = mso_trans(REG_PT_SPIMODE, mso->protocol_trigger.spimode); + /* Select the default config bank */ + ops[15] = mso_trans(REG_CTL2, mso->ctlbase2); return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); } @@ -348,7 +398,7 @@ static int mso_parse_serial(const char *iSerial, const char *iProduct, return SR_OK; } -static int hw_init(char *deviceinfo) +static int hw_init(const char *deviceinfo) { struct sr_device_instance *sdi; int devcnt = 0; @@ -366,7 +416,7 @@ static int hw_init(char *deviceinfo) */ udev = udev_new(); if (!udev) { - g_warning("Failed to initialize udev."); + sr_warn("Failed to initialize udev."); goto ret; } enumerate = udev_enumerate_new(udev); @@ -386,8 +436,8 @@ static int hw_init(char *deviceinfo) parent = udev_device_get_parent_with_subsystem_devtype( dev, "usb", "usb_device"); if (!parent) { - g_warning("Unable to find parent usb device for %s", - sysname); + sr_warn("Unable to find parent usb device for %s", + sysname); continue; } @@ -405,38 +455,49 @@ static int hw_init(char *deviceinfo) s = strcspn(iProduct, " "); if (s > sizeof(product) || strlen(iProduct) - s > sizeof(manufacturer)) { - g_warning("Could not parse iProduct: %s", iProduct); + sr_warn("Could not parse iProduct: %s", iProduct); continue; } strncpy(product, iProduct, s); product[s] = 0; strcpy(manufacturer, iProduct + s); - sprintf(hwrev, "r%d", mso->hwrev); - mso = malloc(sizeof(struct mso)); - if (!mso) - continue; - memset(mso, 0, sizeof(struct mso)); + if (!(mso = g_try_malloc0(sizeof(struct mso)))) { + sr_err("mso19: %s: mso malloc failed", __func__); + continue; /* TODO: Errors handled correctly? */ + } if (mso_parse_serial(iSerial, iProduct, mso) != SR_OK) { - g_warning("Invalid iSerial: %s", iSerial); + sr_warn("Invalid iSerial: %s", iSerial); goto err_free_mso; } + sprintf(hwrev, "r%d", mso->hwrev); + /* hardware initial state */ - mso->ctlbase = 0; + mso->ctlbase1 = 0; + { + /* Initialize the protocol trigger configuration */ + int i; + for (i = 0; i < 4; i++) + { + mso->protocol_trigger.word[i] = 0; + mso->protocol_trigger.mask[i] = 0xff; + } + mso->protocol_trigger.spimode = 0; + } - sdi = sr_device_instance_new(devcnt, ST_INITIALIZING, + sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING, manufacturer, product, hwrev); if (!sdi) { - g_warning("Unable to create device instance for %s", - sysname); + sr_warn("Unable to create device instance for %s", + sysname); goto err_free_mso; } /* save a pointer to our private instance data */ sdi->priv = mso; - sdi->serial = serial_device_instance_new(path, -1); + sdi->serial = sr_serial_device_instance_new(path, -1); if (!sdi->serial) goto err_device_instance_free; @@ -468,7 +529,10 @@ static void hw_cleanup(void) if (sdi->serial->fd != -1) serial_close(sdi->serial->fd); if (sdi->priv != NULL) + { free(sdi->priv); + sdi->priv = NULL; + } sr_device_instance_free(sdi); } g_slist_free(device_instances); @@ -493,40 +557,48 @@ static int hw_opendev(int device_index) if (ret != SR_OK) return ret; - sdi->status = ST_ACTIVE; + sdi->status = SR_ST_ACTIVE; /* FIXME: discard serial buffer */ mso_check_trigger(sdi, &mso->trigger_state); -// g_warning("trigger state: %c", mso->trigger_state); + sr_dbg("trigger state: 0x%x", mso->trigger_state); ret = mso_reset_adc(sdi); if (ret != SR_OK) return ret; mso_check_trigger(sdi, &mso->trigger_state); -// g_warning("trigger state: %c", mso->trigger_state); + sr_dbg("trigger state: 0x%x", mso->trigger_state); // ret = mso_reset_fsm(sdi); // if (ret != SR_OK) // return ret; + sr_dbg("Finished %s", __func__); + // return SR_ERR; return SR_OK; } -static void hw_closedev(int device_index) +static int hw_closedev(int device_index) { struct sr_device_instance *sdi; - if (!(sdi = sr_get_device_instance(device_instances, device_index))) - return; + if (!(sdi = sr_get_device_instance(device_instances, device_index))) { + sr_err("mso19: %s: sdi was NULL", __func__); + return SR_ERR; /* TODO: SR_ERR_ARG? */ + } + /* TODO */ if (sdi->serial->fd != -1) { serial_close(sdi->serial->fd); sdi->serial->fd = -1; - sdi->status = ST_INACTIVE; + sdi->status = SR_ST_INACTIVE; } + + sr_dbg("finished %s", __func__); + return SR_OK; } static void *hw_get_device_info(int device_index, int device_info_id) @@ -540,19 +612,22 @@ static void *hw_get_device_info(int device_index, int device_info_id) mso = sdi->priv; switch (device_info_id) { - case DI_INSTANCE: + case SR_DI_INSTANCE: info = sdi; break; - case DI_NUM_PROBES: /* FIXME: How to report analog probe? */ - info = GINT_TO_POINTER(8); + case SR_DI_NUM_PROBES: /* FIXME: How to report analog probe? */ + info = GINT_TO_POINTER(NUM_PROBES); break; - case DI_SAMPLERATES: + case SR_DI_PROBE_NAMES: + info = probe_names; + break; + case SR_DI_SAMPLERATES: info = &samplerates; break; - case DI_TRIGGER_TYPES: + case SR_DI_TRIGGER_TYPES: info = "01"; /* FIXME */ break; - case DI_CUR_SAMPLERATE: + case SR_DI_CUR_SAMPLERATE: info = &mso->cur_rate; break; } @@ -564,7 +639,7 @@ static int hw_get_status(int device_index) struct sr_device_instance *sdi; if (!(sdi = sr_get_device_instance(device_instances, device_index))) - return ST_NOT_FOUND; + return SR_ST_NOT_FOUND; return sdi->status; } @@ -582,14 +657,13 @@ static int hw_set_configuration(int device_index, int capability, void *value) return SR_ERR; switch (capability) { - case HWCAP_SAMPLERATE: + case SR_HWCAP_SAMPLERATE: return mso_configure_rate(sdi, *(uint64_t *) value); - case HWCAP_PROBECONFIG: - case HWCAP_LIMIT_SAMPLES: + case SR_HWCAP_PROBECONFIG: + case SR_HWCAP_LIMIT_SAMPLES: default: return SR_OK; /* FIXME */ } - } #define MSO_TRIGGER_UNKNOWN '!' @@ -606,6 +680,7 @@ static int receive_data(int fd, int revents, void *user_data) struct sr_device_instance *sdi = user_data; struct mso *mso = sdi->priv; struct sr_datafeed_packet packet; + struct sr_datafeed_logic logic; uint8_t in[1024], logic_out[1024]; double analog_out[1024]; size_t i, s; @@ -645,21 +720,24 @@ static int receive_data(int fd, int revents, void *user_data) ((mso->buffer[i * 3 + 2] & 0x3f) << 2); } - packet.type = DF_LOGIC; - packet.length = 1024; - packet.unitsize = 1; - packet.payload = logic_out; - session_bus(mso->session_id, &packet); - + packet.type = SR_DF_LOGIC; + packet.payload = &logic; + logic.length = 1024; + logic.unitsize = 1; + logic.data = logic_out; + sr_session_bus(mso->session_id, &packet); - packet.type = DF_ANALOG; + // Dont bother fixing this yet, keep it "old style" + /* + packet.type = SR_DF_ANALOG; packet.length = 1024; packet.unitsize = sizeof(double); packet.payload = analog_out; - session_bus(mso->session_id, &packet); + sr_session_bus(mso->session_id, &packet); + */ - packet.type = DF_END; - session_bus(mso->session_id, &packet); + packet.type = SR_DF_END; + sr_session_bus(mso->session_id, &packet); return TRUE; } @@ -682,8 +760,8 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id) // return ret; /* FIXME: ACDC Mode */ - mso->ctlbase &= 0x7f; -// mso->ctlbase |= mso->acdcmode; + mso->ctlbase1 &= 0x7f; +// mso->ctlbase1 |= mso->acdcmode; ret = mso_configure_rate(sdi, mso->cur_rate); if (ret != SR_OK) @@ -723,18 +801,16 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id) return ret; mso->session_id = session_device_id; - source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi); + sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi); - packet.type = DF_HEADER; - packet.length = sizeof(struct sr_datafeed_header); + packet.type = SR_DF_HEADER; packet.payload = (unsigned char *) &header; header.feed_version = 1; gettimeofday(&header.starttime, NULL); header.samplerate = mso->cur_rate; - header.num_analog_probes = 1; + // header.num_analog_probes = 1; header.num_logic_probes = 8; - header.protocol_id = PROTO_RAW; - session_bus(session_device_id, &packet); + sr_session_bus(session_device_id, &packet); return ret; } @@ -746,18 +822,18 @@ static void hw_stop_acquisition(int device_index, gpointer session_device_id) device_index = device_index; - packet.type = DF_END; - session_bus(session_device_id, &packet); + packet.type = SR_DF_END; + sr_session_bus(session_device_id, &packet); } -struct device_plugin link_mso19_plugin_info = { +SR_PRIV struct sr_device_plugin link_mso19_plugin_info = { .name = "link-mso19", + .longname = "Link Instruments MSO-19", .api_version = 1, .init = hw_init, .cleanup = hw_cleanup, - - .open = hw_opendev, - .close = hw_closedev, + .opendev = hw_opendev, + .closedev = hw_closedev, .get_device_info = hw_get_device_info, .get_status = hw_get_status, .get_capabilities = hw_get_capabilities,