X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Frigol-ds%2Fprotocol.c;h=97cb353c6ca597d1b9fe19b7e94396a0c043d7e8;hb=43cd4637285833706f8a404ca027bcf0ee75b9ae;hp=4f4ff412b015d7240b37c08ccb5fb30ffe346b02;hpb=f76c24f6fdf09075a68197659df35eb1530e3004;p=libsigrok.git diff --git a/hardware/rigol-ds/protocol.c b/hardware/rigol-ds/protocol.c index 4f4ff412..97cb353c 100644 --- a/hardware/rigol-ds/protocol.c +++ b/hardware/rigol-ds/protocol.c @@ -185,7 +185,7 @@ static int rigol_ds_trigger_wait(const struct sr_dev_inst *sdi) * Timebase * num hor. divs * 85(%) * 1e6(usecs) / 100 * -> 85 percent of sweep time */ - s = (devc->timebase * devc->model->num_horizontal_divs + s = (devc->timebase * devc->model->series->num_horizontal_divs * 85e6) / 100L; sr_spew("Sleeping for %ld usecs instead of trigger-wait", s); g_usleep(s); @@ -207,19 +207,22 @@ static int rigol_ds_stop_wait(const struct sr_dev_inst *sdi) static int rigol_ds_check_stop(const struct sr_dev_inst *sdi) { struct dev_context *devc; - struct sr_probe *probe; + struct sr_channel *ch; int tmp; if (!(devc = sdi->priv)) return SR_ERR; - probe = devc->channel_entry->data; + ch = devc->channel_entry->data; - if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN%d", - probe->index + 1) != SR_OK) + if (devc->model->series->protocol <= PROTOCOL_V2) + return SR_OK; + + if (rigol_ds_config_set(sdi, ":WAV:SOUR CHAN%d", + ch->index + 1) != SR_OK) return SR_ERR; /* Check that the number of samples will be accepted */ - if (sr_scpi_send(sdi->conn, ":WAV:POIN %d;*OPC", devc->analog_frame_size) != SR_OK) + if (rigol_ds_config_set(sdi, ":WAV:POIN %d", devc->analog_frame_size) != SR_OK) return SR_ERR; if (sr_scpi_get_int(sdi->conn, "*ESR?", &tmp) != SR_OK) return SR_ERR; @@ -239,7 +242,7 @@ static int rigol_ds_check_stop(const struct sr_dev_inst *sdi) sr_warn("Single shot acquisition failed, retrying..."); /* Sleep a bit, otherwise the single shot will often fail */ g_usleep(500000); - sr_scpi_send(sdi->conn, ":SING"); + rigol_ds_config_set(sdi, ":SING"); rigol_ds_set_wait_event(devc, WAIT_STOP); return SR_ERR; } @@ -258,39 +261,67 @@ static int rigol_ds_block_wait(const struct sr_dev_inst *sdi) if (!(devc = sdi->priv)) return SR_ERR; - start = time(NULL); + if (devc->model->series->protocol >= PROTOCOL_V3) { - do { - if (time(NULL) - start >= 3) { - sr_dbg("Timeout waiting for data block"); - return SR_ERR_TIMEOUT; - } + start = time(NULL); - /* - * The scope copies data really slowly from sample - * memory to its output buffer, so try not to bother - * it too much with SCPI requests but don't wait too - * long for short sample frame sizes. - */ - g_usleep(devc->analog_frame_size < 15000 ? 100000 : 1000000); + do { + if (time(NULL) - start >= 3) { + sr_dbg("Timeout waiting for data block"); + return SR_ERR_TIMEOUT; + } - /* "READ,nnnn" (still working) or "IDLE,nnnn" (finished) */ - if (sr_scpi_get_string(sdi->conn, ":WAV:STAT?", &buf) != SR_OK) - return SR_ERR; + /* + * The scope copies data really slowly from sample + * memory to its output buffer, so try not to bother + * it too much with SCPI requests but don't wait too + * long for short sample frame sizes. + */ + g_usleep(devc->analog_frame_size < 15000 ? 100000 : 1000000); - if (parse_int(buf + 5, &len) != SR_OK) - return SR_ERR; - } while (buf[0] == 'R' && len < 1000000); + /* "READ,nnnn" (still working) or "IDLE,nnnn" (finished) */ + if (sr_scpi_get_string(sdi->conn, ":WAV:STAT?", &buf) != SR_OK) + return SR_ERR; + + if (parse_int(buf + 5, &len) != SR_OK) + return SR_ERR; + } while (buf[0] == 'R' && len < 1000000); + } rigol_ds_set_wait_event(devc, WAIT_NONE); return SR_OK; } +/* Send a configuration setting. */ +SR_PRIV int rigol_ds_config_set(const struct sr_dev_inst *sdi, const char *format, ...) +{ + struct dev_context *devc = sdi->priv; + va_list args; + int ret; + + va_start(args, format); + ret = sr_scpi_send_variadic(sdi->conn, format, args); + va_end(args); + + if (ret != SR_OK) + return SR_ERR; + + if (devc->model->series->protocol == PROTOCOL_V2) { + /* The DS1000 series needs this stupid delay, *OPC? doesn't work. */ + sr_spew("delay %dms", 100); + g_usleep(100000); + return SR_OK; + } else { + return sr_scpi_get_opc(sdi->conn); + } +} + /* Start capturing a new frameset */ SR_PRIV int rigol_ds_capture_start(const struct sr_dev_inst *sdi) { struct dev_context *devc; + gchar *trig_mode; if (!(devc = sdi->priv)) return SR_ERR; @@ -298,18 +329,44 @@ SR_PRIV int rigol_ds_capture_start(const struct sr_dev_inst *sdi) sr_dbg("Starting data capture for frameset %lu of %lu", devc->num_frames + 1, devc->limit_frames); - if (sr_scpi_send(sdi->conn, ":WAV:FORM BYTE") != SR_OK) - return SR_ERR; - if (devc->data_source == DATA_SOURCE_LIVE) { - if (sr_scpi_send(sdi->conn, ":WAV:MODE NORM") != SR_OK) - return SR_ERR; + switch (devc->model->series->protocol) { + case PROTOCOL_V1: rigol_ds_set_wait_event(devc, WAIT_TRIGGER); - } else { - if (sr_scpi_send(sdi->conn, ":WAV:MODE RAW") != SR_OK) + break; + case PROTOCOL_V2: + if (devc->data_source == DATA_SOURCE_LIVE) { + if (rigol_ds_config_set(sdi, ":WAV:POIN:MODE NORMAL") != SR_OK) + return SR_ERR; + rigol_ds_set_wait_event(devc, WAIT_TRIGGER); + } else { + if (rigol_ds_config_set(sdi, ":STOP") != SR_OK) + return SR_ERR; + if (rigol_ds_config_set(sdi, ":WAV:POIN:MODE RAW") != SR_OK) + return SR_ERR; + if (sr_scpi_get_string(sdi->conn, ":TRIG:MODE?", &trig_mode) != SR_OK) + return SR_ERR; + if (rigol_ds_config_set(sdi, ":TRIG:%s:SWE SING", trig_mode) != SR_OK) + return SR_ERR; + if (rigol_ds_config_set(sdi, ":RUN") != SR_OK) + return SR_ERR; + rigol_ds_set_wait_event(devc, WAIT_STOP); + } + break; + case PROTOCOL_V3: + if (rigol_ds_config_set(sdi, ":WAV:FORM BYTE") != SR_OK) return SR_ERR; - if (sr_scpi_send(sdi->conn, ":SING", devc->analog_frame_size) != SR_OK) - return SR_ERR; - rigol_ds_set_wait_event(devc, WAIT_STOP); + if (devc->data_source == DATA_SOURCE_LIVE) { + if (rigol_ds_config_set(sdi, ":WAV:MODE NORM") != SR_OK) + return SR_ERR; + rigol_ds_set_wait_event(devc, WAIT_TRIGGER); + } else { + if (rigol_ds_config_set(sdi, ":WAV:MODE RAW") != SR_OK) + return SR_ERR; + if (rigol_ds_config_set(sdi, ":SING") != SR_OK) + return SR_ERR; + rigol_ds_set_wait_event(devc, WAIT_STOP); + } + break; } return SR_OK; @@ -319,78 +376,101 @@ SR_PRIV int rigol_ds_capture_start(const struct sr_dev_inst *sdi) SR_PRIV int rigol_ds_channel_start(const struct sr_dev_inst *sdi) { struct dev_context *devc; - struct sr_probe *probe; + struct sr_channel *ch; if (!(devc = sdi->priv)) return SR_ERR; - probe = devc->channel_entry->data; + ch = devc->channel_entry->data; - sr_dbg("Starting reading data from channel %d", probe->index + 1); + sr_dbg("Starting reading data from channel %d", ch->index + 1); - if (devc->model->series < RIGOL_DS1000Z) { - if (probe->type == SR_PROBE_LOGIC) { + if (devc->model->series->protocol <= PROTOCOL_V2) { + if (ch->type == SR_CHANNEL_LOGIC) { if (sr_scpi_send(sdi->conn, ":WAV:DATA? DIG") != SR_OK) return SR_ERR; } else { if (sr_scpi_send(sdi->conn, ":WAV:DATA? CHAN%d", - probe->index + 1) != SR_OK) + ch->index + 1) != SR_OK) return SR_ERR; } + rigol_ds_set_wait_event(devc, WAIT_NONE); } else { - if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN%d", - probe->index + 1) != SR_OK) + if (rigol_ds_config_set(sdi, ":WAV:SOUR CHAN%d", + ch->index + 1) != SR_OK) return SR_ERR; if (devc->data_source != DATA_SOURCE_LIVE) { - if (sr_scpi_send(sdi->conn, ":WAV:RES") != SR_OK) + if (rigol_ds_config_set(sdi, ":WAV:RES") != SR_OK) return SR_ERR; - if (sr_scpi_send(sdi->conn, ":WAV:BEG") != SR_OK) + if (rigol_ds_config_set(sdi, ":WAV:BEG") != SR_OK) return SR_ERR; - rigol_ds_set_wait_event(devc, WAIT_BLOCK); - } else - rigol_ds_set_wait_event(devc, WAIT_NONE); + } } + rigol_ds_set_wait_event(devc, WAIT_BLOCK); + devc->num_channel_bytes = 0; + devc->num_header_bytes = 0; devc->num_block_bytes = 0; return SR_OK; } /* Read the header of a data block */ -static int rigol_ds_read_header(struct sr_scpi_dev_inst *scpi) +static int rigol_ds_read_header(struct sr_dev_inst *sdi) { - char start[3], length[10]; - int len, tmp; - - /* Read the hashsign and length digit. */ - tmp = sr_scpi_read_data(scpi, start, 2); - start[2] = '\0'; - if (tmp != 2) { - sr_err("Failed to read first two bytes of data block header."); - return -1; + struct sr_scpi_dev_inst *scpi = sdi->conn; + struct dev_context *devc = sdi->priv; + char *buf = (char *) devc->buffer; + size_t header_length; + int ret; + + /* Try to read the hashsign and length digit. */ + if (devc->num_header_bytes < 2) { + ret = sr_scpi_read_data(scpi, buf + devc->num_header_bytes, + 2 - devc->num_header_bytes); + if (ret < 0) { + sr_err("Read error while reading data header."); + return SR_ERR; + } + devc->num_header_bytes += ret; } - if (start[0] != '#' || !isdigit(start[1]) || start[1] == '0') { - sr_err("Received invalid data block header start '%s'.", start); - return -1; + + if (devc->num_header_bytes < 2) + return 0; + + if (buf[0] != '#' || !isdigit(buf[1]) || buf[1] == '0') { + sr_err("Received invalid data block header '%c%c'.", buf[0], buf[1]); + return SR_ERR; } - len = atoi(start + 1); - /* Read the data length. */ - tmp = sr_scpi_read_data(scpi, length, len); - length[len] = '\0'; - if (tmp != len) { - sr_err("Failed to read %d bytes of data block length.", len); - return -1; + header_length = 2 + buf[1] - '0'; + + /* Try to read the length. */ + if (devc->num_header_bytes < header_length) { + ret = sr_scpi_read_data(scpi, buf + devc->num_header_bytes, + header_length - devc->num_header_bytes); + if (ret < 0) { + sr_err("Read error while reading data header."); + return SR_ERR; + } + devc->num_header_bytes += ret; } - if (parse_int(length, &len) != SR_OK) { - sr_err("Received invalid data block length '%s'.", length); + + if (devc->num_header_bytes < header_length) + return 0; + + /* Read the data length. */ + buf[header_length] = '\0'; + + if (parse_int(buf + 2, &ret) != SR_OK) { + sr_err("Received invalid data block length '%s'.", buf + 2); return -1; } - sr_dbg("Received data block header: %s%s -> block length %d", start, length, len); + sr_dbg("Received data block header: '%s' -> block length %d", buf, ret); - return len; + return ret; } SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data) @@ -403,7 +483,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data) struct sr_datafeed_logic logic; double vdiv, offset; int len, i, vref; - struct sr_probe *probe; + struct sr_channel *ch; gsize expected_data_bytes; (void)fd; @@ -417,54 +497,57 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data) scpi = sdi->conn; if (revents == G_IO_IN || revents == 0) { - if (devc->model->series >= RIGOL_DS1000Z) { - switch(devc->wait_event) { - case WAIT_NONE: - break; - case WAIT_TRIGGER: - if (rigol_ds_trigger_wait(sdi) != SR_OK) - return TRUE; - if (rigol_ds_channel_start(sdi) != SR_OK) - return TRUE; - break; - case WAIT_BLOCK: - if (rigol_ds_block_wait(sdi) != SR_OK) - return TRUE; - break; - case WAIT_STOP: - if (rigol_ds_stop_wait(sdi) != SR_OK) - return TRUE; - if (rigol_ds_check_stop(sdi) != SR_OK) - return TRUE; - if (rigol_ds_channel_start(sdi) != SR_OK) - return TRUE; + switch(devc->wait_event) { + case WAIT_NONE: + break; + case WAIT_TRIGGER: + if (rigol_ds_trigger_wait(sdi) != SR_OK) return TRUE; - default: - sr_err("BUG: Unknown event target encountered"); - } + if (rigol_ds_channel_start(sdi) != SR_OK) + return TRUE; + return TRUE; + case WAIT_BLOCK: + if (rigol_ds_block_wait(sdi) != SR_OK) + return TRUE; + break; + case WAIT_STOP: + if (rigol_ds_stop_wait(sdi) != SR_OK) + return TRUE; + if (rigol_ds_check_stop(sdi) != SR_OK) + return TRUE; + if (rigol_ds_channel_start(sdi) != SR_OK) + return TRUE; + return TRUE; + default: + sr_err("BUG: Unknown event target encountered"); } - probe = devc->channel_entry->data; + ch = devc->channel_entry->data; - expected_data_bytes = probe->type == SR_PROBE_ANALOG ? + expected_data_bytes = ch->type == SR_CHANNEL_ANALOG ? devc->analog_frame_size : devc->digital_frame_size; - if (devc->num_block_bytes == 0 && - devc->model->series >= RIGOL_DS1000Z) { + if (devc->num_block_bytes == 0) { + if (devc->model->series->protocol >= PROTOCOL_V3) if (sr_scpi_send(sdi->conn, ":WAV:DATA?") != SR_OK) return TRUE; - } - - if (devc->num_block_bytes == 0) { if (sr_scpi_read_begin(scpi) != SR_OK) return TRUE; - if (devc->model->protocol == PROTOCOL_IEEE488_2) { + if (devc->format == FORMAT_IEEE488_2) { sr_dbg("New block header expected"); - len = rigol_ds_read_header(scpi); - if (len == -1) + len = rigol_ds_read_header(sdi); + if (len == 0) + /* Still reading the header. */ return TRUE; + if (len == -1) { + sr_err("Read error, aborting capture."); + packet.type = SR_DF_FRAME_END; + sr_session_send(cb_data, &packet); + sdi->driver->dev_acquisition_stop(sdi, cb_data); + return TRUE; + } /* At slow timebases in live capture the DS2072 * sometimes returns "short" data blocks, with * apparently no way to get the rest of the data. @@ -485,26 +568,35 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data) } len = devc->num_block_bytes - devc->num_block_read; - len = sr_scpi_read_data(scpi, (char *)devc->buffer, - len < ACQ_BUFFER_SIZE ? len : ACQ_BUFFER_SIZE); + if (len > ACQ_BUFFER_SIZE) + len = ACQ_BUFFER_SIZE; + sr_dbg("Requesting read of %d bytes", len); - sr_dbg("Received %d bytes.", len); - if (len == -1) + len = sr_scpi_read_data(scpi, (char *)devc->buffer, len); + + if (len == -1) { + sr_err("Read error, aborting capture."); + packet.type = SR_DF_FRAME_END; + sr_session_send(cb_data, &packet); + sdi->driver->dev_acquisition_stop(sdi, cb_data); return TRUE; + } + + sr_dbg("Received %d bytes.", len); devc->num_block_read += len; - if (probe->type == SR_PROBE_ANALOG) { - vref = devc->vert_reference[probe->index]; - vdiv = devc->vdiv[probe->index] / 25.6; - offset = devc->vert_offset[probe->index]; - if (devc->model->series >= RIGOL_DS1000Z) + if (ch->type == SR_CHANNEL_ANALOG) { + vref = devc->vert_reference[ch->index]; + vdiv = devc->vdiv[ch->index] / 25.6; + offset = devc->vert_offset[ch->index]; + if (devc->model->series->protocol >= PROTOCOL_V3) for (i = 0; i < len; i++) devc->data[i] = ((int)devc->buffer[i] - vref) * vdiv - offset; else for (i = 0; i < len; i++) devc->data[i] = (128 - devc->buffer[i]) * vdiv - offset; - analog.probes = g_slist_append(NULL, probe); + analog.channels = g_slist_append(NULL, ch); analog.num_samples = len; analog.data = devc->data; analog.mq = SR_MQ_VOLTAGE; @@ -513,7 +605,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data) packet.type = SR_DF_ANALOG; packet.payload = &analog; sr_session_send(cb_data, &packet); - g_slist_free(analog.probes); + g_slist_free(analog.channels); } else { logic.length = len; logic.unitsize = 2; @@ -525,18 +617,21 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data) if (devc->num_block_read == devc->num_block_bytes) { sr_dbg("Block has been completed"); - if (devc->model->series >= RIGOL_DS1000Z) { + if (devc->model->series->protocol >= PROTOCOL_V3) { /* Discard the terminating linefeed */ sr_scpi_read_data(scpi, (char *)devc->buffer, 1); } - if (devc->model->protocol == PROTOCOL_IEEE488_2) { + if (devc->format == FORMAT_IEEE488_2) { /* Prepare for possible next block */ + devc->num_header_bytes = 0; devc->num_block_bytes = 0; if (devc->data_source != DATA_SOURCE_LIVE) rigol_ds_set_wait_event(devc, WAIT_BLOCK); } if (!sr_scpi_read_complete(scpi)) { sr_err("Read should have been completed"); + packet.type = SR_DF_FRAME_END; + sr_session_send(cb_data, &packet); sdi->driver->dev_acquisition_stop(sdi, cb_data); return TRUE; } @@ -552,7 +647,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data) return TRUE; /* End of data for this channel. */ - if (devc->model->series >= RIGOL_DS1000Z) { + if (devc->model->series->protocol >= PROTOCOL_V3) { /* Signal end of data download to scope */ if (devc->data_source != DATA_SOURCE_LIVE) /* @@ -560,10 +655,10 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data) * to the next channel causes an error. Fun with * firmware... */ - sr_scpi_send(sdi->conn, ":WAV:END"); + rigol_ds_config_set(sdi, ":WAV:END"); } - if (probe->type == SR_PROBE_ANALOG + if (ch->type == SR_CHANNEL_ANALOG && devc->channel_entry->next != NULL) { /* We got the frame for this analog channel, but * there's another analog channel. */ @@ -571,10 +666,10 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data) rigol_ds_channel_start(sdi); } else { /* Done with all analog channels in this frame. */ - if (devc->enabled_digital_probes - && devc->channel_entry != devc->enabled_digital_probes) { + if (devc->enabled_digital_channels + && devc->channel_entry != devc->enabled_digital_channels) { /* Now we need to get the digital data. */ - devc->channel_entry = devc->enabled_digital_probes; + devc->channel_entry = devc->enabled_digital_channels; rigol_ds_channel_start(sdi); } else { /* Done with this frame. */ @@ -586,15 +681,12 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data) sdi->driver->dev_acquisition_stop(sdi, cb_data); } else { /* Get the next frame, starting with the first analog channel. */ - if (devc->enabled_analog_probes) - devc->channel_entry = devc->enabled_analog_probes; + if (devc->enabled_analog_channels) + devc->channel_entry = devc->enabled_analog_channels; else - devc->channel_entry = devc->enabled_digital_probes; + devc->channel_entry = devc->enabled_digital_channels; - if (devc->model->series < RIGOL_DS1000Z) - rigol_ds_channel_start(sdi); - else - rigol_ds_capture_start(sdi); + rigol_ds_capture_start(sdi); /* Start of next frame. */ packet.type = SR_DF_FRAME_BEGIN; @@ -666,10 +758,10 @@ SR_PRIV int rigol_ds_get_dev_cfg(const struct sr_dev_inst *sdi) sr_dbg("CH%d %g", i + 1, devc->vdiv[i]); sr_dbg("Current vertical reference:"); - if (devc->model->series >= RIGOL_DS1000Z) { + if (devc->model->series->protocol >= PROTOCOL_V3) { /* Vertical reference - not certain if this is the place to read it. */ for (i = 0; i < devc->model->analog_channels; i++) { - if (sr_scpi_send(sdi->conn, ":WAV:SOUR CHAN%d", i + 1) != SR_OK) + if (rigol_ds_config_set(sdi, ":WAV:SOUR CHAN%d", i + 1) != SR_OK) return SR_ERR; if (sr_scpi_get_int(sdi->conn, ":WAV:YREF?", &devc->vert_reference[i]) != SR_OK) return SR_ERR;