X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Frigol-ds%2Fprotocol.c;h=4a307178b604b8d2f636e07fa943cea21e4ddadc;hb=5faebab2903dc91949edc31f0a4b118d86090a30;hp=3197d1916382795b407284d604dca049774433ee;hpb=98bfc4741f76b07e7db4c9628a6924004fc4fff4;p=libsigrok.git diff --git a/src/hardware/rigol-ds/protocol.c b/src/hardware/rigol-ds/protocol.c index 3197d191..4a307178 100644 --- a/src/hardware/rigol-ds/protocol.c +++ b/src/hardware/rigol-ds/protocol.c @@ -19,6 +19,7 @@ * along with this program. If not, see . */ +#include #include #include #include @@ -28,8 +29,9 @@ #include #include #include -#include "libsigrok.h" +#include #include "libsigrok-internal.h" +#include "scpi.h" #include "protocol.h" /* @@ -215,7 +217,7 @@ static int rigol_ds_check_stop(const struct sr_dev_inst *sdi) ch = devc->channel_entry->data; - if (devc->model->series->protocol <= PROTOCOL_V2) + if (devc->model->series->protocol != PROTOCOL_V3) return SR_OK; if (rigol_ds_config_set(sdi, ":WAV:SOUR CHAN%d", @@ -241,7 +243,7 @@ static int rigol_ds_check_stop(const struct sr_dev_inst *sdi) if (tmp & 0x10) { sr_warn("Single shot acquisition failed, retrying..."); /* Sleep a bit, otherwise the single shot will often fail */ - g_usleep(500000); + g_usleep(500 * 1000); rigol_ds_config_set(sdi, ":SING"); rigol_ds_set_wait_event(devc, WAIT_STOP); return SR_ERR; @@ -261,7 +263,7 @@ static int rigol_ds_block_wait(const struct sr_dev_inst *sdi) if (!(devc = sdi->priv)) return SR_ERR; - if (devc->model->series->protocol >= PROTOCOL_V3) { + if (devc->model->series->protocol == PROTOCOL_V3) { start = time(NULL); @@ -277,7 +279,7 @@ static int rigol_ds_block_wait(const struct sr_dev_inst *sdi) * 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); + g_usleep(devc->analog_frame_size < (15 * 1000) ? (100 * 1000) : (1000 * 1000)); /* "READ,nnnn" (still working) or "IDLE,nnnn" (finished) */ if (sr_scpi_get_string(sdi->conn, ":WAV:STAT?", &buf) != SR_OK) @@ -285,7 +287,7 @@ static int rigol_ds_block_wait(const struct sr_dev_inst *sdi) if (parse_int(buf + 5, &len) != SR_OK) return SR_ERR; - } while (buf[0] == 'R' && len < 1000000); + } while (buf[0] == 'R' && len < (1000 * 1000)); } rigol_ds_set_wait_event(devc, WAIT_NONE); @@ -310,7 +312,7 @@ SR_PRIV int rigol_ds_config_set(const struct sr_dev_inst *sdi, const char *forma 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); + g_usleep(100 * 1000); return SR_OK; } else { return sr_scpi_get_opc(sdi->conn); @@ -322,11 +324,12 @@ SR_PRIV int rigol_ds_capture_start(const struct sr_dev_inst *sdi) { struct dev_context *devc; gchar *trig_mode; + unsigned int num_channels, i, j; if (!(devc = sdi->priv)) return SR_ERR; - sr_dbg("Starting data capture for frameset %lu of %lu", + sr_dbg("Starting data capture for frameset %" PRIu64 " of %" PRIu64, devc->num_frames + 1, devc->limit_frames); switch (devc->model->series->protocol) { @@ -353,15 +356,44 @@ SR_PRIV int rigol_ds_capture_start(const struct sr_dev_inst *sdi) } break; case PROTOCOL_V3: + case PROTOCOL_V4: if (rigol_ds_config_set(sdi, ":WAV:FORM BYTE") != SR_OK) return SR_ERR; if (devc->data_source == DATA_SOURCE_LIVE) { if (rigol_ds_config_set(sdi, ":WAV:MODE NORM") != SR_OK) return SR_ERR; + devc->analog_frame_size = devc->model->series->live_samples; + devc->digital_frame_size = devc->model->series->live_samples; rigol_ds_set_wait_event(devc, WAIT_TRIGGER); } else { - if (rigol_ds_config_set(sdi, ":WAV:MODE RAW") != SR_OK) - return SR_ERR; + if (devc->model->series->protocol == PROTOCOL_V3) { + if (rigol_ds_config_set(sdi, ":WAV:MODE RAW") != SR_OK) + return SR_ERR; + } else if (devc->model->series->protocol == PROTOCOL_V4) { + num_channels = 0; + + /* Channels 3 and 4 are multiplexed with D0-7 and D8-15 */ + for (i = 0; i < devc->model->analog_channels; i++) { + if (devc->analog_channels[i]) { + num_channels++; + } else if (i >= 2 && devc->model->has_digital) { + for (j = 0; j < 8; j++) { + if (devc->digital_channels[8 * (i - 2) + j]) { + num_channels++; + break; + } + } + } + } + + devc->analog_frame_size = devc->digital_frame_size = + num_channels == 1 ? + devc->model->series->buffer_samples : + num_channels == 2 ? + devc->model->series->buffer_samples / 2 : + devc->model->series->buffer_samples / 4; + } + if (rigol_ds_config_set(sdi, ":SING") != SR_OK) return SR_ERR; rigol_ds_set_wait_event(devc, WAIT_STOP); @@ -385,7 +417,9 @@ SR_PRIV int rigol_ds_channel_start(const struct sr_dev_inst *sdi) sr_dbg("Starting reading data from channel %d", ch->index + 1); - if (devc->model->series->protocol <= PROTOCOL_V2) { + switch (devc->model->series->protocol) { + case PROTOCOL_V1: + case PROTOCOL_V2: if (ch->type == SR_CHANNEL_LOGIC) { if (sr_scpi_send(sdi->conn, ":WAV:DATA? DIG") != SR_OK) return SR_ERR; @@ -395,7 +429,8 @@ SR_PRIV int rigol_ds_channel_start(const struct sr_dev_inst *sdi) return SR_ERR; } rigol_ds_set_wait_event(devc, WAIT_NONE); - } else { + break; + case PROTOCOL_V3: if (rigol_ds_config_set(sdi, ":WAV:SOUR CHAN%d", ch->index + 1) != SR_OK) return SR_ERR; @@ -405,6 +440,31 @@ SR_PRIV int rigol_ds_channel_start(const struct sr_dev_inst *sdi) if (rigol_ds_config_set(sdi, ":WAV:BEG") != SR_OK) return SR_ERR; } + break; + case PROTOCOL_V4: + if (ch->type == SR_CHANNEL_ANALOG) { + if (rigol_ds_config_set(sdi, ":WAV:SOUR CHAN%d", + ch->index + 1) != SR_OK) + return SR_ERR; + } else { + if (rigol_ds_config_set(sdi, ":WAV:SOUR D%d", + ch->index) != SR_OK) + return SR_ERR; + } + + if (rigol_ds_config_set(sdi, + devc->data_source == DATA_SOURCE_LIVE ? + ":WAV:MODE NORM" :":WAV:MODE RAW") != SR_OK) + return SR_ERR; + break; + } + + if (devc->model->series->protocol >= PROTOCOL_V3 && + ch->type == SR_CHANNEL_ANALOG) { + /* Vertical reference. */ + if (sr_scpi_get_int(sdi->conn, ":WAV:YREF?", + &devc->vert_reference[ch->index]) != SR_OK) + return SR_ERR; } rigol_ds_set_wait_event(devc, WAIT_BLOCK); @@ -479,7 +539,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data) struct sr_scpi_dev_inst *scpi; struct dev_context *devc; struct sr_datafeed_packet packet; - struct sr_datafeed_analog analog; + struct sr_datafeed_analog_old analog; struct sr_datafeed_logic logic; double vdiv, offset; int len, i, vref; @@ -496,204 +556,206 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data) scpi = sdi->conn; - if (revents == G_IO_IN || revents == 0) { - 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; + if (!(revents == G_IO_IN || revents == 0)) + return TRUE; + + switch (devc->wait_event) { + case WAIT_NONE: + break; + case WAIT_TRIGGER: + if (rigol_ds_trigger_wait(sdi) != SR_OK) 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; + if (rigol_ds_channel_start(sdi) != SR_OK) return TRUE; - default: - sr_err("BUG: Unknown event target encountered"); - break; - } - - ch = devc->channel_entry->data; + 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"); + break; + } - expected_data_bytes = ch->type == SR_CHANNEL_ANALOG ? - devc->analog_frame_size : devc->digital_frame_size; + ch = devc->channel_entry->data; - 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; + expected_data_bytes = ch->type == SR_CHANNEL_ANALOG ? + devc->analog_frame_size : devc->digital_frame_size; - if (sr_scpi_read_begin(scpi) != SR_OK) + if (devc->num_block_bytes == 0) { + if (devc->model->series->protocol >= PROTOCOL_V4) { + if (sr_scpi_send(sdi->conn, ":WAV:START %d", + devc->num_channel_bytes + 1) != SR_OK) + return TRUE; + if (sr_scpi_send(sdi->conn, ":WAV:STOP %d", + MIN(devc->num_channel_bytes + ACQ_BLOCK_SIZE, + devc->analog_frame_size)) != SR_OK) return TRUE; - - if (devc->format == FORMAT_IEEE488_2) { - sr_dbg("New block header expected"); - 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. - * Discard these, the complete data block will - * appear eventually. - */ - if (devc->data_source == DATA_SOURCE_LIVE - && (unsigned)len < expected_data_bytes) { - sr_dbg("Discarding short data block"); - sr_scpi_read_data(scpi, (char *)devc->buffer, len + 1); - return TRUE; - } - devc->num_block_bytes = len; - } else { - devc->num_block_bytes = expected_data_bytes; - } - devc->num_block_read = 0; } - len = devc->num_block_bytes - devc->num_block_read; - if (len > ACQ_BUFFER_SIZE) - len = ACQ_BUFFER_SIZE; - sr_dbg("Requesting read of %d bytes", len); - - len = sr_scpi_read_data(scpi, (char *)devc->buffer, len); + if (devc->model->series->protocol >= PROTOCOL_V3) + if (sr_scpi_send(sdi->conn, ":WAV:DATA?") != SR_OK) + 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); + if (sr_scpi_read_begin(scpi) != SR_OK) return TRUE; - } - - sr_dbg("Received %d bytes.", len); - - devc->num_block_read += len; - - 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.channels = g_slist_append(NULL, ch); - analog.num_samples = len; - analog.data = devc->data; - analog.mq = SR_MQ_VOLTAGE; - analog.unit = SR_UNIT_VOLT; - analog.mqflags = 0; - packet.type = SR_DF_ANALOG; - packet.payload = &analog; - sr_session_send(cb_data, &packet); - g_slist_free(analog.channels); - } else { - logic.length = len; - logic.unitsize = 2; - logic.data = devc->buffer; - packet.type = SR_DF_LOGIC; - packet.payload = &logic; - sr_session_send(cb_data, &packet); - } - if (devc->num_block_read == devc->num_block_bytes) { - sr_dbg("Block has been completed"); - if (devc->model->series->protocol >= PROTOCOL_V3) { - /* Discard the terminating linefeed */ - sr_scpi_read_data(scpi, (char *)devc->buffer, 1); - } - 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"); + if (devc->format == FORMAT_IEEE488_2) { + sr_dbg("New block header expected"); + 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; } - devc->num_block_read = 0; + /* At slow timebases in live capture the DS2072 + * sometimes returns "short" data blocks, with + * apparently no way to get the rest of the data. + * Discard these, the complete data block will + * appear eventually. + */ + if (devc->data_source == DATA_SOURCE_LIVE + && (unsigned)len < expected_data_bytes) { + sr_dbg("Discarding short data block"); + sr_scpi_read_data(scpi, (char *)devc->buffer, len + 1); + return TRUE; + } + devc->num_block_bytes = len; } else { - sr_dbg("%d of %d block bytes read", devc->num_block_read, devc->num_block_bytes); + devc->num_block_bytes = expected_data_bytes; } + devc->num_block_read = 0; + } - devc->num_channel_bytes += len; + len = devc->num_block_bytes - devc->num_block_read; + if (len > ACQ_BUFFER_SIZE) + len = ACQ_BUFFER_SIZE; + sr_dbg("Requesting read of %d bytes", len); - if (devc->num_channel_bytes < expected_data_bytes) - /* Don't have the full data for this channel yet, re-run. */ - return TRUE; + 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; + } - /* End of data for this channel. */ + sr_dbg("Received %d bytes.", len); + + devc->num_block_read += len; + + 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.channels = g_slist_append(NULL, ch); + analog.num_samples = len; + analog.data = devc->data; + analog.mq = SR_MQ_VOLTAGE; + analog.unit = SR_UNIT_VOLT; + analog.mqflags = 0; + packet.type = SR_DF_ANALOG_OLD; + packet.payload = &analog; + sr_session_send(cb_data, &packet); + g_slist_free(analog.channels); + } else { + logic.length = len; + // TODO: For the MSO1000Z series, we need a way to express that + // this data is in fact just for a single channel, with the valid + // data for that channel in the LSB of each byte. + logic.unitsize = devc->model->series->protocol == PROTOCOL_V4 ? 1 : 2; + logic.data = devc->buffer; + packet.type = SR_DF_LOGIC; + packet.payload = &logic; + sr_session_send(cb_data, &packet); + } + + if (devc->num_block_read == devc->num_block_bytes) { + sr_dbg("Block has been completed"); if (devc->model->series->protocol >= PROTOCOL_V3) { - /* Signal end of data download to scope */ + /* Discard the terminating linefeed */ + sr_scpi_read_data(scpi, (char *)devc->buffer, 1); + } + 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) - /* - * This causes a query error, without it switching - * to the next channel causes an error. Fun with - * firmware... - */ - rigol_ds_config_set(sdi, ":WAV:END"); + 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; + } + devc->num_block_read = 0; + } else { + sr_dbg("%" PRIu64 " of %" PRIu64 " block bytes read", + devc->num_block_read, devc->num_block_bytes); + } + + devc->num_channel_bytes += len; + + if (devc->num_channel_bytes < expected_data_bytes) + /* Don't have the full data for this channel yet, re-run. */ + return TRUE; + + /* End of data for this channel. */ + if (devc->model->series->protocol == PROTOCOL_V3) { + /* Signal end of data download to scope */ + if (devc->data_source != DATA_SOURCE_LIVE) + /* + * This causes a query error, without it switching + * to the next channel causes an error. Fun with + * firmware... + */ + rigol_ds_config_set(sdi, ":WAV:END"); + } + + if (devc->channel_entry->next) { + /* We got the frame for this channel, now get the next channel. */ + devc->channel_entry = devc->channel_entry->next; + rigol_ds_channel_start(sdi); + } else { + /* Done with this frame. */ + packet.type = SR_DF_FRAME_END; + sr_session_send(cb_data, &packet); - 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. */ - devc->channel_entry = devc->channel_entry->next; - rigol_ds_channel_start(sdi); + if (++devc->num_frames == devc->limit_frames) { + /* Last frame, stop capture. */ + sdi->driver->dev_acquisition_stop(sdi, cb_data); } else { - /* Done with all analog channels in this frame. */ - 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_channels; - rigol_ds_channel_start(sdi); - } else { - /* Done with this frame. */ - packet.type = SR_DF_FRAME_END; - sr_session_send(cb_data, &packet); + /* Get the next frame, starting with the first channel. */ + devc->channel_entry = devc->enabled_channels; - if (++devc->num_frames == devc->limit_frames) { - /* Last frame, stop capture. */ - sdi->driver->dev_acquisition_stop(sdi, cb_data); - } else { - /* Get the next frame, starting with the first analog channel. */ - if (devc->enabled_analog_channels) - devc->channel_entry = devc->enabled_analog_channels; - else - devc->channel_entry = devc->enabled_digital_channels; - - rigol_ds_capture_start(sdi); - - /* Start of next frame. */ - packet.type = SR_DF_FRAME_BEGIN; - sr_session_send(cb_data, &packet); - } - } + rigol_ds_capture_start(sdi); + + /* Start of next frame. */ + packet.type = SR_DF_FRAME_BEGIN; + sr_session_send(cb_data, &packet); } } @@ -723,13 +785,17 @@ SR_PRIV int rigol_ds_get_dev_cfg(const struct sr_dev_inst *sdi) /* Digital channel state. */ if (devc->model->has_digital) { - if (sr_scpi_get_bool(sdi->conn, ":LA:DISP?", + if (sr_scpi_get_bool(sdi->conn, + devc->model->series->protocol >= PROTOCOL_V4 ? + ":LA:STAT?" : ":LA:DISP?", &devc->la_enabled) != SR_OK) return SR_ERR; sr_dbg("Logic analyzer %s, current digital channel state:", devc->la_enabled ? "enabled" : "disabled"); for (i = 0; i < ARRAY_SIZE(devc->digital_channels); i++) { - cmd = g_strdup_printf(":DIG%d:TURN?", i); + cmd = g_strdup_printf( + devc->model->series->protocol >= PROTOCOL_V4 ? + ":LA:DIG%d:DISP?" : ":DIG%d:TURN?", i); res = sr_scpi_get_bool(sdi->conn, cmd, &devc->digital_channels[i]); g_free(cmd); if (res != SR_OK) @@ -755,18 +821,6 @@ SR_PRIV int rigol_ds_get_dev_cfg(const struct sr_dev_inst *sdi) for (i = 0; i < devc->model->analog_channels; i++) sr_dbg("CH%d %g", i + 1, devc->vdiv[i]); - sr_dbg("Current vertical reference:"); - 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 (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; - sr_dbg("CH%d %d", i + 1, devc->vert_reference[i]); - } - } - /* Vertical offset. */ for (i = 0; i < devc->model->analog_channels; i++) { cmd = g_strdup_printf(":CHAN%d:OFFS?", i + 1);