Skip to content

Commit 0c0eada

Browse files
bigguinessgregkh
authored andcommitted
staging: comedi: dmm32at: introduce dmm32_ai_get_sample()
Introduce a helper function to read the two's complement analog input sample from the hardware and munge it to the offset binary (unsigned) format that comedi expects. Use the comedi_offset_munge() helper to munge the data. Use the new helper in the analog input (*insn_read) and in the interrupt handler for the async command. Signed-off-by: H Hartley Sweeten <[email protected]> Reviewed-by: Ian Abbott <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 933fca1 commit 0c0eada

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

drivers/staging/comedi/drivers/dmm32at.c

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@ struct dmm32at_private {
156156
unsigned char dio_config;
157157
};
158158

159+
static unsigned int dmm32at_ai_get_sample(struct comedi_device *dev,
160+
struct comedi_subdevice *s)
161+
{
162+
unsigned int val;
163+
164+
val = inb(dev->iobase + DMM32AT_AILSB);
165+
val |= (inb(dev->iobase + DMM32AT_AIMSB) << 8);
166+
167+
/* munge two's complement value to offset binary */
168+
return comedi_offset_munge(s, val);
169+
}
170+
159171
static int dmm32at_ai_status(struct comedi_device *dev,
160172
struct comedi_subdevice *s,
161173
struct comedi_insn *insn,
@@ -174,8 +186,6 @@ static int dmm32at_ai_rinsn(struct comedi_device *dev,
174186
struct comedi_insn *insn, unsigned int *data)
175187
{
176188
int n;
177-
unsigned int d;
178-
unsigned short msb, lsb;
179189
unsigned char chan;
180190
int range;
181191
int ret;
@@ -210,19 +220,7 @@ static int dmm32at_ai_rinsn(struct comedi_device *dev,
210220
if (ret)
211221
return ret;
212222

213-
/* read data */
214-
lsb = inb(dev->iobase + DMM32AT_AILSB);
215-
msb = inb(dev->iobase + DMM32AT_AIMSB);
216-
217-
/* invert sign bit to make range unsigned, this is an
218-
idiosyncrasy of the diamond board, it return
219-
conversions as a signed value, i.e. -32768 to
220-
32767, flipping the bit and interpreting it as
221-
signed gives you a range of 0 to 65535 which is
222-
used by comedi */
223-
d = ((msb ^ 0x0080) << 8) + lsb;
224-
225-
data[n] = d;
223+
data[n] = dmm32at_ai_get_sample(dev, s);
226224
}
227225

228226
/* return the number of samples read/written */
@@ -465,8 +463,7 @@ static irqreturn_t dmm32at_isr(int irq, void *d)
465463
{
466464
struct comedi_device *dev = d;
467465
unsigned char intstat;
468-
unsigned int samp;
469-
unsigned short msb, lsb;
466+
unsigned int val;
470467
int i;
471468

472469
if (!dev->attached) {
@@ -481,13 +478,8 @@ static irqreturn_t dmm32at_isr(int irq, void *d)
481478
struct comedi_cmd *cmd = &s->async->cmd;
482479

483480
for (i = 0; i < cmd->chanlist_len; i++) {
484-
/* read data */
485-
lsb = inb(dev->iobase + DMM32AT_AILSB);
486-
msb = inb(dev->iobase + DMM32AT_AIMSB);
487-
488-
/* invert sign bit to make range unsigned */
489-
samp = ((msb ^ 0x0080) << 8) + lsb;
490-
comedi_buf_write_samples(s, &samp, 1);
481+
val = dmm32at_ai_get_sample(dev, s);
482+
comedi_buf_write_samples(s, &val, 1);
491483
}
492484

493485
if (cmd->stop_src == TRIG_COUNT &&

0 commit comments

Comments
 (0)