Import into DevLib.
[bertos.git] / drv / ntc.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2004, 2005 Develer S.r.l. (http://www.de+veler.com/)
5  * All Rights Reserved.
6  * -->
7  *
8  * \brief Driver for NTC (reads a temperature through an ADC)
9  *
10  * \version $Id$
11  *
12  * \author Giovanni Bajo <rasky@develer.com>
13  * \author Francesco Sacchi <batt@develer.com>
14  *
15  *
16  * This module handles an external NTC bound to an AD converter. As usual,
17  * it relies on a low-level API (ntc_hw_*) (see below):
18  *
19  */
20
21 /*#*
22  *#* $Log$
23  *#* Revision 1.1  2005/11/04 17:59:47  bernie
24  *#* Import into DevLib.
25  *#*
26  *#* Revision 1.1  2005/05/24 09:17:58  batt
27  *#* Move drivers to top-level.
28  *#*/
29
30 #include <drv/ntc.h>
31 #include <hw_ntc.h>
32 #include <ntc_map.h>
33
34 #include <cfg/debug.h>
35
36 DB(bool ntc_initialized;)
37
38 /*!
39  * Find in a table of values \a orig_table of size \a size, the index which
40  * value is less or equal to \a val.
41  *
42  * \retval 0 When \a val is higher than the first table entry.
43  * \retval size When \a val is lower than the last table entry.
44  * \retval 1..size-1 When \a val is within the table.
45  */
46 static size_t upper_bound(const res_t *orig_table, size_t size, res_t val)
47 {
48         const res_t *table = orig_table;
49
50         while (size)
51         {
52                 size_t pos = size / 2;
53                 if (val > table[pos])
54                         size = pos;
55                 else
56                 {
57                         table += pos+1;
58                         size -= pos+1;
59                 }
60         }
61
62         return table - orig_table;
63 }
64
65
66 /*!
67  * Read the temperature for the NTC channel \a dev.
68  * First read the resistence of the NTC through ntc_hw_read(), then,
69  * for the conversion from resistance to temperature, since the formula
70  * varies from device to device, we implemented a generic system using
71  * a table of data which maps temperature (index) to resistance (data).
72  * The range of the table (min/max temperature) and the step
73  * (temperature difference between two consecutive elements of the table)
74  * is variable and can be specified. Notice that values inbetween the
75  * table elements are still possible as the library does a linear
76  * interpolation using the actual calculated resistance to find out
77  * the exact temperature.
78  *
79  * The low-level API provides a function to get access to a description
80  * of the NTC (ntc_hw_getInfo()), including the resistance table.
81  *
82  */
83 deg_t ntc_read(NtcDev dev)
84 {
85         const NtcHwInfo* hw = ntc_hw_getInfo(dev);
86         const res_t* r = hw->resistances;
87
88         float rx;
89         size_t i;
90         deg_t degrees;
91
92         rx = ntc_hw_read(dev);
93
94
95         i = upper_bound(r, hw->num_resistances, rx);
96         ASSERT(i <= hw->num_resistances);
97
98         if (i >= hw->num_resistances)
99                 return NTC_SHORT_CIRCUIT;
100         else if (i == 0)
101                 return NTC_OPEN_CIRCUIT;
102
103         /*
104          * Interpolated value in 0.1 degrees multiplied by 10:
105          *   delta t          step t
106          * ----------  = ----------------
107          * (rx - r[i])   (r[i-1] - r [i])
108          */
109         float tmp;
110         tmp = 10 * hw->degrees_step * (rx - r[i]) / (r[i - 1] - r[i]);
111
112         /*
113          * degrees = integer part corresponding to the superior index
114          *           in the table multiplied by 10
115          *           - decimal part interpolated (already multiplied by 10)
116          */
117         degrees = (i * hw->degrees_step + hw->degrees_min) * 10 - (int)(tmp);
118
119         //kprintf("dev= %d, I=%d, degrees = %d\n", dev, i , degrees);
120
121         return degrees;
122 }
123
124
125 /*!
126  * Init NTC hardware.
127  */
128 void ntc_init(void)
129 {
130         NTC_HW_INIT;
131         DB(ntc_initialized = true;)
132 }
133