dec27c161a2b05a413498c75106432cf9ed6e6a0
[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.2  2006/07/19 12:56:26  bernie
24  *#* Convert to new Doxygen style.
25  *#*
26  *#* Revision 1.1  2005/11/04 17:59:47  bernie
27  *#* Import into DevLib.
28  *#*
29  *#* Revision 1.1  2005/05/24 09:17:58  batt
30  *#* Move drivers to top-level.
31  *#*/
32
33 #include <drv/ntc.h>
34 #include <hw_ntc.h>
35 #include <ntc_map.h>
36
37 #include <cfg/debug.h>
38
39 DB(bool ntc_initialized;)
40
41 /**
42  * Find in a table of values \a orig_table of size \a size, the index which
43  * value is less or equal to \a val.
44  *
45  * \retval 0 When \a val is higher than the first table entry.
46  * \retval size When \a val is lower than the last table entry.
47  * \retval 1..size-1 When \a val is within the table.
48  */
49 static size_t upper_bound(const res_t *orig_table, size_t size, res_t val)
50 {
51         const res_t *table = orig_table;
52
53         while (size)
54         {
55                 size_t pos = size / 2;
56                 if (val > table[pos])
57                         size = pos;
58                 else
59                 {
60                         table += pos+1;
61                         size -= pos+1;
62                 }
63         }
64
65         return table - orig_table;
66 }
67
68
69 /**
70  * Read the temperature for the NTC channel \a dev.
71  * First read the resistence of the NTC through ntc_hw_read(), then,
72  * for the conversion from resistance to temperature, since the formula
73  * varies from device to device, we implemented a generic system using
74  * a table of data which maps temperature (index) to resistance (data).
75  * The range of the table (min/max temperature) and the step
76  * (temperature difference between two consecutive elements of the table)
77  * is variable and can be specified. Notice that values inbetween the
78  * table elements are still possible as the library does a linear
79  * interpolation using the actual calculated resistance to find out
80  * the exact temperature.
81  *
82  * The low-level API provides a function to get access to a description
83  * of the NTC (ntc_hw_getInfo()), including the resistance table.
84  *
85  */
86 deg_t ntc_read(NtcDev dev)
87 {
88         const NtcHwInfo* hw = ntc_hw_getInfo(dev);
89         const res_t* r = hw->resistances;
90
91         float rx;
92         size_t i;
93         deg_t degrees;
94
95         rx = ntc_hw_read(dev);
96
97
98         i = upper_bound(r, hw->num_resistances, rx);
99         ASSERT(i <= hw->num_resistances);
100
101         if (i >= hw->num_resistances)
102                 return NTC_SHORT_CIRCUIT;
103         else if (i == 0)
104                 return NTC_OPEN_CIRCUIT;
105
106         /*
107          * Interpolated value in 0.1 degrees multiplied by 10:
108          *   delta t          step t
109          * ----------  = ----------------
110          * (rx - r[i])   (r[i-1] - r [i])
111          */
112         float tmp;
113         tmp = 10 * hw->degrees_step * (rx - r[i]) / (r[i - 1] - r[i]);
114
115         /*
116          * degrees = integer part corresponding to the superior index
117          *           in the table multiplied by 10
118          *           - decimal part interpolated (already multiplied by 10)
119          */
120         degrees = (i * hw->degrees_step + hw->degrees_min) * 10 - (int)(tmp);
121
122         //kprintf("dev= %d, I=%d, degrees = %d\n", dev, i , degrees);
123
124         return degrees;
125 }
126
127
128 /**
129  * Init NTC hardware.
130  */
131 void ntc_init(void)
132 {
133         NTC_HW_INIT;
134         DB(ntc_initialized = true;)
135 }
136