4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2004, 2005 Develer S.r.l. (http://www.de+veler.com/)
30 * All Rights Reserved.
33 * \brief Driver for NTC (reads a temperature through an ADC)
37 * \author Giovanni Bajo <rasky@develer.com>
38 * \author Francesco Sacchi <batt@develer.com>
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):
48 *#* Revision 1.2 2006/07/19 12:56:26 bernie
49 *#* Convert to new Doxygen style.
51 *#* Revision 1.1 2005/11/04 17:59:47 bernie
52 *#* Import into DevLib.
54 *#* Revision 1.1 2005/05/24 09:17:58 batt
55 *#* Move drivers to top-level.
62 #include <cfg/debug.h>
64 DB(bool ntc_initialized;)
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.
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.
74 static size_t upper_bound(const res_t *orig_table, size_t size, res_t val)
76 const res_t *table = orig_table;
80 size_t pos = size / 2;
90 return table - orig_table;
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.
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.
111 deg_t ntc_read(NtcDev dev)
113 const NtcHwInfo* hw = ntc_hw_getInfo(dev);
114 const res_t* r = hw->resistances;
120 rx = ntc_hw_read(dev);
123 i = upper_bound(r, hw->num_resistances, rx);
124 ASSERT(i <= hw->num_resistances);
126 if (i >= hw->num_resistances)
127 return NTC_SHORT_CIRCUIT;
129 return NTC_OPEN_CIRCUIT;
132 * Interpolated value in 0.1 degrees multiplied by 10:
134 * ---------- = ----------------
135 * (rx - r[i]) (r[i-1] - r [i])
138 tmp = 10 * hw->degrees_step * (rx - r[i]) / (r[i - 1] - r[i]);
141 * degrees = integer part corresponding to the superior index
142 * in the table multiplied by 10
143 * - decimal part interpolated (already multiplied by 10)
145 degrees = (i * hw->degrees_step + hw->degrees_min) * 10 - (int)(tmp);
147 //kprintf("dev= %d, I=%d, degrees = %d\n", dev, i , degrees);
159 DB(ntc_initialized = true;)