Skip to contentSkip to conversation

11-bit temperature problem

DISCUSSION Jean Delvare 2 participants 7 messages May 19, 2005

lore
Share & tools
lore
#0 lore raw

Hi all,

I'm in the process of writing a driver for the LM90 (we have at least two users waiting for it). I am now facing a problem related to 11-bit temperatures the LM90 handles for the remote temperature. The 11 bits are splitted over two different bytes. This means that reading from these two bytes in sequence could, in a few cases, read the high byte of a conversion and the low byte of another conversion. This is bad and unwanted. Other high precision chipsets (the LM75 comes to mind) don't have the problem because you read a word, so it all happens in a single operation and the above-mention case can't happen.

Here's what National Semiconductor say in the datasheet:

"When retrieving all 10 bits from a previous remote diode temperature measurement, the master must insure that all 10 bits are from the same temperature conversion. This may be achieved by using one-shot mode or by setting the conversion rate and monitoring the busy bit so that no conversion occurs in between reading the MSB and LSB of the last temperature conversion."

To me, both proposed solution are bad. In the first case, you'll have to wait for the full conversion each time you want to read the value. The I2C bus is slow enough, the hell if we want to wait even more each time we read a value. In the second case, I think it just doesn't work because reading the busy bit isn't different from reading the second temperature byte. Reading that the busy bit is set doesn't help us. What will we do? Read the first byte again, then busy again, until the busy bit isn't set? It is possible that the conversion occurs between us reading the first byte and us reading the busy flag, so we don't see the flag and read the second byte, although it doesn't match the fisrt byte. Or I am missing obvious?

Is there another driver that has the same kind of probleme, so that I can take a look at the proposed solution?

The solution I have come to is:

Read first byte, read second byte, read first byte again. If the new first byte matches the old one, it's ok, we have our reading (most cases). If not, read the second byte once again. If the conversion rate is low enough (and I'll make sure it is) the second try should not fail. The good point there is that we don't have to wait for any event nor monitor any flag. The bad point is a 50 to 100% overhead. Still I believe it's overall better than the other possibilities (if you can call them that way).

Note that if we can't get to a solution everyone is happy with, I'll plain *drop* support for the extra resolution registers. After all, what's the point in a 0.125 degree resolution when the sensor has a +/- 3 degrees accuracy? I just don't get it. Who on earth wants to know it's CPU temperature with a 0.125 degrees resolution anyway? And it makes the driver *much* more complicated.

Signature · 2 lines
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/
#1 lore raw
Quoted 22 lines · Jean Delvare

Hi all,

I'm in the process of writing a driver for the LM90 (we have at least two users waiting for it). I am now facing a problem related to 11-bit temperatures the LM90 handles for the remote temperature. The 11 bits are splitted over two different bytes. This means that reading from these two bytes in sequence could, in a few cases, read the high byte of a conversion and the low byte of another conversion. This is bad and unwanted. Other high precision chipsets (the LM75 comes to mind) don't have the problem because you read a word, so it all happens in a single operation and the above-mention case can't happen.

Here's what National Semiconductor say in the datasheet:

"When retrieving all 10 bits from a previous remote diode temperature measurement, the master must insure that all 10 bits are from the same temperature conversion. This may be achieved by using one-shot mode or by setting the conversion rate and monitoring the busy bit so that no conversion occurs in between reading the MSB and LSB of the last temperature conversion."

Quoted 3 lines

To me, both proposed solution are bad. In the first case, you'll have to wait for the full conversion each time you want to read the value. The I2C bus is slow enough, the hell if we want to wait even more each time

Why not fire off a one-shot conversion and wait for it to finish?

10kHz smbus speed (conservative - spec'ed minimum according to datasheet) 40 bits per read (smbus write + smbus read incl. start/stop/ack bits) 22 registers to read (conservative - probably need only 15)

So 31.25ms for the conversion + 88ms for the update => ~120ms. There's other overhead too but it can't be helped. This is pushing the limits of what people can notice, worst case.

But if you really must optimize it...

update() {
	start_conversion();
	read_non_volatile_regs();
	while (still_converting)
		i2c_delay(1);
	read_volatile_regs();
}
Quoted 23 lines

we read a value. In the second case, I think it just doesn't work because reading the busy bit isn't different from reading the second temperature byte. Reading that the busy bit is set doesn't help us. What will we do? Read the first byte again, then busy again, until the busy bit isn't set? It is possible that the conversion occurs between us reading the first byte and us reading the busy flag, so we don't see the flag and read the second byte, although it doesn't match the fisrt byte. Or I am missing obvious?

Is there another driver that has the same kind of probleme, so that I can take a look at the proposed solution?

The solution I have come to is:

Read first byte, read second byte, read first byte again. If the new first byte matches the old one, it's ok, we have our reading (most cases). If not, read the second byte once again. If the conversion rate is low enough (and I'll make sure it is) the second try should not fail. The good point there is that we don't have to wait for any event nor monitor any flag. The bad point is a 50 to 100% overhead. Still I believe it's overall better than the other possibilities (if you can call them that way).

