Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933080AbbFERba (ORCPT ); Fri, 5 Jun 2015 13:31:30 -0400 Received: from smtp81.iad3a.emailsrvr.com ([173.203.187.81]:51874 "EHLO smtp81.iad3a.emailsrvr.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933022AbbFERbV (ORCPT ); Fri, 5 Jun 2015 13:31:21 -0400 X-Sender-Id: abbotti@mev.co.uk From: Ian Abbott To: Cc: Greg Kroah-Hartman , Ian Abbott , H Hartley Sweeten , Subject: [PATCH 31/32] staging: comedi: das08: clarify sign-magnitude conversion Date: Fri, 5 Jun 2015 18:30:34 +0100 Message-Id: <1433525435-12986-32-git-send-email-abbotti@mev.co.uk> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1433525435-12986-1-git-send-email-abbotti@mev.co.uk> References: <1433525435-12986-1-git-send-email-abbotti@mev.co.uk> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2563 Lines: 61 For DAS08/JR/16 and DAS08/JR/AO-16 boards, the 16-bit analog input readings are assumed to be in a sign-magnitude format and need converting to the COMEDI unsigned sample format. The expressions to do the conversion look a little messy. Use a local variable `magnitude` to make it easier to follow. Also, there seems to be some discrepancy between the manual for these boards and the COMEDI code. The manual implies that 0 is full-scale negative and 65535 is full-scale positive. However, the COMEDI code has used the sign-magnitude conversion for these boards since these two boards were initially supported by a patch from an external contributor to the COMEDI project back in 2001. Assume the code is correct for now, but add a comment to mention the discrepancy. Signed-off-by: Ian Abbott --- drivers/staging/comedi/drivers/das08.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/drivers/staging/comedi/drivers/das08.c b/drivers/staging/comedi/drivers/das08.c index f74c4b0..6c9bd10 100644 --- a/drivers/staging/comedi/drivers/das08.c +++ b/drivers/staging/comedi/drivers/das08.c @@ -228,11 +228,26 @@ static int das08_ai_insn_read(struct comedi_device *dev, } else if (thisboard->ai_encoding == das08_pcm_encode12) { data[n] = (msb << 8) + lsb; } else if (thisboard->ai_encoding == das08_encode16) { - /* FPOS 16-bit boards are sign-magnitude */ + /* + * "JR" 16-bit boards are sign-magnitude. + * + * XXX The manual seems to imply that 0 is full-scale + * negative and 65535 is full-scale positive, but the + * original COMEDI patch to add support for the + * DAS08/JR/16 and DAS08/JR/16-AO boards have it + * encoded as sign-magnitude. Assume the original + * COMEDI code is correct for now. + */ + unsigned int magnitude = lsb | ((msb & 0x7f) << 8); + + /* + * MSB bit 7 is 0 for negative, 1 for positive voltage. + * COMEDI 16-bit bipolar data value for 0V is 0x8000. + */ if (msb & 0x80) - data[n] = (1 << 15) | lsb | ((msb & 0x7f) << 8); + data[n] = (1 << 15) + magnitude; else - data[n] = (1 << 15) - (lsb | (msb & 0x7f) << 8); + data[n] = (1 << 15) - magnitude; } else { dev_err(dev->class_dev, "bug! unknown ai encoding\n"); return -1; -- 2.1.4 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/