From: magnuskarlsson Date: Thu, 5 Jun 2014 21:28:50 +0000 (-0700) Subject: DEMUX and RLE fixes X-Git-Tag: libsigrok-0.4.0~1123 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=b94cff407f6a260e4ae916137da54f7602f3d050;p=libsigrok.git DEMUX and RLE fixes --- diff --git a/src/hardware/pipistrello-ols/api.c b/src/hardware/pipistrello-ols/api.c index 5423a320..1a58d718 100644 --- a/src/hardware/pipistrello-ols/api.c +++ b/src/hardware/pipistrello-ols/api.c @@ -99,7 +99,7 @@ static GSList *scan(GSList *options) } /* Device-specific settings */ - devc->max_samples = devc->max_samplerate = devc->protocol_version = 0; + devc->max_samplebytes = devc->max_samplerate = devc->protocol_version = 0; /* Acquisition settings */ devc->limit_samples = devc->capture_ratio = 0; @@ -404,7 +404,7 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, devc = sdi->priv; if (devc->flag_reg & FLAG_RLE) return SR_ERR_NA; - if (devc->max_samples == 0) + if (devc->max_samplebytes == 0) /* Device didn't specify sample memory size in metadata. */ return SR_ERR_NA; /* @@ -423,8 +423,11 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, * divide by zero. */ break; } + /* 3 channel groups takes as many bytes as 4 channel groups */ + if (num_channels == 3) + num_channels = 4; grange[0] = g_variant_new_uint64(MIN_NUM_SAMPLES); - grange[1] = g_variant_new_uint64(devc->max_samples / num_channels); + grange[1] = g_variant_new_uint64(devc->max_samplebytes / num_channels); *data = g_variant_new_tuple(grange, 2); break; default: @@ -518,7 +521,8 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, struct dev_context *devc; uint32_t samplecount, readcount, delaycount; uint8_t changrp_mask, arg[4]; - int num_channels; + uint16_t flag_tmp; + int num_channels, samplespercount; int ret, i; if (sdi->status != SR_ST_ACTIVE) @@ -544,6 +548,11 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, num_channels++; } } + /* 3 channel groups takes as many bytes as 4 channel groups */ + if (num_channels == 3) + num_channels = 4; + /* maximum number of samples (or RLE counts) the buffer memory can hold */ + devc->max_samples = devc->max_samplebytes / num_channels; /* * Limit readcount to prevent reading past the end of the hardware @@ -551,19 +560,28 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, */ sr_dbg("max_samples = %d", devc->max_samples); sr_dbg("limit_samples = %d", devc->limit_samples); - samplecount = MIN(devc->max_samples / num_channels, devc->limit_samples); - readcount = samplecount / 4; + samplecount = MIN(devc->max_samples, devc->limit_samples); sr_dbg("Samplecount = %d", samplecount); + /* In demux mode the OLS is processing two samples per clock */ + if (devc->flag_reg & FLAG_DEMUX) { + samplespercount = 8; + } + else { + samplespercount = 4; + } + + readcount = samplecount / samplespercount; + /* Rather read too many samples than too few. */ - if (samplecount % 4 != 0) + if (samplecount % samplespercount != 0) readcount++; /* Basic triggers. */ if (devc->trigger_mask[0] != 0x00000000) { /* At least one channel has a trigger on it. */ delaycount = readcount * (1 - devc->capture_ratio / 100.0); - devc->trigger_at = (readcount - delaycount) * 4 - devc->num_stages; + devc->trigger_at = (readcount - delaycount) * samplespercount - devc->num_stages; for (i = 0; i <= devc->num_stages; i++) { sr_dbg("Setting stage %d trigger.", i); if ((ret = set_trigger(sdi, i)) != SR_OK) @@ -607,10 +625,26 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, devc->flag_reg & FLAG_FILTER ? "on": "off", devc->flag_reg & FLAG_DEMUX ? "on" : "off"); - /* 1 means "disable channel". */ + /* + * Enable/disable OLS channel groups in the flag register according + * to the channel mask. 1 means "disable channel". + */ + devc->flag_reg &= ~0x3c; devc->flag_reg |= ~(changrp_mask << 2) & 0x3c; - arg[0] = devc->flag_reg & 0xff; - arg[1] = devc->flag_reg >> 8; + sr_dbg("flag_reg = %x", devc->flag_reg); + + /* + * In demux mode the OLS is processing two 8-bit or 16-bit samples + * in parallel and for this to work the lower two bits of the four + * "channel_disable" bits must be replicated to the upper two bits. + */ + flag_tmp = devc->flag_reg; + if (devc->flag_reg & FLAG_DEMUX) { + flag_tmp &= ~0x30; + flag_tmp |= ~(changrp_mask << 4) & 0x30; + } + arg[0] = flag_tmp & 0xff; + arg[1] = flag_tmp >> 8; arg[2] = arg[3] = 0x00; if (write_longcommand(devc, CMD_SET_FLAGS, arg) != SR_OK) return SR_ERR; diff --git a/src/hardware/pipistrello-ols/protocol.c b/src/hardware/pipistrello-ols/protocol.c index bef76fba..45ffad00 100644 --- a/src/hardware/pipistrello-ols/protocol.c +++ b/src/hardware/pipistrello-ols/protocol.c @@ -291,7 +291,7 @@ SR_PRIV struct sr_dev_inst *p_ols_get_metadata(uint8_t *buf, int bytes_read, str break; case 0x01: /* Amount of sample memory available (bytes) */ - devc->max_samples = tmp_int; + devc->max_samplebytes = tmp_int; break; case 0x02: /* Amount of dynamic memory available (bytes) */ @@ -415,7 +415,7 @@ SR_PRIV int p_ols_receive_data(int fd, int revents, void *cb_data) memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4); } - if (devc->num_samples < devc->limit_samples) { + if ((devc->num_samples < devc->limit_samples) && (devc->cnt_samples < devc->max_samples)) { num_channels = 0; for (i = NUM_CHANNELS; i > 0x02; i /= 2) { @@ -423,6 +423,7 @@ SR_PRIV int p_ols_receive_data(int fd, int revents, void *cb_data) num_channels++; } } + sr_dbg("num_channels = %d", num_channels); /* Get a block of data. */ bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE); @@ -446,40 +447,44 @@ SR_PRIV int p_ols_receive_data(int fd, int revents, void *cb_data) devc->sample[devc->num_bytes++] = byte; sr_spew("Received byte 0x%.2x.", byte); - if (devc->num_bytes == num_channels) { - devc->cnt_samples++; - devc->cnt_samples_rle++; - /* - * Got a full sample. Convert from the OLS's little-endian - * sample to the local format. - */ - sample = devc->sample[0] | (devc->sample[1] << 8) \ - | (devc->sample[2] << 16) | (devc->sample[3] << 24); - sr_spew("Received sample 0x%.*x.", devc->num_bytes * 2, sample); - if (devc->flag_reg & FLAG_RLE) { + + if ((devc->flag_reg & FLAG_DEMUX) && (devc->flag_reg & FLAG_RLE)) { + /* RLE in demux mode must be processed differently + * since in this case the RLE encoder is operating on pairs of samples. + */ + if (devc->num_bytes == num_channels * 2) { + devc->cnt_samples += 2; + devc->cnt_samples_rle += 2; /* - * In RLE mode the high bit of the sample is the - * "count" flag, meaning this sample is the number - * of times the previous sample occurred. + * Got a sample pair. Convert from the OLS's little-endian + * sample to the local format. + */ + sample = devc->sample[0] | (devc->sample[1] << 8) \ + | (devc->sample[2] << 16) | (devc->sample[3] << 24); + sr_spew("Received sample pair 0x%.*x.", devc->num_bytes * 2, sample); + + /* + * In RLE mode the high bit of the sample pair is the + * "count" flag, meaning this sample pair is the number + * of times the previous sample pair occurred. */ if (devc->sample[devc->num_bytes - 1] & 0x80) { /* Clear the high bit. */ sample &= ~(0x80 << (devc->num_bytes - 1) * 8); devc->rle_count = sample; - devc->cnt_samples_rle += devc->rle_count; - sr_dbg("RLE count: %u.", devc->rle_count); + devc->cnt_samples_rle += devc->rle_count * 2; + sr_dbg("RLE count: %u.", devc->rle_count * 2); devc->num_bytes = 0; continue; } - } - devc->num_samples += devc->rle_count + 1; - if (devc->num_samples > devc->limit_samples) { - /* Save us from overrunning the buffer. */ - devc->rle_count -= devc->num_samples - devc->limit_samples; - devc->num_samples = devc->limit_samples; - } + devc->num_samples += (devc->rle_count + 1) * 2; + if (devc->num_samples > devc->limit_samples) { + /* Save us from overrunning the buffer. */ + devc->rle_count -= (devc->num_samples - devc->limit_samples) / 2; + devc->num_samples = devc->limit_samples; + index = bytes_read; + } - if (num_channels < 4) { /* * Some channel groups may have been turned * off, to speed up transfer between the @@ -490,8 +495,9 @@ SR_PRIV int p_ols_receive_data(int fd, int revents, void *cb_data) * the number of channels. */ j = 0; + /* expand first sample */ memset(devc->tmp_sample, 0, 4); - for (i = 0; i < 4; i++) { + for (i = 0; i < 2; i++) { if (((devc->flag_reg >> 2) & (1 << i)) == 0) { /* * This channel group was @@ -499,32 +505,127 @@ SR_PRIV int p_ols_receive_data(int fd, int revents, void *cb_data) * sample. */ devc->tmp_sample[i] = devc->sample[j++]; - } else if (devc->flag_reg & FLAG_DEMUX && (i > 2)) { - /* group 2 & 3 get added to 0 & 1 */ - devc->tmp_sample[i - 2] = devc->sample[j++]; - } + } } - memcpy(devc->sample, devc->tmp_sample, 4); - sr_spew("Expanded sample: 0x%.8x.", sample); + /* Clear out the most significant bit of the sample */ + devc->tmp_sample[devc->num_bytes - 1] &= 0x7f; + sr_spew("Expanded sample 1: 0x%.8x.", devc->tmp_sample); + + /* expand second sample */ + memset(devc->tmp_sample2, 0, 4); + for (i = 0; i < 2; i++) { + if (((devc->flag_reg >> 2) & (1 << i)) == 0) { + /* + * This channel group was + * enabled, copy from received + * sample. + */ + devc->tmp_sample2[i] = devc->sample[j++]; + } + } + /* Clear out the most significant bit of the sample */ + devc->tmp_sample2[devc->num_bytes - 1] &= 0x7f; + sr_spew("Expanded sample 2: 0x%.8x.", devc->tmp_sample2); + + /* + * OLS sends its sample buffer backwards. + * store it in reverse order here, so we can dump + * this on the session bus later. + */ + offset = (devc->limit_samples - devc->num_samples) * 4; + for (i = 0; i <= devc->rle_count; i++) { + memcpy(devc->raw_sample_buf + offset + (i * 8), + devc->tmp_sample2, 4); + memcpy(devc->raw_sample_buf + offset + (4 + (i * 8)), + devc->tmp_sample, 4); + } + memset(devc->sample, 0, 4); + devc->num_bytes = 0; + devc->rle_count = 0; } + } + else { + if (devc->num_bytes == num_channels) { + devc->cnt_samples++; + devc->cnt_samples_rle++; + /* + * Got a full sample. Convert from the OLS's little-endian + * sample to the local format. + */ + sample = devc->sample[0] | (devc->sample[1] << 8) \ + | (devc->sample[2] << 16) | (devc->sample[3] << 24); + sr_spew("Received sample 0x%.*x.", devc->num_bytes * 2, sample); + if (devc->flag_reg & FLAG_RLE) { + /* + * In RLE mode the high bit of the sample is the + * "count" flag, meaning this sample is the number + * of times the previous sample occurred. + */ + if (devc->sample[devc->num_bytes - 1] & 0x80) { + /* Clear the high bit. */ + sample &= ~(0x80 << (devc->num_bytes - 1) * 8); + devc->rle_count = sample; + devc->cnt_samples_rle += devc->rle_count; + sr_dbg("RLE count: %u.", devc->rle_count); + devc->num_bytes = 0; + continue; + } + } + devc->num_samples += devc->rle_count + 1; + if (devc->num_samples > devc->limit_samples) { + /* Save us from overrunning the buffer. */ + devc->rle_count -= devc->num_samples - devc->limit_samples; + devc->num_samples = devc->limit_samples; + index = bytes_read; + } + + if (num_channels < 4) { + /* + * Some channel groups may have been turned + * off, to speed up transfer between the + * hardware and the PC. Expand that here before + * submitting it over the session bus -- + * whatever is listening on the bus will be + * expecting a full 32-bit sample, based on + * the number of channels. + */ + j = 0; + memset(devc->tmp_sample, 0, 4); + for (i = 0; i < 4; i++) { + if (((devc->flag_reg >> 2) & (1 << i)) == 0) { + /* + * This channel group was + * enabled, copy from received + * sample. + */ + devc->tmp_sample[i] = devc->sample[j++]; + } + } + memcpy(devc->sample, devc->tmp_sample, 4); + sr_spew("Expanded sample: 0x%.8x.", sample); + } - /* - * Pipistrello OLS sends its sample buffer backwards. - * store it in reverse order here, so we can dump - * this on the session bus later. - */ - offset = (devc->limit_samples - devc->num_samples) * 4; - for (i = 0; i <= devc->rle_count; i++) { - memcpy(devc->raw_sample_buf + offset + (i * 4), - devc->sample, 4); + /* + * Pipistrello OLS sends its sample buffer backwards. + * store it in reverse order here, so we can dump + * this on the session bus later. + */ + offset = (devc->limit_samples - devc->num_samples) * 4; + for (i = 0; i <= devc->rle_count; i++) { + memcpy(devc->raw_sample_buf + offset + (i * 4), + devc->sample, 4); + } + memset(devc->sample, 0, 4); + devc->num_bytes = 0; + devc->rle_count = 0; } - memset(devc->sample, 0, 4); - devc->num_bytes = 0; - devc->rle_count = 0; } } return TRUE; } else { + do bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE); + while (bytes_read > 0); + /* * We've acquired all the samples we asked for -- we're done. * Send the (properly-ordered) buffer to the frontend. diff --git a/src/hardware/pipistrello-ols/protocol.h b/src/hardware/pipistrello-ols/protocol.h index 819755ed..17d3948c 100644 --- a/src/hardware/pipistrello-ols/protocol.h +++ b/src/hardware/pipistrello-ols/protocol.h @@ -85,13 +85,14 @@ struct dev_context { /* Fixed device settings */ int max_channels; - uint32_t max_samples; + uint32_t max_samplebytes; uint32_t max_samplerate; uint32_t protocol_version; /* Acquisition settings */ uint64_t cur_samplerate; uint32_t cur_samplerate_divider; + uint32_t max_samples; uint64_t limit_samples; int capture_ratio; int trigger_at; @@ -107,13 +108,14 @@ struct dev_context { unsigned int num_samples; int num_bytes; int cnt_bytes; - int cnt_samples; + unsigned int cnt_samples; int cnt_samples_rle; /* Temporary variables */ unsigned int rle_count; unsigned char sample[4]; unsigned char tmp_sample[4]; + unsigned char tmp_sample2[4]; unsigned char *raw_sample_buf; };