From owner-freebsd-embedded@freebsd.org Thu Dec 1 10:11:18 2016 Return-Path: Delivered-To: freebsd-embedded@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id E171CC60291 for ; Thu, 1 Dec 2016 10:11:18 +0000 (UTC) (envelope-from Nick@ip-knowhow.com) Received: from EUR01-DB5-obe.outbound.protection.outlook.com (mail-db5eur01on0058.outbound.protection.outlook.com [104.47.2.58]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (Client CN "mail.protection.outlook.com", Issuer "Microsoft IT SSL SHA2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 37C991BA2 for ; Thu, 1 Dec 2016 10:11:13 +0000 (UTC) (envelope-from Nick@ip-knowhow.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipknowhow.onmicrosoft.com; s=selector1-ipknowhow-com0e; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=xGxsFRzG9PPxdd5oALQn2w03FbSwySeGCnpd1OHIjpQ=; b=QVk883KkR9+dxH2+WzQmYmPcE6ZF1+20yBAvyacleIUm26vXNAh1/Src7Sp2RxGAsMdrKWNmZL0CCXir2YsHAQY5tQZgbt1iH+31DHfH1+IbO8xvVDjkVQncMnjFdV3eacL5TPlOCxqfxMTLRcPaLzUDVnm8RiJcpissW47MiLw= Received: from HE1PR0802MB2458.eurprd08.prod.outlook.com (10.175.34.143) by HE1PR0802MB2457.eurprd08.prod.outlook.com (10.175.34.142) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.747.13; Thu, 1 Dec 2016 10:11:04 +0000 Received: from HE1PR0802MB2458.eurprd08.prod.outlook.com ([10.175.34.143]) by HE1PR0802MB2458.eurprd08.prod.outlook.com ([10.175.34.143]) with mapi id 15.01.0747.013; Thu, 1 Dec 2016 10:11:04 +0000 From: Nick Hibma To: =?Windows-1252?Q?=93FreeBSD_Embedded_Mailing_List=94?= Subject: Questions about i2c.c (TMP102 temperature sensor) Thread-Topic: Questions about i2c.c (TMP102 temperature sensor) Thread-Index: AQHSS7s5GnN+nzulRkSSDxfz+B+vWg== Date: Thu, 1 Dec 2016 10:11:04 +0000 Message-ID: <9424D7FD-C4B6-43D5-A0C5-76D5BE9ED1DE@ip-knowhow.com> Accept-Language: en-US, nl-NL Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Nick@ip-knowhow.com; x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [2001:980:530a:1:94e6:5d13:d5ff:61b2] x-ms-office365-filtering-correlation-id: f176cdfb-9cd2-471c-d46e-08d419d25c5d x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001); SRVR:HE1PR0802MB2457; x-microsoft-exchange-diagnostics: 1; HE1PR0802MB2457; 7:CDtvRUEjwScqjkAqREve+2YbvI8U89AYKr02dDm6BZd/AP1EFCUiHFO+2IojViiMuF6cVWuSMRDrPspKSkOyv7sbZUJbhoAOfMdAuChZs5b7f0FffBljMSIrZxxoK4QAlKkKtnxQ78kyvxM7lBNyU7vzCFNIaY3HrzojmAgEm2qkd+/L2fDNa1Yesvz5uu5wj6ekXn8L6IOdznR4CEbYN/g/zHw/4MijrlkE2U82KcKGOF5xsCYaoin32vDqriVVptuhIDLk0l+xVeahjLqILk1voWIbQQ+t9Nly2t7zK5vT8+ThlyDu52LscVy1L8SxWzqcSbUYLIcKFw5fxoXFfu3M1mVE9uuh6yvMh1MNaAvAC4BbuPO/SaQ+Mo9r6NoG2k4ESlhXJGSirgvQ6fstWZRrFNT/DrkW7MFJFC89PCz3tNxJlWxCl9sCSPuz/5BRN0o02J2scjixZyO70tMF6A== x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:; x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(6040375)(601004)(2401047)(5005006)(8121501046)(3002001)(10201501046)(6041248)(2016111802025)(20161123562025)(20161123555025)(20161123564025)(20161123560025)(6043046)(6072148); SRVR:HE1PR0802MB2457; BCL:0; PCL:0; RULEID:; SRVR:HE1PR0802MB2457; x-forefront-prvs: 014304E855 x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(7916002)(189002)(199003)(2900100001)(2906002)(7736002)(50986999)(189998001)(82746002)(6116002)(3660700001)(83716003)(3280700002)(97736004)(38730400001)(6506003)(68736007)(606004)(8936002)(106116001)(102836003)(8676002)(6916009)(6512003)(80792005)(7846002)(81156014)(6486002)(39450400002)(81166006)(122556002)(36756003)(101416001)(110136003)(92566002)(77096006)(105586002)(107886002)(86362001)(39410400001)(33656002)(5660300001)(106356001)(54356999)(104396002); DIR:OUT; SFP:1101; SCL:1; SRVR:HE1PR0802MB2457; H:HE1PR0802MB2458.eurprd08.prod.outlook.com; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; received-spf: None (protection.outlook.com: ip-knowhow.com does not designate permitted sender hosts) spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: ip-knowhow.com X-MS-Exchange-CrossTenant-originalarrivaltime: 01 Dec 2016 10:11:04.8461 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 0ba30fa5-b42a-4534-8b5e-dc21f65040ad X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR0802MB2457 Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable X-Content-Filtered-By: Mailman/MimeDel 2.1.23 X-BeenThere: freebsd-embedded@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Dedicated and Embedded Systems List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 01 Dec 2016 10:11:19 -0000 Gents, I am not quite sure who to owner of i2c.c is, so perhaps some I2C enthousia= st could help me with the following problem. I was trying to access a TMP102 I2C temperature sensor. It kind of works wi= th the default i2c utility. But it returns twice the high byte instead of t= he high and low byte for the temperature. Probably because it does 1 byte r= eads on the I2C bus, sending a stop condition after every byte. This was ve= rified with a logic analyser. The device expects continuous reads and no stop/start in between the 2 byte= s. Trying all options i2c, most notably the -m mode switch, yields the same= results all the time. Looking at the code in i2c.c I get confused as it s= eems to somehow fiddle with start/stop to get it right because of the -m sw= itch, but uses read() in the end to get the data, not I2CREAD. fd =3D open(dev, O_RDWR); if (i2c_opt.width) { error =3D ioctl(fd, I2CSTART, &cmd); error =3D ioctl(fd, I2CWRITE, &cmd); if (i2c_opt.mode =3D=3D I2C_MODE_STOP_START) { error =3D ioctl(fd, I2CSTOP, &cmd); } } if (i2c_opt.mode =3D=3D I2C_MODE_STOP_START) { error =3D ioctl(fd, I2CSTART, &cmd); } else if (i2c_opt.mode =3D=3D I2C_MODE_REPEATED_START) { error =3D ioctl(fd, I2CRPTSTART, &cmd); } /******** Without i2c_opt.width set there is no START condition either to s= et the slave address for the read() ****/ /******** Why this stop? ***/ error =3D ioctl(fd, I2CSTOP, &cmd); for (i =3D 0; i < i2c_opt.count; i++) { error =3D read(fd, &i2c_buf[i], 1); } close(fd); I would have expected: fd =3D open(dev, O_RDWR); if (i2c_opt.width) { error =3D ioctl(fd, I2CSTART, &cmd); error =3D ioctl(fd, I2CWRITE, &cmd); if (i2c_opt.mode =3D=3D I2C_MODE_STOP_START) { error =3D ioctl(fd, I2CSTOP, &cmd); error =3D ioctl(fd, I2CSTART, &cmd); } else if (i2c_opt.mode =3D=3D I2C_MODE_REPEATED_START) { error =3D ioctl(fd, I2CRPTSTART, &cmd); } } else { /***** Use START/STOP to set the slave address for the read() below ***/ error =3D ioctl(fd, I2CSTART, &cmd); error =3D ioctl(fd, I2CSTOP, &cmd); } error =3D ioctl(fd, I2CREAD, &cmd); // read all bytes in one go error =3D ioctl(fd, I2CSTOP, &cmd); close(fd); Any opinions on where I am wrong? Second question: Rolling my own little program with this change fails because I cannot get I= 2CREAD to work. It keeps returning EIO. Some other web pages claim that the= re is an implementation problem with that. Using read() on the fd works fin= e, so do get a valid temperature out of the device now. Cheers, Nick Hibma -- ip-knowhow E: nick@ip-knowhow.com T: +31 (0)85 2010923 M: +31 (0)6 14433161