Build-a-Driver #3: Temp Calcs

Here's the third patch in the Build-a-Driver series for MCP9808.
(If this post seems out of context, please refer to "Build-a-Driver Introduction" post.)

mcp9808: this version adds support for temperature calculations

In the last patch we returned the contents of the devices temp register in_temp_raw. Now, we need to present things in a suitable fashion for IIO-land.

So, rather than returning the entire register, we will return the actual temperature bits. We throw out the 3 MSB's, and use the remaining bits. These are in 2's compliment format so we'll do a sign_extend32 to get the integer value to return via in_temp_raw.

Now, I might just divide by 16 and get close enough, but alas, a wise mentor pointed me to the resolution register. We will set the scale based on the current resolution.

For example:
    if Register == 0xc192, then the sign_extended value is 402.
    AND since the integration time for this measurement is 0.0625

    we see:
    /sys/bus/iio/devices/iio:device0$ cat in_temp_raw in_temp_scale
    403
    0.062500000

    403 * 0.0625 = 25.1875 degrees Celsius

If you've been following, you know I hadn't implemented the user selectable resolutions yet. And, I still didn't really need to. The device has a power-on default resolution of .0625. I saved that in the device private global data - which is where I will keep it long term. Of course, this limits the testing, so I will have to test the calculations after the next patch.

To implement this, I saved the poweron default resolution during probe, added the IIO_CHAN_INFO_SCALE mask to my IIO_TEMP channel, and updated read_raw to do the sign_extend32 on the raw temperature data and to use the saved integration time(aka resolution) for the scale.

This version mcp9808-calc.c
To view complete revision history go here.

Labels: ,