Port rigol-ds driver to use common SCPI functions.

This commit is contained in:
Martin Ling 2013-12-03 23:19:40 +00:00
parent a1ff9c1897
commit ae1bc1cc26
2 changed files with 61 additions and 80 deletions

View File

@ -217,73 +217,60 @@ static int probe_port(const char *port, GSList **devices)
{ {
struct dev_context *devc; struct dev_context *devc;
struct sr_dev_inst *sdi; struct sr_dev_inst *sdi;
struct sr_usbtmc_dev_inst *usbtmc; struct sr_scpi_dev_inst *scpi;
struct sr_scpi_hw_info *hw_info;
struct sr_probe *probe; struct sr_probe *probe;
unsigned int i; unsigned int i;
int len, num_tokens;
const char *manufacturer, *model_name, *version;
const struct rigol_ds_model *model = NULL; const struct rigol_ds_model *model = NULL;
char buf[256]; gchar *channel_name;
gchar **tokens, *channel_name;
*devices = NULL; *devices = NULL;
if (!(usbtmc = sr_usbtmc_dev_inst_new(port))) if (!(scpi = scpi_usbtmc_dev_inst_new(port)))
return SR_ERR_MALLOC; return SR_ERR_MALLOC;
if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0) if (sr_scpi_open(scpi) != SR_OK) {
sr_scpi_free(scpi);
return SR_ERR; return SR_ERR;
len = write(usbtmc->fd, "*IDN?", 5); };
len = read(usbtmc->fd, buf, sizeof(buf));
if (close(usbtmc->fd) < 0) if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
sr_info("Couldn't get IDN response.");
sr_scpi_close(scpi);
sr_scpi_free(scpi);
return SR_ERR; return SR_ERR;
sr_usbtmc_dev_inst_free(usbtmc);
if (len == 0)
return SR_ERR_NA;
buf[len] = 0;
tokens = g_strsplit(buf, ",", 0);
sr_dbg("response: %s [%s]", port, buf);
for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
if (num_tokens < 4) {
g_strfreev(tokens);
return SR_ERR_NA;
} }
manufacturer = tokens[0]; if (strcasecmp(hw_info->manufacturer, "Rigol Technologies")) {
model_name = tokens[1]; sr_scpi_hw_info_free(hw_info);
version = tokens[3]; sr_scpi_free(scpi);
if (strcasecmp(manufacturer, "Rigol Technologies")) {
g_strfreev(tokens);
return SR_ERR_NA; return SR_ERR_NA;
} }
for (i = 0; i < ARRAY_SIZE(supported_models); i++) { for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
if (!strcmp(model_name, supported_models[i].name)) { if (!strcmp(hw_info->model, supported_models[i].name)) {
model = &supported_models[i]; model = &supported_models[i];
break; break;
} }
} }
if (!model || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, if (!model || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
manufacturer, model_name, version))) { hw_info->manufacturer, hw_info->model,
g_strfreev(tokens); hw_info->firmware_version))) {
sr_scpi_hw_info_free(hw_info);
sr_scpi_free(scpi);
return SR_ERR_NA; return SR_ERR_NA;
} }
g_strfreev(tokens); sr_scpi_hw_info_free(hw_info);
sdi->conn = scpi;
if (!(sdi->conn = sr_usbtmc_dev_inst_new(port)))
return SR_ERR_MALLOC;
sdi->driver = di; sdi->driver = di;
sdi->inst_type = SR_INST_USBTMC; sdi->inst_type = SR_INST_SCPI;
if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
return SR_ERR_MALLOC; return SR_ERR_MALLOC;
devc->limit_frames = 0; devc->limit_frames = 0;
devc->model = model; devc->model = model;
@ -399,9 +386,9 @@ static GSList *dev_list(void)
static int dev_open(struct sr_dev_inst *sdi) static int dev_open(struct sr_dev_inst *sdi)
{ {
struct sr_usbtmc_dev_inst *usbtmc = sdi->conn; struct sr_scpi_dev_inst *scpi = sdi->conn;
if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0) if (sr_scpi_open(scpi) < 0)
return SR_ERR; return SR_ERR;
if (rigol_ds_get_dev_cfg(sdi) != SR_OK) if (rigol_ds_get_dev_cfg(sdi) != SR_OK)
@ -414,12 +401,13 @@ static int dev_open(struct sr_dev_inst *sdi)
static int dev_close(struct sr_dev_inst *sdi) static int dev_close(struct sr_dev_inst *sdi)
{ {
struct sr_usbtmc_dev_inst *usbtmc; struct sr_scpi_dev_inst *scpi;
usbtmc = sdi->conn; scpi = sdi->conn;
if (usbtmc && usbtmc->fd != -1) {
close(usbtmc->fd); if (scpi) {
usbtmc->fd = -1; if (sr_scpi_close(scpi) < 0)
return SR_ERR;
sdi->status = SR_ST_INACTIVE; sdi->status = SR_ST_INACTIVE;
} }
@ -739,7 +727,7 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
{ {
struct sr_usbtmc_dev_inst *usbtmc; struct sr_scpi_dev_inst *scpi;
struct dev_context *devc; struct dev_context *devc;
struct sr_probe *probe; struct sr_probe *probe;
GSList *l; GSList *l;
@ -748,7 +736,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
if (sdi->status != SR_ST_ACTIVE) if (sdi->status != SR_ST_ACTIVE)
return SR_ERR_DEV_CLOSED; return SR_ERR_DEV_CLOSED;
usbtmc = sdi->conn; scpi = sdi->conn;
devc = sdi->priv; devc = sdi->priv;
if (devc->data_source == DATA_SOURCE_LIVE) { if (devc->data_source == DATA_SOURCE_LIVE) {
@ -794,7 +782,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
if (!devc->enabled_analog_probes && !devc->enabled_digital_probes) if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
return SR_ERR; return SR_ERR;
sr_source_add(usbtmc->fd, G_IO_IN, 50, rigol_ds_receive, (void *)sdi); sr_scpi_source_add(scpi, G_IO_IN, 50, rigol_ds_receive, (void *)sdi);
/* Send header packet to the session bus. */ /* Send header packet to the session bus. */
std_session_send_df_header(cb_data, LOG_PREFIX); std_session_send_df_header(cb_data, LOG_PREFIX);
@ -845,7 +833,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
{ {
struct dev_context *devc; struct dev_context *devc;
struct sr_usbtmc_dev_inst *usbtmc; struct sr_scpi_dev_inst *scpi;
(void)cb_data; (void)cb_data;
@ -860,8 +848,8 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
g_slist_free(devc->enabled_digital_probes); g_slist_free(devc->enabled_digital_probes);
devc->enabled_analog_probes = NULL; devc->enabled_analog_probes = NULL;
devc->enabled_digital_probes = NULL; devc->enabled_digital_probes = NULL;
usbtmc = sdi->conn; scpi = sdi->conn;
sr_source_remove(usbtmc->fd); sr_scpi_source_remove(scpi);
return SR_OK; return SR_OK;
} }

View File

@ -346,13 +346,13 @@ SR_PRIV int rigol_ds_channel_start(const struct sr_dev_inst *sdi)
} }
/* Read the header of a data block */ /* Read the header of a data block */
static int rigol_ds_read_header(struct sr_usbtmc_dev_inst *usbtmc) static int rigol_ds_read_header(struct sr_scpi_dev_inst *scpi)
{ {
char start[3], length[10]; char start[3], length[10];
int len, tmp; int len, tmp;
/* Read the hashsign and length digit. */ /* Read the hashsign and length digit. */
tmp = read(usbtmc->fd, start, 2); tmp = sr_scpi_read(scpi, start, 2);
start[2] = '\0'; start[2] = '\0';
if (tmp != 2) if (tmp != 2)
{ {
@ -367,7 +367,7 @@ static int rigol_ds_read_header(struct sr_usbtmc_dev_inst *usbtmc)
len = atoi(start + 1); len = atoi(start + 1);
/* Read the data length. */ /* Read the data length. */
tmp = read(usbtmc->fd, length, len); tmp = sr_scpi_read(scpi, length, len);
length[len] = '\0'; length[len] = '\0';
if (tmp != len) if (tmp != len)
{ {
@ -388,7 +388,7 @@ static int rigol_ds_read_header(struct sr_usbtmc_dev_inst *usbtmc)
SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data) SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
{ {
struct sr_dev_inst *sdi; struct sr_dev_inst *sdi;
struct sr_usbtmc_dev_inst *usbtmc; struct sr_scpi_dev_inst *scpi;
struct dev_context *devc; struct dev_context *devc;
struct sr_datafeed_packet packet; struct sr_datafeed_packet packet;
struct sr_datafeed_analog analog; struct sr_datafeed_analog analog;
@ -405,7 +405,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
if (!(devc = sdi->priv)) if (!(devc = sdi->priv))
return TRUE; return TRUE;
usbtmc = sdi->conn; scpi = sdi->conn;
if (revents == G_IO_IN) { if (revents == G_IO_IN) {
if (devc->model->protocol == PROTOCOL_IEEE488_2) { if (devc->model->protocol == PROTOCOL_IEEE488_2) {
@ -442,7 +442,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
sr_dbg("New block header expected"); sr_dbg("New block header expected");
if (rigol_ds_send(sdi, ":WAV:DATA?") != SR_OK) if (rigol_ds_send(sdi, ":WAV:DATA?") != SR_OK)
return TRUE; return TRUE;
len = rigol_ds_read_header(usbtmc); len = rigol_ds_read_header(scpi);
if (len == -1) if (len == -1)
return TRUE; return TRUE;
/* At slow timebases in live capture the DS2072 /* At slow timebases in live capture the DS2072
@ -454,7 +454,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
if (devc->data_source == DATA_SOURCE_LIVE if (devc->data_source == DATA_SOURCE_LIVE
&& (unsigned)len < devc->num_frame_bytes) { && (unsigned)len < devc->num_frame_bytes) {
sr_dbg("Discarding short data block"); sr_dbg("Discarding short data block");
read(usbtmc->fd, devc->buffer, len + 1); sr_scpi_read(scpi, (char *)devc->buffer, len + 1);
return TRUE; return TRUE;
} }
devc->num_block_bytes = len; devc->num_block_bytes = len;
@ -465,12 +465,13 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
probe = devc->channel_frame; probe = devc->channel_frame;
if (devc->model->protocol == PROTOCOL_IEEE488_2) { if (devc->model->protocol == PROTOCOL_IEEE488_2) {
len = devc->num_block_bytes - devc->num_block_read; len = devc->num_block_bytes - devc->num_block_read;
len = read(usbtmc->fd, devc->buffer, len = sr_scpi_read(scpi, (char *)devc->buffer,
len < ACQ_BUFFER_SIZE ? len : ACQ_BUFFER_SIZE); len < ACQ_BUFFER_SIZE ? len : ACQ_BUFFER_SIZE);
} else { } else {
waveform_size = probe->type == SR_PROBE_ANALOG ? waveform_size = probe->type == SR_PROBE_ANALOG ?
DS1000_ANALOG_LIVE_WAVEFORM_SIZE : DIGITAL_WAVEFORM_SIZE; DS1000_ANALOG_LIVE_WAVEFORM_SIZE : DIGITAL_WAVEFORM_SIZE;
len = read(usbtmc->fd, devc->buffer, waveform_size - devc->num_frame_bytes); len = sr_scpi_read(scpi, (char *)devc->buffer,
waveform_size - devc->num_frame_bytes);
} }
sr_dbg("Received %d bytes.", len); sr_dbg("Received %d bytes.", len);
if (len == -1) if (len == -1)
@ -510,7 +511,7 @@ SR_PRIV int rigol_ds_receive(int fd, int revents, void *cb_data)
sr_dbg("Block has been completed"); sr_dbg("Block has been completed");
/* Discard the terminating linefeed and prepare for /* Discard the terminating linefeed and prepare for
possible next block */ possible next block */
read(usbtmc->fd, devc->buffer, 1); sr_scpi_read(scpi, (char *)devc->buffer, 1);
devc->num_block_bytes = 0; devc->num_block_bytes = 0;
if (devc->data_source != DATA_SOURCE_LIVE) if (devc->data_source != DATA_SOURCE_LIVE)
rigol_ds_set_wait_event(devc, WAIT_BLOCK); rigol_ds_set_wait_event(devc, WAIT_BLOCK);
@ -610,39 +611,31 @@ SR_PRIV int rigol_ds_send(const struct sr_dev_inst *sdi, const char *format, ...
{ {
va_list args; va_list args;
char buf[256]; char buf[256];
int len, out, ret; struct sr_scpi_dev_inst *scpi = sdi->conn;
struct sr_usbtmc_dev_inst *usbtmc = sdi->conn;
va_start(args, format); va_start(args, format);
len = vsnprintf(buf, 255, format, args); vsnprintf(buf, 255, format, args);
va_end(args); va_end(args);
strcat(buf, "\n");
len++;
out = write(usbtmc->fd, buf, len);
buf[len - 1] = '\0';
if (out != len) {
sr_dbg("Only sent %d/%d bytes of '%s'.", out, len, buf);
ret = SR_ERR;
} else {
sr_spew("Sent '%s'.", buf);
ret = SR_OK;
}
return ret; return sr_scpi_send(scpi, buf);
} }
static int get_cfg(const struct sr_dev_inst *sdi, char *cmd, char *reply, size_t maxlen) static int get_cfg(const struct sr_dev_inst *sdi, char *cmd, char *reply, size_t maxlen)
{ {
int len; int len;
struct dev_context *devc = sdi->priv; struct dev_context *devc = sdi->priv;
struct sr_usbtmc_dev_inst *usbtmc = sdi->conn; struct sr_scpi_dev_inst *scpi = sdi->conn;
char *response;
if (rigol_ds_send(sdi, cmd) != SR_OK) if (sr_scpi_send(scpi, cmd) != SR_OK)
return SR_ERR; return SR_ERR;
if ((len = read(usbtmc->fd, reply, maxlen - 1)) < 0) if (sr_scpi_receive(scpi, &response) != SR_OK)
return SR_ERR; return SR_ERR;
reply[len] = '\0';
g_strlcpy(reply, response, maxlen);
g_free(response);
len = strlen(reply);
if (devc->model->protocol == PROTOCOL_IEEE488_2) { if (devc->model->protocol == PROTOCOL_IEEE488_2) {
/* get rid of trailing linefeed */ /* get rid of trailing linefeed */