From: Gerhard Sittig Date: Sun, 14 Aug 2022 19:07:07 +0000 (+0200) Subject: ols: move sigrok channel creation from protocol.c to api.c X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=fbf9e657ac72c16ec4931065f87fa7a37279261c;p=libsigrok.git ols: move sigrok channel creation from protocol.c to api.c It's unexpected that several code paths redundantly create sigrok channels, and that most of them reside in the protocol.c source file. Concentrate channel creation in api.c instead in the scan() routine, to remain aware during maintenance. Fallback to the maximum channel count for SUMP compatibles, prefer metadata gathered from the device when available, including quirks for models which are known to deviate. Just setup the number during probe, and create the channels at the end of scan() in common code. Change the channel count struct member to unsigned while we are here. Shorten a long comment line to not have to break it. --- diff --git a/src/hardware/openbench-logic-sniffer/api.c b/src/hardware/openbench-logic-sniffer/api.c index 35f6be7e..6e06f7e5 100644 --- a/src/hardware/openbench-logic-sniffer/api.c +++ b/src/hardware/openbench-logic-sniffer/api.c @@ -103,6 +103,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) const char *conn, *serialcomm; char buf[4] = { 0, 0, 0, 0 }; struct dev_context *devc; + size_t ch_max; conn = serialcomm = NULL; for (l = options; l; l = l->next) { @@ -185,7 +186,9 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) /* * Definitely using the OLS protocol, check if it supports - * the metadata command. + * the metadata command. Otherwise assign generic values. + * Create as many sigrok channels as was determined when + * the device was probed. */ send_shortcommand(serial, CMD_METADATA); g_usleep(RESPONSE_DELAY_US); @@ -193,14 +196,19 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) /* Got metadata. */ (void)ols_get_metadata(sdi); } else { - /* Not an OLS -- some other board that uses the sump protocol. */ + /* Not an OLS -- some other board using the SUMP protocol. */ sr_info("Device does not support metadata."); sdi->vendor = g_strdup("Sump"); sdi->model = g_strdup("Logic Analyzer"); sdi->version = g_strdup("v1.0"); - for (i = 0; i < ARRAY_SIZE(ols_channel_names); i++) - sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, - ols_channel_names[i]); + devc->max_channels = ARRAY_SIZE(ols_channel_names); + } + ch_max = ARRAY_SIZE(ols_channel_names); + if (devc->max_channels && ch_max > devc->max_channels) + ch_max = devc->max_channels; + for (i = 0; i < ch_max; i++) { + sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, + ols_channel_names[i]); } /* Configure samplerate and divider. */ if (ols_set_samplerate(sdi, DEFAULT_SAMPLERATE) != SR_OK) diff --git a/src/hardware/openbench-logic-sniffer/protocol.c b/src/hardware/openbench-logic-sniffer/protocol.c index 2bcf9f41..528b05ce 100644 --- a/src/hardware/openbench-logic-sniffer/protocol.c +++ b/src/hardware/openbench-logic-sniffer/protocol.c @@ -139,18 +139,6 @@ static int convert_trigger(const struct sr_dev_inst *sdi, return SR_OK; } -static void ols_channel_new(struct sr_dev_inst *sdi, int num_chan) -{ - struct dev_context *devc = sdi->priv; - int i; - - for (i = 0; i < num_chan; i++) - sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, - ols_channel_names[i]); - - devc->max_channels = num_chan; -} - static void ols_metadata_quirks(struct sr_dev_inst *sdi) { struct dev_context *devc; @@ -165,7 +153,7 @@ static void ols_metadata_quirks(struct sr_dev_inst *sdi) is_shrimp = sdi->model && strcmp(sdi->model, "Shrimp1.0") == 0; if (is_shrimp) { if (!devc->max_channels) - ols_channel_new(sdi, 4); + devc->max_channels = 4; if (!devc->max_samples) devc->max_samples = 256 * 1024; if (!devc->max_samplerate) @@ -252,7 +240,7 @@ SR_PRIV int ols_get_metadata(struct sr_dev_inst *sdi) switch (key) { case METADATA_TOKEN_NUM_PROBES_LONG: /* Number of usable channels */ - ols_channel_new(sdi, tmp_int); + devc->max_channels = tmp_int; break; case METADATA_TOKEN_SAMPLE_MEMORY_BYTES: /* Amount of sample memory available (bytes) */ @@ -286,7 +274,7 @@ SR_PRIV int ols_get_metadata(struct sr_dev_inst *sdi) switch (key) { case METADATA_TOKEN_NUM_PROBES_SHORT: /* Number of usable channels */ - ols_channel_new(sdi, tmp_c); + devc->max_channels = tmp_c; break; case METADATA_TOKEN_PROTOCOL_VERSION_SHORT: /* protocol version */ diff --git a/src/hardware/openbench-logic-sniffer/protocol.h b/src/hardware/openbench-logic-sniffer/protocol.h index 2506c4a9..96917743 100644 --- a/src/hardware/openbench-logic-sniffer/protocol.h +++ b/src/hardware/openbench-logic-sniffer/protocol.h @@ -100,7 +100,7 @@ struct dev_context { /* constant device properties: */ - int max_channels; + size_t max_channels; uint32_t max_samples; uint32_t max_samplerate; uint32_t protocol_version;