aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging
diff options
context:
space:
mode:
authorIan Abbott <abbotti@mev.co.uk>2015-10-27 16:59:11 +0000
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2015-10-29 08:58:36 +0900
commit19e86985b08e2470aeb805f71d2e2ebf19bcdc95 (patch)
tree094399e88a5d62dc78f4a0483dd574c2ff68df9f /drivers/staging
parentstaging: comedi: comedi_test: reformat multi-line comments (diff)
downloadlinux-dev-19e86985b08e2470aeb805f71d2e2ebf19bcdc95.tar.xz
linux-dev-19e86985b08e2470aeb805f71d2e2ebf19bcdc95.zip
staging: comedi: comedi_test: saturate fake waveform values
While an asynchronous command is running on the analog input subdevice, fake waveform generators are connected to each channel to provide the input voltages that are converted to sample value. Channel 0 is connected to a sawtooth generator, channel 1 is connected to a squarewave generator, and the remaining channels are connected to a flatline generator. The non-flatline generators share the same amplitude (in microvolts) and period (in microseconds) which are configured when the COMEDI device is attached. All waveforms are centered around 0 microvolts and the non-flatline waveforms go between -amplitude and +amplitude. It is possible for the waveforms to swing outside the input range of the channels to which they are connected. When that happens, the sample values resulting from simulated A-to-D conversion will wrap around due to integer overflow. Prevent that by clamping the sample values that would go out of range. This is closer to how a real hardware device would behave (assuming the input voltage is not high enough to damage the hardware!). Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/staging')
-rw-r--r--drivers/staging/comedi/drivers/comedi_test.c29
1 files changed, 23 insertions, 6 deletions
diff --git a/drivers/staging/comedi/drivers/comedi_test.c b/drivers/staging/comedi/drivers/comedi_test.c
index 899faf71e014..53c9a5cb5a98 100644
--- a/drivers/staging/comedi/drivers/comedi_test.c
+++ b/drivers/staging/comedi/drivers/comedi_test.c
@@ -104,9 +104,17 @@ static unsigned short fake_sawtooth(struct comedi_device *dev,
value = current_time;
value *= binary_amplitude * 2;
do_div(value, devpriv->usec_period);
- value -= binary_amplitude; /* get rid of sawtooth's dc offset */
+ value += offset;
+ /* get rid of sawtooth's dc offset and clamp value */
+ if (value < binary_amplitude) {
+ value = 0; /* negative saturation */
+ } else {
+ value -= binary_amplitude;
+ if (value > s->maxdata)
+ value = s->maxdata; /* positive saturation */
+ }
- return offset + value;
+ return value;
}
static unsigned short fake_squarewave(struct comedi_device *dev,
@@ -119,16 +127,25 @@ static unsigned short fake_squarewave(struct comedi_device *dev,
u64 value;
const struct comedi_krange *krange =
&s->range_table->range[range_index];
- current_time %= devpriv->usec_period;
+ current_time %= devpriv->usec_period;
value = s->maxdata;
value *= devpriv->uvolt_amplitude;
do_div(value, krange->max - krange->min);
- if (current_time < devpriv->usec_period / 2)
- value *= -1;
+ /* get one of two values for square-wave and clamp */
+ if (current_time < devpriv->usec_period / 2) {
+ if (offset < value)
+ value = 0; /* negative saturation */
+ else
+ value = offset - value;
+ } else {
+ value += offset;
+ if (value > s->maxdata)
+ value = s->maxdata; /* positive saturation */
+ }
- return offset + value;
+ return value;
}
static unsigned short fake_flatline(struct comedi_device *dev,