Quoted 6 lines

Note that if we can't get to a solution everyone is happy with, I'll plain *drop* support for the extra resolution registers. After all, what's the point in a 0.125 degree resolution when the sensor has a +/- 3 degrees accuracy? I just don't get it. Who on earth wants to know it's CPU temperature with a 0.125 degrees resolution anyway? And it makes the driver *much* more complicated.

Useless precision is a pet peeve of mine, too. I was once asked to extend the std C lib struct tm to include a field for microseconds - as if there was some calculation that cared about months and uS at the same time... equivalent to 12 decimals of precision?!

Regards,

Signature · 2 lines
Mark M. Hoffman
[email protected]
#2 lore raw
In reply to Jean Delvare
Quoted 25 lines
Quoted 11 lines

"When retrieving all 10 bits from a previous remote diode temperature measurement, the master must insure that all 10 bits are from the same temperature conversion. This may be achieved by using one-shot mode or by setting the conversion rate and monitoring the busy bit so that no conversion occurs in between reading the MSB and LSB of the last temperature conversion."

To me, both proposed solution are bad. In the first case, you'll have to wait for the full conversion each time you want to read the value. The I2C bus is slow enough, the hell if we want to wait even more each time

Why not fire off a one-shot conversion and wait for it to finish?

10kHz smbus speed (conservative - spec'ed minimum according to datasheet) 40 bits per read (smbus write + smbus read incl. start/stop/ack bits) 22 registers to read (conservative - probably need only 15)

So 31.25ms for the conversion + 88ms for the update => ~120ms. There's other overhead too but it can't be helped. This is pushing the limits of what people can notice, worst case.

But if you really must optimize it...

Quoted 7 lines
update() {
	start_conversion();
	read_non_volatile_regs();
	while (still_converting)
		i2c_delay(1);
	read_volatile_regs();
}

That's interesting. I love it when discussing about things like that on the list make me realize I just missed some clever ideas. This proves how great teamwork is, I believe.

Two remarks. First, when you say "10kHz smbus speed, conservative", I don't quite agree. To me, the conservative value is the highest possible bus speed. This is where the one-shot conversion will increase the overall time more (relatively). So, let's consider a bus speed of 100kHz. How do you come to 40 bits? I count 29 (3 9-bit bytes, plus 1 start and 1 stop). There are 17 registers we want to update (3 volatile, 14 non-volatile). So the update time would be 4.93ms (4.06 non-volatile, 0.87 volatile). Now we can consider that 31.25 extra ms for the conversion is something significant, isn't it?

Of course you can object my reasonment is pointless, and I agree ;) We don't really care what takes time as long as the overall operation fits in a reasonable amount of time. But I don't like the idea much, while I would have if the conversion time was less than the non-volatile registers update.

BTW is there a way we can know at what frequency the bus really works?

Anyway, my main objection against one-shot mode is the hardware protection. The LM90 is supposed to have its ALERT output wired to the SMBus master, and its T_CRIT_A output wired to shutdown control. It is obviously meant for hardware temperature regulation and overtemperature shutdown. If we configure the LM90 in one-shot mode, it means that the status registers will only be updated when someone asks the driver to update. What if a user loads the driver and never use it? (I do that all the time.) Then the hardware protections will be disabled. I don't want to be held responsible for that!

Quoted 10 lines
Quoted 10 lines

The solution I have come to is:

Read first byte, read second byte, read first byte again. If the new first byte matches the old one, it's ok, we have our reading (most cases). If not, read the second byte once again. If the conversion rate is low enough (and I'll make sure it is) the second try should not fail. The good point there is that we don't have to wait for any event nor monitor any flag. The bad point is a 50 to 100% overhead. Still I believe it's overall better than the other possibilities (if you can call them that way).

Any objection to that? I expect this to make me read 18 (or 19) registers instead of 17, this ain't much of an overhead it seems.

Quoted 4 lines

Useless precision is a pet peeve of mine, too. I was once asked to extend the std C lib struct tm to include a field for microseconds - as if there was some calculation that cared about months and uS at the same time... equivalent to 12 decimals of precision?!

Yeah, usually it just means people don't know what they are doing (or that commercial arguments were put on the top of technical ones, which is the same as in the first case).

