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)
35 * \author Giovanni Bajo <rasky@develer.com>
36 * \author Francesco Sacchi <batt@develer.com>
39 * This module handles an external NTC bound to an AD converter. As usual,
40 * it relies on a low-level API (ntc_hw_*) (see below):
44 #include "hw/hw_ntc.h"
45 #include "hw/ntc_map.h"
47 #include "cfg/cfg_ntc.h"
49 // Define logging setting (for cfg/log.h module).
50 #define LOG_LEVEL CONFIG_NTC_LOG_LEVEL
51 #define LOG_VERBOSITY CONFIG_NTC_LOG_FORMAT
53 #include <cfg/debug.h>
57 DB(bool ntc_initialized;)
60 * Find in a table of values \a orig_table of size \a size, the index which
61 * value is less or equal to \a val.
63 * \retval 0 When \a val is higher than the first table entry.
64 * \retval size When \a val is lower than the last table entry.
65 * \retval 1..size-1 When \a val is within the table.
67 static size_t upper_bound(const res_t *orig_table, size_t size, res_t val)
69 const res_t *table = orig_table;
73 size_t pos = size / 2;
83 return table - orig_table;
88 * Read the temperature for the NTC channel \a dev.
89 * First read the resistence of the NTC through ntc_hw_read(), then,
90 * for the conversion from resistance to temperature, since the formula
91 * varies from device to device, we implemented a generic system using
92 * a table of data which maps temperature (index) to resistance (data).
93 * The range of the table (min/max temperature) and the step
94 * (temperature difference between two consecutive elements of the table)
95 * is variable and can be specified. Notice that values inbetween the
96 * table elements are still possible as the library does a linear
97 * interpolation using the actual calculated resistance to find out
98 * the exact temperature.
100 * The low-level API provides a function to get access to a description
101 * of the NTC (ntc_hw_getInfo()), including the resistance table.
104 deg_t ntc_read(NtcDev dev)
106 const NtcHwInfo* hw = ntc_hw_getInfo(dev);
107 const res_t* r = hw->resistances;
113 rx = ntc_hw_read(dev);
116 i = upper_bound(r, hw->num_resistances, rx);
117 ASSERT(i <= hw->num_resistances);
119 if (i >= hw->num_resistances)
120 return NTC_SHORT_CIRCUIT;
122 return NTC_OPEN_CIRCUIT;
125 * Interpolated value in 0.1 degrees multiplied by 10:
127 * ---------- = ----------------
128 * (rx - r[i]) (r[i-1] - r [i])
131 tmp = 10 * hw->degrees_step * (rx - r[i]) / (r[i - 1] - r[i]);
134 * degrees = integer part corresponding to the superior index
135 * in the table multiplied by 10
136 * - decimal part interpolated (already multiplied by 10)
138 degrees = (i * hw->degrees_step + hw->degrees_min) * 10 - (int)(tmp);
140 //kprintf("dev= %d, I=%d, degrees = %d\n", dev, i , degrees);
152 DB(ntc_initialized = true;)