Move AT91 SPI register definitions.
[bertos.git] / drv / ntc.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004, 2005 Develer S.r.l. (http://www.de+veler.com/)
30  * All Rights Reserved.
31  * -->
32  *
33  * \brief Driver for NTC (reads a temperature through an ADC)
34  *
35  * \version $Id$
36  *
37  * \author Giovanni Bajo <rasky@develer.com>
38  * \author Francesco Sacchi <batt@develer.com>
39  *
40  *
41  * This module handles an external NTC bound to an AD converter. As usual,
42  * it relies on a low-level API (ntc_hw_*) (see below):
43  *
44  */
45
46 /*#*
47  *#* $Log$
48  *#* Revision 1.2  2006/07/19 12:56:26  bernie
49  *#* Convert to new Doxygen style.
50  *#*
51  *#* Revision 1.1  2005/11/04 17:59:47  bernie
52  *#* Import into DevLib.
53  *#*
54  *#* Revision 1.1  2005/05/24 09:17:58  batt
55  *#* Move drivers to top-level.
56  *#*/
57
58 #include <drv/ntc.h>
59 #include <hw_ntc.h>
60 #include <ntc_map.h>
61
62 #include <cfg/debug.h>
63
64 DB(bool ntc_initialized;)
65
66 /**
67  * Find in a table of values \a orig_table of size \a size, the index which
68  * value is less or equal to \a val.
69  *
70  * \retval 0 When \a val is higher than the first table entry.
71  * \retval size When \a val is lower than the last table entry.
72  * \retval 1..size-1 When \a val is within the table.
73  */
74 static size_t upper_bound(const res_t *orig_table, size_t size, res_t val)
75 {
76         const res_t *table = orig_table;
77
78         while (size)
79         {
80                 size_t pos = size / 2;
81                 if (val > table[pos])
82                         size = pos;
83                 else
84                 {
85                         table += pos+1;
86                         size -= pos+1;
87                 }
88         }
89
90         return table - orig_table;
91 }
92
93
94 /**
95  * Read the temperature for the NTC channel \a dev.
96  * First read the resistence of the NTC through ntc_hw_read(), then,
97  * for the conversion from resistance to temperature, since the formula
98  * varies from device to device, we implemented a generic system using
99  * a table of data which maps temperature (index) to resistance (data).
100  * The range of the table (min/max temperature) and the step
101  * (temperature difference between two consecutive elements of the table)
102  * is variable and can be specified. Notice that values inbetween the
103  * table elements are still possible as the library does a linear
104  * interpolation using the actual calculated resistance to find out
105  * the exact temperature.
106  *
107  * The low-level API provides a function to get access to a description
108  * of the NTC (ntc_hw_getInfo()), including the resistance table.
109  *
110  */
111 deg_t ntc_read(NtcDev dev)
112 {
113         const NtcHwInfo* hw = ntc_hw_getInfo(dev);
114         const res_t* r = hw->resistances;
115
116         float rx;
117         size_t i;
118         deg_t degrees;
119
120         rx = ntc_hw_read(dev);
121
122
123         i = upper_bound(r, hw->num_resistances, rx);
124         ASSERT(i <= hw->num_resistances);
125
126         if (i >= hw->num_resistances)
127                 return NTC_SHORT_CIRCUIT;
128         else if (i == 0)
129                 return NTC_OPEN_CIRCUIT;
130
131         /*
132          * Interpolated value in 0.1 degrees multiplied by 10:
133          *   delta t          step t
134          * ----------  = ----------------
135          * (rx - r[i])   (r[i-1] - r [i])
136          */
137         float tmp;
138         tmp = 10 * hw->degrees_step * (rx - r[i]) / (r[i - 1] - r[i]);
139
140         /*
141          * degrees = integer part corresponding to the superior index
142          *           in the table multiplied by 10
143          *           - decimal part interpolated (already multiplied by 10)
144          */
145         degrees = (i * hw->degrees_step + hw->degrees_min) * 10 - (int)(tmp);
146
147         //kprintf("dev= %d, I=%d, degrees = %d\n", dev, i , degrees);
148
149         return degrees;
150 }
151
152
153 /**
154  * Init NTC hardware.
155  */
156 void ntc_init(void)
157 {
158         NTC_HW_INIT;
159         DB(ntc_initialized = true;)
160 }
161