Thanks a lot for helping me think about all the possible solutions :)

Signature · 2 lines
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/
#3 lore raw
In reply to Jean Delvare
Quoted 6 lines · Jean Delvare

* Mark M. Hoffman <[email protected]>:

Quoted 5 lines

10kHz smbus speed (conservative - spec'ed minimum according to datasheet) 40 bits per read (smbus write + smbus read incl. start/stop/ack bits) 22 registers to read (conservative - probably need only 15)

<snip>

Quoted 5 lines

Two remarks. First, when you say "10kHz smbus speed, conservative", I don't quite agree. To me, the conservative value is the highest possible bus speed. This is where the one-shot conversion will increase the overall time more (relatively). So, let's consider a bus speed of 100kHz. How do you come to 40 bits? I count 29 (3 9-bit bytes, plus 1

According to the datasheet timing diagrams on p 11...

(a) is i2c_smbus_write_byte_data() ~29 bits
(b) is i2c_smbus_write_byte() ~20 bits
(c) is i2c_smbus_read_byte() ~20 bits

I.e. i2c_smbus_read_byte_data() is not supported by this chip, so you need to use (b) + (c) to read a register.

And I say ~40 instead of exactly 40 because there are limits to how close one can stack transactions (the bus driver/hw should take care of that).

Quoted 6 lines

start and 1 stop). There are 17 registers we want to update (3 volatile, 14 non-volatile). So the update time would be 4.93ms (4.06 non-volatile, 0.87 volatile). Now we can consider that 31.25 extra ms for the conversion is something significant, isn't it?

Of course you can object my reasonment is pointless, and I agree ;) We

Indeed. ;)

Quoted 6 lines

don't really care what takes time as long as the overall operation fits in a reasonable amount of time. But I don't like the idea much, while I would have if the conversion time was less than the non-volatile registers update.

BTW is there a way we can know at what frequency the bus really works?

The simplest correct answer is "no".

A more useful answer is "look at the bus datasheet".

But this is actually not a simple topic at all. Even if you know the nominal bus speed, the protocol allows for the master or slaves to "stretch" any single bit-time to arbitrary lengths (in theory). Of course in practice a master or slave will not wait forever; the amount of time they *will* wait is published. E.g. 10kHz minimum for LM90 implies that the master better not stretch a bit-time longer than 100uS.

If I managed to get you interested about what's "on the wire", look here:
http://www.semiconductors.philips.com/buses/i2c/

Quoted 9 lines

Anyway, my main objection against one-shot mode is the hardware protection. The LM90 is supposed to have its ALERT output wired to the SMBus master, and its T_CRIT_A output wired to shutdown control. It is obviously meant for hardware temperature regulation and overtemperature shutdown. If we configure the LM90 in one-shot mode, it means that the status registers will only be updated when someone asks the driver to update. What if a user loads the driver and never use it? (I do that all the time.) Then the hardware protections will be disabled. I don't want to be held responsible for that!

You're right - I withdraw my suggestion.

Quoted 11 lines
Quoted 10 lines
Quoted 10 lines

The solution I have come to is:

Read first byte, read second byte, read first byte again. If the new first byte matches the old one, it's ok, we have our reading (most cases). If not, read the second byte once again. If the conversion rate is low enough (and I'll make sure it is) the second try should not fail. The good point there is that we don't have to wait for any event nor monitor any flag. The bad point is a 50 to 100% overhead. Still I believe it's overall better than the other possibilities (if you can call them that way).

Quoted 2 lines

Any objection to that? I expect this to make me read 18 (or 19) registers instead of 17, this ain't much of an overhead it seems.

Looks good then.

<snip>

Quoted 1 line

Thanks a lot for helping me think about all the possible solutions :)

You're welcome; regards,

Signature · 2 lines
Mark M. Hoffman
[email protected]
#4 lore raw
In reply to Jean Delvare
Quoted 17 lines · Jean Delvare
Quoted 6 lines

* Mark M. Hoffman <[email protected]>:

Quoted 5 lines

