Specific the directory for all hw and cfg module. Use double quote for cfg and hw...
[bertos.git] / bertos / 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 #include "hw/hw_ntc.h"
47 #include "hw/ntc_map.h"
48
49 #include <cfg/debug.h>
50
51 #include <drv/ntc.h>
52
53 DB(bool ntc_initialized;)
54
55 /**
56  * Find in a table of values \a orig_table of size \a size, the index which
57  * value is less or equal to \a val.
58  *
59  * \retval 0 When \a val is higher than the first table entry.
60  * \retval size When \a val is lower than the last table entry.
61  * \retval 1..size-1 When \a val is within the table.
62  */
63 static size_t upper_bound(const res_t *orig_table, size_t size, res_t val)
64 {
65         const res_t *table = orig_table;
66
67         while (size)
68         {
69                 size_t pos = size / 2;
70                 if (val > table[pos])
71                         size = pos;
72                 else
73                 {
74                         table += pos+1;
75                         size -= pos+1;
76                 }
77         }
78
79         return table - orig_table;
80 }
81
82
83 /**
84  * Read the temperature for the NTC channel \a dev.
85  * First read the resistence of the NTC through ntc_hw_read(), then,
86  * for the conversion from resistance to temperature, since the formula
87  * varies from device to device, we implemented a generic system using
88  * a table of data which maps temperature (index) to resistance (data).
89  * The range of the table (min/max temperature) and the step
90  * (temperature difference between two consecutive elements of the table)
91  * is variable and can be specified. Notice that values inbetween the
92  * table elements are still possible as the library does a linear
93  * interpolation using the actual calculated resistance to find out
94  * the exact temperature.
95  *
96  * The low-level API provides a function to get access to a description
97  * of the NTC (ntc_hw_getInfo()), including the resistance table.
98  *
99  */
100 deg_t ntc_read(NtcDev dev)
101 {
102         const NtcHwInfo* hw = ntc_hw_getInfo(dev);
103         const res_t* r = hw->resistances;
104
105         float rx;
106         size_t i;
107         deg_t degrees;
108
109         rx = ntc_hw_read(dev);
110
111
112         i = upper_bound(r, hw->num_resistances, rx);
113         ASSERT(i <= hw->num_resistances);
114
115         if (i >= hw->num_resistances)
116                 return NTC_SHORT_CIRCUIT;
117         else if (i == 0)
118                 return NTC_OPEN_CIRCUIT;
119
120         /*
121          * Interpolated value in 0.1 degrees multiplied by 10:
122          *   delta t          step t
123          * ----------  = ----------------
124          * (rx - r[i])   (r[i-1] - r [i])
125          */
126         float tmp;
127         tmp = 10 * hw->degrees_step * (rx - r[i]) / (r[i - 1] - r[i]);
128
129         /*
130          * degrees = integer part corresponding to the superior index
131          *           in the table multiplied by 10
132          *           - decimal part interpolated (already multiplied by 10)
133          */
134         degrees = (i * hw->degrees_step + hw->degrees_min) * 10 - (int)(tmp);
135
136         //kprintf("dev= %d, I=%d, degrees = %d\n", dev, i , degrees);
137
138         return degrees;
139 }
140
141
142 /**
143  * Init NTC hardware.
144  */
145 void ntc_init(void)
146 {
147         NTC_HW_INIT;
148         DB(ntc_initialized = true;)
149 }
150