10kHz smbus speed (conservative - spec'ed minimum according to datasheet) 40 bits per read (smbus write + smbus read incl. start/stop/ack bits) 22 registers to read (conservative - probably need only 15)

<snip>

Quoted 6 lines

Two remarks. First, when you say "10kHz smbus speed, conservative", I don't quite agree. To me, the conservative value is the highest possible bus speed. This is where the one-shot conversion will increase the overall time more (relatively). So, let's consider a bus speed of 100kHz. How do you come to 40 bits? I count 29 (3 9-bit bytes, plus 1

Quoted 8 lines

According to the datasheet timing diagrams on p 11...

(a) is i2c_smbus_write_byte_data() ~29 bits
(b) is i2c_smbus_write_byte() ~20 bits
(c) is i2c_smbus_read_byte() ~20 bits

I.e. i2c_smbus_read_byte_data() is not supported by this chip, so you need to use (b) + (c) to read a register.

Interesting, it seems you read the datasheet more carefully than I did ;) However, this part is exactly the same as for the LM83, and it happens that in my lm83 driver, I *do* use i2c_smbus_read_byte_data(), and it works. Since the LM90 is more recent than the LM83, I think it'll be the same here (and actuall some LM90 users have been using the lm83 driver so far, and it was working). So it seems that the read byte data mode is indeed supported by both chips. Or should I really change the code to match the datasheet? (I don't know if the chip could handle that mode but "not like it", for example.)

Anyway, thanks for pointing this out, I'll try to be more careful next time.

Quoted 3 lines

And I say ~40 instead of exactly 40 because there are limits to how close one can stack transactions (the bus driver/hw should take care of that).

Yes, I had understood that.

Quoted 15 lines
Quoted 2 lines

BTW is there a way we can know at what frequency the bus really works?

The simplest correct answer is "no".

A more useful answer is "look at the bus datasheet".

But this is actually not a simple topic at all. Even if you know the nominal bus speed, the protocol allows for the master or slaves to "stretch" any single bit-time to arbitrary lengths (in theory). Of course in practice a master or slave will not wait forever; the amount of time they *will* wait is published. E.g. 10kHz minimum for LM90 implies that the master better not stretch a bit-time longer than 100uS.

Quoted 2 lines

If I managed to get you interested about what's "on the wire", look
here: http://www.semiconductors.philips.com/buses/i2c/

Oh, I got that one printed already. Now I just need some time to actually read it, but I know I will spend much time in trains next week, so I should find some time there (when my laptop batteries die and I can't keep programming, for example).

Thanks again!

Signature · 2 lines
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/
#5 lore raw
In reply to Jean Delvare
Quoted 17 lines · Jean Delvare
Quoted 8 lines · Jean Delvare
Quoted 6 lines

Two remarks. First, when you say "10kHz smbus speed, conservative", I don't quite agree. To me, the conservative value is the highest possible bus speed. This is where the one-shot conversion will increase the overall time more (relatively). So, let's consider a bus speed of 100kHz. How do you come to 40 bits? I count 29 (3 9-bit bytes, plus 1

Quoted 8 lines

According to the datasheet timing diagrams on p 11...

(a) is i2c_smbus_write_byte_data() ~29 bits
(b) is i2c_smbus_write_byte() ~20 bits
(c) is i2c_smbus_read_byte() ~20 bits

I.e. i2c_smbus_read_byte_data() is not supported by this chip, so you need to use (b) + (c) to read a register.

Quoted 9 lines

Interesting, it seems you read the datasheet more carefully than I did ;) However, this part is exactly the same as for the LM83, and it happens that in my lm83 driver, I *do* use i2c_smbus_read_byte_data(), and it works. Since the LM90 is more recent than the LM83, I think it'll be the same here (and actuall some LM90 users have been using the lm83 driver so far, and it was working). So it seems that the read byte data mode is indeed supported by both chips. Or should I really change the code to match the datasheet? (I don't know if the chip could handle that mode but "not like it", for example.)

Gah! I'm sorry: (b) + (c) *is* i2c_smbus_read_byte_data() minus a stop bit. From our smbus protocol doc...

Quoted 7 lines

SMBus Read Byte Data
==========

This reads a single byte from a device, from a designated register. The register is specified through the Comm byte.

S Addr Wr [A] Comm [A] S Addr Rd [A] [Data] NA P

So, that makes 39 bits not 29.

Quoted 2 lines

Anyway, thanks for pointing this out, I'll try to be more careful next time.

Heh, me too.

Signature · 2 lines
Mark "goes to penalty box and feels shame" Hoffman
[email protected]
#6 lore raw
In reply to Jean Delvare
Quoted 2 lines

Gah! I'm sorry: (b) + (c) *is* i2c_smbus_read_byte_data() minus a stop bit.

Aha, OK, that's it. Didn't know that (there are sooo many things I still don't know, shame on me).

Quoted 9 lines

SMBus Read Byte Data
==========

This reads a single byte from a device, from a designated register. The register is specified through the Comm byte.

S Addr Wr [A] Comm [A] S Addr Rd [A] [Data] NA P

So, that makes 39 bits not 29.

You're perfectly right. But that doesn't matter anymore ;)

Signature · 2 lines
Jean "I swear I'll read the I2C specs soon" Delvare
http://www.ensicaen.ismra.fr/~delvare/